Driver.cs 24 KB

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