1 /*
2 This file is part of darktable,
3 Copyright (C) 2015-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 #include "lua/lualib.h"
19 #include "control/control.h"
20 #include "gui/accelerators.h"
21 #include "libs/lib.h"
22 #include "lua/widget/widget.h"
23 #include "views/view.h"
24
25 typedef struct position_description_t
26 {
27 const char *view;
28 uint32_t container;
29 int position;
30 } position_description_t;
31
32 typedef struct
33 {
34 char *name;
35 lua_widget widget;
36 gboolean expandable;
37 GList *position_descriptions;
38 const char **views;
39 } lua_lib_data_t;
40
expandable_wrapper(struct dt_lib_module_t * self)41 static int expandable_wrapper(struct dt_lib_module_t *self)
42 {
43 return ((lua_lib_data_t *)self->data)->expandable;
44 }
45
version_wrapper()46 static int version_wrapper()
47 {
48 return 0;
49 }
50
name_wrapper(struct dt_lib_module_t * self)51 static const char *name_wrapper(struct dt_lib_module_t *self)
52 {
53 return ((lua_lib_data_t *)self->data)->name;
54 }
55
56
gui_init_wrapper(struct dt_lib_module_t * self)57 static void gui_init_wrapper(struct dt_lib_module_t *self)
58 {
59 lua_lib_data_t *gui_data =self->data;
60 self->widget = gui_data->widget->widget;
61 }
62
gui_reset_wrapper(struct dt_lib_module_t * self)63 static void gui_reset_wrapper(struct dt_lib_module_t *self)
64 {
65 lua_lib_data_t *gui_data =self->data;
66 dt_lua_async_call_alien(dt_lua_widget_trigger_callback,
67 0, NULL,NULL,
68 LUA_ASYNC_TYPENAME,"lua_widget",gui_data->widget,
69 LUA_ASYNC_TYPENAME,"const char*","reset",
70 LUA_ASYNC_DONE);
71 }
72
gui_cleanup_wrapper(struct dt_lib_module_t * self)73 static void gui_cleanup_wrapper(struct dt_lib_module_t *self)
74 {
75 lua_lib_data_t *gui_data =self->data;
76 free(gui_data->name);
77 free(gui_data->views);
78 g_list_free(gui_data->position_descriptions);
79 free(self->data);
80 self->widget = NULL;
81 }
82
83
view_wrapper(struct dt_lib_module_t * self)84 static const char **view_wrapper(struct dt_lib_module_t *self)
85 {
86 lua_lib_data_t *gui_data = self->data;
87 return (gui_data->views);
88 }
89
get_position_description(lua_lib_data_t * gui_data,const dt_view_t * cur_view)90 static position_description_t *get_position_description(lua_lib_data_t *gui_data, const dt_view_t *cur_view)
91 {
92 for(GList *iter = gui_data->position_descriptions; iter; iter = g_list_next(iter))
93 {
94 position_description_t *position_description = (position_description_t *)iter->data;
95 if(!strcmp(position_description->view, cur_view->module_name)) return position_description;
96 }
97 return NULL;
98 }
99
container_wrapper(struct dt_lib_module_t * self)100 uint32_t container_wrapper(struct dt_lib_module_t *self)
101 {
102 const dt_view_t *cur_view = dt_view_manager_get_current_view(darktable.view_manager);
103 lua_lib_data_t *gui_data = self->data;
104 position_description_t *position_description = get_position_description(gui_data, cur_view);
105 if(position_description) return position_description->container;
106 printf("ERROR in lualib, couldn't find a container for `%s', this should never happen\n", gui_data->name);
107 return 0;
108 }
109
position_wrapper(const struct dt_lib_module_t * self)110 int position_wrapper(const struct dt_lib_module_t *self)
111 {
112 const dt_view_t *cur_view = dt_view_manager_get_current_view(darktable.view_manager);
113 lua_lib_data_t *gui_data = self->data;
114 position_description_t *position_description = get_position_description(gui_data, cur_view);
115 if(position_description) return position_description->position;
116 printf("ERROR in lualib, couldn't find a position for `%s', this should never happen\n", gui_data->name);
117 /*
118 No position found. This can happen if we are called while current view is not one
119 of our views. just return 0
120 */
121 return 0;
122 }
123
async_lib_call(lua_State * L)124 static int async_lib_call(lua_State * L)
125 {
126 //lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_libs");
127 const char* event = lua_tostring(L,1);
128 dt_lib_module_t * module = *(dt_lib_module_t**)lua_touserdata(L,2);
129 dt_lua_module_entry_push(L,"lib",module->plugin_name);
130 lua_getuservalue(L,-1);
131 lua_getfield(L,-1,event);
132 if(lua_isnoneornil(L,-1)) {
133 lua_pop(L,7);
134 return 0;
135 }
136 lua_pushvalue(L,2);
137 lua_pushvalue(L,3);
138 lua_pushvalue(L,4);
139 lua_call(L,3,0);
140 lua_pop(L,6);
141 return 0;
142 }
view_enter_wrapper(struct dt_lib_module_t * self,struct dt_view_t * old_view,struct dt_view_t * new_view)143 static void view_enter_wrapper(struct dt_lib_module_t *self,struct dt_view_t *old_view,struct dt_view_t *new_view)
144 {
145 dt_lua_async_call_alien(async_lib_call,
146 0, NULL,NULL,
147 LUA_ASYNC_TYPENAME,"const char*","view_enter",
148 LUA_ASYNC_TYPENAME,"dt_lua_lib_t",self,
149 LUA_ASYNC_TYPENAME,"dt_lua_view_t",old_view,
150 LUA_ASYNC_TYPENAME,"dt_lua_view_t",new_view,
151 LUA_ASYNC_DONE);
152 }
153
view_leave_wrapper(struct dt_lib_module_t * self,struct dt_view_t * old_view,struct dt_view_t * new_view)154 static void view_leave_wrapper(struct dt_lib_module_t *self,struct dt_view_t *old_view,struct dt_view_t *new_view)
155 {
156 dt_lua_async_call_alien(async_lib_call,
157 0, NULL,NULL,
158 LUA_ASYNC_TYPENAME,"const char*","view_leave",
159 LUA_ASYNC_TYPENAME,"dt_lua_lib_t",self,
160 LUA_ASYNC_TYPENAME,"dt_lua_view_t",old_view,
161 LUA_ASYNC_TYPENAME,"dt_lua_view_t",new_view,
162 LUA_ASYNC_DONE);
163 }
164
165 static dt_lib_module_t ref_lib = {
166 .module = NULL,
167 .data = NULL,
168 .plugin_name ={ 0 },
169 .widget = NULL,
170 .expander = NULL,
171 .version = version_wrapper,
172 .name = name_wrapper,
173 .views = view_wrapper,
174 .container = container_wrapper,
175 .expandable = expandable_wrapper,
176 .init = NULL,
177 .gui_init = gui_init_wrapper,
178 .gui_cleanup = gui_cleanup_wrapper,
179 .gui_reset = gui_reset_wrapper,
180 .gui_post_expose = NULL,
181 .mouse_leave = NULL,
182 .mouse_moved = NULL,
183 .button_released = NULL,
184 .button_pressed = NULL,
185 .scrolled = NULL,
186 .configure = NULL,
187 .position = position_wrapper,
188 .legacy_params = NULL,
189 .get_params = NULL,
190 .set_params = NULL,
191 .init_presets = NULL,
192 .init_key_accels = NULL,
193 .connect_key_accels = NULL,
194 .accel_closures = NULL,
195 .reset_button = NULL,
196 .presets_button = NULL,
197 .view_enter = view_enter_wrapper,
198 .view_leave = view_leave_wrapper,
199 };
200
register_lib(lua_State * L)201 static int register_lib(lua_State *L)
202 {
203 dt_lib_module_t *lib = malloc(sizeof(dt_lib_module_t));
204 memcpy(lib, &ref_lib, sizeof(dt_lib_module_t));
205 lib->data = calloc(1, sizeof(lua_lib_data_t));
206 lua_lib_data_t *data = lib->data;
207
208 const char *plugin_name = luaL_checkstring(L, 1);
209 g_strlcpy(lib->plugin_name, plugin_name, sizeof(lib->plugin_name));
210 dt_lua_lib_register(L, lib);
211 /* push the object on the stack to have its metadata */
212 dt_lua_module_entry_push(L,"lib",lib->plugin_name);
213 lua_getuservalue(L,-1);
214 lua_pushvalue(L, 1);
215 lua_setfield(L, -2, "plugin_name");
216 const char *name = luaL_checkstring(L, 2);
217 lua_pushvalue(L, 2);
218 lua_setfield(L, -2, "name");
219 data->name = strdup(name);
220 data->widget = NULL;
221
222 luaL_checktype(L,3,LUA_TBOOLEAN);
223 data->expandable = lua_toboolean(L,3);
224
225 luaL_checktype(L,4,LUA_TBOOLEAN);
226 if(!lua_toboolean(L,4)) {
227 lib->gui_reset = NULL;
228 }
229
230 luaL_checktype(L, 5, LUA_TTABLE);
231 lua_pushnil(L);
232 while(lua_next(L, 5))
233 {
234 dt_view_t *tmp_view;
235 luaA_to(L, dt_lua_view_t, &tmp_view, -2);
236
237 luaL_checktype(L, -1, LUA_TTABLE);
238 position_description_t *position_description = malloc(sizeof(position_description_t));
239 data->position_descriptions = g_list_append(data->position_descriptions, position_description);
240
241 position_description->view = tmp_view->module_name;
242
243 // get the container
244 lua_pushinteger(L,1);
245 lua_gettable(L,-2);
246 dt_ui_container_t container;
247 luaA_to(L,dt_ui_container_t,&container,-1);
248 lua_pop(L,1);
249 position_description->container = container;
250
251 // get the position
252 lua_pushinteger(L,2);
253 lua_gettable(L,-2);
254 position_description->position = luaL_checkinteger(L,-1);
255 lua_pop(L,1);
256
257 lua_pop(L, 1);
258 }
259 data->views = calloc(g_list_length(data->position_descriptions) + 1, sizeof(char *));
260 int i = 0;
261 for(GList *iter = data->position_descriptions; iter; iter = g_list_next(iter))
262 {
263 position_description_t *position_description = (position_description_t *)iter->data;
264 data->views[i++] = position_description->view;
265 }
266
267 lua_widget widget;
268 luaA_to(L,lua_widget,&widget,6);
269 dt_lua_widget_bind(L,widget);
270 data->widget = widget;
271
272 if(lua_isfunction(L,7)) {
273 lua_pushvalue(L, 7);
274 lua_setfield(L, -2, "view_enter");
275 } else {
276 lib->view_enter = NULL;
277 }
278
279 if(lua_isfunction(L,8)) {
280 lua_pushvalue(L, 8);
281 lua_setfield(L, -2, "view_leave");
282 } else {
283 lib->view_leave = NULL;
284 }
285
286 lua_pop(L,2);
287
288
289
290
291
292 if(lib->gui_reset)
293 {
294 dt_accel_register_lib(lib, NC_("accel", "reset lib parameters"), 0, 0);
295 }
296 if(lib->init) lib->init(lib);
297
298 lib->gui_init(lib);
299 if(lib->widget) g_object_ref(lib->widget);
300
301 darktable.lib->plugins = g_list_insert_sorted(darktable.lib->plugins, lib, dt_lib_sort_plugins);
302 dt_lib_init_presets(lib);
303 if(darktable.gui && lib->init_key_accels) lib->init_key_accels(lib);
304
305 dt_view_manager_switch_by_view(darktable.view_manager, dt_view_manager_get_current_view(darktable.view_manager));
306 return 0;
307 }
308
309
310
dt_lua_init_lualib(lua_State * L)311 int dt_lua_init_lualib(lua_State *L)
312 {
313 dt_lua_push_darktable_lib(L);
314 lua_pushstring(L, "register_lib");
315 lua_pushcfunction(L, ®ister_lib);
316 dt_lua_gtk_wrap(L);
317 lua_settable(L, -3);
318 lua_pop(L, 1);
319
320 return 0;
321 }
322
323 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
324 // vim: shiftwidth=2 expandtab tabstop=2 cindent
325 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
326