From 7be7bdc5163ebb517a5354af8ad792b2933c8862 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Thu, 6 Dec 2018 17:42:23 +0000 Subject: [PATCH] fixup: handle XDG_DATA_DIRS correctly --- swaybar/tray/icon.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/swaybar/tray/icon.c b/swaybar/tray/icon.c index 8a634c16a6..b5e31f2823 100644 --- a/swaybar/tray/icon.c +++ b/swaybar/tray/icon.c @@ -16,45 +16,44 @@ static list_t *get_basedirs(void) { list_t *basedirs = create_list(); - list_add(basedirs, "$HOME/.icons"); // for backwards compatibility + list_add(basedirs, strdup("$HOME/.icons")); // for backwards compatibility char *data_home = getenv("XDG_DATA_HOME"); - list_add(basedirs, data_home && *data_home ? - "$XDG_DATA_HOME/icons" : "$HOME/.local/share/icons"); + list_add(basedirs, strdup(data_home && *data_home ? + "$XDG_DATA_HOME/icons" : "$HOME/.local/share/icons")); - list_add(basedirs, "/usr/share/pixmaps"); + list_add(basedirs, strdup("/usr/share/pixmaps")); char *data_dirs = getenv("XDG_DATA_DIRS"); - if (data_dirs && *data_dirs) { - data_dirs = strdup(data_dirs); - char *dir = strtok(data_dirs, ","); - list_add(basedirs, dir); - while ((dir = strtok(NULL, ","))) { - list_add(basedirs, dir); - } - } else { - data_dirs = NULL; - list_add(basedirs, "/usr/local/share/icons"); - list_add(basedirs, "/usr/share/icons"); + if (!(data_dirs && *data_dirs)) { + data_dirs = "/usr/local/share:/usr/share"; } + data_dirs = strdup(data_dirs); + char *dir = strtok(data_dirs, ":"); + do { + size_t path_len = snprintf(NULL, 0, "%s/icons", dir) + 1; + char *path = malloc(path_len); + snprintf(path, path_len, "%s/icons", dir); + list_add(basedirs, path); + } while ((dir = strtok(NULL, ":"))); + free(data_dirs); + list_t *basedirs_expanded = create_list(); for (int i = 0; i < basedirs->length; ++i) { wordexp_t p; if (wordexp(basedirs->items[i], &p, WRDE_UNDEF) == 0) { struct stat sb; if (stat(p.we_wordv[0], &sb) == 0 && S_ISDIR(sb.st_mode)) { - basedirs->items[i] = strdup(p.we_wordv[0]); - } else { - list_del(basedirs, i--); + list_add(basedirs_expanded, strdup(p.we_wordv[0])); } wordfree(&p); - } else { - list_del(basedirs, i--); } } - free(data_dirs); - return basedirs; + list_foreach(basedirs, free); + list_free(basedirs); + + return basedirs_expanded; } static void destroy_theme(struct icon_theme *theme) { @@ -306,6 +305,7 @@ static list_t *load_themes_in_dir(char *basedir) { } void init_themes(list_t **themes, list_t **basedirs) { + // TODO add warning message if Hicolor not found *basedirs = get_basedirs(); *themes = create_list();