summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rocklua.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/rocklua.c')
-rw-r--r--apps/plugins/lua/rocklua.c68
1 files changed, 53 insertions, 15 deletions
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
index 0d0b1f63f7..eb48fa2799 100644
--- a/apps/plugins/lua/rocklua.c
+++ b/apps/plugins/lua/rocklua.c
@@ -28,6 +28,8 @@
28#include "luadir.h" 28#include "luadir.h"
29#include "rocklib_events.h" 29#include "rocklib_events.h"
30 30
31static lua_State *Ls = NULL;
32static int lu_status = 0;
31 33
32static const luaL_Reg lualibs[] = { 34static const luaL_Reg lualibs[] = {
33 {"", luaopen_base}, 35 {"", luaopen_base},
@@ -142,41 +144,77 @@ static int docall (lua_State *L) {
142 return status; 144 return status;
143} 145}
144 146
147static void lua_atexit(void);
148static int loadfile_newstate(lua_State **L, const char *filename)
149{
150 *L = luaL_newstate();
151 rb_atexit(lua_atexit);
152 rocklua_openlibs(*L);
153 return luaL_loadfile(*L, filename);
154}
155
156static void lua_atexit(void)
157{
158 char *filename;
159
160 if(Ls && lua_gettop(Ls) > 1)
161 {
162 if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */
163 {
164 filename = (char *) malloc(MAX_PATH);
165
166 if (filename) /* out of memory? */
167 rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH);
168 lua_close(Ls); /* close old state */
169
170 lu_status = loadfile_newstate(&Ls, filename);
171
172 free(filename);
173 plugin_start(NULL);
174 }
175 else if (lua_tointeger(Ls, -1) != 0) /* os.exit */
176 {
177 lu_status = LUA_ERRRUN;
178 lua_pop(Ls, 1); /* put exit string on top of stack */
179 plugin_start(NULL);
180 }
181 }
182 _exit(0); /* don't call exit handler */
183}
145 184
146/***************** Plugin Entry Point *****************/ 185/***************** Plugin Entry Point *****************/
147enum plugin_status plugin_start(const void* parameter) 186enum plugin_status plugin_start(const void* parameter)
148{ 187{
149 const char* filename; 188 const char* filename;
150 int status;
151 189
152 if (parameter == NULL) 190 if (parameter == NULL)
153 { 191 {
192 if (!Ls)
154 rb->splash(HZ, "Play a .lua file!"); 193 rb->splash(HZ, "Play a .lua file!");
155 return PLUGIN_ERROR;
156 } 194 }
157 else 195 else
158 { 196 {
159 filename = (char*) parameter; 197 filename = (char*) parameter;
198 lu_status = loadfile_newstate(&Ls, filename);
199 }
160 200
161 lua_State *L = luaL_newstate(); 201 if (Ls)
162 202 {
163 rocklua_openlibs(L); 203 if (!lu_status) {
164 status = luaL_loadfile(L, filename);
165 if (!status) {
166 rb->lcd_scroll_stop(); /* rb doesn't like bg change while scroll */ 204 rb->lcd_scroll_stop(); /* rb doesn't like bg change while scroll */
167 rb->lcd_clear_display(); 205 rb->lcd_clear_display();
168 status = docall(L); 206 lu_status= docall(Ls);
169 } 207 }
170 208
171 if (status) { 209 if (lu_status) {
172 DEBUGF("%s\n", lua_tostring(L, -1)); 210 DEBUGF("%s\n", lua_tostring(Ls, -1));
173 rb->splashf(5 * HZ, "%s", lua_tostring(L, -1)); 211 rb->splash(5 * HZ, lua_tostring(Ls, -1));
174 lua_pop(L, 1); 212 /*lua_pop(Ls, 1);*/
175 } 213 }
176 214 lua_close(Ls);
177 lua_close(L);
178 } 215 }
216 else
217 return PLUGIN_ERROR;
179 218
180 return PLUGIN_OK; 219 return PLUGIN_OK;
181} 220}
182