Browse Source

Fix NetDriver and WindowsDriver.

BDisp 9 months ago
parent
commit
ee8d040407
2 changed files with 61 additions and 31 deletions
  1. 51 14
      Terminal.Gui/ConsoleDrivers/NetDriver.cs
  2. 10 17
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

+ 51 - 14
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -198,7 +198,7 @@ internal class NetEvents : IDisposable
         return null;
         return null;
     }
     }
 
 
-    private static ConsoleKeyInfo ReadConsoleKeyInfo (CancellationToken cancellationToken, bool intercept = true)
+    private ConsoleKeyInfo ReadConsoleKeyInfo (CancellationToken cancellationToken, bool intercept = true)
     {
     {
         // if there is a key available, return it without waiting
         // if there is a key available, return it without waiting
         //  (or dispatching work to the thread queue)
         //  (or dispatching work to the thread queue)
@@ -215,6 +215,30 @@ internal class NetEvents : IDisposable
             {
             {
                 return Console.ReadKey (intercept);
                 return Console.ReadKey (intercept);
             }
             }
+
+            if (EscSeqUtils.IncompleteCkInfos is null && EscSeqRequests is { Statuses.Count: > 0 })
+            {
+                if (_retries > 1)
+                {
+                    EscSeqRequests.Statuses.TryDequeue (out EscSeqReqStatus seqReqStatus);
+
+                    lock (seqReqStatus.AnsiRequest._responseLock)
+                    {
+                        seqReqStatus.AnsiRequest.Response = string.Empty;
+                        seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, string.Empty);
+                    }
+
+                    _retries = 0;
+                }
+                else
+                {
+                    _retries++;
+                }
+            }
+            else
+            {
+                _retries = 0;
+            }
         }
         }
 
 
         cancellationToken.ThrowIfCancellationRequested ();
         cancellationToken.ThrowIfCancellationRequested ();
@@ -223,6 +247,7 @@ internal class NetEvents : IDisposable
     }
     }
 
 
     internal bool _forceRead;
     internal bool _forceRead;
+    private int _retries;
 
 
     private void ProcessInputQueue ()
     private void ProcessInputQueue ()
     {
     {
@@ -230,7 +255,10 @@ internal class NetEvents : IDisposable
         {
         {
             try
             try
             {
             {
-                _waitForStart.Wait (_inputReadyCancellationTokenSource.Token);
+                if (!_forceRead)
+                {
+                    _waitForStart.Wait (_inputReadyCancellationTokenSource.Token);
+                }
             }
             }
             catch (OperationCanceledException)
             catch (OperationCanceledException)
             {
             {
@@ -258,6 +286,11 @@ internal class NetEvents : IDisposable
                         return;
                         return;
                     }
                     }
 
 
+                    if (EscSeqUtils.IncompleteCkInfos is { })
+                    {
+                        EscSeqUtils.InsertArray (EscSeqUtils.IncompleteCkInfos, _cki);
+                    }
+
                     if ((consoleKeyInfo.KeyChar == (char)KeyCode.Esc && !_isEscSeq)
                     if ((consoleKeyInfo.KeyChar == (char)KeyCode.Esc && !_isEscSeq)
                         || (consoleKeyInfo.KeyChar != (char)KeyCode.Esc && _isEscSeq))
                         || (consoleKeyInfo.KeyChar != (char)KeyCode.Esc && _isEscSeq))
                     {
                     {
@@ -277,7 +310,7 @@ internal class NetEvents : IDisposable
 
 
                         _isEscSeq = true;
                         _isEscSeq = true;
 
 
-                        if (consoleKeyInfo.KeyChar != Key.Esc && consoleKeyInfo.KeyChar <= Key.Space)
+                        if (_cki is { } && _cki [^1].KeyChar != Key.Esc && consoleKeyInfo.KeyChar != Key.Esc && consoleKeyInfo.KeyChar <= Key.Space)
                         {
                         {
                             ProcessRequestResponse (ref newConsoleKeyInfo, ref key, _cki, ref mod);
                             ProcessRequestResponse (ref newConsoleKeyInfo, ref key, _cki, ref mod);
                             _cki = null;
                             _cki = null;
@@ -322,6 +355,11 @@ internal class NetEvents : IDisposable
 
 
                     ProcessMapConsoleKeyInfo (consoleKeyInfo);
                     ProcessMapConsoleKeyInfo (consoleKeyInfo);
 
 
+                    if (_retries > 0)
+                    {
+                        _retries = 0;
+                    }
+
                     break;
                     break;
                 }
                 }
             }
             }
@@ -460,17 +498,13 @@ internal class NetEvents : IDisposable
         if (seqReqStatus is { })
         if (seqReqStatus is { })
         {
         {
             //HandleRequestResponseEvent (c1Control, code, values, terminating);
             //HandleRequestResponseEvent (c1Control, code, values, terminating);
-            StringBuilder sb = new ();
 
 
-            foreach (ConsoleKeyInfo keyChar in cki)
-            {
-                sb.Append (keyChar.KeyChar);
-            }
+            var ckiString = EscSeqUtils.ToString (cki);
 
 
             lock (seqReqStatus.AnsiRequest._responseLock)
             lock (seqReqStatus.AnsiRequest._responseLock)
             {
             {
-                seqReqStatus.AnsiRequest.Response = sb.ToString ();
-                seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, sb.ToString ());
+                seqReqStatus.AnsiRequest.Response = ckiString;
+                seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, ckiString);
             }
             }
 
 
             return;
             return;
@@ -1165,7 +1199,7 @@ internal class NetDriver : ConsoleDriver
 
 
         return new MainLoop (_mainLoopDriver);
         return new MainLoop (_mainLoopDriver);
     }
     }
-    
+
     private void ProcessInput (InputResult inputEvent)
     private void ProcessInput (InputResult inputEvent)
     {
     {
         switch (inputEvent.EventType)
         switch (inputEvent.EventType)
@@ -1457,12 +1491,12 @@ internal class NetDriver : ConsoleDriver
                                                  };
                                                  };
 
 
                 _mainLoopDriver._netEvents.EscSeqRequests.Add (ansiRequest);
                 _mainLoopDriver._netEvents.EscSeqRequests.Add (ansiRequest);
-            }
 
 
-            if (!_ansiResponseTokenSource.IsCancellationRequested && Console.KeyAvailable)
-            {
                 _mainLoopDriver._netEvents._forceRead = true;
                 _mainLoopDriver._netEvents._forceRead = true;
+            }
 
 
+            if (!_ansiResponseTokenSource.IsCancellationRequested)
+            {
                 _mainLoopDriver._netEvents._waitForStart.Set ();
                 _mainLoopDriver._netEvents._waitForStart.Set ();
 
 
                 if (!_mainLoopDriver._waitForProbe.IsSet)
                 if (!_mainLoopDriver._waitForProbe.IsSet)
@@ -1497,6 +1531,9 @@ internal class NetDriver : ConsoleDriver
         return response;
         return response;
     }
     }
 
 
