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". --- libraries/luaproc/Makefile | 3 ++- libraries/luaproc/luaproc.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- libraries/luaproc/luaproc.h | 3 ++- 3 files changed, 47 insertions(+), 4 deletions(-) (limited to 'libraries/luaproc') 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