tony / oxwm

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