tony / oxwm

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