Don't recreate every string everytime.

This commit is contained in:
Dave Davenport 2017-02-03 20:49:16 +01:00
parent 2fe22cb7e2
commit 8ef7b4b794
9 changed files with 89 additions and 88 deletions

View File

@ -23,22 +23,22 @@
*/
typedef struct
{
widget widget;
unsigned long flags;
short cursor;
Color color_fg, color_bg;
char *text;
PangoLayout *layout;
int tbft;
int markup;
int changed;
widget widget;
unsigned long flags;
short cursor;
Color color_fg, color_bg;
char *text;
PangoLayout *layout;
int tbft;
int markup;
int changed;
int blink;
guint blink_timeout;
int blink;
guint blink_timeout;
PangoFontMetrics *metrics;
PangoFontMetrics *metrics;
//
const char *theme_name;
const char *theme_name;
} textbox;
/**

View File

@ -154,13 +154,13 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned
CombiModePrivateData *pd = mode_get_private_data ( sw );
if ( input[0][0] == '!' ) {
int switcher = -1;
char *eob = strchrnul ( input[0], ' ' );
int switcher = -1;
char *eob = strchrnul ( input[0], ' ' );
ssize_t bang_len = g_utf8_pointer_to_offset ( input[0], eob ) - 1;
if ( bang_len > 0 ) {
for ( unsigned i = 0; switcher == -1 && i < pd->num_switchers; i++ ) {
const char *mode_name = mode_get_name ( pd->switchers[i] );
size_t mode_name_len = g_utf8_strlen ( mode_name, -1 );
const char *mode_name = mode_get_name ( pd->switchers[i] );
size_t mode_name_len = g_utf8_strlen ( mode_name, -1 );
if ( (size_t) bang_len <= mode_name_len && utf8_strncmp ( &input[0][1], mode_name, bang_len ) == 0 ) {
switcher = i;
}
@ -168,7 +168,7 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned
}
if ( switcher >= 0 ) {
if ( eob[0] == ' ' ) {
char *n = eob+1;
char *n = eob + 1;
return mode_result ( pd->switchers[switcher], mretv, &n,
selected_line - pd->starts[switcher] );
}
@ -244,18 +244,18 @@ static char * combi_preprocess_input ( Mode *sw, const char *input )
CombiModePrivateData *pd = mode_get_private_data ( sw );
pd->current = NULL;
if ( input != NULL && input[0] == '!' ) {
char *eob = strchrnul ( input, ' ' );
char *eob = strchrnul ( input, ' ' );
ssize_t bang_len = g_utf8_pointer_to_offset ( input, eob ) - 1;
if ( bang_len > 0 ) {
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
const char *mode_name = mode_get_name ( pd->switchers[i] );
size_t mode_name_len = g_utf8_strlen ( mode_name, -1 );
const char *mode_name = mode_get_name ( pd->switchers[i] );
size_t mode_name_len = g_utf8_strlen ( mode_name, -1 );
if ( (size_t) bang_len <= mode_name_len && utf8_strncmp ( &input[1], mode_name, bang_len ) == 0 ) {
pd->current = pd->switchers[i];
if ( eob[0] == '\0' || eob[1] == '\0' ) {
return NULL;
}
return g_strdup ( eob+1 );
return g_strdup ( eob + 1 );
}
}
}

View File

@ -158,7 +158,7 @@ static void async_read_callback ( GObject *source_object, GAsyncResult *res, gpo
}
if ( !g_cancellable_is_cancelled ( pd->cancel ) ) {
// Hack, don't use get active.
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Clearing overlay");
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Clearing overlay" );
rofi_view_set_overlay ( rofi_view_get_active (), NULL );
g_input_stream_close_async ( G_INPUT_STREAM ( stream ), G_PRIORITY_LOW, pd->cancel, async_close_callback, pd );
}
@ -171,7 +171,7 @@ static void async_read_cancel ( G_GNUC_UNUSED GCancellable *cancel, G_GNUC_UNUSE
static int get_dmenu_async ( DmenuModePrivateData *pd, int sync_pre_read )
{
while(sync_pre_read-- ){
while ( sync_pre_read-- ) {
gsize len = 0;
char *data = g_data_input_stream_read_upto ( pd->data_input_stream, &( pd->separator ), 1, &len, NULL, NULL );
if ( data == NULL ) {
@ -636,7 +636,7 @@ int dmenu_switcher_dialog ( void )
}
if ( async ) {
unsigned int pre_read = 25;
find_arg_uint("-async-pre-read", &pre_read);
find_arg_uint ( "-async-pre-read", &pre_read );
async = get_dmenu_async ( pd, pre_read );
}
else {
@ -728,5 +728,5 @@ void print_dmenu_options ( void )
print_help_msg ( "-sep", "[char]", "Element separator.", "'\\n'", is_term );
print_help_msg ( "-input", "[filename]", "Read input from file instead from standard input.", NULL, is_term );
print_help_msg ( "-sync", "", "Force dmenu to first read all input data, then show dialog.", NULL, is_term );
print_help_msg ( "-async-pre-read", "[number]", "Read several entries blocking before switching to async mode", "25", is_term);
print_help_msg ( "-async-pre-read", "[number]", "Read several entries blocking before switching to async mode", "25", is_term );
}

View File

@ -87,17 +87,17 @@ static inline int execsh ( const char *wd, const char *cmd, int run_in_term )
typedef struct
{
/* Root */
char *root;
char *root;
/* Path to desktop file */
char *path;
char *path;
/* Executable */
char *exec;
char *exec;
/* Name of the Entry */
char *name;
char *name;
/* Generic Name */
char *generic_name;
char *generic_name;
GKeyFile *key_file;
GKeyFile *key_file;
} DRunModeEntry;
typedef struct
@ -288,8 +288,8 @@ static void read_desktop_file ( DRunModePrivateData *pd, const char *root, const
}
size_t nl = ( ( pd->cmd_list_length ) + 1 );
if ( nl >= pd->cmd_list_length_actual ) {
pd->cmd_list_length_actual+=256;
pd->entry_list = g_realloc ( pd->entry_list, pd->cmd_list_length_actual * sizeof ( *( pd->entry_list ) ) );
pd->cmd_list_length_actual += 256;
pd->entry_list = g_realloc ( pd->entry_list, pd->cmd_list_length_actual * sizeof ( *( pd->entry_list ) ) );
}
pd->entry_list[pd->cmd_list_length].root = g_strdup ( root );
pd->entry_list[pd->cmd_list_length].path = g_strdup ( path );

