dmenu: add support for fallbacks in icon metadata
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This adds support for fallback icons using comma-separated values in the icon metadata. When the primary icon is not found, subsequent icons in the list will be tried until one is successfully loaded. Example usage: Firefox\0icon\x1ffirefox,web-browser,application-x-executable
authored by e-tho · committed by Daniel Eklöf · parent 9d00e7f · Browse files · 5 files·+80 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+80 added−0 removed
- other:+80 added−0 removed
Provenance
- Commit
b789998- Tree
f541a61- Author
e-tho <etho.ledroy@pm.me>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
9d00e7f
5 files changed+80 added−0 removed
modified:doc/fuzzel.1.scd+6 added
376 unmodified lines377 *\\0icon\\x1f<icon-name>*. Example:378 379 echo -en "Firefox\\0icon\\x1ffirefox" | fuzzel --dmenu380 381+ Fallback icons using comma-separated values are also supported.382+ When the primary icon is not found, subsequent icons in the383+ list will be tried until one is successfully loaded. Example:384+ 385+ echo -en "Firefox\\0icon\\x1ffirefox,web-browser,application-x-executable" | fuzzel --dmenu386+387 *--dmenu0*388 Like *--dmenu*, but input is NUL separated instead of newline389 separated. Note that in this mode, icons are not supported.390 110 unmodified linesmodified:CHANGELOG.md+7 added
29 unmodified lines30 31 32 ## Unreleased33 ### Added34+35+* Add support for fallback icons in dmenu mode using comma-separated36+ values in the icon metadata ([#504][504]).37+38+[504]: https://codeberg.org/dnkl/fuzzel/pulls/50439+40+41 ### Changed42 43 * Number of render workers is now automatically limited to the number44 of displayed entries.923 unmodified linesmodified:dmenu.c+56 added
1 #include "dmenu.h"2 #include "column.h"3+#include "icon.h"4 5 #include <stdlib.h>6 #include <stdbool.h>7 #include <string.h>121 unmodified lines129 * an icon by appending ‘\0icon\x1f<icon-name>’ to the130 * entry:131 *132 * “hello world\0icon\x1ffirefox”133+ *134+ * We also support fallback icons using comma-separated values.135+ * When the primary icon is not found, subsequent icons in the136+ * list will be tried until one is successfully loaded:137+ *138+ * “hello world\0icon\x1ffirefox,web-browser,application-x-executable”139 */140 char *icon_name = NULL;141 const char *extra = memchr(buffer, '\0', entry_len);142 118 unmodified lines261 }262 263 return true;264 }265+266+static void267+try_icon_list(struct application *app, icon_theme_list_t themes, int icon_size)268+{269+ if (app->icon.name == NULL || strchr(app->icon.name, ',') == NULL)270+ return;271+ 272+ if (app->icon.type != ICON_NONE) {273+ return;274+ }275+ 276+ char *icon_list = xstrdup(app->icon.name);277+ char *saveptr = NULL;278+ 279+ for (char *icon_name = strtok_r(icon_list, ",", &saveptr);280+ icon_name != NULL;281+ icon_name = strtok_r(NULL, ",", &saveptr))282+ {283+ free(app->icon.name);284+ app->icon.name = xstrdup(icon_name);285+ 286+ app->icon.type = ICON_NONE;287+ app->icon.path = NULL;288+ app->icon.svg = NULL;289+ app->icon.png = NULL;290+ 291+ struct application_list temp_list = {292+ .v = &app,293+ .count = 1,294+ .visible_count = 1295+ };296+ 297+ icon_lookup_application_icons(themes, icon_size, &temp_list);298+ 299+ if (app->icon.type != ICON_NONE)300+ break;301+ }302+ 303+ free(icon_list);304+}305+306+void307+dmenu_try_icon_list(struct application_list *applications, icon_theme_list_t themes, int icon_size)308+{309+ for (size_t i = 0; i < applications->count; i++) {310+ struct application *app = applications->v[i];311+ try_icon_list(app, themes, icon_size);312+ }313+}modified:dmenu.h+3 added
2 unmodified lines3 #include <sys/types.h>4 5 #include "application.h"6 #include "config.h"7+#include "icon.h"8 #include "prompt.h"9 10 void dmenu_load_entries(11 struct application_list *applications, char delim, int event_fd, int abort_fd);12 bool dmenu_execute(const struct application *app, ssize_t index,13 const struct prompt *prompt, enum dmenu_mode format, unsigned int column);14+void dmenu_try_icon_list(struct application_list *applications,15+ icon_theme_list_t themes, int icon_size);modified:main.c+8 added
437 unmodified lines438 439 if (conf->icons_enabled) {440 icon_lookup_application_icons(441 *ctx->themes, ctx->icon_size, ctx->apps);442+443+ if (conf->dmenu.enabled) {444+ dmenu_try_icon_list(ctx->apps, *ctx->themes, ctx->icon_size);445+ }446 }447 }448 mtx_unlock(ctx->icon_lock);449 }129 unmodified lines579 *ctx->themes = icon_themes;580 if (ctx->icon_size > 0) {581 icon_lookup_application_icons(582 *ctx->themes, ctx->icon_size, apps);583+584+ if (dmenu_enabled) {585+ dmenu_try_icon_list(apps, *ctx->themes, ctx->icon_size);586+ }587 }588 }589 mtx_unlock(ctx->icon_lock);590 1488 unmodified lines