diff options
author | Dave Davenport <qball@gmpclient.org> | 2020-04-16 16:35:03 +0200 |
---|---|---|
committer | Dave Davenport <qball@gmpclient.org> | 2020-04-16 16:35:03 +0200 |
commit | 51a9bc9cf99804624fe2e59df0ad5307241d4bc1 (patch) | |
tree | bd3fbe8d2bd7eed8813169ea87dd19dfddd9986d | |
parent | 11e5646a96b203ae306c9a20e3ec2413c61bd4f0 (diff) |
Add icons to run.
-rw-r--r-- | source/dialogs/drun.c | 5 | ||||
-rw-r--r-- | source/dialogs/run.c | 120 |
2 files changed, 81 insertions, 44 deletions
diff --git a/source/dialogs/drun.c b/source/dialogs/drun.c index afc19ba6..d44c10e9 100644 --- a/source/dialogs/drun.c +++ b/source/dialogs/drun.c @@ -80,10 +80,6 @@ typedef struct char *desktop_id; /* Icon stuff */ char *icon_name; - /* Icon size is used to indicate what size is requested by the gui. - * secondary it indicates if the request for a lookup has been issued (0 not issued ) - */ - int icon_size; /* Surface holding the icon. */ cairo_surface_t *icon; /* Executable */ @@ -459,7 +455,6 @@ static void read_desktop_file ( DRunModePrivateData *pd, const char *root, const else { pd->entry_list[pd->cmd_list_length].sort_index = -nl; } - pd->entry_list[pd->cmd_list_length].icon_size = 0; pd->entry_list[pd->cmd_list_length].icon_fetch_uid = 0; pd->entry_list[pd->cmd_list_length].root = g_strdup ( root ); pd->entry_list[pd->cmd_list_length].path = g_strdup ( path ); diff --git a/source/dialogs/run.c b/source/dialogs/run.c index 5f4b43bb..2c81ca64 100644 --- a/source/dialogs/run.c +++ b/source/dialogs/run.c @@ -62,13 +62,22 @@ */ #define RUN_CACHE_FILE "rofi-3.runcache" + +typedef struct +{ + char *entry; + uint32_t icon_fetch_uid; + /* Surface holding the icon. */ + cairo_surface_t *icon; +} RunEntry; + /** * The internal data structure holding the private data of the Run Mode. */ typedef struct { /** list of available commands. */ - char **cmd_list; + RunEntry *cmd_list; /** Length of the #cmd_list. */ unsigned int cmd_list_length; /** Current mode. */ @@ -122,11 +131,11 @@ static void exec_cmd ( const char *cmd, int run_in_term ) * * Remove command from history. */ -static void delete_entry ( const char *cmd ) +static void delete_entry ( const RunEntry *cmd ) { char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL ); - history_remove ( path, cmd ); + history_remove ( path, cmd->entry ); g_free ( path ); } @@ -142,25 +151,25 @@ static void delete_entry ( const char *cmd ) */ static int sort_func ( const void *a, const void *b, G_GNUC_UNUSED void *data ) { - const char *astr = *( const char * const * ) a; - const char *bstr = *( const char * const * ) b; + const RunEntry *astr = ( const RunEntry * ) a; + const RunEntry *bstr = ( const RunEntry * ) b; - if ( astr == NULL && bstr == NULL ) { + if ( astr->entry == NULL && bstr->entry == NULL ) { return 0; } - else if ( astr == NULL ) { + else if ( astr->entry == NULL ) { return 1; } - else if ( bstr == NULL ) { + else if ( bstr->entry == NULL ) { return -1; } - return g_strcmp0 ( astr, bstr ); + return g_strcmp0 ( astr->entry , bstr->entry ); } /** * External spider to get list of executables. */ -static char ** get_apps_external ( char **retv, unsigned int *length, unsigned int num_favorites ) +static RunEntry * get_apps_external ( RunEntry *retv, unsigned int *length, unsigned int num_favorites ) { int fd = execute_generator ( config.run_list_command ); if ( fd >= 0 ) { @@ -179,7 +188,7 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i // This is a nice little penalty, but doable? time will tell. // given num_favorites is max 25. for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) { - if ( strcasecmp ( buffer, retv[j] ) == 0 ) { + if ( strcasecmp ( buffer, retv[j].entry ) == 0 ) { found = 1; } } @@ -189,8 +198,10 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i } // No duplicate, add it. - retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); - retv[( *length )] = g_strdup ( buffer ); + retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( RunEntry ) ); + retv[( *length )].entry = g_strdup ( buffer ); + retv[( *length )].icon = NULL; + retv[( *length )].icon_fetch_uid = 0; ( *length )++; } @@ -203,17 +214,19 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i } } } - retv[( *length ) ] = NULL; + retv[( *length ) ].entry = NULL; + retv[( *length ) ].icon = NULL; + retv[( *length ) ].icon_fetch_uid = 0; return retv; } /** * Internal spider used to get list of executables. */ -static char ** get_apps ( unsigned int *length ) +static RunEntry * get_apps ( unsigned int *length ) { GError *error = NULL; - char **retv = NULL; + RunEntry *retv = NULL; unsigned int num_favorites = 0; char *path; @@ -222,7 +235,12 @@ static char ** get_apps ( unsigned int *length ) } TICK_N ( "start" ); path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL ); - retv = history_get_list ( path, length ); + char **hretv = history_get_list ( path, length ); + retv = (RunEntry*)g_malloc0((*length+1)*sizeof(RunEntry)); + for(unsigned int i =0; i < *length; i++ ){ + retv[i].entry = hretv[i]; + } + g_free(hretv); g_free ( path ); // Keep track of how many where loaded as favorite. num_favorites = ( *length ); @@ -288,7 +306,7 @@ static char ** get_apps ( unsigned int *length ) // given num_favorites is max 25. int found = 0; for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) { - if ( g_strcmp0 ( name, retv[j] ) == 0 ) { + if ( g_strcmp0 ( name, retv[j].entry ) == 0 ) { found = 1; } } @@ -298,9 +316,13 @@ static char ** get_apps ( unsigned int *length ) continue; } - retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); - retv[( *length )] = name; - retv[( *length ) + 1] = NULL; + retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( RunEntry ) ); + retv[( *length )].entry = name; + retv[( *length )].icon = NULL; + retv[( *length )].icon_fetch_uid = 0; + retv[( *length ) + 1].entry = NULL; + retv[( *length ) + 1].icon = NULL; + retv[( *length ) + 1].icon_fetch_uid = 0; ( *length )++; } @@ -319,21 +341,21 @@ static char ** get_apps ( unsigned int *length ) } // TODO: check this is still fast enough. (takes 1ms on laptop.) if ( ( *length ) > num_favorites ) { - g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func, NULL ); + g_qsort_with_data ( &(retv[num_favorites]), ( *length ) - num_favorites, sizeof ( RunEntry ), sort_func, NULL ); } g_free ( path ); unsigned int removed = 0; for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) { - if ( g_strcmp0 ( retv[index], retv[index + 1] ) == 0 ) { - g_free ( retv[index] ); - retv[index] = NULL; + if ( g_strcmp0 ( retv[index].entry , retv[index + 1].entry ) == 0 ) { + g_free ( retv[index].entry ); + retv[index].entry = NULL; removed++; } } if ( ( *length ) > num_favorites ) { - g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), + g_qsort_with_data ( &(retv[num_favorites]), ( *length ) - num_favorites, sizeof ( RunEntry ), sort_func, NULL ); } @@ -351,6 +373,7 @@ static int run_mode_init ( Mode *sw ) sw->private_data = (void *) pd; pd->cmd_list = get_apps ( &( pd->cmd_list_length ) ); pd->completer = create_new_file_browser (); + mode_init ( pd->completer ); } return TRUE; @@ -359,7 +382,13 @@ static void run_mode_destroy ( Mode *sw ) { RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data; if ( rmpd != NULL ) { - g_strfreev ( rmpd->cmd_list ); + for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ){ + g_free ( rmpd->cmd_list[i].entry ); + if ( rmpd->cmd_list[i].icon != NULL ) { + cairo_surface_destroy ( rmpd->cmd_list[i].icon ); + } + } + g_free ( rmpd->cmd_list ); g_free ( rmpd->old_input ); mode_destroy ( rmpd->completer ); g_free ( rmpd ); @@ -389,8 +418,6 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in retv = RELOAD_DIALOG; - - if ( ( mretv& (MENU_COMPLETE)) ) { if ( *input ) g_free (*input); *input = NULL; @@ -406,9 +433,9 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in if ( retv == MODE_EXIT ) { if ( path == NULL ) { - exec_cmd ( rmpd->cmd_list[rmpd->selected_line], run_in_term ); + exec_cmd ( rmpd->cmd_list[rmpd->selected_line].entry, run_in_term ); } else { - char *arg= g_strdup_printf ( "%s '%s'", rmpd->cmd_list[rmpd->selected_line], path); + char *arg= g_strdup_printf ( "%s '%s'", rmpd->cmd_list[rmpd->selected_line].entry, path); printf("Comnad: %s\r\n", arg); exec_cmd ( arg, run_in_term ); g_free(arg); @@ -429,14 +456,14 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in else if ( mretv & MENU_QUICK_SWITCH ) { retv = ( mretv & MENU_LOWER_MASK ); } - else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) { - exec_cmd ( rmpd->cmd_list[selected_line], run_in_term ); + else if ( ( mretv & MENU_OK ) && selected_line < rmpd->cmd_list_length ) { + exec_cmd ( rmpd->cmd_list[selected_line].entry, run_in_term ); } else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { exec_cmd ( *input, run_in_term ); } - else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line] ) { - delete_entry ( rmpd->cmd_list[selected_line] ); + else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line].entry ) { + delete_entry ( &(rmpd->cmd_list[selected_line]) ); // Clear the list. retv = RELOAD_DIALOG; @@ -445,7 +472,7 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in } else if ( ( mretv& MENU_COMPLETE) ) { retv = RELOAD_DIALOG; - mode_init ( rmpd->completer ); + if ( selected_line < rmpd->cmd_list_length ) { rmpd->selected_line = selected_line; g_free ( rmpd->old_input ); @@ -454,7 +481,8 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in if ( *input ) g_free (*input); *input = NULL; - rmpd->file_complete = TRUE; + rmpd->file_complete = TRUE; + } } return retv; } @@ -465,7 +493,7 @@ static char *_get_display_value ( const Mode *sw, unsigned int selected_line, in if ( rmpd->file_complete ){ return rmpd->completer->_get_display_value (rmpd->completer, selected_line, state, list, get_entry ); } - return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL; + return get_entry ? g_strdup ( rmpd->cmd_list[selected_line].entry ) : NULL; } static int run_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index ) @@ -476,7 +504,7 @@ static int run_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned return rmpd->completer->_token_match (rmpd->completer, tokens, index ); } - return helper_token_match ( tokens, rmpd->cmd_list[index] ); + return helper_token_match ( tokens, rmpd->cmd_list[index].entry ); } static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int height ) { @@ -484,6 +512,20 @@ static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, if ( rmpd->file_complete ){ return rmpd->completer->_get_icon (rmpd->completer, selected_line, height ); } + else { + RunModePrivateData *pd = (RunModePrivateData *) mode_get_private_data ( sw ); + g_return_val_if_fail ( pd->cmd_list != NULL, NULL ); + RunEntry *dr = &( pd->cmd_list[selected_line] ); + if ( dr->icon_fetch_uid > 0 ) { + return rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + } + char ** str = g_strsplit(dr->entry, " ", 2); + if ( str ) { + dr->icon_fetch_uid = rofi_icon_fetcher_query ( str[0], height ); + g_strfreev ( str ); + return rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + } + } return NULL; } |