First usable lua callback
authorKlaus Ethgen <Klaus@Ethgen.de>
Sun, 7 Mar 2010 19:04:23 +0000 (20:04 +0100)
committerKlaus Ethgen <Klaus@Ethgen.de>
Thu, 23 Dec 2010 17:12:58 +0000 (18:12 +0100)
Implementing get_datum to get a named exif datum from the image.

src/glua.h
src/image-overlay.c
src/lua.c

index d72b47b..410cfb6 100644 (file)
 
 #ifdef HAVE_LUA
 
+#include <glib.h>
+#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
index e2f80b5..13e94f4 100644 (file)
@@ -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
index 18d11f4..469e1c6 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
 #include <stdio.h>
 #include <glib.h>
 
+#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);