| @@ -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 |
|