changeset 2199:4e1f60bc8518

First usable lua callback Implementing get_datum to get a named exif datum from the image.
author Klaus Ethgen <Klaus@Ethgen.de>
date Sun, 07 Mar 2010 20:04:23 +0100
parents a51edfa978a8
children 76c4d6774ab8
files src/glua.h src/image-overlay.c src/lua.c
diffstat 3 files changed, 57 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/glua.h	Sun Mar 07 14:57:00 2010 +0100
+++ b/src/glua.h	Sun Mar 07 20:04:23 2010 +0100
@@ -23,9 +23,12 @@
 
 #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
--- a/src/image-overlay.c	Sun Mar 07 14:57:00 2010 +0100
+++ b/src/image-overlay.c	Sun Mar 07 20:04:23 2010 +0100
@@ -327,7 +327,7 @@
 			if (!tmp)
 				break;
 			*tmp = '\0';
-			data = lua_callvalue(name+4, tmp+1);
+			data = lua_callvalue(imd->image_fd, name+4, tmp+1);
 			}
 #endif
 		else
--- a/src/lua.c	Sun Mar 07 14:57:00 2010 +0100
+++ b/src/lua.c	Sun Mar 07 20:04:23 2010 +0100
@@ -29,12 +29,49 @@
 #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 @@
 {
 	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);