From 0afdda11f38f8ae6a3595b18b6ce9dcdd30a9909 Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Sat, 9 Sep 2017 17:09:50 +0300 Subject: [PATCH] Drop dependency on gtk3 As per discussion in #334 drop dependency on gtk3 and instead depend only on gdk-pixbuf2 for icon loading. The only gtk3 function used was gdk_cairo_set_source_pixbuf in order to convert the loaded pixbufs into cairo surfaces for us to render. In order to drop the dependency this step was bypassed by using pixbufs export to png capability and importing it into cairo via a pngstream. --- config.mk | 2 +- src/x11/x.c | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/config.mk b/config.mk index 7b26a48..68b29f2 100644 --- a/config.mk +++ b/config.mk @@ -17,7 +17,7 @@ LDFLAGS_DEBUG := pkg_config_packs := dbus-1 \ gio-2.0 \ - gdk-3.0 \ + gdk-pixbuf-2.0 \ "glib-2.0 >= 2.36" \ pangocairo \ x11 \ diff --git a/src/x11/x.c b/src/x11/x.c index dcfef35..0cef023 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -288,21 +287,29 @@ static dimension_t calculate_dimensions(GSList *layouts) return dim; } -static cairo_surface_t *gdk_pixbuf_to_cairo_surface(const GdkPixbuf *pixbuf) +struct buffer { + char *buf; + gsize bufsize; +}; + +static cairo_status_t read_from_buf(void *closure, unsigned char *data, unsigned int size) +{ + struct buffer *buf = (struct buffer *)closure; + unsigned int cpy = MIN(size, buf->bufsize); + memcpy(data, buf->buf, cpy); + buf->buf += cpy; + buf->bufsize -= cpy; + return CAIRO_STATUS_SUCCESS; +} + +static cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf) { cairo_surface_t *icon_surface = NULL; - cairo_t *cr; - cairo_format_t format; - double width, height; + struct buffer buffer; + + gdk_pixbuf_save_to_buffer(pixbuf, &buffer.buf, &buffer.bufsize, "png", NULL, NULL); + icon_surface = cairo_image_surface_create_from_png_stream(read_from_buf, &buffer); - format = gdk_pixbuf_get_has_alpha(pixbuf) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24; - width = gdk_pixbuf_get_width(pixbuf); - height = gdk_pixbuf_get_height(pixbuf); - icon_surface = cairo_image_surface_create(format, width, height); - cr = cairo_create(icon_surface); - gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); - cairo_paint(cr); - cairo_destroy(cr); return icon_surface; }