This commit is contained in:
Uli Schlachter 2018-02-17 13:26:23 +01:00
commit b2d1e8574f
5 changed files with 33 additions and 8 deletions

View File

@ -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()

2
draw.c
View File

@ -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);

4
draw.h
View File

@ -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);

View File

@ -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)

22
luaa.c
View File

@ -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 },