tony / oxwm

Branch: 4b35081
Switch branches/tags
master
Iziram fix: missing grid layout symbol setting 4b35081 2026-03-07
1373 lines · 39.9 KB Raw Blame History
1 const std = @import("std");
2 pub const config_mod = @import("config.zig");
3 const Config = config_mod.Config;
4 const Keybind = config_mod.Keybind;
5 const Action = config_mod.Action;
6 const Rule = config_mod.Rule;
7 const Block = config_mod.Block;
8 const ColorScheme = config_mod.ColorScheme;
9
10 const c = @cImport({
11 @cInclude("lua.h");
12 @cInclude("lauxlib.h");
13 @cInclude("lualib.h");
14 });
15
16 var L: ?*c.lua_State = null;
17 var config: ?*Config = null;
18
19 pub fn init(cfg: *Config) bool {
20 config = cfg;
21 L = c.luaL_newstate();
22 if (L == null) return false;
23 c.luaL_openlibs(L);
24 registerApi();
25 return true;
26 }
27
28 pub fn deinit() void {
29 if (L) |state| {
30 c.lua_close(state);
31 }
32 L = null;
33 config = null;
34 }
35
36 pub fn loadFile(path: []const u8) bool {
37 const state = L orelse return false;
38 var path_buf: [512]u8 = undefined;
39 if (path.len >= path_buf.len) return false;
40 @memcpy(path_buf[0..path.len], path);
41 path_buf[path.len] = 0;
42
43 if (std.mem.lastIndexOfScalar(u8, path, '/')) |last_slash| {
44 const dir = path[0..last_slash];
45 var setup_buf: [600]u8 = undefined;
46 const setup_code = std.fmt.bufPrintZ(&setup_buf, "package.path = '{s}/?.lua;' .. package.path", .{dir}) catch return false;
47 if (c.luaL_loadstring(state, setup_code.ptr) != 0 or c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) {
48 c.lua_settop(state, -2);
49 return false;
50 }
51 }
52
53 if (c.luaL_loadfilex(state, &path_buf, null) != 0) {
54 const err = c.lua_tolstring(state, -1, null);
55 if (err != null) {
56 std.debug.print("lua load error: {s}\n", .{std.mem.span(err)});
57 }
58 c.lua_settop(state, -2);
59 return false;
60 }
61
62 if (c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) {
63 const err = c.lua_tolstring(state, -1, null);
64 if (err != null) {
65 std.debug.print("lua runtime error: {s}\n", .{std.mem.span(err)});
66 }
67 c.lua_settop(state, -2);
68 return false;
69 }
70
71 return true;
72 }
73
74 pub fn loadConfig() 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
343 fn createActionTable(state: *c.lua_State, action_name: [*:0]const u8) void {
344 c.lua_createtable(state, 0, 2);
345 _ = c.lua_pushstring(state, action_name);
346 c.lua_setfield(state, -2, "__action");
347 }
348
349 fn createActionTableWithInt(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
350 c.lua_createtable(state, 0, 2);
351 _ = c.lua_pushstring(state, action_name);
352 c.lua_setfield(state, -2, "__action");
353 c.lua_pushinteger(state, arg);
354 c.lua_setfield(state, -2, "__arg");
355 }
356
357 fn createActionTableWithString(state: *c.lua_State, action_name: [*:0]const u8) void {
358 c.lua_createtable(state, 0, 2);
359 _ = c.lua_pushstring(state, action_name);
360 c.lua_setfield(state, -2, "__action");
361 c.lua_pushvalue(state, 1);
362 c.lua_setfield(state, -2, "__arg");
363 }
364
365 fn luaSpawn(state: ?*c.lua_State) callconv(.c) c_int {
366 const s = state orelse return 0;
367 createActionTableWithString(s, "Spawn");
368 return 1;
369 }
370
371 fn luaSpawnTerminal(state: ?*c.lua_State) callconv(.c) c_int {
372 const s = state orelse return 0;
373 createActionTable(s, "SpawnTerminal");
374 return 1;
375 }
376
377 fn luaKeyBind(state: ?*c.lua_State) callconv(.c) c_int {
378 const s = state orelse return 0;
379 const cfg = config orelse return 0;
380
381 const mod_mask = parseModifiers(s, 1);
382 const key_str = getStringArg(s, 2) orelse return 0;
383 const keysym = keynameToKeysym(key_str) orelse return 0;
384
385 if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0;
386
387 _ = c.lua_getfield(s, 3, "__action");
388 const action_str = getLuaString(s, -1) orelse {
389 c.lua_settop(s, -2);
390 return 0;
391 };
392 c.lua_settop(s, -2);
393
394 const action = parseAction(action_str) orelse return 0;
395
396 var int_arg: i32 = 0;
397 var str_arg: ?[]const u8 = null;
398
399 _ = c.lua_getfield(s, 3, "__arg");
400 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
401 int_arg = @intCast(c.lua_tointegerx(s, -1, null));
402 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
403 str_arg = getLuaString(s, -1);
404 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
405 str_arg = extractSpawnCommand(s, -1);
406 }
407 c.lua_settop(s, -2);
408
409 var keybind: config_mod.Keybind = .{
410 .action = action,
411 .int_arg = int_arg,
412 .str_arg = str_arg,
413 };
414 keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym };
415 keybind.key_count = 1;
416
417 cfg.addKeybind(keybind) catch return 0;
418
419 return 0;
420 }
421
422 fn luaKeyChord(state: ?*c.lua_State) callconv(.c) c_int {
423 const s = state orelse return 0;
424 const cfg = config orelse return 0;
425
426 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
427 if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0;
428
429 var keybind: config_mod.Keybind = .{
430 .action = .quit,
431 .int_arg = 0,
432 .str_arg = null,
433 };
434 keybind.key_count = 0;
435
436 const num_keys = c.lua_rawlen(s, 1);
437 if (num_keys == 0 or num_keys > 4) return 0;
438
439 var i: usize = 1;
440 while (i <= num_keys) : (i += 1) {
441 _ = c.lua_rawgeti(s, 1, @intCast(i));
442 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
443 c.lua_settop(s, -2);
444 return 0;
445 }
446
447 _ = c.lua_rawgeti(s, -1, 1);
448 const mod_mask = parseModifiersAtTop(s);
449 c.lua_settop(s, -2);
450
451 _ = c.lua_rawgeti(s, -1, 2);
452 const key_str = getLuaString(s, -1) orelse {
453 c.lua_settop(s, -3);
454 return 0;
455 };
456 c.lua_settop(s, -2);
457
458 const keysym = keynameToKeysym(key_str) orelse {
459 c.lua_settop(s, -2);
460 return 0;
461 };
462
463 keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym };
464 keybind.key_count += 1;
465
466 c.lua_settop(s, -2);
467 }
468
469 _ = c.lua_getfield(s, 2, "__action");
470 const action_str = getLuaString(s, -1) orelse {
471 c.lua_settop(s, -2);
472 return 0;
473 };
474 c.lua_settop(s, -2);
475
476 keybind.action = parseAction(action_str) orelse return 0;
477
478 _ = c.lua_getfield(s, 2, "__arg");
479 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
480 keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null));
481 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
482 keybind.str_arg = getLuaString(s, -1);
483 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
484 keybind.str_arg = extractSpawnCommand(s, -1);
485 }
486
487 c.lua_settop(s, -2);
488 cfg.addKeybind(keybind) catch return 0;
489
490 return 0;
491 }
492
493 fn luaGapsSetEnabled(state: ?*c.lua_State) callconv(.c) c_int {
494 const cfg = config orelse return 0;
495 const s = state orelse return 0;
496 cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0;
497 return 0;
498 }
499
500 fn luaGapsEnable(state: ?*c.lua_State) callconv(.c) c_int {
501 _ = state;
502 const cfg = config orelse return 0;
503 cfg.gaps_enabled = true;
504 return 0;
505 }
506
507 fn luaGapsDisable(state: ?*c.lua_State) callconv(.c) c_int {
508 _ = state;
509 const cfg = config orelse return 0;
510 cfg.gaps_enabled = false;
511 return 0;
512 }
513
514 fn luaGapsSetInner(state: ?*c.lua_State) callconv(.c) c_int {
515 const cfg = config orelse return 0;
516 const s = state orelse return 0;
517 cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null));
518 cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null));
519 return 0;
520 }
521
522 fn luaGapsSetOuter(state: ?*c.lua_State) callconv(.c) c_int {
523 const cfg = config orelse return 0;
524 const s = state orelse return 0;
525 cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null));
526 cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null));
527 return 0;
528 }
529
530 fn luaGapsSetSmart(state: ?*c.lua_State) callconv(.c) c_int {
531 const cfg = config orelse return 0;
532 const s = state orelse return 0;
533 cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0;
534 return 0;
535 }
536
537 fn luaBorderSetWidth(state: ?*c.lua_State) callconv(.c) c_int {
538 const cfg = config orelse return 0;
539 const s = state orelse return 0;
540 cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null));
541 return 0;
542 }
543
544 fn luaBorderSetFocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
545 const cfg = config orelse return 0;
546 const s = state orelse return 0;
547 cfg.border_focused = parseColor(s, 1);
548 return 0;
549 }
550
551 fn luaBorderSetUnfocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
552 const cfg = config orelse return 0;
553 const s = state orelse return 0;
554 cfg.border_unfocused = parseColor(s, 1);
555 return 0;
556 }
557
558 fn luaClientKill(state: ?*c.lua_State) callconv(.c) c_int {
559 const s = state orelse return 0;
560 createActionTable(s, "KillClient");
561 return 1;
562 }
563
564 fn luaClientToggleFullscreen(state: ?*c.lua_State) callconv(.c) c_int {
565 const s = state orelse return 0;
566 createActionTable(s, "ToggleFullScreen");
567 return 1;
568 }
569
570 fn luaClientToggleFloating(state: ?*c.lua_State) callconv(.c) c_int {
571 const s = state orelse return 0;
572 createActionTable(s, "ToggleFloating");
573 return 1;
574 }
575
576 fn luaClientFocusStack(state: ?*c.lua_State) callconv(.c) c_int {
577 const s = state orelse return 0;
578 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
579 createActionTableWithInt(s, "FocusStack", dir);
580 return 1;
581 }
582
583 fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int {
584 const s = state orelse return 0;
585 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
586 createActionTableWithInt(s, "MoveStack", dir);
587 return 1;
588 }
589
590 fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int {
591 const s = state orelse return 0;
592 createActionTable(s, "CycleLayout");
593 return 1;
594 }
595
596 fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int {
597 const s = state orelse return 0;
598 createActionTableWithString(s, "ChangeLayout");
599 return 1;
600 }
601
602 fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int {
603 const s = state orelse return 0;
604 createActionTable(s, "ScrollLeft");
605 return 1;
606 }
607
608 fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
609 const s = state orelse return 0;
610 createActionTable(s, "ScrollRight");
611 return 1;
612 }
613
614 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
615 const s = state orelse return 0;
616 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
617 createActionTableWithInt(s, "ViewTag", idx);
618 return 1;
619 }
620
621 fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int {
622 const s = state orelse return 0;
623 createActionTable(s, "ViewNextTag");
624 return 1;
625 }
626
627 fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int {
628 const s = state orelse return 0;
629 createActionTable(s, "ViewPreviousTag");
630 return 1;
631 }
632
633 fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int {
634 const s = state orelse return 0;
635 createActionTable(s, "ViewNextNonEmptyTag");
636 return 1;
637 }
638
639 fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int {
640 const s = state orelse return 0;
641 createActionTable(s, "ViewPreviousNonEmptyTag");
642 return 1;
643 }
644
645 fn luaTagToggleview(state: ?*c.lua_State) callconv(.c) c_int {
646 const s = state orelse return 0;
647 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
648 createActionTableWithInt(s, "ToggleView", idx);
649 return 1;
650 }
651
652 fn luaTagMoveTo(state: ?*c.lua_State) callconv(.c) c_int {
653 const s = state orelse return 0;
654 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
655 createActionTableWithInt(s, "MoveToTag", idx);
656 return 1;
657 }
658
659 fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int {
660 const s = state orelse return 0;
661 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
662 createActionTableWithInt(s, "ToggleTag", idx);
663 return 1;
664 }
665
666 fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int {
667 const cfg = config orelse return 0;
668 const s = state orelse return 0;
669 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
670 return 0;
671 }
672
673 fn luaMonitorFocus(state: ?*c.lua_State) callconv(.c) c_int {
674 const s = state orelse return 0;
675 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
676 createActionTableWithInt(s, "FocusMonitor", dir);
677 return 1;
678 }
679
680 fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int {
681 const s = state orelse return 0;
682 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
683 createActionTableWithInt(s, "TagMonitor", dir);
684 return 1;
685 }
686
687 fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int {
688 const cfg = config orelse return 0;
689 const s = state orelse return 0;
690
691 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
692
693 var rule = Rule{
694 .class = null,
695 .instance = null,
696 .title = null,
697 .tags = 0,
698 .is_floating = false,
699 .monitor = -1,
700 .focus = false,
701 };
702
703 _ = c.lua_getfield(s, 1, "class");
704 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
705 rule.class = getLuaString(s, -1);
706 }
707 c.lua_settop(s, -2);
708
709 _ = c.lua_getfield(s, 1, "instance");
710 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
711 rule.instance = getLuaString(s, -1);
712 }
713 c.lua_settop(s, -2);
714
715 _ = c.lua_getfield(s, 1, "title");
716 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
717 rule.title = getLuaString(s, -1);
718 }
719 c.lua_settop(s, -2);
720
721 _ = c.lua_getfield(s, 1, "tag");
722 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
723 const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null));
724 if (tag_idx > 0) {
725 rule.tags = @as(u32, 1) << @intCast(tag_idx - 1);
726 }
727 }
728 c.lua_settop(s, -2);
729
730 _ = c.lua_getfield(s, 1, "floating");
731 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
732 rule.is_floating = c.lua_toboolean(s, -1) != 0;
733 }
734 c.lua_settop(s, -2);
735
736 _ = c.lua_getfield(s, 1, "monitor");
737 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
738 rule.monitor = @intCast(c.lua_tointegerx(s, -1, null));
739 }
740 c.lua_settop(s, -2);
741
742 _ = c.lua_getfield(s, 1, "focus");
743 if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) {
744 rule.focus = c.lua_toboolean(s, -1) != 0;
745 }
746 c.lua_settop(s, -2);
747
748 cfg.addRule(rule) catch return 0;
749 return 0;
750 }
751
752 fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int {
753 const cfg = config orelse return 0;
754 const s = state orelse return 0;
755 if (dupeLuaString(s, 1)) |font| {
756 cfg.font = font;
757 }
758 return 0;
759 }
760
761 fn luaBarSetPosition(state: ?*c.lua_State) callconv(.c) c_int {
762 const cfg = config orelse return 0;
763 const s = state orelse return 0;
764 if (dupeLuaString(s, 1)) |position| {
765 cfg.bar_position = position;
766 }
767 return 0;
768 }
769
770 fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int {
771 const cfg = config orelse return 0;
772 const s = state orelse return 0;
773
774 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
775
776 const len = c.lua_rawlen(s, 1);
777 var i: usize = 1;
778 while (i <= len) : (i += 1) {
779 _ = c.lua_rawgeti(s, 1, @intCast(i));
780
781 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
782 c.lua_settop(s, -2);
783 continue;
784 }
785
786 if (parseBlockConfig(s, -1)) |block| {
787 cfg.addBlock(block) catch {};
788 }
789
790 c.lua_settop(s, -2);
791 }
792
793 return 0;
794 }
795
796 fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
797 _ = c.lua_getfield(state, idx, "__block_type");
798 const block_type_str = getLuaString(state, -1) orelse {
799 c.lua_settop(state, -2);
800 return null;
801 };
802 c.lua_settop(state, -2);
803
804 _ = c.lua_getfield(state, idx, "format");
805 const format = dupeLuaString(state, -1) orelse "";
806 c.lua_settop(state, -2);
807
808 _ = c.lua_getfield(state, idx, "interval");
809 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
810 c.lua_settop(state, -2);
811
812 _ = c.lua_getfield(state, idx, "color");
813 const color = parseColor(state, -1);
814 c.lua_settop(state, -2);
815
816 _ = c.lua_getfield(state, idx, "underline");
817 const underline = c.lua_toboolean(state, -1) != 0;
818 c.lua_settop(state, -2);
819
820 var block = Block{
821 .block_type = .static,
822 .format = format,
823 .interval = interval,
824 .color = color,
825 .underline = underline,
826 };
827
828 if (std.mem.eql(u8, block_type_str, "Ram")) {
829 block.block_type = .ram;
830 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
831 block.block_type = .datetime;
832 _ = c.lua_getfield(state, idx, "__arg");
833 block.datetime_format = dupeLuaString(state, -1);
834 c.lua_settop(state, -2);
835 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
836 block.block_type = .shell;
837 _ = c.lua_getfield(state, idx, "__arg");
838 block.command = dupeLuaString(state, -1);
839 c.lua_settop(state, -2);
840 } else if (std.mem.eql(u8, block_type_str, "Static")) {
841 block.block_type = .static;
842 _ = c.lua_getfield(state, idx, "__arg");
843 if (dupeLuaString(state, -1)) |text| {
844 block.format = text;
845 }
846 c.lua_settop(state, -2);
847 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
848 block.block_type = .battery;
849 _ = c.lua_getfield(state, idx, "__arg");
850 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
851 _ = c.lua_getfield(state, -1, "charging");
852 block.format_charging = dupeLuaString(state, -1);
853 c.lua_settop(state, -2);
854
855 _ = c.lua_getfield(state, -1, "discharging");
856 block.format_discharging = dupeLuaString(state, -1);
857 c.lua_settop(state, -2);
858
859 _ = c.lua_getfield(state, -1, "full");
860 block.format_full = dupeLuaString(state, -1);
861 c.lua_settop(state, -2);
862
863 _ = c.lua_getfield(state, -1, "battery_name");
864 block.battery_name = dupeLuaString(state, -1);
865 c.lua_settop(state, -2);
866 }
867 c.lua_settop(state, -2);
868 } else {
869 return null;
870 }
871
872 return block;
873 }
874
875 fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int {
876 const cfg = config orelse return 0;
877 const s = state orelse return 0;
878 cfg.scheme_normal = parseScheme(s);
879 return 0;
880 }
881
882 fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int {
883 const cfg = config orelse return 0;
884 const s = state orelse return 0;
885 cfg.scheme_selected = parseScheme(s);
886 return 0;
887 }
888
889 fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int {
890 const cfg = config orelse return 0;
891 const s = state orelse return 0;
892 cfg.scheme_occupied = parseScheme(s);
893 return 0;
894 }
895
896 fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int {
897 const cfg = config orelse return 0;
898 const s = state orelse return 0;
899 cfg.scheme_urgent = parseScheme(s);
900 return 0;
901 }
902
903 fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int {
904 const cfg = config orelse return 0;
905 const s = state orelse return 0;
906 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
907 return 0;
908 }
909
910 fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int {
911 const s = state orelse return 0;
912 createBlockTable(s, "Ram", null);
913 return 1;
914 }
915
916 fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int {
917 const s = state orelse return 0;
918 _ = c.lua_getfield(s, 1, "date_format");
919 const date_format = getLuaString(s, -1);
920 c.lua_settop(s, -2);
921 createBlockTable(s, "DateTime", date_format);
922 return 1;
923 }
924
925 fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int {
926 const s = state orelse return 0;
927 _ = c.lua_getfield(s, 1, "command");
928 const command = getLuaString(s, -1);
929 c.lua_settop(s, -2);
930 createBlockTable(s, "Shell", command);
931 return 1;
932 }
933
934 fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
935 const s = state orelse return 0;
936 _ = c.lua_getfield(s, 1, "text");
937 const text = getLuaString(s, -1);
938 c.lua_settop(s, -2);
939 createBlockTable(s, "Static", text);
940 return 1;
941 }
942
943 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
944 const s = state orelse return 0;
945
946 c.lua_createtable(s, 0, 6);
947
948 _ = c.lua_pushstring(s, "Battery");
949 c.lua_setfield(s, -2, "__block_type");
950
951 _ = c.lua_getfield(s, 1, "format");
952 c.lua_setfield(s, -2, "format");
953
954 _ = c.lua_getfield(s, 1, "interval");
955 c.lua_setfield(s, -2, "interval");
956
957 _ = c.lua_getfield(s, 1, "color");
958 c.lua_setfield(s, -2, "color");
959
960 _ = c.lua_getfield(s, 1, "underline");
961 c.lua_setfield(s, -2, "underline");
962
963 c.lua_createtable(s, 0, 4);
964 _ = c.lua_getfield(s, 1, "charging");
965 c.lua_setfield(s, -2, "charging");
966 _ = c.lua_getfield(s, 1, "discharging");
967 c.lua_setfield(s, -2, "discharging");
968 _ = c.lua_getfield(s, 1, "full");
969 c.lua_setfield(s, -2, "full");
970 _ = c.lua_getfield(s, 1, "battery_name");
971 c.lua_setfield(s, -2, "battery_name");
972 c.lua_setfield(s, -2, "__arg");
973
974 return 1;
975 }
976
977 fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
978 c.lua_createtable(state, 0, 6);
979
980 _ = c.lua_pushstring(state, block_type);
981 c.lua_setfield(state, -2, "__block_type");
982
983 _ = c.lua_getfield(state, 1, "format");
984 c.lua_setfield(state, -2, "format");
985
986 _ = c.lua_getfield(state, 1, "interval");
987 c.lua_setfield(state, -2, "interval");
988
989 _ = c.lua_getfield(state, 1, "color");
990 c.lua_setfield(state, -2, "color");
991
992 _ = c.lua_getfield(state, 1, "underline");
993 c.lua_setfield(state, -2, "underline");
994
995 if (arg) |a| {
996 var buf: [256]u8 = undefined;
997 if (a.len < buf.len) {
998 @memcpy(buf[0..a.len], a);
999 buf[a.len] = 0;
1000 _ = c.lua_pushstring(state, &buf);
1001 c.lua_setfield(state, -2, "__arg");
1002 }
1003 }
1004 }
1005
1006 fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int {
1007 const cfg = config orelse return 0;
1008 const s = state orelse return 0;
1009 if (dupeLuaString(s, 1)) |term| {
1010 cfg.terminal = term;
1011 }
1012 return 0;
1013 }
1014
1015 fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int {
1016 const cfg = config orelse return 0;
1017 const s = state orelse return 0;
1018 if (dupeLuaString(s, 1)) |layout| {
1019 cfg.layout = layout;
1020 }
1021 return 0;
1022 }
1023
1024 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1025 const cfg = config orelse return 0;
1026 const s = state orelse return 0;
1027 if (getStringArg(s, 1)) |modkey_str| {
1028 cfg.modkey = parseSingleModifier(modkey_str);
1029 cfg.addButton(.{
1030 .click = .client_win,
1031 .mod_mask = cfg.modkey,
1032 .button = 1,
1033 .action = .move_mouse,
1034 }) catch {};
1035 cfg.addButton(.{
1036 .click = .client_win,
1037 .mod_mask = cfg.modkey,
1038 .button = 3,
1039 .action = .resize_mouse,
1040 }) catch {};
1041 }
1042 return 0;
1043 }
1044
1045 fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1046 const cfg = config orelse return 0;
1047 const s = state orelse return 0;
1048
1049 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
1050
1051 const len = c.lua_rawlen(s, 1);
1052 var i: usize = 0;
1053 while (i < len and i < 9) : (i += 1) {
1054 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1055 if (dupeLuaString(s, -1)) |tag_str| {
1056 cfg.tags[i] = tag_str;
1057 }
1058 c.lua_settop(s, -2);
1059 }
1060
1061 return 0;
1062 }
1063
1064 fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1065 const cfg = config orelse return 0;
1066 const s = state orelse return 0;
1067 if (dupeLuaString(s, 1)) |cmd| {
1068 cfg.addAutostart(cmd) catch return 0;
1069 }
1070 return 0;
1071 }
1072
1073 fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1074 const cfg = config orelse return 0;
1075 const s = state orelse return 0;
1076 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1077 return 0;
1078 }
1079
1080 fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1081 const cfg = config orelse return 0;
1082 const s = state orelse return 0;
1083 const name = getStringArg(s, 1) orelse return 0;
1084 const symbol = dupeLuaString(s, 2) orelse return 0;
1085
1086 const layout_map = .{
1087 .{ "tiling", &cfg.layout_tile_symbol },
1088 .{ "tile", &cfg.layout_tile_symbol },
1089 .{ "normie", &cfg.layout_floating_symbol },
1090 .{ "floating", &cfg.layout_floating_symbol },
1091 .{ "float", &cfg.layout_floating_symbol },
1092 .{ "monocle", &cfg.layout_monocle_symbol },
1093 .{ "scrolling", &cfg.layout_scrolling_symbol },
1094 .{ "scroll", &cfg.layout_scrolling_symbol },
1095 .{ "grid", &cfg.layout_grid_symbol },
1096 };
1097
1098 inline for (layout_map) |entry| {
1099 if (std.mem.eql(u8, name, entry[0])) {
1100 entry[1].* = symbol;
1101 return 0;
1102 }
1103 }
1104 return 0;
1105 }
1106
1107 fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1108 const s = state orelse return 0;
1109 createActionTable(s, "Quit");
1110 return 1;
1111 }
1112
1113 fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1114 const s = state orelse return 0;
1115 createActionTable(s, "Restart");
1116 return 1;
1117 }
1118
1119 fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1120 const s = state orelse return 0;
1121 createActionTable(s, "ToggleGaps");
1122 return 1;
1123 }
1124
1125 fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1126 const s = state orelse return 0;
1127 createActionTable(s, "ShowKeybinds");
1128 return 1;
1129 }
1130
1131 fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1132 const s = state orelse return 0;
1133 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1134 createActionTableWithInt(s, "ResizeMaster", delta);
1135 return 1;
1136 }
1137
1138 fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1139 const s = state orelse return 0;
1140 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1141 if (delta > 0) {
1142 createActionTable(s, "IncMaster");
1143 } else {
1144 createActionTable(s, "DecMaster");
1145 }
1146 return 1;
1147 }
1148
1149 fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1150 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1151 return getLuaString(state, idx);
1152 }
1153
1154 fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1155 const len = c.lua_rawlen(state, idx);
1156 if (len == 0) return null;
1157
1158 if (len >= 3) {
1159 _ = c.lua_rawgeti(state, idx, 1);
1160 const first = getLuaString(state, -1);
1161 c.lua_settop(state, -2);
1162
1163 _ = c.lua_rawgeti(state, idx, 2);
1164 const second = getLuaString(state, -1);
1165 c.lua_settop(state, -2);
1166
1167 if (first != null and second != null and
1168 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1169 {
1170 _ = c.lua_rawgeti(state, idx, 3);
1171 const cmd = getLuaString(state, -1);
1172 c.lua_settop(state, -2);
1173 return cmd;
1174 }
1175 }
1176
1177 _ = c.lua_rawgeti(state, idx, 1);
1178 const first_elem = getLuaString(state, -1);
1179 c.lua_settop(state, -2);
1180 return first_elem;
1181 }
1182
1183 fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1184 const cstr = c.lua_tolstring(state, idx, null);
1185 if (cstr == null) return null;
1186 return std.mem.span(cstr);
1187 }
1188
1189 fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1190 const cfg = config orelse return null;
1191 const lua_str = getLuaString(state, idx) orelse return null;
1192 const arena_allocator = cfg.string_arena.allocator();
1193 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1194 return duped;
1195 }
1196
1197 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1198 const lua_type = c.lua_type(state, idx);
1199 if (lua_type == c.LUA_TNUMBER) {
1200 return @intCast(c.lua_tointegerx(state, idx, null));
1201 }
1202 if (lua_type == c.LUA_TSTRING) {
1203 const str = getLuaString(state, idx) orelse return 0;
1204 if (str.len > 0 and str[0] == '#') {
1205 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1206 }
1207 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1208 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1209 }
1210 return std.fmt.parseInt(u32, str, 16) catch return 0;
1211 }
1212 return 0;
1213 }
1214
1215 fn parseScheme(state: *c.lua_State) ColorScheme {
1216 return ColorScheme{
1217 .foreground = parseColor(state, 1),
1218 .background = parseColor(state, 2),
1219 .border = parseColor(state, 3),
1220 };
1221 }
1222
1223 fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1224 var mod_mask: u32 = 0;
1225
1226 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1227
1228 const len = c.lua_rawlen(state, idx);
1229 var i: usize = 1;
1230 while (i <= len) : (i += 1) {
1231 _ = c.lua_rawgeti(state, idx, @intCast(i));
1232 if (getLuaString(state, -1)) |mod_str| {
1233 const parsed = parseSingleModifier(mod_str);
1234 mod_mask |= parsed;
1235 }
1236 c.lua_settop(state, -2);
1237 }
1238
1239 return mod_mask;
1240 }
1241
1242 fn parseModifiersAtTop(state: *c.lua_State) u32 {
1243 return parseModifiers(state, -1);
1244 }
1245
1246 fn parseSingleModifier(name: []const u8) u32 {
1247 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1248 return (1 << 6);
1249 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1250 return (1 << 3);
1251 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1252 return (1 << 0);
1253 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1254 return (1 << 2);
1255 }
1256 return 0;
1257 }
1258
1259 fn parseAction(name: []const u8) ?Action {
1260 const action_map = .{
1261 .{ "Spawn", Action.spawn },
1262 .{ "SpawnTerminal", Action.spawn_terminal },
1263 .{ "KillClient", Action.kill_client },
1264 .{ "Quit", Action.quit },
1265 .{ "Restart", Action.restart },
1266 .{ "ShowKeybinds", Action.show_keybinds },
1267 .{ "FocusStack", Action.focus_next },
1268 .{ "MoveStack", Action.move_next },
1269 .{ "ResizeMaster", Action.resize_master },
1270 .{ "IncMaster", Action.inc_master },
1271 .{ "DecMaster", Action.dec_master },
1272 .{ "ToggleFloating", Action.toggle_floating },
1273 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1274 .{ "ToggleGaps", Action.toggle_gaps },
1275 .{ "CycleLayout", Action.cycle_layout },
1276 .{ "ChangeLayout", Action.set_layout },
1277 .{ "ViewTag", Action.view_tag },
1278 .{ "ViewNextTag", Action.view_next_tag },
1279 .{ "ViewPreviousTag", Action.view_prev_tag },
1280 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1281 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1282 .{ "MoveToTag", Action.move_to_tag },
1283 .{ "ToggleView", Action.toggle_view_tag },
1284 .{ "ToggleTag", Action.toggle_tag },
1285 .{ "FocusMonitor", Action.focus_monitor },
1286 .{ "TagMonitor", Action.send_to_monitor },
1287 .{ "ScrollLeft", Action.scroll_left },
1288 .{ "ScrollRight", Action.scroll_right },
1289 };
1290
1291 inline for (action_map) |entry| {
1292 if (std.mem.eql(u8, name, entry[0])) {
1293 return entry[1];
1294 }
1295 }
1296 return null;
1297 }
1298
1299 fn keynameToKeysym(name: []const u8) ?u64 {
1300 const key_map = .{
1301 .{ "Return", 0xff0d },
1302 .{ "Enter", 0xff0d },
1303 .{ "Tab", 0xff09 },
1304 .{ "Escape", 0xff1b },
1305 .{ "BackSpace", 0xff08 },
1306 .{ "Delete", 0xffff },
1307 .{ "space", 0x0020 },
1308 .{ "Space", 0x0020 },
1309 .{ "comma", 0x002c },
1310 .{ "Comma", 0x002c },
1311 .{ "period", 0x002e },
1312 .{ "Period", 0x002e },
1313 .{ "slash", 0x002f },
1314 .{ "Slash", 0x002f },
1315 .{ "minus", 0x002d },
1316 .{ "Minus", 0x002d },
1317 .{ "equal", 0x003d },
1318 .{ "Equal", 0x003d },
1319 .{ "bracketleft", 0x005b },
1320 .{ "bracketright", 0x005d },
1321 .{ "backslash", 0x005c },
1322 .{ "semicolon", 0x003b },
1323 .{ "apostrophe", 0x0027 },
1324 .{ "grave", 0x0060 },
1325 .{ "Left", 0xff51 },
1326 .{ "Up", 0xff52 },
1327 .{ "Right", 0xff53 },
1328 .{ "Down", 0xff54 },
1329 .{ "F1", 0xffbe },
1330 .{ "F2", 0xffbf },
1331 .{ "F3", 0xffc0 },
1332 .{ "F4", 0xffc1 },
1333 .{ "F5", 0xffc2 },
1334 .{ "F6", 0xffc3 },
1335 .{ "F7", 0xffc4 },
1336 .{ "F8", 0xffc5 },
1337 .{ "F9", 0xffc6 },
1338 .{ "F10", 0xffc7 },
1339 .{ "F11", 0xffc8 },
1340 .{ "F12", 0xffc9 },
1341 .{ "Print", 0xff61 },
1342 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1343 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1344 .{ "XF86AudioMute", 0x1008ff12 },
1345 .{ "XF86AudioPlay", 0x1008ff14 },
1346 .{ "XF86AudioPause", 0x1008ff31 },
1347 .{ "XF86AudioNext", 0x1008ff17 },
1348 .{ "XF86AudioPrev", 0x1008ff16 },
1349 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1350 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1351 };
1352
1353 inline for (key_map) |entry| {
1354 if (std.mem.eql(u8, name, entry[0])) {
1355 return entry[1];
1356 }
1357 }
1358
1359 if (name.len == 1) {
1360 const char = name[0];
1361 if (char >= 'a' and char <= 'z') {
1362 return char;
1363 }
1364 if (char >= 'A' and char <= 'Z') {
1365 return char + 32;
1366 }
1367 if (char >= '0' and char <= '9') {
1368 return char;
1369 }
1370 }
1371
1372 return null;
1373 }