tony / oxwm

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