all repos — tint2 @ 8b6aad3a4150d55baa1b3c1f1c876c45b641f45e

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

Refactored svg image loading
o9000 mrovi9000@gmail.com
commit

8b6aad3a4150d55baa1b3c1f1c876c45b641f45e

parent

985c557dcda88f21063659321ca668ad1c0822e4

3 files changed, 57 insertions(+), 43 deletions(-)

jump to
M src/launcher/launcher.csrc/launcher/launcher.c

@@ -32,11 +32,6 @@ #include <glib.h>

#include <glib/gstdio.h> #include <gdk-pixbuf/gdk-pixbuf.h> #include <sys/types.h> -#include <sys/wait.h> - -#ifdef HAVE_RSVG -#include <librsvg/rsvg.h> -#endif #include "window.h" #include "server.h"

@@ -203,43 +198,8 @@ }

// 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 @@ if (!launcherIcon->image) {

// 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);
M src/util/common.csrc/util/common.c

@@ -28,9 +28,14 @@ #include <string.h>

#include <math.h> #include <unistd.h> #include <glib.h> +#include <glib/gstdio.h> #include "common.h" #include "../server.h" +#include <sys/wait.h> +#ifdef HAVE_RSVG +#include <librsvg/rsvg.h> +#endif void copy_file(const char *pathSrc, const char *pathDest)

@@ -455,3 +460,51 @@ pango_cairo_update_layout (c, layout);

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; +}
M src/util/common.hsrc/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