From 9451fec6f17b011e9703de929b9ae386afbc9db2 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 29 Jan 2012 14:08:57 +0100 Subject: [PATCH] Add awesome.load_image() This uses imlib2 for loading the image and thus supports more image formats than just PNG. Almost fixes: FS#958 Signed-off-by: Uli Schlachter --- awesomeConfig.cmake | 1 + draw.c | 67 +++++++++++++++++++++++++++++++++++++++++++++ draw.h | 1 + luaa.c | 14 ++++++++++ luadoc/awesome.lua | 6 ++++ 5 files changed, 89 insertions(+) diff --git a/awesomeConfig.cmake b/awesomeConfig.cmake index 484eb2244..c50295eca 100644 --- a/awesomeConfig.cmake +++ b/awesomeConfig.cmake @@ -144,6 +144,7 @@ pkg_check_modules(AWESOME_REQUIRED REQUIRED cairo-xcb libstartup-notification-1.0>=0.10 xproto>=7.0.15 + imlib2 libxdg-basedir>=1.0.0) if(NOT AWESOME_REQUIRED_FOUND OR NOT AWESOME_COMMON_REQUIRED_FOUND) diff --git a/draw.c b/draw.c index 16e63c466..a2dd5111f 100644 --- a/draw.c +++ b/draw.c @@ -29,6 +29,8 @@ #include #include +#include + #include "globalconf.h" #include "screen.h" @@ -160,4 +162,69 @@ draw_dup_image_surface(cairo_surface_t *surface) return res; } +static const char * +image_imlib_load_strerror(Imlib_Load_Error e) +{ + switch(e) + { + case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: + return "no such file or directory"; + case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: + return "file is a directory"; + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: + return "read permission denied"; + case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: + return "no loader for file format"; + case IMLIB_LOAD_ERROR_PATH_TOO_LONG: + return "path too long"; + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: + return "path component non existent"; + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: + return "path component not a directory"; + case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: + return "path points outside address space"; + case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: + return "too many symbolic links"; + case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: + return "out of memory"; + case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: + return "out of file descriptors"; + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: + return "write permission denied"; + case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE: + return "out of disk space"; + case IMLIB_LOAD_ERROR_UNKNOWN: + return "unknown error, that's really bad"; + case IMLIB_LOAD_ERROR_NONE: + return "no error, oops"; + } + + return "unknown error"; +} + +/** Load the specified path into a cairo surface + * \param L Lua state + * \param path file to load + * \return A cairo image surface or NULL on error. + */ +int +draw_load_image(lua_State *L, const char *path) +{ + Imlib_Image imimage; + Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE; + int ret; + + imimage = imlib_load_image_with_error_return(path, &e); + if (!imimage) { + luaL_error(L, "Cannot load image '%s': %s", path, image_imlib_load_strerror(e)); + return 0; + } + + imlib_context_set_image(imimage); + ret = luaA_surface_from_data(L, imlib_image_get_width(), + imlib_image_get_height(), imlib_image_get_data_for_reading_only()); + imlib_free_image_and_decache(); + return ret; +} + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/draw.h b/draw.h index d8a4c1cbb..c039d55bb 100644 --- a/draw.h +++ b/draw.h @@ -67,6 +67,7 @@ a_iso2utf8(const char *str, ssize_t len, char **dest, ssize_t *dlen) int luaA_surface_from_data(lua_State *L, int width, int height, uint32_t *data); cairo_surface_t *draw_dup_image_surface(cairo_surface_t *surface); +int draw_load_image(lua_State *L, const char *path); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/luaa.c b/luaa.c index 103c63fa5..ee2636601 100644 --- a/luaa.c +++ b/luaa.c @@ -102,6 +102,19 @@ luaA_restart(lua_State *L) return 0; } +/** Load an image from a given path. + * \param L The Lua VM state. + * \return The number of elements pushed on stack. + * \luastack + * \lparam The command line to execute. + */ +static int +luaA_load_image(lua_State *L) +{ + const char *filename = luaL_checkstring(L, 1); + return draw_load_image(L, filename); +} + /** UTF-8 aware string length computing. * \param L The Lua VM state. * \return The number of elements pushed on stack. @@ -516,6 +529,7 @@ luaA_init(xdgHandle* xdg) { "disconnect_signal", luaA_awesome_disconnect_signal }, { "emit_signal", luaA_awesome_emit_signal }, { "systray", luaA_systray }, + { "load_image", luaA_load_image }, { "__index", luaA_awesome_index }, { NULL, NULL } }; diff --git a/luadoc/awesome.lua b/luadoc/awesome.lua index 31a0e981b..2e509c9c2 100644 --- a/luadoc/awesome.lua +++ b/luadoc/awesome.lua @@ -35,6 +35,12 @@ module("awesome") -- @param use_sn Use startup-notification, true or false, default to true. -- @return Process ID if everything is OK, or an error string if an error occured. +--- Load an image +-- @param name The file name +-- @return An oocairo image surface +-- @name load_image +-- @class function + --- Add a global signal. -- @param name A string with the event name. -- @param func The function to call.