From 5cd9e0b7fb94e21d5006edc1dcfc38ed7957caa7 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Tue, 8 Jun 2021 20:21:28 +0200 Subject: [PATCH] Add fallback icon option. --- config/config.c | 4 +++- include/settings.h | 2 ++ source/dialogs/drun.c | 27 +++++++++++++++++++++++++-- source/dialogs/run.c | 30 +++++++++++++++++++++++++++--- source/xrmoptions.c | 2 ++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/config/config.c b/config/config.c index eede6718..29f10efd 100644 --- a/config/config.c +++ b/config/config.c @@ -173,5 +173,7 @@ Settings config = { /** normalize match */ .normalize_match = FALSE, /** steal focus */ - .steal_focus = FALSE + .steal_focus = FALSE, + /** fallback icon */ + .application_fallback_icon = NULL }; diff --git a/include/settings.h b/include/settings.h index e002c86f..f498bbfa 100644 --- a/include/settings.h +++ b/include/settings.h @@ -207,6 +207,8 @@ typedef struct gboolean normalize_match; /** Steal focus */ gboolean steal_focus; + /** fallback icon */ + char *application_fallback_icon; } Settings; /** Global Settings structure. */ extern Settings config; diff --git a/source/dialogs/drun.c b/source/dialogs/drun.c index fafc6068..2bb12009 100644 --- a/source/dialogs/drun.c +++ b/source/dialogs/drun.c @@ -169,6 +169,10 @@ struct _DRunModePrivateData char *old_completer_input; uint32_t selected_line; char *old_input; + + /** fallback icon */ + uint32_t fallback_icon_fetch_uid; + cairo_surface_t *fallback_icon; }; struct RegexEvalArg @@ -1242,6 +1246,17 @@ static char *_get_display_value ( const Mode *sw, unsigned int selected_line, in return retv; } +static cairo_surface_t *fallback_icon ( DRunModePrivateData *pd, int height ) +{ + if ( config.application_fallback_icon ) { + // FALLBACK + if ( pd->fallback_icon_fetch_uid > 0 ) { + return rofi_icon_fetcher_get ( pd->fallback_icon_fetch_uid ); + } + pd->fallback_icon_fetch_uid = rofi_icon_fetcher_query ( config.application_fallback_icon, height ); + } + return NULL; +} static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int height ) { DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw ); @@ -1254,10 +1269,18 @@ static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, return NULL; } if ( dr->icon_fetch_uid > 0 ) { - return rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + if ( icon ) { + return icon; + } + return fallback_icon ( pd, height ); } dr->icon_fetch_uid = rofi_icon_fetcher_query ( dr->icon_name, height ); - return rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + if ( icon ) { + return icon; + } + return fallback_icon ( pd, height ); } static char *drun_get_completion ( const Mode *sw, unsigned int index ) diff --git a/source/dialogs/run.c b/source/dialogs/run.c index 69dbe659..d2bd3428 100644 --- a/source/dialogs/run.c +++ b/source/dialogs/run.c @@ -87,6 +87,9 @@ typedef struct Mode *completer; char *old_completer_input; + /** fallback icon */ + uint32_t fallback_icon_fetch_uid; + cairo_surface_t *fallback_icon; } RunModePrivateData; /** @@ -520,6 +523,18 @@ static char *run_get_message ( const Mode *sw ) } return NULL; } +static cairo_surface_t *fallback_icon ( RunModePrivateData *pd, int height ) +{ + if ( config.application_fallback_icon ) { + // FALLBACK + if ( pd->fallback_icon_fetch_uid > 0 ) { + return rofi_icon_fetcher_get ( pd->fallback_icon_fetch_uid ); + } + pd->fallback_icon_fetch_uid = rofi_icon_fetcher_query ( config.application_fallback_icon, height ); + } + return NULL; + +} static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int height ) { RunModePrivateData *pd = (RunModePrivateData *) mode_get_private_data ( sw ); @@ -528,16 +543,25 @@ static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, } 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 ); + cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + if ( icon ) { + return icon; + } + return fallback_icon ( pd, height ); } + /** lookup icon */ 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 ); + cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid ); + if ( icon ) { + return icon; + } } - return NULL; + return fallback_icon ( pd, height ); } #include "mode-private.h" diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 0de18903..18837ae0 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -233,6 +233,8 @@ static XrmOption xrmOptions[] = { "Normalize string when matching (disables match highlighting).", CONFIG_DEFAULT }, { xrm_Boolean, "steal-focus", { .snum = &config.steal_focus }, NULL, "Steal focus on launch and restore to window that had it on rofi start on close .", CONFIG_DEFAULT }, + { xrm_String, "application-fallback-icon", { .snum = &(config.application_fallback_icon) }, NULL, + "Fallback icon to use when the application icon is not found in run/drun.", CONFIG_DEFAULT }, }; /** Dynamic array of extra options */