forked from createuniverses/praxis
/
luaInterface.cpp
348 lines (280 loc) · 7.45 KB
/
luaInterface.cpp
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
// Author: Greg "fugue" Santucci, 2011
// Email: thecodewitch@gmail.com
// Web: http://createuniverses.blogspot.com/
#include "luaInterface.h"
#ifdef __PRAXIS_WINDOWS__
#include <windows.h>
extern "C"
{
extern HWND g_AppHWND;
extern int g_nLastBreakTime;
}
#endif
//extern "C"
//{
// extern int g_nLastBreakTime;
//}
#ifdef __PRAXIS_LINUX__
#include <X11/Xlib.h>
#include "SDL.h"
#include "GL/openglut.h"
int g_nLastBreakTime = 0;
extern "C"
{
extern Display * g_pAppDisplay;
}
#endif
lua_State * g_pLuaState = 0;
std::string g_sLuaOutput;
std::string g_sLuaError;
//bool g_bLuaBreak = false;
void cbLuaBreakHook(lua_State *L, lua_Debug *ar);
//int cbLuaFoo(lua_State *L);
int cbLuaPrint(lua_State *L);
// Critical sections....hmm....
// This may not be necessary at all on Linux because the midi stuff isn't being called from some super-duper accurately timed
// windows midi thread
#ifdef __PRAXIS_WINDOWS__
extern CRITICAL_SECTION g_cs;
#endif
bool g_bLuaRunning = false;
extern "C" {
int luaopen_lpeg (lua_State *L);
}
void luaInit()
{
#ifdef __PRAXIS_WINDOWS__
EnterCriticalSection (&g_cs) ;
#endif
g_pLuaState = luaL_newstate();
luaL_openlibs(g_pLuaState);
luaopen_lpeg(g_pLuaState);
// lua_register(g_pLuaState, "foo", cbLuaFoo);
lua_register(g_pLuaState, "print", cbLuaPrint);
luaL_dostring(g_pLuaState,
"function onerror(s) "
"endGL() "
"glResetStencil() "
"return s .. \"\\n\" .. debug.traceback() "
"end");
lua_sethook(g_pLuaState, cbLuaBreakHook, LUA_MASKCOUNT, 1000);
#ifdef __PRAXIS_WINDOWS__
LeaveCriticalSection (&g_cs) ;
#endif
g_bLuaRunning = true;
}
bool luaCall(std::string sCmd)
{
#ifdef __PRAXIS_WINDOWS__
EnterCriticalSection (&g_cs) ;
#endif
lua_getglobal(g_pLuaState, "onerror");
int error = (luaL_loadstring(g_pLuaState, sCmd.c_str()) || lua_pcall(g_pLuaState, 0, LUA_MULTRET, -2));
if (error)
{
g_sLuaError += lua_tostring(g_pLuaState, -1);
g_sLuaError += "\n";
lua_pop(g_pLuaState, 1);
}
// Pop the error function sitting on the stack
lua_pop(g_pLuaState, 1);
#ifdef __PRAXIS_WINDOWS__
LeaveCriticalSection (&g_cs) ;
#endif
return (error == 0);
}
std::string & luaGetOutput()
{
return g_sLuaOutput;
}
std::string & luaGetError()
{
return g_sLuaError;
}
void luaClearError()
{
g_sLuaError = "";
}
void luaClearOutput()
{
g_sLuaOutput = "";
}
void luaClose()
{
g_bLuaRunning = false;
#ifdef __PRAXIS_WINDOWS__
EnterCriticalSection (&g_cs) ;
#endif
lua_close(g_pLuaState);
// Dump the error and output strings to a file?
#ifdef __PRAXIS_WINDOWS__
LeaveCriticalSection (&g_cs) ;
#endif
}
int cbLuaPrint( lua_State *L)
{
int n = lua_gettop(L);
int i = 0;
for(i = 1; i <= n; i++)
{
if(lua_isstring(L, i))
{
g_sLuaOutput += lua_tostring(L, i);
g_sLuaOutput += "\n";
}
else
{
g_sLuaOutput += "Not a string\n";
}
}
return 0;
}
bool luaIsCommandComplete(std::string sCmd)
{
int status = luaL_loadbuffer(g_pLuaState, sCmd.c_str(), sCmd.length(), "=stdin");
// pushes either a lua chunk or an error message
if (status == LUA_ERRSYNTAX)
{
size_t lmsg;
const char *msg = lua_tolstring(g_pLuaState, -1, &lmsg);
const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
if (strstr(msg, LUA_QL("<eof>")) == tp)
{
lua_pop(g_pLuaState, 1); // Pop the error message
return false;
}
}
lua_pop(g_pLuaState, 1); // Pop the lua chunk
return true;
}
//void luaBreak()
//{
// g_bLuaBreak = true;
//}
void cbLuaBreakHook(lua_State *L, lua_Debug *ar)
{
// Need some other way to do this in Linux.
#ifdef __PRAXIS_WINDOWS__
// Force GetAsyncKeyState to be called for both keys so that
// their respective statuses are up to date.
// Previously, only Ctrl was kept up to date. Q was checked only if Ctrl held down.
// Press q, then other stuff, then when Ctrl pressed, a break would be triggered.
bool bCtrlDown = ::GetAsyncKeyState(VK_LCONTROL) != 0;
bool bQDown = ::GetAsyncKeyState(0x51) != 0;
if(bCtrlDown && bQDown) // Ctrl-Q pressed
{
// Break detected. Check the time since last break.
// If its too low, ignore.
int nTime = ::timeGetTime();
if(nTime < (g_nLastBreakTime + 200))
return;
// If its high enough, then break.
g_nLastBreakTime = nTime;
lua_pushstring(L, "User break.");
lua_error(L);
return;
}
#endif
#ifdef __PRAXIS_LINUX__
// SDL_GetKeyState
// http://sdl.beuc.net/sdl.wiki/SDL_GetKeyState
//
// #include "SDL.h"
// Uint8 *SDL_GetKeyState(int *numkeys);
//
// Example:
//
// Uint8 *keystate = SDL_GetKeyState(NULL);
// if ( keystate[SDLK_RETURN] ) printf("Return Key Pressed.\n");
// if ( keystate[SDLK_RIGHT] && keystate[SDLK_UP] ) printf("Right and Up Keys Pressed.\n");
//
// Note: Use SDL_PumpEvents to update the state array
//
// http://sdl.beuc.net/sdl.wiki/SDLKey
// SDLK_LCTRL
// SDLK_RCTRL
// SDLK_q
//
// Calling SDL_PumpEvents shouldn't have side effects because SDL isn't being used for OpenGL
char keys_return[32];
//glutMainLoopEvent();
XQueryKeymap(g_pAppDisplay, keys_return);
#if 0
bool bNonZero = false;
for (int i = 0; i < 32; i++)
{
if(keys_return[i] != 0)
bNonZero = true;
}
if(bNonZero)
{
for (int i = 0; i < 32; i++)
{
printf("%d ", (unsigned int)keys_return[i]);
}
printf("\n");
}
#endif
if(keys_return[3] == 1 && keys_return[4] == 32)
{
// Break detected. Check the time since last break.
// If its too low, ignore.
int nTime = glutGet(GLUT_ELAPSED_TIME);
if(nTime < (g_nLastBreakTime + 200))
return;
// If its high enough, then break.
g_nLastBreakTime = nTime;
lua_pushstring(L, "User break.");
lua_error(L);
return;
}
#endif
return;
#if 0
HGLOBAL hGlobal ;
PTSTR pGlobal ;
std::string sText;
OpenClipboard (g_AppHWND) ;
if (hGlobal = GetClipboardData (CF_TEXT))
{
pGlobal = (PTSTR)GlobalLock (hGlobal) ;
sText = pGlobal;
GlobalUnlock(hGlobal);
}
CloseClipboard () ;
if(sText == "praxis:STOP")
{
OpenClipboard (g_AppHWND) ;
EmptyClipboard();
CloseClipboard () ;
lua_pushstring(L, "User break.");
lua_error(L);
}
#endif
}
//std::string luaLogCall(std::string sCmd)
//{
// g_luaOutputString = "";
// lua_getglobal(g_pLuaState, "logLiveCode");
// lua_pushstring(g_pLuaState, sCmd.c_str());
// lua_call(g_pLuaState, 1, 0);
// return g_luaOutputString;
//}
//int cbLuaFoo(lua_State *L)
//{
// int n = lua_gettop(L); /* number of arguments */
// lua_Number sum = 0;
// int i;
// for (i = 1; i <= n; i++)
// {
// if (!lua_isnumber(L, i))
// {
// lua_pushstring(L, "incorrect argument");
// lua_error(L);
// }
// sum += lua_tonumber(L, i);
// }
// lua_pushnumber(L, sum/n); /* first result */
// lua_pushnumber(L, sum); /* second result */
// return 2; /* number of results */
//}