+    /// <inheritdoc />
+    public override void WriteRaw (string ansi) { throw new NotImplementedException (); }
+
     private MouseEventArgs ToDriverMouse (NetEvents.MouseEvent me)
     private MouseEventArgs ToDriverMouse (NetEvents.MouseEvent me)
     {
     {
        //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
        //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");

+ 10 - 17
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -18,9 +18,7 @@
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
-using System.Text;
 using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
 using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
-using static Terminal.Gui.SpinnerStyle;
 
 
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
@@ -1066,9 +1064,6 @@ internal class WindowsDriver : ConsoleDriver
 
 
     public override bool SupportsTrueColor => RunningUnitTests || (Environment.OSVersion.Version.Build >= 14931 && _isWindowsTerminal);
     public override bool SupportsTrueColor => RunningUnitTests || (Environment.OSVersion.Version.Build >= 14931 && _isWindowsTerminal);
 
 
-    /// <inheritdoc />
-    public override bool IsReportingMouseMoves { get; internal set; }
-
     public WindowsConsole WinConsole { get; private set; }
     public WindowsConsole WinConsole { get; private set; }
 
 
     public WindowsConsole.KeyEventRecord FromVKPacketToKeyEventRecord (WindowsConsole.KeyEventRecord keyEvent)
     public WindowsConsole.KeyEventRecord FromVKPacketToKeyEventRecord (WindowsConsole.KeyEventRecord keyEvent)
@@ -1196,7 +1191,7 @@ internal class WindowsDriver : ConsoleDriver
         }
         }
     }
     }
 
 
-    /// <inheritdoc />
+    /// <inheritdoc/>
     public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest)
     public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest)
     {
     {
         if (_mainLoopDriver is null)
         if (_mainLoopDriver is null)
@@ -1217,12 +1212,11 @@ internal class WindowsDriver : ConsoleDriver
 
 
         try
         try
         {
         {
-            if (WinConsole?.WriteANSI (ansiRequest.Request) == true)
-            {
-                Thread.Sleep (100); // Allow time for the terminal to respond
+            WriteRaw (ansiRequest.Request);
 
 
-                return ReadAnsiResponseDefault (ansiRequest);
-            }
+            Thread.Sleep (100); // Allow time for the terminal to respond
+
+            return ReadAnsiResponseDefault (ansiRequest);
         }
         }
         catch (Exception)
         catch (Exception)
         {
         {
@@ -1232,18 +1226,17 @@ internal class WindowsDriver : ConsoleDriver
         {
         {
             _mainLoopDriver._suspendRead = false;
             _mainLoopDriver._suspendRead = false;
         }
         }
-
-        return string.Empty;
     }
     }
 
 
     #region Not Implemented
     #region Not Implemented
 
 
-    public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
-
-    public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
-
     public override void Suspend () { throw new NotImplementedException (); }
     public override void Suspend () { throw new NotImplementedException (); }
 
 
+    public override void WriteRaw (string ansi)
+    {
+        WinConsole?.WriteANSI (ansi);
+    }
+
     #endregion
     #endregion
 
 
     public WindowsConsole.ConsoleKeyInfoEx ToConsoleKeyInfoEx (WindowsConsole.KeyEventRecord keyEvent)
     public WindowsConsole.ConsoleKeyInfoEx ToConsoleKeyInfoEx (WindowsConsole.KeyEventRecord keyEvent)