Driver.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. //
  2. // Driver.cs: Abstract and concrete interfaces to the console (curses or console)
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. using Mono.Terminal;
  11. using Unix.Terminal;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
  15. /// </summary>
  16. public enum Color {
  17. /// <summary>
  18. /// The black color.
  19. /// </summary>
  20. Black,
  21. /// <summary>
  22. /// The blue color.
  23. /// </summary>
  24. Blue,
  25. /// <summary>
  26. /// The green color.
  27. /// </summary>
  28. Green,
  29. /// <summary>
  30. /// The cyan color.
  31. /// </summary>
  32. Cyan,
  33. /// <summary>
  34. /// The red color.
  35. /// </summary>
  36. Red,
  37. /// <summary>
  38. /// The magenta color.
  39. /// </summary>
  40. Magenta,
  41. /// <summary>
  42. /// The brown color.
  43. /// </summary>
  44. Brown,
  45. /// <summary>
  46. /// The gray color.
  47. /// </summary>
  48. Gray,
  49. /// <summary>
  50. /// The dark gray color.
  51. /// </summary>
  52. DarkGray,
  53. /// <summary>
  54. /// The bright bBlue color.
  55. /// </summary>
  56. BrightBlue,
  57. /// <summary>
  58. /// The bright green color.
  59. /// </summary>
  60. BrightGreen,
  61. /// <summary>
  62. /// The brigh cyan color.
  63. /// </summary>
  64. BrighCyan,
  65. /// <summary>
  66. /// The bright red color.
  67. /// </summary>
  68. BrightRed,
  69. /// <summary>
  70. /// The bright magenta color.
  71. /// </summary>
  72. BrightMagenta,
  73. /// <summary>
  74. /// The bright yellow color.
  75. /// </summary>
  76. BrightYellow,
  77. /// <summary>
  78. /// The White color.
  79. /// </summary>
  80. White
  81. }
  82. /// <summary>
  83. /// Attributes are used as elements that contain both a foreground and a background or platform specific features
  84. /// </summary>
  85. /// <remarks>
  86. /// Attributes are needed to map colors to terminal capabilities that might lack colors, on color
  87. /// scenarios, they encode both the foreground and the background color and are used in the ColorScheme
  88. /// class to define color schemes that can be used in your application.
  89. /// </remarks>
  90. public struct Attribute {
  91. internal int value;
  92. public Attribute (int v)
  93. {
  94. value = v;
  95. }
  96. public static implicit operator int (Attribute c) => c.value;
  97. public static implicit operator Attribute (int v) => new Attribute (v);
  98. }
  99. /// <summary>
  100. /// Color scheme definitions, they cover some common scenarios and are used
  101. /// typically in toplevel containers to set the scheme that is used by all the
  102. /// views contained inside.
  103. /// </summary>
  104. public class ColorScheme {
  105. /// <summary>
  106. /// The default color for text, when the view is not focused.
  107. /// </summary>
  108. public Attribute Normal;
  109. /// <summary>
  110. /// The color for text when the view has the focus.
  111. /// </summary>
  112. public Attribute Focus;
  113. /// <summary>
  114. /// The color for the hotkey when a view is not focused
  115. /// </summary>
  116. public Attribute HotNormal;
  117. /// <summary>
  118. /// The color for the hotkey when the view is focused.
  119. /// </summary>
  120. public Attribute HotFocus;
  121. }
  122. /// <summary>
  123. /// The default ColorSchemes for the application.
  124. /// </summary>
  125. public static class Colors {
  126. /// <summary>
  127. /// The base color scheme, for the default toplevel views.
  128. /// </summary>
  129. public static ColorScheme Base;
  130. /// <summary>
  131. /// The dialog color scheme, for standard popup dialog boxes
  132. /// </summary>
  133. public static ColorScheme Dialog;
  134. /// <summary>
  135. /// The menu bar color
  136. /// </summary>
  137. public static ColorScheme Menu;
  138. /// <summary>
  139. /// The color scheme for showing errors.
  140. /// </summary>
  141. public static ColorScheme Error;
  142. }
  143. /// <summary>
  144. /// Special characters that can be drawn with Driver.AddSpecial.
  145. /// </summary>
  146. public enum SpecialChar {
  147. /// <summary>
  148. /// Horizontal line character.
  149. /// </summary>
  150. HLine,
  151. }
  152. /// <summary>
  153. /// ConsoleDriver is an abstract class that defines the requirements for a console driver. One implementation if the CursesDriver, and another one uses the .NET Console one.
  154. /// </summary>
  155. public abstract class ConsoleDriver {
  156. /// <summary>
  157. /// The current number of columns in the terminal.
  158. /// </summary>
  159. public abstract int Cols { get; }
  160. /// <summary>
  161. /// The current number of rows in the terminal.
  162. /// </summary>
  163. public abstract int Rows { get; }
  164. /// <summary>
  165. /// Initializes the driver
  166. /// </summary>
  167. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  168. public abstract void Init (Action terminalResized);
  169. /// <summary>
  170. /// Moves the cursor to the specified column and row.
  171. /// </summary>
  172. /// <param name="col">Column to move the cursor to.</param>
  173. /// <param name="row">Row to move the cursor to.</param>
  174. public abstract void Move (int col, int row);
  175. /// <summary>
  176. /// Adds the specified rune to the display at the current cursor position
  177. /// </summary>
  178. /// <param name="rune">Rune to add.</param>
  179. public abstract void AddCh (int rune);
  180. /// <summary>
  181. /// Adds the specified
  182. /// </summary>
  183. /// <param name="str">String.</param>
  184. public abstract void AddStr (string str);
  185. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> target, Action<MouseEvent> mouse);
  186. /// <summary>
  187. /// Updates the screen to reflect all the changes that have been done to the display buffer
  188. /// </summary>
  189. public abstract void Refresh ();
  190. /// <summary>
  191. /// Ends the execution of the console driver.
  192. /// </summary>
  193. public abstract void End ();
  194. public abstract void RedrawTop ();
  195. public abstract void SetAttribute (Attribute c);
  196. // Set Colors from limit sets of colors
  197. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  198. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  199. // that independently with the R, G, B values.
  200. /// <summary>
  201. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  202. /// that independently with the R, G, B values.
  203. /// </summary>
  204. /// <param name="foregroundColorId">Foreground color identifier.</param>
  205. /// <param name="backgroundColorId">Background color identifier.</param>
  206. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  207. public abstract void DrawFrame (Rect region, int padding, bool fill);
  208. /// <summary>
  209. /// Draws a special characters in the screen
  210. /// </summary>
  211. /// <param name="ch">Ch.</param>
  212. public abstract void AddSpecial (SpecialChar ch);
  213. /// <summary>
  214. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  215. /// </summary>
  216. public abstract void Suspend ();
  217. Rect clip;
  218. /// <summary>
  219. /// Controls the current clipping region that AddCh/AddStr is subject to.
  220. /// </summary>
  221. /// <value>The clip.</value>
  222. public Rect Clip {
  223. get => clip;
  224. set => this.clip = value;
  225. }
  226. public abstract void StartReportingMouseMoves ();
  227. public abstract void StopReportingMouseMoves ();
  228. }
  229. /// <summary>
  230. /// This is the Curses driver for the gui.cs/Terminal framework.
  231. /// </summary>
  232. internal class CursesDriver : ConsoleDriver {
  233. Action terminalResized;
  234. public override int Cols => Curses.Cols;
  235. public override int Rows => Curses.Lines;
  236. // Current row, and current col, tracked by Move/AddCh only
  237. int ccol, crow;
  238. bool needMove;
  239. public override void Move (int col, int row)
  240. {
  241. ccol = col;
  242. crow = row;
  243. if (Clip.Contains (col, row)) {
  244. Curses.move (row, col);
  245. needMove = false;
  246. } else {
  247. Curses.move (Clip.Y, Clip.X);
  248. needMove = true;
  249. }
  250. }
  251. static bool sync = true;
  252. public override void AddCh (int rune)
  253. {
  254. if (Clip.Contains (ccol, crow)) {
  255. if (needMove) {
  256. Curses.move (crow, ccol);
  257. needMove = false;
  258. }
  259. Curses.addch (rune);
  260. } else
  261. needMove = true;
  262. if (sync)
  263. Application.Driver.Refresh ();
  264. ccol++;
  265. }
  266. public override void AddSpecial (SpecialChar ch)
  267. {
  268. switch (ch) {
  269. case SpecialChar.HLine:
  270. AddCh (Curses.ACS_HLINE);
  271. break;
  272. }
  273. }
  274. public override void AddStr (string str)
  275. {
  276. // TODO; optimize this to determine if the str fits in the clip region, and if so, use Curses.addstr directly
  277. foreach (var rune in str)
  278. AddCh ((int)rune);
  279. }
  280. public override void Refresh () => Curses.refresh ();
  281. public override void End () => Curses.endwin ();
  282. public override void RedrawTop () => window.redrawwin ();
  283. public override void SetAttribute (Attribute c) => Curses.attrset (c.value);
  284. public Curses.Window window;
  285. static short last_color_pair = 16;
  286. static Attribute MakeColor (short f, short b)
  287. {
  288. Curses.InitColorPair (++last_color_pair, f, b);
  289. return new Attribute () { value = Curses.ColorPair (last_color_pair) };
  290. }
  291. int [,] colorPairs = new int [16, 16];
  292. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  293. {
  294. int f = (short)foreground;
  295. int b = (short)background;
  296. var v = colorPairs [f, b];
  297. if ((v & 0x10000) == 0) {
  298. b = b & 0x7;
  299. bool bold = (f & 0x8) != 0;
  300. f = f & 0x7;
  301. v = MakeColor ((short)f, (short)b) | (bold ? Curses.A_BOLD : 0);
  302. colorPairs [(int)foreground, (int)background] = v | 0x1000;
  303. }
  304. SetAttribute (v & 0xffff);
  305. }
  306. Dictionary<int, int> rawPairs = new Dictionary<int, int> ();
  307. public override void SetColors (short foreColorId, short backgroundColorId)
  308. {
  309. int key = (((ushort)foreColorId << 16)) | (ushort)backgroundColorId;
  310. if (!rawPairs.TryGetValue (key, out var v)) {
  311. v = MakeColor (foreColorId, backgroundColorId);
  312. rawPairs [key] = v;
  313. }
  314. SetAttribute (v);
  315. }
  316. static Key MapCursesKey (int cursesKey)
  317. {
  318. switch (cursesKey) {
  319. case Curses.KeyF1: return Key.F1;
  320. case Curses.KeyF2: return Key.F2;
  321. case Curses.KeyF3: return Key.F3;
  322. case Curses.KeyF4: return Key.F4;
  323. case Curses.KeyF5: return Key.F5;
  324. case Curses.KeyF6: return Key.F6;
  325. case Curses.KeyF7: return Key.F7;
  326. case Curses.KeyF8: return Key.F8;
  327. case Curses.KeyF9: return Key.F9;
  328. case Curses.KeyF10: return Key.F10;
  329. case Curses.KeyUp: return Key.CursorUp;
  330. case Curses.KeyDown: return Key.CursorDown;
  331. case Curses.KeyLeft: return Key.CursorLeft;
  332. case Curses.KeyRight: return Key.CursorRight;
  333. case Curses.KeyHome: return Key.Home;
  334. case Curses.KeyEnd: return Key.End;
  335. case Curses.KeyNPage: return Key.PageDown;
  336. case Curses.KeyPPage: return Key.PageUp;
  337. case Curses.KeyDeleteChar: return Key.DeleteChar;
  338. case Curses.KeyInsertChar: return Key.InsertChar;
  339. case Curses.KeyBackTab: return Key.BackTab;
  340. default: return Key.Unknown;
  341. }
  342. }
  343. static MouseEvent ToDriverMouse (Curses.MouseEvent cev)
  344. {
  345. return new MouseEvent () {
  346. X = cev.X,
  347. Y = cev.Y,
  348. Flags = (MouseFlags)cev.ButtonState
  349. };
  350. }
  351. void ProcessInput (Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  352. {
  353. int wch;
  354. var code = Curses.get_wch (out wch);
  355. if (code == Curses.KEY_CODE_YES) {
  356. if (wch == Curses.KeyResize) {
  357. if (Curses.CheckWinChange ()) {
  358. terminalResized ();
  359. return;
  360. }
  361. }
  362. if (wch == Curses.KeyMouse) {
  363. Curses.MouseEvent ev;
  364. Curses.getmouse (out ev);
  365. mouseHandler (ToDriverMouse (ev));
  366. return;
  367. }
  368. keyHandler (new KeyEvent (MapCursesKey (wch)));
  369. return;
  370. }
  371. // Special handling for ESC, we want to try to catch ESC+letter to simulate alt-letter as well as Alt-Fkey
  372. if (wch == 27) {
  373. Curses.timeout (100);
  374. code = Curses.get_wch (out wch);
  375. if (code == Curses.KEY_CODE_YES)
  376. keyHandler (new KeyEvent (Key.AltMask | MapCursesKey (wch)));
  377. if (code == 0) {
  378. KeyEvent key;
  379. // The ESC-number handling, debatable.
  380. if (wch >= '1' && wch <= '9')
  381. key = new KeyEvent ((Key)((int)Key.F1 + (wch - '0' - 1)));
  382. else if (wch == '0')
  383. key = new KeyEvent (Key.F10);
  384. else if (wch == 27)
  385. key = new KeyEvent ((Key)wch);
  386. else
  387. key = new KeyEvent (Key.AltMask | (Key)wch);
  388. keyHandler (key);
  389. } else
  390. keyHandler (new KeyEvent (Key.Esc));
  391. } else
  392. keyHandler (new KeyEvent ((Key)wch));
  393. }
  394. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  395. {
  396. Curses.timeout (-1);
  397. mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
  398. ProcessInput (keyHandler, mouseHandler);
  399. return true;
  400. });
  401. }
  402. public override void DrawFrame (Rect region, int padding, bool fill)
  403. {
  404. int width = region.Width;
  405. int height = region.Height;
  406. int b;
  407. int fwidth = width - padding * 2;
  408. int fheight = height - 1 - padding;
  409. Move (region.X, region.Y);
  410. if (padding > 0) {
  411. for (int l = 0; l < padding; l++)
  412. for (b = 0; b < width; b++)
  413. AddCh (' ');
  414. }
  415. Move (region.X, region.Y + padding);
  416. for (int c = 0; c < padding; c++)
  417. AddCh (' ');
  418. AddCh (Curses.ACS_ULCORNER);
  419. for (b = 0; b < fwidth - 2; b++)
  420. AddCh (Curses.ACS_HLINE);
  421. AddCh (Curses.ACS_URCORNER);
  422. for (int c = 0; c < padding; c++)
  423. AddCh (' ');
  424. for (b = 1+padding; b < fheight; b++) {
  425. Move (region.X, region.Y + b);
  426. for (int c = 0; c < padding; c++)
  427. AddCh (' ');
  428. AddCh (Curses.ACS_VLINE);
  429. if (fill) {
  430. for (int x = 1; x < fwidth - 1; x++)
  431. AddCh (' ');
  432. } else
  433. Move (region.X + fwidth - 1, region.Y + b);
  434. AddCh (Curses.ACS_VLINE);
  435. for (int c = 0; c < padding; c++)
  436. AddCh (' ');
  437. }
  438. Move (region.X, region.Y + fheight);
  439. for (int c = 0; c < padding; c++)
  440. AddCh (' ');
  441. AddCh (Curses.ACS_LLCORNER);
  442. for (b = 0; b < fwidth - 2; b++)
  443. AddCh (Curses.ACS_HLINE);
  444. AddCh (Curses.ACS_LRCORNER);
  445. for (int c = 0; c < padding; c++)
  446. AddCh (' ');
  447. if (padding > 0) {
  448. Move (region.X, region.Y + height - padding);
  449. for (int l = 0; l < padding; l++)
  450. for (b = 0; b < width; b++)
  451. AddCh (' ');
  452. }
  453. }
  454. Curses.Event oldMouseEvents, reportableMouseEvents;
  455. public override void Init (Action terminalResized)
  456. {
  457. if (window != null)
  458. return;
  459. try {
  460. window = Curses.initscr ();
  461. } catch (Exception e) {
  462. Console.WriteLine ("Curses failed to initialize, the exception is: " + e);
  463. }
  464. Curses.raw ();
  465. Curses.noecho ();
  466. Curses.Window.Standard.keypad (true);
  467. reportableMouseEvents = Curses.mousemask (Curses.Event.AllEvents | Curses.Event.ReportMousePosition, out oldMouseEvents);
  468. this.terminalResized = terminalResized;
  469. StartReportingMouseMoves ();
  470. Colors.Base = new ColorScheme ();
  471. Colors.Dialog = new ColorScheme ();
  472. Colors.Menu = new ColorScheme ();
  473. Colors.Error = new ColorScheme ();
  474. Clip = new Rect (0, 0, Cols, Rows);
  475. if (Curses.HasColors) {
  476. Curses.StartColor ();
  477. Curses.UseDefaultColors ();
  478. Colors.Base.Normal = MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLUE);
  479. Colors.Base.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  480. Colors.Base.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLUE);
  481. Colors.Base.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  482. // Focused,
  483. // Selected, Hot: Yellow on Black
  484. // Selected, text: white on black
  485. // Unselected, hot: yellow on cyan
  486. // unselected, text: same as unfocused
  487. Colors.Menu.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLACK);
  488. Colors.Menu.Focus = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLACK);
  489. Colors.Menu.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  490. Colors.Menu.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_CYAN);
  491. Colors.Dialog.Normal = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  492. Colors.Dialog.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  493. Colors.Dialog.HotNormal = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_WHITE);
  494. Colors.Dialog.HotFocus = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_CYAN);
  495. Colors.Error.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_RED);
  496. Colors.Error.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  497. Colors.Error.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_RED);
  498. Colors.Error.HotFocus = Colors.Error.HotNormal;
  499. } else {
  500. Colors.Base.Normal = Curses.A_NORMAL;
  501. Colors.Base.Focus = Curses.A_REVERSE;
  502. Colors.Base.HotNormal = Curses.A_BOLD;
  503. Colors.Base.HotFocus = Curses.A_BOLD | Curses.A_REVERSE;
  504. Colors.Menu.Normal = Curses.A_REVERSE;
  505. Colors.Menu.Focus = Curses.A_NORMAL;
  506. Colors.Menu.HotNormal = Curses.A_BOLD;
  507. Colors.Menu.HotFocus = Curses.A_NORMAL;
  508. Colors.Dialog.Normal = Curses.A_REVERSE;
  509. Colors.Dialog.Focus = Curses.A_NORMAL;
  510. Colors.Dialog.HotNormal = Curses.A_BOLD;
  511. Colors.Dialog.HotFocus = Curses.A_NORMAL;
  512. Colors.Error.Normal = Curses.A_BOLD;
  513. Colors.Error.Focus = Curses.A_BOLD | Curses.A_REVERSE;
  514. Colors.Error.HotNormal = Curses.A_BOLD | Curses.A_REVERSE;
  515. Colors.Error.HotFocus = Curses.A_REVERSE;
  516. }
  517. }
  518. public override void Suspend ()
  519. {
  520. StopReportingMouseMoves ();
  521. Platform.Suspend ();
  522. Curses.Window.Standard.redrawwin ();
  523. Curses.refresh ();
  524. StartReportingMouseMoves ();
  525. }
  526. public override void StartReportingMouseMoves()
  527. {
  528. Console.Out.Write ("\x1b[?1003h");
  529. Console.Out.Flush ();
  530. }
  531. public override void StopReportingMouseMoves()
  532. {
  533. Console.Out.Write ("\x1b[?1003l");
  534. Console.Out.Flush ();
  535. }
  536. }
  537. internal static class Platform {
  538. [DllImport ("libc")]
  539. static extern int uname (IntPtr buf);
  540. [DllImport ("libc")]
  541. static extern int killpg (int pgrp, int pid);
  542. static int suspendSignal;
  543. static int GetSuspendSignal ()
  544. {
  545. if (suspendSignal != 0)
  546. return suspendSignal;
  547. IntPtr buf = Marshal.AllocHGlobal (8192);
  548. if (uname (buf) != 0) {
  549. Marshal.FreeHGlobal (buf);
  550. suspendSignal = -1;
  551. return suspendSignal;
  552. }
  553. try {
  554. switch (Marshal.PtrToStringAnsi (buf)) {
  555. case "Darwin":
  556. case "DragonFly":
  557. case "FreeBSD":
  558. case "NetBSD":
  559. case "OpenBSD":
  560. suspendSignal = 18;
  561. break;
  562. case "Linux":
  563. // TODO: should fetch the machine name and
  564. // if it is MIPS return 24
  565. suspendSignal = 20;
  566. break;
  567. case "Solaris":
  568. suspendSignal = 24;
  569. break;
  570. default:
  571. suspendSignal = -1;
  572. break;
  573. }
  574. return suspendSignal;
  575. } finally {
  576. Marshal.FreeHGlobal (buf);
  577. }
  578. }
  579. /// <summary>
  580. /// Suspends the process by sending SIGTSTP to itself
  581. /// </summary>
  582. /// <returns>The suspend.</returns>
  583. static public bool Suspend ()
  584. {
  585. int signal = GetSuspendSignal ();
  586. if (signal == -1)
  587. return false;
  588. killpg (0, signal);
  589. return true;
  590. }
  591. }
  592. internal class NetDriver : ConsoleDriver {
  593. public override int Cols => Console.WindowWidth;
  594. public override int Rows => Console.WindowHeight;
  595. int [,,] contents;
  596. bool [] dirtyLine;
  597. static int MakeColor (int fg, int bg)
  598. {
  599. return (fg << 16) | bg;
  600. }
  601. void UpdateOffscreen ()
  602. {
  603. int cols = Cols;
  604. int rows = Rows;
  605. contents = new int [cols, rows, 3];
  606. for (int r = 0; r < rows; r++) {
  607. for (int c = 0; c < cols; c++) {
  608. contents [r, c, 0] = ' ';
  609. contents [r, c, 1] = MakeColor (7, 0);
  610. contents [r, c, 2] = 0;
  611. }
  612. }
  613. dirtyLine = new bool [rows];
  614. for (int row = 0; row < rows; row++)
  615. dirtyLine [row] = true;
  616. }
  617. public NetDriver ()
  618. {
  619. UpdateOffscreen ();
  620. }
  621. // Current row, and current col, tracked by Move/AddCh only
  622. int ccol, crow;
  623. public override void Move (int col, int row)
  624. {
  625. ccol = col;
  626. crow = row;
  627. }
  628. public override void AddCh (int rune)
  629. {
  630. if (Clip.Contains (ccol, crow)) {
  631. contents [crow, ccol, 0] = rune;
  632. contents [crow, ccol, 2] = 1;
  633. }
  634. ccol++;
  635. if (ccol == Cols) {
  636. ccol = 0;
  637. if (crow + 1 < Rows)
  638. crow++;
  639. }
  640. }
  641. public override void AddSpecial (SpecialChar ch)
  642. {
  643. AddCh ('*');
  644. }
  645. public override void AddStr (string str)
  646. {
  647. foreach (var rune in str)
  648. AddCh ((int)rune);
  649. }
  650. public override void DrawFrame(Rect region, int padding, bool fill)
  651. {
  652. int width = region.Width;
  653. int height = region.Height;
  654. int b;
  655. Move (region.X, region.Y);
  656. AddCh ('+');
  657. for (b = 0; b < width - 2; b++)
  658. AddCh ('-');
  659. AddCh ('+');
  660. for (b = 1; b < height - 1; b++) {
  661. Move (region.X, region.Y + b);
  662. AddCh ('|');
  663. if (fill) {
  664. for (int x = 1; x < width - 1; x++)
  665. AddCh (' ');
  666. } else
  667. Move (region.X + width - 1, region.Y + b);
  668. AddCh ('|');
  669. }
  670. Move (region.X, region.Y + height - 1);
  671. AddCh ('+');
  672. for (b = 0; b < width - 2; b++)
  673. AddCh ('-');
  674. AddCh ('+');
  675. }
  676. public override void End()
  677. {
  678. }
  679. public override void Init(Action terminalResized)
  680. {
  681. }
  682. public override void RedrawTop()
  683. {
  684. int rows = Rows;
  685. int cols = Cols;
  686. Console.CursorTop = 0;
  687. Console.CursorLeft = 0;
  688. for (int row = 0; row < rows; row++) {
  689. dirtyLine [row] = false;
  690. for (int col = 0; col < cols; col++) {
  691. contents [row, col, 2] = 0;
  692. Console.Write ((char)contents [row, col, 0]);
  693. }
  694. }
  695. }
  696. public override void Refresh()
  697. {
  698. int rows = Rows;
  699. int cols = Cols;
  700. for (int row = 0; row < rows; row++) {
  701. if (!dirtyLine [row])
  702. continue;
  703. dirtyLine [row] = false;
  704. for (int col = 0; col < cols; col++) {
  705. if (contents [row, col, 2] != 1)
  706. continue;
  707. Console.CursorTop = row;
  708. Console.CursorLeft = col;
  709. for (; col < cols && contents [row, col, 2] == 1; col++) {
  710. Console.Write ((char)contents [row, col, 0]);
  711. contents [row, col, 2] = 0;
  712. }
  713. }
  714. }
  715. }
  716. public override void StartReportingMouseMoves()
  717. {
  718. }
  719. public override void StopReportingMouseMoves()
  720. {
  721. }
  722. public override void Suspend()
  723. {
  724. }
  725. public override void SetAttribute(Attribute c)
  726. {
  727. throw new NotImplementedException();
  728. }
  729. public override void PrepareToRun(MainLoop mainLoop, Action<KeyEvent> target, Action<MouseEvent> mouse)
  730. {
  731. throw new NotImplementedException();
  732. }
  733. public override void SetColors(ConsoleColor foreground, ConsoleColor background)
  734. {
  735. throw new NotImplementedException();
  736. }
  737. public override void SetColors(short foregroundColorId, short backgroundColorId)
  738. {
  739. throw new NotImplementedException();
  740. }
  741. }
  742. }