Add kiwmi:schedule

This commit is contained in:
buffet 2020-02-08 15:08:37 +00:00
parent af95af3362
commit 22a32e86a9
5 changed files with 76 additions and 1 deletions

View file

@ -16,7 +16,10 @@ struct kiwmi_lua_callback {
struct wl_list link; struct wl_list link;
struct kiwmi_server *server; struct kiwmi_server *server;
int callback_ref; int callback_ref;
union {
struct wl_event_source *event_source;
struct wl_listener listener; struct wl_listener listener;
};
}; };
int luaK_kiwmi_lua_callback_new(lua_State *L); int luaK_kiwmi_lua_callback_new(lua_State *L);

View file

@ -11,12 +11,14 @@
#include <stdbool.h> #include <stdbool.h>
#include <lua.h> #include <lua.h>
#include <wayland-server.h>
#include "server.h" #include "server.h"
struct kiwmi_lua { struct kiwmi_lua {
lua_State *L; lua_State *L;
int objects; int objects;
struct wl_list scheduled_callbacks;
struct wl_global *global; struct wl_global *global;
}; };

View file

@ -82,6 +82,60 @@ l_kiwmi_server_quit(lua_State *L)
return 0; return 0;
} }
static int
kiwmi_server_schedule_handler(void *data)
{
struct kiwmi_lua_callback *lc = data;
lua_State *L = lc->server->lua->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, lc->callback_ref);
lua_pushvalue(L, -1);
if (lua_pcall(L, 1, 0, 0)) {
wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1));
}
wl_event_source_remove(lc->event_source);
luaL_unref(L, LUA_REGISTRYINDEX, lc->callback_ref);
wl_list_remove(&lc->link);
free(lc);
return 0;
}
static int
l_kiwmi_server_schedule(lua_State *L)
{
struct kiwmi_object *obj =
*(struct kiwmi_object **)luaL_checkudata(L, 1, "kiwmi_server");
luaL_checktype(L, 2, LUA_TNUMBER); // delay
luaL_checktype(L, 3, LUA_TFUNCTION); // callback
struct kiwmi_server *server = obj->object;
struct kiwmi_lua_callback *lc = malloc(sizeof(*lc));
if (!lc) {
return luaL_error(L, "failed to allocate kiwmi_lua_callback");
}
int delay = lua_tonumber(L, 2);
lc->event_source = wl_event_loop_add_timer(server->wl_event_loop, kiwmi_server_schedule_handler, lc);
if (wl_event_source_timer_update(lc->event_source, delay) < 0) {
free(lc);
return luaL_error(L, "failed to arm timer");
}
lc->server = server;
lc->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
wl_list_insert(&server->lua->scheduled_callbacks, &lc->link);
return 0;
}
static int static int
l_kiwmi_server_spawn(lua_State *L) l_kiwmi_server_spawn(lua_State *L)
{ {
@ -159,6 +213,7 @@ static const luaL_Reg kiwmi_server_methods[] = {
{"focused_view", l_kiwmi_server_focused_view}, {"focused_view", l_kiwmi_server_focused_view},
{"on", luaK_callback_register_dispatch}, {"on", luaK_callback_register_dispatch},
{"quit", l_kiwmi_server_quit}, {"quit", l_kiwmi_server_quit},
{"schedule", l_kiwmi_server_schedule},
{"spawn", l_kiwmi_server_spawn}, {"spawn", l_kiwmi_server_spawn},
{"stop_interactive", l_kiwmi_server_stop_interactive}, {"stop_interactive", l_kiwmi_server_stop_interactive},
{"view_at", l_kiwmi_server_view_at}, {"view_at", l_kiwmi_server_view_at},

View file

@ -179,6 +179,8 @@ luaK_create(struct kiwmi_server *server)
luaL_openlibs(L); luaL_openlibs(L);
wl_list_init(&lua->scheduled_callbacks);
// init object registry // init object registry
lua_newtable(L); lua_newtable(L);
lua->objects = luaL_ref(L, LUA_REGISTRYINDEX); lua->objects = luaL_ref(L, LUA_REGISTRYINDEX);
@ -248,5 +250,13 @@ luaK_destroy(struct kiwmi_lua *lua)
{ {
lua_close(lua->L); lua_close(lua->L);
struct kiwmi_lua_callback *lc;
struct kiwmi_lua_callback *tmp;
wl_list_for_each_safe (lc, tmp, &lua->scheduled_callbacks, link) {
wl_event_source_remove(lc->event_source);
wl_list_remove(&lc->link);
free(lc);
}
free(lua); free(lua);
} }

View file

@ -29,6 +29,11 @@ Used to register event listeners.
Quit kiwmi. Quit kiwmi.
#### kiwmi:schedule(delay, callback)
Call `callback` after `delay` ms.
Callback get passed itself, so that it can easily reregister itself.
#### kiwmi:spawn(command) #### kiwmi:spawn(command)
Spawn a new process. Spawn a new process.