NetDriver.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. #nullable enable
  2. //
  3. // NetDriver.cs: The System.Console-based .NET driver, works on Windows and Unix, but is not particularly efficient.
  4. //
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using static Terminal.Gui.NetEvents;
  8. namespace Terminal.Gui;
  9. internal class NetDriver : ConsoleDriver
  10. {
  11. public bool IsWinPlatform { get; private set; }
  12. public NetWinVTConsole? NetWinConsole { get; private set; }
  13. public override void Suspend ()
  14. {
  15. if (Environment.OSVersion.Platform != PlatformID.Unix)
  16. {
  17. return;
  18. }
  19. StopReportingMouseMoves ();
  20. if (!RunningUnitTests)
  21. {
  22. Console.ResetColor ();
  23. Console.Clear ();
  24. //Disable alternative screen buffer.
  25. Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  26. //Set cursor key to cursor.
  27. Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
  28. Platform.Suspend ();
  29. //Enable alternative screen buffer.
  30. Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  31. SetContentsAsDirty ();
  32. Refresh ();
  33. }
  34. StartReportingMouseMoves ();
  35. }
  36. public override bool UpdateScreen ()
  37. {
  38. bool updated = false;
  39. if (RunningUnitTests
  40. || _winSizeChanging
  41. || Console.WindowHeight < 1
  42. || Contents?.Length != Rows * Cols
  43. || Rows != Console.WindowHeight)
  44. {
  45. return updated;
  46. }
  47. var top = 0;
  48. var left = 0;
  49. int rows = Rows;
  50. int cols = Cols;
  51. var output = new StringBuilder ();
  52. Attribute? redrawAttr = null;
  53. int lastCol = -1;
  54. CursorVisibility? savedVisibility = _cachedCursorVisibility;
  55. SetCursorVisibility (CursorVisibility.Invisible);
  56. for (int row = top; row < rows; row++)
  57. {
  58. if (Console.WindowHeight < 1)
  59. {
  60. return updated;
  61. }
  62. if (!_dirtyLines! [row])
  63. {
  64. continue;
  65. }
  66. if (!SetCursorPosition (0, row))
  67. {
  68. return updated;
  69. }
  70. updated = true;
  71. _dirtyLines [row] = false;
  72. output.Clear ();
  73. for (int col = left; col < cols; col++)
  74. {
  75. lastCol = -1;
  76. var outputWidth = 0;
  77. for (; col < cols; col++)
  78. {
  79. if (!Contents [row, col].IsDirty)
  80. {
  81. if (output.Length > 0)
  82. {
  83. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  84. }
  85. else if (lastCol == -1)
  86. {
  87. lastCol = col;
  88. }
  89. if (lastCol + 1 < cols)
  90. {
  91. lastCol++;
  92. }
  93. continue;
  94. }
  95. if (lastCol == -1)
  96. {
  97. lastCol = col;
  98. }
  99. Attribute attr = Contents [row, col].Attribute!.Value;
  100. // Performance: Only send the escape sequence if the attribute has changed.
  101. if (attr != redrawAttr)
  102. {
  103. redrawAttr = attr;
  104. if (Force16Colors)
  105. {
  106. output.Append (
  107. EscSeqUtils.CSI_SetGraphicsRendition (
  108. MapColors (
  109. (ConsoleColor)attr.Background
  110. .GetClosestNamedColor16 (),
  111. false
  112. ),
  113. MapColors (
  114. (ConsoleColor)attr.Foreground
  115. .GetClosestNamedColor16 ())
  116. )
  117. );
  118. }
  119. else
  120. {
  121. output.Append (
  122. EscSeqUtils.CSI_SetForegroundColorRGB (
  123. attr.Foreground.R,
  124. attr.Foreground.G,
  125. attr.Foreground.B
  126. )
  127. );
  128. output.Append (
  129. EscSeqUtils.CSI_SetBackgroundColorRGB (
  130. attr.Background.R,
  131. attr.Background.G,
  132. attr.Background.B
  133. )
  134. );
  135. }
  136. }
  137. outputWidth++;
  138. Rune rune = Contents [row, col].Rune;
  139. output.Append (rune);
  140. if (Contents [row, col].CombiningMarks.Count > 0)
  141. {
  142. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  143. // compatible with the driver architecture. Any CMs (except in the first col)
  144. // are correctly combined with the base char, but are ALSO treated as 1 column
  145. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  146. //
  147. // For now, we just ignore the list of CMs.
  148. //foreach (var combMark in Contents [row, col].CombiningMarks) {
  149. // output.Append (combMark);
  150. //}
  151. // WriteToConsole (output, ref lastCol, row, ref outputWidth);
  152. }
  153. else if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  154. {
  155. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  156. SetCursorPosition (col - 1, row);
  157. }
  158. Contents [row, col].IsDirty = false;
  159. }
  160. }
  161. if (output.Length > 0)
  162. {
  163. SetCursorPosition (lastCol, row);
  164. Console.Write (output);
  165. }
  166. foreach (var s in Application.Sixel)
  167. {
  168. if (!string.IsNullOrWhiteSpace (s.SixelData))
  169. {
  170. SetCursorPosition (s.ScreenPosition.X, s.ScreenPosition.Y);
  171. Console.Write (s.SixelData);
  172. }
  173. }
  174. }
  175. SetCursorPosition (0, 0);
  176. _cachedCursorVisibility = savedVisibility;
  177. void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  178. {
  179. SetCursorPosition (lastCol, row);
  180. Console.Write (output);
  181. output.Clear ();
  182. lastCol += outputWidth;
  183. outputWidth = 0;
  184. }
  185. return updated;
  186. }
  187. #region Init/End/MainLoop
  188. internal NetMainLoop? _mainLoopDriver;
  189. public override MainLoop Init ()
  190. {
  191. PlatformID p = Environment.OSVersion.Platform;
  192. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  193. {
  194. IsWinPlatform = true;
  195. try
  196. {
  197. NetWinConsole = new ();
  198. }
  199. catch (ApplicationException)
  200. {
  201. // Likely running as a unit test, or in a non-interactive session.
  202. }
  203. }
  204. if (IsWinPlatform)
  205. {
  206. Clipboard = new WindowsClipboard ();
  207. }
  208. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  209. {
  210. Clipboard = new MacOSXClipboard ();
  211. }
  212. else
  213. {
  214. if (CursesDriver.Is_WSL_Platform ())
  215. {
  216. Clipboard = new WSLClipboard ();
  217. }
  218. else
  219. {
  220. Clipboard = new CursesClipboard ();
  221. }
  222. }
  223. if (!RunningUnitTests)
  224. {
  225. Console.TreatControlCAsInput = true;
  226. Cols = Console.WindowWidth;
  227. Rows = Console.WindowHeight;
  228. //Enable alternative screen buffer.
  229. Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  230. //Set cursor key to application.
  231. Console.Out.Write (EscSeqUtils.CSI_HideCursor);
  232. }
  233. else
  234. {
  235. // We are being run in an environment that does not support a console
  236. // such as a unit test, or a pipe.
  237. Cols = 80;
  238. Rows = 24;
  239. }
  240. ResizeScreen ();
  241. ClearContents ();
  242. CurrentAttribute = new (Color.White, Color.Black);
  243. StartReportingMouseMoves ();
  244. _mainLoopDriver = new (this);
  245. _mainLoopDriver.ProcessInput = ProcessInput;
  246. return new (_mainLoopDriver);
  247. }
  248. private void ProcessInput (InputResult inputEvent)
  249. {
  250. switch (inputEvent.EventType)
  251. {
  252. case EventType.Key:
  253. ConsoleKeyInfo consoleKeyInfo = inputEvent.ConsoleKeyInfo;
  254. //if (consoleKeyInfo.Key == ConsoleKey.Packet) {
  255. // consoleKeyInfo = FromVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  256. //}
  257. //Debug.WriteLine ($"event: {inputEvent}");
  258. KeyCode map = EscSeqUtils.MapKey (consoleKeyInfo);
  259. if (map == KeyCode.Null)
  260. {
  261. break;
  262. }
  263. OnKeyDown (new (map));
  264. OnKeyUp (new (map));
  265. break;
  266. case EventType.Mouse:
  267. MouseEventArgs me = ToDriverMouse (inputEvent.MouseEvent);
  268. //Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
  269. OnMouseEvent (me);
  270. break;
  271. case EventType.WindowSize:
  272. _winSizeChanging = true;
  273. Top = 0;
  274. Left = 0;
  275. Cols = inputEvent.WindowSizeEvent.Size.Width;
  276. Rows = Math.Max (inputEvent.WindowSizeEvent.Size.Height, 0);
  277. ResizeScreen ();
  278. ClearContents ();
  279. _winSizeChanging = false;
  280. OnSizeChanged (new (new (Cols, Rows)));
  281. break;
  282. case EventType.RequestResponse:
  283. break;
  284. case EventType.WindowPosition:
  285. break;
  286. default:
  287. throw new ArgumentOutOfRangeException ();
  288. }
  289. }
  290. public override void End ()
  291. {
  292. if (IsWinPlatform)
  293. {
  294. NetWinConsole?.Cleanup ();
  295. }
  296. StopReportingMouseMoves ();
  297. if (!RunningUnitTests)
  298. {
  299. Console.ResetColor ();
  300. //Disable alternative screen buffer.
  301. Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  302. //Set cursor key to cursor.
  303. Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
  304. Console.Out.Close ();
  305. }
  306. }
  307. #endregion Init/End/MainLoop
  308. #region Color Handling
  309. public override bool SupportsTrueColor => Environment.OSVersion.Platform == PlatformID.Unix
  310. || (IsWinPlatform && Environment.OSVersion.Version.Build >= 14931);
  311. private const int COLOR_BLACK = 30;
  312. private const int COLOR_BLUE = 34;
  313. private const int COLOR_BRIGHT_BLACK = 90;
  314. private const int COLOR_BRIGHT_BLUE = 94;
  315. private const int COLOR_BRIGHT_CYAN = 96;
  316. private const int COLOR_BRIGHT_GREEN = 92;
  317. private const int COLOR_BRIGHT_MAGENTA = 95;
  318. private const int COLOR_BRIGHT_RED = 91;
  319. private const int COLOR_BRIGHT_WHITE = 97;
  320. private const int COLOR_BRIGHT_YELLOW = 93;
  321. private const int COLOR_CYAN = 36;
  322. private const int COLOR_GREEN = 32;
  323. private const int COLOR_MAGENTA = 35;
  324. private const int COLOR_RED = 31;
  325. private const int COLOR_WHITE = 37;
  326. private const int COLOR_YELLOW = 33;
  327. //// Cache the list of ConsoleColor values.
  328. //[UnconditionalSuppressMessage (
  329. // "AOT",
  330. // "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
  331. // Justification = "<Pending>")]
  332. //private static readonly HashSet<int> ConsoleColorValues = new (
  333. // Enum.GetValues (typeof (ConsoleColor))
  334. // .OfType<ConsoleColor> ()
  335. // .Select (c => (int)c)
  336. // );
  337. // Dictionary for mapping ConsoleColor values to the values used by System.Net.Console.
  338. private static readonly Dictionary<ConsoleColor, int> _colorMap = new ()
  339. {
  340. { ConsoleColor.Black, COLOR_BLACK },
  341. { ConsoleColor.DarkBlue, COLOR_BLUE },
  342. { ConsoleColor.DarkGreen, COLOR_GREEN },
  343. { ConsoleColor.DarkCyan, COLOR_CYAN },
  344. { ConsoleColor.DarkRed, COLOR_RED },
  345. { ConsoleColor.DarkMagenta, COLOR_MAGENTA },
  346. { ConsoleColor.DarkYellow, COLOR_YELLOW },
  347. { ConsoleColor.Gray, COLOR_WHITE },
  348. { ConsoleColor.DarkGray, COLOR_BRIGHT_BLACK },
  349. { ConsoleColor.Blue, COLOR_BRIGHT_BLUE },
  350. { ConsoleColor.Green, COLOR_BRIGHT_GREEN },
  351. { ConsoleColor.Cyan, COLOR_BRIGHT_CYAN },
  352. { ConsoleColor.Red, COLOR_BRIGHT_RED },
  353. { ConsoleColor.Magenta, COLOR_BRIGHT_MAGENTA },
  354. { ConsoleColor.Yellow, COLOR_BRIGHT_YELLOW },
  355. { ConsoleColor.White, COLOR_BRIGHT_WHITE }
  356. };
  357. // Map a ConsoleColor to a platform dependent value.
  358. private int MapColors (ConsoleColor color, bool isForeground = true)
  359. {
  360. return _colorMap.TryGetValue (color, out int colorValue) ? colorValue + (isForeground ? 0 : 10) : 0;
  361. }
  362. #endregion
  363. #region Cursor Handling
  364. private bool SetCursorPosition (int col, int row)
  365. {
  366. if (IsWinPlatform)
  367. {
  368. // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth.
  369. try
  370. {
  371. Console.SetCursorPosition (col, row);
  372. return true;
  373. }
  374. catch (Exception)
  375. {
  376. return false;
  377. }
  378. }
  379. // + 1 is needed because non-Windows is based on 1 instead of 0 and
  380. // Console.CursorTop/CursorLeft isn't reliable.
  381. Console.Out.Write (EscSeqUtils.CSI_SetCursorPosition (row + 1, col + 1));
  382. return true;
  383. }
  384. private CursorVisibility? _cachedCursorVisibility;
  385. public override void UpdateCursor ()
  386. {
  387. EnsureCursorVisibility ();
  388. if (Col >= 0 && Col < Cols && Row >= 0 && Row <= Rows)
  389. {
  390. SetCursorPosition (Col, Row);
  391. SetWindowPosition (0, Row);
  392. }
  393. }
  394. public override bool GetCursorVisibility (out CursorVisibility visibility)
  395. {
  396. visibility = _cachedCursorVisibility ?? CursorVisibility.Default;
  397. return visibility == CursorVisibility.Default;
  398. }
  399. public override bool SetCursorVisibility (CursorVisibility visibility)
  400. {
  401. _cachedCursorVisibility = visibility;
  402. Console.Out.Write (visibility == CursorVisibility.Default ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
  403. return visibility == CursorVisibility.Default;
  404. }
  405. public override bool EnsureCursorVisibility ()
  406. {
  407. if (!(Col >= 0 && Row >= 0 && Col < Cols && Row < Rows))
  408. {
  409. GetCursorVisibility (out CursorVisibility cursorVisibility);
  410. _cachedCursorVisibility = cursorVisibility;
  411. SetCursorVisibility (CursorVisibility.Invisible);
  412. return false;
  413. }
  414. SetCursorVisibility (_cachedCursorVisibility ?? CursorVisibility.Default);
  415. return _cachedCursorVisibility == CursorVisibility.Default;
  416. }
  417. #endregion
  418. #region Mouse Handling
  419. public void StartReportingMouseMoves ()
  420. {
  421. if (!RunningUnitTests)
  422. {
  423. Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
  424. }
  425. }
  426. public void StopReportingMouseMoves ()
  427. {
  428. if (!RunningUnitTests)
  429. {
  430. Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
  431. }
  432. }
  433. private MouseEventArgs ToDriverMouse (MouseEvent me)
  434. {
  435. //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
  436. MouseFlags mouseFlag = 0;
  437. if ((me.ButtonState & MouseButtonState.Button1Pressed) != 0)
  438. {
  439. mouseFlag |= MouseFlags.Button1Pressed;
  440. }
  441. if ((me.ButtonState & MouseButtonState.Button1Released) != 0)
  442. {
  443. mouseFlag |= MouseFlags.Button1Released;
  444. }
  445. if ((me.ButtonState & MouseButtonState.Button1Clicked) != 0)
  446. {
  447. mouseFlag |= MouseFlags.Button1Clicked;
  448. }
  449. if ((me.ButtonState & MouseButtonState.Button1DoubleClicked) != 0)
  450. {
  451. mouseFlag |= MouseFlags.Button1DoubleClicked;
  452. }
  453. if ((me.ButtonState & MouseButtonState.Button1TripleClicked) != 0)
  454. {
  455. mouseFlag |= MouseFlags.Button1TripleClicked;
  456. }
  457. if ((me.ButtonState & MouseButtonState.Button2Pressed) != 0)
  458. {
  459. mouseFlag |= MouseFlags.Button2Pressed;
  460. }
  461. if ((me.ButtonState & MouseButtonState.Button2Released) != 0)
  462. {
  463. mouseFlag |= MouseFlags.Button2Released;
  464. }
  465. if ((me.ButtonState & MouseButtonState.Button2Clicked) != 0)
  466. {
  467. mouseFlag |= MouseFlags.Button2Clicked;
  468. }
  469. if ((me.ButtonState & MouseButtonState.Button2DoubleClicked) != 0)
  470. {
  471. mouseFlag |= MouseFlags.Button2DoubleClicked;
  472. }
  473. if ((me.ButtonState & MouseButtonState.Button2TripleClicked) != 0)
  474. {
  475. mouseFlag |= MouseFlags.Button2TripleClicked;
  476. }
  477. if ((me.ButtonState & MouseButtonState.Button3Pressed) != 0)
  478. {
  479. mouseFlag |= MouseFlags.Button3Pressed;
  480. }
  481. if ((me.ButtonState & MouseButtonState.Button3Released) != 0)
  482. {
  483. mouseFlag |= MouseFlags.Button3Released;
  484. }
  485. if ((me.ButtonState & MouseButtonState.Button3Clicked) != 0)
  486. {
  487. mouseFlag |= MouseFlags.Button3Clicked;
  488. }
  489. if ((me.ButtonState & MouseButtonState.Button3DoubleClicked) != 0)
  490. {
  491. mouseFlag |= MouseFlags.Button3DoubleClicked;
  492. }
  493. if ((me.ButtonState & MouseButtonState.Button3TripleClicked) != 0)
  494. {
  495. mouseFlag |= MouseFlags.Button3TripleClicked;
  496. }
  497. if ((me.ButtonState & MouseButtonState.ButtonWheeledUp) != 0)
  498. {
  499. mouseFlag |= MouseFlags.WheeledUp;
  500. }
  501. if ((me.ButtonState & MouseButtonState.ButtonWheeledDown) != 0)
  502. {
  503. mouseFlag |= MouseFlags.WheeledDown;
  504. }
  505. if ((me.ButtonState & MouseButtonState.ButtonWheeledLeft) != 0)
  506. {
  507. mouseFlag |= MouseFlags.WheeledLeft;
  508. }
  509. if ((me.ButtonState & MouseButtonState.ButtonWheeledRight) != 0)
  510. {
  511. mouseFlag |= MouseFlags.WheeledRight;
  512. }
  513. if ((me.ButtonState & MouseButtonState.Button4Pressed) != 0)
  514. {
  515. mouseFlag |= MouseFlags.Button4Pressed;
  516. }
  517. if ((me.ButtonState & MouseButtonState.Button4Released) != 0)
  518. {
  519. mouseFlag |= MouseFlags.Button4Released;
  520. }
  521. if ((me.ButtonState & MouseButtonState.Button4Clicked) != 0)
  522. {
  523. mouseFlag |= MouseFlags.Button4Clicked;
  524. }
  525. if ((me.ButtonState & MouseButtonState.Button4DoubleClicked) != 0)
  526. {
  527. mouseFlag |= MouseFlags.Button4DoubleClicked;
  528. }
  529. if ((me.ButtonState & MouseButtonState.Button4TripleClicked) != 0)
  530. {
  531. mouseFlag |= MouseFlags.Button4TripleClicked;
  532. }
  533. if ((me.ButtonState & MouseButtonState.ReportMousePosition) != 0)
  534. {
  535. mouseFlag |= MouseFlags.ReportMousePosition;
  536. }
  537. if ((me.ButtonState & MouseButtonState.ButtonShift) != 0)
  538. {
  539. mouseFlag |= MouseFlags.ButtonShift;
  540. }
  541. if ((me.ButtonState & MouseButtonState.ButtonCtrl) != 0)
  542. {
  543. mouseFlag |= MouseFlags.ButtonCtrl;
  544. }
  545. if ((me.ButtonState & MouseButtonState.ButtonAlt) != 0)
  546. {
  547. mouseFlag |= MouseFlags.ButtonAlt;
  548. }
  549. return new() { Position = me.Position, Flags = mouseFlag };
  550. }
  551. #endregion Mouse Handling
  552. #region Keyboard Handling
  553. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  554. {
  555. var input = new InputResult
  556. {
  557. EventType = EventType.Key, ConsoleKeyInfo = new (keyChar, key, shift, alt, control)
  558. };
  559. try
  560. {
  561. ProcessInput (input);
  562. }
  563. catch (OverflowException)
  564. { }
  565. }
  566. //private ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  567. //{
  568. // if (consoleKeyInfo.Key != ConsoleKey.Packet)
  569. // {
  570. // return consoleKeyInfo;
  571. // }
  572. // ConsoleModifiers mod = consoleKeyInfo.Modifiers;
  573. // bool shift = (mod & ConsoleModifiers.Shift) != 0;
  574. // bool alt = (mod & ConsoleModifiers.Alt) != 0;
  575. // bool control = (mod & ConsoleModifiers.Control) != 0;
  576. // ConsoleKeyInfo cKeyInfo = DecodeVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  577. // return new (cKeyInfo.KeyChar, cKeyInfo.Key, shift, alt, control);
  578. //}
  579. #endregion Keyboard Handling
  580. #region Low-Level DotNet tuff
  581. /// <inheritdoc/>
  582. public override void WriteRaw (string ansi)
  583. {
  584. Console.Out.Write (ansi);
  585. Console.Out.Flush ();
  586. }
  587. private volatile bool _winSizeChanging;
  588. private void SetWindowPosition (int col, int row)
  589. {
  590. if (!RunningUnitTests)
  591. {
  592. Top = Console.WindowTop;
  593. Left = Console.WindowLeft;
  594. }
  595. else
  596. {
  597. Top = row;
  598. Left = col;
  599. }
  600. }
  601. public virtual void ResizeScreen ()
  602. {
  603. // Not supported on Unix.
  604. if (IsWinPlatform)
  605. {
  606. // Can raise an exception while is still resizing.
  607. try
  608. {
  609. #pragma warning disable CA1416
  610. if (Console.WindowHeight > 0)
  611. {
  612. Console.CursorTop = 0;
  613. Console.CursorLeft = 0;
  614. Console.WindowTop = 0;
  615. Console.WindowLeft = 0;
  616. if (Console.WindowHeight > Rows)
  617. {
  618. Console.SetWindowSize (Cols, Rows);
  619. }
  620. Console.SetBufferSize (Cols, Rows);
  621. }
  622. #pragma warning restore CA1416
  623. }
  624. // INTENT: Why are these eating the exceptions?
  625. // Comments would be good here.
  626. catch (IOException)
  627. {
  628. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  629. Clip = new (Screen);
  630. }
  631. catch (ArgumentOutOfRangeException)
  632. {
  633. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  634. Clip = new (Screen);
  635. }
  636. }
  637. else
  638. {
  639. Console.Out.Write (EscSeqUtils.CSI_SetTerminalWindowSize (Rows, Cols));
  640. }
  641. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  642. Clip = new (Screen);
  643. }
  644. #endregion Low-Level DotNet tuff
  645. }