NetDriver.cs 25 KB

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