diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver/NetDriver.cs index 0422f548f..73ec87d41 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver/NetDriver.cs @@ -1,4 +1,4 @@ -// TODO: #nullable enable +#nullable enable // // NetDriver.cs: The System.Console-based .NET driver, works on Windows and Unix, but is not particularly efficient. // @@ -14,7 +14,7 @@ namespace Terminal.Gui; internal class NetDriver : ConsoleDriver { public bool IsWinPlatform { get; private set; } - public NetWinVTConsole NetWinConsole { get; private set; } + public NetWinVTConsole? NetWinConsole { get; private set; } public override void Refresh () { @@ -61,7 +61,7 @@ internal class NetDriver : ConsoleDriver if (RunningUnitTests || _winSizeChanging || Console.WindowHeight < 1 - || Contents.Length != Rows * Cols + || Contents?.Length != Rows * Cols || Rows != Console.WindowHeight) { return; @@ -85,7 +85,7 @@ internal class NetDriver : ConsoleDriver return; } - if (!_dirtyLines [row]) + if (!_dirtyLines! [row]) { continue; } @@ -129,7 +129,7 @@ internal class NetDriver : ConsoleDriver lastCol = col; } - Attribute attr = Contents [row, col].Attribute.Value; + Attribute attr = Contents [row, col].Attribute!.Value; // Performance: Only send the escape sequence if the attribute has changed. if (attr != redrawAttr) @@ -229,7 +229,7 @@ internal class NetDriver : ConsoleDriver #region Init/End/MainLoop - internal NetMainLoop _mainLoopDriver; + internal NetMainLoop? _mainLoopDriver; internal override MainLoop Init () { @@ -339,7 +339,7 @@ internal class NetDriver : ConsoleDriver Left = 0; Cols = inputEvent.WindowSizeEvent.Size.Width; Rows = Math.Max (inputEvent.WindowSizeEvent.Size.Height, 0); - ; + ResizeScreen (); ClearContents (); _winSizeChanging = false; @@ -727,16 +727,21 @@ internal class NetDriver : ConsoleDriver #region Low-Level DotNet tuff private readonly ManualResetEventSlim _waitAnsiResponse = new (false); - private readonly CancellationTokenSource _ansiResponseTokenSource = new (); + private CancellationTokenSource? _ansiResponseTokenSource; /// - public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest) + public override string? WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest) { - if (_mainLoopDriver is null) + lock (ansiRequest._responseLock) { - return string.Empty; + if (_mainLoopDriver is null) + { + return string.Empty; + } } + _ansiResponseTokenSource ??= new (); + try { lock (ansiRequest._responseLock) @@ -765,12 +770,12 @@ internal class NetDriver : ConsoleDriver { _mainLoopDriver._netEvents._forceRead = false; - if (_mainLoopDriver._netEvents.EscSeqRequests.Statuses.TryPeek (out AnsiEscapeSequenceRequestStatus request)) + if (_mainLoopDriver._netEvents.EscSeqRequests.Statuses.TryPeek (out AnsiEscapeSequenceRequestStatus? request)) { if (_mainLoopDriver._netEvents.EscSeqRequests.Statuses.Count > 0 && string.IsNullOrEmpty (request.AnsiRequest.Response)) { - lock (request!.AnsiRequest._responseLock) + lock (request.AnsiRequest._responseLock) { // Bad request or no response at all _mainLoopDriver._netEvents.EscSeqRequests.Statuses.TryDequeue (out _); diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver/NetEvents.cs b/Terminal.Gui/ConsoleDrivers/NetDriver/NetEvents.cs index 9976c0136..6ce0bbe26 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver/NetEvents.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver/NetEvents.cs @@ -1,4 +1,4 @@ -// TODO: #nullable enable +#nullable enable using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; @@ -6,10 +6,10 @@ namespace Terminal.Gui; internal class NetEvents : IDisposable { - private CancellationTokenSource _inputReadyCancellationTokenSource; + private CancellationTokenSource? _inputReadyCancellationTokenSource; private readonly BlockingCollection _inputQueue = new (new ConcurrentQueue ()); private readonly ConsoleDriver _consoleDriver; - private ConsoleKeyInfo [] _cki; + private ConsoleKeyInfo []? _cki; private bool _isEscSeq; #if PROCESS_REQUEST bool _neededProcessRequest; @@ -62,9 +62,9 @@ internal class NetEvents : IDisposable { if (_retries > 1) { - if (EscSeqRequests.Statuses.TryPeek (out AnsiEscapeSequenceRequestStatus seqReqStatus) && string.IsNullOrEmpty (seqReqStatus.AnsiRequest.Response)) + if (EscSeqRequests.Statuses.TryPeek (out AnsiEscapeSequenceRequestStatus? seqReqStatus) && string.IsNullOrEmpty (seqReqStatus.AnsiRequest.Response)) { - lock (seqReqStatus!.AnsiRequest._responseLock) + lock (seqReqStatus.AnsiRequest._responseLock) { EscSeqRequests.Statuses.TryDequeue (out _); @@ -319,7 +319,7 @@ internal class NetEvents : IDisposable out bool isMouse, out List mouseFlags, out Point pos, - out AnsiEscapeSequenceRequestStatus seqReqStatus, + out AnsiEscapeSequenceRequestStatus? seqReqStatus, (f, p) => HandleMouseEvent (MapMouseFlags (f), p) ); @@ -350,7 +350,7 @@ internal class NetEvents : IDisposable if (!string.IsNullOrEmpty (AnsiEscapeSequenceRequestUtils.InvalidRequestTerminator)) { - if (EscSeqRequests.Statuses.TryDequeue (out AnsiEscapeSequenceRequestStatus result)) + if (EscSeqRequests.Statuses.TryDequeue (out AnsiEscapeSequenceRequestStatus? result)) { lock (result.AnsiRequest._responseLock) { @@ -504,7 +504,7 @@ internal class NetEvents : IDisposable return mbs; } - private Point _lastCursorPosition; + //private Point _lastCursorPosition; //private void HandleRequestResponseEvent (string c1Control, string code, string [] values, string terminating) //{ @@ -651,15 +651,15 @@ internal class NetEvents : IDisposable public readonly override string ToString () { - return EventType switch - { - EventType.Key => ToString (ConsoleKeyInfo), - EventType.Mouse => MouseEvent.ToString (), + return (EventType switch + { + EventType.Key => ToString (ConsoleKeyInfo), + EventType.Mouse => MouseEvent.ToString (), - //EventType.WindowSize => WindowSize.ToString (), - //EventType.RequestResponse => RequestResponse.ToString (), - _ => "Unknown event type: " + EventType - }; + //EventType.WindowSize => WindowSize.ToString (), + //EventType.RequestResponse => RequestResponse.ToString (), + _ => "Unknown event type: " + EventType + })!; } /// Prints a ConsoleKeyInfoEx structure diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs b/Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs index 6e68bf8d5..df6b63203 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs @@ -1,4 +1,5 @@ -using System.Collections.Concurrent; +#nullable enable +using System.Collections.Concurrent; namespace Terminal.Gui; @@ -9,22 +10,22 @@ namespace Terminal.Gui; /// This implementation is used for NetDriver. internal class NetMainLoop : IMainLoopDriver { - internal NetEvents _netEvents; + internal NetEvents? _netEvents; /// Invoked when a Key is pressed. - internal Action ProcessInput; + internal Action? ProcessInput; private readonly ManualResetEventSlim _eventReady = new (false); private readonly CancellationTokenSource _inputHandlerTokenSource = new (); private readonly BlockingCollection _resultQueue = new (new ConcurrentQueue ()); private readonly CancellationTokenSource _eventReadyTokenSource = new (); - private MainLoop _mainLoop; + private MainLoop? _mainLoop; /// Initializes the class with the console driver. /// Passing a consoleDriver is provided to capture windows resizing. /// The console driver used by this Net main loop. /// - public NetMainLoop (ConsoleDriver consoleDriver = null) + public NetMainLoop (ConsoleDriver consoleDriver) { ArgumentNullException.ThrowIfNull (consoleDriver); @@ -47,7 +48,7 @@ internal class NetMainLoop : IMainLoopDriver bool IMainLoopDriver.EventsPending () { - if (_resultQueue.Count > 0 || _mainLoop.CheckTimersAndIdleHandlers (out int waitTimeout)) + if (_resultQueue.Count > 0 || _mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout)) { return true; } @@ -88,24 +89,21 @@ internal class NetMainLoop : IMainLoopDriver // Always dequeue even if it's null and invoke if isn't null if (_resultQueue.TryTake (out NetEvents.InputResult dequeueResult)) { - if (dequeueResult is { }) - { - ProcessInput?.Invoke (dequeueResult); - } + ProcessInput?.Invoke (dequeueResult); } } } void IMainLoopDriver.TearDown () { - _inputHandlerTokenSource?.Cancel (); - _inputHandlerTokenSource?.Dispose (); - _eventReadyTokenSource?.Cancel (); - _eventReadyTokenSource?.Dispose (); + _inputHandlerTokenSource.Cancel (); + _inputHandlerTokenSource.Dispose (); + _eventReadyTokenSource.Cancel (); + _eventReadyTokenSource.Dispose (); - _eventReady?.Dispose (); + _eventReady.Dispose (); - _resultQueue?.Dispose(); + _resultQueue.Dispose(); _netEvents?.Dispose (); _netEvents = null; @@ -123,9 +121,9 @@ internal class NetMainLoop : IMainLoopDriver return; } - if (_resultQueue?.Count == 0 || _netEvents._forceRead) + if (_resultQueue?.Count == 0 || _netEvents!._forceRead) { - NetEvents.InputResult? result = _netEvents.DequeueInput (); + NetEvents.InputResult? result = _netEvents!.DequeueInput (); if (result.HasValue) { diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver/NetWinVTConsole.cs b/Terminal.Gui/ConsoleDrivers/NetDriver/NetWinVTConsole.cs index 81a9f6b68..98666f460 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver/NetWinVTConsole.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver/NetWinVTConsole.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +#nullable enable +using System.Runtime.InteropServices; namespace Terminal.Gui;