View File

@ -50,7 +50,7 @@
#include "rofi.h"
#include "view.h"
#define LOG_DOMAIN "Helper"
#define LOG_DOMAIN "Helper"
const char *const monitor_position_entries[] = {
"on focused monitor",
@ -528,11 +528,11 @@ void remove_pid_file ( int fd )
gboolean helper_validate_font ( PangoFontDescription *pfd, const char *font )
{
const char *fam = pango_font_description_get_family ( pfd );
int size = pango_font_description_get_size ( pfd );
if ( fam == NULL || size == 0 ){
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Pango failed to parse font: '%s'", font);
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got family: <b>%s</b> at size: <b>%d</b>", fam?fam:"{unknown}", size);
const char *fam = pango_font_description_get_family ( pfd );
int size = pango_font_description_get_size ( pfd );
if ( fam == NULL || size == 0 ) {
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Pango failed to parse font: '%s'", font );
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got family: <b>%s</b> at size: <b>%d</b>", fam ? fam : "{unknown}", size );
return FALSE;
}
return TRUE;

View File

@ -642,11 +642,11 @@ void __create_window ( MenuFlags menu_flags )
printf ( "xcb_create_window() failed error=0x%x\n", error->error_code );
exit ( EXIT_FAILURE );
}
TICK_N ( "xcb create window");
TICK_N ( "xcb create window" );
CacheState.gc = xcb_generate_id ( xcb->connection );
xcb_create_gc ( xcb->connection, CacheState.gc, box, 0, 0 );
TICK_N ( "xcb create gc");
TICK_N ( "xcb create gc" );
// Create a drawable.
CacheState.edit_pixmap = xcb_generate_id ( xcb->connection );
xcb_create_pixmap ( xcb->connection, depth->depth,
@ -655,7 +655,7 @@ void __create_window ( MenuFlags menu_flags )
CacheState.edit_surf = cairo_xcb_surface_create ( xcb->connection, CacheState.edit_pixmap, visual, 200, 100 );
CacheState.edit_draw = cairo_create ( CacheState.edit_surf );
TICK_N ( "create cairo surface");
TICK_N ( "create cairo surface" );
// Set up pango context.
cairo_font_options_t *fo = cairo_font_options_create ();
// Take font description from xlib surface
@ -664,7 +664,7 @@ void __create_window ( MenuFlags menu_flags )
PangoContext *p = pango_cairo_create_context ( CacheState.edit_draw );
// Set the font options from the xlib surface
pango_cairo_context_set_font_options ( p, fo );
TICK_N ( "pango cairo font setup");
TICK_N ( "pango cairo font setup" );
CacheState.main_window = box;
CacheState.flags = menu_flags;
@ -677,7 +677,7 @@ void __create_window ( MenuFlags menu_flags )
else if ( config.dpi == 0 || config.dpi == 1 ) {
// Auto-detect mode.
double dpi = 96;
if ( CacheState.mon.mh > 0 && config.dpi == 1 ) {
if ( CacheState.mon.mh > 0 && config.dpi == 1 ) {
dpi = ( CacheState.mon.h * 25.4 ) / (double) ( CacheState.mon.mh );
}
else {
@ -694,14 +694,14 @@ void __create_window ( MenuFlags menu_flags )
char *font = rofi_theme_get_string ( WIDGET ( win ), "font", config.menu_font );
if ( font ) {
PangoFontDescription *pfd = pango_font_description_from_string ( font );
if ( helper_validate_font(pfd, font)) {
if ( helper_validate_font ( pfd, font ) ) {
pango_context_set_font_description ( p, pfd );
}
pango_font_description_free ( pfd );
}
PangoLanguage *l = pango_language_get_default();
PangoLanguage *l = pango_language_get_default ();
pango_context_set_language ( p, l );
TICK_N ( "configure font");
TICK_N ( "configure font" );
// Tell textbox to use this context.
textbox_set_pango_context ( font, p );
@ -709,7 +709,7 @@ void __create_window ( MenuFlags menu_flags )
g_object_unref ( p );
cairo_font_options_destroy ( fo );
TICK_N ( "textbox setup");
TICK_N ( "textbox setup" );
// // make it an unmanaged window
if ( ( ( menu_flags & MENU_NORMAL_WINDOW ) == 0 ) ) {
window_set_atom_prop ( box, xcb->ewmh._NET_WM_STATE, &( xcb->ewmh._NET_WM_STATE_ABOVE ), 1 );
@ -721,7 +721,7 @@ void __create_window ( MenuFlags menu_flags )
x11_disable_decoration ( box );
}
TICK_N ( "setup window attributes");
TICK_N ( "setup window attributes" );
CacheState.fullscreen = rofi_theme_get_boolean ( WIDGET ( win ), "fullscreen", config.fullscreen );
if ( CacheState.fullscreen ) {
xcb_atom_t atoms[] = {
@ -731,15 +731,15 @@ void __create_window ( MenuFlags menu_flags )
window_set_atom_prop ( box, xcb->ewmh._NET_WM_STATE, atoms, sizeof ( atoms ) / sizeof ( xcb_atom_t ) );
}
TICK_N ( "setup window fullscreen");
TICK_N ( "setup window fullscreen" );
// Set the WM_NAME
xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, box, xcb->ewmh._NET_WM_NAME, xcb->ewmh.UTF8_STRING, 8, 4, "rofi" );
xcb_change_property ( xcb->connection, XCB_PROP_MODE_REPLACE, box, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, 4, "rofi" );
const char wm_class_name[] = "rofi\0Rofi";
xcb_icccm_set_wm_class ( xcb->connection, box, sizeof(wm_class_name),wm_class_name);
xcb_icccm_set_wm_class ( xcb->connection, box, sizeof ( wm_class_name ), wm_class_name );
TICK_N ( "setup window name and class");
TICK_N ( "setup window name and class" );
char *transparency = rofi_theme_get_string ( WIDGET ( win ), "transparency", NULL );
if ( transparency == NULL && config.fake_transparency ) {
transparency = config.fake_background;
@ -750,9 +750,9 @@ void __create_window ( MenuFlags menu_flags )
if ( xcb->sncontext != NULL ) {
sn_launchee_context_setup_window ( xcb->sncontext, CacheState.main_window );
}
TICK_N ( "setup startup notification");
TICK_N ( "setup startup notification" );
widget_free ( WIDGET ( win ) );
TICK_N ( "done");
TICK_N ( "done" );
}
/**
@ -1014,9 +1014,9 @@ static void rofi_view_refilter ( RofiViewState *state )
state->tokens = NULL;
}
if ( strlen ( state->text->text ) > 0 ) {
unsigned int j = 0;
unsigned int j = 0;
gchar *pattern = mode_preprocess_input ( state->sw, state->text->text );
glong plen = g_utf8_strlen ( pattern, -1 );
glong plen = g_utf8_strlen ( pattern, -1 );
state->tokens = tokenize ( pattern, config.case_sensitive );
/**
* On long lists it can be beneficial to parallelize.
@ -1597,8 +1597,8 @@ RofiViewState *rofi_view_create ( Mode *sw,
box_add ( state->main_box, WIDGET ( box ), FALSE, end ? 8 : 2 );
}
state->overlay = textbox_create ( "window.overlay", TB_AUTOWIDTH | TB_AUTOHEIGHT, URGENT, "blaat" );
state->overlay->widget.parent = WIDGET(state->main_window);
state->overlay = textbox_create ( "window.overlay", TB_AUTOWIDTH | TB_AUTOHEIGHT, URGENT, "blaat" );
state->overlay->widget.parent = WIDGET ( state->main_window );
widget_disable ( WIDGET ( state->overlay ) );
state->list_view = listview_create ( "window.mainbox.listview", update_callback, state, config.element_height, end );

View File

@ -237,12 +237,12 @@ static void listview_recompute_elements ( listview *lv )
}
lv->boxes = g_realloc ( lv->boxes, newne * sizeof ( textbox* ) );
if ( newne > 0 ) {
char *name = g_strjoin ( ".", lv->listview_name, "element", NULL );
for ( unsigned int i = lv->cur_elements; i < newne; i++ ) {
TextboxFlags flags = ( lv->multi_select ) ? TB_INDICATOR : 0;
char *name = g_strjoin ( ".", lv->listview_name, "element", NULL );
lv->boxes[i] = textbox_create ( name, flags, NORMAL, "" );
g_free ( name );
}
g_free ( name );
}
lv->rchanged = TRUE;
lv->cur_elements = newne;

View File

@ -59,7 +59,8 @@ static PangoContext *p_context = NULL;
static PangoFontMetrics *p_metrics = NULL;
/** Cache to hold font descriptions. This it to avoid having to lookup each time. */
typedef struct TBFontConfig {
typedef struct TBFontConfig
{
/** Font description */
PangoFontDescription *pfd;
/** Font metrics */
@ -72,7 +73,7 @@ static gboolean textbox_blink ( gpointer data )
{
textbox *tb = (textbox *) data;
if ( tb->blink < 2 ) {
tb->blink = !tb->blink;
tb->blink = !tb->blink;
widget_queue_redraw ( WIDGET ( tb ) );
rofi_view_queue_redraw ( );
}
@ -116,22 +117,23 @@ textbox* textbox_create ( const char *name, TextboxFlags flags, TextBoxFontType
tb->changed = FALSE;
tb->layout = pango_layout_new ( p_context );
tb->layout = pango_layout_new ( p_context );
textbox_font ( tb, tbft );
tb->metrics = p_metrics;
char * font = rofi_theme_get_string ( WIDGET ( tb ), "font", NULL );
if ( font ){
if ( font ) {
TBFontConfig *tbfc = g_hash_table_lookup ( tbfc_cache, font );
if ( tbfc == NULL ){
tbfc = g_malloc0 ( sizeof (TBFontConfig) );
if ( tbfc == NULL ) {
tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
tbfc->pfd = pango_font_description_from_string ( font );
if ( helper_validate_font ( tbfc->pfd, font)) {
if ( helper_validate_font ( tbfc->pfd, font ) ) {
tbfc->metrics = pango_context_get_metrics ( p_context, tbfc->pfd, NULL );
g_hash_table_insert ( tbfc_cache, font, tbfc);
} else {
g_hash_table_insert ( tbfc_cache, font, tbfc );
}
else {
pango_font_description_free ( tbfc->pfd );
g_free( tbfc);
g_free ( tbfc );
tbfc = NULL;
}
}
@ -142,7 +144,6 @@ textbox* textbox_create ( const char *name, TextboxFlags flags, TextBoxFontType
}
}
if ( ( flags & TB_WRAP ) == TB_WRAP ) {
pango_layout_set_wrap ( tb->layout, PANGO_WRAP_WORD_CHAR );
}
@ -328,14 +329,14 @@ static void textbox_free ( widget *wid )
static void textbox_draw ( widget *wid, cairo_t *draw )
{
textbox *tb = (textbox *) wid;
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
int font_height = textbox_get_font_height ( tb );
textbox *tb = (textbox *) wid;
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
int font_height = textbox_get_font_height ( tb );
int cursor_x = 0;
int cursor_y = 0;
int cursor_width = 2; //MAX ( 2, font_height / 10 );
int cursor_height = font_height;
int cursor_x = 0;
int cursor_y = 0;
int cursor_width = 2; //MAX ( 2, font_height / 10 );
int cursor_height = font_height;
if ( tb->changed ) {
__textbox_update_pango_text ( tb );
@ -696,25 +697,25 @@ gboolean textbox_append_char ( textbox *tb, const char *pad, const int pad_len )
static void tbfc_entry_free ( TBFontConfig *tbfc )
{
pango_font_metrics_unref ( tbfc->metrics);
if ( tbfc->pfd ){
pango_font_metrics_unref ( tbfc->metrics );
if ( tbfc->pfd ) {
pango_font_description_free ( tbfc->pfd );
}
g_free ( tbfc );
}
void textbox_setup ( void )
{
tbfc_cache = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, (GDestroyNotify)tbfc_entry_free );
tbfc_cache = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, (GDestroyNotify) tbfc_entry_free );
}
const char *default_font_name = "default";
void textbox_set_pango_context ( const char *font, PangoContext *p )
{
g_assert ( p_metrics == NULL);
g_assert ( p_metrics == NULL );
p_context = g_object_ref ( p );
p_metrics = pango_context_get_metrics ( p_context, NULL, NULL );
TBFontConfig *tbfc = g_malloc0 ( sizeof (TBFontConfig) );
TBFontConfig *tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
tbfc->metrics = p_metrics;
g_hash_table_insert ( tbfc_cache,(gpointer *)(font?font:default_font_name), tbfc );
g_hash_table_insert ( tbfc_cache, (gpointer *) ( font ? font : default_font_name ), tbfc );
}
void textbox_cleanup ( void )

View File

@ -8,14 +8,14 @@
void widget_init ( widget *widget, const char *name )
{
widget->name = g_strdup ( name );
widget->name = g_strdup ( name );
widget->def_padding = (Padding){ { WIDGET_DEFAULT_PADDING, PW_PX, SOLID }, { WIDGET_DEFAULT_PADDING, PW_PX, SOLID }, { WIDGET_DEFAULT_PADDING, PW_PX, SOLID }, { WIDGET_DEFAULT_PADDING, PW_PX, SOLID } };
widget->def_border = (Padding){ { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID } };
widget->def_margin = (Padding){ { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID }, { 0, PW_PX, SOLID } };
widget->padding = rofi_theme_get_padding ( widget, "padding", widget->def_padding );
widget->border = rofi_theme_get_padding ( widget, "border", widget->def_border );
widget->margin = rofi_theme_get_padding ( widget, "margin", widget->def_margin );
widget->border = rofi_theme_get_padding ( widget, "border", widget->def_border );
widget->margin = rofi_theme_get_padding ( widget, "margin", widget->def_margin );
}
void widget_set_state ( widget *widget, const char *state )