From 4ecedcdb3b63087ab52e674f53aa19da3d802f9b Mon Sep 17 00:00:00 2001 From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:21:30 +0100 Subject: [PATCH] desktop-entry: separate icon and app_id icon lookup This patch splits desktop_entry_icon_lookup() into two separate functions - desktop_entry_load_icon(): load a icon from the configured icon theme - desktop_entry_load_icon_from_app_id(): load a icon name from a .desktop file based on the given app_id and supply it to _load_icon(). The _load_icon() function will be used in a future menu icon implementation whereas the _load_icon_from_app_id() function is used within the SSD titlebar window icon lookup routine. --- include/desktop-entry.h | 7 ++-- src/desktop-entry.c | 77 ++++++++++++++++++++++++----------------- src/ssd/ssd-titlebar.c | 2 +- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/include/desktop-entry.h b/include/desktop-entry.h index 75643067..ca3f77e4 100644 --- a/include/desktop-entry.h +++ b/include/desktop-entry.h @@ -7,8 +7,11 @@ struct server; void desktop_entry_init(struct server *server); void desktop_entry_finish(struct server *server); -struct lab_img *desktop_entry_icon_lookup(struct server *server, - const char *app_id, int size, float scale); +struct lab_img *desktop_entry_load_icon_from_app_id( + struct server *server, const char *app_id, int size, float scale); + +struct lab_img *desktop_entry_load_icon( + struct server *server, const char *icon_name, int size, float scale); /** * desktop_entry_name_lookup() - return the application name diff --git a/src/desktop-entry.c b/src/desktop-entry.c index c5ed846d..f28ce9cb 100644 --- a/src/desktop-entry.c +++ b/src/desktop-entry.c @@ -299,8 +299,47 @@ convert_img_type(enum sfdo_icon_file_format fmt) } struct lab_img * -desktop_entry_icon_lookup(struct server *server, const char *app_id, int size, - float scale) +desktop_entry_load_icon(struct server *server, const char *icon_name, int size, float scale) +{ + /* static analyzer isn't able to detect the NULL check in string_null_or_empty() */ + if (string_null_or_empty(icon_name) || !icon_name) { + return NULL; + } + + struct sfdo *sfdo = server->sfdo; + if (!sfdo) { + return NULL; + } + + /* + * libsfdo doesn't support loading icons for fractional scales, + * so round down and increase the icon size to compensate. + */ + int lookup_scale = MAX((int)scale, 1); + int lookup_size = lroundf(size * scale / lookup_scale); + + struct icon_ctx ctx = {0}; + int ret; + if (icon_name[0] == '/') { + ret = process_abs_name(&ctx, icon_name); + } else { + ret = process_rel_name(&ctx, icon_name, sfdo, lookup_size, lookup_scale); + } + if (ret < 0) { + wlr_log(WLR_INFO, "failed to load icon file %s", icon_name); + return NULL; + } + + wlr_log(WLR_DEBUG, "loading icon file %s", ctx.path); + struct lab_img *img = lab_img_load(convert_img_type(ctx.format), ctx.path, NULL); + + free(ctx.path); + return img; +} + +struct lab_img * +desktop_entry_load_icon_from_app_id(struct server *server, + const char *app_id, int size, float scale) { if (string_null_or_empty(app_id)) { return NULL; @@ -317,37 +356,11 @@ desktop_entry_icon_lookup(struct server *server, const char *app_id, int size, icon_name = sfdo_desktop_entry_get_icon(entry, NULL); } - /* - * libsfdo doesn't support loading icons for fractional scales, - * so round down and increase the icon size to compensate. - */ - int lookup_scale = MAX((int)scale, 1); - int lookup_size = lroundf(size * scale / lookup_scale); - - struct icon_ctx ctx = {0}; - int ret; - if (!icon_name) { - /* fall back to app id */ - ret = process_rel_name(&ctx, app_id, sfdo, lookup_size, lookup_scale); - } else if (icon_name[0] == '/') { - ret = process_abs_name(&ctx, icon_name); - } else { - /* this should be the case for most icons */ - ret = process_rel_name(&ctx, icon_name, sfdo, lookup_size, lookup_scale); - /* Icon defined in .desktop file could not be loaded, retry with app_id */ - if (ret < 0) { - ret = process_rel_name(&ctx, app_id, sfdo, lookup_size, lookup_scale); - } + struct lab_img *img = desktop_entry_load_icon(server, icon_name, size, scale); + if (!img) { + /* Icon not defined in .desktop file or could not be loaded */ + img = desktop_entry_load_icon(server, app_id, size, scale); } - if (ret < 0) { - return NULL; - } - - wlr_log(WLR_DEBUG, "loading icon file %s", ctx.path); - struct lab_img *img = lab_img_load(convert_img_type(ctx.format), ctx.path, NULL); - - free(ctx.path); - return img; } diff --git a/src/ssd/ssd-titlebar.c b/src/ssd/ssd-titlebar.c index 46ba2e21..26b5635b 100644 --- a/src/ssd/ssd-titlebar.c +++ b/src/ssd/ssd-titlebar.c @@ -609,7 +609,7 @@ ssd_update_window_icon(struct ssd *ssd) */ float icon_scale = output_max_scale(ssd->view->server); - struct lab_img *icon_img = desktop_entry_icon_lookup( + struct lab_img *icon_img = desktop_entry_load_icon_from_app_id( ssd->view->server, app_id, icon_size, icon_scale); if (!icon_img) { wlr_log(WLR_DEBUG, "icon could not be loaded for %s", app_id);