tony / oxwm

Branch: c7ee21b
Switch branches/tags
master
Iziram feature : set default layout c7ee21b 2026-03-05
1360 lines · 39.5 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, luaSetLayout);
304 c.lua_setfield(state, -2, "set_layout");
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, 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 luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int {
1004 const cfg = config orelse return 0;
1005 const s = state orelse return 0;
1006 if (dupeLuaString(s, 1)) |layout| {
1007 cfg.layout = layout;
1008 }
1009 return 0;
1010 }
1011
1012 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1013 const cfg = config orelse return 0;
1014 const s = state orelse return 0;
1015 if (getStringArg(s, 1)) |modkey_str| {
1016 cfg.modkey = parseSingleModifier(modkey_str);
1017 cfg.addButton(.{
1018 .click = .client_win,
1019 .mod_mask = cfg.modkey,
1020 .button = 1,
1021 .action = .move_mouse,
1022 }) catch {};
1023 cfg.addButton(.{
1024 .click = .client_win,
1025 .mod_mask = cfg.modkey,
1026 .button = 3,
1027 .action = .resize_mouse,
1028 }) catch {};
1029 }
1030 return 0;
1031 }
1032
1033 fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1034 const cfg = config orelse return 0;
1035 const s = state orelse return 0;
1036
1037 if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
1038
1039 const len = c.lua_rawlen(s, 1);
1040 var i: usize = 0;
1041 while (i < len and i < 9) : (i += 1) {
1042 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1043 if (dupeLuaString(s, -1)) |tag_str| {
1044 cfg.tags[i] = tag_str;
1045 }
1046 c.lua_settop(s, -2);
1047 }
1048
1049 return 0;
1050 }
1051
1052 fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1053 const cfg = config orelse return 0;
1054 const s = state orelse return 0;
1055 if (dupeLuaString(s, 1)) |cmd| {
1056 cfg.addAutostart(cmd) catch return 0;
1057 }
1058 return 0;
1059 }
1060
1061 fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1062 const cfg = config orelse return 0;
1063 const s = state orelse return 0;
1064 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1065 return 0;
1066 }
1067
1068 fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1069 const cfg = config orelse return 0;
1070 const s = state orelse return 0;
1071 const name = getStringArg(s, 1) orelse return 0;
1072 const symbol = dupeLuaString(s, 2) orelse return 0;
1073
1074 const layout_map = .{
1075 .{ "tiling", &cfg.layout_tile_symbol },
1076 .{ "tile", &cfg.layout_tile_symbol },
1077 .{ "normie", &cfg.layout_floating_symbol },
1078 .{ "floating", &cfg.layout_floating_symbol },
1079 .{ "float", &cfg.layout_floating_symbol },
1080 .{ "monocle", &cfg.layout_monocle_symbol },
1081 .{ "scrolling", &cfg.layout_scrolling_symbol },
1082 .{ "scroll", &cfg.layout_scrolling_symbol },
1083 };
1084
1085 inline for (layout_map) |entry| {
1086 if (std.mem.eql(u8, name, entry[0])) {
1087 entry[1].* = symbol;
1088 return 0;
1089 }
1090 }
1091 return 0;
1092 }
1093
1094 fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1095 const s = state orelse return 0;
1096 createActionTable(s, "Quit");
1097 return 1;
1098 }
1099
1100 fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1101 const s = state orelse return 0;
1102 createActionTable(s, "Restart");
1103 return 1;
1104 }
1105
1106 fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1107 const s = state orelse return 0;
1108 createActionTable(s, "ToggleGaps");
1109 return 1;
1110 }
1111
1112 fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1113 const s = state orelse return 0;
1114 createActionTable(s, "ShowKeybinds");
1115 return 1;
1116 }
1117
1118 fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1119 const s = state orelse return 0;
1120 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1121 createActionTableWithInt(s, "ResizeMaster", delta);
1122 return 1;
1123 }
1124
1125 fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1126 const s = state orelse return 0;
1127 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1128 if (delta > 0) {
1129 createActionTable(s, "IncMaster");
1130 } else {
1131 createActionTable(s, "DecMaster");
1132 }
1133 return 1;
1134 }
1135
1136 fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1137 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1138 return getLuaString(state, idx);
1139 }
1140
1141 fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1142 const len = c.lua_rawlen(state, idx);
1143 if (len == 0) return null;
1144
1145 if (len >= 3) {
1146 _ = c.lua_rawgeti(state, idx, 1);
1147 const first = getLuaString(state, -1);
1148 c.lua_settop(state, -2);
1149
1150 _ = c.lua_rawgeti(state, idx, 2);
1151 const second = getLuaString(state, -1);
1152 c.lua_settop(state, -2);
1153
1154 if (first != null and second != null and
1155 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1156 {
1157 _ = c.lua_rawgeti(state, idx, 3);
1158 const cmd = getLuaString(state, -1);
1159 c.lua_settop(state, -2);
1160 return cmd;
1161 }
1162 }
1163
1164 _ = c.lua_rawgeti(state, idx, 1);
1165 const first_elem = getLuaString(state, -1);
1166 c.lua_settop(state, -2);
1167 return first_elem;
1168 }
1169
1170 fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1171 const cstr = c.lua_tolstring(state, idx, null);
1172 if (cstr == null) return null;
1173 return std.mem.span(cstr);
1174 }
1175
1176 fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1177 const cfg = config orelse return null;
1178 const lua_str = getLuaString(state, idx) orelse return null;
1179 const arena_allocator = cfg.string_arena.allocator();
1180 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1181 return duped;
1182 }
1183
1184 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1185 const lua_type = c.lua_type(state, idx);
1186 if (lua_type == c.LUA_TNUMBER) {
1187 return @intCast(c.lua_tointegerx(state, idx, null));
1188 }
1189 if (lua_type == c.LUA_TSTRING) {
1190 const str = getLuaString(state, idx) orelse return 0;
1191 if (str.len > 0 and str[0] == '#') {
1192 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1193 }
1194 if (str.len > 2 and str[0] == '0' and str[1] == 'x') {
1195 return std.fmt.parseInt(u32, str[2..], 16) catch return 0;
1196 }
1197 return std.fmt.parseInt(u32, str, 16) catch return 0;
1198 }
1199 return 0;
1200 }
1201
1202 fn parseScheme(state: *c.lua_State) ColorScheme {
1203 return ColorScheme{
1204 .foreground = parseColor(state, 1),
1205 .background = parseColor(state, 2),
1206 .border = parseColor(state, 3),
1207 };
1208 }
1209
1210 fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1211 var mod_mask: u32 = 0;
1212
1213 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
1214
1215 const len = c.lua_rawlen(state, idx);
1216 var i: usize = 1;
1217 while (i <= len) : (i += 1) {
1218 _ = c.lua_rawgeti(state, idx, @intCast(i));
1219 if (getLuaString(state, -1)) |mod_str| {
1220 const parsed = parseSingleModifier(mod_str);
1221 mod_mask |= parsed;
1222 }
1223 c.lua_settop(state, -2);
1224 }
1225
1226 return mod_mask;
1227 }
1228
1229 fn parseModifiersAtTop(state: *c.lua_State) u32 {
1230 return parseModifiers(state, -1);
1231 }
1232
1233 fn parseSingleModifier(name: []const u8) u32 {
1234 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1235 return (1 << 6);
1236 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
1237 return (1 << 3);
1238 } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) {
1239 return (1 << 0);
1240 } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) {
1241 return (1 << 2);
1242 }
1243 return 0;
1244 }
1245
1246 fn parseAction(name: []const u8) ?Action {
1247 const action_map = .{
1248 .{ "Spawn", Action.spawn },
1249 .{ "SpawnTerminal", Action.spawn_terminal },
1250 .{ "KillClient", Action.kill_client },
1251 .{ "Quit", Action.quit },
1252 .{ "Restart", Action.restart },
1253 .{ "ShowKeybinds", Action.show_keybinds },
1254 .{ "FocusStack", Action.focus_next },
1255 .{ "MoveStack", Action.move_next },
1256 .{ "ResizeMaster", Action.resize_master },
1257 .{ "IncMaster", Action.inc_master },
1258 .{ "DecMaster", Action.dec_master },
1259 .{ "ToggleFloating", Action.toggle_floating },
1260 .{ "ToggleFullScreen", Action.toggle_fullscreen },
1261 .{ "ToggleGaps", Action.toggle_gaps },
1262 .{ "CycleLayout", Action.cycle_layout },
1263 .{ "ChangeLayout", Action.set_layout },
1264 .{ "ViewTag", Action.view_tag },
1265 .{ "ViewNextTag", Action.view_next_tag },
1266 .{ "ViewPreviousTag", Action.view_prev_tag },
1267 .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag },
1268 .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag },
1269 .{ "MoveToTag", Action.move_to_tag },
1270 .{ "ToggleView", Action.toggle_view_tag },
1271 .{ "ToggleTag", Action.toggle_tag },
1272 .{ "FocusMonitor", Action.focus_monitor },
1273 .{ "TagMonitor", Action.send_to_monitor },
1274 .{ "ScrollLeft", Action.scroll_left },
1275 .{ "ScrollRight", Action.scroll_right },
1276 };
1277
1278 inline for (action_map) |entry| {
1279 if (std.mem.eql(u8, name, entry[0])) {
1280 return entry[1];
1281 }
1282 }
1283 return null;
1284 }
1285
1286 fn keynameToKeysym(name: []const u8) ?u64 {
1287 const key_map = .{
1288 .{ "Return", 0xff0d },
1289 .{ "Enter", 0xff0d },
1290 .{ "Tab", 0xff09 },
1291 .{ "Escape", 0xff1b },
1292 .{ "BackSpace", 0xff08 },
1293 .{ "Delete", 0xffff },
1294 .{ "space", 0x0020 },
1295 .{ "Space", 0x0020 },
1296 .{ "comma", 0x002c },
1297 .{ "Comma", 0x002c },
1298 .{ "period", 0x002e },
1299 .{ "Period", 0x002e },
1300 .{ "slash", 0x002f },
1301 .{ "Slash", 0x002f },
1302 .{ "minus", 0x002d },
1303 .{ "Minus", 0x002d },
1304 .{ "equal", 0x003d },
1305 .{ "Equal", 0x003d },
1306 .{ "bracketleft", 0x005b },
1307 .{ "bracketright", 0x005d },
1308 .{ "backslash", 0x005c },
1309 .{ "semicolon", 0x003b },
1310 .{ "apostrophe", 0x0027 },
1311 .{ "grave", 0x0060 },
1312 .{ "Left", 0xff51 },
1313 .{ "Up", 0xff52 },
1314 .{ "Right", 0xff53 },
1315 .{ "Down", 0xff54 },
1316 .{ "F1", 0xffbe },
1317 .{ "F2", 0xffbf },
1318 .{ "F3", 0xffc0 },
1319 .{ "F4", 0xffc1 },
1320 .{ "F5", 0xffc2 },
1321 .{ "F6", 0xffc3 },
1322 .{ "F7", 0xffc4 },
1323 .{ "F8", 0xffc5 },
1324 .{ "F9", 0xffc6 },
1325 .{ "F10", 0xffc7 },
1326 .{ "F11", 0xffc8 },
1327 .{ "F12", 0xffc9 },
1328 .{ "Print", 0xff61 },
1329 .{ "XF86AudioRaiseVolume", 0x1008ff13 },
1330 .{ "XF86AudioLowerVolume", 0x1008ff11 },
1331 .{ "XF86AudioMute", 0x1008ff12 },
1332 .{ "XF86AudioPlay", 0x1008ff14 },
1333 .{ "XF86AudioPause", 0x1008ff31 },
1334 .{ "XF86AudioNext", 0x1008ff17 },
1335 .{ "XF86AudioPrev", 0x1008ff16 },
1336 .{ "XF86MonBrightnessUp", 0x1008ff02 },
1337 .{ "XF86MonBrightnessDown", 0x1008ff03 },
1338 };
1339
1340 inline for (key_map) |entry| {
1341 if (std.mem.eql(u8, name, entry[0])) {
1342 return entry[1];
1343 }
1344 }
1345
1346 if (name.len == 1) {
1347 const char = name[0];
1348 if (char >= 'a' and char <= 'z') {
1349 return char;
1350 }
1351 if (char >= 'A' and char <= 'Z') {
1352 return char + 32;
1353 }
1354 if (char >= '0' and char <= '9') {
1355 return char;
1356 }
1357 }
1358
1359 return null;
1360 }