tony / oxwm

refactor: create `ChordState` type and remove chord state globals Browse files
emzywastaken committed 2026-02-20 parent 2141216 commit 78f29dc49b5b5a82ec6db7959e1540c1ca37e463
Showing 2 changed files with 86 additions and 46 deletions. UnifiedSplit
+69 −0 src/keyboard/chord.zig new file mode 100644
@@ -0,0 +1,69 @@
1 +const std = @import("std");
2 +const xlib = @import("../x11/xlib.zig");
3 +const config = @import("../config/config.zig");
4 +
5 +pub const max_chord_len: u8 = 4;
6 +/// How long (in milliseconds) the user has between key presses within
7 +/// a chord before the sequence is abandoned and state is reset.
8 +pub const timeout_ms: i64 = 1000;
9 +
10 +/// Tracks the in-progress key-chord sequence.
11 +///
12 +/// Owned by `WindowManager`. Call `update` on every key press; it returns
13 +/// whether the sequence is still live. Call `reset` to abandon the
14 +/// current sequence and release the keyboard grab if one is held.
15 +pub const ChordState = struct {
16 + keys: [max_chord_len]config.Key_Press = .{config.Key_Press{}} ** max_chord_len,
17 + index: u8 = 0,
18 + last_timestamp: i64 = 0,
19 + keyboard_grabbed: bool = false,
20 +
21 + /// Push a new key press onto the sequence and update the timestamp.
22 + ///
23 + /// Returns `false` if the sequence is already at maximum length, in which
24 + /// case the caller should call `reset` before retrying.
25 + pub fn push(self: *ChordState, key: config.Key_Press) bool {
26 + if (self.index >= max_chord_len) return false;
27 + self.keys[self.index] = key;
28 + self.index += 1;
29 + self.last_timestamp = std.time.milliTimestamp();
30 +
31 + return true;
32 + }
33 +
34 + /// Returns true if the sequence has timed out and should be reset.
35 + pub fn is_timed_out(self: *const ChordState) bool {
36 + if (self.index == 0) return false;
37 + return (std.time.milliTimestamp() - self.last_timestamp) >= timeout_ms;
38 + }
39 +
40 + /// Clears the sequence and releases the keyboard grab if one is held.
41 + ///
42 + /// `display` may be null only during early startup before the connection
43 + /// is open, in normal operation it should always be provided.
44 + pub fn reset(self: *ChordState, display: ?*xlib.Display) void {
45 + self.keys = .{config.Key_Press{}} ** max_chord_len;
46 + self.index = 0;
47 + self.last_timestamp = 0;
48 +
49 + if (self.keyboard_grabbed) {
50 + if (display) |dpy| {
51 + _ = xlib.XUngrabKeyboard(dpy, xlib.CurrentTime);
52 + }
53 + self.keyboard_grabbed = false;
54 + }
55 + }
56 +
57 + /// Try to grab the keyboard for exclusive input during a partial match.
58 + ///
59 + /// Sets `keyboard_grabbed` on success. Safe to call repeatedly,
60 + /// does nothing if already grabbed.
61 + pub fn grab_keyboard(self: *ChordState, display: *xlib.Display, root: xlib.Window) void {
62 + if (self.keyboard_grabbed) return;
63 +
64 + const result = xlib.XGrabKeyboard(display, root, xlib.True, xlib.GrabModeAsync, xlib.GrabModeAsync, xlib.CurrentTime);
65 + if (result == xlib.GrabSuccess) {
66 + self.keyboard_grabbed = true;
67 + }
68 + }
69 +};
+17 −46 src/main.zig
@@ -1,5 +1,6 @@
1 const std = @import("std"); 1 const std = @import("std");
2 const VERSION = "v0.11.2"; 2 const VERSION = "v0.11.2";
3 +const kchord = @import("keyboard/chord.zig");
3 const display_mod = @import("x11/display.zig"); 4 const display_mod = @import("x11/display.zig");
4 const events = @import("x11/events.zig"); 5 const events = @import("x11/events.zig");
5 const xlib = @import("x11/xlib.zig"); 6 const xlib = @import("x11/xlib.zig");
@@ -70,11 +71,7 @@ var config_path_global: ?[]const u8 = null;
70 var scroll_animation: animations.Scroll_Animation = .{}; 71 var scroll_animation: animations.Scroll_Animation = .{};
71 var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out }; 72 var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out };
72 73
73 -var chord_keys: [4]config_mod.Key_Press = [_]config_mod.Key_Press{.{}} ** 4; 74 +var chord = kchord.ChordState{};
74 -var chord_index: u8 = 0;
75 -var chord_timestamp: i64 = 0;
76 -const chord_timeout_ms: i64 = 1000;
77 -var keyboard_grabbed: bool = false;
78 75
79 var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null; 76 var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null;
80 77
@@ -900,16 +897,8 @@ fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEve
900 _ = xlib.XSync(display.handle, xlib.False); 897 _ = xlib.XSync(display.handle, xlib.False);
901 } 898 }
902 899
903 -fn reset_chord_state() void { 900 +fn reset_chord_state(display_handle: *xlib.Display) void {
904 - chord_index = 0; 901 + chord.reset(display_handle);
905 - chord_keys = [_]config_mod.Key_Press{.{}} ** 4;
906 - chord_timestamp = 0;
907 - if (keyboard_grabbed) {
908 - if (display_global) |dsp| {
909 - _ = xlib.XUngrabKeyboard(dsp.handle, xlib.CurrentTime);
910 - }
911 - keyboard_grabbed = false;
912 - }
913 } 902 }
914 903
915 fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void { 904 fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
@@ -924,31 +913,26 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
924 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask); 913 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
925 const current_time = std.time.milliTimestamp(); 914 const current_time = std.time.milliTimestamp();
926 915
927 - if (chord_index > 0 and (current_time - chord_timestamp) > chord_timeout_ms) { 916 + if (chord.index > 0 and (current_time - chord.last_timestamp) > kchord.timeout_ms) {
928 - reset_chord_state(); 917 + reset_chord_state(display.handle);
929 } 918 }
930 919
931 - chord_keys[chord_index] = .{ .mod_mask = clean_state, .keysym = keysym }; 920 + _ = chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
932 - chord_index += 1;
933 - chord_timestamp = current_time;
934 921
935 for (config.keybinds.items) |keybind| { 922 for (config.keybinds.items) |keybind| {
936 if (keybind.key_count == 0) continue; 923 if (keybind.key_count == 0) continue;
937 924
938 - if (keybind.key_count == chord_index) { 925 + if (keybind.key_count == chord.index) {
939 var matches = true; 926 var matches = true;
940 - var i: u8 = 0; 927 + for (0..keybind.key_count) |i| {
941 - while (i < keybind.key_count) : (i += 1) { 928 + if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
942 - if (chord_keys[i].keysym != keybind.keys[i].keysym or
943 - chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
944 - {
945 matches = false; 929 matches = false;
946 break; 930 break;
947 } 931 }
948 } 932 }
949 if (matches) { 933 if (matches) {
950 execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg); 934 execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg);
951 - reset_chord_state(); 935 + reset_chord_state(display.handle);
952 return; 936 return;
953 } 937 }
954 } 938 }
@@ -956,13 +940,10 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
956 940
957 var has_partial_match = false; 941 var has_partial_match = false;
958 for (config.keybinds.items) |keybind| { 942 for (config.keybinds.items) |keybind| {
959 - if (keybind.key_count > chord_index) { 943 + if (keybind.key_count > chord.index) {
960 var matches = true; 944 var matches = true;
961 - var i: u8 = 0; 945 + for (0..chord.index) |i| {
962 - while (i < chord_index) : (i += 1) { 946 + if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
963 - if (chord_keys[i].keysym != keybind.keys[i].keysym or
964 - chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
965 - {
966 matches = false; 947 matches = false;
967 break; 948 break;
968 } 949 }
@@ -974,20 +955,10 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
974 } 955 }
975 } 956 }
976 957
977 - if (has_partial_match and !keyboard_grabbed) { 958 + if (has_partial_match) {
978 - const grab_result = xlib.XGrabKeyboard( 959 + chord.grab_keyboard(display.handle, display.root);
979 - display.handle,
980 - display.root,
981 - xlib.True,
982 - xlib.GrabModeAsync,
983 - xlib.GrabModeAsync,
984 - xlib.CurrentTime,
985 - );
986 - if (grab_result == xlib.GrabSuccess) {
987 - keyboard_grabbed = true;
988 - }
989 } else if (!has_partial_match) { 960 } else if (!has_partial_match) {
990 - reset_chord_state(); 961 + reset_chord_state(display.handle);
991 } 962 }
992 } 963 }
993 964