forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.c
More file actions
157 lines (140 loc) · 4.5 KB
/
Copy pathlua.c
File metadata and controls
157 lines (140 loc) · 4.5 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
/*
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 "lua/lua.h"
#include "common/darktable.h"
#include "control/control.h"
#include "lua/call.h"
void dt_lua_debug_stack_internal(lua_State *L, const char *function, int line)
{
printf("lua stack at %s:%d", function, line);
if(!L)
{
printf(" Stack is NULL\n");
return;
}
else
{
printf("(size %d),\n",lua_gettop(L)); //usefull to detect underflows
}
for(int i = 1; i <= lua_gettop(L); i++)
{
#if 1
printf("\t%d:%s %s\n", i, lua_typename(L, lua_type(L, i)), luaL_tolstring(L, i, NULL));
lua_pop(L, 1); // remove the result of luaL_tolstring() from the stack
#else
// no tolstring when stack is really screwed up
printf("\t%d:%s %p\n", i, lua_typename(L, lua_type(L, i)),lua_topointer(L,i));
#endif
}
}
void dt_lua_debug_table_internal(lua_State *L, int t, const char *function, int line)
{
t = lua_absindex(L,t);
/* table is in the stack at index 't' */
lua_len(L,t);
printf("lua table at index %d at %s:%d (length %f)\n", t, function, line,lua_tonumber(L,-1));
lua_pop(L,1);
if(lua_type(L, t) != LUA_TTABLE)
{
printf("\tnot a table: %s\n", lua_typename(L, lua_type(L, t)));
return;
}
lua_pushnil(L); /* first key */
while(lua_next(L, t ) != 0)
{
/* uses 'key' (at index -2) and 'value' (at index -1) */
if(lua_type(L,-2) != LUA_TNUMBER) {
printf("%s - %s\n", lua_tostring(L, -2), lua_typename(L, lua_type(L, -1)));
} else {
printf("%f - %s\n", luaL_checknumber(L, -2), lua_typename(L, lua_type(L, -1)));
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
}
int dt_lua_push_darktable_lib(lua_State *L)
{
lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_dtlib");
if(lua_isnil(L, -1))
{
lua_pop(L, 1);
lua_newtable(L);
lua_newtable(L);
lua_setmetatable(L, -2);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, "dt_lua_dtlib");
}
return 1;
}
void dt_lua_goto_subtable(lua_State *L, const char *sub_name)
{
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, sub_name);
if(lua_isnil(L, -1))
{
lua_pop(L, 1);
lua_newtable(L);
lua_setfield(L, -2, sub_name);
lua_getfield(L, -1, sub_name);
}
lua_remove(L, -2);
}
void dt_lua_init_lock()
{
pthread_mutexattr_t a;
pthread_mutexattr_init(&a);
//pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
dt_pthread_mutex_init(&darktable.lua_state.mutex, &a);
pthread_mutexattr_destroy(&a);
// we want our lock initialized locked
dt_pthread_mutex_lock(&darktable.lua_state.mutex);
}
void dt_lua_lock_internal(const char *function, const char *file, int line, gboolean silent)
{
if(!silent && !darktable.lua_state.ending && pthread_equal(darktable.control->gui_thread, pthread_self()) != 0)
{
dt_print(DT_DEBUG_LUA, "LUA WARNING locking from the gui thread should be avoided\n");
//g_assert(false);
}
#ifdef _DEBUG
dt_print(DT_DEBUG_LUA,"LUA DEBUG : thread %p waiting from %s:%d\n", g_thread_self(), function, line);
#endif
dt_pthread_mutex_lock(&darktable.lua_state.mutex);
#ifdef _DEBUG
dt_print(DT_DEBUG_LUA,"LUA DEBUG : thread %p taken from %s:%d\n", g_thread_self(), function, line);
#endif
}
void dt_lua_unlock_internal(const char *function, int line)
{
#ifdef _DEBUG
dt_print(DT_DEBUG_LUA,"LUA DEBUG : thread %p released from %s:%d\n",g_thread_self(), function,line);
#endif
dt_pthread_mutex_unlock(&darktable.lua_state.mutex);
}
static gboolean async_redraw(gpointer data)
{
dt_control_queue_redraw();
return false;
}
void dt_lua_redraw_screen()
{
if(darktable.gui != NULL)
{
g_idle_add(async_redraw,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-spaces modified;