Compatibility function for lua > 5.1
[geeqie.git] / src / lua.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Klaus Ethgen
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_LUA
24
25 #define _XOPEN_SOURCE
26
27 #include <lua.h>
28 #include <lauxlib.h>
29 #include <lualib.h>
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include <string.h>
34 #include <time.h>
35
36 #include "main.h"
37 #include "glua.h"
38 #include "ui_fileops.h"
39 #include "exif.h"
40
41 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
42                        * a upper-case variable to match the documentation!) */
43
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)
47 {
48         const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
49         return luaL_argerror(L, narg, msg);
50 }
51
52 # define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0);
53 # define LUA_register_global(L, string, func) \
54         lua_newtable(L); \
55         luaL_setfuncs(L, func, 0); \
56         lua_pushvalue(L, -1); \
57         lua_setglobal(L, string)
58 #else
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)
61 #endif
62
63 static FileData *lua_check_image(lua_State *L, int index)
64 {
65         FileData **fd;
66         luaL_checktype(L, index, LUA_TUSERDATA);
67         fd = (FileData **)luaL_checkudata(L, index, "Image");
68         if (fd == NULL) luaL_typerror(L, index, "Image");
69         return *fd;
70 }
71
72 static int lua_image_get_exif(lua_State *L)
73 {
74         FileData *fd;
75         ExifData *exif;
76         ExifData **exif_data;
77
78         fd = lua_check_image(L, 1);
79         exif = exif_read_fd(fd);
80
81         exif_data = (ExifData **)lua_newuserdata(L, sizeof(ExifData *));
82         luaL_getmetatable(L, "Exif");
83         lua_setmetatable(L, -2);
84
85         *exif_data = exif;
86
87         return 1;
88 }
89
90 static int lua_image_get_path(lua_State *L)
91 {
92         FileData *fd;
93
94         fd = lua_check_image(L, 1);
95         lua_pushstring(L, fd->path);
96         return 1;
97 }
98
99 static int lua_image_get_name(lua_State *L)
100 {
101         FileData *fd;
102
103         fd = lua_check_image(L, 1);
104         lua_pushstring(L, fd->name);
105         return 1;
106 }
107
108 static int lua_image_get_extension(lua_State *L)
109 {
110         FileData *fd;
111
112         fd = lua_check_image(L, 1);
113         lua_pushstring(L, fd->extension);
114         return 1;
115 }
116
117 static int lua_image_get_date(lua_State *L)
118 {
119         FileData *fd;
120
121         fd = lua_check_image(L, 1);
122         lua_pushnumber(L, fd->date);
123         return 1;
124 }
125
126 static int lua_image_get_size(lua_State *L)
127 {
128         FileData *fd;
129
130         fd = lua_check_image(L, 1);
131         lua_pushnumber(L, fd->size);
132         return 1;
133 }
134
135 static int lua_image_get_marks(lua_State *L)
136 {
137         FileData *fd;
138
139         fd = lua_check_image(L, 1);
140         lua_pushnumber(L, fd->marks);
141         return 1;
142 }
143
144 static ExifData *lua_check_exif(lua_State *L, int index)
145 {
146         ExifData **exif;
147         luaL_checktype(L, index, LUA_TUSERDATA);
148         exif = (ExifData **)luaL_checkudata(L, index, "Exif");
149         if (exif == NULL) luaL_typerror(L, index, "Exif");
150         return *exif;
151 }
152
153 /* Interface for EXIF data */
154 static int lua_exif_get_datum(lua_State *L)
155 {
156         const gchar *key;
157         gchar *value = NULL;
158         ExifData *exif;
159         struct tm tm;
160         time_t datetime;
161
162         exif = lua_check_exif(L, 1);
163         key = luaL_checkstring(L, 2);
164         if (key == (gchar*)NULL || key[0] == '\0')
165                 {
166                 lua_pushnil(L);
167                 return 1;
168                 }
169         if (!exif)
170                 {
171                 lua_pushnil(L);
172                 return 1;
173                 }
174         value = exif_get_data_as_text(exif, key);
175         if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
176                 {
177                 memset(&tm, 0, sizeof(tm));
178                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
179                         {
180                         datetime = mktime(&tm);
181                         lua_pushnumber(L, datetime);
182                         return 1;
183                         }
184                 else
185                         {
186                         lua_pushnil(L);
187                         return 1;
188                         }
189                 }
190         else if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
191                 {
192                 memset(&tm, 0, sizeof(tm));
193                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
194                         {
195                         datetime = mktime(&tm);
196                         lua_pushnumber(L, datetime);
197                         return 1;
198                         }
199                 else
200                         {
201                         lua_pushnil(L);
202                         return 1;
203                         }
204                 }
205         lua_pushstring(L, value);
206         return 1;
207 }
208
209 /**
210  * \brief Initialize the lua interpreter.
211  */
212 void lua_init(void)
213 {
214         L = luaL_newstate();
215         luaL_openlibs(L); /* Open all libraries for lua programms */
216
217         /* Now create custom methodes to do something */
218         static const luaL_Reg meta_methods[] = {
219                         {NULL, NULL}
220         };
221
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},
231                         {NULL, NULL}
232         };
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);
238         lua_settable(L, -3);
239         lua_pushliteral(L, "__metatable");
240         lua_pushvalue(L, -3);
241         lua_settable(L, -3);
242         lua_pop(L, 1);
243         lua_pop(L, 1);
244
245         /* The Exif table and methodes */
246         static const luaL_Reg exif_methods[] = {
247                         {"get_datum", lua_exif_get_datum},
248                         {NULL, NULL}
249         };
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);
255         lua_settable(L, -3);
256         lua_pushliteral(L, "__metatable");
257         lua_pushvalue(L, -3);
258         lua_settable(L, -3);
259         lua_pop(L, 1);
260         lua_pop(L, 1);
261 }
262
263 /**
264  * \brief Call a lua function to get a single value.
265  */
266 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
267 {
268         gint result;
269         gchar *data = NULL;
270         gchar *dir;
271         gchar *path;
272         FileData **image_data;
273         gchar *tmp;
274         GError *error = NULL;
275
276         /* Collection Table (Dummy at the moment) */
277         lua_newtable(L);
278         lua_setglobal(L, "Collection");
279
280         /* Current Image */
281         image_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
282         luaL_getmetatable(L, "Image");
283         lua_setmetatable(L, -2);
284         lua_setglobal(L, "Image");
285
286         *image_data = fd;
287         if (file[0] == '\0')
288                 {
289                 result = luaL_dostring(L, function);
290                 }
291         else
292                 {
293                 dir = g_build_filename(get_rc_dir(), "lua", NULL);
294                 path = g_build_filename(dir, file, NULL);
295                 result = luaL_dofile(L, path);
296                 g_free(path);
297                 g_free(dir);
298                 }
299
300         if (result)
301                 {
302                 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
303                 return data;
304                 }
305         data = g_strdup(lua_tostring(L, -1));
306         tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
307         if (error)
308                 {
309                 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
310                 g_error_free(error);
311                 }
312         else
313                 {
314                 g_free(data);
315                 data = g_strdup(tmp);
316                 } // if (error) { ... } else
317         return data;
318 }
319
320 #endif
321 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */