Skip to content

Commit

Permalink
fixup: handle XDG_DATA_DIRS correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ianyfan committed Dec 6, 2018
1 parent 01dc7ea commit 7be7bdc
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions swaybar/tray/icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7be7bdc

Please sign in to comment.