Driver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. var code = Curses.getch ();
  174. if ((code == -1) || (code == Curses.KeyResize)) {
  175. if (Curses.CheckWinChange ()) {
  176. terminalResized ();
  177. }
  178. }
  179. if (code == Curses.KeyMouse) {
  180. // TODO
  181. // Curses.MouseEvent ev;
  182. // Curses.getmouse (out ev);
  183. // handler.HandleMouse ();
  184. return;
  185. }
  186. // ESC+letter is Alt-Letter.
  187. if (code == 27) {
  188. Curses.timeout (100);
  189. int k = Curses.getch ();
  190. if (k != Curses.ERR && k != 27) {
  191. var mapped = MapCursesKey (k) | Key.AltMask;
  192. handler.ProcessKey (new KeyEvent (mapped));
  193. }
  194. } else {
  195. handler.ProcessKey (new KeyEvent (MapCursesKey (code)));
  196. }
  197. }
  198. public override void PrepareToRun (MainLoop mainLoop, Responder handler)
  199. {
  200. Curses.timeout (-1);
  201. mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
  202. ProcessInput (handler);
  203. return true;
  204. });
  205. }
  206. public override void DrawFrame (Rect region, bool fill)
  207. {
  208. int width = region.Width;
  209. int height = region.Height;
  210. int b;
  211. Move (region.X, region.Y);
  212. AddCh (Curses.ACS_ULCORNER);
  213. for (b = 0; b < width - 2; b++)
  214. AddCh (Curses.ACS_HLINE);
  215. AddCh (Curses.ACS_URCORNER);
  216. for (b = 1; b < height - 1; b++) {
  217. Move (region.X, region.Y + b);
  218. AddCh (Curses.ACS_VLINE);
  219. if (fill) {
  220. for (int x = 1; x < width - 1; x++)
  221. AddCh (' ');
  222. } else
  223. Move (region.X + width - 1, region.Y + b);
  224. AddCh (Curses.ACS_VLINE);
  225. }
  226. Move (region.X, region.Y + height - 1);
  227. AddCh (Curses.ACS_LLCORNER);
  228. for (b = 0; b < width - 2; b++)
  229. AddCh (Curses.ACS_HLINE);
  230. AddCh (Curses.ACS_LRCORNER);
  231. }
  232. public override void Init(Action terminalResized)
  233. {
  234. if (window != null)
  235. return;
  236. try {
  237. window = Curses.initscr ();
  238. } catch (Exception e){
  239. Console.WriteLine ("Curses failed to initialize, the exception is: " + e);
  240. }
  241. Curses.raw ();
  242. Curses.noecho ();
  243. Curses.Window.Standard.keypad (true);
  244. this.terminalResized = terminalResized;
  245. Colors.Base = new ColorScheme ();
  246. Colors.Dialog = new ColorScheme ();
  247. Colors.Menu = new ColorScheme ();
  248. Colors.Error = new ColorScheme ();
  249. Clip = new Rect (0, 0, Cols, Rows);
  250. if (Curses.HasColors){
  251. Curses.StartColor ();
  252. Curses.UseDefaultColors ();
  253. Colors.Base.Normal = MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLUE);
  254. Colors.Base.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  255. Colors.Base.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLUE);
  256. Colors.Base.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  257. Colors.Menu.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_CYAN);
  258. Colors.Menu.Focus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_CYAN);
  259. Colors.Menu.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_BLACK);
  260. Colors.Menu.HotFocus = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_BLACK);
  261. Colors.Dialog.Normal = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  262. Colors.Dialog.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_CYAN);
  263. Colors.Dialog.HotNormal = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_WHITE);
  264. Colors.Dialog.HotFocus = MakeColor (Curses.COLOR_BLUE, Curses.COLOR_CYAN);
  265. Colors.Error.Normal = Curses.A_BOLD | MakeColor (Curses.COLOR_WHITE, Curses.COLOR_RED);
  266. Colors.Error.Focus = MakeColor (Curses.COLOR_BLACK, Curses.COLOR_WHITE);
  267. Colors.Error.HotNormal = Curses.A_BOLD | MakeColor (Curses.COLOR_YELLOW, Curses.COLOR_RED);
  268. Colors.Error.HotFocus = Colors.Error.HotNormal;
  269. } else {
  270. Colors.Base.Normal = Curses.A_NORMAL;
  271. Colors.Base.Focus = Curses.A_REVERSE;
  272. Colors.Base.HotNormal = Curses.A_BOLD;
  273. Colors.Base.HotFocus = Curses.A_BOLD | Curses.A_REVERSE;
  274. Colors.Menu.Normal = Curses.A_REVERSE;
  275. Colors.Menu.Focus = Curses.A_NORMAL;
  276. Colors.Menu.HotNormal = Curses.A_BOLD;
  277. Colors.Menu.HotFocus = Curses.A_NORMAL;
  278. Colors.Dialog.Normal = Curses.A_REVERSE;
  279. Colors.Dialog.Focus = Curses.A_NORMAL;
  280. Colors.Dialog.HotNormal = Curses.A_BOLD;
  281. Colors.Dialog.HotFocus = Curses.A_NORMAL;
  282. Colors.Error.Normal = Curses.A_BOLD;
  283. Colors.Error.Focus = Curses.A_BOLD | Curses.A_REVERSE;
  284. Colors.Error.HotNormal = Curses.A_BOLD | Curses.A_REVERSE;
  285. Colors.Error.HotFocus = Curses.A_REVERSE;
  286. }
  287. }
  288. }
  289. }