tray: tidy code

This includes some refactoring and fixing a small memory leak.
This commit is contained in:
Ian Fan 2020-03-04 14:34:32 +00:00 committed by Simon Ser
parent 3b894c387d
commit 68f53cd509
4 changed files with 17 additions and 28 deletions

View file

@ -3,16 +3,16 @@
#include "list.h" #include "list.h"
enum subdir_type {
THRESHOLD,
SCALABLE,
FIXED
};
struct icon_theme_subdir { struct icon_theme_subdir {
char *name; char *name;
int size; int size;
enum subdir_type type;
enum {
THRESHOLD,
SCALABLE,
FIXED
} type;
int max_size; int max_size;
int min_size; int min_size;
int threshold; 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 *find_icon(list_t *themes, list_t *basedirs, char *name, int size,
char *theme, int *min_size, int *max_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 #endif

View file

@ -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, static int get_registered_snis_callback(sd_bus_message *msg, void *data,
sd_bus_error *error) { sd_bus_error *error) {
if (sd_bus_message_is_method_error(msg, NULL)) { if (sd_bus_message_is_method_error(msg, NULL)) {
sd_bus_error err = *sd_bus_message_get_error(msg); const sd_bus_error *err = sd_bus_message_get_error(msg);
sway_log(SWAY_ERROR, "Failed to get registered SNIs: %s", err.message); sway_log(SWAY_ERROR, "Failed to get registered SNIs: %s", err->message);
return -sd_bus_error_get_errno(&err); return -sd_bus_error_get_errno(err);
} }
int ret = sd_bus_message_enter_container(msg, 'v', NULL); 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; struct swaybar_tray *tray = data;
for (char **id = ids; *id; ++id) { for (char **id = ids; *id; ++id) {
add_sni(tray, *id); add_sni(tray, *id);
free(*id);
} }
} }
free(ids);
return ret; return ret;
} }

View file

@ -412,7 +412,7 @@ static char *find_icon_in_subdir(char *name, char *basedir, char *theme,
#endif #endif
"png", "png",
#if HAVE_GDK_PIXBUF #if HAVE_GDK_PIXBUF
"xpm" "xpm" // deprecated
#endif #endif
}; };
@ -508,21 +508,13 @@ static char *find_icon_with_theme(list_t *basedirs, list_t *themes, char *name,
return icon; 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, static char *find_fallback_icon(list_t *basedirs, char *name, int *min_size,
int *max_size) { int *max_size) {
for (int i = 0; i < basedirs->length; ++i) { 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) { if (icon) {
*min_size = 1;
*max_size = 512;
return icon; 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, icon = find_icon_with_theme(basedirs, themes, name, size, theme,
min_size, max_size); min_size, max_size);
} }
if (!icon) { if (!icon && !(theme && strcmp(theme, "Hicolor") == 0)) {
icon = find_icon_with_theme(basedirs, themes, name, size, "Hicolor", icon = find_icon_with_theme(basedirs, themes, name, size, "Hicolor",
min_size, max_size); min_size, max_size);
} }

View file

@ -416,10 +416,6 @@ static void reload_sni(struct swaybar_sni *sni, char *icon_theme,
icon_name, target_size, icon_theme, icon_name, target_size, icon_theme,
&sni->min_size, &sni->max_size); &sni->min_size, &sni->max_size);
list_free(icon_search_paths); 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) { if (icon_path) {
cairo_surface_destroy(sni->icon); cairo_surface_destroy(sni->icon);
sni->icon = load_background_image(icon_path); sni->icon = load_background_image(icon_path);