1 /*
2    This file is part of darktable,
3    Copyright (C) 2013-2020 darktable developers.
4 
5    darktable is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    darktable is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 #include "common/darktable.h"
22 #include "common/file_location.h"
23 #include "lua/configuration.h"
24 #include "lua/lua.h"
25 
check_version(lua_State * L)26 static int check_version(lua_State *L)
27 {
28   const char *module_name = "<unnamed module>";
29   if(!lua_isnil(L,1)) module_name = luaL_checkstring(L, 1);
30   gboolean valid = false;
31   for(int i = 2; i <= lua_gettop(L); i++)
32   {
33     lua_pushinteger(L, 1);
34     lua_gettable(L, i);
35     int major = luaL_checkinteger(L, -1);
36     lua_pop(L, 1);
37 
38     lua_pushinteger(L, 2);
39     lua_gettable(L, i);
40     int minor = luaL_checkinteger(L, -1);
41     lua_pop(L, 1);
42 
43     /*
44        patch number is not needed to check for compatibility
45        but let's take the good habits
46        lua_pushinteger(L,3);
47        lua_gettable(L,i);
48        int patch= luaL_checkinteger(L,-1);
49        lua_pop(L,1);
50      */
51     if(major == LUA_API_VERSION_MAJOR && minor <= LUA_API_VERSION_MINOR)
52     {
53       valid = true;
54     }
55   }
56   if(valid)
57   {
58     // nothing
59   }
60   else if(strlen(LUA_API_VERSION_SUFFIX) == 0)
61   {
62     luaL_error(L, "Module %s is not compatible with API %d.%d.%d", module_name, LUA_API_VERSION_MAJOR,
63                LUA_API_VERSION_MINOR, LUA_API_VERSION_PATCH);
64   }
65   else
66   {
67     dt_print(DT_DEBUG_LUA, "LUA ERROR Module %s is not compatible with API %d.%d.%d-%s\n", module_name,
68              LUA_API_VERSION_MAJOR, LUA_API_VERSION_MINOR, LUA_API_VERSION_PATCH, LUA_API_VERSION_SUFFIX);
69   }
70   return 0;
71 }
72 
73 
74 typedef enum
75 {
76   os_windows,
77   os_macos,
78   os_linux,
79   os_unix
80 } lua_os_type;
81 #if defined(_WIN32)
82 static const lua_os_type cur_os = os_windows;
83 #elif defined(__MACH__) || defined(__APPLE__)
84 static const lua_os_type cur_os = os_macos;
85 #elif defined(__linux__)
86 static const lua_os_type cur_os = os_linux;
87 #else
88 static const lua_os_type cur_os = os_unix;
89 #endif
90 
dt_lua_init_configuration(lua_State * L)91 int dt_lua_init_configuration(lua_State *L)
92 {
93   char tmp_path[PATH_MAX] = { 0 };
94 
95   dt_lua_push_darktable_lib(L);
96 
97   dt_lua_goto_subtable(L, "configuration");
98   // build the table containing the configuration info
99 
100   lua_pushstring(L, "tmp_dir");
101   dt_loc_get_tmp_dir(tmp_path, sizeof(tmp_path));
102   lua_pushstring(L, tmp_path);
103   lua_settable(L, -3);
104 
105   lua_pushstring(L, "config_dir");
106   dt_loc_get_user_config_dir(tmp_path, sizeof(tmp_path));
107   lua_pushstring(L, tmp_path);
108   lua_settable(L, -3);
109 
110   lua_pushstring(L, "cache_dir");
111   dt_loc_get_user_cache_dir(tmp_path, sizeof(tmp_path));
112   lua_pushstring(L, tmp_path);
113   lua_settable(L, -3);
114 
115   lua_pushstring(L, "version");
116   lua_pushstring(L, darktable_package_version);
117   lua_settable(L, -3);
118 
119   lua_pushstring(L, "verbose");
120   lua_pushboolean(L, darktable.unmuted & DT_DEBUG_LUA);
121   lua_settable(L, -3);
122 
123   lua_pushstring(L, "has_gui");
124   lua_pushboolean(L, darktable.gui != NULL);
125   lua_settable(L, -3);
126 
127   lua_pushstring(L, "api_version_major");
128   lua_pushinteger(L, LUA_API_VERSION_MAJOR);
129   lua_settable(L, -3);
130 
131   lua_pushstring(L, "api_version_minor");
132   lua_pushinteger(L, LUA_API_VERSION_MINOR);
133   lua_settable(L, -3);
134 
135   lua_pushstring(L, "api_version_patch");
136   lua_pushinteger(L, LUA_API_VERSION_PATCH);
137   lua_settable(L, -3);
138 
139   lua_pushstring(L, "api_version_suffix");
140   lua_pushstring(L, LUA_API_VERSION_SUFFIX);
141   lua_settable(L, -3);
142 
143   lua_pushstring(L, "api_version_string");
144   if(LUA_API_VERSION_SUFFIX[0] == '\0')
145   {
146     lua_pushfstring(L, "%d.%d.%d", LUA_API_VERSION_MAJOR, LUA_API_VERSION_MINOR, LUA_API_VERSION_PATCH);
147   }
148   else
149   {
150     lua_pushfstring(L, "%d.%d.%d-%s", LUA_API_VERSION_MAJOR, LUA_API_VERSION_MINOR, LUA_API_VERSION_PATCH,
151                     LUA_API_VERSION_SUFFIX);
152   }
153   lua_settable(L, -3);
154 
155   lua_pushstring(L, "check_version");
156   lua_pushcfunction(L, check_version);
157   lua_settable(L, -3);
158 
159   luaA_enum(L, lua_os_type);
160   luaA_enum_value_name(L, lua_os_type, os_windows, "windows");
161   luaA_enum_value_name(L, lua_os_type, os_macos, "macos");
162   luaA_enum_value_name(L, lua_os_type, os_linux, "linux");
163   luaA_enum_value_name(L, lua_os_type, os_unix, "unix");
164   lua_pushstring(L, "running_os");
165   luaA_push(L, lua_os_type, &cur_os);
166   lua_settable(L, -3);
167 
168   lua_pop(L, 1); // remove the configuration table from the stack
169   return 0;
170 }
171 
172 
173 
174 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
175 // vim: shiftwidth=2 expandtab tabstop=2 cindent
176 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
177