WindowsDriver.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. //
  2. // WindowsDriver.cs: Windows specific driver
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. // Nick Van Dyck ([email protected])
  7. //
  8. // Copyright (c) 2018
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in all
  18. // copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. // SOFTWARE.
  27. //
  28. using System;
  29. using System.CodeDom;
  30. using System.Diagnostics;
  31. using System.Runtime.InteropServices;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. using Mono.Terminal;
  35. using NStack;
  36. namespace Terminal.Gui {
  37. internal class WindowsConsole {
  38. public const int STD_OUTPUT_HANDLE = -11;
  39. public const int STD_INPUT_HANDLE = -10;
  40. public const int STD_ERROR_HANDLE = -12;
  41. internal IntPtr InputHandle, OutputHandle;
  42. IntPtr ScreenBuffer;
  43. uint originalConsoleMode;
  44. public WindowsConsole ()
  45. {
  46. InputHandle = GetStdHandle (STD_INPUT_HANDLE);
  47. OutputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
  48. originalConsoleMode = ConsoleMode;
  49. var newConsoleMode = originalConsoleMode;
  50. newConsoleMode |= (uint)(ConsoleModes.EnableMouseInput | ConsoleModes.EnableExtendedFlags);
  51. newConsoleMode &= ~(uint)ConsoleModes.EnableQuickEditMode;
  52. newConsoleMode &= ~(uint)ConsoleModes.EnableProcessedInput;
  53. ConsoleMode = newConsoleMode;
  54. }
  55. public CharInfo [] OriginalStdOutChars;
  56. public bool WriteToConsole (CharInfo [] charInfoBuffer, Coord coords, SmallRect window)
  57. {
  58. if (ScreenBuffer == IntPtr.Zero) {
  59. ScreenBuffer = CreateConsoleScreenBuffer (
  60. DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
  61. ShareMode.FileShareRead | ShareMode.FileShareWrite,
  62. IntPtr.Zero,
  63. 1,
  64. IntPtr.Zero
  65. );
  66. if (ScreenBuffer == INVALID_HANDLE_VALUE) {
  67. var err = Marshal.GetLastWin32Error ();
  68. if (err != 0)
  69. throw new System.ComponentModel.Win32Exception (err);
  70. }
  71. if (!SetConsoleActiveScreenBuffer (ScreenBuffer)) {
  72. var err = Marshal.GetLastWin32Error ();
  73. throw new System.ComponentModel.Win32Exception (err);
  74. }
  75. OriginalStdOutChars = new CharInfo [Console.WindowHeight * Console.WindowWidth];
  76. ReadConsoleOutput (OutputHandle, OriginalStdOutChars, coords, new Coord () { X = 0, Y = 0 }, ref window);
  77. }
  78. return WriteConsoleOutput (ScreenBuffer, charInfoBuffer, coords, new Coord () { X = window.Left, Y = window.Top }, ref window);
  79. }
  80. public bool SetCursorPosition (Coord position)
  81. {
  82. return SetConsoleCursorPosition (ScreenBuffer, position);
  83. }
  84. public void Cleanup ()
  85. {
  86. ConsoleMode = originalConsoleMode;
  87. //ContinueListeningForConsoleEvents = false;
  88. if (!SetConsoleActiveScreenBuffer (OutputHandle)) {
  89. var err = Marshal.GetLastWin32Error ();
  90. Console.WriteLine ("Error: {0}", err);
  91. }
  92. if (ScreenBuffer != IntPtr.Zero)
  93. CloseHandle (ScreenBuffer);
  94. ScreenBuffer = IntPtr.Zero;
  95. }
  96. //bool ContinueListeningForConsoleEvents = true;
  97. public uint ConsoleMode {
  98. get {
  99. uint v;
  100. GetConsoleMode (InputHandle, out v);
  101. return v;
  102. }
  103. set {
  104. SetConsoleMode (InputHandle, value);
  105. }
  106. }
  107. [Flags]
  108. public enum ConsoleModes : uint {
  109. EnableProcessedInput = 1,
  110. EnableMouseInput = 16,
  111. EnableQuickEditMode = 64,
  112. EnableExtendedFlags = 128,
  113. }
  114. [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
  115. public struct KeyEventRecord {
  116. [FieldOffset (0), MarshalAs (UnmanagedType.Bool)]
  117. public bool bKeyDown;
  118. [FieldOffset (4), MarshalAs (UnmanagedType.U2)]
  119. public ushort wRepeatCount;
  120. [FieldOffset (6), MarshalAs (UnmanagedType.U2)]
  121. public ushort wVirtualKeyCode;
  122. [FieldOffset (8), MarshalAs (UnmanagedType.U2)]
  123. public ushort wVirtualScanCode;
  124. [FieldOffset (10)]
  125. public char UnicodeChar;
  126. [FieldOffset (12), MarshalAs (UnmanagedType.U4)]
  127. public ControlKeyState dwControlKeyState;
  128. }
  129. [Flags]
  130. public enum ButtonState {
  131. Button1Pressed = 1,
  132. Button2Pressed = 4,
  133. Button3Pressed = 8,
  134. Button4Pressed = 16,
  135. RightmostButtonPressed = 2,
  136. WheeledUp = unchecked((int)0x780000),
  137. WheeledDown = unchecked((int)0xFF880000),
  138. }
  139. [Flags]
  140. public enum ControlKeyState {
  141. RightAltPressed = 1,
  142. LeftAltPressed = 2,
  143. RightControlPressed = 4,
  144. LeftControlPressed = 8,
  145. ShiftPressed = 16,
  146. NumlockOn = 32,
  147. ScrolllockOn = 64,
  148. CapslockOn = 128,
  149. EnhancedKey = 256
  150. }
  151. [Flags]
  152. public enum EventFlags {
  153. MouseMoved = 1,
  154. DoubleClick = 2,
  155. MouseWheeled = 4,
  156. MouseHorizontalWheeled = 8
  157. }
  158. [StructLayout (LayoutKind.Explicit)]
  159. public struct MouseEventRecord {
  160. [FieldOffset (0)]
  161. public Coordinate MousePosition;
  162. [FieldOffset (4)]
  163. public ButtonState ButtonState;
  164. [FieldOffset (8)]
  165. public ControlKeyState ControlKeyState;
  166. [FieldOffset (12)]
  167. public EventFlags EventFlags;
  168. public override string ToString ()
  169. {
  170. return $"[Mouse({MousePosition},{ButtonState},{ControlKeyState},{EventFlags}";
  171. }
  172. }
  173. [StructLayout (LayoutKind.Sequential)]
  174. public struct Coordinate {
  175. public short X;
  176. public short Y;
  177. public Coordinate (short X, short Y)
  178. {
  179. this.X = X;
  180. this.Y = Y;
  181. }
  182. public override string ToString () => $"({X},{Y})";
  183. };
  184. internal struct WindowBufferSizeRecord {
  185. public Coordinate size;
  186. public WindowBufferSizeRecord (short x, short y)
  187. {
  188. this.size = new Coordinate (x, y);
  189. }
  190. public override string ToString () => $"[WindowBufferSize{size}";
  191. }
  192. [StructLayout (LayoutKind.Sequential)]
  193. public struct MenuEventRecord {
  194. public uint dwCommandId;
  195. }
  196. [StructLayout (LayoutKind.Sequential)]
  197. public struct FocusEventRecord {
  198. public uint bSetFocus;
  199. }
  200. public enum EventType : ushort {
  201. Focus = 0x10,
  202. Key = 0x1,
  203. Menu = 0x8,
  204. Mouse = 2,
  205. WindowBufferSize = 4
  206. }
  207. [StructLayout (LayoutKind.Explicit)]
  208. public struct InputRecord {
  209. [FieldOffset (0)]
  210. public EventType EventType;
  211. [FieldOffset (4)]
  212. public KeyEventRecord KeyEvent;
  213. [FieldOffset (4)]
  214. public MouseEventRecord MouseEvent;
  215. [FieldOffset (4)]
  216. public WindowBufferSizeRecord WindowBufferSizeEvent;
  217. [FieldOffset (4)]
  218. public MenuEventRecord MenuEvent;
  219. [FieldOffset (4)]
  220. public FocusEventRecord FocusEvent;
  221. public override string ToString ()
  222. {
  223. switch (EventType) {
  224. case EventType.Focus:
  225. return FocusEvent.ToString ();
  226. case EventType.Key:
  227. return KeyEvent.ToString ();
  228. case EventType.Menu:
  229. return MenuEvent.ToString ();
  230. case EventType.Mouse:
  231. return MouseEvent.ToString ();
  232. case EventType.WindowBufferSize:
  233. return WindowBufferSizeEvent.ToString ();
  234. default:
  235. return "Unknown event type: " + EventType;
  236. }
  237. }
  238. };
  239. [Flags]
  240. enum ShareMode : uint {
  241. FileShareRead = 1,
  242. FileShareWrite = 2,
  243. }
  244. [Flags]
  245. enum DesiredAccess : uint {
  246. GenericRead = 2147483648,
  247. GenericWrite = 1073741824,
  248. }
  249. [StructLayout (LayoutKind.Sequential)]
  250. public struct ConsoleScreenBufferInfo {
  251. public Coord dwSize;
  252. public Coord dwCursorPosition;
  253. public ushort wAttributes;
  254. public SmallRect srWindow;
  255. public Coord dwMaximumWindowSize;
  256. }
  257. [StructLayout (LayoutKind.Sequential)]
  258. public struct Coord {
  259. public short X;
  260. public short Y;
  261. public Coord (short X, short Y)
  262. {
  263. this.X = X;
  264. this.Y = Y;
  265. }
  266. public override string ToString () => $"({X},{Y})";
  267. };
  268. [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
  269. public struct CharUnion {
  270. [FieldOffset (0)] public char UnicodeChar;
  271. [FieldOffset (0)] public byte AsciiChar;
  272. }
  273. [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
  274. public struct CharInfo {
  275. [FieldOffset (0)] public CharUnion Char;
  276. [FieldOffset (2)] public ushort Attributes;
  277. }
  278. [StructLayout (LayoutKind.Sequential)]
  279. public struct SmallRect {
  280. public short Left;
  281. public short Top;
  282. public short Right;
  283. public short Bottom;
  284. public static void MakeEmpty (ref SmallRect rect)
  285. {
  286. rect.Left = -1;
  287. }
  288. public static void Update (ref SmallRect rect, short col, short row)
  289. {
  290. if (rect.Left == -1) {
  291. //System.Diagnostics.Debugger.Log (0, "debug", $"damager From Empty {col},{row}\n");
  292. rect.Left = rect.Right = col;
  293. rect.Bottom = rect.Top = row;
  294. return;
  295. }
  296. if (col >= rect.Left && col <= rect.Right && row >= rect.Top && row <= rect.Bottom)
  297. return;
  298. if (col < rect.Left)
  299. rect.Left = col;
  300. if (col > rect.Right)
  301. rect.Right = col;
  302. if (row < rect.Top)
  303. rect.Top = row;
  304. if (row > rect.Bottom)
  305. rect.Bottom = row;
  306. //System.Diagnostics.Debugger.Log (0, "debug", $"Expanding {rect.ToString ()}\n");
  307. }
  308. public override string ToString ()
  309. {
  310. return $"Left={Left},Top={Top},Right={Right},Bottom={Bottom}";
  311. }
  312. }
  313. [DllImport ("kernel32.dll", SetLastError = true)]
  314. static extern IntPtr GetStdHandle (int nStdHandle);
  315. [DllImport ("kernel32.dll", SetLastError = true)]
  316. static extern bool CloseHandle (IntPtr handle);
  317. [DllImport ("kernel32.dll", EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
  318. public static extern bool ReadConsoleInput (
  319. IntPtr hConsoleInput,
  320. [Out] InputRecord [] lpBuffer,
  321. uint nLength,
  322. out uint lpNumberOfEventsRead);
  323. [DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  324. static extern bool ReadConsoleOutput (
  325. IntPtr hConsoleOutput,
  326. [Out] CharInfo [] lpBuffer,
  327. Coord dwBufferSize,
  328. Coord dwBufferCoord,
  329. ref SmallRect lpReadRegion
  330. );
  331. [DllImport ("kernel32.dll", EntryPoint = "WriteConsoleOutput", SetLastError = true, CharSet = CharSet.Unicode)]
  332. static extern bool WriteConsoleOutput (
  333. IntPtr hConsoleOutput,
  334. CharInfo [] lpBuffer,
  335. Coord dwBufferSize,
  336. Coord dwBufferCoord,
  337. ref SmallRect lpWriteRegion
  338. );
  339. [DllImport ("kernel32.dll")]
  340. static extern bool SetConsoleCursorPosition (IntPtr hConsoleOutput, Coord dwCursorPosition);
  341. [DllImport ("kernel32.dll")]
  342. static extern bool GetConsoleMode (IntPtr hConsoleHandle, out uint lpMode);
  343. [DllImport ("kernel32.dll")]
  344. static extern bool SetConsoleMode (IntPtr hConsoleHandle, uint dwMode);
  345. [DllImport ("kernel32.dll", SetLastError = true)]
  346. static extern IntPtr CreateConsoleScreenBuffer (
  347. DesiredAccess dwDesiredAccess,
  348. ShareMode dwShareMode,
  349. IntPtr secutiryAttributes,
  350. UInt32 flags,
  351. IntPtr screenBufferData
  352. );
  353. internal static IntPtr INVALID_HANDLE_VALUE = new IntPtr (-1);
  354. [DllImport ("kernel32.dll", SetLastError = true)]
  355. static extern bool SetConsoleActiveScreenBuffer (IntPtr Handle);
  356. [DllImport ("kernel32.dll", SetLastError = true)]
  357. static extern bool GetNumberOfConsoleInputEvents (IntPtr handle, out uint lpcNumberOfEvents);
  358. public uint InputEventCount {
  359. get {
  360. uint v;
  361. GetNumberOfConsoleInputEvents (InputHandle, out v);
  362. return v;
  363. }
  364. }
  365. }
  366. internal class WindowsDriver : ConsoleDriver, Mono.Terminal.IMainLoopDriver {
  367. static bool sync = false;
  368. ManualResetEventSlim eventReady = new ManualResetEventSlim (false);
  369. ManualResetEventSlim waitForProbe = new ManualResetEventSlim (false);
  370. MainLoop mainLoop;
  371. WindowsConsole.CharInfo [] OutputBuffer;
  372. int cols, rows;
  373. WindowsConsole winConsole;
  374. WindowsConsole.SmallRect damageRegion;
  375. public override int Cols => cols;
  376. public override int Rows => rows;
  377. public WindowsDriver ()
  378. {
  379. winConsole = new WindowsConsole ();
  380. SetupColorsAndBorders ();
  381. cols = Console.WindowWidth;
  382. rows = Console.WindowHeight;
  383. WindowsConsole.SmallRect.MakeEmpty (ref damageRegion);
  384. ResizeScreen ();
  385. UpdateOffScreen ();
  386. Task.Run ((Action)WindowsInputHandler);
  387. }
  388. private void SetupColorsAndBorders ()
  389. {
  390. Colors.TopLevel = new ColorScheme ();
  391. Colors.Base = new ColorScheme ();
  392. Colors.Dialog = new ColorScheme ();
  393. Colors.Menu = new ColorScheme ();
  394. Colors.Error = new ColorScheme ();
  395. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  396. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  397. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  398. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  399. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.DarkBlue);
  400. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  401. Colors.Base.HotNormal = MakeColor (ConsoleColor.DarkCyan, ConsoleColor.DarkBlue);
  402. Colors.Base.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  403. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.DarkGray);
  404. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  405. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.DarkGray);
  406. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  407. Colors.Menu.Disabled = MakeColor (ConsoleColor.Gray, ConsoleColor.DarkGray);
  408. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  409. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.DarkGray);
  410. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.Gray);
  411. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkGray);
  412. Colors.Error.Normal = MakeColor (ConsoleColor.DarkRed, ConsoleColor.White);
  413. Colors.Error.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkRed);
  414. Colors.Error.HotNormal = MakeColor (ConsoleColor.Black, ConsoleColor.White);
  415. Colors.Error.HotFocus = MakeColor (ConsoleColor.Black, ConsoleColor.DarkRed);
  416. HLine = '\u2500';
  417. VLine = '\u2502';
  418. Stipple = '\u2592';
  419. Diamond = '\u25c6';
  420. ULCorner = '\u250C';
  421. LLCorner = '\u2514';
  422. URCorner = '\u2510';
  423. LRCorner = '\u2518';
  424. LeftTee = '\u251c';
  425. RightTee = '\u2524';
  426. TopTee = '\u22a4';
  427. BottomTee = '\u22a5';
  428. }
  429. [StructLayout (LayoutKind.Sequential)]
  430. public struct ConsoleKeyInfoEx {
  431. public ConsoleKeyInfo consoleKeyInfo;
  432. public bool CapsLock;
  433. public bool NumLock;
  434. public ConsoleKeyInfoEx (ConsoleKeyInfo consoleKeyInfo, bool capslock, bool numlock)
  435. {
  436. this.consoleKeyInfo = consoleKeyInfo;
  437. CapsLock = capslock;
  438. NumLock = numlock;
  439. }
  440. }
  441. // The records that we keep fetching
  442. WindowsConsole.InputRecord [] result, records = new WindowsConsole.InputRecord [1];
  443. void WindowsInputHandler ()
  444. {
  445. while (true) {
  446. waitForProbe.Wait ();
  447. waitForProbe.Reset ();
  448. uint numberEventsRead = 0;
  449. WindowsConsole.ReadConsoleInput (winConsole.InputHandle, records, 1, out numberEventsRead);
  450. if (numberEventsRead == 0)
  451. result = null;
  452. else
  453. result = records;
  454. eventReady.Set ();
  455. }
  456. }
  457. void IMainLoopDriver.Setup (MainLoop mainLoop)
  458. {
  459. this.mainLoop = mainLoop;
  460. }
  461. void IMainLoopDriver.Wakeup ()
  462. {
  463. //tokenSource.Cancel ();
  464. eventReady.Reset ();
  465. eventReady.Set ();
  466. }
  467. bool IMainLoopDriver.EventsPending (bool wait)
  468. {
  469. long now = DateTime.UtcNow.Ticks;
  470. int waitTimeout;
  471. if (mainLoop.timeouts.Count > 0) {
  472. waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
  473. if (waitTimeout < 0)
  474. return true;
  475. } else
  476. waitTimeout = -1;
  477. if (!wait)
  478. waitTimeout = 0;
  479. result = null;
  480. waitForProbe.Set ();
  481. try {
  482. if (!tokenSource.IsCancellationRequested)
  483. eventReady.Wait (waitTimeout, tokenSource.Token);
  484. } catch (OperationCanceledException) {
  485. return true;
  486. } finally {
  487. eventReady.Reset ();
  488. }
  489. if (!tokenSource.IsCancellationRequested)
  490. return result != null;
  491. tokenSource.Dispose ();
  492. tokenSource = new CancellationTokenSource ();
  493. return true;
  494. }
  495. Action<KeyEvent> keyHandler;
  496. Action<KeyEvent> keyDownHandler;
  497. Action<KeyEvent> keyUpHandler;
  498. Action<MouseEvent> mouseHandler;
  499. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  500. {
  501. this.keyHandler = keyHandler;
  502. this.keyDownHandler = keyDownHandler;
  503. this.keyUpHandler = keyUpHandler;
  504. this.mouseHandler = mouseHandler;
  505. }
  506. void IMainLoopDriver.MainIteration ()
  507. {
  508. if (result == null)
  509. return;
  510. var inputEvent = result [0];
  511. switch (inputEvent.EventType) {
  512. case WindowsConsole.EventType.Key:
  513. var map = MapKey (ToConsoleKeyInfoEx (inputEvent.KeyEvent));
  514. if (map == (Key)0xffffffff) {
  515. KeyEvent key = new KeyEvent ();
  516. // Shift = VK_SHIFT = 0x10
  517. // Ctrl = VK_CONTROL = 0x11
  518. // Alt = VK_MENU = 0x12
  519. if (inputEvent.KeyEvent.dwControlKeyState.HasFlag (WindowsConsole.ControlKeyState.CapslockOn)) {
  520. inputEvent.KeyEvent.dwControlKeyState &= ~WindowsConsole.ControlKeyState.CapslockOn;
  521. }
  522. if (inputEvent.KeyEvent.dwControlKeyState.HasFlag (WindowsConsole.ControlKeyState.ScrolllockOn)) {
  523. inputEvent.KeyEvent.dwControlKeyState &= ~WindowsConsole.ControlKeyState.ScrolllockOn;
  524. }
  525. if (inputEvent.KeyEvent.dwControlKeyState.HasFlag (WindowsConsole.ControlKeyState.NumlockOn)) {
  526. inputEvent.KeyEvent.dwControlKeyState &= ~WindowsConsole.ControlKeyState.NumlockOn;
  527. }
  528. switch (inputEvent.KeyEvent.dwControlKeyState) {
  529. case WindowsConsole.ControlKeyState.RightAltPressed:
  530. case WindowsConsole.ControlKeyState.RightAltPressed |
  531. WindowsConsole.ControlKeyState.LeftControlPressed |
  532. WindowsConsole.ControlKeyState.EnhancedKey:
  533. case WindowsConsole.ControlKeyState.EnhancedKey:
  534. key = new KeyEvent (Key.CtrlMask | Key.AltMask);
  535. break;
  536. case WindowsConsole.ControlKeyState.LeftAltPressed:
  537. key = new KeyEvent (Key.AltMask);
  538. break;
  539. case WindowsConsole.ControlKeyState.RightControlPressed:
  540. case WindowsConsole.ControlKeyState.LeftControlPressed:
  541. key = new KeyEvent (Key.CtrlMask);
  542. break;
  543. case WindowsConsole.ControlKeyState.ShiftPressed:
  544. key = new KeyEvent (Key.ShiftMask);
  545. break;
  546. case WindowsConsole.ControlKeyState.NumlockOn:
  547. break;
  548. case WindowsConsole.ControlKeyState.ScrolllockOn:
  549. break;
  550. case WindowsConsole.ControlKeyState.CapslockOn:
  551. break;
  552. default:
  553. switch (inputEvent.KeyEvent.wVirtualKeyCode) {
  554. case 0x10:
  555. key = new KeyEvent (Key.ShiftMask);
  556. break;
  557. case 0x11:
  558. key = new KeyEvent (Key.CtrlMask);
  559. break;
  560. case 0x12:
  561. key = new KeyEvent (Key.AltMask);
  562. break;
  563. default:
  564. key = new KeyEvent (Key.Unknown);
  565. break;
  566. }
  567. break;
  568. }
  569. if (inputEvent.KeyEvent.bKeyDown)
  570. keyDownHandler (key);
  571. else
  572. keyUpHandler (key);
  573. } else {
  574. if (inputEvent.KeyEvent.bKeyDown) {
  575. // Key Down - Fire KeyDown Event and KeyStroke (ProcessKey) Event
  576. keyHandler (new KeyEvent (map));
  577. keyDownHandler (new KeyEvent (map));
  578. } else {
  579. // Key Up - Fire KeyDown Event and KeyStroke (ProcessKey) Event
  580. keyHandler (new KeyEvent (map));
  581. keyUpHandler (new KeyEvent (map));
  582. }
  583. }
  584. break;
  585. case WindowsConsole.EventType.Mouse:
  586. mouseHandler (ToDriverMouse (inputEvent.MouseEvent));
  587. if (IsButtonReleased)
  588. mouseHandler (ToDriverMouse (inputEvent.MouseEvent));
  589. break;
  590. case WindowsConsole.EventType.WindowBufferSize:
  591. cols = inputEvent.WindowBufferSizeEvent.size.X;
  592. rows = inputEvent.WindowBufferSizeEvent.size.Y;
  593. ResizeScreen ();
  594. UpdateOffScreen ();
  595. TerminalResized?.Invoke ();
  596. break;
  597. }
  598. result = null;
  599. }
  600. WindowsConsole.ButtonState? LastMouseButtonPressed = null;
  601. bool IsButtonPressed = false;
  602. bool IsButtonReleased = false;
  603. bool IsButtonDoubleClicked = false;
  604. Point point;
  605. MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
  606. {
  607. MouseFlags mouseFlag = MouseFlags.AllEvents;
  608. if (IsButtonDoubleClicked) {
  609. Task.Run (async () => {
  610. await Task.Delay (100);
  611. IsButtonDoubleClicked = false;
  612. });
  613. }
  614. // The ButtonState member of the MouseEvent structure has bit corresponding to each mouse button.
  615. // This will tell when a mouse button is pressed. When the button is released this event will
  616. // be fired with it's bit set to 0. So when the button is up ButtonState will be 0.
  617. // To map to the correct driver events we save the last pressed mouse button so we can
  618. // map to the correct clicked event.
  619. if ((LastMouseButtonPressed != null || IsButtonReleased) && mouseEvent.ButtonState != 0) {
  620. LastMouseButtonPressed = null;
  621. IsButtonPressed = false;
  622. IsButtonReleased = false;
  623. }
  624. if ((mouseEvent.EventFlags == 0 && LastMouseButtonPressed == null && !IsButtonDoubleClicked) ||
  625. (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved &&
  626. mouseEvent.ButtonState != 0 && !IsButtonReleased && !IsButtonDoubleClicked)) {
  627. switch (mouseEvent.ButtonState) {
  628. case WindowsConsole.ButtonState.Button1Pressed:
  629. mouseFlag = MouseFlags.Button1Pressed;
  630. break;
  631. case WindowsConsole.ButtonState.Button2Pressed:
  632. mouseFlag = MouseFlags.Button2Pressed;
  633. break;
  634. case WindowsConsole.ButtonState.RightmostButtonPressed:
  635. mouseFlag = MouseFlags.Button3Pressed;
  636. break;
  637. }
  638. if (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved) {
  639. mouseFlag |= MouseFlags.ReportMousePosition;
  640. point = new Point ();
  641. IsButtonReleased = false;
  642. } else {
  643. point = new Point () {
  644. X = mouseEvent.MousePosition.X,
  645. Y = mouseEvent.MousePosition.Y
  646. };
  647. }
  648. LastMouseButtonPressed = mouseEvent.ButtonState;
  649. IsButtonPressed = true;
  650. if ((mouseFlag & MouseFlags.ReportMousePosition) == 0) {
  651. Task.Run (async () => {
  652. while (IsButtonPressed) {
  653. await Task.Delay (200);
  654. var me = new MouseEvent () {
  655. X = mouseEvent.MousePosition.X,
  656. Y = mouseEvent.MousePosition.Y,
  657. Flags = mouseFlag
  658. };
  659. var view = Application.wantContinuousButtonPressedView;
  660. if (view == null)
  661. break;
  662. if (IsButtonPressed && (mouseFlag & MouseFlags.ReportMousePosition) == 0) {
  663. mouseHandler (me);
  664. mainLoop.Driver.Wakeup ();
  665. }
  666. }
  667. });
  668. }
  669. } else if ((mouseEvent.EventFlags == 0 || mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved) &&
  670. LastMouseButtonPressed != null && !IsButtonReleased && !IsButtonDoubleClicked) {
  671. switch (LastMouseButtonPressed) {
  672. case WindowsConsole.ButtonState.Button1Pressed:
  673. mouseFlag = MouseFlags.Button1Released;
  674. break;
  675. case WindowsConsole.ButtonState.Button2Pressed:
  676. mouseFlag = MouseFlags.Button2Released;
  677. break;
  678. case WindowsConsole.ButtonState.RightmostButtonPressed:
  679. mouseFlag = MouseFlags.Button3Released;
  680. break;
  681. }
  682. IsButtonPressed = false;
  683. IsButtonReleased = true;
  684. } else if ((mouseEvent.EventFlags == 0 || mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved) &&
  685. IsButtonReleased) {
  686. var p = new Point () {
  687. X = mouseEvent.MousePosition.X,
  688. Y = mouseEvent.MousePosition.Y
  689. };
  690. if (p == point) {
  691. switch (LastMouseButtonPressed) {
  692. case WindowsConsole.ButtonState.Button1Pressed:
  693. mouseFlag = MouseFlags.Button1Clicked;
  694. break;
  695. case WindowsConsole.ButtonState.Button2Pressed:
  696. mouseFlag = MouseFlags.Button2Clicked;
  697. break;
  698. case WindowsConsole.ButtonState.RightmostButtonPressed:
  699. mouseFlag = MouseFlags.Button3Clicked;
  700. break;
  701. }
  702. } else {
  703. mouseFlag = 0;
  704. }
  705. LastMouseButtonPressed = null;
  706. IsButtonReleased = false;
  707. } else if (mouseEvent.EventFlags.HasFlag (WindowsConsole.EventFlags.DoubleClick)) {
  708. switch (mouseEvent.ButtonState) {
  709. case WindowsConsole.ButtonState.Button1Pressed:
  710. mouseFlag = MouseFlags.Button1DoubleClicked;
  711. break;
  712. case WindowsConsole.ButtonState.Button2Pressed:
  713. mouseFlag = MouseFlags.Button2DoubleClicked;
  714. break;
  715. case WindowsConsole.ButtonState.RightmostButtonPressed:
  716. mouseFlag = MouseFlags.Button3DoubleClicked;
  717. break;
  718. }
  719. IsButtonDoubleClicked = true;
  720. } else if (mouseEvent.EventFlags == 0 && mouseEvent.ButtonState != 0 && IsButtonDoubleClicked) {
  721. switch (mouseEvent.ButtonState) {
  722. case WindowsConsole.ButtonState.Button1Pressed:
  723. mouseFlag = MouseFlags.Button1TripleClicked;
  724. break;
  725. case WindowsConsole.ButtonState.Button2Pressed:
  726. mouseFlag = MouseFlags.Button2TripleClicked;
  727. break;
  728. case WindowsConsole.ButtonState.RightmostButtonPressed:
  729. mouseFlag = MouseFlags.Button3TripleClicked;
  730. break;
  731. }
  732. IsButtonDoubleClicked = false;
  733. } else if (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseWheeled) {
  734. switch (mouseEvent.ButtonState) {
  735. case WindowsConsole.ButtonState.WheeledUp:
  736. mouseFlag = MouseFlags.WheeledUp;
  737. break;
  738. case WindowsConsole.ButtonState.WheeledDown:
  739. mouseFlag = MouseFlags.WheeledDown;
  740. break;
  741. }
  742. } else if (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved) {
  743. mouseFlag = MouseFlags.ReportMousePosition;
  744. } else if (mouseEvent.ButtonState == 0 && mouseEvent.EventFlags == 0) {
  745. mouseFlag = 0;
  746. }
  747. mouseFlag = SetControlKeyStates (mouseEvent, mouseFlag);
  748. return new MouseEvent () {
  749. X = mouseEvent.MousePosition.X,
  750. Y = mouseEvent.MousePosition.Y,
  751. Flags = mouseFlag
  752. };
  753. }
  754. static MouseFlags SetControlKeyStates (WindowsConsole.MouseEventRecord mouseEvent, MouseFlags mouseFlag)
  755. {
  756. if (mouseEvent.ControlKeyState.HasFlag (WindowsConsole.ControlKeyState.RightControlPressed) ||
  757. mouseEvent.ControlKeyState.HasFlag (WindowsConsole.ControlKeyState.LeftControlPressed))
  758. mouseFlag |= MouseFlags.ButtonCtrl;
  759. if (mouseEvent.ControlKeyState.HasFlag (WindowsConsole.ControlKeyState.ShiftPressed))
  760. mouseFlag |= MouseFlags.ButtonShift;
  761. if (mouseEvent.ControlKeyState.HasFlag (WindowsConsole.ControlKeyState.RightAltPressed) ||
  762. mouseEvent.ControlKeyState.HasFlag (WindowsConsole.ControlKeyState.LeftAltPressed))
  763. mouseFlag |= MouseFlags.ButtonAlt;
  764. return mouseFlag;
  765. }
  766. public ConsoleKeyInfoEx ToConsoleKeyInfoEx (WindowsConsole.KeyEventRecord keyEvent)
  767. {
  768. var state = keyEvent.dwControlKeyState;
  769. bool shift = (state & WindowsConsole.ControlKeyState.ShiftPressed) != 0;
  770. bool alt = (state & (WindowsConsole.ControlKeyState.LeftAltPressed | WindowsConsole.ControlKeyState.RightAltPressed)) != 0;
  771. bool control = (state & (WindowsConsole.ControlKeyState.LeftControlPressed | WindowsConsole.ControlKeyState.RightControlPressed)) != 0;
  772. bool capslock = (state & (WindowsConsole.ControlKeyState.CapslockOn)) != 0;
  773. bool numlock = (state & (WindowsConsole.ControlKeyState.NumlockOn)) != 0;
  774. var ConsoleKeyInfo = new ConsoleKeyInfo (keyEvent.UnicodeChar, (ConsoleKey)keyEvent.wVirtualKeyCode, shift, alt, control);
  775. return new ConsoleKeyInfoEx (ConsoleKeyInfo, capslock, numlock);
  776. }
  777. public Key MapKey (ConsoleKeyInfoEx keyInfoEx)
  778. {
  779. var keyInfo = keyInfoEx.consoleKeyInfo;
  780. switch (keyInfo.Key) {
  781. case ConsoleKey.Escape:
  782. return MapKeyModifiers (keyInfo, Key.Esc);
  783. case ConsoleKey.Tab:
  784. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  785. case ConsoleKey.Home:
  786. return MapKeyModifiers (keyInfo, Key.Home);
  787. case ConsoleKey.End:
  788. return MapKeyModifiers (keyInfo, Key.End);
  789. case ConsoleKey.LeftArrow:
  790. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  791. case ConsoleKey.RightArrow:
  792. return MapKeyModifiers (keyInfo, Key.CursorRight);
  793. case ConsoleKey.UpArrow:
  794. return MapKeyModifiers (keyInfo, Key.CursorUp);
  795. case ConsoleKey.DownArrow:
  796. return MapKeyModifiers (keyInfo, Key.CursorDown);
  797. case ConsoleKey.PageUp:
  798. return MapKeyModifiers (keyInfo, Key.PageUp);
  799. case ConsoleKey.PageDown:
  800. return MapKeyModifiers (keyInfo, Key.PageDown);
  801. case ConsoleKey.Enter:
  802. return MapKeyModifiers (keyInfo, Key.Enter);
  803. case ConsoleKey.Spacebar:
  804. return MapKeyModifiers (keyInfo, Key.Space);
  805. case ConsoleKey.Backspace:
  806. return MapKeyModifiers (keyInfo, Key.Backspace);
  807. case ConsoleKey.Delete:
  808. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  809. case ConsoleKey.Insert:
  810. return MapKeyModifiers (keyInfo, Key.InsertChar);
  811. case ConsoleKey.NumPad0:
  812. return keyInfoEx.NumLock ? (Key)(uint)'0' : Key.InsertChar;
  813. case ConsoleKey.NumPad1:
  814. return keyInfoEx.NumLock ? (Key)(uint)'1' : Key.End;
  815. case ConsoleKey.NumPad2:
  816. return keyInfoEx.NumLock ? (Key)(uint)'2' : Key.CursorDown;
  817. case ConsoleKey.NumPad3:
  818. return keyInfoEx.NumLock ? (Key)(uint)'3' : Key.PageDown;
  819. case ConsoleKey.NumPad4:
  820. return keyInfoEx.NumLock ? (Key)(uint)'4' : Key.CursorLeft;
  821. case ConsoleKey.NumPad5:
  822. return keyInfoEx.NumLock ? (Key)(uint)'5' : (Key)((uint)keyInfo.KeyChar);
  823. case ConsoleKey.NumPad6:
  824. return keyInfoEx.NumLock ? (Key)(uint)'6' : Key.CursorRight;
  825. case ConsoleKey.NumPad7:
  826. return keyInfoEx.NumLock ? (Key)(uint)'7' : Key.Home;
  827. case ConsoleKey.NumPad8:
  828. return keyInfoEx.NumLock ? (Key)(uint)'8' : Key.CursorUp;
  829. case ConsoleKey.NumPad9:
  830. return keyInfoEx.NumLock ? (Key)(uint)'9' : Key.PageUp;
  831. case ConsoleKey.Oem1:
  832. case ConsoleKey.Oem2:
  833. case ConsoleKey.Oem3:
  834. case ConsoleKey.Oem4:
  835. case ConsoleKey.Oem5:
  836. case ConsoleKey.Oem6:
  837. case ConsoleKey.Oem7:
  838. case ConsoleKey.Oem8:
  839. case ConsoleKey.Oem102:
  840. case ConsoleKey.OemPeriod:
  841. case ConsoleKey.OemComma:
  842. case ConsoleKey.OemPlus:
  843. case ConsoleKey.OemMinus:
  844. if (keyInfo.KeyChar == 0)
  845. return Key.Unknown;
  846. return (Key)((uint)keyInfo.KeyChar);
  847. }
  848. var key = keyInfo.Key;
  849. //var alphaBase = ((keyInfo.Modifiers == ConsoleModifiers.Shift) ^ (keyInfoEx.CapsLock)) ? 'A' : 'a';
  850. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  851. var delta = key - ConsoleKey.A;
  852. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  853. return (Key)((uint)Key.ControlA + delta);
  854. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  855. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  856. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  857. if (keyInfo.KeyChar == 0)
  858. return (Key)(((uint)Key.AltMask) + ((uint)Key.ControlA + delta));
  859. else
  860. return (Key)((uint)keyInfo.KeyChar);
  861. }
  862. //return (Key)((uint)alphaBase + delta);
  863. return (Key)((uint)keyInfo.KeyChar);
  864. }
  865. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  866. var delta = key - ConsoleKey.D0;
  867. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  868. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  869. return (Key)((uint)keyInfo.KeyChar);
  870. }
  871. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  872. var delta = key - ConsoleKey.F1;
  873. return (Key)((int)Key.F1 + delta);
  874. }
  875. return (Key)(0xffffffff);
  876. }
  877. private static Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  878. {
  879. Key keyMod = new Key ();
  880. if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift))
  881. keyMod = Key.ShiftMask;
  882. if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control))
  883. keyMod |= Key.CtrlMask;
  884. if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt))
  885. keyMod |= Key.AltMask;
  886. return keyMod != Key.ControlSpace ? keyMod | key : key;
  887. }
  888. public override void Init (Action terminalResized)
  889. {
  890. TerminalResized = terminalResized;
  891. SetupColorsAndBorders ();
  892. }
  893. void ResizeScreen ()
  894. {
  895. OutputBuffer = new WindowsConsole.CharInfo [Rows * Cols];
  896. Clip = new Rect (0, 0, Cols, Rows);
  897. damageRegion = new WindowsConsole.SmallRect () {
  898. Top = 0,
  899. Left = 0,
  900. Bottom = (short)Rows,
  901. Right = (short)Cols
  902. };
  903. }
  904. void UpdateOffScreen ()
  905. {
  906. for (int row = 0; row < rows; row++)
  907. for (int col = 0; col < cols; col++) {
  908. int position = row * cols + col;
  909. OutputBuffer [position].Attributes = (ushort)Colors.TopLevel.Normal;
  910. OutputBuffer [position].Char.UnicodeChar = ' ';
  911. }
  912. }
  913. int ccol, crow;
  914. public override void Move (int col, int row)
  915. {
  916. ccol = col;
  917. crow = row;
  918. }
  919. public override void AddRune (Rune rune)
  920. {
  921. var position = crow * Cols + ccol;
  922. if (Clip.Contains (ccol, crow)) {
  923. OutputBuffer [position].Attributes = (ushort)currentAttribute;
  924. OutputBuffer [position].Char.UnicodeChar = (char)rune;
  925. WindowsConsole.SmallRect.Update (ref damageRegion, (short)ccol, (short)crow);
  926. }
  927. ccol++;
  928. var runeWidth = Rune.ColumnWidth (rune);
  929. if (runeWidth > 1) {
  930. for (int i = 1; i < runeWidth; i++) {
  931. AddStr (" ");
  932. }
  933. }
  934. //if (ccol == Cols) {
  935. // ccol = 0;
  936. // if (crow + 1 < Rows)
  937. // crow++;
  938. //}
  939. if (sync)
  940. UpdateScreen ();
  941. }
  942. public override void AddStr (ustring str)
  943. {
  944. foreach (var rune in str)
  945. AddRune (rune);
  946. }
  947. int currentAttribute;
  948. CancellationTokenSource tokenSource = new CancellationTokenSource ();
  949. public override void SetAttribute (Attribute c)
  950. {
  951. currentAttribute = c.value;
  952. }
  953. Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  954. {
  955. // Encode the colors into the int value.
  956. return new Attribute () {
  957. value = ((int)f | (int)b << 4),
  958. foreground = (Color)f,
  959. background = (Color)b
  960. };
  961. }
  962. public override Attribute MakeAttribute (Color fore, Color back)
  963. {
  964. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  965. }
  966. public override void Refresh ()
  967. {
  968. UpdateScreen ();
  969. #if false
  970. var bufferCoords = new WindowsConsole.Coord (){
  971. X = (short)Clip.Width,
  972. Y = (short)Clip.Height
  973. };
  974. var window = new WindowsConsole.SmallRect (){
  975. Top = 0,
  976. Left = 0,
  977. Right = (short)Clip.Right,
  978. Bottom = (short)Clip.Bottom
  979. };
  980. UpdateCursor();
  981. winConsole.WriteToConsole (OutputBuffer, bufferCoords, window);
  982. #endif
  983. }
  984. public override void UpdateScreen ()
  985. {
  986. if (damageRegion.Left == -1)
  987. return;
  988. var bufferCoords = new WindowsConsole.Coord () {
  989. X = (short)Clip.Width,
  990. Y = (short)Clip.Height
  991. };
  992. var window = new WindowsConsole.SmallRect () {
  993. Top = 0,
  994. Left = 0,
  995. Right = (short)Clip.Right,
  996. Bottom = (short)Clip.Bottom
  997. };
  998. UpdateCursor ();
  999. winConsole.WriteToConsole (OutputBuffer, bufferCoords, damageRegion);
  1000. // System.Diagnostics.Debugger.Log(0, "debug", $"Region={damageRegion.Right - damageRegion.Left},{damageRegion.Bottom - damageRegion.Top}\n");
  1001. WindowsConsole.SmallRect.MakeEmpty (ref damageRegion);
  1002. }
  1003. public override void UpdateCursor ()
  1004. {
  1005. var position = new WindowsConsole.Coord () {
  1006. X = (short)ccol,
  1007. Y = (short)crow
  1008. };
  1009. winConsole.SetCursorPosition (position);
  1010. }
  1011. public override void End ()
  1012. {
  1013. winConsole.Cleanup ();
  1014. }
  1015. #region Unused
  1016. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  1017. {
  1018. }
  1019. public override void SetColors (short foregroundColorId, short backgroundColorId)
  1020. {
  1021. }
  1022. public override void Suspend ()
  1023. {
  1024. }
  1025. public override void StartReportingMouseMoves ()
  1026. {
  1027. }
  1028. public override void StopReportingMouseMoves ()
  1029. {
  1030. }
  1031. public override void UncookMouse ()
  1032. {
  1033. }
  1034. public override void CookMouse ()
  1035. {
  1036. }
  1037. #endregion
  1038. }
  1039. }