NetDriver.cs 11 KB

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