Driver.cs 10 KB

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