aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ClientHamr/GuiLua/GuiLua.c
diff options
context:
space:
mode:
Diffstat (limited to 'ClientHamr/GuiLua/GuiLua.c')
-rw-r--r--ClientHamr/GuiLua/GuiLua.c379
1 files changed, 1 insertions, 378 deletions
diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c
index cb7f64c..1e6d3fa 100644
--- a/ClientHamr/GuiLua/GuiLua.c
+++ b/ClientHamr/GuiLua/GuiLua.c
@@ -147,383 +147,6 @@ globals ourGlobals;
147static const char *globName = "ourGlobals"; 147static const char *globName = "ourGlobals";
148 148
149 149
150void dumpStack(lua_State *L, int i)
151{
152 int type = lua_type(L, i);
153
154 switch (type)
155 {
156 case LUA_TNONE : printf("Stack %d is empty\n", i); break;
157 case LUA_TNIL : printf("Stack %d is a nil\n", i); break;
158 case LUA_TBOOLEAN : printf("Stack %d is a boolean - %d\n", i, lua_toboolean(L, i)); break;
159 case LUA_TNUMBER : printf("Stack %d is a number\n - %f", i, lua_tonumber(L, i)); break;
160 case LUA_TSTRING : printf("Stack %d is a string - %s\n", i, lua_tostring(L, i)); break;
161 case LUA_TFUNCTION : printf("Stack %d is a function\n", i); break;
162 case LUA_TTHREAD : printf("Stack %d is a thread\n", i); break;
163 case LUA_TTABLE :
164 {
165 int j;
166
167 printf("Stack %d is a table", i);
168 lua_getfield(L, i, "_NAME");
169 j = lua_gettop(L);
170 if (lua_isstring(L, j))
171 printf(" - %s", lua_tostring(L, j));
172 lua_pop(L, 1);
173 printf("\n");
174 break;
175 }
176 case LUA_TUSERDATA : printf("Stack %d is a userdata\n", i); break;
177 case LUA_TLIGHTUSERDATA : printf("Stack %d is a light userdata\n", i); break;
178 default : printf("Stack %d is unknown\n", i); break;
179 }
180}
181
182static char dateTime[DATE_TIME_LEN];
183
184static void _ggg_log_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args)
185{
186 FILE *f = data;
187 char dt[DATE_TIME_LEN + 1];
188 char fileTab[256], funcTab[256];
189
190 getDateTime(NULL, dt, NULL);
191 dt[19] = '\0';
192 if (12 > strlen(file))
193 snprintf(fileTab, sizeof(fileTab), "%s\t\t", file);
194 else
195 snprintf(fileTab, sizeof(fileTab), "%s\t", file);
196 snprintf(funcTab, sizeof(funcTab), "\t%s", fnc);
197 fprintf(f, "%s ", dt);
198 if (f == stderr)
199 eina_log_print_cb_stderr(d, level, fileTab, funcTab, line, fmt, data, args);
200 else if (f == stdout)
201 eina_log_print_cb_stdout(d, level, fileTab, funcTab, line, fmt, data, args);
202 fflush(f);
203}
204
205void loggingStartup(globals *ourGlobals)
206{
207 if (ourGlobals->logDom < 0)
208 {
209 ourGlobals->logDom = eina_log_domain_register("GuiLua", NULL);
210 if (ourGlobals->logDom < 0)
211 {
212 EINA_LOG_CRIT("could not register log domain 'GuiLua'");
213 return;
214 }
215 }
216 eina_log_level_set(EINA_LOG_LEVEL_DBG);
217 eina_log_domain_level_set("GuiLua", EINA_LOG_LEVEL_DBG);
218 eina_log_print_cb_set(_ggg_log_print_cb, stderr);
219
220 // Shut up the excess debugging shit from EFL.
221 eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN);
222}
223
224char *getDateTime(struct tm **nowOut, char *dateOut, time_t *timeOut)
225{
226 struct tm *newTime;
227 time_t szClock;
228 char *date = dateTime;
229
230 // Get time in seconds
231 time(&szClock);
232 // Convert time to struct tm form
233 newTime = localtime(&szClock);
234
235 if (nowOut)
236 *nowOut = newTime;
237 if (dateOut)
238 date = dateOut;
239 if (timeOut)
240 *timeOut = szClock;
241
242 // format
243 strftime(date, DATE_TIME_LEN, "%d/%m/%Y %H:%M:%S\r", newTime);
244 return (dateTime);
245}
246
247
248// These are what the various symbols are for each type -
249// int %
250// num #
251// str $
252// bool !
253// C func &
254// table.field @ Expects an integer and a string.
255// nil ~
256// table {} Starts and stops filling up a new table.
257// ( Just syntax sugar for call.
258// call ) Expects an integer, the number of results left after the call.
259// FIXME: Still to do, if we ever use them -
260// stack = Get a value from the stack, expects a stack index.
261// userdata +
262// lightuserdata *
263// thread ^
264
265static char *_push_name(lua_State *L, char *q, int *idx) // Stack usage [-0, +1, e or m]
266{
267 char *p = q;
268 char temp = '\0';
269
270 // A simplistic scan through an identifier, it's wrong, but it's quick,
271 // and we don't mind that it's wrong, coz this is only internal.
272 while (isalnum((int)*q))
273 q++;
274 temp = *q;
275 *q = '\0';
276 if (*idx > 0)
277 lua_getfield(L, *idx, p); // Stack usage [-0, +1, e]
278 else
279 {
280 if (p != q)
281 lua_pushstring(L, p); // Stack usage [-0, +1, m]
282 else
283 {
284 lua_pushnumber(L, (lua_Number) (0 - (*idx)));
285 (*idx)--;
286 }
287 }
288 *q = temp;
289
290 return q;
291}
292
293int pull_lua(lua_State *L, int i, char *params, ...) // Stack usage -
294 // if i is a table
295 // [-n, +n, e]
296 // else
297 // [-0, +0, -]
298{
299 va_list vl;
300 char *f = strdup(params);
301 char *p = f;
302 int n = 0, j = i, count = 0;
303 Eina_Bool table = EINA_FALSE;
304
305 if (!f) return -1;
306 va_start(vl, params);
307
308 if (lua_istable(L, i)) // Stack usage [-0, +0, -]
309 {
310 j = -1;
311 table = EINA_TRUE;
312 }
313
314 while (*p)
315 {
316 char *q;
317 Eina_Bool get = EINA_TRUE;
318
319 while (isspace((int)*p))
320 p++;
321 q = p + 1;
322 switch (*p)
323 {
324 case '%':
325 {
326 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, e]
327 if (lua_isnumber(L, j)) // Stack usage [-0, +0, -]
328 {
329 int *v = va_arg(vl, int *);
330 *v = lua_tointeger(L, j); // Stack usage [-0, +0, -]
331 n++;
332 }
333 break;
334 }
335 case '#':
336 {
337 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, e]
338 if (lua_isnumber(L, j)) // Stack usage [-0, +0, -]
339 {
340 double *v = va_arg(vl, double *);
341 *v = lua_tonumber(L, j); // Stack usage [-0, +0, -]
342 n++;
343 }
344 break;
345 }
346 case '$':
347 {
348 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, e]
349 if (lua_isstring(L, j)) // Stack usage [-0, +0, -]
350 {
351 char **v = va_arg(vl, char **);
352 size_t len;
353 char *temp = (char *) lua_tolstring(L, j, &len); // Stack usage [-0, +0, m]
354
355 len++; // Cater for the null at the end.
356 *v = malloc(len);
357 if (*v)
358 {
359 memcpy(*v, temp, len);
360 n++;
361 }
362 }
363 break;
364 }
365 case '!':
366 {
367 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, e]
368 if (lua_isboolean(L, j)) // Stack usage [-0, +0, -]
369 {
370 int *v = va_arg(vl, int *);
371 *v = lua_toboolean(L, j); // Stack usage [-0, +0, -]
372 n++;
373 }
374 break;
375 }
376 default:
377 {
378 get = EINA_FALSE;
379 break;
380 }
381 }
382
383 if (get)
384 {
385 if (table)
386 {
387 // If this is a table, then we pushed a value on the stack, pop it off.
388 lua_pop(L, 1); // Stack usage [-n, +0, -]
389 }
390 else
391 j++;
392 count++;
393 }
394 p = q;
395 }
396
397 va_end(vl);
398 free(f);
399 if (count > n)
400 n = 0;
401 else if (table)
402 n = 1;
403 return n;
404}
405
406int push_lua(lua_State *L, char *params, ...) // Stack usage [-0, +n, em]
407{
408 va_list vl;
409 char *f = strdup(params);
410 char *p = f;
411 int n = 0, table = 0, i = -1;
412
413 if (!f) return -1;
414
415 va_start(vl, params);
416
417 while (*p)
418 {
419 char *q;
420 Eina_Bool set = EINA_TRUE;
421
422 while (isspace((int)*p))
423 p++;
424 q = p + 1;
425 switch (*p)
426 {
427 case '%':
428 {
429 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
430 lua_pushinteger(L, va_arg(vl, int)); // Stack usage [-0, +1, -]
431 break;
432 }
433 case '#':
434 {
435 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
436 lua_pushnumber(L, va_arg(vl, double)); // Stack usage [-0, +1, -]
437 break;
438 }
439 case '$':
440 {
441 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
442 lua_pushstring(L, va_arg(vl, char *)); // Stack usage [-0, +1, m]
443 break;
444 }
445 case '!':
446 {
447 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
448 lua_pushboolean(L, va_arg(vl, int)); // Stack usage [-0, +1, -]
449 break;
450 }
451 case '=':
452 {
453 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
454 lua_pushvalue(L, va_arg(vl, int)); // Stack usage [-0, +1, -]
455 break;
456 }
457 case '@':
458 {
459 int tabl = va_arg(vl, int);
460 char *field = va_arg(vl, char *);
461
462 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
463 lua_getfield(L, tabl, field); // Stack usage [-0, +1, e]
464 break;
465 }
466 case '&':
467 {
468 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
469 lua_pushcfunction(L, va_arg(vl, void *)); // Stack usage [-0, +1, m]
470 break;
471 }
472 case '~':
473 {
474 if (table) q = _push_name(L, q, &i); // Stack usage [-0, +1, m]
475 lua_pushnil(L); // Stack usage [-0, +1, -]
476 break;
477 }
478 case '(': // Just syntax sugar.
479 {
480 set = EINA_FALSE;
481 break;
482 }
483 case ')':
484 {
485 lua_call(L, n - 1, va_arg(vl, int));
486 n = 0;
487 set = EINA_FALSE;
488 break;
489 }
490 case '{':
491 {
492 lua_newtable(L);
493 table++;
494 n++;
495 set = EINA_FALSE;
496 break;
497 }
498 case '}':
499 {
500 table--;
501 set = EINA_FALSE;
502 break;
503 }
504 default:
505 {
506 set = EINA_FALSE;
507 break;
508 }
509 }
510
511 if (set)
512 {
513 if (table > 0)
514 lua_settable(L, -3); // Stack usage [-2, +0, e]
515 else
516 n++;
517 }
518 p = q;
519 }
520
521 va_end(vl);
522 free(f);
523 return n;
524}
525
526
527// TODO - These functions should be able to deal with multiple windows. 150// TODO - These functions should be able to deal with multiple windows.
528// TODO - Should be able to open external and internal windows, and even switch between them on the fly. 151// TODO - Should be able to open external and internal windows, and even switch between them on the fly.
529static void _on_done(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) 152static void _on_done(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
@@ -742,7 +365,7 @@ int luaopen_libGuiLua(lua_State *L)
742 365
743 // In theory this function only ever gets called once. 366 // In theory this function only ever gets called once.
744 memset(&ourGlobals, 0, sizeof(globals)); 367 memset(&ourGlobals, 0, sizeof(globals));
745 loggingStartup(&ourGlobals); 368 ourGlobals.logDom = loggingStartup("GuiLua", ourGlobals.logDom);
746 369
747 elm_policy_set(ELM_POLICY_EXIT, ELM_POLICY_EXIT_NONE); 370 elm_policy_set(ELM_POLICY_EXIT, ELM_POLICY_EXIT_NONE);
748 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_NONE); 371 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_NONE);