From 66d3502769a119eb99220d50492649903f9dfd8a Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Sat, 1 May 2021 13:04:19 +0200 Subject: [PATCH] Search for deskop files in correct directories Signed-off-by: Felix Weilbach --- common/util.c | 6 ++++++ include/util.h | 2 ++ swaybar/desktop.c | 36 +++++++++++++++++++++++++++++++++--- swaybar/icon.c | 6 ------ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/common/util.c b/common/util.c index 13ed712bc..f014dd9cb 100644 --- a/common/util.c +++ b/common/util.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "log.h" #include "util.h" @@ -162,3 +163,8 @@ char *append_path_safe(const char *base_path, const char *append_path) { snprintf(path, path_len, "%s/%s", base_path, append_path); return path; } + +bool dir_exists(char *path) { + struct stat sb; + return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode); +} diff --git a/include/util.h b/include/util.h index 1cb1ce18d..31d783e7f 100644 --- a/include/util.h +++ b/include/util.h @@ -69,4 +69,6 @@ bool sway_set_cloexec(int fd, bool cloexec); char *append_path_safe(const char *base_path, const char *append_path); +bool dir_exists(char *path); + #endif diff --git a/swaybar/desktop.c b/swaybar/desktop.c index d2303be39..4fb2eb82a 100644 --- a/swaybar/desktop.c +++ b/swaybar/desktop.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "swaybar/icon.h" #include "desktop.h" @@ -14,9 +15,38 @@ static list_t *get_desktop_files_basedirs() { list_t *basedirs = create_list(); - // TODO: Get correct list of directories - list_add(basedirs, "/usr/share/applications"); - return basedirs; + + char *data_home = getenv("XDG_DATA_HOME"); + list_add(basedirs, + strdup(data_home && *data_home + ? "$XDG_DATA_HOME/applications" + : "$HOME/.local/share/applications")); + char *data_dirs = getenv("XDG_DATA_DIRS"); + if (!(data_dirs && *data_dirs)) { + data_dirs = "/usr/local/share:/usr/share"; + } + data_dirs = strdup(data_dirs); + char *dir = strtok(data_dirs, ":"); + do { + char *path = append_path_safe(dir, "applications"); + 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) { + if (dir_exists(p.we_wordv[0])) { + list_add(basedirs_expanded, strdup(p.we_wordv[0])); + } + wordfree(&p); + } + } + + list_free_items_and_destroy(basedirs); + + return basedirs_expanded; } static char *load_desktop_entry(const char *app_name, list_t *basedirs) { diff --git a/swaybar/icon.c b/swaybar/icon.c index ce9facac4..cd427b970 100644 --- a/swaybar/icon.c +++ b/swaybar/icon.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include "swaybar/icon.h" @@ -20,11 +19,6 @@ static int cmp_id(const void *item, const void *cmp_to) { return strcmp(item, cmp_to); } -static bool dir_exists(char *path) { - struct stat sb; - return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode); -} - static list_t *get_basedirs(void) { list_t *basedirs = create_list(); list_add(basedirs, strdup("$HOME/.icons")); // deprecated