awesome.load_image(): Return errors instead of "throwing" them

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-01-15 18:38:23 +01:00
parent 43896f68ca
commit 2e58a9c6eb
3 changed files with 19 additions and 15 deletions

15
draw.c
View File

@ -229,23 +229,18 @@ draw_dup_image_surface(cairo_surface_t *surface)
/** Load the specified path into a cairo surface /** Load the specified path into a cairo surface
* \param L Lua state * \param L Lua state
* \param path file to load * \param path file to load
* \param error A place to store an error message, if needed
* \return A cairo image surface or NULL on error. * \return A cairo image surface or NULL on error.
*/ */
cairo_surface_t * cairo_surface_t *
draw_load_image(lua_State *L, const char *path) draw_load_image(lua_State *L, const char *path, GError **error)
{ {
GError *error = NULL;
cairo_surface_t *ret; cairo_surface_t *ret;
GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, &error); GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, error);
if (!buf) { if (!buf)
luaL_where(L, 1); /* error was set above */
lua_pushstring(L, error->message);
lua_concat(L, 2);
g_error_free(error);
lua_error(L);
return NULL; return NULL;
}
ret = draw_surface_from_pixbuf(buf); ret = draw_surface_from_pixbuf(buf);
g_object_unref(buf); g_object_unref(buf);

3
draw.h
View File

@ -25,6 +25,7 @@
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include <cairo.h> #include <cairo.h>
#include <lua.h> #include <lua.h>
#include <glib.h> /* for GError */
#include "common/util.h" #include "common/util.h"
@ -70,7 +71,7 @@ a_iso2utf8(const char *str, ssize_t len, char **dest, ssize_t *dlen)
cairo_surface_t *draw_surface_from_data(int width, int height, uint32_t *data); 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_dup_image_surface(cairo_surface_t *surface);
cairo_surface_t *draw_load_image(lua_State *L, const char *path); cairo_surface_t *draw_load_image(lua_State *L, const char *path, GError **error);
xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual); 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); xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s);

16
luaa.c
View File

@ -147,16 +147,24 @@ luaA_restart(lua_State *L)
/** Load an image from a given path. /** Load an image from a given path.
* *
* @param name The file name. * @param name The file name.
* @return A cairo surface as light user datum. * @return[1] A cairo surface as light user datum.
* @return[2] nil
* @treturn[2] string Error message
* @function load_image * @function load_image
*/ */
static int static int
luaA_load_image(lua_State *L) luaA_load_image(lua_State *L)
{ {
GError *error = NULL;
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
cairo_surface_t *surface = draw_load_image(L, filename); cairo_surface_t *surface = draw_load_image(L, filename, &error);
if (!surface) if (!surface) {
return 0; lua_pushnil(L);
lua_pushstring(L, error->message);
g_error_free(error);
return 2;
}
/* lua has to make sure to free the ref or we have a leak */ /* lua has to make sure to free the ref or we have a leak */
lua_pushlightuserdata(L, surface); lua_pushlightuserdata(L, surface);
return 1; return 1;