tony / oxwm

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