NetDriver.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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.Collections.Concurrent;
  6. using System.Diagnostics;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Runtime.InteropServices;
  9. using static Terminal.Gui.NetEvents;
  10. namespace Terminal.Gui;
  11. internal class NetDriver : ConsoleDriver
  12. {
  13. public bool IsWinPlatform { get; private set; }
  14. public NetWinVTConsole? NetWinConsole { get; private set; }
  15. public override void Suspend ()
  16. {
  17. if (Environment.OSVersion.Platform != PlatformID.Unix)
  18. {
  19. return;
  20. }
  21. StopReportingMouseMoves ();
  22. if (!RunningUnitTests)
  23. {
  24. Console.ResetColor ();
  25. Console.Clear ();
  26. //Disable alternative screen buffer.
  27. Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  28. //Set cursor key to cursor.
  29. Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
  30. Platform.Suspend ();
  31. //Enable alternative screen buffer.
  32. Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  33. SetContentsAsDirty ();
  34. Refresh ();
  35. }
  36. StartReportingMouseMoves ();
  37. }
  38. public override bool UpdateScreen ()
  39. {
  40. bool updated = false;
  41. if (RunningUnitTests
  42. || _winSizeChanging
  43. || Console.WindowHeight < 1
  44. || Contents?.Length != Rows * Cols
  45. || Rows != Console.WindowHeight)
  46. {
  47. return updated;
  48. }
  49. var top = 0;
  50. var left = 0;
  51. int rows = Rows;
  52. int cols = Cols;
  53. var output = new StringBuilder ();
  54. Attribute? redrawAttr = null;
  55. int lastCol = -1;
  56. CursorVisibility? savedVisibility = _cachedCursorVisibility;
  57. SetCursorVisibility (CursorVisibility.Invisible);
  58. for (int row = top; row < rows; row++)
  59. {
  60. if (Console.WindowHeight < 1)
  61. {
  62. return updated;
  63. }
  64. if (!_dirtyLines! [row])
  65. {
  66. continue;
  67. }
  68. if (!SetCursorPosition (0, row))
  69. {
  70. return updated;
  71. }
  72. updated = true;
  73. _dirtyLines [row] = false;
  74. output.Clear ();
  75. for (int col = left; col < cols; col++)
  76. {
  77. lastCol = -1;
  78. var outputWidth = 0;
  79. for (; col < cols; col++)
  80. {
  81. if (!Contents [row, col].IsDirty)
  82. {
  83. if (output.Length > 0)
  84. {
  85. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  86. }
  87. else if (lastCol == -1)
  88. {
  89. lastCol = col;
  90. }
  91. if (lastCol + 1 < cols)
  92. {
  93. lastCol++;
  94. }
  95. continue;
  96. }
  97. if (lastCol == -1)
  98. {
  99. lastCol = col;
  100. }
  101. Attribute attr = Contents [row, col].Attribute!.Value;
  102. // Performance: Only send the escape sequence if the attribute has changed.
  103. if (attr != redrawAttr)
  104. {
  105. redrawAttr = attr;
  106. if (Force16Colors)
  107. {
  108. output.Append (
  109. EscSeqUtils.CSI_SetGraphicsRendition (
  110. MapColors (
  111. (ConsoleColor)attr.Background.GetClosestNamedColor16 (),
  112. false
  113. ),
  114. MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor16 ())
  115. )
  116. );
  117. }
  118. else
  119. {
  120. output.Append (
  121. EscSeqUtils.CSI_SetForegroundColorRGB (
  122. attr.Foreground.R,
  123. attr.Foreground.G,
  124. attr.Foreground.B
  125. )
  126. );
  127. output.Append (
  128. EscSeqUtils.CSI_SetBackgroundColorRGB (
  129. attr.Background.R,
  130. attr.Background.G,
  131. attr.Background.B
  132. )
  133. );
  134. }
  135. }
  136. outputWidth++;
  137. Rune rune = Contents [row, col].Rune;
  138. output.Append (rune);
  139. if (Contents [row, col].CombiningMarks.Count > 0)
  140. {
  141. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  142. // compatible with the driver architecture. Any CMs (except in the first col)
  143. // are correctly combined with the base char, but are ALSO treated as 1 column
  144. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  145. //
  146. // For now, we just ignore the list of CMs.
  147. //foreach (var combMark in Contents [row, col].CombiningMarks) {
  148. // output.Append (combMark);
  149. //}
  150. // WriteToConsole (output, ref lastCol, row, ref outputWidth);
  151. }
  152. else if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  153. {
  154. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  155. SetCursorPosition (col - 1, row);
  156. }
  157. Contents [row, col].IsDirty = false;
  158. }
  159. }
  160. if (output.Length > 0)
  161. {
  162. SetCursorPosition (lastCol, row);
  163. Console.Write (output);
  164. }
  165. foreach (var s in Application.Sixel)
  166. {
  167. if (!string.IsNullOrWhiteSpace (s.SixelData))
  168. {
  169. SetCursorPosition (s.ScreenPosition.X, s.ScreenPosition.Y);
  170. Console.Write (s.SixelData);
  171. }
  172. }
  173. }
  174. SetCursorPosition (0, 0);
  175. _cachedCursorVisibility = savedVisibility;
  176. void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  177. {
  178. SetCursorPosition (lastCol, row);
  179. Console.Write (output);
  180. output.Clear ();
  181. lastCol += outputWidth;
  182. outputWidth = 0;
  183. }
  184. return updated;
  185. }
  186. #region Init/End/MainLoop
  187. // BUGBUG: Fix this nullable issue.
  188. /// <inheritdoc />
  189. internal override IAnsiResponseParser GetParser () => _mainLoopDriver._netEvents.Parser;
  190. internal NetMainLoop? _mainLoopDriver;
  191. /// <inheritdoc />
  192. public override MainLoop Init ()
  193. {
  194. PlatformID p = Environment.OSVersion.Platform;
  195. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  196. {
  197. IsWinPlatform = true;
  198. try
  199. {
  200. NetWinConsole = new NetWinVTConsole ();
  201. }
  202. catch (ApplicationException)
  203. {
  204. // Likely running as a unit test, or in a non-interactive session.
  205. }
  206. }
  207. if (IsWinPlatform)
  208. {
  209. Clipboard = new WindowsClipboard ();
  210. }
  211. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  212. {
  213. Clipboard = new MacOSXClipboard ();
  214. }
  215. else
  216. {
  217. if (CursesDriver.Is_WSL_Platform ())
  218. {
  219. Clipboard = new WSLClipboard ();
  220. }
  221. else
  222. {
  223. Clipboard = new CursesClipboard ();
  224. }
  225. }
  226. if (!RunningUnitTests)
  227. {
  228. Console.TreatControlCAsInput = true;
  229. Cols = Console.WindowWidth;
  230. Rows = Console.WindowHeight;
  231. //Enable alternative screen buffer.
  232. Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  233. //Set cursor key to application.
  234. Console.Out.Write (EscSeqUtils.CSI_HideCursor);
  235. }
  236. else
  237. {
  238. // We are being run in an environment that does not support a console
  239. // such as a unit test, or a pipe.
  240. Cols = 80;
  241. Rows = 24;
  242. }
  243. ResizeScreen ();
  244. ClearContents ();
  245. CurrentAttribute = new (Color.White, Color.Black);
  246. StartReportingMouseMoves ();
  247. _mainLoopDriver = new (this);
  248. _mainLoopDriver.ProcessInput = ProcessInput;
  249. return new (_mainLoopDriver);
  250. }
  251. private void ProcessInput (InputResult inputEvent)
  252. {
  253. switch (inputEvent.EventType)
  254. {
  255. case EventType.Key:
  256. ConsoleKeyInfo consoleKeyInfo = inputEvent.ConsoleKeyInfo;
  257. //if (consoleKeyInfo.Key == ConsoleKey.Packet) {
  258. // consoleKeyInfo = FromVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  259. //}
  260. //Debug.WriteLine ($"event: {inputEvent}");
  261. KeyCode map = EscSeqUtils.MapKey (consoleKeyInfo);
  262. if (map == KeyCode.Null)
  263. {
  264. break;
  265. }
  266. OnKeyDown (new (map));
  267. OnKeyUp (new (map));
  268. break;
  269. case EventType.Mouse:
  270. MouseEventArgs me = ToDriverMouse (inputEvent.MouseEvent);
  271. //Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
  272. OnMouseEvent (me);
  273. break;
  274. case EventType.WindowSize:
  275. _winSizeChanging = true;
  276. Top = 0;
  277. Left = 0;
  278. Cols = inputEvent.WindowSizeEvent.Size.Width;
  279. Rows = Math.Max (inputEvent.WindowSizeEvent.Size.Height, 0);
  280. ;
  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. public 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 (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  306. //Set cursor key to cursor.
  307. Console.Out.Write (EscSeqUtils.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 (EscSeqUtils.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 ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
  407. return visibility == CursorVisibility.Default;
  408. }
  409. private void 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;
  417. }
  418. SetCursorVisibility (_cachedCursorVisibility ?? CursorVisibility.Default);
  419. }
  420. #endregion
  421. #region Mouse Handling
  422. public void StartReportingMouseMoves ()
  423. {
  424. if (!RunningUnitTests)
  425. {
  426. Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
  427. }
  428. }
  429. public void StopReportingMouseMoves ()
  430. {
  431. if (!RunningUnitTests)
  432. {
  433. Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
  434. }
  435. }
  436. private MouseEventArgs ToDriverMouse (MouseEvent me)
  437. {
  438. //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
  439. MouseFlags mouseFlag = 0;
  440. if ((me.ButtonState & MouseButtonState.Button1Pressed) != 0)
  441. {
  442. mouseFlag |= MouseFlags.Button1Pressed;
  443. }
  444. if ((me.ButtonState & MouseButtonState.Button1Released) != 0)
  445. {
  446. mouseFlag |= MouseFlags.Button1Released;
  447. }
  448. if ((me.ButtonState & MouseButtonState.Button1Clicked) != 0)
  449. {
  450. mouseFlag |= MouseFlags.Button1Clicked;
  451. }
  452. if ((me.ButtonState & MouseButtonState.Button1DoubleClicked) != 0)
  453. {
  454. mouseFlag |= MouseFlags.Button1DoubleClicked;
  455. }
  456. if ((me.ButtonState & MouseButtonState.Button1TripleClicked) != 0)
  457. {
  458. mouseFlag |= MouseFlags.Button1TripleClicked;
  459. }
  460. if ((me.ButtonState & MouseButtonState.Button2Pressed) != 0)
  461. {
  462. mouseFlag |= MouseFlags.Button2Pressed;
  463. }
  464. if ((me.ButtonState & MouseButtonState.Button2Released) != 0)
  465. {
  466. mouseFlag |= MouseFlags.Button2Released;
  467. }
  468. if ((me.ButtonState & MouseButtonState.Button2Clicked) != 0)
  469. {
  470. mouseFlag |= MouseFlags.Button2Clicked;
  471. }
  472. if ((me.ButtonState & MouseButtonState.Button2DoubleClicked) != 0)
  473. {
  474. mouseFlag |= MouseFlags.Button2DoubleClicked;
  475. }
  476. if ((me.ButtonState & MouseButtonState.Button2TripleClicked) != 0)
  477. {
  478. mouseFlag |= MouseFlags.Button2TripleClicked;
  479. }
  480. if ((me.ButtonState & MouseButtonState.Button3Pressed) != 0)
  481. {
  482. mouseFlag |= MouseFlags.Button3Pressed;
  483. }
  484. if ((me.ButtonState & MouseButtonState.Button3Released) != 0)
  485. {
  486. mouseFlag |= MouseFlags.Button3Released;
  487. }
  488. if ((me.ButtonState & MouseButtonState.Button3Clicked) != 0)
  489. {
  490. mouseFlag |= MouseFlags.Button3Clicked;
  491. }
  492. if ((me.ButtonState & MouseButtonState.Button3DoubleClicked) != 0)
  493. {
  494. mouseFlag |= MouseFlags.Button3DoubleClicked;
  495. }
  496. if ((me.ButtonState & MouseButtonState.Button3TripleClicked) != 0)
  497. {
  498. mouseFlag |= MouseFlags.Button3TripleClicked;
  499. }
  500. if ((me.ButtonState & MouseButtonState.ButtonWheeledUp) != 0)
  501. {
  502. mouseFlag |= MouseFlags.WheeledUp;
  503. }
  504. if ((me.ButtonState & MouseButtonState.ButtonWheeledDown) != 0)
  505. {
  506. mouseFlag |= MouseFlags.WheeledDown;
  507. }
  508. if ((me.ButtonState & MouseButtonState.ButtonWheeledLeft) != 0)
  509. {
  510. mouseFlag |= MouseFlags.WheeledLeft;
  511. }
  512. if ((me.ButtonState & MouseButtonState.ButtonWheeledRight) != 0)
  513. {
  514. mouseFlag |= MouseFlags.WheeledRight;
  515. }
  516. if ((me.ButtonState & MouseButtonState.Button4Pressed) != 0)
  517. {
  518. mouseFlag |= MouseFlags.Button4Pressed;
  519. }
  520. if ((me.ButtonState & MouseButtonState.Button4Released) != 0)
  521. {
  522. mouseFlag |= MouseFlags.Button4Released;
  523. }
  524. if ((me.ButtonState & MouseButtonState.Button4Clicked) != 0)
  525. {
  526. mouseFlag |= MouseFlags.Button4Clicked;
  527. }
  528. if ((me.ButtonState & MouseButtonState.Button4DoubleClicked) != 0)
  529. {
  530. mouseFlag |= MouseFlags.Button4DoubleClicked;
  531. }
  532. if ((me.ButtonState & MouseButtonState.Button4TripleClicked) != 0)
  533. {
  534. mouseFlag |= MouseFlags.Button4TripleClicked;
  535. }
  536. if ((me.ButtonState & MouseButtonState.ReportMousePosition) != 0)
  537. {
  538. mouseFlag |= MouseFlags.ReportMousePosition;
  539. }
  540. if ((me.ButtonState & MouseButtonState.ButtonShift) != 0)
  541. {
  542. mouseFlag |= MouseFlags.ButtonShift;
  543. }
  544. if ((me.ButtonState & MouseButtonState.ButtonCtrl) != 0)
  545. {
  546. mouseFlag |= MouseFlags.ButtonCtrl;
  547. }
  548. if ((me.ButtonState & MouseButtonState.ButtonAlt) != 0)
  549. {
  550. mouseFlag |= MouseFlags.ButtonAlt;
  551. }
  552. return new() { Position = me.Position, Flags = mouseFlag };
  553. }
  554. #endregion Mouse Handling
  555. #region Keyboard Handling
  556. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  557. {
  558. var input = new InputResult
  559. {
  560. EventType = EventType.Key, ConsoleKeyInfo = new (keyChar, key, shift, alt, control)
  561. };
  562. try
  563. {
  564. ProcessInput (input);
  565. }
  566. catch (OverflowException)
  567. { }
  568. }
  569. //private ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  570. //{
  571. // if (consoleKeyInfo.Key != ConsoleKey.Packet)
  572. // {
  573. // return consoleKeyInfo;
  574. // }
  575. // ConsoleModifiers mod = consoleKeyInfo.Modifiers;
  576. // bool shift = (mod & ConsoleModifiers.Shift) != 0;
  577. // bool alt = (mod & ConsoleModifiers.Alt) != 0;
  578. // bool control = (mod & ConsoleModifiers.Control) != 0;
  579. // ConsoleKeyInfo cKeyInfo = DecodeVKPacketToKConsoleKeyInfo (consoleKeyInfo);
  580. // return new (cKeyInfo.KeyChar, cKeyInfo.Key, shift, alt, control);
  581. //}
  582. #endregion Keyboard Handling
  583. #region Low-Level DotNet tuff
  584. /// <inheritdoc/>
  585. public override void WriteRaw (string ansi)
  586. {
  587. Console.Out.Write (ansi);
  588. Console.Out.Flush ();
  589. }
  590. private volatile bool _winSizeChanging;
  591. private void SetWindowPosition (int col, int row)
  592. {
  593. if (!RunningUnitTests)
  594. {
  595. Top = Console.WindowTop;
  596. Left = Console.WindowLeft;
  597. }
  598. else
  599. {
  600. Top = row;
  601. Left = col;
  602. }
  603. }
  604. public virtual void ResizeScreen ()
  605. {
  606. // Not supported on Unix.
  607. if (IsWinPlatform)
  608. {
  609. // Can raise an exception while is still resizing.
  610. try
  611. {
  612. #pragma warning disable CA1416
  613. if (Console.WindowHeight > 0)
  614. {
  615. Console.CursorTop = 0;
  616. Console.CursorLeft = 0;
  617. Console.WindowTop = 0;
  618. Console.WindowLeft = 0;
  619. if (Console.WindowHeight > Rows)
  620. {
  621. Console.SetWindowSize (Cols, Rows);
  622. }
  623. Console.SetBufferSize (Cols, Rows);
  624. }
  625. #pragma warning restore CA1416
  626. }
  627. // INTENT: Why are these eating the exceptions?
  628. // Comments would be good here.
  629. catch (IOException)
  630. {
  631. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  632. Clip = new (Screen);
  633. }
  634. catch (ArgumentOutOfRangeException)
  635. {
  636. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  637. Clip = new (Screen);
  638. }
  639. }
  640. else
  641. {
  642. Console.Out.Write (EscSeqUtils.CSI_SetTerminalWindowSize (Rows, Cols));
  643. }
  644. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  645. Clip = new (Screen);
  646. }
  647. #endregion Low-Level DotNet tuff
  648. }