Browse Source

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 <psychon@znc.in>
Uli Schlachter 1 year ago
parent
commit
c5badcbe37
3 changed files with 27 additions and 1 deletions
  1. 1
    1
      draw.c
  2. 4
    0
      draw.h
  3. 22
    0
      luaa.c

+ 1
- 1
draw.c View File

@@ -81,7 +81,7 @@ draw_surface_from_data(int width, int height, uint32_t *data)
81 81
  * \param buf The pixbuf
82 82
  * \return Number of items pushed on the lua stack.
83 83
  */
84
-static cairo_surface_t *
84
+cairo_surface_t *
85 85
 draw_surface_from_pixbuf(GdkPixbuf *buf)
86 86
 {
87 87
     int width = gdk_pixbuf_get_width(buf);

+ 4
- 0
draw.h View File

@@ -30,6 +30,9 @@
30 30
 #include "common/array.h"
31 31
 #include "common/util.h"
32 32
 
33
+/* Forward definition */
34
+typedef struct _GdkPixbuf GdkPixbuf;
35
+
33 36
 typedef struct area_t area_t;
34 37
 struct area_t
35 38
 {
@@ -57,6 +60,7 @@ DO_ARRAY(cairo_surface_t *, cairo_surface, cairo_surface_array_destroy_surface)
57 60
 cairo_surface_t *draw_surface_from_data(int width, int height, uint32_t *data);
58 61
 cairo_surface_t *draw_dup_image_surface(cairo_surface_t *surface);
59 62
 cairo_surface_t *draw_load_image(lua_State *L, const char *path, GError **error);
63
+cairo_surface_t *draw_surface_from_pixbuf(GdkPixbuf *buf);
60 64
 
61 65
 xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual);
62 66
 xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s);

+ 22
- 0
luaa.c View File

@@ -281,6 +281,23 @@ luaA_sync(lua_State *L)
281 281
     return 0;
282 282
 }
283 283
 
284
+/** Translate a GdkPixbuf to a cairo image surface..
285
+ *
286
+ * @param pixbuf The pixbuf as a light user datum.
287
+ * @return A cairo surface as light user datum.
288
+ * @function pixbuf_to_surface
289
+ */
290
+static int
291
+luaA_pixbuf_to_surface(lua_State *L)
292
+{
293
+    GdkPixbuf *pixbuf = (GdkPixbuf *) lua_touserdata(L, 1);
294
+    cairo_surface_t *surface = draw_surface_from_pixbuf(pixbuf);
295
+
296
+    /* lua has to make sure to free the ref or we have a leak */
297
+    lua_pushlightuserdata(L, surface);
298
+    return 1;
299
+}
300
+
284 301
 /** Load an image from a given path.
285 302
  *
286 303
  * @param name The file name.
@@ -292,6 +309,10 @@ luaA_sync(lua_State *L)
292 309
 static int
293 310
 luaA_load_image(lua_State *L)
294 311
 {
312
+    /* TODO: Deprecate this function, Lua can use GdkPixbuf directly plus
313
+     * awesome.pixbuf_to_surface
314
+     */
315
+
295 316
     GError *error = NULL;
296 317
     const char *filename = luaL_checkstring(L, 1);
297 318
     cairo_surface_t *surface = draw_load_image(L, filename, &error);
@@ -776,6 +797,7 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath)
776 797
         { "emit_signal", luaA_awesome_emit_signal },
777 798
         { "systray", luaA_systray },
778 799
         { "load_image", luaA_load_image },
800
+        { "pixbuf_to_surface", luaA_pixbuf_to_surface },
779 801
         { "set_preferred_icon_size", luaA_set_preferred_icon_size },
780 802
         { "register_xproperty", luaA_register_xproperty },
781 803
         { "set_xproperty", luaA_set_xproperty },

Loading…
Cancel
Save