Driver.cs 12 KB

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