tony / oxwm

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