From 6af3a0c6c8b6ad20ea18767571560b6521b57a91 Mon Sep 17 00:00:00 2001 From: Guo Rui Date: Mon, 13 Jul 2009 00:21:11 +0800 Subject: [PATCH] Implement screen.schedule interface. This is used to run a function at specified time. Also fix a compile error in the previous commit. --- src/display.c | 2 +- src/lua.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/display.c b/src/display.c index 4ac4300..623b591 100644 --- a/src/display.c +++ b/src/display.c @@ -4199,7 +4199,7 @@ char *data; flayer = D_forecv->c_layer; fore = D_fore; #ifdef SCRIPT - trigger_sevent(display->d_sev.onidle, display); + trigger_sevent(&display->d_sev.onidle, display); #endif DoAction(&idleaction, -1); if (idleaction.nr == RC_BLANKER) diff --git a/src/lua.c b/src/lua.c index b7539ac..a4f198a 100644 --- a/src/lua.c +++ b/src/lua.c @@ -938,7 +938,7 @@ screen_input(lua_State *L) sidata = (struct sinput_data *)malloc(sizeof(struct sinput_data)); if (!sidata) { - Free(lh); + LuaFreeHandler(L, &lh); luaL_error(L, "Out of Memory"); } @@ -959,6 +959,59 @@ screen_input(lua_State *L) return 0; } +static void +sev_schedule_fn(struct event *ev, char *data) +{ + struct sinput_data *si = (struct sinput_data *)data; + lua_handler lh = si->lh; + lua_State *L = si->L; + free(si); + evdeq(ev); + Free(ev); + + LuaPushHandler(L, lh); + if (lua_pcall(L, 1, 0, 0) == LUA_ERRRUN) + { + if(lua_isstring(L, -1)) + { + LuaShowErr(L); + } + } + LuaFreeHandler(L, &lh); +} + +static int +screen_schedule(lua_State *L) +{ + int timeout = luaL_checkinteger(L, 1); + lua_handler lh = LuaCheckHandler(L, 2, 1); + struct sinput_data *si; + struct event *ev; + if (timeout <= 0) + return 0; + + ev = (struct event *) calloc(1, sizeof(struct event)); + if (!ev) + { + LuaFreeHandler(L, &lh); + luaL_error(L, "Out of Memory"); + } + si = (struct sinput_data *) malloc(sizeof(struct sinput_data)); + if (!si) + { + LuaFreeHandler(L, &lh); + Free(ev); + luaL_error(L, "Out of Memory"); + } + + ev->type = EV_TIMEOUT; + ev->data = (char *)si; + ev->handler = sev_schedule_fn; + evenq(ev); + SetTimeout(ev, timeout * 1000); + return 0; +} + static const luaL_reg screen_methods[] = { {"windows", screen_get_windows}, {"displays", screen_get_displays}, @@ -968,6 +1021,7 @@ static const luaL_reg screen_methods[] = { {"append_msg", screen_append_msg}, {"hook", LuaRegEvent}, {"input", screen_input}, + {"schedule", screen_schedule}, {0, 0} }; -- 2.11.4.GIT