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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2012-01-29 14:08:57 +01:00
parent c6e92081e2
commit 9451fec6f1
5 changed files with 89 additions and 0 deletions

View File

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

67
draw.c
View File

@ -29,6 +29,8 @@
#include <ctype.h>
#include <math.h>
#include <Imlib2.h>
#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

1
draw.h
View File

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

14
luaa.c
View File

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

View File

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