Driver.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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> keyHandler, Action<MouseEvent> mouseHandler);
  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 virtual void DrawFrame (Rect region, int padding, bool fill)
  257. {
  258. int width = region.Width;
  259. int height = region.Height;
  260. int b;
  261. int fwidth = width - padding * 2;
  262. int fheight = height - 1 - padding;
  263. Move (region.X, region.Y);
  264. if (padding > 0) {
  265. for (int l = 0; l < padding; l++)
  266. for (b = 0; b < width; b++)
  267. AddRune (' ');
  268. }
  269. Move (region.X, region.Y + padding);
  270. for (int c = 0; c < padding; c++)
  271. AddRune (' ');
  272. AddRune (ULCorner);
  273. for (b = 0; b < fwidth - 2; b++)
  274. AddRune (HLine);
  275. AddRune (URCorner);
  276. for (int c = 0; c < padding; c++)
  277. AddRune (' ');
  278. for (b = 1 + padding; b < fheight; b++) {
  279. Move (region.X, region.Y + b);
  280. for (int c = 0; c < padding; c++)
  281. AddRune (' ');
  282. AddRune (VLine);
  283. if (fill) {
  284. for (int x = 1; x < fwidth - 1; x++)
  285. AddRune (' ');
  286. } else
  287. Move (region.X + fwidth - 1, region.Y + b);
  288. AddRune (VLine);
  289. for (int c = 0; c < padding; c++)
  290. AddRune (' ');
  291. }
  292. Move (region.X, region.Y + fheight);
  293. for (int c = 0; c < padding; c++)
  294. AddRune (' ');
  295. AddRune (LLCorner);
  296. for (b = 0; b < fwidth - 2; b++)
  297. AddRune (HLine);
  298. AddRune (LRCorner);
  299. for (int c = 0; c < padding; c++)
  300. AddRune (' ');
  301. if (padding > 0) {
  302. Move (region.X, region.Y + height - padding);
  303. for (int l = 0; l < padding; l++)
  304. for (b = 0; b < width; b++)
  305. AddRune (' ');
  306. }
  307. }
  308. /// <summary>
  309. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  310. /// </summary>
  311. public abstract void Suspend ();
  312. Rect clip;
  313. /// <summary>
  314. /// Controls the current clipping region that AddRune/AddStr is subject to.
  315. /// </summary>
  316. /// <value>The clip.</value>
  317. public Rect Clip {
  318. get => clip;
  319. set => this.clip = value;
  320. }
  321. public abstract void StartReportingMouseMoves ();
  322. public abstract void StopReportingMouseMoves ();
  323. /// <summary>
  324. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  325. /// </summary>
  326. public abstract void UncookMouse ();
  327. /// <summary>
  328. /// Enables the cooked event processing from the mouse driver
  329. /// </summary>
  330. public abstract void CookMouse ();
  331. /// <summary>
  332. /// Horizontal line character.
  333. /// </summary>
  334. public Rune HLine;
  335. /// <summary>
  336. /// Vertical line character.
  337. /// </summary>
  338. public Rune VLine;
  339. /// <summary>
  340. /// Stipple pattern
  341. /// </summary>
  342. public Rune Stipple;
  343. /// <summary>
  344. /// Diamond character
  345. /// </summary>
  346. public Rune Diamond;
  347. /// <summary>
  348. /// Upper left corner
  349. /// </summary>
  350. public Rune ULCorner;
  351. /// <summary>
  352. /// Lower left corner
  353. /// </summary>
  354. public Rune LLCorner;
  355. /// <summary>
  356. /// Upper right corner
  357. /// </summary>
  358. public Rune URCorner;
  359. /// <summary>
  360. /// Lower right corner
  361. /// </summary>
  362. public Rune LRCorner;
  363. /// <summary>
  364. /// Left tee
  365. /// </summary>
  366. public Rune LeftTee;
  367. /// <summary>
  368. /// Right tee
  369. /// </summary>
  370. public Rune RightTee;
  371. /// <summary>
  372. /// Top tee
  373. /// </summary>
  374. public Rune TopTee;
  375. /// <summary>
  376. /// The bottom tee.
  377. /// </summary>
  378. public Rune BottomTee;
  379. }
  380. /// <summary>
  381. /// This is the Curses driver for the gui.cs/Terminal framework.
  382. /// </summary>
  383. internal class CursesDriver : ConsoleDriver {
  384. Action terminalResized;
  385. public override int Cols => Curses.Cols;
  386. public override int Rows => Curses.Lines;
  387. // Current row, and current col, tracked by Move/AddRune only
  388. int ccol, crow;
  389. bool needMove;
  390. public override void Move (int col, int row)
  391. {
  392. ccol = col;
  393. crow = row;
  394. if (Clip.Contains (col, row)) {
  395. Curses.move (row, col);
  396. needMove = false;
  397. } else {
  398. Curses.move (Clip.Y, Clip.X);
  399. needMove = true;
  400. }
  401. }
  402. static bool sync;
  403. public override void AddRune (Rune rune)
  404. {
  405. if (Clip.Contains (ccol, crow)) {
  406. if (needMove) {
  407. Curses.move (crow, ccol);
  408. needMove = false;
  409. }
  410. Curses.addch ((int)(uint)rune);
  411. } else
  412. needMove = true;
  413. if (sync)
  414. Application.Driver.Refresh ();
  415. ccol++;
  416. }
  417. public override void AddStr (ustring str)
  418. {
  419. // TODO; optimize this to determine if the str fits in the clip region, and if so, use Curses.addstr directly
  420. foreach (var rune in str)
  421. AddRune (rune);
  422. }
  423. public override void Refresh () => Curses.refresh ();
  424. public override void End () => Curses.endwin ();
  425. public override void RedrawTop () => window.redrawwin ();
  426. public override void SetAttribute (Attribute c) => Curses.attrset (c.value);
  427. public Curses.Window window;
  428. static short last_color_pair = 16;
  429. static Attribute MakeColor (short f, short b)
  430. {
  431. Curses.InitColorPair (++last_color_pair, f, b);
  432. return new Attribute () { value = Curses.ColorPair (last_color_pair) };
  433. }
  434. int [,] colorPairs = new int [16, 16];
  435. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  436. {
  437. int f = (short)foreground;
  438. int b = (short)background;
  439. var v = colorPairs [f, b];
  440. if ((v & 0x10000) == 0) {
  441. b = b & 0x7;
  442. bool bold = (f & 0x8) != 0;
  443. f = f & 0x7;
  444. v = MakeColor ((short)f, (short)b) | (bold ? Curses.A_BOLD : 0);
  445. colorPairs [(int)foreground, (int)background] = v | 0x1000;
  446. }
  447. SetAttribute (v & 0xffff);
  448. }
  449. Dictionary<int, int> rawPairs = new Dictionary<int, int> ();
  450. public override void SetColors (short foreColorId, short backgroundColorId)
  451. {
  452. int key = (((ushort)foreColorId << 16)) | (ushort)backgroundColorId;
  453. if (!rawPairs.TryGetValue (key, out var v)) {
  454. v = MakeColor (foreColorId, backgroundColorId);
  455. rawPairs [key] = v;
  456. }
  457. SetAttribute (v);
  458. }
  459. static Key MapCursesKey (int cursesKey)
  460. {
  461. switch (cursesKey) {
  462. case Curses.KeyF1: return Key.F1;
  463. case Curses.KeyF2: return Key.F2;
  464. case Curses.KeyF3: return Key.F3;
  465. case Curses.KeyF4: return Key.F4;
  466. case Curses.KeyF5: return Key.F5;
  467. case Curses.KeyF6: return Key.F6;
  468. case Curses.KeyF7: return Key.F7;
  469. case Curses.KeyF8: return Key.F8;
  470. case Curses.KeyF9: return Key.F9;
  471. case Curses.KeyF10: return Key.F10;
  472. case Curses.KeyUp: return Key.CursorUp;
  473. case Curses.KeyDown: return Key.CursorDown;
  474. case Curses.KeyLeft: return Key.CursorLeft;
  475. case Curses.KeyRight: return Key.CursorRight;
  476. case Curses.KeyHome: return Key.Home;
  477. case Curses.KeyEnd: return Key.End;
  478. case Curses.KeyNPage: return Key.PageDown;
  479. case Curses.KeyPPage: return Key.PageUp;
  480. case Curses.KeyDeleteChar: return Key.DeleteChar;
  481. case Curses.KeyInsertChar: return Key.InsertChar;
  482. case Curses.KeyBackTab: return Key.BackTab;
  483. default: return Key.Unknown;
  484. }
  485. }
  486. static MouseEvent ToDriverMouse (Curses.MouseEvent cev)
  487. {
  488. return new MouseEvent () {
  489. X = cev.X,
  490. Y = cev.Y,
  491. Flags = (MouseFlags)cev.ButtonState
  492. };
  493. }
  494. void ProcessInput (Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  495. {
  496. int wch;
  497. var code = Curses.get_wch (out wch);
  498. if (code == Curses.KEY_CODE_YES) {
  499. if (wch == Curses.KeyResize) {
  500. if (Curses.CheckWinChange ()) {
  501. terminalResized ();
  502. return;
  503. }
  504. }
  505. if (wch == Curses.KeyMouse) {
  506. Curses.MouseEvent ev;
  507. Curses.getmouse (out ev);
  508. mouseHandler (ToDriverMouse (ev));
  509. return;
  510. }
  511. keyHandler (new KeyEvent (MapCursesKey (wch)));
  512. return;
  513. }
  514. // Special handling for ESC, we want to try to catch ESC+letter to simulate alt-letter as well as Alt-Fkey
  515. if (wch == 27) {
  516. Curses.timeout (100);
  517. code = Curses.get_wch (out wch);
  518. if (code == Curses.KEY_CODE_YES)
  519. keyHandler (new KeyEvent (Key.AltMask | MapCursesKey (wch)));
  520. if (code == 0) {
  521. KeyEvent key;
  522. // The ESC-number handling, debatable.
  523. if (wch >= '1' && wch <= '9')
  524. key = new KeyEvent ((Key)((int)Key.F1 + (wch - '0' - 1)));
  525. else if (wch == '0')
  526. key = new KeyEvent (Key.F10);
  527. else if (wch == 27)
  528. key = new KeyEvent ((Key)wch);
  529. else
  530. key = new KeyEvent (Key.AltMask | (Key)wch);
  531. keyHandler (key);
  532. } else
  533. keyHandler (new KeyEvent (Key.Esc));
  534. } else
  535. keyHandler (new KeyEvent ((Key)wch));
  536. }
  537. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  538. {
  539. Curses.timeout (-1);
  540. mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
  541. ProcessInput (keyHandler, mouseHandler);
  542. return true;
  543. });
  544. }
  545. Curses.Event oldMouseEvents, reportableMouseEvents;
  546. public override void Init (Action terminalResized)
  547. {
  548. if (window != null)
  549. return;
  550. try {
  551. window = Curses.initscr ();
  552. } catch (Exception e) {
  553. Console.WriteLine ("Curses failed to initialize, the exception is: " + e);
  554. }
  555. Curses.raw ();
  556. Curses.noecho ();
  557. Curses.Window.Standard.keypad (true);
  558. reportableMouseEvents = Curses.mousemask (Curses.Event.AllEvents | Curses.Event.ReportMousePosition, out oldMouseEvents);
  559. this.terminalResized = terminalResized;
  560. StartReportingMouseMoves ();
  561. HLine = Curses.ACS_HLINE;
  562. VLine = Curses.ACS_VLINE;
  563. Stipple = Curses.ACS_CKBOARD;
  564. Diamond = Curses.ACS_DIAMOND;
  565. ULCorner = Curses.ACS_ULCORNER;
  566. LLCorner = Curses.ACS_LLCORNER;
  567. URCorner = Curses.ACS_URCORNER;
  568. LRCorner = Curses.ACS_LRCORNER;
  569. LeftTee = Curses.ACS_LTEE;
  570. RightTee = Curses.ACS_RTEE;
  571. TopTee = Curses.ACS_TTEE;
  572. BottomTee = Curses.ACS_BTEE;
  573. Colors.Base = new ColorScheme ();
  574. Colors.Dialog = new ColorScheme ();
  575. Colors.Menu = new ColorScheme ();
  576. Colors.Error = new ColorScheme ();
  577. Clip = new Rect (0, 0, Cols, Rows);
  578. if (Curses.HasColors) {
  579. Curses.StartColor ();
  580. Curses.UseDefaultColors ();
  581. Colors.Base.Normal = MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLUE);
  582. Colors.Base.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  583. Colors.Base.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLUE);
  584. Colors.Base.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  585. // Focused,
  586. // Selected, Hot: Yellow on Black
  587. // Selected, text: white on black
  588. // Unselected, hot: yellow on cyan
  589. // unselected, text: same as unfocused
  590. Colors.Menu.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLACK);
  591. Colors.Menu.Focus = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLACK);
  592. Colors.Menu.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  593. Colors.Menu.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_CYAN);
  594. Colors.Dialog.Normal = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  595. Colors.Dialog.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  596. Colors.Dialog.HotNormal = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_WHITE);
  597. Colors.Dialog.HotFocus = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_CYAN);
  598. Colors.Error.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_RED);
  599. Colors.Error.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  600. Colors.Error.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_RED);
  601. Colors.Error.HotFocus = Colors.Error.HotNormal;
  602. } else {
  603. Colors.Base.Normal = Curses.A_NORMAL;
  604. Colors.Base.Focus = Curses.A_REVERSE;
  605. Colors.Base.HotNormal = Curses.A_BOLD;
  606. Colors.Base.HotFocus = Curses.A_BOLD | Curses.A_REVERSE;
  607. Colors.Menu.Normal = Curses.A_REVERSE;
  608. Colors.Menu.Focus = Curses.A_NORMAL;
  609. Colors.Menu.HotNormal = Curses.A_BOLD;
  610. Colors.Menu.HotFocus = Curses.A_NORMAL;
  611. Colors.Dialog.Normal = Curses.A_REVERSE;
  612. Colors.Dialog.Focus = Curses.A_NORMAL;
  613. Colors.Dialog.HotNormal = Curses.A_BOLD;
  614. Colors.Dialog.HotFocus = Curses.A_NORMAL;
  615. Colors.Error.Normal = Curses.A_BOLD;
  616. Colors.Error.Focus = Curses.A_BOLD | Curses.A_REVERSE;
  617. Colors.Error.HotNormal = Curses.A_BOLD | Curses.A_REVERSE;
  618. Colors.Error.HotFocus = Curses.A_REVERSE;
  619. }
  620. }
  621. public override void Suspend ()
  622. {
  623. StopReportingMouseMoves ();
  624. Platform.Suspend ();
  625. Curses.Window.Standard.redrawwin ();
  626. Curses.refresh ();
  627. StartReportingMouseMoves ();
  628. }
  629. public override void StartReportingMouseMoves()
  630. {
  631. Console.Out.Write ("\x1b[?1003h");
  632. Console.Out.Flush ();
  633. }
  634. public override void StopReportingMouseMoves()
  635. {
  636. Console.Out.Write ("\x1b[?1003l");
  637. Console.Out.Flush ();
  638. }
  639. int lastMouseInterval;
  640. bool mouseGrabbed;
  641. public override void UncookMouse()
  642. {
  643. if (mouseGrabbed)
  644. return;
  645. lastMouseInterval = Curses.mouseinterval (0);
  646. mouseGrabbed = true;
  647. }
  648. public override void CookMouse()
  649. {
  650. mouseGrabbed = false;
  651. Curses.mouseinterval (lastMouseInterval);
  652. }
  653. }
  654. internal static class Platform {
  655. [DllImport ("libc")]
  656. static extern int uname (IntPtr buf);
  657. [DllImport ("libc")]
  658. static extern int killpg (int pgrp, int pid);
  659. static int suspendSignal;
  660. static int GetSuspendSignal ()
  661. {
  662. if (suspendSignal != 0)
  663. return suspendSignal;
  664. IntPtr buf = Marshal.AllocHGlobal (8192);
  665. if (uname (buf) != 0) {
  666. Marshal.FreeHGlobal (buf);
  667. suspendSignal = -1;
  668. return suspendSignal;
  669. }
  670. try {
  671. switch (Marshal.PtrToStringAnsi (buf)) {
  672. case "Darwin":
  673. case "DragonFly":
  674. case "FreeBSD":
  675. case "NetBSD":
  676. case "OpenBSD":
  677. suspendSignal = 18;
  678. break;
  679. case "Linux":
  680. // TODO: should fetch the machine name and
  681. // if it is MIPS return 24
  682. suspendSignal = 20;
  683. break;
  684. case "Solaris":
  685. suspendSignal = 24;
  686. break;
  687. default:
  688. suspendSignal = -1;
  689. break;
  690. }
  691. return suspendSignal;
  692. } finally {
  693. Marshal.FreeHGlobal (buf);
  694. }
  695. }
  696. /// <summary>
  697. /// Suspends the process by sending SIGTSTP to itself
  698. /// </summary>
  699. /// <returns>The suspend.</returns>
  700. static public bool Suspend ()
  701. {
  702. int signal = GetSuspendSignal ();
  703. if (signal == -1)
  704. return false;
  705. killpg (0, signal);
  706. return true;
  707. }
  708. }
  709. internal class NetDriver : ConsoleDriver {
  710. public override int Cols => Console.WindowWidth;
  711. public override int Rows => Console.WindowHeight;
  712. // The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  713. int [,,] contents;
  714. bool [] dirtyLine;
  715. void UpdateOffscreen ()
  716. {
  717. int cols = Cols;
  718. int rows = Rows;
  719. contents = new int [rows, cols, 3];
  720. for (int r = 0; r < rows; r++) {
  721. for (int c = 0; c < cols; c++) {
  722. contents [r, c, 0] = ' ';
  723. contents [r, c, 1] = MakeColor (ConsoleColor.Gray, ConsoleColor.Black);
  724. contents [r, c, 2] = 0;
  725. }
  726. }
  727. dirtyLine = new bool [rows];
  728. for (int row = 0; row < rows; row++)
  729. dirtyLine [row] = true;
  730. }
  731. static bool sync;
  732. public NetDriver ()
  733. {
  734. UpdateOffscreen ();
  735. }
  736. // Current row, and current col, tracked by Move/AddCh only
  737. int ccol, crow;
  738. public override void Move (int col, int row)
  739. {
  740. ccol = col;
  741. crow = row;
  742. }
  743. public override void AddRune (Rune rune)
  744. {
  745. if (Clip.Contains (ccol, crow)) {
  746. contents [crow, ccol, 0] = (int) (uint) rune;
  747. contents [crow, ccol, 1] = currentAttribute;
  748. contents [crow, ccol, 2] = 1;
  749. }
  750. ccol++;
  751. if (ccol == Cols) {
  752. ccol = 0;
  753. if (crow + 1 < Rows)
  754. crow++;
  755. }
  756. if (sync)
  757. RedrawTop ();
  758. }
  759. public override void AddStr (ustring str)
  760. {
  761. foreach (var rune in str)
  762. AddRune (rune);
  763. }
  764. public override void End ()
  765. {
  766. }
  767. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  768. {
  769. // Encode the colors into the int value.
  770. return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
  771. }
  772. public override void Init (Action terminalResized)
  773. {
  774. Colors.Base = new ColorScheme ();
  775. Colors.Dialog = new ColorScheme ();
  776. Colors.Menu = new ColorScheme ();
  777. Colors.Error = new ColorScheme ();
  778. Clip = new Rect (0, 0, Cols, Rows);
  779. HLine = '\u2500';
  780. VLine = '\u2502';
  781. Stipple = '\u2592';
  782. Diamond = '\u25c6';
  783. ULCorner = '\u250C';
  784. LLCorner = '\u2514';
  785. URCorner = '\u2510';
  786. LRCorner = '\u2518';
  787. LeftTee = '\u251c';
  788. RightTee = '\u2524';
  789. TopTee = '\u22a4';
  790. BottomTee = '\u22a5';
  791. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  792. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  793. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  794. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  795. // Focused,
  796. // Selected, Hot: Yellow on Black
  797. // Selected, text: white on black
  798. // Unselected, hot: yellow on cyan
  799. // unselected, text: same as unfocused
  800. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  801. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  802. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  803. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  804. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  805. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  806. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  807. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  808. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  809. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  810. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  811. Colors.Error.HotFocus = Colors.Error.HotNormal;
  812. }
  813. int redrawColor = -1;
  814. void SetColor (int color)
  815. {
  816. redrawColor = color;
  817. Console.BackgroundColor = (ConsoleColor)(color & 0xffff);
  818. Console.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  819. }
  820. public override void RedrawTop ()
  821. {
  822. int rows = Rows;
  823. int cols = Cols;
  824. Console.CursorTop = 0;
  825. Console.CursorLeft = 0;
  826. for (int row = 0; row < rows; row++) {
  827. dirtyLine [row] = false;
  828. for (int col = 0; col < cols; col++) {
  829. contents [row, col, 2] = 0;
  830. var color = contents [row, col, 1];
  831. if (color != redrawColor)
  832. SetColor (color);
  833. Console.Write ((char)contents [row, col, 0]);
  834. }
  835. }
  836. }
  837. public override void Refresh ()
  838. {
  839. int rows = Rows;
  840. int cols = Cols;
  841. for (int row = 0; row < rows; row++) {
  842. if (!dirtyLine [row])
  843. continue;
  844. dirtyLine [row] = false;
  845. for (int col = 0; col < cols; col++) {
  846. if (contents [row, col, 2] != 1)
  847. continue;
  848. Console.CursorTop = row;
  849. Console.CursorLeft = col;
  850. for (; col < cols && contents [row, col, 2] == 1; col++) {
  851. var color = contents [row, col, 1];
  852. if (color != redrawColor)
  853. SetColor (color);
  854. Console.Write ((char)contents [row, col, 0]);
  855. contents [row, col, 2] = 0;
  856. }
  857. }
  858. }
  859. }
  860. public override void StartReportingMouseMoves()
  861. {
  862. }
  863. public override void StopReportingMouseMoves ()
  864. {
  865. }
  866. public override void Suspend ()
  867. {
  868. }
  869. int currentAttribute;
  870. public override void SetAttribute (Attribute c)
  871. {
  872. currentAttribute = c.value;
  873. }
  874. Key MapKey (ConsoleKeyInfo keyInfo)
  875. {
  876. var key = keyInfo.Key;
  877. if (key >= ConsoleKey.A && key <= ConsoleKey.Z){
  878. var delta = key - ConsoleKey.A;
  879. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  880. return (Key)((uint)Key.ControlA + delta);
  881. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  882. return (Key) (((uint)Key.AltMask) | ((uint)'A' + delta));
  883. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  884. return (Key)((uint)'A' + delta);
  885. else
  886. return (Key)((uint)'a' + delta);
  887. }
  888. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  889. var delta = key - ConsoleKey.F1;
  890. return (Key)(ConsoleKey.F1 + delta);
  891. }
  892. switch (keyInfo.Key){
  893. case ConsoleKey.Tab:
  894. return Key.ControlT;
  895. case ConsoleKey.Escape:
  896. return Key.Esc;
  897. case ConsoleKey.Home:
  898. return Key.Home;
  899. case ConsoleKey.End:
  900. return Key.End;
  901. case ConsoleKey.LeftArrow:
  902. return Key.CursorLeft;
  903. case ConsoleKey.RightArrow:
  904. return Key.CursorRight;
  905. case ConsoleKey.UpArrow:
  906. return Key.CursorUp;
  907. case ConsoleKey.DownArrow:
  908. return Key.CursorDown;
  909. case ConsoleKey.PageUp:
  910. return Key.PageUp;
  911. case ConsoleKey.PageDown:
  912. return Key.PageDown;
  913. case ConsoleKey.Enter:
  914. return Key.Enter;
  915. case ConsoleKey.Spacebar:
  916. return Key.Space;
  917. case ConsoleKey.Backspace:
  918. return Key.Backspace;
  919. case ConsoleKey.Delete:
  920. return Key.Delete;
  921. }
  922. return (Key)(0xffffffff);
  923. }
  924. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  925. {
  926. mainLoop.WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  927. var map = MapKey (consoleKey);
  928. if (map == (Key) 0xffffffff)
  929. return;
  930. keyHandler (new KeyEvent (map));
  931. };
  932. }
  933. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  934. {
  935. throw new NotImplementedException ();
  936. }
  937. public override void SetColors (short foregroundColorId, short backgroundColorId)
  938. {
  939. throw new NotImplementedException ();
  940. }
  941. public override void CookMouse ()
  942. {
  943. }
  944. public override void UncookMouse ()
  945. {
  946. }
  947. }
  948. }