Use Lua for config

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-08-10 07:25:01 +03:00
parent 4e23877697
commit 146c4e27cf
8 changed files with 504 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
#include "LunarWM.h"
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
@@ -127,6 +128,66 @@ bool LunarWM_Toplevel_update(LunarWM_Toplevel *this)
return true;
}
static void keysym_name(xkb_keysym_t sym, char *buf, size_t bufsz)
{
if (bufsz == 0)
return;
buf[0] = 0;
char tmp[128] = { 0 };
int n = xkb_keysym_get_name(sym, tmp, sizeof tmp);
if (n <= 0) {
snprintf(buf, bufsz, "Unknown");
return;
}
if (strlen(tmp) == 1) {
buf[0] = (char)toupper((unsigned char)tmp[0]);
buf[1] = 0;
} else {
snprintf(buf, bufsz, "%s", tmp);
}
}
static size_t fill_mod_list(uint32_t mods, char const *outv[4])
{
size_t k = 0;
if (mods & WLR_MODIFIER_LOGO)
outv[k++] = "Super";
if (mods & WLR_MODIFIER_SHIFT)
outv[k++] = "Shift";
if (mods & WLR_MODIFIER_CTRL)
outv[k++] = "Ctrl";
if (mods & WLR_MODIFIER_ALT)
outv[k++] = "Alt";
return k;
}
static char *make_hotkey_string(uint32_t mods, xkb_keysym_t sym)
{
char keyname[128];
keysym_name(sym, keyname, sizeof keyname);
char const *mods_list[4] = { 0 };
size_t mcount = fill_mod_list(mods, mods_list);
size_t need = strlen(keyname) + 1;
for (size_t i = 0; i < mcount; ++i)
need += strlen(mods_list[i]) + 1;
char *s = (char *)malloc(need);
if (!s)
return NULL;
s[0] = 0;
for (size_t i = 0; i < mcount; ++i) {
strcat(s, mods_list[i]);
strcat(s, "-");
}
strcat(s, keyname);
return s;
}
static void Keyboard_modifiers_notify(struct wl_listener *listener, void *)
{
auto *kbd
@@ -150,7 +211,7 @@ static void Keyboard_key_notify(struct wl_listener *listener, void *data)
xkb_keysym_t const keysym
= xkb_state_key_get_one_sym(kbd->wlr_keyboard->xkb_state, keycode);
bool const handled = false;
bool handled = false;
uint32_t const modifiers = wlr_keyboard_get_modifiers(kbd->wlr_keyboard);
if (server->wayland.session && event->state == WL_KEYBOARD_KEY_STATE_PRESSED
&& keysym >= XKB_KEY_XF86Switch_VT_1
@@ -160,8 +221,11 @@ static void Keyboard_key_notify(struct wl_listener *listener, void *data)
return;
}
if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
if ((modifiers & WLR_MODIFIER_LOGO) && syms[XKB_KEY_Q]) {
LunarWM_terminate(server);
for (int i = 0; i < nsyms; i++) {
if (trigger_ref_modsym(
server->cman->L, &server->cman->cfg, modifiers, syms[i])
== 0)
return; // handled
}
}
@@ -534,7 +598,7 @@ static bool init_xr(LunarWM *this)
wlr_log(WLR_ERROR, "Failed to get GLES graphics requirements");
return false;
}
printf("OpenGL ES range: %d.%d.%d - %d.%d.%d\n",
wlr_log(WLR_INFO, "OpenGL ES range: %d.%d.%d - %d.%d.%d\n",
XR_VERSION_MAJOR(reqs.minApiVersionSupported),
XR_VERSION_MINOR(reqs.minApiVersionSupported),
XR_VERSION_PATCH(reqs.minApiVersionSupported),
@@ -982,6 +1046,25 @@ static bool init_xr(LunarWM *this)
return true;
}
static int l_reload_config(lua_State *L)
{
ConfigManager *cm = g_wm.cman;
if (!cm) {
lua_pushnil(L);
return 1;
}
config_manager_reload(cm);
lua_pushnil(L);
return 1;
}
static int l_quit_compositor(lua_State *L)
{
LunarWM_terminate(&g_wm);
return 0;
}
bool LunarWM_init(LunarWM *this)
{
{ // Init defaults
@@ -999,6 +1082,22 @@ bool LunarWM_init(LunarWM *this)
this->renderer.camera.projection = CAMERA_PERSPECTIVE;
}
this->cman = config_manager_create(get_config_path());
assert(this->cman);
{
lua_State *L = this->cman->L;
lua_newtable(L);
lua_pushcfunction(L, l_quit_compositor);
lua_setfield(L, -2, "quit_compositor");
lua_pushcfunction(L, l_reload_config);
lua_setfield(L, -2, "reload_config");
lua_setglobal(L, "lunar");
config_manager_reload(this->cman);
}
if (getenv("DISPLAY") != nullptr || getenv("WAYLAND_DISPLAY") != nullptr) {
wlr_log(WLR_ERROR, "This compositor can only be ran in DRM mode.");
return false;