From 68f53cd509af8812562d743a55128a1b341247f5 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Wed, 4 Mar 2020 14:34:32 +0000 Subject: [PATCH] tray: tidy code This includes some refactoring and fixing a small memory leak. --- include/swaybar/tray/icon.h | 15 +++++++-------- swaybar/tray/host.c | 8 +++++--- swaybar/tray/icon.c | 18 +++++------------- swaybar/tray/item.c | 4 ---- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/include/swaybar/tray/icon.h b/include/swaybar/tray/icon.h index 8e1d471e..3673674b 100644 --- a/include/swaybar/tray/icon.h +++ b/include/swaybar/tray/icon.h @@ -3,16 +3,16 @@ #include "list.h" -enum subdir_type { - THRESHOLD, - SCALABLE, - FIXED -}; - struct icon_theme_subdir { char *name; int size; - enum subdir_type type; + + enum { + THRESHOLD, + SCALABLE, + FIXED + } type; + int max_size; int min_size; int threshold; @@ -39,6 +39,5 @@ void finish_themes(list_t *themes, list_t *basedirs); */ char *find_icon(list_t *themes, list_t *basedirs, char *name, int size, char *theme, int *min_size, int *max_size); -char *find_icon_in_dir(char *name, char *dir, int *min_size, int *max_size); #endif diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c index 451b0896..ddf2416d 100644 --- a/swaybar/tray/host.c +++ b/swaybar/tray/host.c @@ -65,9 +65,9 @@ static int handle_sni_unregistered(sd_bus_message *msg, void *data, static int get_registered_snis_callback(sd_bus_message *msg, void *data, sd_bus_error *error) { if (sd_bus_message_is_method_error(msg, NULL)) { - sd_bus_error err = *sd_bus_message_get_error(msg); - sway_log(SWAY_ERROR, "Failed to get registered SNIs: %s", err.message); - return -sd_bus_error_get_errno(&err); + const sd_bus_error *err = sd_bus_message_get_error(msg); + sway_log(SWAY_ERROR, "Failed to get registered SNIs: %s", err->message); + return -sd_bus_error_get_errno(err); } int ret = sd_bus_message_enter_container(msg, 'v', NULL); @@ -87,9 +87,11 @@ static int get_registered_snis_callback(sd_bus_message *msg, void *data, struct swaybar_tray *tray = data; for (char **id = ids; *id; ++id) { add_sni(tray, *id); + free(*id); } } + free(ids); return ret; } diff --git a/swaybar/tray/icon.c b/swaybar/tray/icon.c index 69df3582..c426c3d4 100644 --- a/swaybar/tray/icon.c +++ b/swaybar/tray/icon.c @@ -412,7 +412,7 @@ static char *find_icon_in_subdir(char *name, char *basedir, char *theme, #endif "png", #if HAVE_GDK_PIXBUF - "xpm" + "xpm" // deprecated #endif }; @@ -508,21 +508,13 @@ static char *find_icon_with_theme(list_t *basedirs, list_t *themes, char *name, return icon; } -char *find_icon_in_dir(char *name, char *dir, int *min_size, int *max_size) { - char *icon = find_icon_in_subdir(name, dir, "", ""); - if (icon) { - *min_size = 1; - *max_size = 512; - } - return icon; - -} - static char *find_fallback_icon(list_t *basedirs, char *name, int *min_size, int *max_size) { for (int i = 0; i < basedirs->length; ++i) { - char *icon = find_icon_in_dir(name, basedirs->items[i], min_size, max_size); + char *icon = find_icon_in_subdir(name, basedirs->items[i], "", ""); if (icon) { + *min_size = 1; + *max_size = 512; return icon; } } @@ -537,7 +529,7 @@ char *find_icon(list_t *themes, list_t *basedirs, char *name, int size, icon = find_icon_with_theme(basedirs, themes, name, size, theme, min_size, max_size); } - if (!icon) { + if (!icon && !(theme && strcmp(theme, "Hicolor") == 0)) { icon = find_icon_with_theme(basedirs, themes, name, size, "Hicolor", min_size, max_size); } diff --git a/swaybar/tray/item.c b/swaybar/tray/item.c index b4238417..63578cd9 100644 --- a/swaybar/tray/item.c +++ b/swaybar/tray/item.c @@ -416,10 +416,6 @@ static void reload_sni(struct swaybar_sni *sni, char *icon_theme, icon_name, target_size, icon_theme, &sni->min_size, &sni->max_size); list_free(icon_search_paths); - if (!icon_path && sni->icon_theme_path) { - icon_path = find_icon_in_dir(icon_name, sni->icon_theme_path, - &sni->min_size, &sni->max_size); - } if (icon_path) { cairo_surface_destroy(sni->icon); sni->icon = load_background_image(icon_path);