tony / oxwm

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