From a02ab4cf701e9ba54c8235785f2084f17905051a Mon Sep 17 00:00:00 2001 From: Gjum Date: Thu, 27 Feb 2014 14:37:03 +0100 Subject: [PATCH] Change how icons are loaded - use several icon_folders instead of a single icon_path - check all paths for the icon --- config.def.h | 4 ++-- dunstrc | 4 ++-- notification.c | 7 ------- settings.c | 6 +++--- settings.h | 2 +- x.c | 54 ++++++++++++++++++++++++++++++++++++++++---------- 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/config.def.h b/config.def.h index 42a3f9b..8fdc144 100644 --- a/config.def.h +++ b/config.def.h @@ -50,8 +50,8 @@ char *dmenu = "/usr/bin/dmenu"; char *browser = "/usr/bin/firefox"; -/* path to default icons */ -char *icon_path = "/usr/share/icons/gnome/16x16/status/"; +/* paths to default icons */ +char *icon_folders = "/usr/share/icons/gnome/16x16/status/:/usr/share/icons/gnome/16x16/devices/"; /* follow focus to different monitor and display notifications there? * possible values: diff --git a/dunstrc b/dunstrc index 4901086..28a28fb 100644 --- a/dunstrc +++ b/dunstrc @@ -141,8 +141,8 @@ # Align icons left/right/off icon_position = off - # Path to default icons. - icon_path = /usr/share/icons/gnome/16x16/status/ + # Paths to default icons. + icon_folders = /usr/share/icons/gnome/16x16/status/:/usr/share/icons/gnome/16x16/devices/ [frame] width = 3 diff --git a/notification.c b/notification.c index d806f25..42242d6 100644 --- a/notification.c +++ b/notification.c @@ -281,13 +281,6 @@ int notification_init(notification * n, int id) n->urls = notification_extract_markup_urls(&(n->body)); - /* Add icon path if relative */ - if (strlen(n->icon) > 0 && n->icon[0] != '/' && n->icon[0] != '~') { - char *new = g_strconcat(settings.icon_path, n->icon, ".png", NULL); - free(n->icon); - n->icon = new; - } - n->msg = string_replace("%a", n->appname, g_strdup(n->format)); n->msg = string_replace("%s", n->summary, n->msg); if (n->icon) { diff --git a/settings.c b/settings.c index 1eaeb44..63ffcf1 100644 --- a/settings.c +++ b/settings.c @@ -210,9 +210,9 @@ void load_settings(char *cmdline_config_path) } } - settings.icon_path = - option_get_string("global", "icon_path", "-icon_path", icon_path, - "path to default icons"); + settings.icon_folders = + option_get_string("global", "icon_folders", "-icon_folders", icon_folders, + "paths to default icons"); settings.frame_width = option_get_int("frame", "width", "-frame_width", frame_width, diff --git a/settings.h b/settings.h index 6f6e3c2..8f3726b 100644 --- a/settings.h +++ b/settings.h @@ -43,7 +43,7 @@ typedef struct _settings { char **dmenu_cmd; char *browser; enum icon_position_t icon_position; - char *icon_path; + char *icon_folders; enum follow_mode f_mode; keyboard_shortcut close_ks; keyboard_shortcut close_all_ks; diff --git a/x.c b/x.c index 5923e9d..3aff9e0 100644 --- a/x.c +++ b/x.c @@ -258,6 +258,49 @@ static dimension_t calculate_dimensions(GSList *layouts) return dim; } +static cairo_t *get_icon_surface(char *icon_path) +{ + cairo_t *icon_surface = NULL; + if (strlen(icon_path) > 0 && settings.icon_position != icons_off) { + /* absolute path? */ + if (icon_path[0] == '/' || icon_path[0] == '~') { + icon_surface = cairo_image_surface_create_from_png(icon_path); + if (cairo_surface_status(icon_surface) != CAIRO_STATUS_SUCCESS) { + cairo_surface_destroy(icon_surface); + icon_surface = NULL; + } + } + /* search in icon_folders */ + if (icon_surface == NULL) { + char *start = settings.icon_folders, + *end, *current_folder, *maybe_icon_path; + do { + end = strchr(start, ':'); + if (end == NULL) end = strchr(settings.icon_folders, '\0'); /* end = end of string */ + + current_folder = strndup(start, end - start); + maybe_icon_path = g_strconcat(current_folder, "/", icon_path, ".png", NULL); + free(current_folder); + + icon_surface = cairo_image_surface_create_from_png(maybe_icon_path); + free(maybe_icon_path); + if (cairo_surface_status(icon_surface) == CAIRO_STATUS_SUCCESS) { + return icon_surface; + } else { + cairo_surface_destroy(icon_surface); + icon_surface = NULL; + } + + start = end + 1; + } while (*(end) != '\0'); + } + if (icon_surface == NULL) + fprintf(stderr, + "Could not load icon: '%s'\n", icon_path); + } + return NULL; +} + static colored_layout *r_init_shared(cairo_t *c, notification *n) { colored_layout *cl = malloc(sizeof(colored_layout)); @@ -267,16 +310,7 @@ static colored_layout *r_init_shared(cairo_t *c, notification *n) pango_layout_set_ellipsize(cl->l, PANGO_ELLIPSIZE_MIDDLE); } - cl->icon = NULL; - if (strlen(n->icon) > 0 && settings.icon_position != icons_off) { - cl->icon = cairo_image_surface_create_from_png(n->icon); - if (cairo_surface_status(cl->icon) != CAIRO_STATUS_SUCCESS) { - cairo_surface_destroy(cl->icon); - cl->icon = NULL; - fprintf(stderr, - "Could not load icon: %s\n", n->icon); - } - } + cl->icon = get_icon_surface(n->icon); cl->fg = x_string_to_color_t(n->color_strings[ColFG]); cl->bg = x_string_to_color_t(n->color_strings[ColBG]);