469e1c6ca9514a1fe812e8e0338f84ec11eae007
[geeqie.git] / src / lua.c
1 /** \file
2  * \short LUA implementation
3  * \author Klaus Ethgen <Klaus@Ethgen.de>
4  */
5
6 /*
7  *  This file is a part of Geeqie project (http://geeqie.sourceforge.net/).
8  *  Copyright (C) 2008 - 2010 The Geeqie Team
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the Free
12  *  Software Foundation; either version 2 of the License, or (at your option)
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  *  more details.
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_LUA
24
25 #include <lua.h>
26 #include <lauxlib.h>
27 #include <lualib.h>
28
29 #include <stdio.h>
30 #include <glib.h>
31
32 #include "main.h"
33 #include "glua.h"
34 #include "ui_fileops.h"
35 #include "exif.h"
36
37 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
38                        * a upper-case variable to match the documentation!) */
39
40 static FileData *lua_check_image(lua_State *L, int index)
41 {
42         FileData **fd;
43         luaL_checktype(L, index, LUA_TUSERDATA);
44         fd = (FileData **)luaL_checkudata(L, index, "Image");
45         if (fd == NULL) luaL_typerror(L, index, "Image");
46         return *fd;
47 }
48
49 /* Interface for EXIF data */
50 static int lua_exif_get_datum(lua_State *L)
51 {
52         const gchar *key;
53         gchar *value = NULL;
54         ExifData *exif;
55         FileData *fd;
56
57         fd = lua_check_image(L, 1);
58         key = luaL_checkstring(L, 2);
59         if (key == (gchar*)NULL || key[0] == '\0')
60                 {
61                 lua_pushnil(L);
62                 return 1;
63                 }
64         exif = exif_read_fd(fd);
65         if (!exif)
66                 {
67                 lua_pushnil(L);
68                 return 1;
69                 }
70         value = exif_get_data_as_text(exif, key);
71         lua_pushstring(L, value);
72         return 1;
73 }
74
75 /**
76  * \brief Initialize the lua interpreter.
77  */
78 void lua_init(void)
79 {
80         L = luaL_newstate();
81         luaL_openlibs(L); /* Open all libraries for lua programms */
82
83         /* Now create custom methodes to do something */
84         static const luaL_Reg exif_methods[] = {
85                         {"get_datum", lua_exif_get_datum},
86                         {NULL, NULL}
87         };
88         luaL_register(L, "Exif", exif_methods);
89 }
90
91 /**
92  * \brief Call a lua function to get a single value.
93  */
94 gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
95 {
96         gint result;
97         gchar *data = NULL;
98         gchar *dir;
99         gchar *path;
100         FileData **user_data;
101
102         user_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
103         luaL_newmetatable(L, "Image");
104         //luaL_getmetatable(L, "Image");
105         lua_setmetatable(L, -2);
106         lua_setglobal(L, "Image");
107         *user_data = fd;
108         if (file[0] == '\0')
109                 {
110                 result = luaL_dostring(L, function);
111                 }
112         else
113                 {
114                 dir = g_build_filename(get_rc_dir(), "lua", NULL);
115                 path = g_build_filename(dir, file, NULL);
116                 result = luaL_dofile(L, path);
117                 g_free(path);
118                 g_free(dir);
119                 }
120
121         if (result)
122                 {
123                 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
124                 return data;
125                 }
126         data = g_strdup(lua_tostring(L, -1));
127         return data;
128 }
129
130 #endif
131 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */