tony / oxwm

Branch: d37b3e0
Switch branches/tags
master
tonybanters updates for zig v16 c381491 2026-05-14
1482 lines · 43.5 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(env: *std.process.Environ.Map) bool {
75 const home = env.get("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 var isnum: c_int = 0;
594 const raw = c.lua_tointegerx(s, 1, &isnum);
595 const dir: i32 = if (isnum != 0) @intCast(raw) else 1;
596 createActionTableWithInt(s, "FocusStack", dir);
597 return 1;
598 }
599
600 fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int {
601 const s = state orelse return 0;
602 var isnum: c_int = 0;
603 const raw = c.lua_tointegerx(s, 1, &isnum);
604 const dir: i32 = if (isnum != 0) @intCast(raw) else 1;
605 createActionTableWithInt(s, "MoveStack", dir);
606 return 1;
607 }
608
609 fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int {
610 const s = state orelse return 0;
611 createActionTable(s, "CycleLayout");
612 return 1;
613 }
614
615 fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int {
616 const s = state orelse return 0;
617 createActionTableWithString(s, "ChangeLayout");
618 return 1;
619 }
620
621 fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int {
622 const s = state orelse return 0;
623 createActionTable(s, "ScrollLeft");
624 return 1;
625 }
626
627 fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
628 const s = state orelse return 0;
629 createActionTable(s, "ScrollRight");
630 return 1;
631 }
632
633 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
634 const s = state orelse return 0;
635 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
636 createActionTableWithInt(s, "ViewTag", idx);
637 return 1;
638 }
639
640 fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int {
641 const s = state orelse return 0;
642 createActionTable(s, "ViewNextTag");
643 return 1;
644 }
645
646 fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int {
647 const s = state orelse return 0;
648 createActionTable(s, "ViewPreviousTag");
649 return 1;
650 }
651
652 fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int {
653 const s = state orelse return 0;
654 createActionTable(s, "ViewNextNonEmptyTag");
655 return 1;
656 }
657
658 fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int {
659 const s = state orelse return 0;
660 createActionTable(s, "ViewPreviousNonEmptyTag");
661 return 1;
662 }
663
664 fn luaTagToggleview(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, "ToggleView", idx);
668 return 1;
669 }
670
671 fn luaTagMoveTo(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, "MoveToTag", idx);
675 return 1;
676 }
677
678 fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int {
679 const s = state orelse return 0;
680 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
681 createActionTableWithInt(s, "ToggleTag", idx);
682 return 1;
683 }
684
685 fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int {
686 const cfg = config orelse return 0;
687 const s = state orelse return 0;
688 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
689 return 0;
690 }
691
692 fn luaMonitorFocus(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, "FocusMonitor", dir);
696 return 1;
697 }
698
699 fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int {
700 const s = state orelse return 0;
701 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
702 createActionTableWithInt(s, "TagMonitor", dir);
703 return 1;
704 }
705
706 fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int {
707 const cfg = config orelse return 0;
708 const s = state orelse return 0;
709
710 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
711
712 var rule = Rule{
713 .class = null,
714 .instance = null,
715 .title = null,
716 .tags = 0,
717 .is_floating = false,
718 .monitor = -1,
719 .focus = false,
720 };
721
722 _ = c.lua_getfield(s, 1, "class");
723 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
724 rule.class = getLuaString(s, -1);
725 }
726 c.lua_settop(s, -2);
727
728 _ = c.lua_getfield(s, 1, "instance");
729 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
730 rule.instance = getLuaString(s, -1);
731 }
732 c.lua_settop(s, -2);
733
734 _ = c.lua_getfield(s, 1, "title");
735 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
736 rule.title = getLuaString(s, -1);
737 }
738 c.lua_settop(s, -2);
739
740 _ = c.lua_getfield(s, 1, "tag");
741 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
742 const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null));
743 if (tag_idx > 0) {
744 rule.tags = @as(u32, 1) << @intCast(tag_idx - 1);
745 }
746 }
747 c.lua_settop(s, -2);
748
749 _ = c.lua_getfield(s, 1, "floating");
750 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
751 rule.is_floating = c.lua_toboolean(s, -1) != 0;
752 }
753 c.lua_settop(s, -2);
754
755 _ = c.lua_getfield(s, 1, "monitor");
756 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
757 rule.monitor = @intCast(c.lua_tointegerx(s, -1, null));
758 }
759 c.lua_settop(s, -2);
760
761 _ = c.lua_getfield(s, 1, "focus");
762 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
763 rule.focus = c.lua_toboolean(s, -1) != 0;
764 }
765 c.lua_settop(s, -2);
766
767 cfg.addRule(rule) catch return 0;
768 return 0;
769 }
770
771 fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int {
772 const cfg = config orelse return 0;
773 const s = state orelse return 0;
774 if (dupeLuaString(s, 1)) |font| {
775 cfg.font = font;
776 }
777 return 0;
778 }
779
780 fn luaBarSetPosition(state: ?*c.lua_State) callconv(.c) c_int {
781 const cfg = config orelse return 0;
782 const s = state orelse return 0;
783 if (dupeLuaString(s, 1)) |position| {
784 cfg.bar_position = position;
785 }
786 return 0;
787 }
788
789 fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int {
790 const cfg = config orelse return 0;
791 const s = state orelse return 0;
792
793 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
794
795 const len = c.lua_rawlen(s, 1);
796 var i: usize = 1;
797 while (i <= len) : (i += 1) {
798 _ = c.lua_rawgeti(s, 1, @intCast(i));
799
800 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
801 c.lua_settop(s, -2);
802 continue;
803 }
804
805 if (parseBlockConfig(s, -1)) |block| {
806 cfg.addBlock(block) catch {};
807 }
808
809 c.lua_settop(s, -2);
810 }
811
812 return 0;
813 }
814
815 fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
816 _ = c.lua_getfield(state, idx, "__block_type");
817 const block_type_str = getLuaString(state, -1) orelse {
818 c.lua_settop(state, -2);
819 return null;
820 };
821 c.lua_settop(state, -2);
822
823 _ = c.lua_getfield(state, idx, "format");
824 const format = dupeLuaString(state, -1) orelse "";
825 c.lua_settop(state, -2);
826
827 _ = c.lua_getfield(state, idx, "interval");
828 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
829 c.lua_settop(state, -2);
830
831 _ = c.lua_getfield(state, idx, "color");
832 const color = parseColor(state, -1);
833 c.lua_settop(state, -2);
834
835 _ = c.lua_getfield(state, idx, "underline");
836 const underline = c.lua_toboolean(state, -1) != 0;
837 c.lua_settop(state, -2);
838
839 _ = c.lua_getfield(state, idx, "click");
840 const click = parseClickAction(state, -1);
841 c.lua_settop(state, -2);
842
843 var block = Block{
844 .block_type = .static,
845 .format = format,
846 .interval = interval,
847 .color = color,
848 .underline = underline,
849 .click = click,
850 };
851
852 if (std.mem.eql(u8, block_type_str, "Ram")) {
853 block.block_type = .ram;
854 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
855 block.block_type = .datetime;
856 _ = c.lua_getfield(state, idx, "__arg");
857 block.datetime_format = dupeLuaString(state, -1);
858 c.lua_settop(state, -2);
859 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
860 block.block_type = .shell;
861 _ = c.lua_getfield(state, idx, "__arg");
862 block.command = dupeLuaString(state, -1);
863 c.lua_settop(state, -2);
864 } else if (std.mem.eql(u8, block_type_str, "Static")) {
865 block.block_type = .static;
866 _ = c.lua_getfield(state, idx, "__arg");
867 if (dupeLuaString(state, -1)) |text| {
868 block.format = text;
869 }
870 c.lua_settop(state, -2);
871 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
872 block.block_type = .battery;
873 _ = c.lua_getfield(state, idx, "__arg");
874 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
875 _ = c.lua_getfield(state, -1, "charging");
876 block.format_charging = dupeLuaString(state, -1);
877 c.lua_settop(state, -2);
878
879 _ = c.lua_getfield(state, -1, "discharging");
880 block.format_discharging = dupeLuaString(state, -1);
881 c.lua_settop(state, -2);
882
883 _ = c.lua_getfield(state, -1, "full");
884 block.format_full = dupeLuaString(state, -1);
885 c.lua_settop(state, -2);
886
887 _ = c.lua_getfield(state, -1, "battery_name");
888 block.battery_name = dupeLuaString(state, -1);
889 c.lua_settop(state, -2);
890 }
891 c.lua_settop(state, -2);
892 } else if (std.mem.eql(u8, block_type_str, "Systray")) {
893 block.block_type = .systray;
894 } else {
895 return null;
896 }
897
898 return block;
899 }
900
901 fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int {
902 const cfg = config orelse return 0;
903 const s = state orelse return 0;
904 cfg.scheme_normal = parseScheme(s);
905 return 0;
906 }
907
908 fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int {
909 const cfg = config orelse return 0;
910 const s = state orelse return 0;
911 cfg.scheme_selected = parseScheme(s);
912 return 0;
913 }
914
915 fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int {
916 const cfg = config orelse return 0;
917 const s = state orelse return 0;
918 cfg.scheme_occupied = parseScheme(s);
919 return 0;
920 }
921
922 fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int {
923 const cfg = config orelse return 0;
924 const s = state orelse return 0;
925 cfg.scheme_urgent = parseScheme(s);
926 return 0;
927 }
928
929 fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int {
930 const cfg = config orelse return 0;
931 const s = state orelse return 0;
932 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
933 return 0;
934 }
935
936 fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int {
937 const s = state orelse return 0;
938 createBlockTable(s, "Ram", null);
939 return 1;
940 }
941
942 fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int {
943 const s = state orelse return 0;
944 _ = c.lua_getfield(s, 1, "date_format");
945 const date_format = getLuaString(s, -1);
946 c.lua_settop(s, -2);
947 createBlockTable(s, "DateTime", date_format);
948 return 1;
949 }
950
951 fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int {
952 const s = state orelse return 0;
953 _ = c.lua_getfield(s, 1, "command");
954 const command = getLuaString(s, -1);
955 c.lua_settop(s, -2);
956 createBlockTable(s, "Shell", command);
957 return 1;
958 }
959
960 fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
961 const s = state orelse return 0;
962 _ = c.lua_getfield(s, 1, "text");
963 const text = getLuaString(s, -1);
964 c.lua_settop(s, -2);
965 createBlockTable(s, "Static", text);
966 return 1;
967 }
968
969 fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int {
970 const s = state orelse return 0;
971 createBlockTable(s, "Systray", null);
972 return 1;
973 }
974
975 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
976 const s = state orelse return 0;
977
978 c.lua_createtable(s, 0, 7);
979
980 _ = c.lua_pushstring(s, "Battery");
981 c.lua_setfield(s, -2, "__block_type");
982
983 _ = c.lua_getfield(s, 1, "format");
984 c.lua_setfield(s, -2, "format");
985
986 _ = c.lua_getfield(s, 1, "interval");
987 c.lua_setfield(s, -2, "interval");
988
989 _ = c.lua_getfield(s, 1, "color");
990 c.lua_setfield(s, -2, "color");
991
992 _ = c.lua_getfield(s, 1, "underline");
993 c.lua_setfield(s, -2, "underline");
994
995 _ = c.lua_getfield(s, 1, "click");
996 c.lua_setfield(s, -2, "click");
997
998 c.lua_createtable(s, 0, 4);
999 _ = c.lua_getfield(s, 1, "charging");
1000 c.lua_setfield(s, -2, "charging");
1001 _ = c.lua_getfield(s, 1, "discharging");
1002 c.lua_setfield(s, -2, "discharging");
1003 _ = c.lua_getfield(s, 1, "full");
1004 c.lua_setfield(s, -2, "full");
1005 _ = c.lua_getfield(s, 1, "battery_name");
1006 c.lua_setfield(s, -2, "battery_name");
1007 c.lua_setfield(s, -2, "__arg");
1008
1009 return 1;
1010 }
1011
1012 fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
1013 c.lua_createtable(state, 0, 7);
1014
1015 _ = c.lua_pushstring(state, block_type);
1016 c.lua_setfield(state, -2, "__block_type");
1017
1018 _ = c.lua_getfield(state, 1, "format");
1019 c.lua_setfield(state, -2, "format");
1020
1021 _ = c.lua_getfield(state, 1, "interval");
1022 c.lua_setfield(state, -2, "interval");
1023
1024 _ = c.lua_getfield(state, 1, "color");
1025 c.lua_setfield(state, -2, "color");
1026
1027 _ = c.lua_getfield(state, 1, "underline");
1028 c.lua_setfield(state, -2, "underline");
1029
1030 _ = c.lua_getfield(state, 1, "click");
1031 c.lua_setfield(state, -2, "click");
1032
1033 if (arg) |a| {
1034 var buf: [256]u8 = undefined;
1035 if (a.len < buf.len) {
1036 @memcpy(buf[0..a.len], a);
1037 buf[a.len] = 0;
1038 _ = c.lua_pushstring(state, &buf);
1039 c.lua_setfield(state, -2, "__arg");
1040 }
1041 }
1042 }
1043
1044 fn luaSetFloatingPosition(state: ?*c.lua_State) callconv(.c) c_int {
1045 const cfg = config orelse return 0;
1046 const s = state orelse return 0;
1047 if (getLuaString(s, 1)) |name| {
1048 if (config_mod.FloatingPosition.fromString(name)) |pos| {
1049 cfg.floating_position = pos;
1050 }
1051 }
1052 return 0;
1053 }
1054
1055 fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int {
1056 const cfg = config orelse return 0;
1057 const s = state orelse return 0;
1058 if (dupeLuaString(s, 1)) |term| {
1059 cfg.terminal = term;
1060 }
1061 return 0;
1062 }
1063
1064 fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int {
1065 const cfg = config orelse return 0;
1066 const s = state orelse return 0;
1067 const name = getStringArg(s, 1) orelse return 0;
1068 if (config_mod.Layouts.fromString(name) == null) {
1069 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
1070 return 0;
1071 }
1072 if (dupeLuaString(s, 1)) |layout| {
1073 cfg.layout = layout;
1074 }
1075 return 0;
1076 }
1077
1078 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1079 const cfg = config orelse return 0;
1080 const s = state orelse return 0;
1081 if (getStringArg(s, 1)) |modkey_str| {
1082 cfg.modkey = parseSingleModifier(modkey_str);
1083 cfg.addButton(.{
1084 .click = .client_win,
1085 .mod_mask = cfg.modkey,
1086 .button = 1,
1087 .action = .move_mouse,
1088 }) catch {};
1089 cfg.addButton(.{
1090 .click = .client_win,
1091 .mod_mask = cfg.modkey,
1092 .button = 3,
1093 .action = .resize_mouse,
1094 }) catch {};
1095 }
1096 return 0;
1097 }
1098
1099 fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1100 const cfg = config orelse return 0;
1101 const s = state orelse return 0;
1102
1103 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
1104
1105 const len = c.lua_rawlen(s, 1);
1106 var i: usize = 0;
1107 while (i < len and i < 9) : (i += 1) {
1108 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1109 if (dupeLuaString(s, -1)) |tag_str| {
1110 cfg.tags[i] = tag_str;
1111 }
1112 c.lua_settop(s, -2);
1113 }
1114
1115 return 0;
1116 }
1117
1118 fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1119 const cfg = config orelse return 0;
1120 const s = state orelse return 0;
1121 if (dupeLuaString(s, 1)) |cmd| {
1122 cfg.addAutostart(cmd) catch return 0;
1123 }
1124 return 0;
1125 }
1126
1127 fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1128 const cfg = config orelse return 0;
1129 const s = state orelse return 0;
1130 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1131 return 0;
1132 }
1133
1134 fn luaTiledResizeMode(state: ?*c.lua_State) callconv(.c) c_int {
1135 const cfg = config orelse return 0;
1136 const s = state orelse return 0;
1137 cfg.tiled_resize_mode = c.lua_toboolean(s, 1) != 0;
1138 return 0;
1139 }
1140
1141 fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1142 const cfg = config orelse return 0;
1143 const s = state orelse return 0;
1144 const name = getStringArg(s, 1) orelse return 0;
1145 const symbol = dupeLuaString(s, 2) orelse return 0;
1146
1147 const layout = config_mod.Layouts.fromString(name) orelse return 0;
1148 switch (layout) {
1149 .tiling => cfg.layout_tile_symbol = symbol,
1150 .monocle => cfg.layout_monocle_symbol = symbol,
1151 .floating => cfg.layout_floating_symbol = symbol,
1152 .scrolling => cfg.layout_scrolling_symbol = symbol,
1153 .grid => cfg.layout_grid_symbol = symbol,
1154 .dwindle => cfg.layout_dwindle_symbol = symbol,
1155 }
1156 return 0;
1157 }
1158
1159 fn luaSetTagLayout(state: ?*c.lua_State) callconv(.c) c_int {
1160 const cfg = config orelse return 0;
1161 const s = state orelse return 0;
1162 const tag_index = c.lua_tointegerx(s, 1, null);
1163 if (tag_index < 1 or tag_index > 9) return 0;
1164 const name = getStringArg(s, 2) orelse return 0;
1165 if (config_mod.Layouts.fromString(name) == null) {
1166 std.debug.print("set_tag_layout: unknown layout '{s}'\n", .{name});
1167 return 0;
1168 }
1169 if (dupeLuaString(s, 2)) |layout_str| {
1170 cfg.tag_layouts[@intCast(tag_index - 1)] = layout_str;
1171 }
1172 return 0;
1173 }
1174
1175 fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1176 const s = state orelse return 0;
1177 createActionTable(s, "Quit");
1178 return 1;
1179 }
1180
1181 fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1182 const s = state orelse return 0;
1183 createActionTable(s, "Restart");
1184 return 1;
1185 }
1186
1187 fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1188 const s = state orelse return 0;
1189 createActionTable(s, "ToggleGaps");
1190 return 1;
1191 }
1192
1193 fn luaToggleBar(state: ?*c.lua_State) callconv(.c) c_int {
1194 const s = state orelse return 0;
1195 createActionTable(s, "ToggleBar");
1196 return 1;
1197 }
1198
1199 fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1200 const s = state orelse return 0;
1201 createActionTable(s, "ShowKeybinds");
1202 return 1;
1203 }
1204
1205 fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1206 const s = state orelse return 0;
1207 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1208 createActionTableWithInt(s, "ResizeMaster", delta);
1209 return 1;
1210 }
1211
1212 fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1213 const s = state orelse return 0;
1214 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1215 if (delta > 0) {
1216 createActionTable(s, "IncMaster");
1217 } else {
1218 createActionTable(s, "DecMaster");
1219 }
1220 return 1;
1221 }
1222
1223 fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1224 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1225 return getLuaString(state, idx);
1226 }
1227
1228 fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1229 const len = c.lua_rawlen(state, idx);
1230 if (len == 0) return null;
1231
1232 if (len >= 3) {
1233 _ = c.lua_rawgeti(state, idx, 1);
1234 const first = getLuaString(state, -1);
1235 c.lua_settop(state, -2);
1236
1237 _ = c.lua_rawgeti(state, idx, 2);
1238 const second = getLuaString(state, -1);
1239 c.lua_settop(state, -2);
1240
1241 if (first != null and second != null and
1242 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1243 {
1244 _ = c.lua_rawgeti(state, idx, 3);
1245 const cmd = getLuaString(state, -1);
1246 c.lua_settop(state, -2);
1247 return cmd;
1248 }
1249 }
1250
1251 _ = c.lua_rawgeti(state, idx, 1);
1252 const first_elem = getLuaString(state, -1);
1253 c.lua_settop(state, -2);
1254 return first_elem;
1255 }
1256
1257 fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1258 const cstr = c.lua_tolstring(state, idx, null);
1259 if (cstr == null) return null;
1260 return std.mem.span(cstr);
1261 }
1262
1263 fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1264 const cfg = config orelse return null;
1265 const lua_str = getLuaString(state, idx) orelse return null;
1266 const arena_allocator = cfg.string_arena.allocator();
1267 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1268 return duped;
1269 }
1270
1271 fn parseClickAction(state: *c.lua_State, idx: c_int) ?config_mod.ClickAction {
1272 const lua_type = c.lua_type(state, idx);
1273 if (lua_type == c.LUA_TSTRING) {
1274 const cmd = dupeLuaString(state, idx) orelse return null;
1275 return .{ .command = cmd };
1276 } else if (lua_type == c.LUA_TTABLE) {
1277 _ = c.lua_getfield(state, idx, "command");
1278 const cmd = dupeLuaString(state, -1);
1279 c.lua_settop(state, -2);
1280 if (cmd == null) return null;
1281
1282 _ = c.lua_getfield(state, idx, "floating");
1283 const floating = c.lua_toboolean(state, -1) != 0;
1284 c.lua_settop(state, -2);
1285
1286 _ = c.lua_getfield(state, idx, "bypass_rules");
1287 const bypass_rules = c.lua_toboolean(state, -1) != 0;
1288 c.lua_settop(state, -2);
1289
1290 return .{ .command = cmd.?, .floating = floating, .bypass_rules = bypass_rules };
1291 }
1292 return null;
1293 }
1294
1295 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1296 const lua_type = c.lua_type(state, idx);
1297 if (lua_type == c.LUA_TNUMBER) {
1298 return @intCast(c.lua_tointegerx(state, idx, null));
1299 }
1300 if (lua_type == c.LUA_TSTRING) {
1301 const str = getLuaString(state, idx) orelse return 0;
1302 if (str.len > 0 and str[0] == '#') {
1303 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1304 }
1305 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1306 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1307 }
1308 return std.fmt.parseInt(u32, str, 16) catch return 0;
1309 }
1310 return 0;
1311 }
1312
1313 fn parseScheme(state: *c.lua_State) ColorScheme {
1314 return ColorScheme{
1315 .foreground = parseColor(state, 1),
1316 .background = parseColor(state, 2),
1317 .border = parseColor(state, 3),
1318 };
1319 }
1320
1321 fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1322 var mod_mask: u32 = 0;
1323
1324 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1325
1326 const len = c.lua_rawlen(state, idx);
1327 var i: usize = 1;
1328 while (i <= len) : (i += 1) {
1329 _ = c.lua_rawgeti(state, idx, @intCast(i));
1330 if (getLuaString(state, -1)) |mod_str| {
1331 const parsed = parseSingleModifier(mod_str);
1332 mod_mask |= parsed;
1333 }
1334 c.lua_settop(state, -2);
1335 }
1336
1337 return mod_mask;
1338 }
1339
1340 fn parseModifiersAtTop(state: *c.lua_State) u32 {
1341 return parseModifiers(state, -1);
1342 }
1343
1344 fn parseSingleModifier(name: []const u8) u32 {
1345 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1346 return (1 << 6);
1347 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1348 return (1 << 3);
1349 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1350 return (1 << 0);
1351 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1352 return (1 << 2);
1353 }
1354 return 0;
1355 }
1356
1357 fn parseAction(name: []const u8) ?Action {
1358 const action_map = .{
1359 .{ "Spawn", Action.spawn },
1360 .{ "SpawnTerminal", Action.spawn_terminal },
1361 .{ "KillClient", Action.kill_client },
1362 .{ "Quit", Action.quit },
1363 .{ "Restart", Action.restart },
1364 .{ "ShowKeybinds", Action.show_keybinds },
1365 .{ "FocusStack", Action.focus_next },
1366 .{ "MoveStack", Action.move_next },
1367 .{ "ResizeMaster", Action.resize_master },
1368 .{ "IncMaster", Action.inc_master },
1369 .{ "DecMaster", Action.dec_master },
1370 .{ "ToggleFloating", Action.toggle_floating },
1371 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1372 .{ "ToggleGaps", Action.toggle_gaps },
1373 .{ "ToggleBar", Action.toggle_bar },
1374 .{ "CycleLayout", Action.cycle_layout },
1375 .{ "ChangeLayout", Action.set_layout },
1376 .{ "ViewTag", Action.view_tag },
1377 .{ "ViewNextTag", Action.view_next_tag },
1378 .{ "ViewPreviousTag", Action.view_prev_tag },
1379 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1380 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1381 .{ "MoveToTag", Action.move_to_tag },
1382 .{ "ToggleView", Action.toggle_view_tag },
1383 .{ "ToggleTag", Action.toggle_tag },
1384 .{ "FocusMonitor", Action.focus_monitor },
1385 .{ "TagMonitor", Action.send_to_monitor },
1386 .{ "ScrollLeft", Action.scroll_left },
1387 .{ "ScrollRight", Action.scroll_right },
1388 };
1389
1390 inline for (action_map) |entry| {
1391 if (std.mem.eql(u8, name, entry[0])) {
1392 return entry[1];
1393 }
1394 }
1395 return null;
1396 }
1397
1398 fn keynameToKeysym(name: []const u8) ?u64 {
1399 const key_map = .{
1400 .{ "Return", 0xff0d },
1401 .{ "Enter", 0xff0d },
1402 .{ "Tab", 0xff09 },
1403 .{ "Escape", 0xff1b },
1404 .{ "BackSpace", 0xff08 },
1405 .{ "Delete", 0xffff },
1406 .{ "space", 0x0020 },
1407 .{ "Space", 0x0020 },
1408 .{ "comma", 0x002c },
1409 .{ "Comma", 0x002c },
1410 .{ "period", 0x002e },
1411 .{ "Period", 0x002e },
1412 .{ "slash", 0x002f },
1413 .{ "Slash", 0x002f },
1414 .{ "minus", 0x002d },
1415 .{ "Minus", 0x002d },
1416 .{ "equal", 0x003d },
1417 .{ "Equal", 0x003d },
1418 .{ "bracketleft", 0x005b },
1419 .{ "bracketright", 0x005d },
1420 .{ "backslash", 0x005c },
1421 .{ "colon", 0x003a },
1422 .{ "semicolon", 0x003b },
1423 .{ "apostrophe", 0x0027 },
1424 .{ "quotedbl", 0x0022 },
1425 .{ "ampersand", 0x0026 },
1426 .{ "parenleft", 0x0028 },
1427 .{ "parenright", 0x0029 },
1428 .{ "underscore", 0x005f },
1429 .{ "grave", 0x0060 },
1430 .{ "agrave", 0x00e0 },
1431 .{ "egrave", 0x00e8 },
1432 .{ "ccedilla", 0x00e7 },
1433 .{ "eacute", 0x00e9 },
1434 .{ "Left", 0xff51 },
1435 .{ "Up", 0xff52 },
1436 .{ "Right", 0xff53 },
1437 .{ "Down", 0xff54 },
1438 .{ "F1", 0xffbe },
1439 .{ "F2", 0xffbf },
1440 .{ "F3", 0xffc0 },
1441 .{ "F4", 0xffc1 },
1442 .{ "F5", 0xffc2 },
1443 .{ "F6", 0xffc3 },
1444 .{ "F7", 0xffc4 },
1445 .{ "F8", 0xffc5 },
1446 .{ "F9", 0xffc6 },
1447 .{ "F10", 0xffc7 },
1448 .{ "F11", 0xffc8 },
1449 .{ "F12", 0xffc9 },
1450 .{ "Print", 0xff61 },
1451 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1452 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1453 .{ "XF86AudioMute", 0x1008ff12 },
1454 .{ "XF86AudioPlay", 0x1008ff14 },
1455 .{ "XF86AudioPause", 0x1008ff31 },
1456 .{ "XF86AudioNext", 0x1008ff17 },
1457 .{ "XF86AudioPrev", 0x1008ff16 },
1458 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1459 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1460 };
1461
1462 inline for (key_map) |entry| {
1463 if (std.mem.eql(u8, name, entry[0])) {
1464 return entry[1];
1465 }
1466 }
1467
1468 if (name.len == 1) {
1469 const char = name[0];
1470 if (char >= 'a' and char <= 'z') {
1471 return char;
1472 }
1473 if (char >= 'A' and char <= 'Z') {
1474 return char + 32;
1475 }
1476 if (char >= '0' and char <= '9') {
1477 return char;
1478 }
1479 }
1480
1481 return null;
1482 }