-
Notifications
You must be signed in to change notification settings - Fork 4
/
ncluaw.c
572 lines (492 loc) · 14.3 KB
/
ncluaw.c
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* ncluaw.c -- The NCLua wrapper (Lua-free) interface.
Copyright (C) 2013-2018 PUC-Rio/Laboratorio TeleMidia
This file is part of NCLua.
NCLua 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 2 of the License, or
(at your option) any later version.
NCLua 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 NCLua. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <string.h>
#include "aux-glib.h"
#include "aux-lua.h"
#include "nclua.h"
#include "ncluaw.h"
/* NCLua wrapper local registry. */
static const int _ncluaw_magic = 0;
#define NCLUAW_REGISTRY_INDEX (deconst (void *, &_ncluaw_magic))
#define ncluaw_registry_create(L)\
luax_mregistry_create (L, NCLUAW_REGISTRY_INDEX)
#define ncluaw_registry_destroy(L)\
luax_mregistry_destroy (L, NCLUAW_REGISTRY_INDEX)
#define ncluaw_registry_get_field(L, field)\
luax_mregistry_getfield (L, NCLUAW_REGISTRY_INDEX, field)
#define ncluaw_registry_set_field(L, field)\
luax_mregistry_setfield (L, NCLUAW_REGISTRY_INDEX, field)
/* List of NCLua Event plugins to be loaded by ncluaw_open(). */
static const char *plugin_list[] = {
#if defined WITH_SOUP && WITH_SOUP
"http",
#endif
"key",
"ncl",
"pointer",
#if defined WITH_GIO && WITH_GIO
"tcp",
#endif
"user",
NULL
};
/* Fail-safe malloc. */
static void *
xmalloc (size_t n)
{
void *p;
p = g_malloc (n);
g_assert_nonnull (p); /* out of memory */
return p;
}
/* Fail-safe xstrdup. */
static char *
xstrdup (const char *s)
{
size_t n;
char *dup;
g_assert_nonnull (s);
n = strlen (s) + 1;
dup = (char *) xmalloc (sizeof (*dup) * n);
return (char *) memcpy (dup, s, n);
}
/* Returns the class of event at index INDEX. If the object at the given
index is not a known event, returns NCLUAW_EVENT_UNKNOWN. */
static ncluaw_event_class_t
get_event_class (lua_State *L, int index)
{
static const char *known_class_list[] = {"key", "ncl", "pointer"};
ncluaw_event_class_t result;
const char *name;
size_t i;
if (lua_type (L, index) != LUA_TTABLE)
return NCLUAW_EVENT_UNKNOWN;
lua_getfield (L, index, "class");
name = lua_tostring (L, -1);
if (name == NULL)
{
lua_pop (L, 1);
return NCLUAW_EVENT_UNKNOWN;
}
result = NCLUAW_EVENT_UNKNOWN;
for (i = 0; i < nelementsof (known_class_list); i++)
{
if (g_str_equal (name, known_class_list[i]))
{
result = (ncluaw_event_class_t) i;
break;
}
}
lua_pop (L, 1);
return result;
}
/* Returns the Lua state associate with NW. */
#define ncluaw_get_lua_state(nw) ((lua_State *)(nw))
/* Returns the wrapper associated with Lua state L. */
#define ncluaw_get_nw_from_lua_state(L) ((ncluaw_t *)(L))
/* Calls the panic function stored in field "panic" of module's registry.
If this field is empty, calls the default panic function (which is stored
in field "lua_panic"). */
static int
ncluaw_panic_wrapper (lua_State *L)
{
ncluaw_panic_function_t panic;
lua_CFunction lua_panic;
g_assert (lua_checkstack (L, 1));
ncluaw_registry_get_field (L, "panic");
panic = (ncluaw_panic_function_t) integralof (lua_touserdata (L, -1));
lua_pop (L, 1);
if (panic != NULL)
{
panic (ncluaw_get_nw_from_lua_state (L), lua_tostring (L, -1));
}
else
{
g_assert (lua_checkstack (L, 1));
ncluaw_registry_get_field (L, "lua_panic");
lua_panic = (lua_CFunction) integralof (lua_touserdata (L, -1));
g_assert_nonnull (lua_panic);
lua_insert (L, -2);
return lua_panic (L);
}
return 0;
}
/********************************* Event **********************************/
/*-
* Returns a copy of event EVT.
*
* The caller owns this copy and must call ncluaw_event_free() when done
* with it.
*/
ncluaw_event_t *
ncluaw_event_clone (const ncluaw_event_t *evt)
{
ncluaw_event_t *dup;
dup = (ncluaw_event_t *) xmalloc (sizeof (*dup));
memset (dup, 0, sizeof (*dup));
dup->cls = evt->cls;
switch (dup->cls)
{
case NCLUAW_EVENT_KEY:
dup->u.key.type = xstrdup (evt->u.key.type);
dup->u.key.key = xstrdup (evt->u.key.key);
break;
case NCLUAW_EVENT_NCL:
dup->u.ncl.type = xstrdup (evt->u.ncl.type);
dup->u.ncl.action = xstrdup (evt->u.ncl.action);
dup->u.ncl.name = xstrdup (evt->u.ncl.name);
dup->u.ncl.value = (evt->u.ncl.value != NULL)
? xstrdup (evt->u.ncl.value) : NULL;
break;
case NCLUAW_EVENT_POINTER:
dup->u.pointer.type = xstrdup (evt->u.pointer.type);
dup->u.pointer.x = evt->u.pointer.x;
dup->u.pointer.y = evt->u.pointer.y;
break;
case NCLUAW_EVENT_UNKNOWN:
default:
g_assert_not_reached ();
}
return dup;
}
/*-
* Releases the resources associated with event EVT.
*/
void
ncluaw_event_free (ncluaw_event_t *evt)
{
switch (evt->cls)
{
case NCLUAW_EVENT_KEY:
g_free (deconst (char *, evt->u.key.type));
g_free (deconst (char *, evt->u.key.key));
break;
case NCLUAW_EVENT_NCL:
g_free (deconst (char *, evt->u.ncl.type));
g_free (deconst (char *, evt->u.ncl.action));
g_free (deconst (char *, evt->u.ncl.name));
g_free (deconst (char *, evt->u.ncl.value));
break;
case NCLUAW_EVENT_POINTER:
g_free (deconst (char *, evt->u.pointer.type));
break;
case NCLUAW_EVENT_UNKNOWN:
default:
g_assert_not_reached ();
}
g_free (evt);
}
/*-
* Returns true if events E1 and E2 are equal (have the same content),
* otherwise returns false.
*/
G_GNUC_PURE int
ncluaw_event_equals (const ncluaw_event_t *e1, const ncluaw_event_t *e2)
{
int result;
if (e1->cls != e2->cls)
return FALSE;
result = FALSE;
switch (e1->cls)
{
case NCLUAW_EVENT_KEY:
result = g_str_equal (e1->u.key.type, e2->u.key.type)
&& g_str_equal (e1->u.key.key, e2->u.key.key);
break;
case NCLUAW_EVENT_NCL:
result = g_str_equal (e1->u.ncl.type, e2->u.ncl.type)
&& g_str_equal (e1->u.ncl.action, e2->u.ncl.action)
&& g_str_equal (e1->u.ncl.name, e2->u.ncl.name)
&& ((e1->u.ncl.value == NULL && e2->u.ncl.value == NULL)
|| (e1->u.ncl.value && e2->u.ncl.value
&& g_str_equal (e1->u.ncl.value, e2->u.ncl.value)));
break;
case NCLUAW_EVENT_POINTER:
result = g_str_equal (e1->u.pointer.type, e2->u.pointer.type)
&& e1->u.pointer.x == e2->u.pointer.x
&& e1->u.pointer.y == e2->u.pointer.y;
break;
case NCLUAW_EVENT_UNKNOWN:
default:
g_assert_not_reached ();
}
return result;
}
/******************************** Wrappers ********************************/
/*-
* Creates a new NCLua state from the NCLua script at path PATH.
*
* The WIDTH and HEIGHT parameters define the dimensions (in pixels) of the
* global canvas.
*
* Returns a new NCLua state if successful, otherwise returns NULL and, if
* ERRMSG is non-NULL, stores a copy of the error message into *ERRMSG; the
* caller owns this copy and should call free() when done with it.
*/
ncluaw_t *
ncluaw_open (const char *path, int width, int height, char **errmsg)
{
lua_CFunction lua_panic;
lua_State *L;
int err;
L = luaL_newstate ();
g_assert_nonnull (L); /* out of memory */
luaL_openlibs (L);
err = nclua_open (L, width, height, plugin_list);
if (unlikely (err != LUA_OK))
goto fail;
err = luaL_dofile (L, path);
if (unlikely (err != LUA_OK))
goto fail;
lua_newtable (L);
ncluaw_registry_create (L);
lua_panic = lua_atpanic (L, ncluaw_panic_wrapper);
g_assert_nonnull (lua_panic);
lua_pushlightuserdata (L, pointerof (lua_panic));
ncluaw_registry_set_field (L, "lua_panic");
return (ncluaw_t *) L;
fail:
tryset (errmsg, xstrdup (luaL_checkstring (L, -1)));
lua_close (L);
return NULL;
}
/*-
* Releases the resources associated with NCLua state NW.
*/
void
ncluaw_close (ncluaw_t *nw)
{
lua_State *L;
if (unlikely (nw == NULL))
return; /* nothing to do */
L = ncluaw_get_lua_state (nw);
ncluaw_registry_destroy (L);
nclua_close (L);
}
/*-
* Sets a new panic function and returns the old one or NULL if no custom
* panic function was set. If PANIC is NULL, revert to use the default
* panic function.
*/
ncluaw_panic_function_t
ncluaw_at_panic (ncluaw_t *nw, ncluaw_panic_function_t panic)
{
lua_State *L;
ncluaw_panic_function_t old_panic;
L = ncluaw_get_lua_state (nw);
ncluaw_registry_get_field (L, "panic");
old_panic = (ncluaw_panic_function_t) integralof (lua_touserdata (L, -1));
if (panic == NULL)
{
if (old_panic == NULL)
return NULL; /* nothing to do */
lua_pushnil (L);
}
else
{
lua_pushlightuserdata (L, pointerof (panic));
}
ncluaw_registry_set_field (L, "panic");
return old_panic;
}
/*-
* Cycles NW's engine once.
*/
void
ncluaw_cycle (ncluaw_t *nw)
{
nclua_cycle (ncluaw_get_lua_state (nw));
}
/*-
* Receives an event from NW's engine -- the caller owns this event and
* should call free() when done with it.
*
* If there is no known event to be received returns NULL.
*
* WARNING: This function silently discards unknown events. If you need to
* deal with such events, use the NCLua core API.
*/
ncluaw_event_t *
ncluaw_receive (ncluaw_t *nw)
{
lua_State *L;
ncluaw_event_t evt;
ncluaw_event_t *dup = NULL;
L = ncluaw_get_lua_state (nw);
nclua_receive (L);
if (lua_isnil (L, -1))
goto done;
evt.cls = get_event_class (L, -1);
switch (evt.cls)
{
case NCLUAW_EVENT_KEY:
lua_getfield (L, -1, "type");
evt.u.key.type = luaL_checkstring (L, -1);
lua_getfield (L, -2, "key");
evt.u.key.key = luaL_checkstring (L, -1);
dup = ncluaw_event_clone (&evt);
lua_pop (L, 2);
break;
case NCLUAW_EVENT_NCL:
lua_getfield (L, -1, "type");
evt.u.ncl.type = luaL_checkstring (L, -1);
lua_getfield (L, -2, "action");
evt.u.ncl.action = luaL_checkstring (L, -1);
lua_getfield (L, -3, "label");
if (!lua_isnil (L, -1))
{
evt.u.ncl.name = luaL_checkstring (L, -1);
evt.u.ncl.value = NULL;
}
else
{
lua_pop (L, 1);
lua_getfield (L, -3, "name");
if (!lua_isnil (L, -1))
{
evt.u.ncl.name = luaL_checkstring (L, -1);
lua_getfield (L, -4, "value");
evt.u.ncl.value = luaL_checkstring (L, -1);
lua_pop (L, 1);
}
else
{
evt.u.ncl.name = "";
evt.u.ncl.value = NULL;
}
}
dup = ncluaw_event_clone (&evt);
lua_pop (L, 3);
break;
case NCLUAW_EVENT_POINTER:
lua_getfield (L, -1, "type");
evt.u.pointer.type = luaL_checkstring (L, -1);
lua_getfield (L, -2, "x");
evt.u.pointer.x = luaL_checkint (L, -1);
lua_getfield (L, -3, "y");
evt.u.pointer.y = luaL_checkint (L, -1);
dup = ncluaw_event_clone (&evt);
lua_pop (L, 3);
break;
case NCLUAW_EVENT_UNKNOWN:
break;
default:
g_assert_not_reached ();
}
done:
lua_pop (L, 1);
return dup;
}
/*-
* Sends a copy of event EVT to NW's engine.
*/
void
ncluaw_send (ncluaw_t *nw, const ncluaw_event_t *evt)
{
lua_State *L;
L = ncluaw_get_lua_state (nw);
lua_newtable (L);
/* TODO: Check if evt is valid using plugin:check(). */
switch (evt->cls)
{
case NCLUAW_EVENT_KEY:
luax_setstringfield (L, -1, "class", "key");
luax_setstringfield (L, -1, "type", evt->u.key.type);
luax_setstringfield (L, -1, "key", evt->u.key.key);
break;
case NCLUAW_EVENT_NCL:
luax_setstringfield (L, -1, "class", "ncl");
luax_setstringfield (L, -1, "type", evt->u.ncl.type);
luax_setstringfield (L, -1, "action", evt->u.ncl.action);
if (evt->u.ncl.value != NULL)
{
luax_setstringfield (L, -1, "name", evt->u.ncl.name);
luax_setstringfield (L, -1, "value", evt->u.ncl.value);
}
else
{
luax_setstringfield (L, -1, "label", evt->u.ncl.name);
}
break;
case NCLUAW_EVENT_POINTER:
luax_setstringfield (L, -1, "class", "pointer");
luax_setstringfield (L, -1, "type", evt->u.pointer.type);
luax_setintegerfield (L, -1, "x", evt->u.pointer.x);
luax_setintegerfield (L, -1, "y", evt->u.pointer.y);
break;
case NCLUAW_EVENT_UNKNOWN:
default:
g_assert_not_reached ();
}
nclua_send (L);
}
/*-
* Paints the surface of NW's global canvas into buffer BUFFER.
*/
void
ncluaw_paint (ncluaw_t *nw, unsigned char *buffer, const char *format,
int width, int height, int stride)
{
nclua_paint (ncluaw_get_lua_state (nw), buffer, format,
width, height, stride);
}
/*-
* Resizes the surface of NW's global canvas to the given dimensions.
*/
void
ncluaw_resize (ncluaw_t *nw, int width, int height)
{
nclua_resize (ncluaw_get_lua_state (nw), width, height);
}
/********************************* Debug **********************************/
/*-
* Dumps the surface of NW's global canvas into PNG file at path PATH.
*
* Returns true if successful, otherwise returns false and, if ERRMSG is
* non-NULL, stores a copy of the error message into *ERRMSG -- the caller
* owns this copy and should call free() when done with it.
*/
int
ncluaw_debug_dump_surface (ncluaw_t *nw, const char *path, char **errmsg)
{
lua_State *L;
int err;
L = ncluaw_get_lua_state (nw);
err = nclua_debug_dump_surface (L, path);
if (unlikely (err != LUA_OK))
{
tryset (errmsg, xstrdup (luaL_checkstring (L, -1)));
return FALSE;
}
return TRUE;
}
/*-
* Returns a pointer to the Lua state associated with NCLua state NW.
*/
G_GNUC_CONST void *
ncluaw_debug_get_lua_state (ncluaw_t *nw)
{
return (void *) ncluaw_get_lua_state (nw);
}
/*-
* Returns a pointer to the surface of NW's global canvas.
*
* WARNING: This pointer may become invalid after ncluaw_cycle() is called.
*/
void *
ncluaw_debug_get_surface (ncluaw_t *nw)
{
return nclua_debug_get_surface (ncluaw_get_lua_state (nw));
}