tony / oxwm

added tests, overlay, keychords. path to 100% compliance is en route. Browse files
tonybanters committed 2026-02-04 parent a8a8323 commit 2bbe4276bfcd504288c54b25f503d2a30a6ce8db
Showing 8 changed files with 780 additions and 84 deletions. UnifiedSplit
+16 −0 build.zig
@@ -40,6 +40,22 @@ pub fn build(b: *std.Build) void {
40 }); 40 });
41 test_step.dependOn(&b.addRunArtifact(unit_tests).step); 41 test_step.dependOn(&b.addRunArtifact(unit_tests).step);
42 42
43 + const lua_config_tests = b.addTest(.{
44 + .root_module = b.createModule(.{
45 + .root_source_file = b.path("tests/lua_config_tests.zig"),
46 + .target = target,
47 + .optimize = optimize,
48 + }),
49 + });
50 + lua_config_tests.root_module.addImport("lua", b.createModule(.{
51 + .root_source_file = b.path("src/config/lua.zig"),
52 + .target = target,
53 + .optimize = optimize,
54 + }));
55 + lua_config_tests.linkSystemLibrary("lua");
56 + lua_config_tests.linkLibC();
57 + test_step.dependOn(&b.addRunArtifact(lua_config_tests).step);
58 +
43 const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)"); 59 const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)");
44 xephyr_step.dependOn(&add_xephyr_run(b, exe, false).step); 60 xephyr_step.dependOn(&add_xephyr_run(b, exe, false).step);
45 61
+36 −36 resources/test-config.lua
@@ -107,50 +107,50 @@ oxwm.key.bind({ modkey, "Shift" }, "J", oxwm.client.move_stack(1))
107 oxwm.key.bind({ modkey, "Shift" }, "K", oxwm.client.move_stack(-1)) 107 oxwm.key.bind({ modkey, "Shift" }, "K", oxwm.client.move_stack(-1))
108 108
109 -- View tag (switch workspace) 109 -- View tag (switch workspace)
110 -oxwm.key.bind({ modkey }, "1", oxwm.tag.view(1)) 110 +oxwm.key.bind({ modkey }, "1", oxwm.tag.view(0))
111 -oxwm.key.bind({ modkey }, "2", oxwm.tag.view(2)) 111 +oxwm.key.bind({ modkey }, "2", oxwm.tag.view(1))
112 -oxwm.key.bind({ modkey }, "3", oxwm.tag.view(3)) 112 +oxwm.key.bind({ modkey }, "3", oxwm.tag.view(2))
113 -oxwm.key.bind({ modkey }, "4", oxwm.tag.view(4)) 113 +oxwm.key.bind({ modkey }, "4", oxwm.tag.view(3))
114 -oxwm.key.bind({ modkey }, "5", oxwm.tag.view(5)) 114 +oxwm.key.bind({ modkey }, "5", oxwm.tag.view(4))
115 -oxwm.key.bind({ modkey }, "6", oxwm.tag.view(6)) 115 +oxwm.key.bind({ modkey }, "6", oxwm.tag.view(5))
116 -oxwm.key.bind({ modkey }, "7", oxwm.tag.view(7)) 116 +oxwm.key.bind({ modkey }, "7", oxwm.tag.view(6))
117 -oxwm.key.bind({ modkey }, "8", oxwm.tag.view(8)) 117 +oxwm.key.bind({ modkey }, "8", oxwm.tag.view(7))
118 -oxwm.key.bind({ modkey }, "9", oxwm.tag.view(9)) 118 +oxwm.key.bind({ modkey }, "9", oxwm.tag.view(8))
119 119
120 -- Move window to tag 120 -- Move window to tag
121 -oxwm.key.bind({ modkey, "Shift" }, "1", oxwm.tag.move_to(1)) 121 +oxwm.key.bind({ modkey, "Shift" }, "1", oxwm.tag.move_to(0))
122 -oxwm.key.bind({ modkey, "Shift" }, "2", oxwm.tag.move_to(2)) 122 +oxwm.key.bind({ modkey, "Shift" }, "2", oxwm.tag.move_to(1))
123 -oxwm.key.bind({ modkey, "Shift" }, "3", oxwm.tag.move_to(3)) 123 +oxwm.key.bind({ modkey, "Shift" }, "3", oxwm.tag.move_to(2))
124 -oxwm.key.bind({ modkey, "Shift" }, "4", oxwm.tag.move_to(4)) 124 +oxwm.key.bind({ modkey, "Shift" }, "4", oxwm.tag.move_to(3))
125 -oxwm.key.bind({ modkey, "Shift" }, "5", oxwm.tag.move_to(5)) 125 +oxwm.key.bind({ modkey, "Shift" }, "5", oxwm.tag.move_to(4))
126 -oxwm.key.bind({ modkey, "Shift" }, "6", oxwm.tag.move_to(6)) 126 +oxwm.key.bind({ modkey, "Shift" }, "6", oxwm.tag.move_to(5))
127 -oxwm.key.bind({ modkey, "Shift" }, "7", oxwm.tag.move_to(7)) 127 +oxwm.key.bind({ modkey, "Shift" }, "7", oxwm.tag.move_to(6))
128 -oxwm.key.bind({ modkey, "Shift" }, "8", oxwm.tag.move_to(8)) 128 +oxwm.key.bind({ modkey, "Shift" }, "8", oxwm.tag.move_to(7))
129 -oxwm.key.bind({ modkey, "Shift" }, "9", oxwm.tag.move_to(9)) 129 +oxwm.key.bind({ modkey, "Shift" }, "9", oxwm.tag.move_to(8))
130 130
131 -- Toggle view (view multiple tags at once) - dwm-style multi-tag viewing 131 -- Toggle view (view multiple tags at once) - dwm-style multi-tag viewing
132 -- Example: Mod+Ctrl+2 while on tag 1 will show BOTH tags 1 and 2 132 -- Example: Mod+Ctrl+2 while on tag 1 will show BOTH tags 1 and 2
133 -oxwm.key.bind({ modkey, "Control" }, "1", oxwm.tag.toggleview(1)) 133 +oxwm.key.bind({ modkey, "Control" }, "1", oxwm.tag.toggleview(0))
134 -oxwm.key.bind({ modkey, "Control" }, "2", oxwm.tag.toggleview(2)) 134 +oxwm.key.bind({ modkey, "Control" }, "2", oxwm.tag.toggleview(1))
135 -oxwm.key.bind({ modkey, "Control" }, "3", oxwm.tag.toggleview(3)) 135 +oxwm.key.bind({ modkey, "Control" }, "3", oxwm.tag.toggleview(2))
136 -oxwm.key.bind({ modkey, "Control" }, "4", oxwm.tag.toggleview(4)) 136 +oxwm.key.bind({ modkey, "Control" }, "4", oxwm.tag.toggleview(3))
137 -oxwm.key.bind({ modkey, "Control" }, "5", oxwm.tag.toggleview(5)) 137 +oxwm.key.bind({ modkey, "Control" }, "5", oxwm.tag.toggleview(4))
138 -oxwm.key.bind({ modkey, "Control" }, "6", oxwm.tag.toggleview(6)) 138 +oxwm.key.bind({ modkey, "Control" }, "6", oxwm.tag.toggleview(5))
139 -oxwm.key.bind({ modkey, "Control" }, "7", oxwm.tag.toggleview(7)) 139 +oxwm.key.bind({ modkey, "Control" }, "7", oxwm.tag.toggleview(6))
140 -oxwm.key.bind({ modkey, "Control" }, "8", oxwm.tag.toggleview(8)) 140 +oxwm.key.bind({ modkey, "Control" }, "8", oxwm.tag.toggleview(7))
141 -oxwm.key.bind({ modkey, "Control" }, "9", oxwm.tag.toggleview(9)) 141 +oxwm.key.bind({ modkey, "Control" }, "9", oxwm.tag.toggleview(8))
142 142
143 -- Toggle tag (window on multiple tags) - dwm-style sticky windows 143 -- Toggle tag (window on multiple tags) - dwm-style sticky windows
144 -- Example: Mod+Ctrl+Shift+2 puts focused window on BOTH current tag and tag 2 144 -- Example: Mod+Ctrl+Shift+2 puts focused window on BOTH current tag and tag 2
145 -oxwm.key.bind({ modkey, "Control", "Shift" }, "1", oxwm.tag.toggletag(1)) 145 +oxwm.key.bind({ modkey, "Control", "Shift" }, "1", oxwm.tag.toggletag(0))
146 -oxwm.key.bind({ modkey, "Control", "Shift" }, "2", oxwm.tag.toggletag(2)) 146 +oxwm.key.bind({ modkey, "Control", "Shift" }, "2", oxwm.tag.toggletag(1))
147 -oxwm.key.bind({ modkey, "Control", "Shift" }, "3", oxwm.tag.toggletag(3)) 147 +oxwm.key.bind({ modkey, "Control", "Shift" }, "3", oxwm.tag.toggletag(2))
148 -oxwm.key.bind({ modkey, "Control", "Shift" }, "4", oxwm.tag.toggletag(4)) 148 +oxwm.key.bind({ modkey, "Control", "Shift" }, "4", oxwm.tag.toggletag(3))
149 -oxwm.key.bind({ modkey, "Control", "Shift" }, "5", oxwm.tag.toggletag(5)) 149 +oxwm.key.bind({ modkey, "Control", "Shift" }, "5", oxwm.tag.toggletag(4))
150 -oxwm.key.bind({ modkey, "Control", "Shift" }, "6", oxwm.tag.toggletag(6)) 150 +oxwm.key.bind({ modkey, "Control", "Shift" }, "6", oxwm.tag.toggletag(5))
151 -oxwm.key.bind({ modkey, "Control", "Shift" }, "7", oxwm.tag.toggletag(7)) 151 +oxwm.key.bind({ modkey, "Control", "Shift" }, "7", oxwm.tag.toggletag(6))
152 -oxwm.key.bind({ modkey, "Control", "Shift" }, "8", oxwm.tag.toggletag(8)) 152 +oxwm.key.bind({ modkey, "Control", "Shift" }, "8", oxwm.tag.toggletag(7))
153 -oxwm.key.bind({ modkey, "Control", "Shift" }, "9", oxwm.tag.toggletag(9)) 153 +oxwm.key.bind({ modkey, "Control", "Shift" }, "9", oxwm.tag.toggletag(8))
154 154
155 oxwm.key.bind({ modkey }, "Tab", oxwm.tag.view_next()) 155 oxwm.key.bind({ modkey }, "Tab", oxwm.tag.view_next())
156 oxwm.key.bind({ modkey, "Shift" }, "Tab", oxwm.tag.view_previous()) 156 oxwm.key.bind({ modkey, "Shift" }, "Tab", oxwm.tag.view_previous())
+7 −2 src/config/config.zig
@@ -36,9 +36,14 @@ pub const Action = enum {
36 scroll_right, 36 scroll_right,
37 }; 37 };
38 38
39 +pub const Key_Press = struct {
40 + mod_mask: u32 = 0,
41 + keysym: u64 = 0,
42 +};
43 +
39 pub const Keybind = struct { 44 pub const Keybind = struct {
40 - mod_mask: u32, 45 + keys: [4]Key_Press = [_]Key_Press{.{}} ** 4,
41 - keysym: u64, 46 + key_count: u8 = 1,
42 action: Action, 47 action: Action,
43 int_arg: i32 = 0, 48 int_arg: i32 = 0,
44 str_arg: ?[]const u8 = null, 49 str_arg: ?[]const u8 = null,
+80 −10 src/config/lua.zig
@@ -1,5 +1,5 @@
1 const std = @import("std"); 1 const std = @import("std");
2 -const config_mod = @import("config.zig"); 2 +pub const config_mod = @import("config.zig");
3 const Config = config_mod.Config; 3 const Config = config_mod.Config;
4 const Keybind = config_mod.Keybind; 4 const Keybind = config_mod.Keybind;
5 const Action = config_mod.Action; 5 const Action = config_mod.Action;
@@ -392,19 +392,85 @@ fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
392 } 392 }
393 c.lua_settop(s, -2); 393 c.lua_settop(s, -2);
394 394
395 - cfg.add_keybind(.{ 395 + var keybind: config_mod.Keybind = .{
396 - .mod_mask = mod_mask,
397 - .keysym = keysym,
398 .action = action, 396 .action = action,
399 .int_arg = int_arg, 397 .int_arg = int_arg,
400 .str_arg = str_arg, 398 .str_arg = str_arg,
401 - }) catch return 0; 399 + };
400 + keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym };
401 + keybind.key_count = 1;
402 +
403 + cfg.add_keybind(keybind) catch return 0;
402 404
403 return 0; 405 return 0;
404 } 406 }
405 407
406 fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int { 408 fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
407 - _ = state; 409 + const s = state orelse return 0;
410 + const cfg = config orelse return 0;
411 +
412 + if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0;
413 + if (c.lua_type(s, 2) != c.LUA_TTABLE) return 0;
414 +
415 + var keybind: config_mod.Keybind = .{
416 + .action = .quit,
417 + .int_arg = 0,
418 + .str_arg = null,
419 + };
420 + keybind.key_count = 0;
421 +
422 + const num_keys = c.lua_rawlen(s, 1);
423 + if (num_keys == 0 or num_keys > 4) return 0;
424 +
425 + var i: usize = 1;
426 + while (i <= num_keys) : (i += 1) {
427 + _ = c.lua_rawgeti(s, 1, @intCast(i));
428 + if (c.lua_type(s, -1) != c.LUA_TTABLE) {
429 + c.lua_settop(s, -2);
430 + return 0;
431 + }
432 +
433 + _ = c.lua_rawgeti(s, -1, 1);
434 + const mod_mask = parse_modifiers_at_top(s);
435 + c.lua_settop(s, -2);
436 +
437 + _ = c.lua_rawgeti(s, -1, 2);
438 + const key_str = get_lua_string(s, -1) orelse {
439 + c.lua_settop(s, -3);
440 + return 0;
441 + };
442 + c.lua_settop(s, -2);
443 +
444 + const keysym = key_name_to_keysym(key_str) orelse {
445 + c.lua_settop(s, -2);
446 + return 0;
447 + };
448 +
449 + keybind.keys[keybind.key_count] = .{ .mod_mask = mod_mask, .keysym = keysym };
450 + keybind.key_count += 1;
451 +
452 + c.lua_settop(s, -2);
453 + }
454 +
455 + _ = c.lua_getfield(s, 2, "__action");
456 + const action_str = get_lua_string(s, -1) orelse {
457 + c.lua_settop(s, -2);
458 + return 0;
459 + };
460 + c.lua_settop(s, -2);
461 +
462 + keybind.action = parse_action(action_str) orelse return 0;
463 +
464 + _ = c.lua_getfield(s, 2, "__arg");
465 + if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
466 + keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null));
467 + } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
468 + keybind.str_arg = get_lua_string(s, -1);
469 + }
470 + c.lua_settop(s, -2);
471 +
472 + cfg.add_keybind(keybind) catch return 0;
473 +
408 return 0; 474 return 0;
409 } 475 }
410 476
@@ -532,7 +598,7 @@ fn lua_layout_scroll_right(state: ?*c.lua_State) callconv(.c) c_int {
532 fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int { 598 fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int {
533 const s = state orelse return 0; 599 const s = state orelse return 0;
534 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); 600 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
535 - create_action_table_with_int(s, "ViewTag", idx - 1); 601 + create_action_table_with_int(s, "ViewTag", idx);
536 return 1; 602 return 1;
537 } 603 }
538 604
@@ -563,21 +629,21 @@ fn lua_tag_view_previous_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
563 fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int { 629 fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int {
564 const s = state orelse return 0; 630 const s = state orelse return 0;
565 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); 631 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
566 - create_action_table_with_int(s, "ToggleView", idx - 1); 632 + create_action_table_with_int(s, "ToggleView", idx);
567 return 1; 633 return 1;
568 } 634 }
569 635
570 fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int { 636 fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int {
571 const s = state orelse return 0; 637 const s = state orelse return 0;
572 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); 638 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
573 - create_action_table_with_int(s, "MoveToTag", idx - 1); 639 + create_action_table_with_int(s, "MoveToTag", idx);
574 return 1; 640 return 1;
575 } 641 }
576 642
577 fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int { 643 fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int {
578 const s = state orelse return 0; 644 const s = state orelse return 0;
579 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); 645 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
580 - create_action_table_with_int(s, "ToggleTag", idx - 1); 646 + create_action_table_with_int(s, "ToggleTag", idx);
581 return 1; 647 return 1;
582 } 648 }
583 649
@@ -1081,6 +1147,10 @@ fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1081 return mod_mask; 1147 return mod_mask;
1082 } 1148 }
1083 1149
1150 +fn parse_modifiers_at_top(state: *c.lua_State) u32 {
1151 + return parse_modifiers(state, -1);
1152 +}
1153 +
1084 fn parse_single_modifier(name: []const u8) u32 { 1154 fn parse_single_modifier(name: []const u8) u32 {
1085 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) { 1155 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1086 return (1 << 6); 1156 return (1 << 6);
+250 −36 src/main.zig
@@ -13,6 +13,7 @@ const bar_mod = @import("bar/bar.zig");
13 const blocks_mod = @import("bar/blocks/blocks.zig"); 13 const blocks_mod = @import("bar/blocks/blocks.zig");
14 const config_mod = @import("config/config.zig"); 14 const config_mod = @import("config/config.zig");
15 const lua = @import("config/lua.zig"); 15 const lua = @import("config/lua.zig");
16 +const overlay_mod = @import("overlay.zig");
16 17
17 const Display = display_mod.Display; 18 const Display = display_mod.Display;
18 const Client = client_mod.Client; 19 const Client = client_mod.Client;
@@ -67,6 +68,87 @@ var config_path_global: ?[]const u8 = null;
67 var scroll_animation: animations.Scroll_Animation = .{}; 68 var scroll_animation: animations.Scroll_Animation = .{};
68 var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out }; 69 var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out };
69 70
71 +var chord_keys: [4]config_mod.Key_Press = [_]config_mod.Key_Press{.{}} ** 4;
72 +var chord_index: u8 = 0;
73 +var chord_timestamp: i64 = 0;
74 +const chord_timeout_ms: i64 = 1000;
75 +var keyboard_grabbed: bool = false;
76 +
77 +var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null;
78 +
79 +fn print_help() void {
80 + std.debug.print(
81 + \\oxwm - A window manager
82 + \\
83 + \\USAGE:
84 + \\ oxwm [OPTIONS]
85 + \\
86 + \\OPTIONS:
87 + \\ --init Create default config in ~/.config/oxwm/config.lua
88 + \\ --config <PATH> Use custom config file
89 + \\ --version Print version information
90 + \\ --help Print this help message
91 + \\
92 + \\CONFIG:
93 + \\ Location: ~/.config/oxwm/config.lua
94 + \\ Edit the config file and use Mod+Shift+R to reload
95 + \\ No compilation needed - instant hot-reload!
96 + \\
97 + \\FIRST RUN:
98 + \\ Run 'oxwm --init' to create a config file
99 + \\ Or just start oxwm and it will create one automatically
100 + \\
101 + , .{});
102 +}
103 +
104 +fn init_config(allocator: std.mem.Allocator) void {
105 + const home = std.posix.getenv("HOME") orelse {
106 + std.debug.print("error: HOME environment variable not set\n", .{});
107 + return;
108 + };
109 +
110 + var path_buf: [512]u8 = undefined;
111 + const config_dir = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm", .{home}) catch {
112 + std.debug.print("error: path too long\n", .{});
113 + return;
114 + };
115 +
116 + std.fs.makeDirAbsolute(config_dir) catch |err| {
117 + if (err != error.PathAlreadyExists) {
118 + std.debug.print("error: could not create config directory: {}\n", .{err});
119 + return;
120 + }
121 + };
122 +
123 + var config_path_buf: [512]u8 = undefined;
124 + const config_path = std.fmt.bufPrint(&config_path_buf, "{s}/config.lua", .{config_dir}) catch {
125 + std.debug.print("error: path too long\n", .{});
126 + return;
127 + };
128 +
129 + const template = std.fs.cwd().readFileAlloc(allocator, "templates/config.lua", 64 * 1024) catch |err| {
130 + std.debug.print("error: could not read template (templates/config.lua): {}\n", .{err});
131 + std.debug.print("hint: run from the oxwm source directory, or copy templates/config.lua manually\n", .{});
132 + return;
133 + };
134 + defer allocator.free(template);
135 +
136 + const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
137 + std.debug.print("error: could not create config file: {}\n", .{err});
138 + return;
139 + };
140 + defer file.close();
141 +
142 + _ = file.writeAll(template) catch |err| {
143 + std.debug.print("error: could not write config file: {}\n", .{err});
144 + return;
145 + };
146 +
147 + std.debug.print("Config created at {s}\n", .{config_path});
148 + std.debug.print("Edit the file and reload with Mod+Shift+R\n", .{});
149 + std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
150 +}
151 +
70 pub fn main() !void { 152 pub fn main() !void {
71 const allocator = gpa.allocator(); 153 const allocator = gpa.allocator();
72 defer _ = gpa.deinit(); 154 defer _ = gpa.deinit();
@@ -80,7 +162,13 @@ pub fn main() !void {
80 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) { 162 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
81 config_path = args.next(); 163 config_path = args.next();
82 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { 164 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
83 - std.debug.print("usage: oxwm [-c config.lua]\n", .{}); 165 + print_help();
166 + return;
167 + } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
168 + std.debug.print("oxwm 0.1.0\n", .{});
169 + return;
170 + } else if (std.mem.eql(u8, arg, "--init")) {
171 + init_config(allocator);
84 return; 172 return;
85 } 173 }
86 } 174 }
@@ -140,6 +228,7 @@ pub fn main() !void {
140 228
141 setup_monitors(&display); 229 setup_monitors(&display);
142 setup_bars(allocator, &display); 230 setup_bars(allocator, &display);
231 + setup_overlay(allocator, &display);
143 grab_keybinds(&display); 232 grab_keybinds(&display);
144 scan_existing_windows(&display); 233 scan_existing_windows(&display);
145 234
@@ -222,6 +311,10 @@ fn setup_bars(allocator: std.mem.Allocator, display: *Display) void {
222 } 311 }
223 } 312 }
224 313
314 +fn setup_overlay(allocator: std.mem.Allocator, display: *Display) void {
315 + keybind_overlay = overlay_mod.Keybind_Overlay.init(display.handle, display.screen, display.root, config.font, allocator);
316 +}
317 +
225 fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block { 318 fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
226 return switch (cfg.block_type) { 319 return switch (cfg.block_type) {
227 .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline), 320 .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
@@ -335,41 +428,60 @@ fn apply_config_values() void {
335 tags = config.tags; 428 tags = config.tags;
336 } 429 }
337 430
431 +fn make_keybind(mod: u32, key: u64, action: config_mod.Action) config_mod.Keybind {
432 + var kb: config_mod.Keybind = .{ .action = action };
433 + kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
434 + kb.key_count = 1;
435 + return kb;
436 +}
437 +
438 +fn make_keybind_int(mod: u32, key: u64, action: config_mod.Action, int_arg: i32) config_mod.Keybind {
439 + var kb = make_keybind(mod, key, action);
440 + kb.int_arg = int_arg;
441 + return kb;
442 +}
443 +
444 +fn make_keybind_str(mod: u32, key: u64, action: config_mod.Action, str_arg: []const u8) config_mod.Keybind {
445 + var kb = make_keybind(mod, key, action);
446 + kb.str_arg = str_arg;
447 + return kb;
448 +}
449 +
338 fn setup_default_keybinds() void { 450 fn setup_default_keybinds() void {
339 const mod_key: u32 = 1 << 6; 451 const mod_key: u32 = 1 << 6;
340 const shift_key: u32 = 1 << 0; 452 const shift_key: u32 = 1 << 0;
341 const control_key: u32 = 1 << 2; 453 const control_key: u32 = 1 << 2;
342 454
343 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0xff0d, .action = .spawn_terminal }) catch {}; 455 + config.add_keybind(make_keybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
344 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'd', .action = .spawn, .str_arg = "rofi -show drun" }) catch {}; 456 + config.add_keybind(make_keybind_str(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
345 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 's', .action = .spawn, .str_arg = "maim -s | xclip -selection clipboard -t image/png" }) catch {}; 457 + config.add_keybind(make_keybind_str(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
346 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'q', .action = .kill_client }) catch {}; 458 + config.add_keybind(make_keybind(mod_key, 'q', .kill_client)) catch {};
347 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'q', .action = .quit }) catch {}; 459 + config.add_keybind(make_keybind(mod_key | shift_key, 'q', .quit)) catch {};
348 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'r', .action = .reload_config }) catch {}; 460 + config.add_keybind(make_keybind(mod_key | shift_key, 'r', .reload_config)) catch {};
349 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'j', .action = .focus_next }) catch {}; 461 + config.add_keybind(make_keybind(mod_key, 'j', .focus_next)) catch {};
350 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'k', .action = .focus_prev }) catch {}; 462 + config.add_keybind(make_keybind(mod_key, 'k', .focus_prev)) catch {};
351 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'j', .action = .move_next }) catch {}; 463 + config.add_keybind(make_keybind(mod_key | shift_key, 'j', .move_next)) catch {};
352 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'k', .action = .move_prev }) catch {}; 464 + config.add_keybind(make_keybind(mod_key | shift_key, 'k', .move_prev)) catch {};
353 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'h', .action = .resize_master, .int_arg = -50 }) catch {}; 465 + config.add_keybind(make_keybind_int(mod_key, 'h', .resize_master, -50)) catch {};
354 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'l', .action = .resize_master, .int_arg = 50 }) catch {}; 466 + config.add_keybind(make_keybind_int(mod_key, 'l', .resize_master, 50)) catch {};
355 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'i', .action = .inc_master }) catch {}; 467 + config.add_keybind(make_keybind(mod_key, 'i', .inc_master)) catch {};
356 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'p', .action = .dec_master }) catch {}; 468 + config.add_keybind(make_keybind(mod_key, 'p', .dec_master)) catch {};
357 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'a', .action = .toggle_gaps }) catch {}; 469 + config.add_keybind(make_keybind(mod_key, 'a', .toggle_gaps)) catch {};
358 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'f', .action = .toggle_fullscreen }) catch {}; 470 + config.add_keybind(make_keybind(mod_key, 'f', .toggle_fullscreen)) catch {};
359 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x0020, .action = .toggle_floating }) catch {}; 471 + config.add_keybind(make_keybind(mod_key, 0x0020, .toggle_floating)) catch {};
360 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'n', .action = .cycle_layout }) catch {}; 472 + config.add_keybind(make_keybind(mod_key, 'n', .cycle_layout)) catch {};
361 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x002c, .action = .focus_monitor, .int_arg = -1 }) catch {}; 473 + config.add_keybind(make_keybind_int(mod_key, 0x002c, .focus_monitor, -1)) catch {};
362 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x002e, .action = .focus_monitor, .int_arg = 1 }) catch {}; 474 + config.add_keybind(make_keybind_int(mod_key, 0x002e, .focus_monitor, 1)) catch {};
363 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 0x002c, .action = .send_to_monitor, .int_arg = -1 }) catch {}; 475 + config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
364 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 0x002e, .action = .send_to_monitor, .int_arg = 1 }) catch {}; 476 + config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
365 477
366 var tag_index: i32 = 0; 478 var tag_index: i32 = 0;
367 while (tag_index < 9) : (tag_index += 1) { 479 while (tag_index < 9) : (tag_index += 1) {
368 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index)); 480 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
369 - config.add_keybind(.{ .mod_mask = mod_key, .keysym = keysym, .action = .view_tag, .int_arg = tag_index }) catch {}; 481 + config.add_keybind(make_keybind_int(mod_key, keysym, .view_tag, tag_index)) catch {};
370 - config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = keysym, .action = .move_to_tag, .int_arg = tag_index }) catch {}; 482 + config.add_keybind(make_keybind_int(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
371 - config.add_keybind(.{ .mod_mask = mod_key | control_key, .keysym = keysym, .action = .toggle_view_tag, .int_arg = tag_index }) catch {}; 483 + config.add_keybind(make_keybind_int(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
372 - config.add_keybind(.{ .mod_mask = mod_key | control_key | shift_key, .keysym = keysym, .action = .toggle_tag, .int_arg = tag_index }) catch {}; 484 + config.add_keybind(make_keybind_int(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
373 } 485 }
374 } 486 }
375 487
@@ -380,13 +492,15 @@ fn grab_keybinds(display: *Display) void {
380 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root); 492 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
381 493
382 for (config.keybinds.items) |keybind| { 494 for (config.keybinds.items) |keybind| {
383 - const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(keybind.keysym)); 495 + if (keybind.key_count == 0) continue;
496 + const first_key = keybind.keys[0];
497 + const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(first_key.keysym));
384 if (keycode != 0) { 498 if (keycode != 0) {
385 for (modifiers) |modifier| { 499 for (modifiers) |modifier| {
386 _ = xlib.XGrabKey( 500 _ = xlib.XGrabKey(
387 display.handle, 501 display.handle,
388 keycode, 502 keycode,
389 - keybind.mod_mask | modifier, 503 + first_key.mod_mask | modifier,
390 display.root, 504 display.root,
391 xlib.True, 505 xlib.True,
392 xlib.GrabModeAsync, 506 xlib.GrabModeAsync,
@@ -708,15 +822,94 @@ fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEve
708 _ = xlib.XSync(display.handle, xlib.False); 822 _ = xlib.XSync(display.handle, xlib.False);
709 } 823 }
710 824
825 +fn reset_chord_state() void {
826 + chord_index = 0;
827 + chord_keys = [_]config_mod.Key_Press{.{}} ** 4;
828 + chord_timestamp = 0;
829 + if (keyboard_grabbed) {
830 + if (display_global) |dsp| {
831 + _ = xlib.XUngrabKeyboard(dsp.handle, xlib.CurrentTime);
832 + }
833 + keyboard_grabbed = false;
834 + }
835 +}
836 +
711 fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void { 837 fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
712 const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0); 838 const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0);
839 +
840 + if (keybind_overlay) |overlay| {
841 + if (overlay.handle_key(keysym)) {
842 + return;
843 + }
844 + }
845 +
713 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask); 846 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
847 + const current_time = std.time.milliTimestamp();
848 +
849 + if (chord_index > 0 and (current_time - chord_timestamp) > chord_timeout_ms) {
850 + reset_chord_state();
851 + }
852 +
853 + chord_keys[chord_index] = .{ .mod_mask = clean_state, .keysym = keysym };
854 + chord_index += 1;
855 + chord_timestamp = current_time;
714 856
715 for (config.keybinds.items) |keybind| { 857 for (config.keybinds.items) |keybind| {
716 - if (keysym == keybind.keysym and clean_state == keybind.mod_mask) { 858 + if (keybind.key_count == 0) continue;
717 - execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg); 859 +
718 - return; 860 + if (keybind.key_count == chord_index) {
861 + var matches = true;
862 + var i: u8 = 0;
863 + while (i < keybind.key_count) : (i += 1) {
864 + if (chord_keys[i].keysym != keybind.keys[i].keysym or
865 + chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
866 + {
867 + matches = false;
868 + break;
869 + }
870 + }
871 + if (matches) {
872 + execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg);
873 + reset_chord_state();
874 + return;
875 + }
876 + }
877 + }
878 +
879 + var has_partial_match = false;
880 + for (config.keybinds.items) |keybind| {
881 + if (keybind.key_count > chord_index) {
882 + var matches = true;
883 + var i: u8 = 0;
884 + while (i < chord_index) : (i += 1) {
885 + if (chord_keys[i].keysym != keybind.keys[i].keysym or
886 + chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
887 + {
888 + matches = false;
889 + break;
890 + }
891 + }
892 + if (matches) {
893 + has_partial_match = true;
894 + break;
895 + }
896 + }
897 + }
898 +
899 + if (has_partial_match and !keyboard_grabbed) {
900 + const grab_result = xlib.XGrabKeyboard(
901 + display.handle,
902 + display.root,
903 + xlib.True,
904 + xlib.GrabModeAsync,
905 + xlib.GrabModeAsync,
906 + xlib.CurrentTime,
907 + );
908 + if (grab_result == xlib.GrabSuccess) {
909 + keyboard_grabbed = true;
719 } 910 }
911 + } else if (!has_partial_match) {
912 + reset_chord_state();
720 } 913 }
721 } 914 }
722 915
@@ -734,8 +927,15 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st
734 running = false; 927 running = false;
735 }, 928 },
736 .reload_config => reload_config(display), 929 .reload_config => reload_config(display),
737 - .restart => {}, 930 + .restart => reload_config(display),
738 - .show_keybinds => {}, 931 + .show_keybinds => {
932 + if (keybind_overlay) |overlay| {
933 + const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors;
934 + if (mon) |m| {
935 + overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h);
936 + }
937 + }
938 + },
739 .focus_next => focusstack(display, 1), 939 .focus_next => focusstack(display, 1),
740 .focus_prev => focusstack(display, -1), 940 .focus_prev => focusstack(display, -1),
741 .move_next => movestack(display, 1), 941 .move_next => movestack(display, 1),
@@ -748,8 +948,8 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st
748 .toggle_gaps => toggle_gaps(), 948 .toggle_gaps => toggle_gaps(),
749 .cycle_layout => cycle_layout(), 949 .cycle_layout => cycle_layout(),
750 .set_layout => set_layout(str_arg), 950 .set_layout => set_layout(str_arg),
751 - .set_layout_tiling => {}, 951 + .set_layout_tiling => set_layout_index(0),
752 - .set_layout_floating => {}, 952 + .set_layout_floating => set_layout_index(2),
753 .view_tag => { 953 .view_tag => {
754 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); 954 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
755 view(display, tag_mask); 955 view(display, tag_mask);
@@ -1323,6 +1523,20 @@ fn set_layout(layout_name: ?[]const u8) void {
1323 } 1523 }
1324 } 1524 }
1325 1525
1526 +fn set_layout_index(index: u32) void {
1527 + const monitor = monitor_mod.selected_monitor orelse return;
1528 + monitor.sel_lt = index;
1529 + monitor.pertag.sellts[monitor.pertag.curtag] = index;
1530 + if (index != 3) {
1531 + monitor.scroll_offset = 0;
1532 + }
1533 + arrange(monitor);
1534 + bar_mod.invalidate_bars();
1535 + if (monitor.lt[monitor.sel_lt]) |layout| {
1536 + std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
1537 + }
1538 +}
1539 +
1326 fn focusmon(display: *Display, direction: i32) void { 1540 fn focusmon(display: *Display, direction: i32) void {
1327 const selmon = monitor_mod.selected_monitor orelse return; 1541 const selmon = monitor_mod.selected_monitor orelse return;
1328 const target = monitor_mod.dir_to_monitor(direction) orelse return; 1542 const target = monitor_mod.dir_to_monitor(direction) orelse return;
+371 −0 src/overlay.zig new file mode 100644
@@ -0,0 +1,371 @@
1 +const std = @import("std");
2 +const xlib = @import("x11/xlib.zig");
3 +const config_mod = @import("config/config.zig");
4 +
5 +const padding: i32 = 32;
6 +const line_spacing: i32 = 12;
7 +const key_action_spacing: i32 = 32;
8 +const border_width: i32 = 4;
9 +const border_color: c_ulong = 0x7fccff;
10 +const bg_color: c_ulong = 0x1a1a1a;
11 +const fg_color: c_ulong = 0xffffff;
12 +const key_bg_color: c_ulong = 0x2a2a2a;
13 +
14 +const max_lines: usize = 12;
15 +
16 +pub const Keybind_Overlay = struct {
17 + window: xlib.Window = 0,
18 + pixmap: xlib.Pixmap = 0,
19 + gc: xlib.GC = null,
20 + xft_draw: ?*xlib.XftDraw = null,
21 + font: ?*xlib.XftFont = null,
22 + font_height: i32 = 0,
23 + width: i32 = 0,
24 + height: i32 = 0,
25 + visible: bool = false,
26 + display: ?*xlib.Display = null,
27 + root: xlib.Window = 0,
28 + screen: c_int = 0,
29 +
30 + key_bufs: [max_lines][64]u8 = undefined,
31 + key_lens: [max_lines]usize = undefined,
32 + descs: [max_lines][]const u8 = undefined,
33 + line_count: usize = 0,
34 +
35 + pub fn init(display: *xlib.Display, screen: c_int, root: xlib.Window, font_name: []const u8, allocator: std.mem.Allocator) ?*Keybind_Overlay {
36 + const overlay = allocator.create(Keybind_Overlay) catch return null;
37 +
38 + const font_name_z = allocator.dupeZ(u8, font_name) catch {
39 + allocator.destroy(overlay);
40 + return null;
41 + };
42 + defer allocator.free(font_name_z);
43 +
44 + const font = xlib.XftFontOpenName(display, screen, font_name_z);
45 + if (font == null) {
46 + allocator.destroy(overlay);
47 + return null;
48 + }
49 +
50 + const font_height = font.*.ascent + font.*.descent;
51 +
52 + overlay.* = .{
53 + .display = display,
54 + .root = root,
55 + .font = font,
56 + .font_height = font_height,
57 + .screen = screen,
58 + };
59 +
60 + return overlay;
61 + }
62 +
63 + pub fn deinit(self: *Keybind_Overlay, allocator: std.mem.Allocator) void {
64 + if (self.display) |display| {
65 + self.destroy_window(display);
66 + if (self.font) |font| {
67 + xlib.XftFontClose(display, font);
68 + }
69 + }
70 + allocator.destroy(self);
71 + }
72 +
73 + fn destroy_window(self: *Keybind_Overlay, display: *xlib.Display) void {
74 + if (self.xft_draw) |xft_draw| {
75 + xlib.XftDrawDestroy(xft_draw);
76 + self.xft_draw = null;
77 + }
78 + if (self.gc) |gc| {
79 + _ = xlib.XFreeGC(display, gc);
80 + self.gc = null;
81 + }
82 + if (self.pixmap != 0) {
83 + _ = xlib.XFreePixmap(display, self.pixmap);
84 + self.pixmap = 0;
85 + }
86 + if (self.window != 0) {
87 + _ = xlib.c.XDestroyWindow(display, self.window);
88 + self.window = 0;
89 + }
90 + }
91 +
92 + pub fn toggle(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void {
93 + if (self.visible) {
94 + self.hide();
95 + } else {
96 + self.show(mon_x, mon_y, mon_w, mon_h);
97 + }
98 + }
99 +
100 + pub fn show(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void {
101 + const display = self.display orelse return;
102 + const cfg = config_mod.get_config() orelse return;
103 +
104 + self.collect_keybinds(cfg);
105 + if (self.line_count == 0) return;
106 +
107 + var max_key_width: i32 = 0;
108 + var max_desc_width: i32 = 0;
109 +
110 + for (0..self.line_count) |i| {
111 + const key_slice = self.key_bufs[i][0..self.key_lens[i]];
112 + const key_w = self.text_width(display, key_slice);
113 + const desc_w = self.text_width(display, self.descs[i]);
114 + if (key_w > max_key_width) max_key_width = key_w;
115 + if (desc_w > max_desc_width) max_desc_width = desc_w;
116 + }
117 +
118 + const title = "Keybindings";
119 + const title_width = self.text_width(display, title);
120 +
121 + const content_width = max_key_width + key_action_spacing + max_desc_width;
122 + const min_width = @max(title_width, content_width);
123 +
124 + self.width = min_width + padding * 2;
125 + const line_height = self.font_height + line_spacing;
126 + const title_height = self.font_height + 20;
127 + self.height = title_height + @as(i32, @intCast(self.line_count)) * line_height + padding * 2;
128 +
129 + self.destroy_window(display);
130 +
131 + const x: i32 = mon_x + @divTrunc(mon_w - self.width, 2);
132 + const y: i32 = mon_y + @divTrunc(mon_h - self.height, 2);
133 +
134 + const visual = xlib.XDefaultVisual(display, self.screen);
135 + const colormap = xlib.XDefaultColormap(display, self.screen);
136 + const depth = xlib.XDefaultDepth(display, self.screen);
137 +
138 + self.window = xlib.c.XCreateSimpleWindow(
139 + display,
140 + self.root,
141 + x,
142 + y,
143 + @intCast(self.width),
144 + @intCast(self.height),
145 + @intCast(border_width),
146 + border_color,
147 + bg_color,
148 + );
149 +
150 + var attrs: xlib.c.XSetWindowAttributes = undefined;
151 + attrs.override_redirect = xlib.True;
152 + attrs.event_mask = xlib.c.ExposureMask | xlib.c.KeyPressMask | xlib.c.ButtonPressMask;
153 + _ = xlib.c.XChangeWindowAttributes(display, self.window, xlib.c.CWOverrideRedirect | xlib.c.CWEventMask, &attrs);
154 +
155 + self.pixmap = xlib.XCreatePixmap(display, self.window, @intCast(self.width), @intCast(self.height), @intCast(depth));
156 + self.gc = xlib.XCreateGC(display, self.pixmap, 0, null);
157 + self.xft_draw = xlib.XftDrawCreate(display, self.pixmap, visual, colormap);
158 +
159 + _ = xlib.XMapWindow(display, self.window);
160 + _ = xlib.XRaiseWindow(display, self.window);
161 +
162 + self.draw(display, max_key_width, title);
163 +
164 + _ = xlib.XGrabKeyboard(display, self.window, xlib.True, xlib.GrabModeAsync, xlib.GrabModeAsync, xlib.CurrentTime);
165 +
166 + _ = xlib.XSync(display, xlib.False);
167 +
168 + self.visible = true;
169 + }
170 +
171 + pub fn hide(self: *Keybind_Overlay) void {
172 + if (!self.visible) return;
173 + if (self.display) |display| {
174 + _ = xlib.XUngrabKeyboard(display, xlib.CurrentTime);
175 + if (self.window != 0) {
176 + _ = xlib.c.XUnmapWindow(display, self.window);
177 + }
178 + }
179 + self.visible = false;
180 + }
181 +
182 + pub fn handle_key(self: *Keybind_Overlay, keysym: u64) bool {
183 + if (!self.visible) return false;
184 + if (keysym == 0xff1b or keysym == 'q' or keysym == 'Q') {
185 + self.hide();
186 + return true;
187 + }
188 + return false;
189 + }
190 +
191 + pub fn is_overlay_window(self: *Keybind_Overlay, win: xlib.Window) bool {
192 + return self.visible and self.window != 0 and self.window == win;
193 + }
194 +
195 + fn draw(self: *Keybind_Overlay, display: *xlib.Display, max_key_width: i32, title: []const u8) void {
196 + self.fill_rect(display, 0, 0, self.width, self.height, bg_color);
197 +
198 + const title_x = @divTrunc(self.width - self.text_width(display, title), 2);
199 + const title_y = padding + self.font.?.*.ascent;
200 + self.draw_text(display, title_x, title_y, title, fg_color);
201 +
202 + const line_height = self.font_height + line_spacing;
203 + var y = padding + self.font_height + 20 + self.font.?.*.ascent;
204 +
205 + for (0..self.line_count) |i| {
206 + const key_slice = self.key_bufs[i][0..self.key_lens[i]];
207 + const key_w = self.text_width(display, key_slice);
208 + self.fill_rect(display, padding - 4, y - self.font.?.*.ascent - 2, key_w + 8, self.font_height + 4, key_bg_color);
209 + self.draw_text(display, padding, y, key_slice, fg_color);
210 +
211 + const desc_x = padding + max_key_width + key_action_spacing;
212 + self.draw_text(display, desc_x, y, self.descs[i], fg_color);
213 +
214 + y += line_height;
215 + }
216 +
217 + _ = xlib.c.XCopyArea(display, self.pixmap, self.window, self.gc, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0);
218 + _ = xlib.c.XFlush(display);
219 + }
220 +
221 + fn fill_rect(self: *Keybind_Overlay, display: *xlib.Display, x: i32, y: i32, w: i32, h: i32, color: c_ulong) void {
222 + _ = xlib.XSetForeground(display, self.gc, color);
223 + _ = xlib.XFillRectangle(display, self.pixmap, self.gc, x, y, @intCast(w), @intCast(h));
224 + }
225 +
226 + fn draw_text(self: *Keybind_Overlay, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
227 + if (self.xft_draw == null or self.font == null) return;
228 + if (text.len == 0) return;
229 +
230 + var xft_color: xlib.XftColor = undefined;
231 + var render_color: xlib.XRenderColor = undefined;
232 + render_color.red = @intCast((color >> 16 & 0xff) * 257);
233 + render_color.green = @intCast((color >> 8 & 0xff) * 257);
234 + render_color.blue = @intCast((color & 0xff) * 257);
235 + render_color.alpha = 0xffff;
236 +
237 + const visual = xlib.XDefaultVisual(display, self.screen);
238 + const colormap = xlib.XDefaultColormap(display, self.screen);
239 +
240 + _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
241 + xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
242 + xlib.XftColorFree(display, visual, colormap, &xft_color);
243 + }
244 +
245 + fn text_width(self: *Keybind_Overlay, display: *xlib.Display, text: []const u8) i32 {
246 + if (self.font == null or text.len == 0) return 0;
247 + var extents: xlib.XGlyphInfo = undefined;
248 + xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
249 + return extents.xOff;
250 + }
251 +
252 + fn collect_keybinds(self: *Keybind_Overlay, cfg: *config_mod.Config) void {
253 + const priority_actions = [_]config_mod.Action{
254 + .show_keybinds,
255 + .quit,
256 + .reload_config,
257 + .kill_client,
258 + .spawn_terminal,
259 + .toggle_fullscreen,
260 + .toggle_floating,
261 + .cycle_layout,
262 + .focus_next,
263 + .focus_prev,
264 + .view_tag,
265 + .move_to_tag,
266 + };
267 +
268 + self.line_count = 0;
269 +
270 + for (priority_actions) |action| {
271 + if (self.line_count >= max_lines) break;
272 +
273 + for (cfg.keybinds.items) |kb| {
274 + if (kb.action == action and kb.key_count > 0) {
275 + self.format_key_to_buf(self.line_count, &kb.keys[0]);
276 + self.descs[self.line_count] = action_desc(action);
277 + self.line_count += 1;
278 + break;
279 + }
280 + }
281 + }
282 + }
283 +
284 + fn format_key_to_buf(self: *Keybind_Overlay, idx: usize, key: *const config_mod.Key_Press) void {
285 + var len: usize = 0;
286 + var buf = &self.key_bufs[idx];
287 +
288 + if (key.mod_mask & (1 << 6) != 0) {
289 + const s = "Mod + ";
290 + @memcpy(buf[len .. len + s.len], s);
291 + len += s.len;
292 + }
293 + if (key.mod_mask & (1 << 0) != 0) {
294 + const s = "Shift + ";
295 + @memcpy(buf[len .. len + s.len], s);
296 + len += s.len;
297 + }
298 + if (key.mod_mask & (1 << 2) != 0) {
299 + const s = "Ctrl + ";
300 + @memcpy(buf[len .. len + s.len], s);
301 + len += s.len;
302 + }
303 +
304 + const key_name = keysym_to_name(key.keysym);
305 + if (len + key_name.len < buf.len) {
306 + @memcpy(buf[len .. len + key_name.len], key_name);
307 + len += key_name.len;
308 + }
309 +
310 + self.key_lens[idx] = len;
311 + }
312 +
313 + fn keysym_to_name(keysym: u64) []const u8 {
314 + return switch (keysym) {
315 + 0xff0d => "Return",
316 + 0x0020 => "Space",
317 + 0xff1b => "Escape",
318 + 0xff08 => "BackSpace",
319 + 0xff09 => "Tab",
320 + 0xffbe => "F1",
321 + 0xffbf => "F2",
322 + 0xffc0 => "F3",
323 + 0xffc1 => "F4",
324 + 0xffc2 => "F5",
325 + 0xffc3 => "F6",
326 + 0xffc4 => "F7",
327 + 0xffc5 => "F8",
328 + 0xffc6 => "F9",
329 + 0xffc7 => "F10",
330 + 0xffc8 => "F11",
331 + 0xffc9 => "F12",
332 + 0xff51 => "Left",
333 + 0xff52 => "Up",
334 + 0xff53 => "Right",
335 + 0xff54 => "Down",
336 + 0x002c => ",",
337 + 0x002e => ".",
338 + 0x002f => "/",
339 + 'a'...'z' => |c| &[_]u8{@intCast(c - 32)},
340 + 'A'...'Z' => |c| &[_]u8{@intCast(c)},
341 + '0'...'9' => |c| &[_]u8{@intCast(c)},
342 + else => "?",
343 + };
344 + }
345 +
346 + fn action_desc(action: config_mod.Action) []const u8 {
347 + return switch (action) {
348 + .show_keybinds => "Show Keybinds",
349 + .quit => "Quit WM",
350 + .reload_config => "Reload Config",
351 + .restart => "Restart WM",
352 + .kill_client => "Close Window",
353 + .spawn_terminal => "Open Terminal",
354 + .spawn => "Launch Program",
355 + .toggle_fullscreen => "Toggle Fullscreen",
356 + .toggle_floating => "Toggle Floating",
357 + .toggle_gaps => "Toggle Gaps",
358 + .cycle_layout => "Cycle Layout",
359 + .set_layout => "Set Layout",
360 + .focus_next => "Focus Next",
361 + .focus_prev => "Focus Previous",
362 + .move_next => "Move Next",
363 + .move_prev => "Move Previous",
364 + .view_tag => "View Tag",
365 + .move_to_tag => "Move to Tag",
366 + .focus_monitor => "Focus Monitor",
367 + .send_to_monitor => "Send to Monitor",
368 + else => "Action",
369 + };
370 + }
371 +};
+2 −0 src/x11/xlib.zig
@@ -252,6 +252,8 @@ pub const XGrabServer = c.XGrabServer;
252 pub const XUngrabServer = c.XUngrabServer; 252 pub const XUngrabServer = c.XUngrabServer;
253 pub const XUngrabButton = c.XUngrabButton; 253 pub const XUngrabButton = c.XUngrabButton;
254 pub const XUngrabKey = c.XUngrabKey; 254 pub const XUngrabKey = c.XUngrabKey;
255 +pub const XGrabKeyboard = c.XGrabKeyboard;
256 +pub const XUngrabKeyboard = c.XUngrabKeyboard;
255 pub const AnyKey = c.AnyKey; 257 pub const AnyKey = c.AnyKey;
256 pub const AnyModifier = c.AnyModifier; 258 pub const AnyModifier = c.AnyModifier;
257 259
+18 −0 tests/lua_config_tests.zig new file mode 100644
@@ -0,0 +1,18 @@
1 +const std = @import("std");
2 +const testing = std.testing;
3 +const lua = @import("lua");
4 +const Config = lua.config_mod.Config;
5 +
6 +test "test-config.lua loads without errors" {
7 + var cfg = Config.init(testing.allocator);
8 + defer cfg.deinit();
9 +
10 + const initialized = lua.init(&cfg);
11 + if (!initialized) {
12 + return error.LuaInitFailed;
13 + }
14 + defer lua.deinit();
15 +
16 + const loaded = lua.load_file("resources/test-config.lua");
17 + try testing.expect(loaded);
18 +}