NetDriver.cs 12 KB

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