NetDriver.cs 9.1 KB

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