From: Klaus Ethgen Date: Sun, 7 Mar 2010 19:04:23 +0000 (+0100) Subject: First usable lua callback X-Git-Tag: v1.2~11^2~9 X-Git-Url: http://geeqie.org/cgi-bin/gitweb.cgi?p=geeqie.git;a=commitdiff_plain;h=de4f59037f1d026e11760a7847c5b187e89351ea First usable lua callback Implementing get_datum to get a named exif datum from the image. --- diff --git a/src/glua.h b/src/glua.h index d72b47bf..410cfb68 100644 --- a/src/glua.h +++ b/src/glua.h @@ -23,9 +23,12 @@ #ifdef HAVE_LUA +#include +#include "main.h" + void lua_init(void); -gchar *lua_callvalue(gchar *, gchar *); +gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function); #endif #endif diff --git a/src/image-overlay.c b/src/image-overlay.c index e2f80b5c..13e94f4b 100644 --- a/src/image-overlay.c +++ b/src/image-overlay.c @@ -327,7 +327,7 @@ static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *v if (!tmp) break; *tmp = '\0'; - data = lua_callvalue(name+4, tmp+1); + data = lua_callvalue(imd->image_fd, name+4, tmp+1); } #endif else diff --git a/src/lua.c b/src/lua.c index 18d11f4a..469e1c6c 100644 --- a/src/lua.c +++ b/src/lua.c @@ -29,12 +29,49 @@ #include #include +#include "main.h" #include "glua.h" #include "ui_fileops.h" +#include "exif.h" static lua_State *L; /** The LUA object needed for all operations (NOTE: That is * a upper-case variable to match the documentation!) */ +static FileData *lua_check_image(lua_State *L, int index) +{ + FileData **fd; + luaL_checktype(L, index, LUA_TUSERDATA); + fd = (FileData **)luaL_checkudata(L, index, "Image"); + if (fd == NULL) luaL_typerror(L, index, "Image"); + return *fd; +} + +/* Interface for EXIF data */ +static int lua_exif_get_datum(lua_State *L) +{ + const gchar *key; + gchar *value = NULL; + ExifData *exif; + FileData *fd; + + fd = lua_check_image(L, 1); + key = luaL_checkstring(L, 2); + if (key == (gchar*)NULL || key[0] == '\0') + { + lua_pushnil(L); + return 1; + } + exif = exif_read_fd(fd); + if (!exif) + { + lua_pushnil(L); + return 1; + } + value = exif_get_data_as_text(exif, key); + lua_pushstring(L, value); + return 1; +} + /** * \brief Initialize the lua interpreter. */ @@ -42,18 +79,32 @@ void lua_init(void) { L = luaL_newstate(); luaL_openlibs(L); /* Open all libraries for lua programms */ + + /* Now create custom methodes to do something */ + static const luaL_Reg exif_methods[] = { + {"get_datum", lua_exif_get_datum}, + {NULL, NULL} + }; + luaL_register(L, "Exif", exif_methods); } /** * \brief Call a lua function to get a single value. */ -gchar *lua_callvalue(gchar *file, gchar *function) +gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function) { gint result; gchar *data = NULL; gchar *dir; gchar *path; + FileData **user_data; + user_data = (FileData **)lua_newuserdata(L, sizeof(FileData *)); + luaL_newmetatable(L, "Image"); + //luaL_getmetatable(L, "Image"); + lua_setmetatable(L, -2); + lua_setglobal(L, "Image"); + *user_data = fd; if (file[0] == '\0') { result = luaL_dostring(L, function);