123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- #nullable enable
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- using Terminal.Gui.ConsoleDrivers;
- namespace Terminal.Gui;
- internal partial class WindowsConsole
- {
- private CancellationTokenSource? _inputReadyCancellationTokenSource;
- private readonly BlockingCollection<InputRecord> _inputQueue = new (new ConcurrentQueue<InputRecord> ());
- internal WindowsMainLoop? _mainLoop;
- public const int STD_OUTPUT_HANDLE = -11;
- public const int STD_INPUT_HANDLE = -10;
- private readonly nint _inputHandle;
- private nint _outputHandle;
- //private nint _screenBuffer;
- private readonly uint _originalConsoleMode;
- private CursorVisibility? _initialCursorVisibility;
- private CursorVisibility? _currentCursorVisibility;
- private CursorVisibility? _pendingCursorVisibility;
- private readonly StringBuilder _stringBuilder = new (256 * 1024);
- private string _lastWrite = string.Empty;
- public WindowsConsole ()
- {
- _inputHandle = GetStdHandle (STD_INPUT_HANDLE);
- _outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
- _originalConsoleMode = ConsoleMode;
- uint newConsoleMode = _originalConsoleMode;
- newConsoleMode |= (uint)(ConsoleModes.EnableMouseInput | ConsoleModes.EnableExtendedFlags);
- newConsoleMode &= ~(uint)ConsoleModes.EnableQuickEditMode;
- newConsoleMode &= ~(uint)ConsoleModes.EnableProcessedInput;
- ConsoleMode = newConsoleMode;
- _inputReadyCancellationTokenSource = new ();
- Task.Run (ProcessInputQueue, _inputReadyCancellationTokenSource.Token);
- }
- public InputRecord? DequeueInput ()
- {
- while (_inputReadyCancellationTokenSource is { })
- {
- try
- {
- return _inputQueue.Take (_inputReadyCancellationTokenSource.Token);
- }
- catch (OperationCanceledException)
- {
- return null;
- }
- }
- return null;
- }
- public InputRecord? ReadConsoleInput ()
- {
- const int BUFFER_SIZE = 1;
- InputRecord inputRecord = default;
- uint numberEventsRead = 0;
- while (!_inputReadyCancellationTokenSource!.IsCancellationRequested)
- {
- try
- {
- // Peek to check if there is any input available
- if (PeekConsoleInput (_inputHandle, out _, BUFFER_SIZE, out uint eventsRead) && eventsRead > 0)
- {
- // Read the input since it is available
- ReadConsoleInput (
- _inputHandle,
- out inputRecord,
- BUFFER_SIZE,
- out numberEventsRead);
- }
- if (numberEventsRead > 0)
- {
- return inputRecord;
- }
- try
- {
- Task.Delay (100, _inputReadyCancellationTokenSource.Token).Wait (_inputReadyCancellationTokenSource.Token);
- }
- catch (OperationCanceledException)
- {
- return null;
- }
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException or ObjectDisposedException)
- {
- return null;
- }
- throw;
- }
- }
- return null;
- }
- private void ProcessInputQueue ()
- {
- while (_inputReadyCancellationTokenSource is { IsCancellationRequested: false })
- {
- try
- {
- if (_inputQueue.Count == 0)
- {
- while (_inputReadyCancellationTokenSource is { IsCancellationRequested: false })
- {
- try
- {
- InputRecord? inpRec = ReadConsoleInput ();
- if (inpRec is { })
- {
- _inputQueue.Add (inpRec.Value);
- break;
- }
- }
- catch (OperationCanceledException)
- {
- return;
- }
- }
- }
- }
- catch (OperationCanceledException)
- {
- return;
- }
- }
- }
- private CharInfo []? _originalStdOutChars;
- public bool WriteToConsole (Size size, ExtendedCharInfo [] charInfoBuffer, Coord bufferSize, SmallRect window, bool force16Colors)
- {
- //Debug.WriteLine ("WriteToConsole");
- //if (_screenBuffer == nint.Zero)
- //{
- // ReadFromConsoleOutput (size, bufferSize, ref window);
- //}
- var result = false;
- if (force16Colors)
- {
- var i = 0;
- CharInfo [] ci = new CharInfo [charInfoBuffer.Length];
- foreach (ExtendedCharInfo info in charInfoBuffer)
- {
- ci [i++] = new CharInfo
- {
- Char = new CharUnion { UnicodeChar = info.Char },
- Attributes =
- (ushort)((int)info.Attribute.Foreground.GetClosestNamedColor16 () | ((int)info.Attribute.Background.GetClosestNamedColor16 () << 4))
- };
- }
- result = WriteConsoleOutput (_outputHandle, ci, bufferSize, new Coord { X = window.Left, Y = window.Top }, ref window);
- }
- else
- {
- _stringBuilder.Clear ();
- _stringBuilder.Append (EscSeqUtils.CSI_SaveCursorPosition);
- EscSeqUtils.CSI_AppendCursorPosition (_stringBuilder, 0, 0);
- Attribute? prev = null;
- foreach (ExtendedCharInfo info in charInfoBuffer)
- {
- Attribute attr = info.Attribute;
- if (attr != prev)
- {
- prev = attr;
- EscSeqUtils.CSI_AppendForegroundColorRGB (_stringBuilder, attr.Foreground.R, attr.Foreground.G, attr.Foreground.B);
- EscSeqUtils.CSI_AppendBackgroundColorRGB (_stringBuilder, attr.Background.R, attr.Background.G, attr.Background.B);
- }
- if (info.Char != '\x1b')
- {
- if (!info.Empty)
- {
- _stringBuilder.Append (info.Char);
- }
- }
- else
- {
- _stringBuilder.Append (' ');
- }
- }
- _stringBuilder.Append (EscSeqUtils.CSI_RestoreCursorPosition);
- _stringBuilder.Append (EscSeqUtils.CSI_HideCursor);
- var s = _stringBuilder.ToString ();
- // TODO: requires extensive testing if we go down this route
- // If console output has changed
- if (s != _lastWrite)
- {
- // supply console with the new content
- result = WriteConsole (_outputHandle, s, (uint)s.Length, out uint _, nint.Zero);
- }
- _lastWrite = s;
- foreach (var sixel in Application.Sixel)
- {
- SetCursorPosition (new Coord ((short)sixel.ScreenPosition.X, (short)sixel.ScreenPosition.Y));
- WriteConsole (_outputHandle, sixel.SixelData, (uint)sixel.SixelData.Length, out uint _, nint.Zero);
- }
- }
- if (!result)
- {
- int err = Marshal.GetLastWin32Error ();
- if (err != 0)
- {
- throw new Win32Exception (err);
- }
- }
- return result;
- }
- internal bool WriteANSI (string ansi)
- {
- if (WriteConsole (_outputHandle, ansi, (uint)ansi.Length, out uint _, nint.Zero))
- {
- // Flush the output to make sure it's sent immediately
- return FlushFileBuffers (_outputHandle);
- }
- return false;
- }
- public void ReadFromConsoleOutput (Size size, Coord coords, ref SmallRect window)
- {
- //_screenBuffer = CreateConsoleScreenBuffer (
- // DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
- // ShareMode.FileShareRead | ShareMode.FileShareWrite,
- // nint.Zero,
- // 1,
- // nint.Zero
- // );
- //if (_screenBuffer == INVALID_HANDLE_VALUE)
- //{
- // int err = Marshal.GetLastWin32Error ();
- // if (err != 0)
- // {
- // throw new Win32Exception (err);
- // }
- //}
- SetInitialCursorVisibility ();
- //if (!SetConsoleActiveScreenBuffer (_screenBuffer))
- //{
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- //}
- _originalStdOutChars = new CharInfo [size.Height * size.Width];
- if (!ReadConsoleOutput (_outputHandle, _originalStdOutChars, coords, new Coord { X = 0, Y = 0 }, ref window))
- {
- throw new Win32Exception (Marshal.GetLastWin32Error ());
- }
- }
- public bool SetCursorPosition (Coord position)
- {
- return SetConsoleCursorPosition (_outputHandle, position);
- }
- public void SetInitialCursorVisibility ()
- {
- if (_initialCursorVisibility.HasValue == false && GetCursorVisibility (out CursorVisibility visibility))
- {
- _initialCursorVisibility = visibility;
- }
- }
- public bool GetCursorVisibility (out CursorVisibility visibility)
- {
- if (_outputHandle == nint.Zero)
- {
- visibility = CursorVisibility.Invisible;
- return false;
- }
- if (!GetConsoleCursorInfo (_outputHandle, out ConsoleCursorInfo info))
- {
- int err = Marshal.GetLastWin32Error ();
- if (err != 0)
- {
- throw new Win32Exception (err);
- }
- visibility = CursorVisibility.Default;
- return false;
- }
- if (!info.bVisible)
- {
- visibility = CursorVisibility.Invisible;
- }
- else if (info.dwSize > 50)
- {
- visibility = CursorVisibility.Default;
- }
- else
- {
- visibility = CursorVisibility.Default;
- }
- return visibility != CursorVisibility.Invisible;
- }
- public bool EnsureCursorVisibility ()
- {
- if (_initialCursorVisibility.HasValue && _pendingCursorVisibility.HasValue && SetCursorVisibility (_pendingCursorVisibility.Value))
- {
- _pendingCursorVisibility = null;
- return true;
- }
- return false;
- }
- public void ForceRefreshCursorVisibility ()
- {
- if (_currentCursorVisibility.HasValue)
- {
- _pendingCursorVisibility = _currentCursorVisibility;
- _currentCursorVisibility = null;
- }
- }
- public bool SetCursorVisibility (CursorVisibility visibility)
- {
- if (_initialCursorVisibility.HasValue == false)
- {
- _pendingCursorVisibility = visibility;
- return false;
- }
- if (_currentCursorVisibility.HasValue == false || _currentCursorVisibility.Value != visibility)
- {
- var info = new ConsoleCursorInfo
- {
- dwSize = (uint)visibility & 0x00FF,
- bVisible = ((uint)visibility & 0xFF00) != 0
- };
- if (!SetConsoleCursorInfo (_outputHandle, ref info))
- {
- return false;
- }
- _currentCursorVisibility = visibility;
- }
- return true;
- }
- public void Cleanup ()
- {
- if (_initialCursorVisibility.HasValue)
- {
- SetCursorVisibility (_initialCursorVisibility.Value);
- }
- //SetConsoleOutputWindow (out _);
- ConsoleMode = _originalConsoleMode;
- _outputHandle = CreateConsoleScreenBuffer (
- DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
- ShareMode.FileShareRead | ShareMode.FileShareWrite,
- nint.Zero,
- 1,
- nint.Zero
- );
- if (!SetConsoleActiveScreenBuffer (_outputHandle))
- {
- int err = Marshal.GetLastWin32Error ();
- Console.WriteLine ("Error: {0}", err);
- }
- //if (_screenBuffer != nint.Zero)
- //{
- // CloseHandle (_screenBuffer);
- //}
- //_screenBuffer = nint.Zero;
- _inputReadyCancellationTokenSource?.Cancel ();
- _inputReadyCancellationTokenSource?.Dispose ();
- _inputReadyCancellationTokenSource = null;
- }
- internal Size GetConsoleBufferWindow (out Point position)
- {
- if (_outputHandle == nint.Zero)
- {
- position = Point.Empty;
- return Size.Empty;
- }
- var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
- csbi.cbSize = (uint)Marshal.SizeOf (csbi);
- if (!GetConsoleScreenBufferInfoEx (_outputHandle, ref csbi))
- {
- //throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
- position = Point.Empty;
- return Size.Empty;
- }
- Size sz = new (
- csbi.srWindow.Right - csbi.srWindow.Left + 1,
- csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
- position = new (csbi.srWindow.Left, csbi.srWindow.Top);
- return sz;
- }
- internal Size GetConsoleOutputWindow (out Point position)
- {
- var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
- csbi.cbSize = (uint)Marshal.SizeOf (csbi);
- if (!GetConsoleScreenBufferInfoEx (_outputHandle, ref csbi))
- {
- throw new Win32Exception (Marshal.GetLastWin32Error ());
- }
- Size sz = new (
- csbi.srWindow.Right - csbi.srWindow.Left + 1,
- csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
- position = new (csbi.srWindow.Left, csbi.srWindow.Top);
- return sz;
- }
- //internal Size SetConsoleWindow (short cols, short rows)
- //{
- // var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
- // csbi.cbSize = (uint)Marshal.SizeOf (csbi);
- // if (!GetConsoleScreenBufferInfoEx (_screenBuffer, ref csbi))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- // Coord maxWinSize = GetLargestConsoleWindowSize (_screenBuffer);
- // short newCols = Math.Min (cols, maxWinSize.X);
- // short newRows = Math.Min (rows, maxWinSize.Y);
- // csbi.dwSize = new Coord (newCols, Math.Max (newRows, (short)1));
- // csbi.srWindow = new SmallRect (0, 0, newCols, newRows);
- // csbi.dwMaximumWindowSize = new Coord (newCols, newRows);
- // if (!SetConsoleScreenBufferInfoEx (_screenBuffer, ref csbi))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- // var winRect = new SmallRect (0, 0, (short)(newCols - 1), (short)Math.Max (newRows - 1, 0));
- // if (!SetConsoleWindowInfo (_outputHandle, true, ref winRect))
- // {
- // //throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error ());
- // return new (cols, rows);
- // }
- // SetConsoleOutputWindow (csbi);
- // return new (winRect.Right + 1, newRows - 1 < 0 ? 0 : winRect.Bottom + 1);
- //}
- //private void SetConsoleOutputWindow (CONSOLE_SCREEN_BUFFER_INFOEX csbi)
- //{
- // if (_screenBuffer != nint.Zero && !SetConsoleScreenBufferInfoEx (_screenBuffer, ref csbi))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- //}
- //internal Size SetConsoleOutputWindow (out Point position)
- //{
- // if (_screenBuffer == nint.Zero)
- // {
- // position = Point.Empty;
- // return Size.Empty;
- // }
- // var csbi = new CONSOLE_SCREEN_BUFFER_INFOEX ();
- // csbi.cbSize = (uint)Marshal.SizeOf (csbi);
- // if (!GetConsoleScreenBufferInfoEx (_screenBuffer, ref csbi))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- // Size sz = new (
- // csbi.srWindow.Right - csbi.srWindow.Left + 1,
- // Math.Max (csbi.srWindow.Bottom - csbi.srWindow.Top + 1, 0));
- // position = new (csbi.srWindow.Left, csbi.srWindow.Top);
- // SetConsoleOutputWindow (csbi);
- // var winRect = new SmallRect (0, 0, (short)(sz.Width - 1), (short)Math.Max (sz.Height - 1, 0));
- // if (!SetConsoleScreenBufferInfoEx (_outputHandle, ref csbi))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- // if (!SetConsoleWindowInfo (_outputHandle, true, ref winRect))
- // {
- // throw new Win32Exception (Marshal.GetLastWin32Error ());
- // }
- // return sz;
- //}
- private uint ConsoleMode
- {
- get
- {
- GetConsoleMode (_inputHandle, out uint v);
- return v;
- }
- set => SetConsoleMode (_inputHandle, value);
- }
- [Flags]
- public enum ConsoleModes : uint
- {
- EnableProcessedInput = 1,
- EnableMouseInput = 16,
- EnableQuickEditMode = 64,
- EnableExtendedFlags = 128
- }
- [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
- public struct KeyEventRecord
- {
- [FieldOffset (0)]
- [MarshalAs (UnmanagedType.Bool)]
- public bool bKeyDown;
- [FieldOffset (4)]
- [MarshalAs (UnmanagedType.U2)]
- public ushort wRepeatCount;
- [FieldOffset (6)]
- [MarshalAs (UnmanagedType.U2)]
- public ConsoleKeyMapping.VK wVirtualKeyCode;
- [FieldOffset (8)]
- [MarshalAs (UnmanagedType.U2)]
- public ushort wVirtualScanCode;
- [FieldOffset (10)]
- public char UnicodeChar;
- [FieldOffset (12)]
- [MarshalAs (UnmanagedType.U4)]
- public ControlKeyState dwControlKeyState;
- public readonly override string ToString ()
- {
- return
- $"[KeyEventRecord({(bKeyDown ? "down" : "up")},{wRepeatCount},{wVirtualKeyCode},{wVirtualScanCode},{new Rune (UnicodeChar).MakePrintable ()},{dwControlKeyState})]";
- }
- }
- [Flags]
- public enum ButtonState
- {
- NoButtonPressed = 0,
- Button1Pressed = 1,
- Button2Pressed = 4,
- Button3Pressed = 8,
- Button4Pressed = 16,
- RightmostButtonPressed = 2
- }
- [Flags]
- public enum ControlKeyState
- {
- NoControlKeyPressed = 0,
- RightAltPressed = 1,
- LeftAltPressed = 2,
- RightControlPressed = 4,
- LeftControlPressed = 8,
- ShiftPressed = 16,
- NumlockOn = 32,
- ScrolllockOn = 64,
- CapslockOn = 128,
- EnhancedKey = 256
- }
- [Flags]
- public enum EventFlags
- {
- NoEvent = 0,
- MouseMoved = 1,
- DoubleClick = 2,
- MouseWheeled = 4,
- MouseHorizontalWheeled = 8
- }
- [StructLayout (LayoutKind.Explicit)]
- public struct MouseEventRecord
- {
- [FieldOffset (0)]
- public Coord MousePosition;
- [FieldOffset (4)]
- public ButtonState ButtonState;
- [FieldOffset (8)]
- public ControlKeyState ControlKeyState;
- [FieldOffset (12)]
- public EventFlags EventFlags;
- public readonly override string ToString () { return $"[Mouse{MousePosition},{ButtonState},{ControlKeyState},{EventFlags}]"; }
- }
- public struct WindowBufferSizeRecord
- {
- public Coord _size;
- public WindowBufferSizeRecord (short x, short y) { _size = new Coord (x, y); }
- public readonly override string ToString () { return $"[WindowBufferSize{_size}"; }
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct MenuEventRecord
- {
- public uint dwCommandId;
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct FocusEventRecord
- {
- public uint bSetFocus;
- }
- public enum EventType : ushort
- {
- Focus = 0x10,
- Key = 0x1,
- Menu = 0x8,
- Mouse = 2,
- WindowBufferSize = 4
- }
- [StructLayout (LayoutKind.Explicit)]
- public struct InputRecord
- {
- [FieldOffset (0)]
- public EventType EventType;
- [FieldOffset (4)]
- public KeyEventRecord KeyEvent;
- [FieldOffset (4)]
- public MouseEventRecord MouseEvent;
- [FieldOffset (4)]
- public WindowBufferSizeRecord WindowBufferSizeEvent;
- [FieldOffset (4)]
- public MenuEventRecord MenuEvent;
- [FieldOffset (4)]
- public FocusEventRecord FocusEvent;
- public readonly override string ToString ()
- {
- return (EventType switch
- {
- EventType.Focus => FocusEvent.ToString (),
- EventType.Key => KeyEvent.ToString (),
- EventType.Menu => MenuEvent.ToString (),
- EventType.Mouse => MouseEvent.ToString (),
- EventType.WindowBufferSize => WindowBufferSizeEvent.ToString (),
- _ => "Unknown event type: " + EventType
- })!;
- }
- }
- [Flags]
- private enum ShareMode : uint
- {
- FileShareRead = 1,
- FileShareWrite = 2
- }
- [Flags]
- private enum DesiredAccess : uint
- {
- GenericRead = 2147483648,
- GenericWrite = 1073741824
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct ConsoleScreenBufferInfo
- {
- public Coord dwSize;
- public Coord dwCursorPosition;
- public ushort wAttributes;
- public SmallRect srWindow;
- public Coord dwMaximumWindowSize;
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct Coord
- {
- public short X;
- public short Y;
- public Coord (short x, short y)
- {
- X = x;
- Y = y;
- }
- public readonly override string ToString () { return $"({X},{Y})"; }
- }
- [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
- public struct CharUnion
- {
- [FieldOffset (0)]
- public char UnicodeChar;
- [FieldOffset (0)]
- public byte AsciiChar;
- }
- [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
- public struct CharInfo
- {
- [FieldOffset (0)]
- public CharUnion Char;
- [FieldOffset (2)]
- public ushort Attributes;
- }
- public struct ExtendedCharInfo
- {
- public char Char { get; set; }
- public Attribute Attribute { get; set; }
- public bool Empty { get; set; } // TODO: Temp hack until virtual terminal sequences
- public ExtendedCharInfo (char character, Attribute attribute)
- {
- Char = character;
- Attribute = attribute;
- Empty = false;
- }
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct SmallRect
- {
- public short Left;
- public short Top;
- public short Right;
- public short Bottom;
- public SmallRect (short left, short top, short right, short bottom)
- {
- Left = left;
- Top = top;
- Right = right;
- Bottom = bottom;
- }
- public static void MakeEmpty (ref SmallRect rect) { rect.Left = -1; }
- public static void Update (ref SmallRect rect, short col, short row)
- {
- if (rect.Left == -1)
- {
- rect.Left = rect.Right = col;
- rect.Bottom = rect.Top = row;
- return;
- }
- if (col >= rect.Left && col <= rect.Right && row >= rect.Top && row <= rect.Bottom)
- {
- return;
- }
- if (col < rect.Left)
- {
- rect.Left = col;
- }
- if (col > rect.Right)
- {
- rect.Right = col;
- }
- if (row < rect.Top)
- {
- rect.Top = row;
- }
- if (row > rect.Bottom)
- {
- rect.Bottom = row;
- }
- }
- public readonly override string ToString () { return $"Left={Left},Top={Top},Right={Right},Bottom={Bottom}"; }
- }
- [StructLayout (LayoutKind.Sequential)]
- public struct ConsoleKeyInfoEx
- {
- public ConsoleKeyInfo ConsoleKeyInfo;
- public bool CapsLock;
- public bool NumLock;
- public bool ScrollLock;
- public ConsoleKeyInfoEx (ConsoleKeyInfo consoleKeyInfo, bool capslock, bool numlock, bool scrolllock)
- {
- ConsoleKeyInfo = consoleKeyInfo;
- CapsLock = capslock;
- NumLock = numlock;
- ScrollLock = scrolllock;
- }
- /// <summary>
- /// Prints a ConsoleKeyInfoEx structure
- /// </summary>
- /// <param name="ex"></param>
- /// <returns></returns>
- public readonly string ToString (ConsoleKeyInfoEx ex)
- {
- var ke = new Key ((KeyCode)ex.ConsoleKeyInfo.KeyChar);
- var sb = new StringBuilder ();
- sb.Append ($"Key: {(KeyCode)ex.ConsoleKeyInfo.Key} ({ex.ConsoleKeyInfo.Key})");
- sb.Append ((ex.ConsoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0 ? " | Shift" : string.Empty);
- sb.Append ((ex.ConsoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0 ? " | Control" : string.Empty);
- sb.Append ((ex.ConsoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0 ? " | Alt" : string.Empty);
- sb.Append ($", KeyChar: {ke.AsRune.MakePrintable ()} ({(uint)ex.ConsoleKeyInfo.KeyChar}) ");
- sb.Append (ex.CapsLock ? "caps," : string.Empty);
- sb.Append (ex.NumLock ? "num," : string.Empty);
- sb.Append (ex.ScrollLock ? "scroll," : string.Empty);
- string s = sb.ToString ().TrimEnd (',').TrimEnd (' ');
- return $"[ConsoleKeyInfoEx({s})]";
- }
- }
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern nint GetStdHandle (int nStdHandle);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool CloseHandle (nint handle);
- [DllImport ("kernel32.dll", SetLastError = true)]
- public static extern bool PeekConsoleInput (nint hConsoleInput, out InputRecord lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
- [DllImport ("kernel32.dll", EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
- public static extern bool ReadConsoleInput (
- nint hConsoleInput,
- out InputRecord lpBuffer,
- uint nLength,
- out uint lpNumberOfEventsRead
- );
- [DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- private static extern bool ReadConsoleOutput (
- nint hConsoleOutput,
- [Out] CharInfo [] lpBuffer,
- Coord dwBufferSize,
- Coord dwBufferCoord,
- ref SmallRect lpReadRegion
- );
- // TODO: This API is obsolete. See https://learn.microsoft.com/en-us/windows/console/writeconsoleoutput
- [DllImport ("kernel32.dll", EntryPoint = "WriteConsoleOutputW", SetLastError = true, CharSet = CharSet.Unicode)]
- public static extern bool WriteConsoleOutput (
- nint hConsoleOutput,
- CharInfo [] lpBuffer,
- Coord dwBufferSize,
- Coord dwBufferCoord,
- ref SmallRect lpWriteRegion
- );
- [LibraryImport ("kernel32.dll", EntryPoint = "WriteConsoleW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
- [return: MarshalAs (UnmanagedType.Bool)]
- private static partial bool WriteConsole (
- nint hConsoleOutput,
- ReadOnlySpan<char> lpbufer,
- uint NumberOfCharsToWriten,
- out uint lpNumberOfCharsWritten,
- nint lpReserved
- );
- [DllImport ("kernel32.dll", SetLastError = true)]
- static extern bool FlushFileBuffers (nint hFile);
- [DllImport ("kernel32.dll")]
- private static extern bool SetConsoleCursorPosition (nint hConsoleOutput, Coord dwCursorPosition);
- [StructLayout (LayoutKind.Sequential)]
- public struct ConsoleCursorInfo
- {
- /// <summary>
- /// The percentage of the character cell that is filled by the cursor.This value is between 1 and 100.
- /// The cursor appearance varies, ranging from completely filling the cell to showing up as a horizontal
- /// line at the bottom of the cell.
- /// </summary>
- public uint dwSize;
- public bool bVisible;
- }
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool SetConsoleCursorInfo (nint hConsoleOutput, [In] ref ConsoleCursorInfo lpConsoleCursorInfo);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool GetConsoleCursorInfo (nint hConsoleOutput, out ConsoleCursorInfo lpConsoleCursorInfo);
- [DllImport ("kernel32.dll")]
- private static extern bool GetConsoleMode (nint hConsoleHandle, out uint lpMode);
- [DllImport ("kernel32.dll")]
- private static extern bool SetConsoleMode (nint hConsoleHandle, uint dwMode);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern nint CreateConsoleScreenBuffer (
- DesiredAccess dwDesiredAccess,
- ShareMode dwShareMode,
- nint secutiryAttributes,
- uint flags,
- nint screenBufferData
- );
- internal static nint INVALID_HANDLE_VALUE = new (-1);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool SetConsoleActiveScreenBuffer (nint handle);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool GetNumberOfConsoleInputEvents (nint handle, out uint lpcNumberOfEvents);
- internal uint GetNumberOfConsoleInputEvents ()
- {
- if (!GetNumberOfConsoleInputEvents (_inputHandle, out uint numOfEvents))
- {
- Console.WriteLine ($"Error: {Marshal.GetLastWin32Error ()}");
- return 0;
- }
- return numOfEvents;
- }
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool FlushConsoleInputBuffer (nint handle);
- internal void FlushConsoleInputBuffer ()
- {
- if (!FlushConsoleInputBuffer (_inputHandle))
- {
- Console.WriteLine ($"Error: {Marshal.GetLastWin32Error ()}");
- }
- }
- #if false // Not needed on the constructor. Perhaps could be used on resizing. To study.
- [DllImport ("kernel32.dll", ExactSpelling = true)]
- static extern IntPtr GetConsoleWindow ();
- [DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- static extern bool ShowWindow (IntPtr hWnd, int nCmdShow);
- public const int HIDE = 0;
- public const int MAXIMIZE = 3;
- public const int MINIMIZE = 6;
- public const int RESTORE = 9;
- internal void ShowWindow (int state)
- {
- IntPtr thisConsole = GetConsoleWindow ();
- ShowWindow (thisConsole, state);
- }
- #endif
- // See: https://github.com/gui-cs/Terminal.Gui/issues/357
- [StructLayout (LayoutKind.Sequential)]
- public struct CONSOLE_SCREEN_BUFFER_INFOEX
- {
- public uint cbSize;
- public Coord dwSize;
- public Coord dwCursorPosition;
- public ushort wAttributes;
- public SmallRect srWindow;
- public Coord dwMaximumWindowSize;
- public ushort wPopupAttributes;
- public bool bFullscreenSupported;
- [MarshalAs (UnmanagedType.ByValArray, SizeConst = 16)]
- public COLORREF [] ColorTable;
- }
- [StructLayout (LayoutKind.Explicit, Size = 4)]
- public struct COLORREF
- {
- public COLORREF (byte r, byte g, byte b)
- {
- Value = 0;
- R = r;
- G = g;
- B = b;
- }
- public COLORREF (uint value)
- {
- R = 0;
- G = 0;
- B = 0;
- Value = value & 0x00FFFFFF;
- }
- [FieldOffset (0)]
- public byte R;
- [FieldOffset (1)]
- public byte G;
- [FieldOffset (2)]
- public byte B;
- [FieldOffset (0)]
- public uint Value;
- }
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool GetConsoleScreenBufferInfoEx (nint hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX csbi);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool SetConsoleScreenBufferInfoEx (nint hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX consoleScreenBufferInfo);
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern bool SetConsoleWindowInfo (
- nint hConsoleOutput,
- bool bAbsolute,
- [In] ref SmallRect lpConsoleWindow
- );
- [DllImport ("kernel32.dll", SetLastError = true)]
- private static extern Coord GetLargestConsoleWindowSize (
- nint hConsoleOutput
- );
- }
|