diff --git a/src/launcher/launcher.c b/src/launcher/launcher.c index 2a637f1..2612991 100644 --- a/src/launcher/launcher.c +++ b/src/launcher/launcher.c @@ -32,11 +32,6 @@ #include #include #include -#include - -#ifdef HAVE_RSVG -#include -#endif #include "window.h" #include "server.h" @@ -203,43 +198,8 @@ int resize_launcher(void *obj) // Free the old files free_icon(launcherIcon->image); - launcherIcon->image = NULL; - // Load the new file and scale - launcherIcon->image = imlib_load_image_immediately(new_icon_path); -#ifdef HAVE_RSVG - if (!launcherIcon->image && g_str_has_suffix(new_icon_path, ".svg")) { - char suffix[128]; - sprintf(suffix, "tmpicon-%d.png", getpid()); - // We fork here because librsvg allocates memory like crazy - pid_t pid = fork(); - if (pid == 0) { - // Child - GError* err = NULL; - RsvgHandle* svg = rsvg_handle_new_from_file(new_icon_path, &err); - - if (err != NULL) { - fprintf(stderr, "Could not load svg image!: %s", err->message); - g_error_free(err); - launcherIcon->image = NULL; - } else { - gchar *name = g_build_filename(g_get_user_config_dir(), "tint2", suffix, NULL); - GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf(svg); - gdk_pixbuf_save(pixbuf, name, "png", NULL, NULL); - } - exit(0); - } else { - // Parent - waitpid(pid, 0, 0); - gchar *name = g_build_filename(g_get_user_config_dir(), "tint2", suffix, NULL); - launcherIcon->image = imlib_load_image_immediately_without_cache(name); - g_remove(name); - g_free(name); - } - } else -#endif - { - launcherIcon->image = imlib_load_image_immediately(new_icon_path); - } + // Load the new file + launcherIcon->image = load_image(new_icon_path, 1); // On loading error, fallback to default if (!launcherIcon->image) { free(new_icon_path); @@ -252,7 +212,7 @@ int resize_launcher(void *obj) // Loading default icon failed, draw a blank icon free(new_icon_path); } else { - // Loaded icon successfully + // Loaded icon successfully, rescale it Imlib_Image original = launcherIcon->image; launcherIcon->image = scale_icon(launcherIcon->image, launcherIcon->icon_size); free_icon(original); diff --git a/src/util/common.c b/src/util/common.c index 596130d..94c2c62 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -28,9 +28,14 @@ #include #include #include +#include #include "common.h" #include "../server.h" +#include +#ifdef HAVE_RSVG +#include +#endif void copy_file(const char *pathSrc, const char *pathDest) @@ -455,3 +460,51 @@ void draw_text(PangoLayout *layout, cairo_t *c, int posx, int posy, Color *color cairo_move_to (c, posx, posy); pango_cairo_show_layout (c, layout); } + +Imlib_Image load_image(const char *path, int cached) +{ +#ifdef HAVE_RSVG + Imlib_Image image; + if (cached) { + image = imlib_load_image_immediately(path); + } else { + image = imlib_load_image_immediately_without_cache(path); + } + if (!image && g_str_has_suffix(path, ".svg")) { + char suffix[128]; + sprintf(suffix, "tmpimage-%d.png", getpid()); + // We fork here because librsvg allocates memory like crazy + pid_t pid = fork(); + if (pid == 0) { + // Child + GError* err = NULL; + RsvgHandle* svg = rsvg_handle_new_from_file(path, &err); + + if (err != NULL) { + fprintf(stderr, "Could not load svg image!: %s", err->message); + g_error_free(err); + } else { + gchar *name = g_build_filename(g_get_user_config_dir(), "tint2", suffix, NULL); + GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf(svg); + gdk_pixbuf_save(pixbuf, name, "png", NULL, NULL); + } + exit(0); + } else { + // Parent + waitpid(pid, 0, 0); + gchar *name = g_build_filename(g_get_user_config_dir(), "tint2", suffix, NULL); + image = imlib_load_image_immediately_without_cache(name); + g_remove(name); + g_free(name); + } + } else +#endif + { + if (cached) { + image = imlib_load_image_immediately(path); + } else { + image = imlib_load_image_immediately_without_cache(path); + } + } + return image; +} diff --git a/src/util/common.h b/src/util/common.h index a59d4ad..30af0a6 100644 --- a/src/util/common.h +++ b/src/util/common.h @@ -74,5 +74,6 @@ void render_image(Drawable d, int x, int y); void draw_text(PangoLayout *layout, cairo_t *c, int posx, int posy, Color *color, int font_shadow); +Imlib_Image load_image(const char *path, int cached); #endif