commit ab942d8

0uppy  ·  2026-04-08 16:29:49 +0000 UTC
parent bcfe4ba
slgro 1.2 - Replaced the old config.h for a new Lua config system (1/2)
2 files changed,  +205, -0
+44, -0
 1@@ -0,0 +1,44 @@
 2+border_active          = 0xffffffff
 3+border_normal          = 0xffffffff
 4+border_width           = 2
 5+motion_throttle_hz     = 85
 6+
 7+terminal    = "foot"
 8+menu        = "neumenu-run"
 9+
10+binds = {
11+    { mods="MOD4",       key="q",      action="spawn",          arg=terminal },
12+    { mods="MOD4",       key="d",      action="spawn",          arg=menu },
13+    { mods="MOD4",       key="Tab",    action="focus_next" },
14+    { mods="MOD4",       key="f",      action="fullscreen" },
15+    { mods="MOD4",       key="c",      action="kill" },
16+    { mods="MOD4",       key="Right",  action="move_x",         arg=75 },
17+    { mods="MOD4",       key="Left",   action="move_x",         arg=-75 },
18+    { mods="MOD4",       key="Down",   action="move_y",         arg=75 },
19+    { mods="MOD4",       key="Up",     action="move_y",         arg=-75 },
20+    { mods="MOD4",       key="space",  action="center" },
21+    { mods="MOD4|SHFT",  key="Right",  action="resize_width",   arg=25 },
22+    { mods="MOD4|SHFT",  key="Left",   action="resize_width",   arg=-25 },
23+    { mods="MOD4|SHFT",  key="Down",   action="resize_height",  arg=25 },
24+    { mods="MOD4|SHFT",  key="Up",     action="resize_height",  arg=-25 },
25+    { mods="MOD4",       key="h",      action="snap_left" },
26+    { mods="MOD4",       key="l",      action="snap_right" },
27+    { mods="MOD4",       key="1",      action="workspace_goto",   arg=1 },
28+    { mods="MOD4",       key="2",      action="workspace_goto",   arg=2 },
29+    { mods="MOD4",       key="3",      action="workspace_goto",   arg=3 },
30+    { mods="MOD4",       key="4",      action="workspace_goto",   arg=4 },
31+    { mods="MOD4",       key="5",      action="workspace_goto",   arg=5 },
32+    { mods="MOD4",       key="6",      action="workspace_goto",   arg=6 },
33+    { mods="MOD4",       key="7",      action="workspace_goto",   arg=7 },
34+    { mods="MOD4",       key="8",      action="workspace_goto",   arg=8 },
35+    { mods="MOD4",       key="9",      action="workspace_goto",   arg=9 },
36+    { mods="MOD4|SHFT",  key="1",      action="workspace_moveto", arg=1 },
37+    { mods="MOD4|SHFT",  key="2",      action="workspace_moveto", arg=2 },
38+    { mods="MOD4|SHFT",  key="3",      action="workspace_moveto", arg=3 },
39+    { mods="MOD4|SHFT",  key="4",      action="workspace_moveto", arg=4 },
40+    { mods="MOD4|SHFT",  key="5",      action="workspace_moveto", arg=5 },
41+    { mods="MOD4|SHFT",  key="6",      action="workspace_moveto", arg=6 },
42+    { mods="MOD4|SHFT",  key="7",      action="workspace_moveto", arg=7 },
43+    { mods="MOD4|SHFT",  key="8",      action="workspace_moveto", arg=8 },
44+    { mods="MOD4|SHFT",  key="9",      action="workspace_moveto", arg=9 },
45+}
+161, -0
  1@@ -0,0 +1,161 @@
  2+#include <luajit-2.1/lua.h>
  3+#include <luajit-2.1/lauxlib.h>
  4+#include <luajit-2.1/lualib.h>
  5+#include <xkbcommon/xkbcommon-keysyms.h>
  6+#include <string.h>
  7+#include <stdlib.h>
  8+#include <stdio.h>
  9+
 10+#include "include/types.h"
 11+#include "include/slgro.h"
 12+#include "include/util.h"
 13+
 14+struct config cfg;
 15+struct bind *binds = NULL;
 16+size_t nbinds = 0;
 17+
 18+static char *spawn_args[128][2]; /* I'd ASSUME that 128 binds SHOULD be enough.........,..,., */
 19+static size_t nspawn = 0;
 20+
 21+static uint32_t parse_mods(const char *s) {
 22+
 23+    uint32_t mods = 0;
 24+    char buf[64];
 25+    strncpy(buf, s, sizeof(buf) - 1);
 26+    char *tok = strtok(buf, "|");
 27+
 28+    while (tok) {
 29+        if      (!strcmp(tok, "MOD4")) mods |= SWC_MOD_LOGO;
 30+        else if (!strcmp(tok, "MOD1")) mods |= SWC_MOD_ALT;
 31+        else if (!strcmp(tok, "SHFT")) mods |= SWC_MOD_SHIFT;
 32+        else if (!strcmp(tok, "CTRL")) mods |= SWC_MOD_CTRL;
 33+        tok = strtok(NULL, "|");
 34+    }
 35+
 36+    return mods;
 37+}
 38+
 39+static uint32_t parse_key(const char *s) {
 40+
 41+    if (s[1] == '\0') {
 42+        if (s[0] >= 'a' && s[0] <= 'z') return XKB_KEY_a + (s[0] - 'a');
 43+        if (s[0] >= '1' && s[0] <= '9') return XKB_KEY_1 + (s[0] - '1');
 44+        if (s[0] == '0') return XKB_KEY_0;
 45+    }
 46+
 47+    if (!strcmp(s, "Return")) return XKB_KEY_Return;
 48+    if (!strcmp(s, "Tab"))    return XKB_KEY_Tab;
 49+    if (!strcmp(s, "space"))  return XKB_KEY_space;
 50+    if (!strcmp(s, "Right"))  return XKB_KEY_Right;
 51+    if (!strcmp(s, "Left"))   return XKB_KEY_Left;
 52+    if (!strcmp(s, "Up"))     return XKB_KEY_Up;
 53+    if (!strcmp(s, "Down"))   return XKB_KEY_Down;
 54+    fprintf(stderr, "slgro: sorry, unknown key >.< '%s'\n", s);
 55+
 56+    return XKB_KEY_VoidSymbol;
 57+}
 58+
 59+static void resolve_action(const char *action, const char *arg_str, int arg_int, struct bind *b) {
 60+
 61+    b->type = SWC_BINDING_KEY;
 62+
 63+    if (!strcmp(action, "spawn")) {
 64+        spawn_args[nspawn][0] = strdup(arg_str);
 65+        spawn_args[nspawn][1] = NULL;
 66+        b->arg.v = spawn_args[nspawn++];
 67+        b->fn = spawn;
 68+    }
 69+    
 70+    else if (!strcmp(action, "kill"))             b->fn = kill_sel;
 71+    else if (!strcmp(action, "focus_next"))       b->fn = focus_next;
 72+    else if (!strcmp(action, "fullscreen"))       b->fn = fullscreen;
 73+    else if (!strcmp(action, "center"))           b->fn = center_window;
 74+    else if (!strcmp(action, "snap_left"))        b->fn = snap_left_half;
 75+    else if (!strcmp(action, "snap_right"))       b->fn = snap_right_half;
 76+    else if (!strcmp(action, "quit"))             b->fn = quit;
 77+    else if (!strcmp(action, "move_x"))         { b->fn = kb_move_x;        b->arg.i  = arg_int; }
 78+    else if (!strcmp(action, "move_y"))         { b->fn = kb_move_y;        b->arg.i  = arg_int; }
 79+    else if (!strcmp(action, "resize_width"))   { b->fn = kb_resize_width;  b->arg.i  = arg_int; }
 80+    else if (!strcmp(action, "resize_height"))  { b->fn = kb_resize_height; b->arg.i  = arg_int; }
 81+    else if (!strcmp(action, "workspace_goto"))   { b->fn = workspace_goto;   b->arg.ui = arg_int; }
 82+    else if (!strcmp(action, "workspace_moveto")) { b->fn = workspace_moveto; b->arg.ui = arg_int; }
 83+    else fprintf(stderr, "slgro: sorry, unknown action >.< '%s'\n", action);
 84+}
 85+
 86+void load_config(void) {
 87+
 88+    const char *home = getenv("HOME");
 89+    char path[256];
 90+    snprintf(path, sizeof(path), "%s/.config/slgro/config.lua", home ? home : ".");
 91+
 92+    cfg.motion_throttle_hz = 85;
 93+    cfg.border_col_active  = 0xffffffff;
 94+    cfg.border_col_normal  = 0xffffffff;
 95+    cfg.border_width       = 2;
 96+
 97+    lua_State *L = luaL_newstate();
 98+    luaL_openlibs(L);
 99+
100+    if (luaL_dofile(L, path) != LUA_OK) {
101+            if (luaL_dofile(L, "/usr/share/slgro/config.lua") != LUA_OK) {
102+                fprintf(stderr, "slgro: %s\n", lua_tostring(L, -1));
103+                fprintf(stderr, "slgro: using defaults :P\n");
104+                lua_close(L);
105+                return;
106+            }
107+    }
108+
109+    lua_getglobal(L, "border_active");
110+    if (lua_isnumber(L, -1)) cfg.border_col_active = lua_tonumber(L, -1);
111+    lua_pop(L, 1);
112+
113+    lua_getglobal(L, "border_normal");
114+    if (lua_isnumber(L, -1)) cfg.border_col_normal = lua_tonumber(L, -1);
115+    lua_pop(L, 1);
116+
117+    lua_getglobal(L, "border_width");
118+    if (lua_isnumber(L, -1)) cfg.border_width = lua_tonumber(L, -1);
119+    lua_pop(L, 1);
120+
121+    lua_getglobal(L, "motion_throttle_hz");
122+    if (lua_isnumber(L, -1)) cfg.motion_throttle_hz = lua_tonumber(L, -1);
123+    lua_pop(L, 1);
124+
125+    lua_getglobal(L, "binds");
126+    if (!lua_istable(L, -1)) {
127+        fprintf(stderr, "slgro: couldn't find the binds table :<\n");
128+        lua_close(L);
129+        return;
130+    }
131+
132+    size_t n = lua_objlen(L, -1);
133+    binds = calloc(n, sizeof(struct bind));
134+
135+    for (size_t i = 1; i <= n; i++) {
136+        lua_rawgeti(L, -1, i);
137+        struct bind b = {0};
138+
139+        lua_getfield(L, -1, "mods");
140+        if (lua_isstring(L, -1)) b.mods = parse_mods(lua_tostring(L, -1));
141+        lua_pop(L, 1);
142+
143+        lua_getfield(L, -1, "key");
144+        if (lua_isstring(L, -1)) b.ksym = parse_key(lua_tostring(L, -1));
145+        lua_pop(L, 1);
146+
147+        lua_getfield(L, -1, "action");
148+        const char *action = lua_tostring(L, -1);
149+        lua_pop(L, 1);
150+
151+        lua_getfield(L, -1, "arg");
152+        const char *arg_str = lua_isstring(L, -1)  ? lua_tostring(L, -1)  : "";
153+        int         arg_int = lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0;
154+        lua_pop(L, 1);
155+
156+        if (action) resolve_action(action, arg_str, arg_int, &b);
157+        binds[nbinds++] = b;
158+        lua_pop(L, 1);
159+    }
160+
161+    lua_close(L); /* man i love lua :3 */
162+}