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 "gui/guides.h"
19 #include "common/darktable.h"
20 #include "lua/cairo.h"
21 #include "lua/call.h"
22 #include "lua/lua.h"
23 #include "lua/widget/widget.h"
24 
25 typedef struct callback_data_t
26 {
27   int draw_callback_id;
28   int gui_callback_id;
29 } callback_data_t;
30 
_guides_draw_callback(cairo_t * cr,const float x,const float y,const float w,const float h,const float zoom_scale,void * user_data)31 static void _guides_draw_callback(cairo_t *cr, const float x, const float y,
32                                   const float w, const float h,
33                                   const float zoom_scale, void *user_data)
34 {
35   callback_data_t *d = (callback_data_t *)user_data;
36 
37   dt_lua_lock_silent(); // this code is called from the C side so we have to lock
38 
39   lua_State *L = darktable.lua_state.state;
40   lua_rawgeti(L, LUA_REGISTRYINDEX, d->draw_callback_id);
41 
42   luaA_push(L, dt_lua_cairo_t, &cr);
43   lua_pushnumber(L, x);
44   lua_pushnumber(L, y);
45   lua_pushnumber(L, w);
46   lua_pushnumber(L, h);
47   lua_pushnumber(L, zoom_scale);
48 
49   // this will be called directly from the gui thread so we can just execute it, without caring about the gtk lock
50   dt_lua_treated_pcall(L,6,0);
51 
52   dt_lua_type_gpointer_drop(L,cr);
53 
54   dt_lua_unlock();
55 }
56 
_guides_gui_callback(dt_iop_module_t * self,void * user_data)57 static GtkWidget *_guides_gui_callback(dt_iop_module_t *self, void *user_data)
58 {
59   callback_data_t *d = (callback_data_t *)user_data;
60   dt_lua_lock_silent(); // this code is called from the C side so we have to lock
61   lua_State *L = darktable.lua_state.state;
62   lua_rawgeti(L, LUA_REGISTRYINDEX, d->gui_callback_id);
63   dt_lua_treated_pcall(L,0,1);
64 
65 //   dt_lua_debug_stack(L);
66   lua_widget widget;
67   luaA_to(L, lua_widget, &widget, -1);
68   dt_lua_widget_bind(L, widget);
69   lua_pop(L,1);
70 
71   dt_lua_unlock();
72 
73   return widget->widget;
74 }
75 
register_guide(lua_State * L)76 static int register_guide(lua_State *L)
77 {
78   int draw_callback_id = -1, gui_callback_id = -1;
79   dt_guides_widget_callback gui_callback = NULL;
80 
81   lua_settop(L, 3);
82 
83   const char *name = luaL_checkstring(L, 1);
84 
85   if(!lua_isnil(L, 3))
86   {
87     luaL_checktype(L, 3, LUA_TFUNCTION);
88     gui_callback = _guides_gui_callback;
89     gui_callback_id = luaL_ref(L, LUA_REGISTRYINDEX);
90   }
91   else
92     lua_pop(L, 1); // get rid of the nil
93 
94   if(lua_isnil(L, 2))
95     return luaL_error(L, "missing draw callback");
96 
97   luaL_checktype(L, 2, LUA_TFUNCTION);
98   draw_callback_id = luaL_ref(L, LUA_REGISTRYINDEX);
99 
100   callback_data_t *user_data = (callback_data_t *)malloc(sizeof(callback_data_t));
101   user_data->draw_callback_id = draw_callback_id;
102   user_data->gui_callback_id = gui_callback_id;
103 
104   dt_guides_add_guide(name, _guides_draw_callback, gui_callback, user_data, free);
105   return 0;
106 }
107 
dt_lua_init_guides(lua_State * L)108 int dt_lua_init_guides(lua_State *L)
109 {
110   dt_lua_push_darktable_lib(L);
111 
112   dt_lua_goto_subtable(L, "guides");
113   // build the table containing the guides
114 
115   lua_pushstring(L, "register_guide");
116   lua_pushcfunction(L, register_guide);
117   lua_settable(L, -3);
118 
119   lua_pop(L, 1); // remove the configuration table from the stack
120   return 0;
121 }
122 
123 
124 
125 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
126 // vim: shiftwidth=2 expandtab tabstop=2 cindent
127 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
128