tony / oxwm

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