diff --git a/kiwmi/kiwmi/luak/kiwmi_server.c b/kiwmi/kiwmi/luak/kiwmi_server.c index de0ad14..e396b56 100644 --- a/kiwmi/kiwmi/luak/kiwmi_server.c +++ b/kiwmi/kiwmi/luak/kiwmi_server.c @@ -216,20 +216,21 @@ kiwmi_server_event_loop_fd_handler(int fd, uint32_t mask, void *data) lua_State *L = lc->server->lua->L; lua_rawgeti(L, LUA_REGISTRYINDEX, lc->callback_ref); - lua_pushvalue(L, -1); lua_pushinteger(L, fd); lua_pushinteger(L, mask); - if (lua_pcall(L, 2, 0, 0)) { + if (lua_pcall(L, 2, 1, 0)) { wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1)); } - /* - FIXME we need to call + + /* if callback returns false/nil, remove the handler */ + if (! lua_toboolean(L, -1)) { + wl_event_source_remove(lc->event_source); + luaL_unref(L, LUA_REGISTRYINDEX, lc->callback_ref); free(lc); - - when the file is closed, but probably not before then - */ + } + lua_pop(L, -1); return true; } @@ -242,7 +243,6 @@ l_kiwmi_server_event_loop_add_fd(lua_State *L) struct kiwmi_server *server = obj->object; - int fd = lua_tonumber(L, 2); int mode = lua_tonumber(L, 3); luaL_checktype(L, 4, LUA_TFUNCTION); // callback @@ -265,12 +265,14 @@ l_kiwmi_server_event_loop_add_fd(lua_State *L) /* luaL_ref pops last parameter (callback) from stack */ lc->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - wl_event_loop_add_fd(server->wl_event_loop, - fd, - event_types, - kiwmi_server_event_loop_fd_handler, - lc); - return true; + lc->event_source = + wl_event_loop_add_fd(server->wl_event_loop, + fd, + event_types, + kiwmi_server_event_loop_fd_handler, + lc); + + return 0; } diff --git a/kiwmi/lua_docs.md b/kiwmi/lua_docs.md index 607dddc..f9a8c8c 100644 --- a/kiwmi/lua_docs.md +++ b/kiwmi/lua_docs.md @@ -40,6 +40,9 @@ whenever there is activity on the file descriptor `fd`. The `mode` parameter is one of `O_RDWR`, `O_RDONLY`, `O_WRONLY` as defined in luaposix `posix.fcntl` module. +If `callback` returns false or nil, the handler will be unregistered +from the event loop and not called again. + #### kiwmi:focused_view() Returns the currently focused view. diff --git a/socket-repl.fnl b/socket-repl.fnl index 47f09cc..da95a99 100644 --- a/socket-repl.fnl +++ b/socket-repl.fnl @@ -21,16 +21,28 @@ )) (fn start [pathname] - (print pathname) (unistd.unlink pathname) (unix-socket-listener pathname (fn [fd] (let [sock (fdopen fd "w+") repl-coro (coroutine.create repl)] - (print :fd fd :sock sock) (watch-fd fd fcntl.O_RDONLY - #(coroutine.resume repl-coro (unistd.read $1 1024))) + (fn [fd] + (let [buf (unistd.read fd 1024) + input + (if (and buf (> (# buf) 0)) + buf + "\n,exit")] + (coroutine.resume repl-coro input)) + (if (= (coroutine.status repl-coro) :dead) + (do + (sock:write "bye!\n") + (sock:close) + (unistd.close fd) + false) + true + ))) (coroutine.resume repl-coro {:readChunk (fn [{: stack-size}]