tony / oxwm

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