Add kiwmi:focused_view() and kiwmi:spawn()

This commit is contained in:
buffet 2020-01-03 20:46:39 +00:00
parent ff9c63547d
commit 85120c16cd
2 changed files with 51 additions and 0 deletions

View file

@ -7,6 +7,10 @@
#include "luak/kiwmi_server.h" #include "luak/kiwmi_server.h"
#include <stdlib.h>
#include <unistd.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/types/wlr_cursor.h> #include <wlr/types/wlr_cursor.h>
@ -18,6 +22,26 @@
#include "luak/kiwmi_view.h" #include "luak/kiwmi_view.h"
#include "server.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 static int
l_kiwmi_server_quit(lua_State *L) l_kiwmi_server_quit(lua_State *L)
{ {
@ -29,6 +53,28 @@ l_kiwmi_server_quit(lua_State *L)
return 0; 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 static int
l_kiwmi_server_view_under_cursor(lua_State *L) 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[] = { static const luaL_Reg kiwmi_server_methods[] = {
{"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},
{"spawn", l_kiwmi_server_spawn},
{"view_under_cursor", l_kiwmi_server_view_under_cursor}, {"view_under_cursor", l_kiwmi_server_view_under_cursor},
{NULL, NULL}, {NULL, NULL},
}; };

View file

@ -11,6 +11,7 @@
#include <getopt.h> #include <getopt.h>
#include <limits.h> #include <limits.h>
#include <signal.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
@ -70,6 +71,8 @@ main(int argc, char **argv)
fprintf(stderr, "Using kiwmi v" KIWMI_VERSION "\n"); fprintf(stderr, "Using kiwmi v" KIWMI_VERSION "\n");
signal(SIGCHLD, SIG_IGN); // prevent zombies from kiwmi:spawn()
if (!getenv("XDG_RUNTIME_DIR")) { if (!getenv("XDG_RUNTIME_DIR")) {
wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR not set"); wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR not set");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);