2 * Copyright (C) 2008 - 2016 The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 #include "ui_fileops.h"
41 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
42 * a upper-case variable to match the documentation!) */
44 /* Taking that definition from lua 5.1 source */
45 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
46 int luaL_typerror(lua_State *L, int narg, const char *tname)
48 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
49 return luaL_argerror(L, narg, msg);
52 # define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0);
53 # define LUA_register_global(L, string, func) \
55 luaL_setfuncs(L, func, 0); \
56 lua_pushvalue(L, -1); \
57 lua_setglobal(L, string)
59 # define LUA_register_meta(L, meta) luaL_register(L, NULL, meta)
60 # define LUA_register_global(L, string, func) luaL_register(L, string, func)
63 static FileData *lua_check_image(lua_State *L, int index)
66 luaL_checktype(L, index, LUA_TUSERDATA);
67 fd = (FileData **)luaL_checkudata(L, index, "Image");
68 if (fd == NULL) luaL_typerror(L, index, "Image");
72 static int lua_image_get_exif(lua_State *L)
78 fd = lua_check_image(L, 1);
79 exif = exif_read_fd(fd);
81 exif_data = (ExifData **)lua_newuserdata(L, sizeof(ExifData *));
82 luaL_getmetatable(L, "Exif");
83 lua_setmetatable(L, -2);
90 static int lua_image_get_path(lua_State *L)
94 fd = lua_check_image(L, 1);
95 lua_pushstring(L, fd->path);
99 static int lua_image_get_name(lua_State *L)
103 fd = lua_check_image(L, 1);
104 lua_pushstring(L, fd->name);
108 static int lua_image_get_extension(lua_State *L)
112 fd = lua_check_image(L, 1);
113 lua_pushstring(L, fd->extension);
117 static int lua_image_get_date(lua_State *L)
121 fd = lua_check_image(L, 1);
122 lua_pushnumber(L, fd->date);
126 static int lua_image_get_size(lua_State *L)
130 fd = lua_check_image(L, 1);
131 lua_pushnumber(L, fd->size);
135 static int lua_image_get_marks(lua_State *L)
139 fd = lua_check_image(L, 1);
140 lua_pushnumber(L, fd->marks);
144 static ExifData *lua_check_exif(lua_State *L, int index)
147 luaL_checktype(L, index, LUA_TUSERDATA);
148 exif = (ExifData **)luaL_checkudata(L, index, "Exif");
149 if (exif == NULL) luaL_typerror(L, index, "Exif");
153 /* Interface for EXIF data */
154 static int lua_exif_get_datum(lua_State *L)
162 exif = lua_check_exif(L, 1);
163 key = luaL_checkstring(L, 2);
164 if (key == (gchar*)NULL || key[0] == '\0')
174 value = exif_get_data_as_text(exif, key);
175 if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
177 memset(&tm, 0, sizeof(tm));
178 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
180 datetime = mktime(&tm);
181 lua_pushnumber(L, datetime);
190 else if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
192 memset(&tm, 0, sizeof(tm));
193 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
195 datetime = mktime(&tm);
196 lua_pushnumber(L, datetime);
205 lua_pushstring(L, value);
210 * \brief Initialize the lua interpreter.
215 luaL_openlibs(L); /* Open all libraries for lua programms */
217 /* Now create custom methodes to do something */
218 static const luaL_Reg meta_methods[] = {
222 /* The Image metatable and methodes */
223 static const luaL_Reg image_methods[] = {
224 {"get_path", lua_image_get_path},
225 {"get_name", lua_image_get_name},
226 {"get_extension", lua_image_get_extension},
227 {"get_date", lua_image_get_date},
228 {"get_size", lua_image_get_size},
229 {"get_exif", lua_image_get_exif},
230 {"get_marks", lua_image_get_marks},
233 LUA_register_global(L, "Image", image_methods);
234 luaL_newmetatable(L, "Image");
235 LUA_register_meta(L, meta_methods);
236 lua_pushliteral(L, "__index");
237 lua_pushvalue(L, -3);
239 lua_pushliteral(L, "__metatable");
240 lua_pushvalue(L, -3);
245 /* The Exif table and methodes */
246 static const luaL_Reg exif_methods[] = {
247 {"get_datum", lua_exif_get_datum},
250 LUA_register_global(L, "Exif", exif_methods);
251 luaL_newmetatable(L, "Exif");
252 LUA_register_meta(L, meta_methods);
253 lua_pushliteral(L, "__index");
254 lua_pushvalue(L, -3);
256 lua_pushliteral(L, "__metatable");
257 lua_pushvalue(L, -3);
264 * \brief Call a lua function to get a single value.
266 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
272 FileData **image_data;
274 GError *error = NULL;
276 /* Collection Table (Dummy at the moment) */
278 lua_setglobal(L, "Collection");
281 image_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
282 luaL_getmetatable(L, "Image");
283 lua_setmetatable(L, -2);
284 lua_setglobal(L, "Image");
289 result = luaL_dostring(L, function);
293 dir = g_build_filename(get_rc_dir(), "lua", NULL);
294 path = g_build_filename(dir, file, NULL);
295 result = luaL_dofile(L, path);
302 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
305 data = g_strdup(lua_tostring(L, -1));
306 tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
309 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
315 data = g_strdup(tmp);
316 } // if (error) { ... } else
321 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */