From c5badcbe373dd1487951437977e919daf19280ca Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 18 Jan 2018 18:10:35 +0100 Subject: [PATCH 1/2] Add awesome.pixbuf_to_surface This function takes a GdkPixbuf, copies it to a cairo image surface and returns the image surface. Signed-off-by: Uli Schlachter --- draw.c | 2 +- draw.h | 4 ++++ luaa.c | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/draw.c b/draw.c index 72d3541ce..83d3d3559 100644 --- a/draw.c +++ b/draw.c @@ -81,7 +81,7 @@ draw_surface_from_data(int width, int height, uint32_t *data) * \param buf The pixbuf * \return Number of items pushed on the lua stack. */ -static cairo_surface_t * +cairo_surface_t * draw_surface_from_pixbuf(GdkPixbuf *buf) { int width = gdk_pixbuf_get_width(buf); diff --git a/draw.h b/draw.h index d2a0f7e0f..867296cfe 100644 --- a/draw.h +++ b/draw.h @@ -30,6 +30,9 @@ #include "common/array.h" #include "common/util.h" +/* Forward definition */ +typedef struct _GdkPixbuf GdkPixbuf; + typedef struct area_t area_t; struct area_t { @@ -57,6 +60,7 @@ DO_ARRAY(cairo_surface_t *, cairo_surface, cairo_surface_array_destroy_surface) cairo_surface_t *draw_surface_from_data(int width, int height, uint32_t *data); cairo_surface_t *draw_dup_image_surface(cairo_surface_t *surface); cairo_surface_t *draw_load_image(lua_State *L, const char *path, GError **error); +cairo_surface_t *draw_surface_from_pixbuf(GdkPixbuf *buf); xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual); xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s); diff --git a/luaa.c b/luaa.c index 8a5b0f65c..29c7ec06a 100644 --- a/luaa.c +++ b/luaa.c @@ -281,6 +281,23 @@ luaA_sync(lua_State *L) return 0; } +/** Translate a GdkPixbuf to a cairo image surface.. + * + * @param pixbuf The pixbuf as a light user datum. + * @return A cairo surface as light user datum. + * @function pixbuf_to_surface + */ +static int +luaA_pixbuf_to_surface(lua_State *L) +{ + GdkPixbuf *pixbuf = (GdkPixbuf *) lua_touserdata(L, 1); + cairo_surface_t *surface = draw_surface_from_pixbuf(pixbuf); + + /* lua has to make sure to free the ref or we have a leak */ + lua_pushlightuserdata(L, surface); + return 1; +} + /** Load an image from a given path. * * @param name The file name. @@ -292,6 +309,10 @@ luaA_sync(lua_State *L) static int luaA_load_image(lua_State *L) { + /* TODO: Deprecate this function, Lua can use GdkPixbuf directly plus + * awesome.pixbuf_to_surface + */ + GError *error = NULL; const char *filename = luaL_checkstring(L, 1); cairo_surface_t *surface = draw_load_image(L, filename, &error); @@ -776,6 +797,7 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath) { "emit_signal", luaA_awesome_emit_signal }, { "systray", luaA_systray }, { "load_image", luaA_load_image }, + { "pixbuf_to_surface", luaA_pixbuf_to_surface }, { "set_preferred_icon_size", luaA_set_preferred_icon_size }, { "register_xproperty", luaA_register_xproperty }, { "set_xproperty", luaA_set_xproperty }, From ee944f4da8342187c2244edb01b90c54567129c4 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 18 Jan 2018 18:19:07 +0100 Subject: [PATCH 2/2] gears.surface: Use GdkPixbuf This now does directly what previously awesome.load_image() did. Also, this commit removes the only caller of awesome.load_image(), so that function could (in theory) be removed now. Fixes: https://github.com/awesomeWM/awesome/issues/1235 Signed-off-by: Uli Schlachter --- build-utils/lgi-check.c | 2 +- lib/gears/surface.lua | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/build-utils/lgi-check.c b/build-utils/lgi-check.c index 9d7a2261e..c0e3bfcd2 100644 --- a/build-utils/lgi-check.c +++ b/build-utils/lgi-check.c @@ -35,7 +35,7 @@ const char commands[] = " '0.8.0', require('lgi.version')))\n" "end\n" "lgi = require('lgi')\n" -"assert(lgi.cairo, lgi.Pango, lgi.PangoCairo, lgi.GLib, lgi.Gio)\n" +"assert(lgi.cairo, lgi.Pango, lgi.PangoCairo, lgi.GLib, lgi.Gio, lgi.GdkPixbuf)\n" ; int main() diff --git a/lib/gears/surface.lua b/lib/gears/surface.lua index 3e506b9ba..24d3a0002 100644 --- a/lib/gears/surface.lua +++ b/lib/gears/surface.lua @@ -8,6 +8,7 @@ local setmetatable = setmetatable local type = type local capi = { awesome = awesome } local cairo = require("lgi").cairo +local GdkPixbuf = require("lgi").GdkPixbuf local color = nil local gdebug = require("gears.debug") local hierarchy = require("wibox.hierarchy") @@ -36,7 +37,6 @@ end -- @return The loaded surface, or the replacement default -- @return An error message, or nil on success function surface.load_uncached_silently(_surface, default) - local file -- On nil, return some sane default if not _surface then return get_default(default) @@ -47,12 +47,11 @@ function surface.load_uncached_silently(_surface, default) end -- Strings are assumed to be file names and get loaded if type(_surface) == "string" then - local err - file = _surface - _surface, err = capi.awesome.load_image(file) - if not _surface then - return get_default(default), err + local pixbuf, err = GdkPixbuf.Pixbuf.new_from_file(_surface) + if not pixbuf then + return get_default(default), tostring(err) end + _surface = capi.awesome.pixbuf_to_surface(pixbuf._native) end -- Everything else gets forced into a surface return cairo.Surface(_surface, true)