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"
43 * User API consists of the following namespaces:
45 * @link image_methods Image:@endlink basic image information
47 * <b>Collection</b>: not implemented
49 * @link exif_methods <exif-structure>:get_datum() @endlink get single exif parameter
53 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
54 * a upper-case variable to match the documentation!) */
56 /* Taking that definition from lua 5.1 source */
57 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
58 int luaL_typerror(lua_State *L, int narg, const char *tname)
60 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
61 return luaL_argerror(L, narg, msg);
64 # define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0);
65 # define LUA_register_global(L, string, func) \
67 luaL_setfuncs(L, func, 0); \
68 lua_pushvalue(L, -1); \
69 lua_setglobal(L, string)
71 # define LUA_register_meta(L, meta) luaL_register(L, NULL, meta)
72 # define LUA_register_global(L, string, func) luaL_register(L, string, func)
75 static FileData *lua_check_image(lua_State *L, int index)
78 luaL_checktype(L, index, LUA_TUSERDATA);
79 fd = (FileData **)luaL_checkudata(L, index, "Image");
80 if (fd == NULL) luaL_typerror(L, index, "Image");
85 * @brief Get exif structure of selected image
87 * @returns An #ExifData data structure containing the entire exif data
89 * To be used in conjunction with @link lua_exif_get_datum <exif-structure>:get_datum() @endlink
91 static int lua_image_get_exif(lua_State *L)
97 fd = lua_check_image(L, 1);
98 exif = exif_read_fd(fd);
100 exif_data = (ExifData **)lua_newuserdata(L, sizeof(ExifData *));
101 luaL_getmetatable(L, "Exif");
102 lua_setmetatable(L, -2);
110 * @brief Get full path of selected image
112 * @returns char The full path of the file, including filename and extension
116 static int lua_image_get_path(lua_State *L)
120 fd = lua_check_image(L, 1);
121 lua_pushstring(L, fd->path);
126 * @brief Get full filename of selected image
128 * @returns char The full filename including extension
132 static int lua_image_get_name(lua_State *L)
136 fd = lua_check_image(L, 1);
137 lua_pushstring(L, fd->name);
142 * @brief Get file extension of selected image
144 * @returns char The file extension including preceding dot
148 static int lua_image_get_extension(lua_State *L)
152 fd = lua_check_image(L, 1);
153 lua_pushstring(L, fd->extension);
158 * @brief Get file date of selected image
160 * @returns time_t The file date in Unix timestamp format.
162 * time_t - signed integer which represents the number of seconds since
163 * the start of the Unix epoch: midnight UTC of January 1, 1970
165 static int lua_image_get_date(lua_State *L)
169 fd = lua_check_image(L, 1);
170 lua_pushnumber(L, fd->date);
175 * @brief Get file size of selected image
177 * @returns integer The file size in bytes
181 static int lua_image_get_size(lua_State *L)
185 fd = lua_check_image(L, 1);
186 lua_pushnumber(L, fd->size);
191 * @brief Get marks of selected image
193 * @returns unsigned integer Bit map of marks set
195 * Bit 0 == Mark 1 etc.
199 static int lua_image_get_marks(lua_State *L)
203 fd = lua_check_image(L, 1);
204 lua_pushnumber(L, fd->marks);
208 static ExifData *lua_check_exif(lua_State *L, int index)
211 luaL_checktype(L, index, LUA_TUSERDATA);
212 exif = (ExifData **)luaL_checkudata(L, index, "Exif");
213 if (exif == NULL) luaL_typerror(L, index, "Exif");
218 * @brief Interface for EXIF data
220 * @returns <i>return</i> A single exif tag extracted from a structure output by the @link lua_image_get_exif Image:get_exif() @endlink command
223 * exif_structure = Image:get_exif(); \n
224 * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
226 * Where <i>return</i> is: \n
227 * Exif.Photo.DateTimeOriginal = signed integer time_t \n
228 * Exif.Photo.DateTimeDigitized = signed integer time_t \n
232 static int lua_exif_get_datum(lua_State *L)
240 exif = lua_check_exif(L, 1);
241 key = luaL_checkstring(L, 2);
242 if (key == (gchar*)NULL || key[0] == '\0')
252 value = exif_get_data_as_text(exif, key);
253 if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
255 memset(&tm, 0, sizeof(tm));
256 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
258 datetime = mktime(&tm);
259 lua_pushnumber(L, datetime);
268 else if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
270 memset(&tm, 0, sizeof(tm));
271 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
273 datetime = mktime(&tm);
274 lua_pushnumber(L, datetime);
283 lua_pushstring(L, value);
288 * @brief <b>Image:</b> metatable and methods \n
290 * path_name = @link lua_image_get_path Image:getpath() @endlink \n
291 * where the keyword <b>Image</b> represents the currently selected image
293 static const luaL_Reg image_methods[] = {
294 {"get_path", lua_image_get_path},
295 {"get_name", lua_image_get_name},
296 {"get_extension", lua_image_get_extension},
297 {"get_date", lua_image_get_date},
298 {"get_size", lua_image_get_size},
299 {"get_exif", lua_image_get_exif},
300 {"get_marks", lua_image_get_marks},
305 * @brief <b>exif:</b> table and methods \n
307 * @link lua_exif_get_datum <exif-structure>:get_datum() @endlink \n
308 * where <exif-structure> is the output of @link lua_image_get_exif Image:get_exif() @endlink
310 * exif_structure = Image:get_exif(); \n
311 * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
313 static const luaL_Reg exif_methods[] = {
314 {"get_datum", lua_exif_get_datum},
319 * @brief Initialize the lua interpreter.
324 luaL_openlibs(L); /* Open all libraries for lua programs */
326 /* Now create custom methodes to do something */
327 static const luaL_Reg meta_methods[] = {
331 LUA_register_global(L, "Image", image_methods);
332 luaL_newmetatable(L, "Image");
333 LUA_register_meta(L, meta_methods);
334 lua_pushliteral(L, "__index");
335 lua_pushvalue(L, -3);
337 lua_pushliteral(L, "__metatable");
338 lua_pushvalue(L, -3);
343 LUA_register_global(L, "Exif", exif_methods);
344 luaL_newmetatable(L, "Exif");
345 LUA_register_meta(L, meta_methods);
346 lua_pushliteral(L, "__index");
347 lua_pushvalue(L, -3);
349 lua_pushliteral(L, "__metatable");
350 lua_pushvalue(L, -3);
357 * @brief Call a lua function to get a single value.
359 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
364 FileData **image_data;
366 GError *error = NULL;
369 ok = access(g_build_filename(get_rc_dir(), "lua", file, NULL), R_OK);
372 path = g_build_filename(get_rc_dir(), "lua", file, NULL);
376 /** @FIXME what is the correct way to find the scripts folder? */
377 ok = access(g_build_filename("/usr/local/lib", GQ_APPNAME_LC, file, NULL), R_OK);
380 path = g_build_filename("/usr/local/lib", GQ_APPNAME_LC, file, NULL);
388 /* Collection Table (Dummy at the moment) */
390 lua_setglobal(L, "Collection");
393 image_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
394 luaL_getmetatable(L, "Image");
395 lua_setmetatable(L, -2);
396 lua_setglobal(L, "Image");
401 result = luaL_dostring(L, function);
405 result = luaL_dofile(L, path);
411 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
414 data = g_strdup(lua_tostring(L, -1));
415 tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
418 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
424 data = g_strdup(tmp);
425 } // if (error) { ... } else
429 typedef int dummy_variable;
431 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */