tony / oxwm

Branch: e4b7180
Switch branches/tags
master
tonybanters Updated systray to support underlines in config.lua 12a7a17 2026-03-25
1477 lines · 43.3 KB Raw Blame History
1 const std = @import("std");
2 pub const config_mod = @import("config.zig");
3 const Config = config_mod.Config;
4 const Keybind = config_mod.Keybind;
5 const Action = config_mod.Action;
6 const Rule = config_mod.Rule;
7 const Block = config_mod.Block;
8 const ColorScheme = config_mod.ColorScheme;
9
10 const c = @cImport({
11 @cInclude("lua.h");
12 @cInclude("lauxlib.h");
13 @cInclude("lualib.h");
14 });
15
16 var L: ?*c.lua_State = null;
17 var config: ?*Config = null;
18
19 pub fn init(cfg: *Config) bool {
20 config = cfg;
21 L = c.luaL_newstate();
22 if (L == null) return false;
23 c.luaL_openlibs(L);
24 registerApi();
25 return true;
26 }
27
28 pub fn deinit() void {
29 if (L) |state| {
30 c.lua_close(state);
31 }
32 L = null;
33 config = null;
34 }
35
36 pub fn loadFile(path: []const u8) bool {
37 const state = L orelse return false;
38 var path_buf: [512]u8 = undefined;
39 if (path.len >= path_buf.len) return false;
40 @memcpy(path_buf[0..path.len], path);
41 path_buf[path.len] = 0;
42
43 if (std.mem.lastIndexOfScalar(u8, path, '/')) |last_slash| {
44 const dir = path[0..last_slash];
45 var setup_buf: [600]u8 = undefined;
46 const setup_code = std.fmt.bufPrintZ(&setup_buf, "package.path = '{s}/?.lua;' .. package.path", .{dir}) catch return false;
47 if (c.luaL_loadstring(state, setup_code.ptr) != 0 or c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) {
48 c.lua_settop(state, -2);
49 return false;
50 }
51 }
52
53 if (c.luaL_loadfilex(state, &path_buf, null) != 0) {
54 const err = c.lua_tolstring(state, -1, null);
55 if (err != null) {
56 std.debug.print("lua load error: {s}\n", .{std.mem.span(err)});
57 }
58 c.lua_settop(state, -2);
59 return false;
60 }
61
62 if (c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) {
63 const err = c.lua_tolstring(state, -1, null);
64 if (err != null) {
65 std.debug.print("lua runtime error: {s}\n", .{std.mem.span(err)});
66 }
67 c.lua_settop(state, -2);
68 return false;
69 }
70
71 return true;
72 }
73
74 pub fn loadConfig() bool {
75 const home = std.posix.getenv("HOME") orelse return false;
76 var path_buf: [512]u8 = undefined;
77 const path = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm/config.lua", .{home}) catch return false;
78 return loadFile(path);
79 }
80
81 fn registerApi() void {
82 const state = L orelse return;
83
84 c.lua_createtable(state, 0, 16);
85
86 registerSpawnFunctions(state);
87 registerKeyModule(state);
88 registerGapsModule(state);
89 registerBorderModule(state);
90 registerClientModule(state);
91 registerLayoutModule(state);
92 registerTagModule(state);
93 registerMonitorModule(state);
94 registerRuleModule(state);
95 registerBarModule(state);
96 registerMiscFunctions(state);
97
98 c.lua_setglobal(state, "oxwm");
99 }
100
101 fn registerSpawnFunctions(state: *c.lua_State) void {
102 c.lua_pushcfunction(state, luaSpawn);
103 c.lua_setfield(state, -2, "spawn");
104
105 c.lua_pushcfunction(state, luaSpawnTerminal);
106 c.lua_setfield(state, -2, "spawn_terminal");
107 }
108
109 fn registerKeyModule(state: *c.lua_State) void {
110 c.lua_createtable(state, 0, 2);
111
112 c.lua_pushcfunction(state, luaKeyBind);
113 c.lua_setfield(state, -2, "bind");
114
115 c.lua_pushcfunction(state, luaKeyChord);
116 c.lua_setfield(state, -2, "chord");
117
118 c.lua_setfield(state, -2, "key");
119 }
120
121 fn registerGapsModule(state: *c.lua_State) void {
122 c.lua_createtable(state, 0, 6);
123
124 c.lua_pushcfunction(state, luaGapsSetEnabled);
125 c.lua_setfield(state, -2, "set_enabled");
126
127 c.lua_pushcfunction(state, luaGapsEnable);
128 c.lua_setfield(state, -2, "enable");
129
130 c.lua_pushcfunction(state, luaGapsDisable);
131 c.lua_setfield(state, -2, "disable");
132
133 c.lua_pushcfunction(state, luaGapsSetInner);
134 c.lua_setfield(state, -2, "set_inner");
135
136 c.lua_pushcfunction(state, luaGapsSetOuter);
137 c.lua_setfield(state, -2, "set_outer");
138
139 c.lua_pushcfunction(state, luaGapsSetSmart);
140 c.lua_setfield(state, -2, "set_smart");
141
142 c.lua_setfield(state, -2, "gaps");
143 }
144
145 fn registerBorderModule(state: *c.lua_State) void {
146 c.lua_createtable(state, 0, 3);
147
148 c.lua_pushcfunction(state, luaBorderSetWidth);
149 c.lua_setfield(state, -2, "set_width");
150
151 c.lua_pushcfunction(state, luaBorderSetFocusedColor);
152 c.lua_setfield(state, -2, "set_focused_color");
153
154 c.lua_pushcfunction(state, luaBorderSetUnfocusedColor);
155 c.lua_setfield(state, -2, "set_unfocused_color");
156
157 c.lua_setfield(state, -2, "border");
158 }
159
160 fn registerClientModule(state: *c.lua_State) void {
161 c.lua_createtable(state, 0, 5);
162
163 c.lua_pushcfunction(state, luaClientKill);
164 c.lua_setfield(state, -2, "kill");
165
166 c.lua_pushcfunction(state, luaClientToggleFullscreen);
167 c.lua_setfield(state, -2, "toggle_fullscreen");
168
169 c.lua_pushcfunction(state, luaClientToggleFloating);
170 c.lua_setfield(state, -2, "toggle_floating");
171
172 c.lua_pushcfunction(state, luaClientFocusStack);
173 c.lua_setfield(state, -2, "focus_stack");
174
175 c.lua_pushcfunction(state, luaClientMoveStack);
176 c.lua_setfield(state, -2, "move_stack");
177
178 c.lua_setfield(state, -2, "client");
179 }
180
181 fn registerLayoutModule(state: *c.lua_State) void {
182 c.lua_createtable(state, 0, 4);
183
184 c.lua_pushcfunction(state, luaLayoutCycle);
185 c.lua_setfield(state, -2, "cycle");
186
187 c.lua_pushcfunction(state, luaLayoutSet);
188 c.lua_setfield(state, -2, "set");
189
190 c.lua_pushcfunction(state, luaLayoutScrollLeft);
191 c.lua_setfield(state, -2, "scroll_left");
192
193 c.lua_pushcfunction(state, luaLayoutScrollRight);
194 c.lua_setfield(state, -2, "scroll_right");
195
196 c.lua_setfield(state, -2, "layout");
197 }
198
199 fn registerTagModule(state: *c.lua_State) void {
200 c.lua_createtable(state, 0, 10);
201
202 c.lua_pushcfunction(state, luaTagView);
203 c.lua_setfield(state, -2, "view");
204
205 c.lua_pushcfunction(state, luaTagViewNext);
206 c.lua_setfield(state, -2, "view_next");
207
208 c.lua_pushcfunction(state, luaTagViewPrevious);
209 c.lua_setfield(state, -2, "view_previous");
210
211 c.lua_pushcfunction(state, luaTagViewNextNonempty);
212 c.lua_setfield(state, -2, "view_next_nonempty");
213
214 c.lua_pushcfunction(state, luaTagViewPreviousNonempty);
215 c.lua_setfield(state, -2, "view_previous_nonempty");
216
217 c.lua_pushcfunction(state, luaTagToggleview);
218 c.lua_setfield(state, -2, "toggleview");
219
220 c.lua_pushcfunction(state, luaTagMoveTo);
221 c.lua_setfield(state, -2, "move_to");
222
223 c.lua_pushcfunction(state, luaTagToggletag);
224 c.lua_setfield(state, -2, "toggletag");
225
226 c.lua_pushcfunction(state, luaTagSetBackAndForth);
227 c.lua_setfield(state, -2, "set_back_and_forth");
228
229 c.lua_setfield(state, -2, "tag");
230 }
231
232 fn registerMonitorModule(state: *c.lua_State) void {
233 c.lua_createtable(state, 0, 2);
234
235 c.lua_pushcfunction(state, luaMonitorFocus);
236 c.lua_setfield(state, -2, "focus");
237
238 c.lua_pushcfunction(state, luaMonitorTag);
239 c.lua_setfield(state, -2, "tag");
240
241 c.lua_setfield(state, -2, "monitor");
242 }
243
244 fn registerRuleModule(state: *c.lua_State) void {
245 c.lua_createtable(state, 0, 1);
246
247 c.lua_pushcfunction(state, luaRuleAdd);
248 c.lua_setfield(state, -2, "add");
249
250 c.lua_setfield(state, -2, "rule");
251 }
252
253 fn registerBarModule(state: *c.lua_State) void {
254 c.lua_createtable(state, 0, 10);
255
256 c.lua_pushcfunction(state, luaBarSetFont);
257 c.lua_setfield(state, -2, "set_font");
258
259 c.lua_pushcfunction(state, luaBarSetBlocks);
260 c.lua_setfield(state, -2, "set_blocks");
261
262 c.lua_pushcfunction(state, luaBarSetSchemeNormal);
263 c.lua_setfield(state, -2, "set_scheme_normal");
264
265 c.lua_pushcfunction(state, luaBarSetSchemeSelected);
266 c.lua_setfield(state, -2, "set_scheme_selected");
267
268 c.lua_pushcfunction(state, luaBarSetSchemeOccupied);
269 c.lua_setfield(state, -2, "set_scheme_occupied");
270
271 c.lua_pushcfunction(state, luaBarSetSchemeUrgent);
272 c.lua_setfield(state, -2, "set_scheme_urgent");
273
274 c.lua_pushcfunction(state, luaBarSetHideVacantTags);
275 c.lua_setfield(state, -2, "set_hide_vacant_tags");
276
277 c.lua_pushcfunction(state, luaBarSetPosition);
278 c.lua_setfield(state, -2, "set_position");
279
280 c.lua_createtable(state, 0, 6);
281
282 c.lua_pushcfunction(state, luaBarBlockRam);
283 c.lua_setfield(state, -2, "ram");
284
285 c.lua_pushcfunction(state, luaBarBlockDatetime);
286 c.lua_setfield(state, -2, "datetime");
287
288 c.lua_pushcfunction(state, luaBarBlockShell);
289 c.lua_setfield(state, -2, "shell");
290
291 c.lua_pushcfunction(state, luaBarBlockStatic);
292 c.lua_setfield(state, -2, "static");
293
294 c.lua_pushcfunction(state, luaBarBlockBattery);
295 c.lua_setfield(state, -2, "battery");
296
297 c.lua_pushcfunction(state, luaBarBlockSystray);
298 c.lua_setfield(state, -2, "systray");
299
300 c.lua_setfield(state, -2, "block");
301
302 c.lua_setfield(state, -2, "bar");
303 }
304
305 fn registerMiscFunctions(state: *c.lua_State) void {
306 c.lua_pushcfunction(state, luaSetTerminal);
307 c.lua_setfield(state, -2, "set_terminal");
308
309 c.lua_pushcfunction(state, luaSetLayout);
310 c.lua_setfield(state, -2, "set_layout");
311
312 c.lua_pushcfunction(state, luaSetModkey);
313 c.lua_setfield(state, -2, "set_modkey");
314
315 c.lua_pushcfunction(state, luaSetTags);
316 c.lua_setfield(state, -2, "set_tags");
317
318 c.lua_pushcfunction(state, luaSetLayoutSymbol);
319 c.lua_setfield(state, -2, "set_layout_symbol");
320
321 c.lua_pushcfunction(state, luaAutostart);
322 c.lua_setfield(state, -2, "autostart");
323
324 c.lua_pushcfunction(state, luaAutoTile);
325 c.lua_setfield(state, -2, "auto_tile");
326
327 c.lua_pushcfunction(state, luaTiledResizeMode);
328 c.lua_setfield(state, -2, "tiled_resize_mode");
329
330 c.lua_pushcfunction(state, luaQuit);
331 c.lua_setfield(state, -2, "quit");
332
333 c.lua_pushcfunction(state, luaRestart);
334 c.lua_setfield(state, -2, "restart");
335
336 c.lua_pushcfunction(state, luaToggleGaps);
337 c.lua_setfield(state, -2, "toggle_gaps");
338
339 c.lua_pushcfunction(state, luaToggleBar);
340 c.lua_setfield(state, -2, "toggle_bar");
341
342 c.lua_pushcfunction(state, luaShowKeybinds);
343 c.lua_setfield(state, -2, "show_keybinds");
344
345 c.lua_pushcfunction(state, luaSetMasterFactor);
346 c.lua_setfield(state, -2, "set_master_factor");
347
348 c.lua_pushcfunction(state, luaIncNumMaster);
349 c.lua_setfield(state, -2, "inc_num_master");
350
351 c.lua_pushcfunction(state, luaSetTagLayout);
352 c.lua_setfield(state, -2, "set_tag_layout");
353
354 c.lua_pushcfunction(state, luaSetFloatingPosition);
355 c.lua_setfield(state, -2, "set_floating_position");
356 }
357
358 fn createActionTable(state: *c.lua_State, action_name: [*:0]const u8) void {
359 c.lua_createtable(state, 0, 2);
360 _ = c.lua_pushstring(state, action_name);
361 c.lua_setfield(state, -2, "__action");
362 }
363
364 fn createActionTableWithInt(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
365 c.lua_createtable(state, 0, 2);
366 _ = c.lua_pushstring(state, action_name);
367 c.lua_setfield(state, -2, "__action");
368 c.lua_pushinteger(state, arg);
369 c.lua_setfield(state, -2, "__arg");
370 }
371
372 fn createActionTableWithString(state: *c.lua_State, action_name: [*:0]const u8) void {
373 c.lua_createtable(state, 0, 2);
374 _ = c.lua_pushstring(state, action_name);
375 c.lua_setfield(state, -2, "__action");
376 c.lua_pushvalue(state, 1);
377 c.lua_setfield(state, -2, "__arg");
378 }
379
380 fn luaSpawn(state: ?*c.lua_State) callconv(.c) c_int {
381 const s = state orelse return 0;
382 createActionTableWithString(s, "Spawn");
383 return 1;
384 }
385
386 fn luaSpawnTerminal(state: ?*c.lua_State) callconv(.c) c_int {
387 const s = state orelse return 0;
388 createActionTable(s, "SpawnTerminal");
389 return 1;
390 }
391
392 fn luaKeyBind(state: ?*c.lua_State) callconv(.c) c_int {
393 const s = state orelse return 0;
394 const cfg = config orelse return 0;
395
396 const mod_mask = parseModifiers(s, 1);
397 const key_str = getStringArg(s, 2) orelse return 0;
398 const keysym = keynameToKeysym(key_str) orelse return 0;
399
400 if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0;
401
402 _ = c.lua_getfield(s, 3, "__action");
403 const action_str = getLuaString(s, -1) orelse {
404 c.lua_settop(s, -2);
405 return 0;
406 };
407 c.lua_settop(s, -2);
408
409 const action = parseAction(action_str) orelse return 0;
410
411 var int_arg: i32 = 0;
412 var str_arg: ?[]const u8 = null;
413
414 _ = c.lua_getfield(s, 3, "__arg");
415 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
416 int_arg = @intCast(c.lua_tointegerx(s, -1, null));
417 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
418 str_arg = getLuaString(s, -1);
419 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
420 str_arg = extractSpawnCommand(s, -1);
421 }
422 c.lua_settop(s, -2);
423
424 var keybind: config_mod.Keybind = .{
425 .action = action,
426 .int_arg = int_arg,
427 .str_arg = str_arg,
428 };
429 keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym };
430 keybind.key_count = 1;
431
432 cfg.addKeybind(keybind) catch return 0;
433
434 return 0;
435 }
436
437 fn luaKeyChord(state: ?*c.lua_State) callconv(.c) c_int {
438 const s = state orelse return 0;
439 const cfg = config orelse return 0;
440
441 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
442 if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0;
443
444 var keybind: config_mod.Keybind = .{
445 .action = .quit,
446 .int_arg = 0,
447 .str_arg = null,
448 };
449 keybind.key_count = 0;
450
451 const num_keys = c.lua_rawlen(s, 1);
452 if (num_keys == 0 or num_keys > 4) return 0;
453
454 var i: usize = 1;
455 while (i <= num_keys) : (i += 1) {
456 _ = c.lua_rawgeti(s, 1, @intCast(i));
457 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
458 c.lua_settop(s, -2);
459 return 0;
460 }
461
462 _ = c.lua_rawgeti(s, -1, 1);
463 const mod_mask = parseModifiersAtTop(s);
464 c.lua_settop(s, -2);
465
466 _ = c.lua_rawgeti(s, -1, 2);
467 const key_str = getLuaString(s, -1) orelse {
468 c.lua_settop(s, -3);
469 return 0;
470 };
471 c.lua_settop(s, -2);
472
473 const keysym = keynameToKeysym(key_str) orelse {
474 c.lua_settop(s, -2);
475 return 0;
476 };
477
478 keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym };
479 keybind.key_count += 1;
480
481 c.lua_settop(s, -2);
482 }
483
484 _ = c.lua_getfield(s, 2, "__action");
485 const action_str = getLuaString(s, -1) orelse {
486 c.lua_settop(s, -2);
487 return 0;
488 };
489 c.lua_settop(s, -2);
490
491 keybind.action = parseAction(action_str) orelse return 0;
492
493 _ = c.lua_getfield(s, 2, "__arg");
494 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
495 keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null));
496 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
497 keybind.str_arg = getLuaString(s, -1);
498 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
499 keybind.str_arg = extractSpawnCommand(s, -1);
500 }
501
502 c.lua_settop(s, -2);
503 cfg.addKeybind(keybind) catch return 0;
504
505 return 0;
506 }
507
508 fn luaGapsSetEnabled(state: ?*c.lua_State) callconv(.c) c_int {
509 const cfg = config orelse return 0;
510 const s = state orelse return 0;
511 cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0;
512 return 0;
513 }
514
515 fn luaGapsEnable(state: ?*c.lua_State) callconv(.c) c_int {
516 _ = state;
517 const cfg = config orelse return 0;
518 cfg.gaps_enabled = true;
519 return 0;
520 }
521
522 fn luaGapsDisable(state: ?*c.lua_State) callconv(.c) c_int {
523 _ = state;
524 const cfg = config orelse return 0;
525 cfg.gaps_enabled = false;
526 return 0;
527 }
528
529 fn luaGapsSetInner(state: ?*c.lua_State) callconv(.c) c_int {
530 const cfg = config orelse return 0;
531 const s = state orelse return 0;
532 cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null));
533 cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null));
534 return 0;
535 }
536
537 fn luaGapsSetOuter(state: ?*c.lua_State) callconv(.c) c_int {
538 const cfg = config orelse return 0;
539 const s = state orelse return 0;
540 cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null));
541 cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null));
542 return 0;
543 }
544
545 fn luaGapsSetSmart(state: ?*c.lua_State) callconv(.c) c_int {
546 const cfg = config orelse return 0;
547 const s = state orelse return 0;
548 cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0;
549 return 0;
550 }
551
552 fn luaBorderSetWidth(state: ?*c.lua_State) callconv(.c) c_int {
553 const cfg = config orelse return 0;
554 const s = state orelse return 0;
555 cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null));
556 return 0;
557 }
558
559 fn luaBorderSetFocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
560 const cfg = config orelse return 0;
561 const s = state orelse return 0;
562 cfg.border_focused = parseColor(s, 1);
563 return 0;
564 }
565
566 fn luaBorderSetUnfocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
567 const cfg = config orelse return 0;
568 const s = state orelse return 0;
569 cfg.border_unfocused = parseColor(s, 1);
570 return 0;
571 }
572
573 fn luaClientKill(state: ?*c.lua_State) callconv(.c) c_int {
574 const s = state orelse return 0;
575 createActionTable(s, "KillClient");
576 return 1;
577 }
578
579 fn luaClientToggleFullscreen(state: ?*c.lua_State) callconv(.c) c_int {
580 const s = state orelse return 0;
581 createActionTable(s, "ToggleFullScreen");
582 return 1;
583 }
584
585 fn luaClientToggleFloating(state: ?*c.lua_State) callconv(.c) c_int {
586 const s = state orelse return 0;
587 createActionTable(s, "ToggleFloating");
588 return 1;
589 }
590
591 fn luaClientFocusStack(state: ?*c.lua_State) callconv(.c) c_int {
592 const s = state orelse return 0;
593 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
594 createActionTableWithInt(s, "FocusStack", dir);
595 return 1;
596 }
597
598 fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int {
599 const s = state orelse return 0;
600 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
601 createActionTableWithInt(s, "MoveStack", dir);
602 return 1;
603 }
604
605 fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int {
606 const s = state orelse return 0;
607 createActionTable(s, "CycleLayout");
608 return 1;
609 }
610
611 fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int {
612 const s = state orelse return 0;
613 createActionTableWithString(s, "ChangeLayout");
614 return 1;
615 }
616
617 fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int {
618 const s = state orelse return 0;
619 createActionTable(s, "ScrollLeft");
620 return 1;
621 }
622
623 fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
624 const s = state orelse return 0;
625 createActionTable(s, "ScrollRight");
626 return 1;
627 }
628
629 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
630 const s = state orelse return 0;
631 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
632 createActionTableWithInt(s, "ViewTag", idx);
633 return 1;
634 }
635
636 fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int {
637 const s = state orelse return 0;
638 createActionTable(s, "ViewNextTag");
639 return 1;
640 }
641
642 fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int {
643 const s = state orelse return 0;
644 createActionTable(s, "ViewPreviousTag");
645 return 1;
646 }
647
648 fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int {
649 const s = state orelse return 0;
650 createActionTable(s, "ViewNextNonEmptyTag");
651 return 1;
652 }
653
654 fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int {
655 const s = state orelse return 0;
656 createActionTable(s, "ViewPreviousNonEmptyTag");
657 return 1;
658 }
659
660 fn luaTagToggleview(state: ?*c.lua_State) callconv(.c) c_int {
661 const s = state orelse return 0;
662 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
663 createActionTableWithInt(s, "ToggleView", idx);
664 return 1;
665 }
666
667 fn luaTagMoveTo(state: ?*c.lua_State) callconv(.c) c_int {
668 const s = state orelse return 0;
669 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
670 createActionTableWithInt(s, "MoveToTag", idx);
671 return 1;
672 }
673
674 fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int {
675 const s = state orelse return 0;
676 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
677 createActionTableWithInt(s, "ToggleTag", idx);
678 return 1;
679 }
680
681 fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int {
682 const cfg = config orelse return 0;
683 const s = state orelse return 0;
684 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
685 return 0;
686 }
687
688 fn luaMonitorFocus(state: ?*c.lua_State) callconv(.c) c_int {
689 const s = state orelse return 0;
690 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
691 createActionTableWithInt(s, "FocusMonitor", dir);
692 return 1;
693 }
694
695 fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int {
696 const s = state orelse return 0;
697 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
698 createActionTableWithInt(s, "TagMonitor", dir);
699 return 1;
700 }
701
702 fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int {
703 const cfg = config orelse return 0;
704 const s = state orelse return 0;
705
706 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
707
708 var rule = Rule{
709 .class = null,
710 .instance = null,
711 .title = null,
712 .tags = 0,
713 .is_floating = false,
714 .monitor = -1,
715 .focus = false,
716 };
717
718 _ = c.lua_getfield(s, 1, "class");
719 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
720 rule.class = getLuaString(s, -1);
721 }
722 c.lua_settop(s, -2);
723
724 _ = c.lua_getfield(s, 1, "instance");
725 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
726 rule.instance = getLuaString(s, -1);
727 }
728 c.lua_settop(s, -2);
729
730 _ = c.lua_getfield(s, 1, "title");
731 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
732 rule.title = getLuaString(s, -1);
733 }
734 c.lua_settop(s, -2);
735
736 _ = c.lua_getfield(s, 1, "tag");
737 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
738 const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null));
739 if (tag_idx > 0) {
740 rule.tags = @as(u32, 1) << @intCast(tag_idx - 1);
741 }
742 }
743 c.lua_settop(s, -2);
744
745 _ = c.lua_getfield(s, 1, "floating");
746 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
747 rule.is_floating = c.lua_toboolean(s, -1) != 0;
748 }
749 c.lua_settop(s, -2);
750
751 _ = c.lua_getfield(s, 1, "monitor");
752 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
753 rule.monitor = @intCast(c.lua_tointegerx(s, -1, null));
754 }
755 c.lua_settop(s, -2);
756
757 _ = c.lua_getfield(s, 1, "focus");
758 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
759 rule.focus = c.lua_toboolean(s, -1) != 0;
760 }
761 c.lua_settop(s, -2);
762
763 cfg.addRule(rule) catch return 0;
764 return 0;
765 }
766
767 fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int {
768 const cfg = config orelse return 0;
769 const s = state orelse return 0;
770 if (dupeLuaString(s, 1)) |font| {
771 cfg.font = font;
772 }
773 return 0;
774 }
775
776 fn luaBarSetPosition(state: ?*c.lua_State) callconv(.c) c_int {
777 const cfg = config orelse return 0;
778 const s = state orelse return 0;
779 if (dupeLuaString(s, 1)) |position| {
780 cfg.bar_position = position;
781 }
782 return 0;
783 }
784
785 fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int {
786 const cfg = config orelse return 0;
787 const s = state orelse return 0;
788
789 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
790
791 const len = c.lua_rawlen(s, 1);
792 var i: usize = 1;
793 while (i <= len) : (i += 1) {
794 _ = c.lua_rawgeti(s, 1, @intCast(i));
795
796 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
797 c.lua_settop(s, -2);
798 continue;
799 }
800
801 if (parseBlockConfig(s, -1)) |block| {
802 cfg.addBlock(block) catch {};
803 }
804
805 c.lua_settop(s, -2);
806 }
807
808 return 0;
809 }
810
811 fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
812 _ = c.lua_getfield(state, idx, "__block_type");
813 const block_type_str = getLuaString(state, -1) orelse {
814 c.lua_settop(state, -2);
815 return null;
816 };
817 c.lua_settop(state, -2);
818
819 _ = c.lua_getfield(state, idx, "format");
820 const format = dupeLuaString(state, -1) orelse "";
821 c.lua_settop(state, -2);
822
823 _ = c.lua_getfield(state, idx, "interval");
824 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
825 c.lua_settop(state, -2);
826
827 _ = c.lua_getfield(state, idx, "color");
828 const color = parseColor(state, -1);
829 c.lua_settop(state, -2);
830
831 _ = c.lua_getfield(state, idx, "underline");
832 const underline = c.lua_toboolean(state, -1) != 0;
833 c.lua_settop(state, -2);
834
835 _ = c.lua_getfield(state, idx, "click");
836 const click = parseClickAction(state, -1);
837 c.lua_settop(state, -2);
838
839 var block = Block{
840 .block_type = .static,
841 .format = format,
842 .interval = interval,
843 .color = color,
844 .underline = underline,
845 .click = click,
846 };
847
848 if (std.mem.eql(u8, block_type_str, "Ram")) {
849 block.block_type = .ram;
850 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
851 block.block_type = .datetime;
852 _ = c.lua_getfield(state, idx, "__arg");
853 block.datetime_format = dupeLuaString(state, -1);
854 c.lua_settop(state, -2);
855 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
856 block.block_type = .shell;
857 _ = c.lua_getfield(state, idx, "__arg");
858 block.command = dupeLuaString(state, -1);
859 c.lua_settop(state, -2);
860 } else if (std.mem.eql(u8, block_type_str, "Static")) {
861 block.block_type = .static;
862 _ = c.lua_getfield(state, idx, "__arg");
863 if (dupeLuaString(state, -1)) |text| {
864 block.format = text;
865 }
866 c.lua_settop(state, -2);
867 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
868 block.block_type = .battery;
869 _ = c.lua_getfield(state, idx, "__arg");
870 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
871 _ = c.lua_getfield(state, -1, "charging");
872 block.format_charging = dupeLuaString(state, -1);
873 c.lua_settop(state, -2);
874
875 _ = c.lua_getfield(state, -1, "discharging");
876 block.format_discharging = dupeLuaString(state, -1);
877 c.lua_settop(state, -2);
878
879 _ = c.lua_getfield(state, -1, "full");
880 block.format_full = dupeLuaString(state, -1);
881 c.lua_settop(state, -2);
882
883 _ = c.lua_getfield(state, -1, "battery_name");
884 block.battery_name = dupeLuaString(state, -1);
885 c.lua_settop(state, -2);
886 }
887 c.lua_settop(state, -2);
888 } else if (std.mem.eql(u8, block_type_str, "Systray")) {
889 block.block_type = .systray;
890 } else {
891 return null;
892 }
893
894 return block;
895 }
896
897 fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int {
898 const cfg = config orelse return 0;
899 const s = state orelse return 0;
900 cfg.scheme_normal = parseScheme(s);
901 return 0;
902 }
903
904 fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int {
905 const cfg = config orelse return 0;
906 const s = state orelse return 0;
907 cfg.scheme_selected = parseScheme(s);
908 return 0;
909 }
910
911 fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int {
912 const cfg = config orelse return 0;
913 const s = state orelse return 0;
914 cfg.scheme_occupied = parseScheme(s);
915 return 0;
916 }
917
918 fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int {
919 const cfg = config orelse return 0;
920 const s = state orelse return 0;
921 cfg.scheme_urgent = parseScheme(s);
922 return 0;
923 }
924
925 fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int {
926 const cfg = config orelse return 0;
927 const s = state orelse return 0;
928 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
929 return 0;
930 }
931
932 fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int {
933 const s = state orelse return 0;
934 createBlockTable(s, "Ram", null);
935 return 1;
936 }
937
938 fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int {
939 const s = state orelse return 0;
940 _ = c.lua_getfield(s, 1, "date_format");
941 const date_format = getLuaString(s, -1);
942 c.lua_settop(s, -2);
943 createBlockTable(s, "DateTime", date_format);
944 return 1;
945 }
946
947 fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int {
948 const s = state orelse return 0;
949 _ = c.lua_getfield(s, 1, "command");
950 const command = getLuaString(s, -1);
951 c.lua_settop(s, -2);
952 createBlockTable(s, "Shell", command);
953 return 1;
954 }
955
956 fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
957 const s = state orelse return 0;
958 _ = c.lua_getfield(s, 1, "text");
959 const text = getLuaString(s, -1);
960 c.lua_settop(s, -2);
961 createBlockTable(s, "Static", text);
962 return 1;
963 }
964
965 fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int {
966 const s = state orelse return 0;
967 createBlockTable(s, "Systray", null);
968 return 1;
969 }
970
971 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
972 const s = state orelse return 0;
973
974 c.lua_createtable(s, 0, 7);
975
976 _ = c.lua_pushstring(s, "Battery");
977 c.lua_setfield(s, -2, "__block_type");
978
979 _ = c.lua_getfield(s, 1, "format");
980 c.lua_setfield(s, -2, "format");
981
982 _ = c.lua_getfield(s, 1, "interval");
983 c.lua_setfield(s, -2, "interval");
984
985 _ = c.lua_getfield(s, 1, "color");
986 c.lua_setfield(s, -2, "color");
987
988 _ = c.lua_getfield(s, 1, "underline");
989 c.lua_setfield(s, -2, "underline");
990
991 _ = c.lua_getfield(s, 1, "click");
992 c.lua_setfield(s, -2, "click");
993
994 c.lua_createtable(s, 0, 4);
995 _ = c.lua_getfield(s, 1, "charging");
996 c.lua_setfield(s, -2, "charging");
997 _ = c.lua_getfield(s, 1, "discharging");
998 c.lua_setfield(s, -2, "discharging");
999 _ = c.lua_getfield(s, 1, "full");
1000 c.lua_setfield(s, -2, "full");
1001 _ = c.lua_getfield(s, 1, "battery_name");
1002 c.lua_setfield(s, -2, "battery_name");
1003 c.lua_setfield(s, -2, "__arg");
1004
1005 return 1;
1006 }
1007
1008 fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
1009 c.lua_createtable(state, 0, 7);
1010
1011 _ = c.lua_pushstring(state, block_type);
1012 c.lua_setfield(state, -2, "__block_type");
1013
1014 _ = c.lua_getfield(state, 1, "format");
1015 c.lua_setfield(state, -2, "format");
1016
1017 _ = c.lua_getfield(state, 1, "interval");
1018 c.lua_setfield(state, -2, "interval");
1019
1020 _ = c.lua_getfield(state, 1, "color");
1021 c.lua_setfield(state, -2, "color");
1022
1023 _ = c.lua_getfield(state, 1, "underline");
1024 c.lua_setfield(state, -2, "underline");
1025
1026 _ = c.lua_getfield(state, 1, "click");
1027 c.lua_setfield(state, -2, "click");
1028
1029 if (arg) |a| {
1030 var buf: [256]u8 = undefined;
1031 if (a.len < buf.len) {
1032 @memcpy(buf[0..a.len], a);
1033 buf[a.len] = 0;
1034 _ = c.lua_pushstring(state, &buf);
1035 c.lua_setfield(state, -2, "__arg");
1036 }
1037 }
1038 }
1039
1040 fn luaSetFloatingPosition(state: ?*c.lua_State) callconv(.c) c_int {
1041 const cfg = config orelse return 0;
1042 const s = state orelse return 0;
1043 if (getLuaString(s, 1)) |name| {
1044 if (config_mod.FloatingPosition.fromString(name)) |pos| {
1045 cfg.floating_position = pos;
1046 }
1047 }
1048 return 0;
1049 }
1050
1051 fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int {
1052 const cfg = config orelse return 0;
1053 const s = state orelse return 0;
1054 if (dupeLuaString(s, 1)) |term| {
1055 cfg.terminal = term;
1056 }
1057 return 0;
1058 }
1059
1060 fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int {
1061 const cfg = config orelse return 0;
1062 const s = state orelse return 0;
1063 const name = getStringArg(s, 1) orelse return 0;
1064 if (config_mod.Layouts.fromString(name) == null) {
1065 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
1066 return 0;
1067 }
1068 if (dupeLuaString(s, 1)) |layout| {
1069 cfg.layout = layout;
1070 }
1071 return 0;
1072 }
1073
1074 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1075 const cfg = config orelse return 0;
1076 const s = state orelse return 0;
1077 if (getStringArg(s, 1)) |modkey_str| {
1078 cfg.modkey = parseSingleModifier(modkey_str);
1079 cfg.addButton(.{
1080 .click = .client_win,
1081 .mod_mask = cfg.modkey,
1082 .button = 1,
1083 .action = .move_mouse,
1084 }) catch {};
1085 cfg.addButton(.{
1086 .click = .client_win,
1087 .mod_mask = cfg.modkey,
1088 .button = 3,
1089 .action = .resize_mouse,
1090 }) catch {};
1091 }
1092 return 0;
1093 }
1094
1095 fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1096 const cfg = config orelse return 0;
1097 const s = state orelse return 0;
1098
1099 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
1100
1101 const len = c.lua_rawlen(s, 1);
1102 var i: usize = 0;
1103 while (i < len and i < 9) : (i += 1) {
1104 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1105 if (dupeLuaString(s, -1)) |tag_str| {
1106 cfg.tags[i] = tag_str;
1107 }
1108 c.lua_settop(s, -2);
1109 }
1110
1111 return 0;
1112 }
1113
1114 fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1115 const cfg = config orelse return 0;
1116 const s = state orelse return 0;
1117 if (dupeLuaString(s, 1)) |cmd| {
1118 cfg.addAutostart(cmd) catch return 0;
1119 }
1120 return 0;
1121 }
1122
1123 fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1124 const cfg = config orelse return 0;
1125 const s = state orelse return 0;
1126 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1127 return 0;
1128 }
1129
1130 fn luaTiledResizeMode(state: ?*c.lua_State) callconv(.c) c_int {
1131 const cfg = config orelse return 0;
1132 const s = state orelse return 0;
1133 cfg.tiled_resize_mode = c.lua_toboolean(s, 1) != 0;
1134 return 0;
1135 }
1136
1137 fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1138 const cfg = config orelse return 0;
1139 const s = state orelse return 0;
1140 const name = getStringArg(s, 1) orelse return 0;
1141 const symbol = dupeLuaString(s, 2) orelse return 0;
1142
1143 const layout = config_mod.Layouts.fromString(name) orelse return 0;
1144 switch (layout) {
1145 .tiling => cfg.layout_tile_symbol = symbol,
1146 .monocle => cfg.layout_monocle_symbol = symbol,
1147 .floating => cfg.layout_floating_symbol = symbol,
1148 .scrolling => cfg.layout_scrolling_symbol = symbol,
1149 .grid => cfg.layout_grid_symbol = symbol,
1150 }
1151 return 0;
1152 }
1153
1154 fn luaSetTagLayout(state: ?*c.lua_State) callconv(.c) c_int {
1155 const cfg = config orelse return 0;
1156 const s = state orelse return 0;
1157 const tag_index = c.lua_tointegerx(s, 1, null);
1158 if (tag_index < 1 or tag_index > 9) return 0;
1159 const name = getStringArg(s, 2) orelse return 0;
1160 if (config_mod.Layouts.fromString(name) == null) {
1161 std.debug.print("set_tag_layout: unknown layout '{s}'\n", .{name});
1162 return 0;
1163 }
1164 if (dupeLuaString(s, 2)) |layout_str| {
1165 cfg.tag_layouts[@intCast(tag_index - 1)] = layout_str;
1166 }
1167 return 0;
1168 }
1169
1170 fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1171 const s = state orelse return 0;
1172 createActionTable(s, "Quit");
1173 return 1;
1174 }
1175
1176 fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1177 const s = state orelse return 0;
1178 createActionTable(s, "Restart");
1179 return 1;
1180 }
1181
1182 fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1183 const s = state orelse return 0;
1184 createActionTable(s, "ToggleGaps");
1185 return 1;
1186 }
1187
1188 fn luaToggleBar(state: ?*c.lua_State) callconv(.c) c_int {
1189 const s = state orelse return 0;
1190 createActionTable(s, "ToggleBar");
1191 return 1;
1192 }
1193
1194 fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1195 const s = state orelse return 0;
1196 createActionTable(s, "ShowKeybinds");
1197 return 1;
1198 }
1199
1200 fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1201 const s = state orelse return 0;
1202 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1203 createActionTableWithInt(s, "ResizeMaster", delta);
1204 return 1;
1205 }
1206
1207 fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1208 const s = state orelse return 0;
1209 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1210 if (delta > 0) {
1211 createActionTable(s, "IncMaster");
1212 } else {
1213 createActionTable(s, "DecMaster");
1214 }
1215 return 1;
1216 }
1217
1218 fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1219 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1220 return getLuaString(state, idx);
1221 }
1222
1223 fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1224 const len = c.lua_rawlen(state, idx);
1225 if (len == 0) return null;
1226
1227 if (len >= 3) {
1228 _ = c.lua_rawgeti(state, idx, 1);
1229 const first = getLuaString(state, -1);
1230 c.lua_settop(state, -2);
1231
1232 _ = c.lua_rawgeti(state, idx, 2);
1233 const second = getLuaString(state, -1);
1234 c.lua_settop(state, -2);
1235
1236 if (first != null and second != null and
1237 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1238 {
1239 _ = c.lua_rawgeti(state, idx, 3);
1240 const cmd = getLuaString(state, -1);
1241 c.lua_settop(state, -2);
1242 return cmd;
1243 }
1244 }
1245
1246 _ = c.lua_rawgeti(state, idx, 1);
1247 const first_elem = getLuaString(state, -1);
1248 c.lua_settop(state, -2);
1249 return first_elem;
1250 }
1251
1252 fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1253 const cstr = c.lua_tolstring(state, idx, null);
1254 if (cstr == null) return null;
1255 return std.mem.span(cstr);
1256 }
1257
1258 fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1259 const cfg = config orelse return null;
1260 const lua_str = getLuaString(state, idx) orelse return null;
1261 const arena_allocator = cfg.string_arena.allocator();
1262 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1263 return duped;
1264 }
1265
1266 fn parseClickAction(state: *c.lua_State, idx: c_int) ?config_mod.ClickAction {
1267 const lua_type = c.lua_type(state, idx);
1268 if (lua_type == c.LUA_TSTRING) {
1269 const cmd = dupeLuaString(state, idx) orelse return null;
1270 return .{ .command = cmd };
1271 } else if (lua_type == c.LUA_TTABLE) {
1272 _ = c.lua_getfield(state, idx, "command");
1273 const cmd = dupeLuaString(state, -1);
1274 c.lua_settop(state, -2);
1275 if (cmd == null) return null;
1276
1277 _ = c.lua_getfield(state, idx, "floating");
1278 const floating = c.lua_toboolean(state, -1) != 0;
1279 c.lua_settop(state, -2);
1280
1281 _ = c.lua_getfield(state, idx, "bypass_rules");
1282 const bypass_rules = c.lua_toboolean(state, -1) != 0;
1283 c.lua_settop(state, -2);
1284
1285 return .{ .command = cmd.?, .floating = floating, .bypass_rules = bypass_rules };
1286 }
1287 return null;
1288 }
1289
1290 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1291 const lua_type = c.lua_type(state, idx);
1292 if (lua_type == c.LUA_TNUMBER) {
1293 return @intCast(c.lua_tointegerx(state, idx, null));
1294 }
1295 if (lua_type == c.LUA_TSTRING) {
1296 const str = getLuaString(state, idx) orelse return 0;
1297 if (str.len > 0 and str[0] == '#') {
1298 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1299 }
1300 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1301 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1302 }
1303 return std.fmt.parseInt(u32, str, 16) catch return 0;
1304 }
1305 return 0;
1306 }
1307
1308 fn parseScheme(state: *c.lua_State) ColorScheme {
1309 return ColorScheme{
1310 .foreground = parseColor(state, 1),
1311 .background = parseColor(state, 2),
1312 .border = parseColor(state, 3),
1313 };
1314 }
1315
1316 fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1317 var mod_mask: u32 = 0;
1318
1319 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1320
1321 const len = c.lua_rawlen(state, idx);
1322 var i: usize = 1;
1323 while (i <= len) : (i += 1) {
1324 _ = c.lua_rawgeti(state, idx, @intCast(i));
1325 if (getLuaString(state, -1)) |mod_str| {
1326 const parsed = parseSingleModifier(mod_str);
1327 mod_mask |= parsed;
1328 }
1329 c.lua_settop(state, -2);
1330 }
1331
1332 return mod_mask;
1333 }
1334
1335 fn parseModifiersAtTop(state: *c.lua_State) u32 {
1336 return parseModifiers(state, -1);
1337 }
1338
1339 fn parseSingleModifier(name: []const u8) u32 {
1340 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1341 return (1 << 6);
1342 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1343 return (1 << 3);
1344 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1345 return (1 << 0);
1346 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1347 return (1 << 2);
1348 }
1349 return 0;
1350 }
1351
1352 fn parseAction(name: []const u8) ?Action {
1353 const action_map = .{
1354 .{ "Spawn", Action.spawn },
1355 .{ "SpawnTerminal", Action.spawn_terminal },
1356 .{ "KillClient", Action.kill_client },
1357 .{ "Quit", Action.quit },
1358 .{ "Restart", Action.restart },
1359 .{ "ShowKeybinds", Action.show_keybinds },
1360 .{ "FocusStack", Action.focus_next },
1361 .{ "MoveStack", Action.move_next },
1362 .{ "ResizeMaster", Action.resize_master },
1363 .{ "IncMaster", Action.inc_master },
1364 .{ "DecMaster", Action.dec_master },
1365 .{ "ToggleFloating", Action.toggle_floating },
1366 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1367 .{ "ToggleGaps", Action.toggle_gaps },
1368 .{ "ToggleBar", Action.toggle_bar },
1369 .{ "CycleLayout", Action.cycle_layout },
1370 .{ "ChangeLayout", Action.set_layout },
1371 .{ "ViewTag", Action.view_tag },
1372 .{ "ViewNextTag", Action.view_next_tag },
1373 .{ "ViewPreviousTag", Action.view_prev_tag },
1374 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1375 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1376 .{ "MoveToTag", Action.move_to_tag },
1377 .{ "ToggleView", Action.toggle_view_tag },
1378 .{ "ToggleTag", Action.toggle_tag },
1379 .{ "FocusMonitor", Action.focus_monitor },
1380 .{ "TagMonitor", Action.send_to_monitor },
1381 .{ "ScrollLeft", Action.scroll_left },
1382 .{ "ScrollRight", Action.scroll_right },
1383 };
1384
1385 inline for (action_map) |entry| {
1386 if (std.mem.eql(u8, name, entry[0])) {
1387 return entry[1];
1388 }
1389 }
1390 return null;
1391 }
1392
1393 fn keynameToKeysym(name: []const u8) ?u64 {
1394 const key_map = .{
1395 .{ "Return", 0xff0d },
1396 .{ "Enter", 0xff0d },
1397 .{ "Tab", 0xff09 },
1398 .{ "Escape", 0xff1b },
1399 .{ "BackSpace", 0xff08 },
1400 .{ "Delete", 0xffff },
1401 .{ "space", 0x0020 },
1402 .{ "Space", 0x0020 },
1403 .{ "comma", 0x002c },
1404 .{ "Comma", 0x002c },
1405 .{ "period", 0x002e },
1406 .{ "Period", 0x002e },
1407 .{ "slash", 0x002f },
1408 .{ "Slash", 0x002f },
1409 .{ "minus", 0x002d },
1410 .{ "Minus", 0x002d },
1411 .{ "equal", 0x003d },
1412 .{ "Equal", 0x003d },
1413 .{ "bracketleft", 0x005b },
1414 .{ "bracketright", 0x005d },
1415 .{ "backslash", 0x005c },
1416 .{ "colon", 0x003a },
1417 .{ "semicolon", 0x003b },
1418 .{ "apostrophe", 0x0027 },
1419 .{ "quotedbl", 0x0022 },
1420 .{ "ampersand", 0x0026 },
1421 .{ "parenleft", 0x0028 },
1422 .{ "parenright", 0x0029 },
1423 .{ "underscore", 0x005f },
1424 .{ "grave", 0x0060 },
1425 .{ "agrave", 0x00e0 },
1426 .{ "egrave", 0x00e8 },
1427 .{ "ccedilla", 0x00e7 },
1428 .{ "eacute", 0x00e9 },
1429 .{ "Left", 0xff51 },
1430 .{ "Up", 0xff52 },
1431 .{ "Right", 0xff53 },
1432 .{ "Down", 0xff54 },
1433 .{ "F1", 0xffbe },
1434 .{ "F2", 0xffbf },
1435 .{ "F3", 0xffc0 },
1436 .{ "F4", 0xffc1 },
1437 .{ "F5", 0xffc2 },
1438 .{ "F6", 0xffc3 },
1439 .{ "F7", 0xffc4 },
1440 .{ "F8", 0xffc5 },
1441 .{ "F9", 0xffc6 },
1442 .{ "F10", 0xffc7 },
1443 .{ "F11", 0xffc8 },
1444 .{ "F12", 0xffc9 },
1445 .{ "Print", 0xff61 },
1446 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1447 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1448 .{ "XF86AudioMute", 0x1008ff12 },
1449 .{ "XF86AudioPlay", 0x1008ff14 },
1450 .{ "XF86AudioPause", 0x1008ff31 },
1451 .{ "XF86AudioNext", 0x1008ff17 },
1452 .{ "XF86AudioPrev", 0x1008ff16 },
1453 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1454 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1455 };
1456
1457 inline for (key_map) |entry| {
1458 if (std.mem.eql(u8, name, entry[0])) {
1459 return entry[1];
1460 }
1461 }
1462
1463 if (name.len == 1) {
1464 const char = name[0];
1465 if (char >= 'a' and char <= 'z') {
1466 return char;
1467 }
1468 if (char >= 'A' and char <= 'Z') {
1469 return char + 32;
1470 }
1471 if (char >= '0' and char <= '9') {
1472 return char;
1473 }
1474 }
1475
1476 return null;
1477 }