NetDriver.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 (AnsiEscapeSequenceRequestUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  26. //Set cursor key to cursor.
  27. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_ShowCursor);
  28. Platform.Suspend ();
  29. //Enable alternative screen buffer.
  30. Console.Out.Write (AnsiEscapeSequenceRequestUtils.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. AnsiEscapeSequenceRequestUtils.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. AnsiEscapeSequenceRequestUtils.CSI_SetForegroundColorRGB (
  123. attr.Foreground.R,
  124. attr.Foreground.G,
  125. attr.Foreground.B
  126. )
  127. );
  128. output.Append (
  129. AnsiEscapeSequenceRequestUtils.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. internal 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 (AnsiEscapeSequenceRequestUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  230. //Set cursor key to application.
  231. Console.Out.Write (AnsiEscapeSequenceRequestUtils.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. if (!RunningUnitTests)
  247. {
  248. Task.Run (ProcessAnsiRequestHandler);
  249. }
  250. return new (_mainLoopDriver);
  251. }
  252. private void ProcessInput (InputResult inputEvent)
  253. {
  254. switch (inputEvent.EventType)
  255. {
  256. case EventType.Key:
  257. ConsoleKeyInfo consoleKeyInfo = inputEvent.ConsoleKeyInfo;
  258. //if (consoleKeyInfo.Key == ConsoleKey.Packet) {
  259. // consoleKeyInfo = FromVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  260. //}
  261. //Debug.WriteLine ($"event: {inputEvent}");
  262. KeyCode map = AnsiEscapeSequenceRequestUtils.MapKey (consoleKeyInfo);
  263. if (map == KeyCode.Null)
  264. {
  265. break;
  266. }
  267. OnKeyDown (new (map));
  268. OnKeyUp (new (map));
  269. break;
  270. case EventType.Mouse:
  271. MouseEventArgs me = ToDriverMouse (inputEvent.MouseEvent);
  272. //Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
  273. OnMouseEvent (me);
  274. break;
  275. case EventType.WindowSize:
  276. _winSizeChanging = true;
  277. Top = 0;
  278. Left = 0;
  279. Cols = inputEvent.WindowSizeEvent.Size.Width;
  280. Rows = Math.Max (inputEvent.WindowSizeEvent.Size.Height, 0);
  281. ResizeScreen ();
  282. ClearContents ();
  283. _winSizeChanging = false;
  284. OnSizeChanged (new (new (Cols, Rows)));
  285. break;
  286. case EventType.RequestResponse:
  287. break;
  288. case EventType.WindowPosition:
  289. break;
  290. default:
  291. throw new ArgumentOutOfRangeException ();
  292. }
  293. }
  294. internal override void End ()
  295. {
  296. if (IsWinPlatform)
  297. {
  298. NetWinConsole?.Cleanup ();
  299. }
  300. StopReportingMouseMoves ();
  301. if (!RunningUnitTests)
  302. {
  303. Console.ResetColor ();
  304. //Disable alternative screen buffer.
  305. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  306. //Set cursor key to cursor.
  307. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_ShowCursor);
  308. Console.Out.Close ();
  309. }
  310. }
  311. #endregion Init/End/MainLoop
  312. #region Color Handling
  313. public override bool SupportsTrueColor => Environment.OSVersion.Platform == PlatformID.Unix
  314. || (IsWinPlatform && Environment.OSVersion.Version.Build >= 14931);
  315. private const int COLOR_BLACK = 30;
  316. private const int COLOR_BLUE = 34;
  317. private const int COLOR_BRIGHT_BLACK = 90;
  318. private const int COLOR_BRIGHT_BLUE = 94;
  319. private const int COLOR_BRIGHT_CYAN = 96;
  320. private const int COLOR_BRIGHT_GREEN = 92;
  321. private const int COLOR_BRIGHT_MAGENTA = 95;
  322. private const int COLOR_BRIGHT_RED = 91;
  323. private const int COLOR_BRIGHT_WHITE = 97;
  324. private const int COLOR_BRIGHT_YELLOW = 93;
  325. private const int COLOR_CYAN = 36;
  326. private const int COLOR_GREEN = 32;
  327. private const int COLOR_MAGENTA = 35;
  328. private const int COLOR_RED = 31;
  329. private const int COLOR_WHITE = 37;
  330. private const int COLOR_YELLOW = 33;
  331. //// Cache the list of ConsoleColor values.
  332. //[UnconditionalSuppressMessage (
  333. // "AOT",
  334. // "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
  335. // Justification = "<Pending>")]
  336. //private static readonly HashSet<int> ConsoleColorValues = new (
  337. // Enum.GetValues (typeof (ConsoleColor))
  338. // .OfType<ConsoleColor> ()
  339. // .Select (c => (int)c)
  340. // );
  341. // Dictionary for mapping ConsoleColor values to the values used by System.Net.Console.
  342. private static readonly Dictionary<ConsoleColor, int> _colorMap = new ()
  343. {
  344. { ConsoleColor.Black, COLOR_BLACK },
  345. { ConsoleColor.DarkBlue, COLOR_BLUE },
  346. { ConsoleColor.DarkGreen, COLOR_GREEN },
  347. { ConsoleColor.DarkCyan, COLOR_CYAN },
  348. { ConsoleColor.DarkRed, COLOR_RED },
  349. { ConsoleColor.DarkMagenta, COLOR_MAGENTA },
  350. { ConsoleColor.DarkYellow, COLOR_YELLOW },
  351. { ConsoleColor.Gray, COLOR_WHITE },
  352. { ConsoleColor.DarkGray, COLOR_BRIGHT_BLACK },
  353. { ConsoleColor.Blue, COLOR_BRIGHT_BLUE },
  354. { ConsoleColor.Green, COLOR_BRIGHT_GREEN },
  355. { ConsoleColor.Cyan, COLOR_BRIGHT_CYAN },
  356. { ConsoleColor.Red, COLOR_BRIGHT_RED },
  357. { ConsoleColor.Magenta, COLOR_BRIGHT_MAGENTA },
  358. { ConsoleColor.Yellow, COLOR_BRIGHT_YELLOW },
  359. { ConsoleColor.White, COLOR_BRIGHT_WHITE }
  360. };
  361. // Map a ConsoleColor to a platform dependent value.
  362. private int MapColors (ConsoleColor color, bool isForeground = true)
  363. {
  364. return _colorMap.TryGetValue (color, out int colorValue) ? colorValue + (isForeground ? 0 : 10) : 0;
  365. }
  366. #endregion
  367. #region Cursor Handling
  368. private bool SetCursorPosition (int col, int row)
  369. {
  370. if (IsWinPlatform)
  371. {
  372. // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth.
  373. try
  374. {
  375. Console.SetCursorPosition (col, row);
  376. return true;
  377. }
  378. catch (Exception)
  379. {
  380. return false;
  381. }
  382. }
  383. // + 1 is needed because non-Windows is based on 1 instead of 0 and
  384. // Console.CursorTop/CursorLeft isn't reliable.
  385. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_SetCursorPosition (row + 1, col + 1));
  386. return true;
  387. }
  388. private CursorVisibility? _cachedCursorVisibility;
  389. public override void UpdateCursor ()
  390. {
  391. EnsureCursorVisibility ();
  392. if (Col >= 0 && Col < Cols && Row >= 0 && Row <= Rows)
  393. {
  394. SetCursorPosition (Col, Row);
  395. SetWindowPosition (0, Row);
  396. }
  397. }
  398. public override bool GetCursorVisibility (out CursorVisibility visibility)
  399. {
  400. visibility = _cachedCursorVisibility ?? CursorVisibility.Default;
  401. return visibility == CursorVisibility.Default;
  402. }
  403. public override bool SetCursorVisibility (CursorVisibility visibility)
  404. {
  405. _cachedCursorVisibility = visibility;
  406. Console.Out.Write (visibility == CursorVisibility.Default ? AnsiEscapeSequenceRequestUtils.CSI_ShowCursor : AnsiEscapeSequenceRequestUtils.CSI_HideCursor);
  407. return visibility == CursorVisibility.Default;
  408. }
  409. public override bool EnsureCursorVisibility ()
  410. {
  411. if (!(Col >= 0 && Row >= 0 && Col < Cols && Row < Rows))
  412. {
  413. GetCursorVisibility (out CursorVisibility cursorVisibility);
  414. _cachedCursorVisibility = cursorVisibility;
  415. SetCursorVisibility (CursorVisibility.Invisible);
  416. return false;
  417. }
  418. SetCursorVisibility (_cachedCursorVisibility ?? CursorVisibility.Default);
  419. return _cachedCursorVisibility == CursorVisibility.Default;
  420. }
  421. #endregion
  422. #region Mouse Handling
  423. public void StartReportingMouseMoves ()
  424. {
  425. if (!RunningUnitTests)
  426. {
  427. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_EnableMouseEvents);
  428. }
  429. }
  430. public void StopReportingMouseMoves ()
  431. {
  432. if (!RunningUnitTests)
  433. {
  434. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_DisableMouseEvents);
  435. }
  436. }
  437. private MouseEventArgs ToDriverMouse (MouseEvent me)
  438. {
  439. //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
  440. MouseFlags mouseFlag = 0;
  441. if ((me.ButtonState & MouseButtonState.Button1Pressed) != 0)
  442. {
  443. mouseFlag |= MouseFlags.Button1Pressed;
  444. }
  445. if ((me.ButtonState & MouseButtonState.Button1Released) != 0)
  446. {
  447. mouseFlag |= MouseFlags.Button1Released;
  448. }
  449. if ((me.ButtonState & MouseButtonState.Button1Clicked) != 0)
  450. {
  451. mouseFlag |= MouseFlags.Button1Clicked;
  452. }
  453. if ((me.ButtonState & MouseButtonState.Button1DoubleClicked) != 0)
  454. {
  455. mouseFlag |= MouseFlags.Button1DoubleClicked;
  456. }
  457. if ((me.ButtonState & MouseButtonState.Button1TripleClicked) != 0)
  458. {
  459. mouseFlag |= MouseFlags.Button1TripleClicked;
  460. }
  461. if ((me.ButtonState & MouseButtonState.Button2Pressed) != 0)
  462. {
  463. mouseFlag |= MouseFlags.Button2Pressed;
  464. }
  465. if ((me.ButtonState & MouseButtonState.Button2Released) != 0)
  466. {
  467. mouseFlag |= MouseFlags.Button2Released;
  468. }
  469. if ((me.ButtonState & MouseButtonState.Button2Clicked) != 0)
  470. {
  471. mouseFlag |= MouseFlags.Button2Clicked;
  472. }
  473. if ((me.ButtonState & MouseButtonState.Button2DoubleClicked) != 0)
  474. {
  475. mouseFlag |= MouseFlags.Button2DoubleClicked;
  476. }
  477. if ((me.ButtonState & MouseButtonState.Button2TripleClicked) != 0)
  478. {
  479. mouseFlag |= MouseFlags.Button2TripleClicked;
  480. }
  481. if ((me.ButtonState & MouseButtonState.Button3Pressed) != 0)
  482. {
  483. mouseFlag |= MouseFlags.Button3Pressed;
  484. }
  485. if ((me.ButtonState & MouseButtonState.Button3Released) != 0)
  486. {
  487. mouseFlag |= MouseFlags.Button3Released;
  488. }
  489. if ((me.ButtonState & MouseButtonState.Button3Clicked) != 0)
  490. {
  491. mouseFlag |= MouseFlags.Button3Clicked;
  492. }
  493. if ((me.ButtonState & MouseButtonState.Button3DoubleClicked) != 0)
  494. {
  495. mouseFlag |= MouseFlags.Button3DoubleClicked;
  496. }
  497. if ((me.ButtonState & MouseButtonState.Button3TripleClicked) != 0)
  498. {
  499. mouseFlag |= MouseFlags.Button3TripleClicked;
  500. }
  501. if ((me.ButtonState & MouseButtonState.ButtonWheeledUp) != 0)
  502. {
  503. mouseFlag |= MouseFlags.WheeledUp;
  504. }
  505. if ((me.ButtonState & MouseButtonState.ButtonWheeledDown) != 0)
  506. {
  507. mouseFlag |= MouseFlags.WheeledDown;
  508. }
  509. if ((me.ButtonState & MouseButtonState.ButtonWheeledLeft) != 0)
  510. {
  511. mouseFlag |= MouseFlags.WheeledLeft;
  512. }
  513. if ((me.ButtonState & MouseButtonState.ButtonWheeledRight) != 0)
  514. {
  515. mouseFlag |= MouseFlags.WheeledRight;
  516. }
  517. if ((me.ButtonState & MouseButtonState.Button4Pressed) != 0)
  518. {
  519. mouseFlag |= MouseFlags.Button4Pressed;
  520. }
  521. if ((me.ButtonState & MouseButtonState.Button4Released) != 0)
  522. {
  523. mouseFlag |= MouseFlags.Button4Released;
  524. }
  525. if ((me.ButtonState & MouseButtonState.Button4Clicked) != 0)
  526. {
  527. mouseFlag |= MouseFlags.Button4Clicked;
  528. }
  529. if ((me.ButtonState & MouseButtonState.Button4DoubleClicked) != 0)
  530. {
  531. mouseFlag |= MouseFlags.Button4DoubleClicked;
  532. }
  533. if ((me.ButtonState & MouseButtonState.Button4TripleClicked) != 0)
  534. {
  535. mouseFlag |= MouseFlags.Button4TripleClicked;
  536. }
  537. if ((me.ButtonState & MouseButtonState.ReportMousePosition) != 0)
  538. {
  539. mouseFlag |= MouseFlags.ReportMousePosition;
  540. }
  541. if ((me.ButtonState & MouseButtonState.ButtonShift) != 0)
  542. {
  543. mouseFlag |= MouseFlags.ButtonShift;
  544. }
  545. if ((me.ButtonState & MouseButtonState.ButtonCtrl) != 0)
  546. {
  547. mouseFlag |= MouseFlags.ButtonCtrl;
  548. }
  549. if ((me.ButtonState & MouseButtonState.ButtonAlt) != 0)
  550. {
  551. mouseFlag |= MouseFlags.ButtonAlt;
  552. }
  553. return new() { Position = me.Position, Flags = mouseFlag };
  554. }
  555. #endregion Mouse Handling
  556. #region Keyboard Handling
  557. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  558. {
  559. var input = new InputResult
  560. {
  561. EventType = EventType.Key, ConsoleKeyInfo = new (keyChar, key, shift, alt, control)
  562. };
  563. try
  564. {
  565. ProcessInput (input);
  566. }
  567. catch (OverflowException)
  568. { }
  569. }
  570. //private ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  571. //{
  572. // if (consoleKeyInfo.Key != ConsoleKey.Packet)
  573. // {
  574. // return consoleKeyInfo;
  575. // }
  576. // ConsoleModifiers mod = consoleKeyInfo.Modifiers;
  577. // bool shift = (mod & ConsoleModifiers.Shift) != 0;
  578. // bool alt = (mod & ConsoleModifiers.Alt) != 0;
  579. // bool control = (mod & ConsoleModifiers.Control) != 0;
  580. // ConsoleKeyInfo cKeyInfo = DecodeVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  581. // return new (cKeyInfo.KeyChar, cKeyInfo.Key, shift, alt, control);
  582. //}
  583. #endregion Keyboard Handling
  584. #region Low-Level DotNet tuff
  585. /// <inheritdoc/>
  586. internal override void WriteRaw (string ansi)
  587. {
  588. Console.Out.Write (ansi);
  589. Console.Out.Flush ();
  590. }
  591. private volatile bool _winSizeChanging;
  592. private void SetWindowPosition (int col, int row)
  593. {
  594. if (!RunningUnitTests)
  595. {
  596. Top = Console.WindowTop;
  597. Left = Console.WindowLeft;
  598. }
  599. else
  600. {
  601. Top = row;
  602. Left = col;
  603. }
  604. }
  605. public virtual void ResizeScreen ()
  606. {
  607. // Not supported on Unix.
  608. if (IsWinPlatform)
  609. {
  610. // Can raise an exception while is still resizing.
  611. try
  612. {
  613. #pragma warning disable CA1416
  614. if (Console.WindowHeight > 0)
  615. {
  616. Console.CursorTop = 0;
  617. Console.CursorLeft = 0;
  618. Console.WindowTop = 0;
  619. Console.WindowLeft = 0;
  620. if (Console.WindowHeight > Rows)
  621. {
  622. Console.SetWindowSize (Cols, Rows);
  623. }
  624. Console.SetBufferSize (Cols, Rows);
  625. }
  626. #pragma warning restore CA1416
  627. }
  628. // INTENT: Why are these eating the exceptions?
  629. // Comments would be good here.
  630. catch (IOException)
  631. {
  632. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  633. Clip = new (Screen);
  634. }
  635. catch (ArgumentOutOfRangeException)
  636. {
  637. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  638. Clip = new (Screen);
  639. }
  640. }
  641. else
  642. {
  643. Console.Out.Write (AnsiEscapeSequenceRequestUtils.CSI_SetTerminalWindowSize (Rows, Cols));
  644. }
  645. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  646. Clip = new (Screen);
  647. }
  648. #endregion Low-Level DotNet tuff
  649. }