Driver.cs 22 KB

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