aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ClientHamr
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-04-09 06:55:58 +1000
committerDavid Walter Seikel2014-04-09 06:55:58 +1000
commitac5c985c4ae6a35e085fa5f2e3d06d38508c7506 (patch)
treee9b020a4b02126cd58562a5a3af110647ba6fbe0 /ClientHamr
parentDebugging for test_c to deal with later. (diff)
downloadSledjHamr-ac5c985c4ae6a35e085fa5f2e3d06d38508c7506.zip
SledjHamr-ac5c985c4ae6a35e085fa5f2e3d06d38508c7506.tar.gz
SledjHamr-ac5c985c4ae6a35e085fa5f2e3d06d38508c7506.tar.bz2
SledjHamr-ac5c985c4ae6a35e085fa5f2e3d06d38508c7506.tar.xz
Make GuiLua a bit more real, with real Lua functions and stuff.
Diffstat (limited to 'ClientHamr')
-rw-r--r--ClientHamr/GuiLua/GuiLua.c362
1 files changed, 198 insertions, 164 deletions
diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c
index 03a7bd5..e3af5ee 100644
--- a/ClientHamr/GuiLua/GuiLua.c
+++ b/ClientHamr/GuiLua/GuiLua.c
@@ -174,112 +174,215 @@ struct _globals
174 Evas *canvas; // The canvas for drawing directly onto. 174 Evas *canvas; // The canvas for drawing directly onto.
175 Evas_Object *bg; // Our background edje. 175 Evas_Object *bg; // Our background edje.
176 lua_State *L; // Our Lua state. 176 lua_State *L; // Our Lua state.
177 int logDom; 177 int eina, logDom, ecore_evas, edje;
178}; 178};
179 179
180 globals ourGlobals;
180 181
181 182
182static const char *ourName = "widget"; 183static const char *ourName = "widget";
183int skang, _M; 184int skang, _M;
184 185
185static int wfunc (lua_State *L) 186/*
187static void dumpStack(lua_State *L, int i)
186{ 188{
187 double arg1 = luaL_checknumber(L, 1); 189 int type = lua_type(L, i);
188 const char *arg2 = luaL_checkstring(L, 2);
189 190
190 printf("Inside %s.wfunc(%f, %s)\n", ourName, arg1, arg2); 191 switch (type)
191 return 0; 192 {
193 case LUA_TNONE : printf("Stack %d is empty\n", i); break;
194 case LUA_TNIL : printf("Stack %d is a nil\n", i); break;
195 case LUA_TBOOLEAN : printf("Stack %d is a boolean\n", i); break;
196 case LUA_TNUMBER : printf("Stack %d is a number\n", i); break;
197 case LUA_TSTRING : printf("Stack %d is a string - %s\n", i, lua_tostring(L, i)); break;
198 case LUA_TFUNCTION : printf("Stack %d is a function\n", i); break;
199 case LUA_TTHREAD : printf("Stack %d is a thread\n", i); break;
200 case LUA_TTABLE : printf("Stack %d is a table\n", i); break;
201 case LUA_TUSERDATA : printf("Stack %d is a userdata\n", i); break;
202 case LUA_TLIGHTUSERDATA : printf("Stack %d is a light userdata\n", i); break;
203 default : printf("Stack %d is unknown\n", i); break;
204 }
192} 205}
206*/
193 207
194 208
195 209
196char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut); 210char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut);
197 211
198
199
200# define DATE_TIME_LEN 21 212# define DATE_TIME_LEN 21
201 213
202char dateTime[DATE_TIME_LEN]; 214char dateTime[DATE_TIME_LEN];
203 215
204static 216static 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)
205void _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)
206{ 217{
207 FILE *f = data; 218 FILE *f = data;
208 char dt[DATE_TIME_LEN + 1]; 219 char dt[DATE_TIME_LEN + 1];
209 char fileTab[256], funcTab[256]; 220 char fileTab[256], funcTab[256];
210 221
211 getDateTime(NULL, dt, NULL); 222 getDateTime(NULL, dt, NULL);
212 dt[19] = '\0'; 223 dt[19] = '\0';
213 if (12 > strlen(file)) 224 if (12 > strlen(file))
214 snprintf(fileTab, sizeof(fileTab), "%s\t\t", file); 225 snprintf(fileTab, sizeof(fileTab), "%s\t\t", file);
215 else 226 else
216 snprintf(fileTab, sizeof(fileTab), "%s\t", file); 227 snprintf(fileTab, sizeof(fileTab), "%s\t", file);
217 snprintf(funcTab, sizeof(funcTab), "\t%s", fnc); 228 snprintf(funcTab, sizeof(funcTab), "\t%s", fnc);
218 fprintf(f, "%s ", dt); 229 fprintf(f, "%s ", dt);
219 if (f == stderr) 230 if (f == stderr)
220 eina_log_print_cb_stderr(d, level, fileTab, funcTab, line, fmt, data, args); 231 eina_log_print_cb_stderr(d, level, fileTab, funcTab, line, fmt, data, args);
221 else if (f == stdout) 232 else if (f == stdout)
222 eina_log_print_cb_stdout(d, level, fileTab, funcTab, line, fmt, data, args); 233 eina_log_print_cb_stdout(d, level, fileTab, funcTab, line, fmt, data, args);
223 fflush(f); 234 fflush(f);
224} 235}
225 236
226void loggingStartup(globals *ourGlobals) 237void loggingStartup(globals *ourGlobals)
227{ 238{
239 if (ourGlobals->logDom < 0)
240 {
228 ourGlobals->logDom = eina_log_domain_register("GuiLua", NULL); 241 ourGlobals->logDom = eina_log_domain_register("GuiLua", NULL);
229 if (ourGlobals->logDom < 0) 242 if (ourGlobals->logDom < 0)
230 { 243 {
231 EINA_LOG_CRIT("could not register log domain 'GuiLua'"); 244 EINA_LOG_CRIT("could not register log domain 'GuiLua'");
245 return;
232 } 246 }
233 // TODO - should unregister this later. 247 }
234 eina_log_level_set(EINA_LOG_LEVEL_DBG); 248 eina_log_level_set(EINA_LOG_LEVEL_DBG);
235 eina_log_domain_level_set("GuiLua", EINA_LOG_LEVEL_DBG); 249 eina_log_domain_level_set("GuiLua", EINA_LOG_LEVEL_DBG);
236 eina_log_print_cb_set(_ggg_log_print_cb, stderr); 250 eina_log_print_cb_set(_ggg_log_print_cb, stderr);
237 251
238 // Shut up the excess debugging shit from EFL. 252 // Shut up the excess debugging shit from EFL.
239 eina_log_domain_level_set("eo", EINA_LOG_LEVEL_WARN); 253 eina_log_domain_level_set("eo", EINA_LOG_LEVEL_WARN);
240 eina_log_domain_level_set("eet", EINA_LOG_LEVEL_WARN); 254 eina_log_domain_level_set("eet", EINA_LOG_LEVEL_WARN);
241 eina_log_domain_level_set("ecore", EINA_LOG_LEVEL_WARN); 255 eina_log_domain_level_set("ecore", EINA_LOG_LEVEL_WARN);
242 eina_log_domain_level_set("ecore_audio", EINA_LOG_LEVEL_WARN); 256 eina_log_domain_level_set("ecore_audio", EINA_LOG_LEVEL_WARN);
243 eina_log_domain_level_set("ecore_evas", EINA_LOG_LEVEL_WARN); 257 eina_log_domain_level_set("ecore_evas", EINA_LOG_LEVEL_WARN);
244 eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN); 258 eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN);
245 eina_log_domain_level_set("ecore_system_upower", EINA_LOG_LEVEL_WARN); 259 eina_log_domain_level_set("ecore_system_upower", EINA_LOG_LEVEL_WARN);
246 eina_log_domain_level_set("ecore_x", EINA_LOG_LEVEL_WARN); 260 eina_log_domain_level_set("ecore_x", EINA_LOG_LEVEL_WARN);
247 eina_log_domain_level_set("evas_main", EINA_LOG_LEVEL_WARN); 261 eina_log_domain_level_set("evas_main", EINA_LOG_LEVEL_WARN);
248 eina_log_domain_level_set("eldbus", EINA_LOG_LEVEL_WARN); 262 eina_log_domain_level_set("eldbus", EINA_LOG_LEVEL_WARN);
249} 263}
250 264
251char *getDateTime(struct tm **nowOut, char *dateOut, time_t *timeOut) 265char *getDateTime(struct tm **nowOut, char *dateOut, time_t *timeOut)
252{ 266{
253 struct tm *newTime; 267 struct tm *newTime;
254 time_t szClock; 268 time_t szClock;
255 char *date = dateTime; 269 char *date = dateTime;
256 270
257 // Get time in seconds 271 // Get time in seconds
258 time(&szClock); 272 time(&szClock);
259 // Convert time to struct tm form 273 // Convert time to struct tm form
260 newTime = localtime(&szClock); 274 newTime = localtime(&szClock);
261 275
262 if (nowOut) 276 if (nowOut)
263 *nowOut = newTime; 277 *nowOut = newTime;
264 if (dateOut) 278 if (dateOut)
265 date = dateOut; 279 date = dateOut;
266 if (timeOut) 280 if (timeOut)
267 *timeOut = szClock; 281 *timeOut = szClock;
268 282
269 // format 283 // format
270 strftime(date, DATE_TIME_LEN, "%d/%m/%Y %H:%M:%S\r", newTime); 284 strftime(date, DATE_TIME_LEN, "%d/%m/%Y %H:%M:%S\r", newTime);
271 return (dateTime); 285 return (dateTime);
272} 286}
273 287
274 288
275 289
276static void 290static void _on_delete(Ecore_Evas *ee /*__UNUSED__*/)
277_on_delete(Ecore_Evas *ee /*__UNUSED__*/)
278{ 291{
279 ecore_main_loop_quit(); 292 ecore_main_loop_quit();
280} 293}
281 294
282 295
296static int openWindow(lua_State *L)
297{
298 globals *ourGlobals;
299
300 lua_getfield(L, LUA_REGISTRYINDEX, "ourGlobals");
301 ourGlobals = lua_touserdata(L, -1);
302 lua_pop(L, 1);
303
304 if ((ourGlobals->eina = eina_init()))
305 {
306 loggingStartup(ourGlobals);
307
308 PI("GuiLua running as an application.\n");
309
310 if ((ourGlobals->ecore_evas = ecore_evas_init()))
311 {
312 if ((ourGlobals->edje = edje_init()))
313 {
314 /* this will give you a window with an Evas canvas under the first engine available */
315 ourGlobals->ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
316 if (ourGlobals->ee)
317 {
318 ourGlobals->canvas = ecore_evas_get(ourGlobals->ee);
319 ecore_evas_title_set(ourGlobals->ee, "GuiLua test harness");
320 ecore_evas_show(ourGlobals->ee);
321 ecore_evas_callback_delete_request_set(ourGlobals->ee, _on_delete);
322 }
323 else
324 PC("You got to have at least one evas engine built and linked up to ecore-evas for this to run properly.");
325 }
326 else
327 PC("Failed to init edje!");
328 }
329 else
330 PC("Failed to init ecore_evas!");
331 }
332 else
333 fprintf(stderr, "Failed to init eina!\n");
334
335 return 0;
336}
337
338static int loopWindow(lua_State *L)
339{
340 globals *ourGlobals;
341
342 lua_getfield(L, LUA_REGISTRYINDEX, "ourGlobals");
343 ourGlobals = lua_touserdata(L, -1);
344 lua_pop(L, 1);
345
346 if (ourGlobals->canvas)
347 ecore_main_loop_begin();
348
349 return 0;
350}
351
352static int closeWindow(lua_State *L)
353{
354 globals *ourGlobals;
355
356 lua_getfield(L, LUA_REGISTRYINDEX, "ourGlobals");
357 ourGlobals = lua_touserdata(L, -1);
358 lua_pop(L, 1);
359
360 if (ourGlobals->eina)
361 {
362 if (ourGlobals->ecore_evas)
363 {
364 if (ourGlobals->edje)
365 {
366 if (ourGlobals->ee)
367 {
368 ecore_evas_free(ourGlobals->ee);
369 ourGlobals->ee = NULL;
370 }
371 ourGlobals->edje = edje_shutdown();
372 }
373 ourGlobals->ecore_evas = ecore_evas_shutdown();
374 }
375 if (ourGlobals->logDom >= 0)
376 {
377 eina_log_domain_unregister(ourGlobals->logDom);
378 ourGlobals->logDom = -1;
379 }
380 ourGlobals->eina = eina_shutdown();
381 }
382
383 return 0;
384}
385
283/* local widget = require 'widget' 386/* local widget = require 'widget'
284 387
285Lua's require() function will strip any stuff from the front of the name 388Lua's require() function will strip any stuff from the front of the name
@@ -294,8 +397,6 @@ returned, but we want to do something different with skang.
294*/ 397*/
295int luaopen_widget(lua_State *L) 398int luaopen_widget(lua_State *L)
296{ 399{
297 lua_Number i;
298
299 // In theory, the only thing on the stack now is 'widget' from the require() call. 400 // In theory, the only thing on the stack now is 'widget' from the require() call.
300 401
301// pseudo-indices, special tables that can be accessed like the stack - 402// pseudo-indices, special tables that can be accessed like the stack -
@@ -331,60 +432,34 @@ int luaopen_widget(lua_State *L)
331 // Save this module in the C registry. 432 // Save this module in the C registry.
332 lua_setfield(L, LUA_REGISTRYINDEX, ourName); 433 lua_setfield(L, LUA_REGISTRYINDEX, ourName);
333 434
334// This uses function{} style. 435 // Define our functions.
335// skang.thingasm{_M, 'wfooble,c', 'wfooble help text', 1, widget=\"'edit', 'The wfooble:', 1, 1, 10, 50\", required=true}
336 lua_getfield(L, skang, "thingasm"); 436 lua_getfield(L, skang, "thingasm");
337 i = 1;
338 lua_newtable(L);
339 lua_pushnumber(L, i++);
340 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. 437 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment.
341 lua_settable(L, -3); 438 lua_pushstring(L, "openWindow");
342 439 lua_pushstring(L, "Opens our window.");
343 lua_pushnumber(L, i++); 440 lua_pushcfunction(L, openWindow);
344 lua_pushstring(L, "wfooble,w"); 441 lua_call(L, 4, 0);
345 lua_settable(L, -3);
346
347 lua_pushnumber(L, i++);
348 lua_pushstring(L, "wfooble help text");
349 lua_settable(L, -3);
350
351 lua_pushnumber(L, i++);
352 lua_pushnumber(L, 1);
353 lua_settable(L, -3);
354
355 lua_pushstring(L, "'edit', 'The wfooble:', 1, 1, 10, 50");
356 lua_setfield(L, -2, "widget");
357 lua_pushboolean(L, 1); // Is required.
358 lua_setfield(L, -2, "required");
359 lua_call(L, 1, 0);
360 442
361// skang.thing(_M, 'wbar', 'Help text', 'Default')
362 lua_getfield(L, skang, "thingasm"); 443 lua_getfield(L, skang, "thingasm");
363 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. 444 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment.
364 lua_pushstring(L, "wbar"); 445 lua_pushstring(L, "loopWindow");
365 lua_pushstring(L, "Help text"); 446 lua_pushstring(L, "Run our windows main loop.");
366 lua_pushstring(L, "Default"); 447 lua_pushcfunction(L, loopWindow);
367 lua_call(L, 4, 0); 448 lua_call(L, 4, 0);
368 449
369// skang.thingasm(_M, 'cfoo')
370 lua_getfield(L, skang, "thingasm"); 450 lua_getfield(L, skang, "thingasm");
371 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. 451 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment.
372 lua_pushstring(L, "wfoo"); 452 lua_pushstring(L, "closeWindow");
373 lua_call(L, 2, 0); 453 lua_pushstring(L, "Closes our window.");
454 lua_pushcfunction(L, closeWindow);
455 lua_call(L, 4, 0);
374 456
375// skang.thingasm(_M, 'cfunc', 'cfunc does nothing really', cfunc, 'number,string') 457 lua_pop(L, openWindow(L));
376 lua_getfield(L, skang, "thingasm");
377 lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment.
378 lua_pushstring(L, "wfunc");
379 lua_pushstring(L, "wfunc does nothing really");
380 lua_pushcfunction(L, wfunc);
381 lua_pushstring(L, "number,string");
382 lua_call(L, 5, 0);
383 458
384// skang.moduleEnd(_M) 459// skang.moduleEnd(_M)
385 lua_getfield(L, skang, "moduleEnd"); 460 lua_getfield(L, skang, "moduleEnd");
386 lua_getfield(L, LUA_REGISTRYINDEX, ourName); 461 lua_getfield(L, LUA_REGISTRYINDEX, ourName);
387 lua_call(L, 1, 1); 462 lua_call(L, 1, 0);
388 463
389 return 1; 464 return 1;
390} 465}
@@ -392,72 +467,31 @@ int luaopen_widget(lua_State *L)
392 467
393int main(int argc, char **argv) 468int main(int argc, char **argv)
394{ 469{
395 globals ourGlobals;
396 int result = EXIT_FAILURE; 470 int result = EXIT_FAILURE;
397 471
398 memset(&ourGlobals, 0, sizeof(globals)); 472 memset(&ourGlobals, 0, sizeof(globals));
399 473
400 if (eina_init()) 474 ourGlobals.L = luaL_newstate();
475 if (ourGlobals.L)
401 { 476 {
402 loggingStartup(&ourGlobals); 477 luaL_openlibs(ourGlobals.L);
478 // Shove ourGlobals into the registry.
479 lua_pushlightuserdata(ourGlobals.L, &ourGlobals);
480 lua_setfield(ourGlobals.L, LUA_REGISTRYINDEX, "ourGlobals");
403 481
404 PIm("GuiLua running as an application.\n"); 482 // luaopen_widget() expects a string argument, and returns a table.
483 // Though in this case, both get ignored anyway.
484 lua_pushstring(ourGlobals.L, "widget");
485 lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L));
405 486
406 if (ecore_evas_init()) 487 // Run the main loop via a Lua call.
407 { 488 lua_pop(ourGlobals.L, loopWindow(ourGlobals.L));
408 if (edje_init())
409 {
410 /* this will give you a window with an Evas canvas under the first engine available */
411 ourGlobals.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
412 if (!ourGlobals.ee)
413 {
414 PEm("You got to have at least one evas engine built and linked up to ecore-evas for this example to run properly.");
415 edje_shutdown();
416 ecore_evas_shutdown();
417 return -1;
418 }
419 ourGlobals.canvas = ecore_evas_get(ourGlobals.ee);
420 ecore_evas_title_set(ourGlobals.ee, "GuiLua test harness");
421 ecore_evas_show(ourGlobals.ee);
422
423 ourGlobals.bg = evas_object_rectangle_add(ourGlobals.canvas);
424 evas_object_color_set(ourGlobals.bg, 255, 255, 255, 255); /* white bg */
425 evas_object_move(ourGlobals.bg, 0, 0); /* at canvas' origin */
426 evas_object_size_hint_weight_set(ourGlobals.bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
427 evas_object_resize(ourGlobals.bg, WIDTH, HEIGHT); /* covers full canvas */
428 evas_object_show(ourGlobals.bg);
429 ecore_evas_object_associate(ourGlobals.ee, ourGlobals.bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);
430 evas_object_focus_set(ourGlobals.bg, EINA_TRUE);
431
432 // Setup our callbacks.
433 ecore_evas_callback_delete_request_set(ourGlobals.ee, _on_delete);
434
435 ourGlobals.L = luaL_newstate();
436 if (ourGlobals.L)
437 {
438 luaL_openlibs(ourGlobals.L);
439 luaopen_widget(ourGlobals.L);
440
441 ecore_main_loop_begin();
442
443 lua_close(ourGlobals.L);
444 ecore_evas_free(ourGlobals.ee);
445 }
446 else
447 PCm("Failed to start Lua!");
448
449 edje_shutdown();
450 }
451 else
452 PCm("Failed to init edje!");
453 489
454 ecore_evas_shutdown(); 490 lua_pop(ourGlobals.L, closeWindow(ourGlobals.L));
455 } 491 lua_close(ourGlobals.L);
456 else
457 PCm("Failed to init ecore_evas!");
458 } 492 }
459 else 493 else
460 fprintf(stderr, "Failed to init eina!"); 494 fprintf(stderr, "Failed to start Lua!\n");
461 495
462 return result; 496 return result;
463} 497}