desktop-shell: don't segfault on invalid icon path
authorPhilipp Brüschweiler <blei42@gmail.com>
Wed, 29 Aug 2012 08:53:36 +0000 (10:53 +0200)
committerKristian Høgsberg <krh@bitplanet.net>
Wed, 29 Aug 2012 19:31:25 +0000 (15:31 -0400)
Instead draw a fallback icon and proceed as normal.

https://bugs.freedesktop.org/show_bug.cgi?id=53860

v2: coding style fixes
v3: memory leak, draw icon instead of relying on external files

clients/desktop-shell.c

index dc87e75..588dc1c 100644 (file)
@@ -472,6 +472,40 @@ panel_create(struct display *display)
        return panel;
 }
 
+static cairo_surface_t *
+load_icon_or_fallback(const char *icon)
+{
+       cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
+       cairo_t *cr;
+
+       if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS)
+               return surface;
+
+       cairo_surface_destroy(surface);
+       fprintf(stderr, "ERROR loading icon from file '%s'\n", icon);
+
+       /* draw fallback icon */
+       surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
+                                            20, 20);
+       cr = cairo_create(surface);
+
+       cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
+       cairo_paint(cr);
+
+       cairo_set_source_rgba(cr, 0, 0, 0, 1);
+       cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
+       cairo_rectangle(cr, 0, 0, 20, 20);
+       cairo_move_to(cr, 4, 4);
+       cairo_line_to(cr, 16, 16);
+       cairo_move_to(cr, 4, 16);
+       cairo_line_to(cr, 16, 4);
+       cairo_stroke(cr);
+
+       cairo_destroy(cr);
+
+       return surface;
+}
+
 static void
 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
 {
@@ -481,7 +515,7 @@ panel_add_launcher(struct panel *panel, const char *icon, const char *path)
 
        launcher = malloc(sizeof *launcher);
        memset(launcher, 0, sizeof *launcher);
-       launcher->icon = cairo_image_surface_create_from_png(icon);
+       launcher->icon = load_icon_or_fallback(icon);
        launcher->path = strdup(path);
 
        wl_array_init(&launcher->envp);