Driver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 Mono.Terminal;
  10. using Unix.Terminal;
  11. namespace Terminal {
  12. /// <summary>
  13. /// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
  14. /// </summary>
  15. public enum Color {
  16. Black,
  17. Blue,
  18. Green,
  19. Cyan,
  20. Red,
  21. Magenta,
  22. Brown,
  23. Gray,
  24. DarkGray,
  25. BrightBlue,
  26. BrightGreen,
  27. BrighCyan,
  28. BrightRed,
  29. BrightMagenta,
  30. BrightYellow,
  31. White
  32. }
  33. /// <summary>
  34. /// Attributes are used as elements that contain both a foreground and a background or platform specific features
  35. /// </summary>
  36. /// <remarks>
  37. /// Attributes are needed to map colors to terminal capabilities that might lack colors, on color
  38. /// scenarios, they encode both the foreground and the background color and are used in the ColorScheme
  39. /// class to define color schemes that can be used in your application.
  40. /// </remarks>
  41. public struct Attribute {
  42. internal int value;
  43. public Attribute (int v)
  44. {
  45. value = v;
  46. }
  47. public static implicit operator int (Attribute c) => c.value;
  48. public static implicit operator Attribute (int v) => new Attribute (v);
  49. }
  50. /// <summary>
  51. /// Color scheme definitions
  52. /// </summary>
  53. public class ColorScheme {
  54. public Attribute Normal;
  55. public Attribute Focus;
  56. public Attribute HotNormal;
  57. public Attribute HotFocus;
  58. }
  59. public static class Colors {
  60. public static ColorScheme Base, Dialog, Menu, Error;
  61. }
  62. public enum SpecialChar {
  63. HLine,
  64. }
  65. public abstract class ConsoleDriver {
  66. public abstract int Cols { get; }
  67. public abstract int Rows { get; }
  68. public abstract void Init (Action terminalResized);
  69. public abstract void Move (int col, int row);
  70. public abstract void AddCh (int ch);
  71. public abstract void AddStr (string str);
  72. public abstract void PrepareToRun (MainLoop mainLoop, Responder target);
  73. public abstract void Refresh ();
  74. public abstract void End ();
  75. public abstract void RedrawTop ();
  76. public abstract void SetAttribute (Attribute c);
  77. // Set Colors from limit sets of colors
  78. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  79. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  80. // that independently with the R, G, B values.
  81. public abstract void SetColors (short foreColorId, short backgroundColorId);
  82. public abstract void DrawFrame (Rect region, bool fill);
  83. public abstract void AddSpecial (SpecialChar ch);
  84. Rect clip;
  85. public Rect Clip {
  86. get => clip;
  87. set => this.clip = value;
  88. }
  89. }
  90. public class CursesDriver : ConsoleDriver {
  91. Action terminalResized;
  92. public override int Cols => Curses.Cols;
  93. public override int Rows => Curses.Lines;
  94. // Current row, and current col, tracked by Move/AddCh only
  95. int ccol, crow;
  96. bool needMove;
  97. public override void Move (int col, int row)
  98. {
  99. ccol = col;
  100. crow = row;
  101. if (Clip.Contains (col, row)) {
  102. Curses.move (row, col);
  103. needMove = false;
  104. } else {
  105. Curses.move (Clip.Y, Clip.X);
  106. needMove = true;
  107. }
  108. }
  109. public override void AddCh (int ch)
  110. {
  111. if (Clip.Contains (ccol, crow)) {
  112. if (needMove) {
  113. Curses.move (crow, ccol);
  114. needMove = false;
  115. }
  116. Curses.addch (ch);
  117. } else
  118. needMove = true;
  119. ccol++;
  120. }
  121. public override void AddSpecial (SpecialChar ch)
  122. {
  123. switch (ch) {
  124. case SpecialChar.HLine:
  125. AddCh (Curses.ACS_HLINE);
  126. break;
  127. }
  128. }
  129. public override void AddStr (string str)
  130. {
  131. // TODO; optimize this to determine if the str fits in the clip region, and if so, use Curses.addstr directly
  132. foreach (var c in str)
  133. AddCh ((int)c);
  134. }
  135. public override void Refresh () => Curses.refresh ();
  136. public override void End () => Curses.endwin ();
  137. public override void RedrawTop () => window.redrawwin ();
  138. public override void SetAttribute (Attribute c) => Curses.attrset (c.value);
  139. public Curses.Window window;
  140. static short last_color_pair = 16;
  141. static Attribute MakeColor (short f, short b)
  142. {
  143. Curses.InitColorPair (++last_color_pair, f, b);
  144. return new Attribute () { value = Curses.ColorPair (last_color_pair) };
  145. }
  146. int [,] colorPairs = new int [16, 16];
  147. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  148. {
  149. int f = (short)foreground;
  150. int b = (short)background;
  151. var v = colorPairs [f, b];
  152. if ((v & 0x10000) == 0) {
  153. b = b & 0x7;
  154. bool bold = (f & 0x8) != 0;
  155. f = f & 0x7;
  156. v = MakeColor ((short)f, (short)b) | (bold ? Curses.A_BOLD : 0);
  157. colorPairs [(int)foreground, (int)background] = v | 0x1000;
  158. }
  159. SetAttribute (v & 0xffff);
  160. }
  161. Dictionary<int, int> rawPairs = new Dictionary<int, int> ();
  162. public override void SetColors (short foreColorId, short backgroundColorId)
  163. {
  164. int key = (((ushort)foreColorId << 16)) | (ushort)backgroundColorId;
  165. if (!rawPairs.TryGetValue (key, out var v)) {
  166. v = MakeColor (foreColorId, backgroundColorId);
  167. rawPairs [key] = v;
  168. }
  169. SetAttribute (v);
  170. }
  171. static Key MapCursesKey (int cursesKey)
  172. {
  173. switch (cursesKey) {
  174. case Curses.KeyF1: return Key.F1;
  175. case Curses.KeyF2: return Key.F2;
  176. case Curses.KeyF3: return Key.F3;
  177. case Curses.KeyF4: return Key.F4;
  178. case Curses.KeyF5: return Key.F5;
  179. case Curses.KeyF6: return Key.F6;
  180. case Curses.KeyF7: return Key.F7;
  181. case Curses.KeyF8: return Key.F8;
  182. case Curses.KeyF9: return Key.F9;
  183. case Curses.KeyF10: return Key.F10;
  184. case Curses.KeyUp: return Key.CursorUp;
  185. case Curses.KeyDown: return Key.CursorDown;
  186. case Curses.KeyLeft: return Key.CursorLeft;
  187. case Curses.KeyRight: return Key.CursorRight;
  188. case Curses.KeyHome: return Key.Home;
  189. case Curses.KeyEnd: return Key.End;
  190. case Curses.KeyNPage: return Key.PageDown;
  191. case Curses.KeyPPage: return Key.PageUp;
  192. case Curses.KeyDeleteChar: return Key.DeleteChar;
  193. case Curses.KeyInsertChar: return Key.InsertChar;
  194. case Curses.KeyBackTab: return Key.BackTab;
  195. default: return Key.Unknown;
  196. }
  197. }
  198. void ProcessInput (Responder handler)
  199. {
  200. int wch;
  201. var code = Curses.get_wch (out wch);
  202. if (code == Curses.KEY_CODE_YES) {
  203. if (wch == Curses.KeyResize) {
  204. if (Curses.CheckWinChange ()) {
  205. terminalResized ();
  206. return;
  207. }
  208. }
  209. if (code == Curses.KeyMouse) {
  210. // TODO
  211. // Curses.MouseEvent ev;
  212. // Curses.getmouse (out ev);
  213. // handler.HandleMouse ();
  214. return;
  215. }
  216. handler.ProcessKey (new KeyEvent (MapCursesKey (wch)));
  217. return;
  218. }
  219. // Special handling for ESC, we want to try to catch ESC+letter to simulate alt-letter as well as Alt-Fkey
  220. if (wch == 27) {
  221. Curses.timeout (100);
  222. code = Curses.get_wch (out wch);
  223. if (code == Curses.KEY_CODE_YES)
  224. handler.ProcessKey (new KeyEvent (Key.AltMask | MapCursesKey (wch)));
  225. if (code == 0) {
  226. KeyEvent key;
  227. // The ESC-number handling, debatable.
  228. if (wch >= '1' && wch <= '9')
  229. key = new KeyEvent ((Key)((int)Key.F1 + (wch - '0' - 1)));
  230. else if (wch == '0')
  231. key = new KeyEvent (Key.F10);
  232. else if (wch == 27)
  233. key = new KeyEvent ((Key)wch);
  234. else
  235. key = new KeyEvent (Key.AltMask | (Key)wch);
  236. handler.ProcessKey (key);
  237. } else
  238. handler.ProcessKey (new KeyEvent (Key.Esc));
  239. } else
  240. handler.ProcessKey (new KeyEvent ((Key)wch));
  241. }
  242. public override void PrepareToRun (MainLoop mainLoop, Responder handler)
  243. {
  244. Curses.timeout (-1);
  245. mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
  246. ProcessInput (handler);
  247. return true;
  248. });
  249. }
  250. public override void DrawFrame (Rect region, bool fill)
  251. {
  252. int width = region.Width;
  253. int height = region.Height;
  254. int b;
  255. Move (region.X, region.Y);
  256. AddCh (Curses.ACS_ULCORNER);
  257. for (b = 0; b < width - 2; b++)
  258. AddCh (Curses.ACS_HLINE);
  259. AddCh (Curses.ACS_URCORNER);
  260. for (b = 1; b < height - 1; b++) {
  261. Move (region.X, region.Y + b);
  262. AddCh (Curses.ACS_VLINE);
  263. if (fill) {
  264. for (int x = 1; x < width - 1; x++)
  265. AddCh (' ');
  266. } else
  267. Move (region.X + width - 1, region.Y + b);
  268. AddCh (Curses.ACS_VLINE);
  269. }
  270. Move (region.X, region.Y + height - 1);
  271. AddCh (Curses.ACS_LLCORNER);
  272. for (b = 0; b < width - 2; b++)
  273. AddCh (Curses.ACS_HLINE);
  274. AddCh (Curses.ACS_LRCORNER);
  275. }
  276. public override void Init (Action terminalResized)
  277. {
  278. if (window != null)
  279. return;
  280. try {
  281. window = Curses.initscr ();
  282. } catch (Exception e) {
  283. Console.WriteLine ("Curses failed to initialize, the exception is: " + e);
  284. }
  285. Curses.raw ();
  286. Curses.noecho ();
  287. Curses.Window.Standard.keypad (true);
  288. this.terminalResized = terminalResized;
  289. Colors.Base = new ColorScheme ();
  290. Colors.Dialog = new ColorScheme ();
  291. Colors.Menu = new ColorScheme ();
  292. Colors.Error = new ColorScheme ();
  293. Clip = new Rect (0, 0, Cols, Rows);
  294. if (Curses.HasColors) {
  295. Curses.StartColor ();
  296. Curses.UseDefaultColors ();
  297. Colors.Base.Normal = MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLUE);
  298. Colors.Base.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  299. Colors.Base.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLUE);
  300. Colors.Base.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  301. // Focused,
  302. // Selected, Hot: Yellow on Black
  303. // Selected, text: white on black
  304. // Unselected, hot: yellow on cyan
  305. // unselected, text: same as unfocused
  306. Colors.Menu.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLACK);
  307. Colors.Menu.Focus = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLACK);
  308. Colors.Menu.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  309. Colors.Menu.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_CYAN);
  310. Colors.Dialog.Normal = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  311. Colors.Dialog.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  312. Colors.Dialog.HotNormal = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_WHITE);
  313. Colors.Dialog.HotFocus = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_CYAN);
  314. Colors.Error.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_RED);
  315. Colors.Error.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  316. Colors.Error.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_RED);
  317. Colors.Error.HotFocus = Colors.Error.HotNormal;
  318. } else {
  319. Colors.Base.Normal = Curses.A_NORMAL;
  320. Colors.Base.Focus = Curses.A_REVERSE;
  321. Colors.Base.HotNormal = Curses.A_BOLD;
  322. Colors.Base.HotFocus = Curses.A_BOLD | Curses.A_REVERSE;
  323. Colors.Menu.Normal = Curses.A_REVERSE;
  324. Colors.Menu.Focus = Curses.A_NORMAL;
  325. Colors.Menu.HotNormal = Curses.A_BOLD;
  326. Colors.Menu.HotFocus = Curses.A_NORMAL;
  327. Colors.Dialog.Normal = Curses.A_REVERSE;
  328. Colors.Dialog.Focus = Curses.A_NORMAL;
  329. Colors.Dialog.HotNormal = Curses.A_BOLD;
  330. Colors.Dialog.HotFocus = Curses.A_NORMAL;
  331. Colors.Error.Normal = Curses.A_BOLD;
  332. Colors.Error.Focus = Curses.A_BOLD | Curses.A_REVERSE;
  333. Colors.Error.HotNormal = Curses.A_BOLD | Curses.A_REVERSE;
  334. Colors.Error.HotFocus = Curses.A_REVERSE;
  335. }
  336. }
  337. }
  338. }