tony / oxwm

Branch: fd44600
Switch branches/tags
master
tonybanters Updated systray to support underlines in config.lua fd44600 2026-03-25
1379 lines · 40.2 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_createtable(state, 0, 6);
278
279 c.lua_pushcfunction(state, luaBarBlockRam);
280 c.lua_setfield(state, -2, "ram");
281
282 c.lua_pushcfunction(state, luaBarBlockDatetime);
283 c.lua_setfield(state, -2, "datetime");
284
285 c.lua_pushcfunction(state, luaBarBlockShell);
286 c.lua_setfield(state, -2, "shell");
287
288 c.lua_pushcfunction(state, luaBarBlockStatic);
289 c.lua_setfield(state, -2, "static");
290
291 c.lua_pushcfunction(state, luaBarBlockBattery);
292 c.lua_setfield(state, -2, "battery");
293
294 c.lua_pushcfunction(state, luaBarBlockSystray);
295 c.lua_setfield(state, -2, "systray");
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, luaSetModkey);
307 c.lua_setfield(state, -2, "set_modkey");
308
309 c.lua_pushcfunction(state, luaSetTags);
310 c.lua_setfield(state, -2, "set_tags");
311
312 c.lua_pushcfunction(state, luaSetLayoutSymbol);
313 c.lua_setfield(state, -2, "set_layout_symbol");
314
315 c.lua_pushcfunction(state, luaAutostart);
316 c.lua_setfield(state, -2, "autostart");
317
318 c.lua_pushcfunction(state, luaAutoTile);
319 c.lua_setfield(state, -2, "auto_tile");
320
321 c.lua_pushcfunction(state, luaTiledResizeMode);
322 c.lua_setfield(state, -2, "tiled_resize_mode");
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 luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int {
762 const cfg = config orelse return 0;
763 const s = state orelse return 0;
764
765 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
766
767 const len = c.lua_rawlen(s, 1);
768 var i: usize = 1;
769 while (i <= len) : (i += 1) {
770 _ = c.lua_rawgeti(s, 1, @intCast(i));
771
772 if (c.lua_type(s, -1) != c.LUA_TTABLE) {
773 c.lua_settop(s, -2);
774 continue;
775 }
776
777 if (parseBlockConfig(s, -1)) |block| {
778 cfg.addBlock(block) catch {};
779 }
780
781 c.lua_settop(s, -2);
782 }
783
784 return 0;
785 }
786
787 fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
788 _ = c.lua_getfield(state, idx, "__block_type");
789 const block_type_str = getLuaString(state, -1) orelse {
790 c.lua_settop(state, -2);
791 return null;
792 };
793 c.lua_settop(state, -2);
794
795 _ = c.lua_getfield(state, idx, "format");
796 const format = dupeLuaString(state, -1) orelse "";
797 c.lua_settop(state, -2);
798
799 _ = c.lua_getfield(state, idx, "interval");
800 const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null));
801 c.lua_settop(state, -2);
802
803 _ = c.lua_getfield(state, idx, "color");
804 const color = parseColor(state, -1);
805 c.lua_settop(state, -2);
806
807 _ = c.lua_getfield(state, idx, "underline");
808 const underline = c.lua_toboolean(state, -1) != 0;
809 c.lua_settop(state, -2);
810
811 var block = Block{
812 .block_type = .static,
813 .format = format,
814 .interval = interval,
815 .color = color,
816 .underline = underline,
817 };
818
819 if (std.mem.eql(u8, block_type_str, "Ram")) {
820 block.block_type = .ram;
821 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
822 block.block_type = .datetime;
823 _ = c.lua_getfield(state, idx, "__arg");
824 block.datetime_format = dupeLuaString(state, -1);
825 c.lua_settop(state, -2);
826 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
827 block.block_type = .shell;
828 _ = c.lua_getfield(state, idx, "__arg");
829 block.command = dupeLuaString(state, -1);
830 c.lua_settop(state, -2);
831 } else if (std.mem.eql(u8, block_type_str, "Static")) {
832 block.block_type = .static;
833 _ = c.lua_getfield(state, idx, "__arg");
834 if (dupeLuaString(state, -1)) |text| {
835 block.format = text;
836 }
837 c.lua_settop(state, -2);
838 } else if (std.mem.eql(u8, block_type_str, "Battery")) {
839 block.block_type = .battery;
840 _ = c.lua_getfield(state, idx, "__arg");
841 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
842 _ = c.lua_getfield(state, -1, "charging");
843 block.format_charging = dupeLuaString(state, -1);
844 c.lua_settop(state, -2);
845
846 _ = c.lua_getfield(state, -1, "discharging");
847 block.format_discharging = dupeLuaString(state, -1);
848 c.lua_settop(state, -2);
849
850 _ = c.lua_getfield(state, -1, "full");
851 block.format_full = dupeLuaString(state, -1);
852 c.lua_settop(state, -2);
853
854 _ = c.lua_getfield(state, -1, "battery_name");
855 block.battery_name = dupeLuaString(state, -1);
856 c.lua_settop(state, -2);
857 }
858 c.lua_settop(state, -2);
859 } else if (std.mem.eql(u8, block_type_str, "Systray")) {
860 block.block_type = .systray;
861 } else {
862 return null;
863 }
864
865 return block;
866 }
867
868 fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int {
869 const cfg = config orelse return 0;
870 const s = state orelse return 0;
871 cfg.scheme_normal = parseScheme(s);
872 return 0;
873 }
874
875 fn luaBarSetSchemeSelected(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_selected = parseScheme(s);
879 return 0;
880 }
881
882 fn luaBarSetSchemeOccupied(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_occupied = parseScheme(s);
886 return 0;
887 }
888
889 fn luaBarSetSchemeUrgent(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_urgent = parseScheme(s);
893 return 0;
894 }
895
896 fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int {
897 const cfg = config orelse return 0;
898 const s = state orelse return 0;
899 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
900 return 0;
901 }
902
903 fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int {
904 const s = state orelse return 0;
905 createBlockTable(s, "Ram", null);
906 return 1;
907 }
908
909 fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int {
910 const s = state orelse return 0;
911 _ = c.lua_getfield(s, 1, "date_format");
912 const date_format = getLuaString(s, -1);
913 c.lua_settop(s, -2);
914 createBlockTable(s, "DateTime", date_format);
915 return 1;
916 }
917
918 fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int {
919 const s = state orelse return 0;
920 _ = c.lua_getfield(s, 1, "command");
921 const command = getLuaString(s, -1);
922 c.lua_settop(s, -2);
923 createBlockTable(s, "Shell", command);
924 return 1;
925 }
926
927 fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
928 const s = state orelse return 0;
929 _ = c.lua_getfield(s, 1, "text");
930 const text = getLuaString(s, -1);
931 c.lua_settop(s, -2);
932 createBlockTable(s, "Static", text);
933 return 1;
934 }
935
936 fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int {
937 const s = state orelse return 0;
938 createBlockTable(s, "Systray", null);
939 return 1;
940 }
941
942 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
943 const s = state orelse return 0;
944
945 c.lua_createtable(s, 0, 6);
946
947 _ = c.lua_pushstring(s, "Battery");
948 c.lua_setfield(s, -2, "__block_type");
949
950 _ = c.lua_getfield(s, 1, "format");
951 c.lua_setfield(s, -2, "format");
952
953 _ = c.lua_getfield(s, 1, "interval");
954 c.lua_setfield(s, -2, "interval");
955
956 _ = c.lua_getfield(s, 1, "color");
957 c.lua_setfield(s, -2, "color");
958
959 _ = c.lua_getfield(s, 1, "underline");
960 c.lua_setfield(s, -2, "underline");
961
962 c.lua_createtable(s, 0, 4);
963 _ = c.lua_getfield(s, 1, "charging");
964 c.lua_setfield(s, -2, "charging");
965 _ = c.lua_getfield(s, 1, "discharging");
966 c.lua_setfield(s, -2, "discharging");
967 _ = c.lua_getfield(s, 1, "full");
968 c.lua_setfield(s, -2, "full");
969 _ = c.lua_getfield(s, 1, "battery_name");
970 c.lua_setfield(s, -2, "battery_name");
971 c.lua_setfield(s, -2, "__arg");
972
973 return 1;
974 }
975
976 fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
977 c.lua_createtable(state, 0, 6);
978
979 _ = c.lua_pushstring(state, block_type);
980 c.lua_setfield(state, -2, "__block_type");
981
982 _ = c.lua_getfield(state, 1, "format");
983 c.lua_setfield(state, -2, "format");
984
985 _ = c.lua_getfield(state, 1, "interval");
986 c.lua_setfield(state, -2, "interval");
987
988 _ = c.lua_getfield(state, 1, "color");
989 c.lua_setfield(state, -2, "color");
990
991 _ = c.lua_getfield(state, 1, "underline");
992 c.lua_setfield(state, -2, "underline");
993
994 if (arg) |a| {
995 var buf: [256]u8 = undefined;
996 if (a.len < buf.len) {
997 @memcpy(buf[0..a.len], a);
998 buf[a.len] = 0;
999 _ = c.lua_pushstring(state, &buf);
1000 c.lua_setfield(state, -2, "__arg");
1001 }
1002 }
1003 }
1004
1005 fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int {
1006 const cfg = config orelse return 0;
1007 const s = state orelse return 0;
1008 if (dupeLuaString(s, 1)) |term| {
1009 cfg.terminal = term;
1010 }
1011 return 0;
1012 }
1013
1014 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1015 const cfg = config orelse return 0;
1016 const s = state orelse return 0;
1017 if (getStringArg(s, 1)) |modkey_str| {
1018 cfg.modkey = parseSingleModifier(modkey_str);
1019 cfg.addButton(.{
1020 .click = .client_win,
1021 .mod_mask = cfg.modkey,
1022 .button = 1,
1023 .action = .move_mouse,
1024 }) catch {};
1025 cfg.addButton(.{
1026 .click = .client_win,
1027 .mod_mask = cfg.modkey,
1028 .button = 3,
1029 .action = .resize_mouse,
1030 }) catch {};
1031 }
1032 return 0;
1033 }
1034
1035 fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1036 const cfg = config orelse return 0;
1037 const s = state orelse return 0;
1038
1039 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
1040
1041 const len = c.lua_rawlen(s, 1);
1042 var i: usize = 0;
1043 while (i < len and i < 9) : (i += 1) {
1044 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1045 if (dupeLuaString(s, -1)) |tag_str| {
1046 cfg.tags[i] = tag_str;
1047 }
1048 c.lua_settop(s, -2);
1049 }
1050
1051 return 0;
1052 }
1053
1054 fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1055 const cfg = config orelse return 0;
1056 const s = state orelse return 0;
1057 if (dupeLuaString(s, 1)) |cmd| {
1058 cfg.addAutostart(cmd) catch return 0;
1059 }
1060 return 0;
1061 }
1062
1063 fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1064 const cfg = config orelse return 0;
1065 const s = state orelse return 0;
1066 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1067 return 0;
1068 }
1069
1070 fn luaTiledResizeMode(state: ?*c.lua_State) callconv(.c) c_int {
1071 const cfg = config orelse return 0;
1072 const s = state orelse return 0;
1073 cfg.tiled_resize_mode = c.lua_toboolean(s, 1) != 0;
1074 return 0;
1075 }
1076
1077 fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1078 const cfg = config orelse return 0;
1079 const s = state orelse return 0;
1080 const name = getStringArg(s, 1) orelse return 0;
1081 const symbol = dupeLuaString(s, 2) orelse return 0;
1082
1083 const layout_map = .{
1084 .{ "tiling", &cfg.layout_tile_symbol },
1085 .{ "tile", &cfg.layout_tile_symbol },
1086 .{ "normie", &cfg.layout_floating_symbol },
1087 .{ "floating", &cfg.layout_floating_symbol },
1088 .{ "float", &cfg.layout_floating_symbol },
1089 .{ "monocle", &cfg.layout_monocle_symbol },
1090 .{ "scrolling", &cfg.layout_scrolling_symbol },
1091 .{ "scroll", &cfg.layout_scrolling_symbol },
1092 };
1093
1094 inline for (layout_map) |entry| {
1095 if (std.mem.eql(u8, name, entry[0])) {
1096 entry[1].* = symbol;
1097 return 0;
1098 }
1099 }
1100 return 0;
1101 }
1102
1103 fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1104 const s = state orelse return 0;
1105 createActionTable(s, "Quit");
1106 return 1;
1107 }
1108
1109 fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1110 const s = state orelse return 0;
1111 createActionTable(s, "Restart");
1112 return 1;
1113 }
1114
1115 fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1116 const s = state orelse return 0;
1117 createActionTable(s, "ToggleGaps");
1118 return 1;
1119 }
1120
1121 fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1122 const s = state orelse return 0;
1123 createActionTable(s, "ShowKeybinds");
1124 return 1;
1125 }
1126
1127 fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1128 const s = state orelse return 0;
1129 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1130 createActionTableWithInt(s, "ResizeMaster", delta);
1131 return 1;
1132 }
1133
1134 fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1135 const s = state orelse return 0;
1136 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1137 if (delta > 0) {
1138 createActionTable(s, "IncMaster");
1139 } else {
1140 createActionTable(s, "DecMaster");
1141 }
1142 return 1;
1143 }
1144
1145 fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1146 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1147 return getLuaString(state, idx);
1148 }
1149
1150 fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1151 const len = c.lua_rawlen(state, idx);
1152 if (len == 0) return null;
1153
1154 if (len >= 3) {
1155 _ = c.lua_rawgeti(state, idx, 1);
1156 const first = getLuaString(state, -1);
1157 c.lua_settop(state, -2);
1158
1159 _ = c.lua_rawgeti(state, idx, 2);
1160 const second = getLuaString(state, -1);
1161 c.lua_settop(state, -2);
1162
1163 if (first != null and second != null and
1164 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1165 {
1166 _ = c.lua_rawgeti(state, idx, 3);
1167 const cmd = getLuaString(state, -1);
1168 c.lua_settop(state, -2);
1169 return cmd;
1170 }
1171 }
1172
1173 _ = c.lua_rawgeti(state, idx, 1);
1174 const first_elem = getLuaString(state, -1);
1175 c.lua_settop(state, -2);
1176 return first_elem;
1177 }
1178
1179 fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1180 const cstr = c.lua_tolstring(state, idx, null);
1181 if (cstr == null) return null;
1182 return std.mem.span(cstr);
1183 }
1184
1185 fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1186 const cfg = config orelse return null;
1187 const lua_str = getLuaString(state, idx) orelse return null;
1188 const arena_allocator = cfg.string_arena.allocator();
1189 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1190 return duped;
1191 }
1192
1193 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1194 const lua_type = c.lua_type(state, idx);
1195 if (lua_type == c.LUA_TNUMBER) {
1196 return @intCast(c.lua_tointegerx(state, idx, null));
1197 }
1198 if (lua_type == c.LUA_TSTRING) {
1199 const str = getLuaString(state, idx) orelse return 0;
1200 if (str.len > 0 and str[0] == '#') {
1201 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1202 }
1203 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1204 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1205 }
1206 return std.fmt.parseInt(u32, str, 16) catch return 0;
1207 }
1208 return 0;
1209 }
1210
1211 fn parseScheme(state: *c.lua_State) ColorScheme {
1212 return ColorScheme{
1213 .foreground = parseColor(state, 1),
1214 .background = parseColor(state, 2),
1215 .border = parseColor(state, 3),
1216 };
1217 }
1218
1219 fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1220 var mod_mask: u32 = 0;
1221
1222 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1223
1224 const len = c.lua_rawlen(state, idx);
1225 var i: usize = 1;
1226 while (i <= len) : (i += 1) {
1227 _ = c.lua_rawgeti(state, idx, @intCast(i));
1228 if (getLuaString(state, -1)) |mod_str| {
1229 const parsed = parseSingleModifier(mod_str);
1230 mod_mask |= parsed;
1231 }
1232 c.lua_settop(state, -2);
1233 }
1234
1235 return mod_mask;
1236 }
1237
1238 fn parseModifiersAtTop(state: *c.lua_State) u32 {
1239 return parseModifiers(state, -1);
1240 }
1241
1242 fn parseSingleModifier(name: []const u8) u32 {
1243 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1244 return (1 << 6);
1245 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1246 return (1 << 3);
1247 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1248 return (1 << 0);
1249 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1250 return (1 << 2);
1251 }
1252 return 0;
1253 }
1254
1255 fn parseAction(name: []const u8) ?Action {
1256 const action_map = .{
1257 .{ "Spawn", Action.spawn },
1258 .{ "SpawnTerminal", Action.spawn_terminal },
1259 .{ "KillClient", Action.kill_client },
1260 .{ "Quit", Action.quit },
1261 .{ "Restart", Action.restart },
1262 .{ "ShowKeybinds", Action.show_keybinds },
1263 .{ "FocusStack", Action.focus_next },
1264 .{ "MoveStack", Action.move_next },
1265 .{ "ResizeMaster", Action.resize_master },
1266 .{ "IncMaster", Action.inc_master },
1267 .{ "DecMaster", Action.dec_master },
1268 .{ "ToggleFloating", Action.toggle_floating },
1269 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1270 .{ "ToggleGaps", Action.toggle_gaps },
1271 .{ "CycleLayout", Action.cycle_layout },
1272 .{ "ChangeLayout", Action.set_layout },
1273 .{ "ViewTag", Action.view_tag },
1274 .{ "ViewNextTag", Action.view_next_tag },
1275 .{ "ViewPreviousTag", Action.view_prev_tag },
1276 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1277 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1278 .{ "MoveToTag", Action.move_to_tag },
1279 .{ "ToggleView", Action.toggle_view_tag },
1280 .{ "ToggleTag", Action.toggle_tag },
1281 .{ "FocusMonitor", Action.focus_monitor },
1282 .{ "TagMonitor", Action.send_to_monitor },
1283 .{ "ScrollLeft", Action.scroll_left },
1284 .{ "ScrollRight", Action.scroll_right },
1285 };
1286
1287 inline for (action_map) |entry| {
1288 if (std.mem.eql(u8, name, entry[0])) {
1289 return entry[1];
1290 }
1291 }
1292 return null;
1293 }
1294
1295 fn keynameToKeysym(name: []const u8) ?u64 {
1296 const key_map = .{
1297 .{ "Return", 0xff0d },
1298 .{ "Enter", 0xff0d },
1299 .{ "Tab", 0xff09 },
1300 .{ "Escape", 0xff1b },
1301 .{ "BackSpace", 0xff08 },
1302 .{ "Delete", 0xffff },
1303 .{ "space", 0x0020 },
1304 .{ "Space", 0x0020 },
1305 .{ "comma", 0x002c },
1306 .{ "Comma", 0x002c },
1307 .{ "period", 0x002e },
1308 .{ "Period", 0x002e },
1309 .{ "slash", 0x002f },
1310 .{ "Slash", 0x002f },
1311 .{ "minus", 0x002d },
1312 .{ "Minus", 0x002d },
1313 .{ "equal", 0x003d },
1314 .{ "Equal", 0x003d },
1315 .{ "bracketleft", 0x005b },
1316 .{ "bracketright", 0x005d },
1317 .{ "backslash", 0x005c },
1318 .{ "colon", 0x003a },
1319 .{ "semicolon", 0x003b },
1320 .{ "apostrophe", 0x0027 },
1321 .{ "quotedbl", 0x0022 },
1322 .{ "ampersand", 0x0026 },
1323 .{ "parenleft", 0x0028 },
1324 .{ "parenright", 0x0029 },
1325 .{ "underscore", 0x005f },
1326 .{ "grave", 0x0060 },
1327 .{ "agrave", 0x00e0 },
1328 .{ "egrave", 0x00e8 },
1329 .{ "ccedilla", 0x00e7 },
1330 .{ "eacute", 0x00e9 },
1331 .{ "Left", 0xff51 },
1332 .{ "Up", 0xff52 },
1333 .{ "Right", 0xff53 },
1334 .{ "Down", 0xff54 },
1335 .{ "F1", 0xffbe },
1336 .{ "F2", 0xffbf },
1337 .{ "F3", 0xffc0 },
1338 .{ "F4", 0xffc1 },
1339 .{ "F5", 0xffc2 },
1340 .{ "F6", 0xffc3 },
1341 .{ "F7", 0xffc4 },
1342 .{ "F8", 0xffc5 },
1343 .{ "F9", 0xffc6 },
1344 .{ "F10", 0xffc7 },
1345 .{ "F11", 0xffc8 },
1346 .{ "F12", 0xffc9 },
1347 .{ "Print", 0xff61 },
1348 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1349 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1350 .{ "XF86AudioMute", 0x1008ff12 },
1351 .{ "XF86AudioPlay", 0x1008ff14 },
1352 .{ "XF86AudioPause", 0x1008ff31 },
1353 .{ "XF86AudioNext", 0x1008ff17 },
1354 .{ "XF86AudioPrev", 0x1008ff16 },
1355 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1356 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1357 };
1358
1359 inline for (key_map) |entry| {
1360 if (std.mem.eql(u8, name, entry[0])) {
1361 return entry[1];
1362 }
1363 }
1364
1365 if (name.len == 1) {
1366 const char = name[0];
1367 if (char >= 'a' and char <= 'z') {
1368 return char;
1369 }
1370 if (char >= 'A' and char <= 'Z') {
1371 return char + 32;
1372 }
1373 if (char >= '0' and char <= '9') {
1374 return char;
1375 }
1376 }
1377
1378 return null;
1379 }