NetDriver.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // NetDriver.cs: The System.Console-based .NET driver, works on Windows and Unix, but is not particularly efficient.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using Mono.Terminal;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. internal class NetDriver : ConsoleDriver {
  14. int cols, rows;
  15. public override int Cols => cols;
  16. public override int Rows => rows;
  17. // The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  18. int [,,] contents;
  19. bool [] dirtyLine;
  20. void UpdateOffscreen ()
  21. {
  22. int cols = Cols;
  23. int rows = Rows;
  24. contents = new int [rows, cols, 3];
  25. for (int r = 0; r < rows; r++) {
  26. for (int c = 0; c < cols; c++) {
  27. contents [r, c, 0] = ' ';
  28. contents [r, c, 1] = MakeColor (ConsoleColor.Gray, ConsoleColor.Black);
  29. contents [r, c, 2] = 0;
  30. }
  31. }
  32. dirtyLine = new bool [rows];
  33. for (int row = 0; row < rows; row++)
  34. dirtyLine [row] = true;
  35. }
  36. static bool sync = false;
  37. public NetDriver ()
  38. {
  39. cols = Console.WindowWidth;
  40. rows = Console.WindowHeight - 1;
  41. UpdateOffscreen ();
  42. }
  43. bool needMove;
  44. // Current row, and current col, tracked by Move/AddCh only
  45. int ccol, crow;
  46. public override void Move (int col, int row)
  47. {
  48. ccol = col;
  49. crow = row;
  50. if (Clip.Contains (col, row)) {
  51. Console.CursorTop = row;
  52. Console.CursorLeft = col;
  53. needMove = false;
  54. } else {
  55. Console.CursorTop = Clip.Y;
  56. Console.CursorLeft = Clip.X;
  57. needMove = true;
  58. }
  59. }
  60. public override void AddRune (Rune rune)
  61. {
  62. if (Clip.Contains (ccol, crow)) {
  63. if (needMove) {
  64. //Console.CursorLeft = ccol;
  65. //Console.CursorTop = crow;
  66. needMove = false;
  67. }
  68. contents [crow, ccol, 0] = (int)(uint)rune;
  69. contents [crow, ccol, 1] = currentAttribute;
  70. contents [crow, ccol, 2] = 1;
  71. dirtyLine [crow] = true;
  72. } else
  73. needMove = true;
  74. ccol++;
  75. //if (ccol == Cols) {
  76. // ccol = 0;
  77. // if (crow + 1 < Rows)
  78. // crow++;
  79. //}
  80. if (sync)
  81. UpdateScreen ();
  82. }
  83. public override void AddStr (ustring str)
  84. {
  85. foreach (var rune in str)
  86. AddRune (rune);
  87. }
  88. public override void End ()
  89. {
  90. Console.ResetColor ();
  91. Console.Clear ();
  92. }
  93. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  94. {
  95. // Encode the colors into the int value.
  96. return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
  97. }
  98. public override void Init (Action terminalResized)
  99. {
  100. Colors.TopLevel = new ColorScheme ();
  101. Colors.Base = new ColorScheme ();
  102. Colors.Dialog = new ColorScheme ();
  103. Colors.Menu = new ColorScheme ();
  104. Colors.Error = new ColorScheme ();
  105. Clip = new Rect (0, 0, Cols, Rows);
  106. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  107. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  108. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  109. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  110. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  111. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  112. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  113. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  114. // Focused,
  115. // Selected, Hot: Yellow on Black
  116. // Selected, text: white on black
  117. // Unselected, hot: yellow on cyan
  118. // unselected, text: same as unfocused
  119. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  120. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  121. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  122. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  123. Colors.Menu.Disabled = MakeColor(ConsoleColor.DarkGray, ConsoleColor.Cyan);
  124. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  125. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  126. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  127. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  128. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  129. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  130. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  131. Colors.Error.HotFocus = Colors.Error.HotNormal;
  132. Console.Clear ();
  133. HLine = '\u2500';
  134. VLine = '\u2502';
  135. Stipple = '\u2592';
  136. Diamond = '\u25c6';
  137. ULCorner = '\u250C';
  138. LLCorner = '\u2514';
  139. URCorner = '\u2510';
  140. LRCorner = '\u2518';
  141. LeftTee = '\u251c';
  142. RightTee = '\u2524';
  143. TopTee = '\u22a4';
  144. BottomTee = '\u22a5';
  145. }
  146. public override Attribute MakeAttribute (Color fore, Color back)
  147. {
  148. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  149. }
  150. int redrawColor = -1;
  151. void SetColor (int color)
  152. {
  153. redrawColor = color;
  154. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  155. .OfType<ConsoleColor> ()
  156. .Select (s => (int)s);
  157. if (values.Contains (color & 0xffff)) {
  158. Console.BackgroundColor = (ConsoleColor)(color & 0xffff);
  159. }
  160. if (values.Contains ((color >> 16) & 0xffff)) {
  161. Console.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  162. }
  163. }
  164. public override void UpdateScreen ()
  165. {
  166. int rows = Rows;
  167. int cols = Cols;
  168. Console.CursorTop = 0;
  169. Console.CursorLeft = 0;
  170. for (int row = 0; row < rows; row++) {
  171. dirtyLine [row] = false;
  172. for (int col = 0; col < cols; col++) {
  173. contents [row, col, 2] = 0;
  174. var color = contents [row, col, 1];
  175. if (color != redrawColor)
  176. SetColor (color);
  177. Console.Write ((char)contents [row, col, 0]);
  178. }
  179. }
  180. }
  181. public override void Refresh ()
  182. {
  183. int rows = Rows;
  184. int cols = Cols;
  185. var savedRow = Console.CursorTop;
  186. var savedCol = Console.CursorLeft;
  187. for (int row = 0; row < rows; row++) {
  188. if (!dirtyLine [row])
  189. continue;
  190. dirtyLine [row] = false;
  191. for (int col = 0; col < cols; col++) {
  192. if (contents [row, col, 2] != 1)
  193. continue;
  194. Console.CursorTop = row;
  195. Console.CursorLeft = col;
  196. for (; col < cols && contents [row, col, 2] == 1; col++) {
  197. var color = contents [row, col, 1];
  198. if (color != redrawColor)
  199. SetColor (color);
  200. Console.Write ((char)contents [row, col, 0]);
  201. contents [row, col, 2] = 0;
  202. }
  203. }
  204. }
  205. Console.CursorTop = savedRow;
  206. Console.CursorLeft = savedCol;
  207. }
  208. public override void UpdateCursor ()
  209. {
  210. //
  211. }
  212. public override void StartReportingMouseMoves ()
  213. {
  214. }
  215. public override void StopReportingMouseMoves ()
  216. {
  217. }
  218. public override void Suspend ()
  219. {
  220. }
  221. int currentAttribute;
  222. public override void SetAttribute (Attribute c)
  223. {
  224. currentAttribute = c.value;
  225. }
  226. Key MapKey (ConsoleKeyInfo keyInfo)
  227. {
  228. switch (keyInfo.Key) {
  229. case ConsoleKey.Escape:
  230. return Key.Esc;
  231. case ConsoleKey.Tab:
  232. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  233. case ConsoleKey.Home:
  234. return Key.Home;
  235. case ConsoleKey.End:
  236. return Key.End;
  237. case ConsoleKey.LeftArrow:
  238. return Key.CursorLeft;
  239. case ConsoleKey.RightArrow:
  240. return Key.CursorRight;
  241. case ConsoleKey.UpArrow:
  242. return Key.CursorUp;
  243. case ConsoleKey.DownArrow:
  244. return Key.CursorDown;
  245. case ConsoleKey.PageUp:
  246. return Key.PageUp;
  247. case ConsoleKey.PageDown:
  248. return Key.PageDown;
  249. case ConsoleKey.Enter:
  250. return Key.Enter;
  251. case ConsoleKey.Spacebar:
  252. return Key.Space;
  253. case ConsoleKey.Backspace:
  254. return Key.Backspace;
  255. case ConsoleKey.Delete:
  256. return Key.Delete;
  257. case ConsoleKey.Oem1:
  258. case ConsoleKey.Oem2:
  259. case ConsoleKey.Oem3:
  260. case ConsoleKey.Oem4:
  261. case ConsoleKey.Oem5:
  262. case ConsoleKey.Oem6:
  263. case ConsoleKey.Oem7:
  264. case ConsoleKey.Oem8:
  265. case ConsoleKey.Oem102:
  266. case ConsoleKey.OemPeriod:
  267. case ConsoleKey.OemComma:
  268. case ConsoleKey.OemPlus:
  269. case ConsoleKey.OemMinus:
  270. return (Key)((uint)keyInfo.KeyChar);
  271. }
  272. var key = keyInfo.Key;
  273. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  274. var delta = key - ConsoleKey.A;
  275. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  276. return (Key)((uint)Key.ControlA + delta);
  277. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  278. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  279. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  280. return (Key)((uint)'A' + delta);
  281. else
  282. return (Key)((uint)'a' + delta);
  283. }
  284. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  285. var delta = key - ConsoleKey.D0;
  286. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  287. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  288. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  289. return (Key)((uint)keyInfo.KeyChar);
  290. return (Key)((uint)'0' + delta);
  291. }
  292. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  293. var delta = key - ConsoleKey.F1;
  294. return (Key)((int)Key.F1 + delta);
  295. }
  296. return (Key)(0xffffffff);
  297. }
  298. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  299. {
  300. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  301. (mainLoop.Driver as NetMainLoop).WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  302. var map = MapKey (consoleKey);
  303. if (map == (Key)0xffffffff)
  304. return;
  305. keyHandler (new KeyEvent (map));
  306. keyUpHandler (new KeyEvent (map));
  307. };
  308. }
  309. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  310. {
  311. throw new NotImplementedException ();
  312. }
  313. public override void SetColors (short foregroundColorId, short backgroundColorId)
  314. {
  315. throw new NotImplementedException ();
  316. }
  317. public override void CookMouse ()
  318. {
  319. }
  320. public override void UncookMouse ()
  321. {
  322. }
  323. //
  324. // These are for the .NET driver, but running natively on Windows, wont run
  325. // on the Mono emulation
  326. //
  327. }
  328. }