Driver.cs 23 KB

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