diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/apprt/gtk/key.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/apprt/gtk/key.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/apprt/gtk/key.zig | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/apprt/gtk/key.zig b/vendor/libghostty-vt/src/apprt/gtk/key.zig index 5f717e14..ff2064d4 100644 --- a/vendor/libghostty-vt/src/apprt/gtk/key.zig +++ b/vendor/libghostty-vt/src/apprt/gtk/key.zig @@ -57,6 +57,16 @@ pub fn xdgShortcutFromTrigger( return slice[0 .. slice.len - 1 :0]; } +/// Returns the keysym (identical to a GDK keyval) for a trigger key, +/// or null if the trigger has no keysym representation. +pub fn keysymFromTrigger(trigger: input.Binding.Trigger) ?c_uint { + return switch (trigger.key) { + .physical => |k| keyvalFromKey(k), + .unicode => |cp| gdk.unicodeToKeyval(cp), + .catch_all => null, + }; +} + fn writeTriggerKey( writer: *std.Io.Writer, trigger: input.Binding.Trigger, @@ -217,6 +227,94 @@ pub fn keyFromKeyval(keyval: c_uint) ?input.Key { return null; } +/// Returns the logical key after applying any eligible XKB remapping. +/// +/// Modifier keyvals that Ghostty doesn't represent must not fall back to the +/// physical key. XKB can turn any physical key into a modifier, and using the +/// original key in that case would encode an unrelated key press. +pub fn remapKey( + physical_key: input.Key, + keyval: c_uint, + is_modifier: bool, +) input.Key { + if (keyFromKeyval(keyval)) |remapped| { + if (physical_key.shouldBeRemappable() or remapped.shouldBeRemappable()) + return remapped; + } + + if (is_modifier or isModifierKeyval(keyval)) return .unidentified; + return physical_key; +} + +/// Returns whether a keyval has modifier semantics that GDK may not expose +/// through `gdk_key_event_is_modifier`. +/// +/// In particular, XKB can report ISO level and group modifiers as keyvals +/// such as `Mode_switch` while GDK reports that the event is not a modifier. +fn isModifierKeyval(keyval: c_uint) bool { + return switch (keyval) { + gdk.KEY_Shift_L, + gdk.KEY_Shift_R, + gdk.KEY_Shift_Lock, + gdk.KEY_Control_L, + gdk.KEY_Control_R, + gdk.KEY_Caps_Lock, + gdk.KEY_Meta_L, + gdk.KEY_Meta_R, + gdk.KEY_Alt_L, + gdk.KEY_Alt_R, + gdk.KEY_Super_L, + gdk.KEY_Super_R, + gdk.KEY_Hyper_L, + gdk.KEY_Hyper_R, + gdk.KEY_Num_Lock, + gdk.KEY_Mode_switch, + gdk.KEY_ISO_Lock, + gdk.KEY_ISO_Level2_Latch, + gdk.KEY_ISO_Level3_Shift, + gdk.KEY_ISO_Level3_Latch, + gdk.KEY_ISO_Level3_Lock, + gdk.KEY_ISO_Level5_Shift, + gdk.KEY_ISO_Level5_Latch, + gdk.KEY_ISO_Level5_Lock, + gdk.KEY_ISO_Group_Latch, + gdk.KEY_ISO_Group_Lock, + => true, + + else => false, + }; +} + +test "remap key" { + const testing = std.testing; + + // XKB remaps between non-writing-system keys are honored. + try testing.expectEqual( + input.Key.escape, + remapKey(.caps_lock, gdk.KEY_Escape, false), + ); + + // Writing-system keys retain their physical identity. + try testing.expectEqual( + input.Key.key_a, + remapKey(.key_a, gdk.KEY_b, false), + ); + + // GDK doesn't identify Mode_switch as a modifier, but it must not fall + // back to the physical key. + try testing.expectEqual( + input.Key.unidentified, + remapKey(.delete, gdk.KEY_Mode_switch, false), + ); + + // GDK's modifier flag also covers custom modifier actions whose keyval + // Ghostty doesn't represent. + try testing.expectEqual( + input.Key.unidentified, + remapKey(.delete, gdk.KEY_VoidSymbol, true), + ); +} + /// Returns a keyval from an input key or null if we don't have a mapping. pub fn keyvalFromKey(key: input.Key) ?c_uint { switch (key) { @@ -327,6 +425,28 @@ test "xdgShortcutFromTrigger" { })).?); } +test "keysymFromTrigger" { + const testing = std.testing; + + // Unicode keys use the keysym numbering (latin-1 maps directly). + try testing.expectEqual(@as(?c_uint, 'q'), keysymFromTrigger(.{ + .mods = .{ .super = true }, + .key = .{ .unicode = 'q' }, + })); + + // Physical keys map through our keymap. + try testing.expectEqual(@as(?c_uint, gdk.KEY_a), keysymFromTrigger(.{ + .mods = .{}, + .key = .{ .physical = .key_a }, + })); + + // catch_all has no keysym representation. + try testing.expectEqual(@as(?c_uint, null), keysymFromTrigger(.{ + .mods = .{}, + .key = .catch_all, + })); +} + test "labelFromTrigger" { const testing = std.testing; |
