From e484034467394120225df9e2435a18355b97ce86 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Tue, 8 Apr 2014 01:53:49 +1000 Subject: Whip up a quick and dirty GuiLua test bed by cutting and pasting bits from LuaSL and test_c. --- ClientHamr/GuiLua/GuiLua.c | 341 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 341 insertions(+) (limited to 'ClientHamr') diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c index d70aabc..03a7bd5 100644 --- a/ClientHamr/GuiLua/GuiLua.c +++ b/ClientHamr/GuiLua/GuiLua.c @@ -120,3 +120,344 @@ that has hard coded mappings between some basic "label", "button", etc. and ordinary elementary widgets. Proper introspection can come later. */ + + + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +typedef struct _globals globals; + + +#define WIDTH (300) +#define HEIGHT (300) + +#define PC(...) EINA_LOG_DOM_CRIT(ourGlobals->logDom, __VA_ARGS__) +#define PE(...) EINA_LOG_DOM_ERR(ourGlobals->logDom, __VA_ARGS__) +#define PW(...) EINA_LOG_DOM_WARN(ourGlobals->logDom, __VA_ARGS__) +#define PD(...) EINA_LOG_DOM_DBG(ourGlobals->logDom, __VA_ARGS__) +#define PI(...) EINA_LOG_DOM_INFO(ourGlobals->logDom, __VA_ARGS__) + +#define PCm(...) EINA_LOG_DOM_CRIT(ourGlobals.logDom, __VA_ARGS__) +#define PEm(...) EINA_LOG_DOM_ERR(ourGlobals.logDom, __VA_ARGS__) +#define PWm(...) EINA_LOG_DOM_WARN(ourGlobals.logDom, __VA_ARGS__) +#define PDm(...) EINA_LOG_DOM_DBG(ourGlobals.logDom, __VA_ARGS__) +#define PIm(...) EINA_LOG_DOM_INFO(ourGlobals.logDom, __VA_ARGS__) + +#define D() PD("DEBUG") + +// "01:03:52 01-01-1973\n\0" +#define DATE_TIME_LEN 21 + + +#ifndef FALSE +// NEVER change this +typedef enum +{ + FALSE = 0, + TRUE = 1 +} boolean; +#endif + +struct _globals +{ + Ecore_Evas *ee; // Our window. + Evas *canvas; // The canvas for drawing directly onto. + Evas_Object *bg; // Our background edje. + lua_State *L; // Our Lua state. + int logDom; +}; + + + +static const char *ourName = "widget"; +int skang, _M; + +static int wfunc (lua_State *L) +{ + double arg1 = luaL_checknumber(L, 1); + const char *arg2 = luaL_checkstring(L, 2); + + printf("Inside %s.wfunc(%f, %s)\n", ourName, arg1, arg2); + return 0; +} + + + +char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut); + + + +# define DATE_TIME_LEN 21 + +char dateTime[DATE_TIME_LEN]; + +static +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) +{ + FILE *f = data; + char dt[DATE_TIME_LEN + 1]; + char fileTab[256], funcTab[256]; + + getDateTime(NULL, dt, NULL); + dt[19] = '\0'; + if (12 > strlen(file)) + snprintf(fileTab, sizeof(fileTab), "%s\t\t", file); + else + snprintf(fileTab, sizeof(fileTab), "%s\t", file); + snprintf(funcTab, sizeof(funcTab), "\t%s", fnc); + fprintf(f, "%s ", dt); + if (f == stderr) + eina_log_print_cb_stderr(d, level, fileTab, funcTab, line, fmt, data, args); + else if (f == stdout) + eina_log_print_cb_stdout(d, level, fileTab, funcTab, line, fmt, data, args); + fflush(f); +} + +void loggingStartup(globals *ourGlobals) +{ + ourGlobals->logDom = eina_log_domain_register("GuiLua", NULL); + if (ourGlobals->logDom < 0) + { + EINA_LOG_CRIT("could not register log domain 'GuiLua'"); + } + // TODO - should unregister this later. + eina_log_level_set(EINA_LOG_LEVEL_DBG); + eina_log_domain_level_set("GuiLua", EINA_LOG_LEVEL_DBG); + eina_log_print_cb_set(_ggg_log_print_cb, stderr); + + // Shut up the excess debugging shit from EFL. + eina_log_domain_level_set("eo", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("eet", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore_audio", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore_evas", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore_system_upower", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("ecore_x", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("evas_main", EINA_LOG_LEVEL_WARN); + eina_log_domain_level_set("eldbus", EINA_LOG_LEVEL_WARN); +} + +char *getDateTime(struct tm **nowOut, char *dateOut, time_t *timeOut) +{ + struct tm *newTime; + time_t szClock; + char *date = dateTime; + + // Get time in seconds + time(&szClock); + // Convert time to struct tm form + newTime = localtime(&szClock); + + if (nowOut) + *nowOut = newTime; + if (dateOut) + date = dateOut; + if (timeOut) + *timeOut = szClock; + + // format + strftime(date, DATE_TIME_LEN, "%d/%m/%Y %H:%M:%S\r", newTime); + return (dateTime); +} + + + +static void +_on_delete(Ecore_Evas *ee /*__UNUSED__*/) +{ + ecore_main_loop_quit(); +} + + +/* local widget = require 'widget' + +Lua's require() function will strip any stuff from the front of the name +separated by a hyphen, so 'ClientHamr-GuiLua-test_c' -> 'test_c'. Then +it will search through a path, and eventually find this test_c.so (or +test_c.dll or whatever), then call luaopen_test_c(), which should return +a table. The argument (only thing on the stack) for this function will +be 'test_c'. + +Normally luaL_register() creates a table of functions, that is the table +returned, but we want to do something different with skang. +*/ +int luaopen_widget(lua_State *L) +{ + lua_Number i; + + // In theory, the only thing on the stack now is 'widget' from the require() call. + +// pseudo-indices, special tables that can be accessed like the stack - +// LUA_GLOBALSINDEX - thread environment, where globals are +// LUA_ENVIRONINDEX - C function environment, in this case luaopen_test_c() is the C function +// LUA_REGISTRYINDEX - C registry, global, for unique keys use the module name as a string, or a lightuserdata address to a C object in our module. +// lua_upvalueindex(n) - C function upvalues + +// The only locals we care about are skang and _M. +// All modules go into package.loaded[name] as well. +// skang is essentially a global anyway. +// _M we pass back as the result, and our functions get added to it by skang.thingasm() +// Not entirely true, _M is a proxy table, getmetatable(_M).__values[cfunc] would be our function. + +// local skang = require 'skang' + lua_getglobal(L, "require"); + lua_pushstring(L, "skang"); + lua_call(L, 1, 1); + skang = lua_gettop(L); + +// local _M = skang.moduleBegin('widget', nil, 'Copyright 2014 David Seikel', '0.1', '2014-04-08 00:42:00', nil, false) + lua_getfield(L, skang, "moduleBegin"); + lua_pushstring(L, ourName); + lua_pushnil(L); // Author comes from copyright. + lua_pushstring(L, "Copyright 2014 David Seikel"); + lua_pushstring(L, "0.1"); + lua_pushstring(L, "2014-04-08 00:42:00"); + lua_pushnil(L); // No skin. + lua_pushboolean(L, 0); // We are not a Lua module. + lua_call(L, 7, 1); // call 'skang.moduleBegin' with 7 arguments and 1 result. + + _M = lua_gettop(L); + // Save this module in the C registry. + lua_setfield(L, LUA_REGISTRYINDEX, ourName); + +// This uses function{} style. +// skang.thingasm{_M, 'wfooble,c', 'wfooble help text', 1, widget=\"'edit', 'The wfooble:', 1, 1, 10, 50\", required=true} + lua_getfield(L, skang, "thingasm"); + i = 1; + lua_newtable(L); + lua_pushnumber(L, i++); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. + lua_settable(L, -3); + + lua_pushnumber(L, i++); + lua_pushstring(L, "wfooble,w"); + lua_settable(L, -3); + + lua_pushnumber(L, i++); + lua_pushstring(L, "wfooble help text"); + lua_settable(L, -3); + + lua_pushnumber(L, i++); + lua_pushnumber(L, 1); + lua_settable(L, -3); + + lua_pushstring(L, "'edit', 'The wfooble:', 1, 1, 10, 50"); + lua_setfield(L, -2, "widget"); + lua_pushboolean(L, 1); // Is required. + lua_setfield(L, -2, "required"); + lua_call(L, 1, 0); + +// skang.thing(_M, 'wbar', 'Help text', 'Default') + lua_getfield(L, skang, "thingasm"); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. + lua_pushstring(L, "wbar"); + lua_pushstring(L, "Help text"); + lua_pushstring(L, "Default"); + lua_call(L, 4, 0); + +// skang.thingasm(_M, 'cfoo') + lua_getfield(L, skang, "thingasm"); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. + lua_pushstring(L, "wfoo"); + lua_call(L, 2, 0); + +// skang.thingasm(_M, 'cfunc', 'cfunc does nothing really', cfunc, 'number,string') + lua_getfield(L, skang, "thingasm"); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. + lua_pushstring(L, "wfunc"); + lua_pushstring(L, "wfunc does nothing really"); + lua_pushcfunction(L, wfunc); + lua_pushstring(L, "number,string"); + lua_call(L, 5, 0); + +// skang.moduleEnd(_M) + lua_getfield(L, skang, "moduleEnd"); + lua_getfield(L, LUA_REGISTRYINDEX, ourName); + lua_call(L, 1, 1); + + return 1; +} + + +int main(int argc, char **argv) +{ + globals ourGlobals; + int result = EXIT_FAILURE; + + memset(&ourGlobals, 0, sizeof(globals)); + + if (eina_init()) + { + loggingStartup(&ourGlobals); + + PIm("GuiLua running as an application.\n"); + + if (ecore_evas_init()) + { + if (edje_init()) + { + /* this will give you a window with an Evas canvas under the first engine available */ + ourGlobals.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL); + if (!ourGlobals.ee) + { + PEm("You got to have at least one evas engine built and linked up to ecore-evas for this example to run properly."); + edje_shutdown(); + ecore_evas_shutdown(); + return -1; + } + ourGlobals.canvas = ecore_evas_get(ourGlobals.ee); + ecore_evas_title_set(ourGlobals.ee, "GuiLua test harness"); + ecore_evas_show(ourGlobals.ee); + + ourGlobals.bg = evas_object_rectangle_add(ourGlobals.canvas); + evas_object_color_set(ourGlobals.bg, 255, 255, 255, 255); /* white bg */ + evas_object_move(ourGlobals.bg, 0, 0); /* at canvas' origin */ + evas_object_size_hint_weight_set(ourGlobals.bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_resize(ourGlobals.bg, WIDTH, HEIGHT); /* covers full canvas */ + evas_object_show(ourGlobals.bg); + ecore_evas_object_associate(ourGlobals.ee, ourGlobals.bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE); + evas_object_focus_set(ourGlobals.bg, EINA_TRUE); + + // Setup our callbacks. + ecore_evas_callback_delete_request_set(ourGlobals.ee, _on_delete); + + ourGlobals.L = luaL_newstate(); + if (ourGlobals.L) + { + luaL_openlibs(ourGlobals.L); + luaopen_widget(ourGlobals.L); + + ecore_main_loop_begin(); + + lua_close(ourGlobals.L); + ecore_evas_free(ourGlobals.ee); + } + else + PCm("Failed to start Lua!"); + + edje_shutdown(); + } + else + PCm("Failed to init edje!"); + + ecore_evas_shutdown(); + } + else + PCm("Failed to init ecore_evas!"); + } + else + fprintf(stderr, "Failed to init eina!"); + + return result; +} -- cgit v1.1