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