tony / oxwm

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