NetDriver.cs 10 KB

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