NetDriver.cs 25 KB

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