From 85120c16cdf07c16127939201230796be424028c Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Fri, 3 Jan 2020 20:46:39 +0000 Subject: [PATCH] Add kiwmi:focused_view() and kiwmi:spawn() --- kiwmi/luak/kiwmi_server.c | 48 +++++++++++++++++++++++++++++++++++++++ kiwmi/main.c | 3 +++ 2 files changed, 51 insertions(+) diff --git a/kiwmi/luak/kiwmi_server.c b/kiwmi/luak/kiwmi_server.c index 6608a0e..bfa20f9 100644 --- a/kiwmi/luak/kiwmi_server.c +++ b/kiwmi/luak/kiwmi_server.c @@ -7,6 +7,10 @@ #include "luak/kiwmi_server.h" +#include + +#include + #include #include #include @@ -18,6 +22,26 @@ #include "luak/kiwmi_view.h" #include "server.h" +static int +l_kiwmi_server_focused_view(lua_State *L) +{ + struct kiwmi_server *server = + *(struct kiwmi_server **)luaL_checkudata(L, 1, "kiwmi_server"); + + if (!server->desktop.focused_view) { + return 0; + } + + lua_pushcfunction(L, luaK_kiwmi_view_new); + lua_pushlightuserdata(L, server->desktop.focused_view); + if (lua_pcall(L, 1, 1, 0)) { + wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1)); + return 0; + } + + return 1; +} + static int l_kiwmi_server_quit(lua_State *L) { @@ -29,6 +53,28 @@ l_kiwmi_server_quit(lua_State *L) return 0; } +static int +l_kiwmi_server_spawn(lua_State *L) +{ + luaL_checkudata(L, 1, "kiwmi_server"); + luaL_checktype(L, 2, LUA_TSTRING); + + const char *command = lua_tostring(L, 2); + + pid_t pid = fork(); + + if (pid < 0) { + return luaL_error(L, "Failed to run command (fork)"); + } + + if (pid == 0) { + execl("/bin/sh", "/bin/sh", "-c", command, NULL); + _exit(EXIT_FAILURE); + } + + return 0; +} + static int l_kiwmi_server_view_under_cursor(lua_State *L) { @@ -64,8 +110,10 @@ l_kiwmi_server_view_under_cursor(lua_State *L) } static const luaL_Reg kiwmi_server_methods[] = { + {"focused_view", l_kiwmi_server_focused_view}, {"on", luaK_callback_register_dispatch}, {"quit", l_kiwmi_server_quit}, + {"spawn", l_kiwmi_server_spawn}, {"view_under_cursor", l_kiwmi_server_view_under_cursor}, {NULL, NULL}, }; diff --git a/kiwmi/main.c b/kiwmi/main.c index b57fd0c..3a23f32 100644 --- a/kiwmi/main.c +++ b/kiwmi/main.c @@ -11,6 +11,7 @@ #include #include +#include #include @@ -70,6 +71,8 @@ main(int argc, char **argv) fprintf(stderr, "Using kiwmi v" KIWMI_VERSION "\n"); + signal(SIGCHLD, SIG_IGN); // prevent zombies from kiwmi:spawn() + if (!getenv("XDG_RUNTIME_DIR")) { wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR not set"); exit(EXIT_FAILURE);