NetDriver.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.Menu.Disabled = MakeColor(ConsoleColor.DarkGray, ConsoleColor.Cyan);
  129. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  130. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  131. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  132. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  133. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  134. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  135. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  136. Colors.Error.HotFocus = Colors.Error.HotNormal;
  137. Console.Clear ();
  138. }
  139. public override Attribute MakeAttribute (Color fore, Color back)
  140. {
  141. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  142. }
  143. int redrawColor = -1;
  144. void SetColor (int color)
  145. {
  146. redrawColor = color;
  147. Console.BackgroundColor = (ConsoleColor)(color & 0xffff);
  148. Console.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  149. }
  150. public override void UpdateScreen ()
  151. {
  152. int rows = Rows;
  153. int cols = Cols;
  154. Console.CursorTop = 0;
  155. Console.CursorLeft = 0;
  156. for (int row = 0; row < rows; row++) {
  157. dirtyLine [row] = false;
  158. for (int col = 0; col < cols; col++) {
  159. contents [row, col, 2] = 0;
  160. var color = contents [row, col, 1];
  161. if (color != redrawColor)
  162. SetColor (color);
  163. Console.Write ((char)contents [row, col, 0]);
  164. }
  165. }
  166. }
  167. public override void Refresh ()
  168. {
  169. int rows = Rows;
  170. int cols = Cols;
  171. var savedRow = Console.CursorTop;
  172. var savedCol = Console.CursorLeft;
  173. for (int row = 0; row < rows; row++) {
  174. if (!dirtyLine [row])
  175. continue;
  176. dirtyLine [row] = false;
  177. for (int col = 0; col < cols; col++) {
  178. if (contents [row, col, 2] != 1)
  179. continue;
  180. Console.CursorTop = row;
  181. Console.CursorLeft = col;
  182. for (; col < cols && contents [row, col, 2] == 1; col++) {
  183. var color = contents [row, col, 1];
  184. if (color != redrawColor)
  185. SetColor (color);
  186. Console.Write ((char)contents [row, col, 0]);
  187. contents [row, col, 2] = 0;
  188. }
  189. }
  190. }
  191. Console.CursorTop = savedRow;
  192. Console.CursorLeft = savedCol;
  193. }
  194. public override void UpdateCursor ()
  195. {
  196. //
  197. }
  198. public override void StartReportingMouseMoves ()
  199. {
  200. }
  201. public override void StopReportingMouseMoves ()
  202. {
  203. }
  204. public override void Suspend ()
  205. {
  206. }
  207. int currentAttribute;
  208. public override void SetAttribute (Attribute c)
  209. {
  210. currentAttribute = c.value;
  211. }
  212. Key MapKey (ConsoleKeyInfo keyInfo)
  213. {
  214. switch (keyInfo.Key) {
  215. case ConsoleKey.Escape:
  216. return Key.Esc;
  217. case ConsoleKey.Tab:
  218. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  219. case ConsoleKey.Home:
  220. return Key.Home;
  221. case ConsoleKey.End:
  222. return Key.End;
  223. case ConsoleKey.LeftArrow:
  224. return Key.CursorLeft;
  225. case ConsoleKey.RightArrow:
  226. return Key.CursorRight;
  227. case ConsoleKey.UpArrow:
  228. return Key.CursorUp;
  229. case ConsoleKey.DownArrow:
  230. return Key.CursorDown;
  231. case ConsoleKey.PageUp:
  232. return Key.PageUp;
  233. case ConsoleKey.PageDown:
  234. return Key.PageDown;
  235. case ConsoleKey.Enter:
  236. return Key.Enter;
  237. case ConsoleKey.Spacebar:
  238. return Key.Space;
  239. case ConsoleKey.Backspace:
  240. return Key.Backspace;
  241. case ConsoleKey.Delete:
  242. return Key.Delete;
  243. case ConsoleKey.Oem1:
  244. case ConsoleKey.Oem2:
  245. case ConsoleKey.Oem3:
  246. case ConsoleKey.Oem4:
  247. case ConsoleKey.Oem5:
  248. case ConsoleKey.Oem6:
  249. case ConsoleKey.Oem7:
  250. case ConsoleKey.Oem8:
  251. case ConsoleKey.Oem102:
  252. case ConsoleKey.OemPeriod:
  253. case ConsoleKey.OemComma:
  254. case ConsoleKey.OemPlus:
  255. case ConsoleKey.OemMinus:
  256. return (Key)((uint)keyInfo.KeyChar);
  257. }
  258. var key = keyInfo.Key;
  259. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  260. var delta = key - ConsoleKey.A;
  261. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  262. return (Key)((uint)Key.ControlA + delta);
  263. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  264. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  265. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  266. return (Key)((uint)'A' + delta);
  267. else
  268. return (Key)((uint)'a' + delta);
  269. }
  270. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  271. var delta = key - ConsoleKey.D0;
  272. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  273. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  274. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  275. return (Key)((uint)keyInfo.KeyChar);
  276. return (Key)((uint)'0' + delta);
  277. }
  278. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  279. var delta = key - ConsoleKey.F1;
  280. return (Key)((int)Key.F1 + delta);
  281. }
  282. return (Key)(0xffffffff);
  283. }
  284. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  285. {
  286. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  287. (mainLoop.Driver as NetMainLoop).WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  288. var map = MapKey (consoleKey);
  289. if (map == (Key)0xffffffff)
  290. return;
  291. keyHandler (new KeyEvent (map));
  292. };
  293. }
  294. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. public override void SetColors (short foregroundColorId, short backgroundColorId)
  299. {
  300. throw new NotImplementedException ();
  301. }
  302. public override void CookMouse ()
  303. {
  304. }
  305. public override void UncookMouse ()
  306. {
  307. }
  308. //
  309. // These are for the .NET driver, but running natively on Windows, wont run
  310. // on the Mono emulation
  311. //
  312. }
  313. }