From 292b57249487eda7e0d53ff68275019334f15440 Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Fri, 12 Jun 2009 23:08:27 +0800 Subject: [PATCH] A simple asynchronous input interface. Where does it belongs? Canvas? --- src/lua.c | 36 +++++++++++++++++++++++++++++++----- src/scripts/findwindow.lua | 8 ++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/lua.c b/src/lua.c index a5a53a0..0ef4ef5 100644 --- a/src/lua.c +++ b/src/lua.c @@ -85,6 +85,10 @@ typedef int lua_handler; #define LuaAllocHandler(a) a #define LuaFreeHandler(a) +void LuaPushHandler(lua_State *L, lua_handler lh); +void LuaHRef(lua_State *L, int key, int reg); +lua_handler LuaCheckHandler(lua_State *L, int idx, int ref); + /** Template {{{ */ #define CHECK_TYPE(name, type) \ @@ -628,10 +632,19 @@ screen_append_msg(lua_State *L) return 0; } +struct sinput_data +{ + lua_State *L; + lua_handler lh; +}; void script_input_fn(char *buf, int len, char *priv) { - lua_State *L = (lua_State *)priv; + struct sinput_data *sidata = (struct sinput_data *)priv; + lua_handler lh = sidata->lh; + lua_State *L = sidata->L; + + LuaPushHandler(L, lh); lua_pushstring(L, buf); if (lua_pcall(L, 1, 0, 0) == LUA_ERRRUN) { @@ -640,15 +653,26 @@ script_input_fn(char *buf, int len, char *priv) LuaShowErr(L); } } + free(sidata); + LuaHRef(L, lh, 0); } static int screen_input(lua_State *L) { char * prompt = NULL; + lua_handler lh; + struct sinput_data *sidata; prompt = (char *)luaL_checkstring(L, 1); - Input(prompt, 100, INP_COOKED, script_input_fn, (char *)L, 0); + lh = LuaCheckHandler(L, 2, 1); + sidata = (struct sinput_data *)malloc(sizeof(struct sinput_data)); + if (!sidata) + luaL_error(L, "Out of Memory"); + + sidata->L = L; + sidata->lh = lh; + Input(prompt, 100, INP_COOKED, script_input_fn, (char *)sidata, 0); return 0; } @@ -680,6 +704,8 @@ static const struct Xet_reg screen_getters[] = { /** }}} */ /** Public functions {{{ */ + +/* FIXME: Think about this: will it affect the registered handlers?*/ static lua_State *L; int LuaInit(void) { @@ -839,7 +865,7 @@ LuaPushParams(lua_State *L, const char *params, va_list va) return num; } -int LuaCall(char *func, char **argv) +int LuaCall(const char *func, const char **argv) { int argc; if (!L) @@ -1029,9 +1055,9 @@ LuaCheckHandler(lua_State *L, int idx, int ref) } int -LuaHdlrComp(lua_handler h1, lua_handler h2) +LuaHdlrComp(void *h1, void *h2) { - return h1 - h2; + return (lua_handler)h1 - (lua_handler)h2; } /* }}} **/ diff --git a/src/scripts/findwindow.lua b/src/scripts/findwindow.lua index ff393df..68b57ae 100644 --- a/src/scripts/findwindow.lua +++ b/src/scripts/findwindow.lua @@ -1,4 +1,12 @@ function find_window(name) + if name ~= nil then + find_window_internal(name) + else + screen.input("window to find:", find_window_internal) + end +end + +function find_window_internal(name) display = screen.display() canvases = display:get_canvases() for i, c in pairs(canvases) do -- 2.11.4.GIT