From f93cf73e01e9cf4b0a6be43d61ebc24b9fa64b0d Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 22 Feb 2012 23:46:56 +1000 Subject: Implement callAndReturn(), use it from callAndWait(). That's most of LuaSL's side of "use OpenSim to deal with in world stuff". --- LuaSL/src/LSL.lua | 13 +++++++++---- LuaSL/src/LuaSL.h | 29 ++++++++++++++++++----------- LuaSL/src/LuaSL_main.c | 12 +++++++++++- libraries/luaproc/Makefile | 3 ++- libraries/luaproc/luaproc.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- libraries/luaproc/luaproc.h | 3 ++- 6 files changed, 85 insertions(+), 20 deletions(-) diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua index 8861bf3..b8e2d4b 100644 --- a/LuaSL/src/LSL.lua +++ b/LuaSL/src/LSL.lua @@ -94,14 +94,19 @@ function args2string(doType, ...) return temp end -function mt.callAndReturn(name, ... ) - print("mt.callAndReturn(" .. name .. "(" .. args2string(true, ...) .. "))") +function mt.callAndReturn(name, ...) + luaproc.sendback(name .. "(" .. args2string(true, ...) .. ")") end -function mt.callAndWait(name, ... ) +function mt.callAndWait(name, ...) local func = functions[name] - print("mt.callAndWait(" .. name .. "(" .. args2string(true, ...) .. "))") + mt.callAndReturn(name, ...); + +--[[ TODO - do a luaproc sync receive() waiting for the result. + Eventually a sendForth() is called, which should end up passing through SendToChannel(). + The format of the result should be something like - SID.result({x=0.45, y=0.6, z=1.8}) +]] if "float" == func.Type then return 0.0 elseif "integer" == func.Type then return 0 diff --git a/LuaSL/src/LuaSL.h b/LuaSL/src/LuaSL.h index f6a5fad..6d495fa 100644 --- a/LuaSL/src/LuaSL.h +++ b/LuaSL/src/LuaSL.h @@ -49,17 +49,6 @@ typedef enum typedef struct { - char SID[PATH_MAX]; - char fileName[PATH_MAX]; - struct timeval startTime; - float compileTime; - int bugs, warnings; - boolean running; - -} script; - -typedef struct -{ Ecore_Evas *ee; // Our window. Evas *canvas; // The canvas for drawing directly onto. Evas_Object *bg; // Our background edje, also the game specific stuff. @@ -72,6 +61,24 @@ typedef struct boolean ui; // Wether we actually start up the UI. } gameGlobals; +typedef struct +{ + char SID[PATH_MAX]; + char fileName[PATH_MAX]; + struct timeval startTime; + float compileTime; + int bugs, warnings; + boolean running; + gameGlobals *game; + Ecore_Con_Client *client; +} script; + +typedef struct +{ + script *script; + const char message[PATH_MAX]; +} scriptMessage; + void loggingStartup(gameGlobals *game); char *getDateTime(struct tm **nowOut, char *dateOut, time_t *tiemOut); diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c index e56aaf3..daee816 100644 --- a/LuaSL/src/LuaSL_main.c +++ b/LuaSL/src/LuaSL_main.c @@ -6,6 +6,14 @@ static int CPUs = 4; static Eina_Strbuf *clientStream; +static void _sendBack(void * data) +{ + scriptMessage *message = data; + + sendBack(message->script->game, message->script->client, message->script->SID, message->message); + free(message); +} + static Eina_Bool _add(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev) { ecore_con_client_timeout_set(ev->client, 0); @@ -54,6 +62,8 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D gettimeofday(&me->startTime, NULL); strncpy(me->SID, SID, sizeof(me->SID)); strncpy(me->fileName, file, sizeof(me->fileName)); + me->game = game; + me->client = ev->client; eina_hash_add(game->scripts, me->SID, me); sendBack(game, ev->client, SID, "compiled(true)"); } @@ -69,7 +79,7 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D if (me) { sprintf(buf, "%s.lua.out", me->fileName); - newProc(buf, TRUE); + newProc(buf, TRUE, (Ecore_Cb) _sendBack, me); } } else if (0 == strcmp(command, "exit()")) diff --git a/libraries/luaproc/Makefile b/libraries/luaproc/Makefile index 1efc2d5..3ab61bf 100644 --- a/libraries/luaproc/Makefile +++ b/libraries/luaproc/Makefile @@ -33,7 +33,8 @@ LUA_LIB_PATH=/usr/lib/lua5.1 # standard makefile variables CC=gcc -CFLAGS=-c -Wall -fPIC -I${LUA_INC_PATH} +# TODO - hack, hard coded EFL include paths for now. +CFLAGS=-c -Wall -fPIC -I${LUA_INC_PATH} -I/opt/e17/include/eina-1 -I/opt/e17/include/eina-1/eina -I/opt/e17/include/ecore-1 LDFLAGS=-shared -L${LUA_LIB_PATH} -lpthread SOURCES=sched.c list.c luaproc.c channel.c OBJECTS=${SOURCES:.c=.o} diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c index ee0764a..7f4b2bb 100644 --- a/libraries/luaproc/luaproc.c +++ b/libraries/luaproc/luaproc.c @@ -41,6 +41,7 @@ THE SOFTWARE. #include "sched.h" #include "channel.h" + #define FALSE 0 #define TRUE 1 @@ -67,8 +68,18 @@ struct stluaproc { int args; channel chan; int destroyworker; + void *data; + Ecore_Cb callback; }; +/* TODO - hack, duplicating something from LuaSL for now. */ +typedef struct +{ + void *script; + char message[PATH_MAX]; +} scriptMessage; + + /****************************** * library functions prototypes ******************************/ @@ -90,6 +101,8 @@ static int luaproc_create_worker( lua_State *L ); static int luaproc_destroy_worker( lua_State *L ); /* set amount of lua processes that should be recycled (ie, reused) */ static int luaproc_recycle_set( lua_State *L ); +/* send a message back to the main loop */ +static int luaproc_send_back( lua_State *L ); /* luaproc function registration array - main (parent) functions */ static const struct luaL_reg luaproc_funcs_parent[] = { @@ -98,6 +111,7 @@ static const struct luaL_reg luaproc_funcs_parent[] = { { "createworker", luaproc_create_worker }, { "destroyworker", luaproc_destroy_worker }, { "recycle", luaproc_recycle_set }, + { "sendback", luaproc_send_back }, { NULL, NULL } }; @@ -111,6 +125,7 @@ static const struct luaL_reg luaproc_funcs_child[] = { { "createworker", luaproc_create_worker }, { "destroyworker", luaproc_destroy_worker }, { "recycle", luaproc_recycle_set }, + { "sendback", luaproc_send_back }, { NULL, NULL } }; @@ -359,7 +374,7 @@ static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { } -int newProc(const char *code, int file) +int newProc(const char *code, int file, Ecore_Cb callback, void *data) { /* new lua process pointer */ luaproc lp; @@ -383,6 +398,10 @@ int newProc(const char *code, int file) return 1; } + /* Stash any data and callback given to us. */ + lp->data = data; + lp->callback = callback; + /* increase active luaproc count */ sched_lpcount_inc(); @@ -405,7 +424,7 @@ static int luaproc_create_newproc( lua_State *L ) { /* check if first argument is a string (lua code) */ const char *code = luaL_checkstring( L, 1 ); - switch (newProc(code, FALSE)) + switch (newProc(code, FALSE, NULL, NULL)) { case 1 : /* in case of errors return nil + error msg */ @@ -496,6 +515,28 @@ luaproc luaproc_getself( lua_State *L ) { return lp; } +/* send a message to a lua process */ +static int luaproc_send_back( lua_State *L ) { + + luaproc self; + const char *message = luaL_checkstring( L, 1 ); + + self = luaproc_getself( L ); + if (self && self->callback && self->data) + { + scriptMessage *sm = calloc(1, sizeof(scriptMessage)); + + if (sm) + { + sm->script = self->data; + strcpy(sm->message, message); + ecore_main_loop_thread_safe_call_async(self->callback, sm); + } + } + + return 0; +} + /* error messages for the sendToChannel function */ const char *sendToChannelErrors[] = { diff --git a/libraries/luaproc/luaproc.h b/libraries/luaproc/luaproc.h index a5dde0d..1286107 100644 --- a/libraries/luaproc/luaproc.h +++ b/libraries/luaproc/luaproc.h @@ -29,6 +29,7 @@ THE SOFTWARE. #define _LUAPROC_H_ #include "channel.h" +#include /* process is idle */ #define LUAPROC_STAT_IDLE 0 @@ -46,7 +47,7 @@ typedef struct stluaproc *luaproc; void luaprocInit(void); void luaprocRegister(lua_State *L); -int newProc(const char *code, int file); +int newProc(const char *code, int file, Ecore_Cb callback, void *data); /* return a process' status */ -- cgit v1.1