Sort headers using clang-tidy
[geeqie.git] / src / lua.cc
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 "glua.h"
28
29 #include <cstdio>
30 #include <cstring>
31 #include <ctime>
32 #include <memory>
33
34 #include <glib.h>
35 #include <lua.hpp>
36
37 #include "debug.h"
38 #include "exif.h"
39 #include "filedata.h"
40 #include "main-defines.h"
41 #include "main.h"
42 #include "ui-fileops.h"
43
44 /**
45  * @file
46  * User API consists of the following namespaces:
47  *
48  * @link image_methods Image:@endlink basic image information
49  *
50  * <b>Collection</b>: not implemented
51  *
52  * @link exif_methods <exif-structure>:get_datum() @endlink get single exif parameter
53  *
54  */
55
56 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
57                        * a upper-case variable to match the documentation!) */
58
59 /* Taking that definition from lua 5.1 source */
60 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
61 int luaL_typerror(lua_State *L, int narg, const char *tname)
62 {
63         const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
64         return luaL_argerror(L, narg, msg);
65 }
66
67 # define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0);
68 # define LUA_register_global(L, string, func) \
69         lua_newtable(L); \
70         luaL_setfuncs(L, func, 0); \
71         lua_pushvalue(L, -1); \
72         lua_setglobal(L, string)
73 #else
74 # define LUA_register_meta(L, meta) luaL_register(L, NULL, meta)
75 # define LUA_register_global(L, string, func) luaL_register(L, string, func)
76 #endif
77
78 static FileData *lua_check_image(lua_State *L, int index)
79 {
80         FileData **fd;
81         luaL_checktype(L, index, LUA_TUSERDATA);
82         fd = static_cast<FileData **>(luaL_checkudata(L, index, "Image"));
83         if (fd == nullptr) luaL_typerror(L, index, "Image");
84         return *fd;
85 }
86
87 /**
88  * @brief Get exif structure of selected image
89  * @param L
90  * @returns An #ExifData data structure containing the entire exif data
91  *
92  * To be used in conjunction with @link lua_exif_get_datum <exif-structure>:get_datum() @endlink
93  */
94 static int lua_image_get_exif(lua_State *L)
95 {
96         FileData *fd;
97         ExifData *exif;
98         ExifData **exif_data;
99
100         fd = lua_check_image(L, 1);
101         exif = exif_read_fd(fd);
102
103         exif_data = static_cast<ExifData **>(lua_newuserdata(L, sizeof(ExifData *)));
104         luaL_getmetatable(L, "Exif");
105         lua_setmetatable(L, -2);
106
107         *exif_data = exif;
108
109         return 1;
110 }
111
112 /**
113  * @brief Get full path of selected image
114  * @param L
115  * @returns char The full path of the file, including filename and extension
116  *
117  *
118  */
119 static int lua_image_get_path(lua_State *L)
120 {
121         FileData *fd;
122
123         fd = lua_check_image(L, 1);
124         lua_pushstring(L, fd->path);
125         return 1;
126 }
127
128 /**
129  * @brief Get full filename of selected image
130  * @param L
131  * @returns char The full filename including extension
132  *
133  *
134  */
135 static int lua_image_get_name(lua_State *L)
136 {
137         FileData *fd;
138
139         fd = lua_check_image(L, 1);
140         lua_pushstring(L, fd->name);
141         return 1;
142 }
143
144 /**
145  * @brief Get file extension of selected image
146  * @param L
147  * @returns char The file extension including preceding dot
148  *
149  *
150  */
151 static int lua_image_get_extension(lua_State *L)
152 {
153         FileData *fd;
154
155         fd = lua_check_image(L, 1);
156         lua_pushstring(L, fd->extension);
157         return 1;
158 }
159
160 /**
161  * @brief Get file date of selected image
162  * @param L
163  * @returns time_t The file date in Unix timestamp format.
164  *
165  * time_t - signed integer which represents the number of seconds since
166  * the start of the Unix epoch: midnight UTC of January 1, 1970
167  */
168 static int lua_image_get_date(lua_State *L)
169 {
170         FileData *fd;
171
172         fd = lua_check_image(L, 1);
173         lua_pushnumber(L, fd->date);
174         return 1;
175 }
176
177 /**
178  * @brief Get file size of selected image
179  * @param L
180  * @returns integer The file size in bytes
181  *
182  *
183  */
184 static int lua_image_get_size(lua_State *L)
185 {
186         FileData *fd;
187
188         fd = lua_check_image(L, 1);
189         lua_pushnumber(L, fd->size);
190         return 1;
191 }
192
193 /**
194  * @brief Get marks of selected image
195  * @param L
196  * @returns unsigned integer Bit map of marks set
197  *
198  * Bit 0 == Mark 1 etc.
199  *
200  *
201  */
202 static int lua_image_get_marks(lua_State *L)
203 {
204         FileData *fd;
205
206         fd = lua_check_image(L, 1);
207         lua_pushnumber(L, fd->marks);
208         return 1;
209 }
210
211 static ExifData *lua_check_exif(lua_State *L, int index)
212 {
213         ExifData **exif;
214         luaL_checktype(L, index, LUA_TUSERDATA);
215         exif = static_cast<ExifData **>(luaL_checkudata(L, index, "Exif"));
216         if (exif == nullptr) luaL_typerror(L, index, "Exif");
217         return *exif;
218 }
219
220 /**
221  * @brief Interface for EXIF data
222  * @param L
223  * @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
224  *
225  * e.g. \n
226  * exif_structure = Image:get_exif(); \n
227  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
228  *
229  * Where <i>return</i> is: \n
230  * Exif.Photo.DateTimeOriginal = signed integer time_t \n
231  * Exif.Photo.DateTimeDigitized = signed integer time_t \n
232  * otherwise char
233  *
234  */
235 static int lua_exif_get_datum(lua_State *L)
236 {
237         const gchar *key;
238         gchar *value = nullptr;
239         ExifData *exif;
240         struct tm tm;
241         time_t datetime;
242
243         exif = lua_check_exif(L, 1);
244         key = luaL_checkstring(L, 2);
245         if (key == nullptr || key[0] == '\0')
246                 {
247                 lua_pushnil(L);
248                 return 1;
249                 }
250         if (!exif)
251                 {
252                 lua_pushnil(L);
253                 return 1;
254                 }
255         value = exif_get_data_as_text(exif, key);
256         if (strcmp(key, "Exif.Photo.DateTimeOriginal") == 0)
257                 {
258                 memset(&tm, 0, sizeof(tm));
259                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
260                         {
261                         datetime = mktime(&tm);
262                         lua_pushnumber(L, datetime);
263                         return 1;
264                         }
265
266                 lua_pushnil(L);
267                 return 1;
268                 }
269
270         if (strcmp(key, "Exif.Photo.DateTimeDigitized") == 0)
271                 {
272                 memset(&tm, 0, sizeof(tm));
273                 if (value && strptime(value, "%Y:%m:%d %H:%M:%S", &tm))
274                         {
275                         datetime = mktime(&tm);
276                         lua_pushnumber(L, datetime);
277                         return 1;
278                         }
279
280                 lua_pushnil(L);
281                 return 1;
282                 }
283         lua_pushstring(L, value);
284         return 1;
285 }
286
287 /**
288  * @brief  <b>Image:</b> metatable and methods \n
289  * Call by e.g. \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
292  */
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},
301                 {nullptr, nullptr}
302 };
303
304 /**
305  * @brief  <b>exif:</b> table and methods \n
306  * Call by e.g. \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
309  *
310  * exif_structure = Image:get_exif(); \n
311  * DateTimeDigitized = exif_structure:get_datum("Exif.Photo.DateTimeDigitized");
312  */
313 static const luaL_Reg exif_methods[] = {
314                 {"get_datum", lua_exif_get_datum},
315                 {nullptr, nullptr}
316 };
317
318 /**
319  * @brief Initialize the lua interpreter.
320  */
321 void lua_init()
322 {
323         L = luaL_newstate();
324         luaL_openlibs(L); /* Open all libraries for lua programs */
325
326         /* Now create custom methodes to do something */
327         static const luaL_Reg meta_methods[] = {
328                         {nullptr, nullptr}
329         };
330
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);
336         lua_settable(L, -3);
337         lua_pushliteral(L, "__metatable");
338         lua_pushvalue(L, -3);
339         lua_settable(L, -3);
340         lua_pop(L, 1);
341         lua_pop(L, 1);
342
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);
348         lua_settable(L, -3);
349         lua_pushliteral(L, "__metatable");
350         lua_pushvalue(L, -3);
351         lua_settable(L, -3);
352         lua_pop(L, 1);
353         lua_pop(L, 1);
354 }
355
356 /**
357  * @brief Call a lua function to get a single value.
358  */
359 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
360 {
361         std::unique_ptr<gchar, decltype(&g_free)> path{g_build_filename(get_rc_dir(), "lua", file, NULL), g_free};
362         if (access(path.get(), R_OK) == -1)
363                 {
364                 path.reset(g_build_filename(gq_bindir, file, NULL));
365                 if (access(path.get(), R_OK) == -1)
366                         {
367                         return g_strdup("");
368                         }
369                 }
370
371         /* Collection Table (Dummy at the moment) */
372         lua_newtable(L);
373         lua_setglobal(L, "Collection");
374
375         /* Current Image */
376         auto image_data = static_cast<FileData **>(lua_newuserdata(L, sizeof(FileData *)));
377         luaL_getmetatable(L, "Image");
378         lua_setmetatable(L, -2);
379         lua_setglobal(L, "Image");
380
381         *image_data = fd;
382
383         gint result;
384         if (file[0] == '\0')
385                 {
386                 result = luaL_dostring(L, function);
387                 }
388         else
389                 {
390                 result = luaL_dofile(L, path.get());
391                 }
392
393         if (result)
394                 {
395                 return g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
396                 }
397
398         gchar *data = g_strdup(lua_tostring(L, -1));
399         GError *error = nullptr;
400         gchar *tmp = g_locale_to_utf8(data, strlen(data), nullptr, nullptr, &error);
401         if (error)
402                 {
403                 log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
404                 g_error_free(error);
405                 }
406         else
407                 {
408                 std::swap(data, tmp);
409                 } // if (error) { ... } else
410         g_free(tmp);
411         return data;
412 }
413 #else
414 using dummy_variable = int;
415 #endif
416 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */