Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions Terminal.Gui/App/MainLoop/MainLoopCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ private void BuildDriverIfPossible (IApplication? app)
return;
}

Logging.Trace ($"app: SetDefaultAttribute ({new Attribute (fg ?? new Color (255, 255, 255), bg ?? new Color (0, 0))})");
Logging.Trace ($"app: SetDefaultAttribute ({
new Attribute (fg ?? new Color (255, 255, 255), bg ?? new Color (0, 0))
})");

_driver.SetDefaultAttribute (new Attribute (fg ?? new Color (255, 255, 255), bg ?? new Color (0, 0)));
});
Expand All @@ -183,25 +185,34 @@ private void BuildDriverIfPossible (IApplication? app)
try
{
KittyKeyboardProtocolDetector kittyKeyboardDetector = new (_driver);

kittyKeyboardDetector.Detect (result =>
{
_driver.SetKittyKeyboardProtocol (result);
Trace.Lifecycle (app?.MainThreadId?.ToString (),
"KittyKeyboard",
$"Probe complete: Supported={result.IsSupported}, SupportedFlags={result.SupportedFlags}, EnabledFlags={result.EnabledFlags}");

if (!result.IsSupported || result.EnabledFlags <= 0 || _output is not AnsiOutput ansiOutput)
{
Trace.Lifecycle (app?.MainThreadId?.ToString (), "KittyKeyboard", "Kitty keyboard mode not enabled");
return;
}

ansiOutput.EnableKittyKeyboard (result.EnabledFlags);
_driver.SetKittyKeyboardEnabledFlags (ansiOutput.KittyKeyboardEnabledFlags);
Trace.Lifecycle (app?.MainThreadId?.ToString (),
"KittyKeyboard",
$"Enabled kitty keyboard flags {ansiOutput.KittyKeyboardEnabledFlags}");
});
{
_driver.SetKittyKeyboardProtocol (result);

Trace.Lifecycle (app?.MainThreadId?.ToString (),
"KittyKeyboard",
$"Probe complete: Supported={
result.IsSupported
}, SupportedFlags={
result.SupportedFlags
}, EnabledFlags={
result.EnabledFlags
}");

if (!result.IsSupported || result.EnabledFlags <= 0 || _output is not AnsiOutput ansiOutput)
{
Trace.Lifecycle (app?.MainThreadId?.ToString (), "KittyKeyboard", "Kitty keyboard mode not enabled");

return;
}

_driver.SetKittyKeyboardEnabledFlags (ansiOutput.KittyKeyboardEnabledFlags);

Trace.Lifecycle (app?.MainThreadId?.ToString (),
"KittyKeyboard",
$"Enabled kitty keyboard flags {ansiOutput.KittyKeyboardEnabledFlags}");
});
}
catch (Exception ex)
{
Expand Down Expand Up @@ -255,7 +266,7 @@ private void RunInput (IApplication? app)

if (_stopCalled)
{
Trace.Lifecycle (app?.MainThreadId.ToString (), "Init", $"Input loop exited cleanly");
Trace.Lifecycle (app?.MainThreadId.ToString (), "Init", "Input loop exited cleanly");
}
else
{
Expand Down
34 changes: 34 additions & 0 deletions Terminal.Gui/Drivers/AnsiDriver/AnsiInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class AnsiInput : InputImpl<char>, ITestableInput<char>
// Queue for storing injected input that will be returned by Peek/Read
private readonly ConcurrentQueue<char> _testInput = new ();

private char? _previousWindowsVTLastChar;
Comment thread
BDisp marked this conversation as resolved.
Outdated

private int _peekCallCount;

/// <summary>
Expand Down Expand Up @@ -205,6 +207,23 @@ public override IEnumerable<char> Read ()

string text = _windowsVTInput!.ConsoleInputEncoding.GetString (buffer, 0, bytesRead);

if (_enabledKittyKeyboardFlags != KittyKeyboardFlags.None
&& _previousWindowsVTLastChar is { } lastChar
&& text.Length > 0
&& text [0] == lastChar)
{
text = text [1..];
}

if (text.Length == 0)
{
_previousWindowsVTLastChar = null;

yield break;
}

_previousWindowsVTLastChar = text [^1];

//Trace.Lifecycle (nameof (AnsiInput), "Read", $"Read {bytesRead} bytes from Windows VT Input: {text}");

foreach (char ch in text)
Expand Down Expand Up @@ -358,6 +377,19 @@ private void FlushInput ()
}
}

private KittyKeyboardFlags _enabledKittyKeyboardFlags;

/// <summary>
/// Enables kitty keyboard progressive enhancement flags for the active terminal.
/// </summary>
/// <param name="enabledFlags">The kitty keyboard flags to enable.</param>
internal void EnableKittyKeyboard (KittyKeyboardFlags enabledFlags)
{
_enabledKittyKeyboardFlags = enabledFlags;

Trace.Lifecycle (nameof (AnsiOutput), "KittyKeyboard", $"Input enabled: {enabledFlags}");
}

// Will be called on the main loop thread.
/// <inheritdoc/>
public void InjectInput (char input) => _testInput.Enqueue (input);
Expand Down Expand Up @@ -397,4 +429,6 @@ public override void Dispose ()
// ignore exceptions during disposal
}
}


}
17 changes: 15 additions & 2 deletions Terminal.Gui/Drivers/AnsiHandling/KittyKeyboardPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Terminal.Gui.Drivers;
/// </summary>
public class KittyKeyboardPattern : AnsiKeyboardParserPattern
{
private readonly Regex _pattern = new (@"^\u001b\[(\d+)(?::(\d+))?(?::(\d+))?(?:;([^;u]*))?(?:;([^u]+))?u$");
private readonly Regex _pattern = new (@"^\u001b\[(\d+)(?::(\d*))?(?::(\d*))?(?:;([^;u]*))?(?:;([^u]+))?u$");

private readonly Dictionary<int, Key> _functionalKeyMap = new ()
{
Expand Down Expand Up @@ -53,7 +53,17 @@ public class KittyKeyboardPattern : AnsiKeyboardParserPattern
{ 57384, Key.F21 },
{ 57385, Key.F22 },
{ 57386, Key.F23 },
{ 57387, Key.F24 }
{ 57387, Key.F24 },
{ 57417, Key.CursorLeft },
{ 57418, Key.CursorRight },
{ 57419, Key.CursorUp },
{ 57420, Key.CursorDown },
{ 57421, Key.PageUp },
{ 57422, Key.PageDown },
{ 57423, Key.Home },
{ 57424, Key.End },
{ 57425, Key.InsertChar },
{ 57426, Key.Delete }
};

/// <inheritdoc/>
Expand Down Expand Up @@ -231,6 +241,9 @@ private static (Key Key, string ModifierField) NormalizeShiftedPrintableKey (Key
{ 57447, ModifierKey.RightShift },
{ 57448, ModifierKey.RightCtrl },
{ 57449, ModifierKey.RightAlt },
// 57453 = ISO_Level3_Shift (AltGr). Treat it as a dedicated modifier so
// standalone AltGr does not fall through as a printable Private Use Area rune.
{ 57453, ModifierKey.AltGr },
{ 57450, ModifierKey.RightSuper },
{ 57451, ModifierKey.RightHyper }

Expand Down
11 changes: 11 additions & 0 deletions Terminal.Gui/Drivers/DriverImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ public Attribute SetAttribute (Attribute newAttribute)
internal void SetKittyKeyboardEnabledFlags (KittyKeyboardFlags enabledFlags)
{
KittyKeyboardProtocol.EnabledFlags = enabledFlags;


if ((_componentFactory as AnsiComponentFactory)?.CreateOutput() is AnsiOutput { } output)
{
output.EnableKittyKeyboard (enabledFlags);
}

if ((_componentFactory as AnsiComponentFactory)?.CreateInput () is AnsiInput { } input)
{
input.EnableKittyKeyboard (enabledFlags);
}
}

/// <summary>Event fired when a key is pressed down.</summary>
Expand Down
3 changes: 3 additions & 0 deletions Terminal.Gui/Input/Keyboard/ModifierKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public enum ModifierKey
/// <summary>Right Alt.</summary>
RightAlt,

/// <summary>AltGr / ISO Level 3 Shift.</summary>
AltGr,

/// <summary>Super / Windows / Cmd key (side not distinguished).</summary>
Super,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ public void KittyPattern_RightAlt_Standalone ()
Assert.Equal (ModifierKey.RightAlt, key.ModifierKey);
}

[Fact]
public void KittyPattern_AltGr_Standalone ()
{
// ESC[57453u = AltGr / ISO_Level3_Shift
Key? key = _pattern.GetKey ("\u001b[57453u");

Assert.NotNull (key);
Assert.True (key.IsModifierOnly);
Assert.Equal (ModifierKey.AltGr, key.ModifierKey);
}

[Fact]
public void KittyPattern_CapsLock_Standalone ()
{
Expand Down Expand Up @@ -224,6 +235,57 @@ public void KittyPattern_ModifierKey_WithEventType_Release ()
Assert.Equal (KeyEventType.Release, key.EventType);
}

[Fact]
public void KittyPattern_AltGr_WithEventType_Release ()
{
// ESC[57453;1:3u = AltGr / ISO_Level3_Shift, event type 3 (release)
Key? key = _pattern.GetKey ("\u001b[57453;1:3u");

Assert.NotNull (key);
Assert.True (key.IsModifierOnly);
Assert.Equal (ModifierKey.AltGr, key.ModifierKey);
Assert.Equal (KeyEventType.Release, key.EventType);
}

[Fact]
public void KittyPattern_LeftAlt_WithCtrlModifier_PreservesBothStates ()
{
Key? key = _pattern.GetKey ("\u001b[57443;5u");

Assert.NotNull (key);
Assert.True (key.IsModifierOnly);
Assert.Equal (ModifierKey.LeftAlt, key.ModifierKey);
Assert.True (key.IsCtrl);
Assert.False (key.IsAlt);
Assert.Equal (KeyEventType.Press, key.EventType);
}

[Fact]
public void KittyPattern_LeftAlt_Release_WithCtrlAndAltModifiers_PreservesBothStates ()
{
Key? key = _pattern.GetKey ("\u001b[57443;7:3u");

Assert.NotNull (key);
Assert.True (key.IsModifierOnly);
Assert.Equal (ModifierKey.LeftAlt, key.ModifierKey);
Assert.True (key.IsCtrl);
Assert.True (key.IsAlt);
Assert.Equal (KeyEventType.Release, key.EventType);
}

[Fact]
public void KittyPattern_LeftCtrl_Release_WithCtrlModifier_PreservesState ()
{
Key? key = _pattern.GetKey ("\u001b[57442;5:3u");

Assert.NotNull (key);
Assert.True (key.IsModifierOnly);
Assert.Equal (ModifierKey.LeftCtrl, key.ModifierKey);
Assert.True (key.IsCtrl);
Assert.False (key.IsAlt);
Assert.Equal (KeyEventType.Release, key.EventType);
}

[Fact]
public void KittyPattern_NonModifierKey_IsNotModifierOnly ()
{
Expand Down Expand Up @@ -460,6 +522,58 @@ public void KittyPattern_AlternateKeys_FunctionalKey ()
Assert.Equal ((KeyCode)13, key.BaseLayoutKeyCode);
}

[Fact]
public void KittyPattern_EmptyFields_WithAssociatedText ()
{
// ESC[64::50;;64u = '@' with empty shifted field, base layout '2', empty modifiers, associated text '@'
Key? key = _pattern.GetKey ("\u001b[64::50;;64u");

Assert.NotNull (key);
Assert.Equal (new Key ('@'), key);
Assert.Equal (KeyCode.Null, key.ShiftedKeyCode);
Assert.Equal ((KeyCode)50, key.BaseLayoutKeyCode);
Assert.Equal ("@", key.AssociatedText);
}

[Fact]
public void KittyPattern_AltGr5_Press_ReturnsEuroGrapheme ()
{
// ESC[8364;1:1u = Euro symbol, no modifiers, press
Key? key = _pattern.GetKey ("\u001b[8364;1:1u");

Assert.NotNull (key);
Assert.Equal (KeyEventType.Press, key.EventType);
Assert.Equal ((KeyCode)8364, key.KeyCode);
Assert.Equal ("€", key.AsGrapheme);
Assert.Equal ("€", key.GetPrintableText ());
}

[Fact]
public void KittyPattern_AltGrE_EuroKey_Press_ReturnsEuroGrapheme ()
{
// ESC[8364;1:1u = AltGr+E on many layouts, which sends the euro codepoint 8364.
Key? key = _pattern.GetKey ("\u001b[8364;1:1u");

Assert.NotNull (key);
Assert.Equal (KeyEventType.Press, key.EventType);
Assert.Equal ((KeyCode)8364, key.KeyCode);
Assert.Equal ("€", key.AsGrapheme);
Assert.Equal ("€", key.GetPrintableText ());
}

[Fact]
public void KittyPattern_AltGrE_EuroKey_Release_ReturnsEuroGrapheme ()
{
// ESC[8364;1:3u = Euro symbol, no modifiers, release
Key? key = _pattern.GetKey ("\u001b[8364;1:3u");

Assert.NotNull (key);
Assert.Equal (KeyEventType.Release, key.EventType);
Assert.Equal ((KeyCode)8364, key.KeyCode);
Assert.Equal ("€", key.AsGrapheme);
Assert.Equal ("€", key.GetPrintableText ());
}

#endregion

#region Kitty Flags Validation
Expand Down
Loading
Loading