forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.c
More file actions
428 lines (381 loc) · 14.3 KB
/
Copy pathevents.c
File metadata and controls
428 lines (381 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
This file is part of darktable,
copyright (c) 2012 Jeremy Rosen
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/darktable.h"
#include "common/file_location.h"
#include "control/control.h"
#include "control/jobs/control_jobs.h"
#include "lua/dt_lua.h"
#include "lua/events.h"
#include "lua/image.h"
#include "gui/accelerators.h"
#include "common/imageio_module.h"
typedef struct event_handler{
const char* evt_name;
lua_CFunction on_register;
int (*on_event)(lua_State * L,const char* evt_name, int nargs,int nresults);
} event_handler;
static int dt_lua_trigger_event(const char*event,int nargs,int nresults);
static int register_singleton_event(lua_State* L) {
// 1 is the event name (checked)
// 2 is the action to perform (checked)
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,lua_tostring(L,1));
if(!lua_isnil(L,-1)) {
lua_pop(L,2);
return luaL_error(L,"an action has already been registered for event %s",lua_tostring(L,1));
}
lua_pop(L,1);
lua_pushvalue(L,2);
lua_setfield(L,-2,lua_tostring(L,1));
lua_setfield(L,-2,"action");
lua_pushvalue(L,3);
lua_setfield(L,-2,"data");
lua_pop(L,1);
return 0;
}
static int trigger_singleton_event(lua_State * L,const char* evt_name, int nargs,int nresults) {
// -1..-n are our args
const int top_marker=lua_gettop(L);
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,evt_name);
if(lua_isnil(L,-1)) {
lua_pop(L,2+nargs);
if(nresults == LUA_MULTRET) return 0;
for(int i = 0 ; i < nresults ; i++) lua_pushnil(L);
return nresults;
}
// prepare the call
lua_insert(L,top_marker-nargs+1); // function to call
lua_pushstring(L,evt_name);// param 1 is the event
lua_insert(L,top_marker-nargs+2);
lua_pop(L,1);
return dt_lua_do_chunk(L,0,nargs+2,nresults);
}
static int register_keyed_event(lua_State* L) {
// 1 is the event name (checked)
// 2 is the action to perform (checked)
// 3 is the key (unchecked at this point)
if(lua_isnoneornil(L,3))
return luaL_error(L,"no key provided when registering event");
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,lua_tostring(L,1));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_newtable(L);
lua_pushvalue(L,-1);
lua_setfield(L,-3,lua_tostring(L,1));
}
lua_getfield(L,-1,luaL_checkstring(L,3));
if(!lua_isnil(L,-1))
return luaL_error(L,"key already registerd for event : %s",luaL_checkstring(L,3));
lua_pop(L,1);
lua_pushvalue(L,2);
lua_setfield(L,-2,luaL_checkstring(L,3));
lua_pop(L,2);
return 0;
}
static int trigger_keyed_event(lua_State * L,const char* evt_name, int nargs,int nresults) {
// -1..-n are our args
const int top_marker=lua_gettop(L);
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,evt_name);
if(lua_isnil(L,-1)) {
lua_pop(L,2+nargs);
if(nresults == LUA_MULTRET) return 0;
for(int i = 0 ; i < nresults ; i++) lua_pushnil(L);
return nresults;
}
const char*key=luaL_checkstring(L,-nargs-2); // first arg is the key itself
lua_getfield(L,-1,key);
if(lua_isnil(L,-1)) {
lua_pop(L,2+nargs);
if(nresults == LUA_MULTRET) return 0;
for(int i = 0 ; i < nresults ; i++) lua_pushnil(L);
return nresults;
}
// prepare the call
lua_insert(L,top_marker-nargs+1);
lua_pushstring(L,evt_name);// param 1 is the event
lua_insert(L,top_marker-nargs+2);
lua_pop(L,2);
return dt_lua_do_chunk(L,0,nargs+1,nresults);
}
static gboolean shortcut_callback(GtkAccelGroup *accel_group,
GObject *acceleratable,
guint keyval,
GdkModifierType modifier,
gpointer p)
{
lua_pushstring(darktable.lua_state,(char*)p);
dt_lua_trigger_event("shortcut",1,0);
return TRUE;
}
static void closure_destroy(gpointer data,GClosure *closure) {
free(data);
}
static int register_shortcut_event(lua_State* L) {
// 1 is the event name (checked)
// 2 is the action to perform (checked)
// 3 is the key itself
int result = register_keyed_event(L); // will raise an error in case of duplicate key
char tmp[1024];
snprintf(tmp,1024,"lua/%s\n",luaL_checkstring(L,3));
dt_accel_register_global(tmp,0,0);
dt_accel_connect_global(tmp, g_cclosure_new(G_CALLBACK(shortcut_callback),strdup(luaL_checkstring(L,3)),closure_destroy));
return result;
}
static int register_multiinstance_event(lua_State* L) {
// 1 is the event name (checked)
// 2 is the action to perform (checked)
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,lua_tostring(L,1));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_newtable(L);
lua_pushvalue(L,-1);
lua_setfield(L,-3,lua_tostring(L,1));
}
lua_pushvalue(L,2);
luaL_ref(L,-2);
lua_pop(L,2);
return 0;
}
static int trigger_multiinstance_event(lua_State * L,const char* evt_name, int nargs,int nresults) {
// -1..-n are our args
const int top_marker=lua_gettop(L);
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,evt_name);
if(lua_isnil(L,-1)) {
lua_pop(L,2+nargs);
if(nresults == LUA_MULTRET) return 0;
for(int i = 0 ; i < nresults ; i++) lua_pushnil(L);
return nresults;
}
lua_remove(L,-2);
lua_pushnil(L); /* first key */
int result = 0;
while (lua_next(L, top_marker +1) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) value is the function to call*/
// prepare the call
lua_pushstring(L,evt_name);// param 1 is the event
for(int i = 0 ; i<nargs ;i++) { // event dependant parameters
lua_pushvalue(L, top_marker -nargs +1 +i);
}
int tmp_result = dt_lua_do_chunk(L,0,nargs+1,nresults);
if(tmp_result > 0) {
lua_pushvalue(L,-(tmp_result+1));//push index on top
lua_remove(L,-(tmp_result+2));//remove old index still in stack
}
result += tmp_result;
//result += dt_lua_do_chunk(L,0,nargs+1,nresults);
}
return result;
}
static int register_chained_event(lua_State* L) {
// 1 is the event name (checked)
// 2 is the action to perform (checked)
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,lua_tostring(L,1));
if(lua_isnil(L,-1)) {
lua_pop(L,1);
lua_newtable(L);
lua_pushvalue(L,-1);
lua_setfield(L,-3,lua_tostring(L,1));
}
lua_pushvalue(L,2);
luaL_ref(L,-2);
lua_pop(L,2);
return 0;
}
static int trigger_chained_event(lua_State * L,const char* evt_name, int nargs,int nresults) {
// -1..-n a
if(nargs<nresults)
dt_print(DT_DEBUG_LUA,"error chained parameters must have less results than args (args %d results %d\n",nargs,nresults);
int arg_top=lua_gettop(L);
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_getfield(L,-1,evt_name);
if(lua_isnil(L,-1)) {
lua_pop(L,2); // remove what we built
for(int i = 0 ; i < nargs - nresults ; i++) lua_remove(L,-(nresults+1));
return nresults;
}
lua_remove(L,-2);
// copy variable args on the top of the stack
for(int i = 0 ; i< nresults; i++) {
lua_pushvalue(L,arg_top -nresults +i+1);
}
lua_pushnil(L); /* first key */
while (lua_next(L, -(nresults+2)) != 0) {
/* stack at this point
- all args from the function entry
- table with all the functions to chain
- variable args at this point
- "key" the index we just looked into
- "value" function to call
*/
// remove the loop index
const int loop_index=luaL_checkint(L,-2);
lua_remove(L,-2);
// move the function below the variable args
lua_insert(L,-(nresults+1)); // move fn call below args
//move the evt name just above the function
lua_pushstring(L,evt_name);// param 1 is the event
lua_insert(L,-(nresults+1)); // move evt name below params
// add all fixed args at their final place
for(int i = 0; i < nargs - nresults ; i++) {
lua_pushvalue(L,arg_top -nargs+i+1); // copy all invariant args at the top
lua_insert(L,-(nresults+1));// move them at their final place
}
/* uses 'key' (at index -2) and 'value' (at index -1) */
// prepare the call
dt_lua_do_chunk(L,0,nargs+1,nresults);
lua_pushinteger(L,loop_index);
}
for(int i=0; i< nargs+1 ; i++)
lua_remove(L,-nresults -1); //remove all args that are below the results and the fn table
return nresults;
}
static event_handler event_list[] = {
{"pre-export",register_chained_event,trigger_chained_event},
{"post-import-image",register_multiinstance_event,trigger_multiinstance_event},
{"shortcut",register_shortcut_event,trigger_keyed_event},
{"tmp-export-image",register_multiinstance_event,trigger_multiinstance_event},
{"test",register_singleton_event,trigger_singleton_event}, // avoid error because of unused function
{NULL,NULL,NULL}
};
static int lua_register_event(lua_State *L) {
// 1 is event name
const char*evt_name = luaL_checkstring(L,1);
// 2 is event handler
luaL_checktype(L,2,LUA_TFUNCTION);
lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_event_list");
lua_getfield(L,-1,evt_name);
if(lua_isnil(L,-1)) {
lua_pop(L,2);
return luaL_error(L,"incorrect event type : %s\n",evt_name);
}
luaL_checktype(L,-1,LUA_TLIGHTUSERDATA);
event_handler * handler = lua_touserdata(L,-1);
lua_pop(L,2); // restore the stack to only have the 3 parameters
handler->on_register(L);
return 0;
}
static int dt_lua_trigger_event_internal(const char*event,int nargs,int nresults) {
lua_getfield(darktable.lua_state,LUA_REGISTRYINDEX,"dt_lua_event_list");
if(lua_isnil(darktable.lua_state,-1)) {// events have been disabled
lua_pop(darktable.lua_state,1+nargs);
if(nresults== LUA_MULTRET) return 0;
for(int i=0; i<nresults;i++)
lua_pushnil(darktable.lua_state);
return nresults;
}
lua_getfield(darktable.lua_state,-1,event);
luaL_checktype(darktable.lua_state,-1,LUA_TLIGHTUSERDATA);
event_handler * handler = lua_touserdata(darktable.lua_state,-1);
lua_pop(darktable.lua_state,2);
const int result = handler->on_event(darktable.lua_state,event,nargs,nresults);
return result;
}
static int dt_lua_trigger_event(const char*event,int nargs,int nresults) {
int res;
#pragma omp critical(running_lua)
{
res = dt_lua_trigger_event_internal(event,nargs,nresults);
}
return res;
}
static void on_export_selection(gpointer instance,dt_control_image_enumerator_t * export_descriptor,
gpointer user_data){
lua_State* L = darktable.lua_state;
dt_control_export_t *export_data= (dt_control_export_t*)export_descriptor->data;
int size;
dt_imageio_module_storage_t *mstorage = dt_imageio_get_storage_by_index(export_data->storage_index);
g_assert(mstorage);
dt_imageio_module_data_t *fdata = mstorage->get_params(mstorage, &size);
luaA_push_typeid(L,mstorage->parameter_lua_type,fdata);
mstorage->free_params(mstorage,fdata);
dt_imageio_module_format_t *mformat = dt_imageio_get_format_by_index(export_data->format_index);
g_assert(mformat);
fdata = mformat->get_params(mformat, &size);
luaA_push_typeid(L,mformat->parameter_lua_type,fdata);
mformat->free_params(mformat,fdata);
dt_lua_image_glist_push(L,export_descriptor->index);
g_list_free(export_descriptor->index);
export_descriptor->index =NULL;
dt_lua_trigger_event("pre-export",3,3);
// get the new storage data and the new storage
luaL_getmetafield(L,-3,"__associated_object");
mstorage = lua_touserdata(L,-1);
lua_pop(L,1);
fdata = mstorage->get_params(mstorage, &size);
luaL_getmetafield(L,-3,"__luaA_Type");
luaA_Type storage_type = lua_tointeger(L,-1);
lua_pop(L,1);
luaA_to_typeid(L,storage_type,fdata,-3);
mstorage->set_params(mstorage,fdata,size);
mstorage->free_params(mstorage,fdata);
export_data->storage_index = dt_imageio_get_index_of_storage(mstorage);
// get the new format data and the new format
luaL_getmetafield(L,-2,"__associated_object");
mformat = lua_touserdata(L,-1);
lua_pop(L,1);
fdata = mformat->get_params(mformat, &size);
luaL_getmetafield(L,-2,"__luaA_Type");
luaA_Type format_type = lua_tointeger(L,-1);
lua_pop(L,1);
luaA_to_typeid(L,format_type,fdata,-2);
mformat->set_params(mformat,fdata,size);
mformat->free_params(mformat,fdata);
export_data->format_index = dt_imageio_get_index_of_format(mformat);
// load the new list of images to process
if(lua_isnoneornil(L,-1)) {lua_pop(L,3); return; }// everything already has been removed
export_descriptor->index = dt_lua_image_glist_get(L,-1);
lua_pop(L,1);
}
static void on_export_image_tmpfile(gpointer instance,
int imgid,
char *filename,
gpointer user_data){
dt_lua_image_push(darktable.lua_state,imgid);
lua_pushstring(darktable.lua_state,filename);
dt_lua_trigger_event("tmp-export-image",2,0);
}
static void on_image_imported(gpointer instance,uint8_t id, gpointer user_data){
dt_lua_image_push(darktable.lua_state,id);
dt_lua_trigger_event("post-import-image",1,0);
}
void dt_lua_init_events(lua_State *L) {
lua_newtable(L);
lua_setfield(L,LUA_REGISTRYINDEX,"dt_lua_event_data");
lua_newtable(L);
event_handler * handler = event_list;
while(handler->evt_name) {
lua_pushlightuserdata(L,handler);
lua_setfield(L,-2,handler->evt_name);
handler++;
}
lua_setfield(L,LUA_REGISTRYINDEX,"dt_lua_event_list");
dt_lua_push_darktable_lib(L);
lua_pushstring(L,"register_event");
lua_pushcfunction(L,&lua_register_event);
lua_settable(L,-3);
lua_pop(L,1);
dt_control_signal_connect(darktable.signals,DT_SIGNAL_IMAGE_IMPORT,G_CALLBACK(on_image_imported),NULL);
dt_control_signal_connect(darktable.signals,DT_SIGNAL_IMAGE_EXPORT_MULTIPLE,G_CALLBACK(on_export_selection),NULL);
dt_control_signal_connect(darktable.signals,DT_SIGNAL_IMAGE_EXPORT_TMPFILE,G_CALLBACK(on_export_image_tmpfile),NULL);
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;