tony / oxwm

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