NetDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 System.Threading;
  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. Checked = '\u221a';
  146. UnChecked = ' ';
  147. Selected = '\u25cf';
  148. UnSelected = '\u25cc';
  149. RightArrow = '\u25ba';
  150. LeftArrow = '\u25c4';
  151. UpArrow = '\u25b2';
  152. DownArrow = '\u25bc';
  153. LeftDefaultIndicator = '\u25e6';
  154. RightDefaultIndicator = '\u25e6';
  155. LeftBracket = '[';
  156. RightBracket = ']';
  157. OnMeterSegment = '\u258c';
  158. OffMeterSegement = ' ';
  159. }
  160. public override Attribute MakeAttribute (Color fore, Color back)
  161. {
  162. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  163. }
  164. int redrawColor = -1;
  165. void SetColor (int color)
  166. {
  167. redrawColor = color;
  168. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  169. .OfType<ConsoleColor> ()
  170. .Select (s => (int)s);
  171. if (values.Contains (color & 0xffff)) {
  172. Console.BackgroundColor = (ConsoleColor)(color & 0xffff);
  173. }
  174. if (values.Contains ((color >> 16) & 0xffff)) {
  175. Console.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  176. }
  177. }
  178. public override void UpdateScreen ()
  179. {
  180. int rows = Rows;
  181. int cols = Cols;
  182. Console.CursorTop = 0;
  183. Console.CursorLeft = 0;
  184. for (int row = 0; row < rows; row++) {
  185. dirtyLine [row] = false;
  186. for (int col = 0; col < cols; col++) {
  187. contents [row, col, 2] = 0;
  188. var color = contents [row, col, 1];
  189. if (color != redrawColor)
  190. SetColor (color);
  191. Console.Write ((char)contents [row, col, 0]);
  192. }
  193. }
  194. }
  195. public override void Refresh ()
  196. {
  197. int rows = Rows;
  198. int cols = Cols;
  199. var savedRow = Console.CursorTop;
  200. var savedCol = Console.CursorLeft;
  201. for (int row = 0; row < rows; row++) {
  202. if (!dirtyLine [row])
  203. continue;
  204. dirtyLine [row] = false;
  205. for (int col = 0; col < cols; col++) {
  206. if (contents [row, col, 2] != 1)
  207. continue;
  208. Console.CursorTop = row;
  209. Console.CursorLeft = col;
  210. for (; col < cols && contents [row, col, 2] == 1; col++) {
  211. var color = contents [row, col, 1];
  212. if (color != redrawColor)
  213. SetColor (color);
  214. Console.Write ((char)contents [row, col, 0]);
  215. contents [row, col, 2] = 0;
  216. }
  217. }
  218. }
  219. Console.CursorTop = savedRow;
  220. Console.CursorLeft = savedCol;
  221. }
  222. public override void UpdateCursor ()
  223. {
  224. //
  225. }
  226. public override void StartReportingMouseMoves ()
  227. {
  228. }
  229. public override void StopReportingMouseMoves ()
  230. {
  231. }
  232. public override void Suspend ()
  233. {
  234. }
  235. int currentAttribute;
  236. public override void SetAttribute (Attribute c)
  237. {
  238. currentAttribute = c.value;
  239. }
  240. Key MapKey (ConsoleKeyInfo keyInfo)
  241. {
  242. switch (keyInfo.Key) {
  243. case ConsoleKey.Escape:
  244. return Key.Esc;
  245. case ConsoleKey.Tab:
  246. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  247. case ConsoleKey.Home:
  248. return Key.Home;
  249. case ConsoleKey.End:
  250. return Key.End;
  251. case ConsoleKey.LeftArrow:
  252. return Key.CursorLeft;
  253. case ConsoleKey.RightArrow:
  254. return Key.CursorRight;
  255. case ConsoleKey.UpArrow:
  256. return Key.CursorUp;
  257. case ConsoleKey.DownArrow:
  258. return Key.CursorDown;
  259. case ConsoleKey.PageUp:
  260. return Key.PageUp;
  261. case ConsoleKey.PageDown:
  262. return Key.PageDown;
  263. case ConsoleKey.Enter:
  264. return Key.Enter;
  265. case ConsoleKey.Spacebar:
  266. return Key.Space;
  267. case ConsoleKey.Backspace:
  268. return Key.Backspace;
  269. case ConsoleKey.Delete:
  270. return Key.Delete;
  271. case ConsoleKey.Oem1:
  272. case ConsoleKey.Oem2:
  273. case ConsoleKey.Oem3:
  274. case ConsoleKey.Oem4:
  275. case ConsoleKey.Oem5:
  276. case ConsoleKey.Oem6:
  277. case ConsoleKey.Oem7:
  278. case ConsoleKey.Oem8:
  279. case ConsoleKey.Oem102:
  280. case ConsoleKey.OemPeriod:
  281. case ConsoleKey.OemComma:
  282. case ConsoleKey.OemPlus:
  283. case ConsoleKey.OemMinus:
  284. return (Key)((uint)keyInfo.KeyChar);
  285. }
  286. var key = keyInfo.Key;
  287. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  288. var delta = key - ConsoleKey.A;
  289. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  290. return (Key)((uint)Key.ControlA + delta);
  291. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  292. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  293. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  294. return (Key)((uint)'A' + delta);
  295. else
  296. return (Key)((uint)'a' + delta);
  297. }
  298. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  299. var delta = key - ConsoleKey.D0;
  300. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  301. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  302. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  303. return (Key)((uint)keyInfo.KeyChar);
  304. return (Key)((uint)'0' + delta);
  305. }
  306. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  307. var delta = key - ConsoleKey.F1;
  308. return (Key)((int)Key.F1 + delta);
  309. }
  310. return (Key)(0xffffffff);
  311. }
  312. KeyModifiers keyModifiers = new KeyModifiers ();
  313. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  314. {
  315. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  316. (mainLoop.Driver as NetMainLoop).WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  317. var map = MapKey (consoleKey);
  318. if (map == (Key)0xffffffff)
  319. return;
  320. keyHandler (new KeyEvent (map, keyModifiers));
  321. keyUpHandler (new KeyEvent (map, keyModifiers));
  322. };
  323. }
  324. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  325. {
  326. throw new NotImplementedException ();
  327. }
  328. public override void SetColors (short foregroundColorId, short backgroundColorId)
  329. {
  330. throw new NotImplementedException ();
  331. }
  332. public override void CookMouse ()
  333. {
  334. }
  335. public override void UncookMouse ()
  336. {
  337. }
  338. public override ConsoleFont GetFont ()
  339. {
  340. return null;
  341. }
  342. //
  343. // These are for the .NET driver, but running natively on Windows, wont run
  344. // on the Mono emulation
  345. //
  346. }
  347. /// <summary>
  348. /// Mainloop intended to be used with the .NET System.Console API, and can
  349. /// be used on Windows and Unix, it is cross platform but lacks things like
  350. /// file descriptor monitoring.
  351. /// </summary>
  352. class NetMainLoop : IMainLoopDriver {
  353. AutoResetEvent keyReady = new AutoResetEvent (false);
  354. AutoResetEvent waitForProbe = new AutoResetEvent (false);
  355. ConsoleKeyInfo? windowsKeyResult = null;
  356. public Action<ConsoleKeyInfo> WindowsKeyPressed;
  357. MainLoop mainLoop;
  358. public NetMainLoop ()
  359. {
  360. }
  361. void WindowsKeyReader ()
  362. {
  363. while (true) {
  364. waitForProbe.WaitOne ();
  365. windowsKeyResult = Console.ReadKey (true);
  366. keyReady.Set ();
  367. }
  368. }
  369. void IMainLoopDriver.Setup (MainLoop mainLoop)
  370. {
  371. this.mainLoop = mainLoop;
  372. Thread readThread = new Thread (WindowsKeyReader);
  373. readThread.Start ();
  374. }
  375. void IMainLoopDriver.Wakeup ()
  376. {
  377. }
  378. bool IMainLoopDriver.EventsPending (bool wait)
  379. {
  380. long now = DateTime.UtcNow.Ticks;
  381. int waitTimeout;
  382. if (mainLoop.timeouts.Count > 0) {
  383. waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
  384. if (waitTimeout < 0)
  385. return true;
  386. } else
  387. waitTimeout = -1;
  388. if (!wait)
  389. waitTimeout = 0;
  390. windowsKeyResult = null;
  391. waitForProbe.Set ();
  392. keyReady.WaitOne (waitTimeout);
  393. return windowsKeyResult.HasValue;
  394. }
  395. void IMainLoopDriver.MainIteration ()
  396. {
  397. if (windowsKeyResult.HasValue) {
  398. if (WindowsKeyPressed != null)
  399. WindowsKeyPressed (windowsKeyResult.Value);
  400. windowsKeyResult = null;
  401. }
  402. }
  403. }
  404. }