tony / oxwm

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