2
0

WindowsDriver.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. //
  2. // WindowsDriver.cs: Windows specific driver
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. // Nick Van Dyck ([email protected])
  7. //
  8. // Copyright (c) 2018
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in all
  18. // copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. // SOFTWARE.
  27. //
  28. using System;
  29. using System.Runtime.InteropServices;
  30. using System.Threading.Tasks;
  31. using Mono.Terminal;
  32. using NStack;
  33. namespace Terminal.Gui {
  34. internal class WindowsConsole {
  35. public const int STD_OUTPUT_HANDLE = -11;
  36. public const int STD_INPUT_HANDLE = -10;
  37. public const int STD_ERROR_HANDLE = -12;
  38. IntPtr InputHandle, OutputHandle;
  39. IntPtr ScreenBuffer;
  40. public WindowsConsole ()
  41. {
  42. InputHandle = GetStdHandle (STD_INPUT_HANDLE);
  43. OutputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
  44. }
  45. public CharInfo[] OriginalStdOutChars;
  46. public bool WriteToConsole (CharInfo[] charInfoBuffer, Coord coords, SmallRect window)
  47. {
  48. if (ScreenBuffer == IntPtr.Zero)
  49. {
  50. ScreenBuffer = CreateConsoleScreenBuffer (
  51. DesiredAccess.GenericRead | DesiredAccess.GenericWrite,
  52. ShareMode.FileShareRead | ShareMode.FileShareWrite,
  53. IntPtr.Zero,
  54. 1,
  55. IntPtr.Zero
  56. );
  57. if (ScreenBuffer == INVALID_HANDLE_VALUE){
  58. var err = Marshal.GetLastWin32Error ();
  59. if (err != 0)
  60. throw new System.ComponentModel.Win32Exception(err);
  61. }
  62. if (!SetConsoleActiveScreenBuffer (ScreenBuffer)){
  63. var err = Marshal.GetLastWin32Error();
  64. throw new System.ComponentModel.Win32Exception(err);
  65. }
  66. OriginalStdOutChars = new CharInfo[Console.WindowHeight * Console.WindowWidth];
  67. ReadConsoleOutput (OutputHandle, OriginalStdOutChars, coords, new Coord () { X = 0, Y = 0 }, ref window);
  68. }
  69. return WriteConsoleOutput (ScreenBuffer, charInfoBuffer, coords, new Coord () { X = 0, Y = 0 }, ref window);
  70. }
  71. public bool SetCursorPosition(Coord position)
  72. {
  73. return SetConsoleCursorPosition (ScreenBuffer, position);
  74. }
  75. public void PollEvents (Action<InputRecord> inputEventHandler)
  76. {
  77. if (OriginalConsoleMode != 0)
  78. return;
  79. OriginalConsoleMode = ConsoleMode;
  80. ConsoleMode |= (uint)ConsoleModes.EnableMouseInput;
  81. ConsoleMode &= ~(uint)ConsoleModes.EnableQuickEditMode;
  82. ConsoleMode |= (uint)ConsoleModes.EnableExtendedFlags;
  83. Task.Run (() =>
  84. {
  85. uint numberEventsRead = 0;
  86. uint length = 1;
  87. InputRecord[] records = new InputRecord[length];
  88. while (ContinueListeningForConsoleEvents &&
  89. ReadConsoleInput(InputHandle, records, length, out numberEventsRead) &&
  90. numberEventsRead > 0){
  91. inputEventHandler (records[0]);
  92. }
  93. });
  94. }
  95. public void Cleanup ()
  96. {
  97. ContinueListeningForConsoleEvents = false;
  98. ConsoleMode = OriginalConsoleMode;
  99. OriginalConsoleMode = 0;
  100. if (!SetConsoleActiveScreenBuffer (OutputHandle)){
  101. var err = Marshal.GetLastWin32Error ();
  102. Console.WriteLine("Error: {0}", err);
  103. }
  104. }
  105. private bool ContinueListeningForConsoleEvents = true;
  106. private uint OriginalConsoleMode = 0;
  107. public uint ConsoleMode {
  108. get {
  109. uint v;
  110. GetConsoleMode (InputHandle, out v);
  111. return v;
  112. }
  113. set {
  114. SetConsoleMode (InputHandle, value);
  115. }
  116. }
  117. [Flags]
  118. public enum ConsoleModes : uint
  119. {
  120. EnableMouseInput = 16,
  121. EnableQuickEditMode = 64,
  122. EnableExtendedFlags = 128,
  123. }
  124. [StructLayout (LayoutKind.Explicit, CharSet = CharSet.Unicode)]
  125. public struct KeyEventRecord {
  126. [FieldOffset (0), MarshalAs (UnmanagedType.Bool)]
  127. public bool bKeyDown;
  128. [FieldOffset (4), MarshalAs (UnmanagedType.U2)]
  129. public ushort wRepeatCount;
  130. [FieldOffset (6), MarshalAs (UnmanagedType.U2)]
  131. public ushort wVirtualKeyCode;
  132. [FieldOffset (8), MarshalAs (UnmanagedType.U2)]
  133. public ushort wVirtualScanCode;
  134. [FieldOffset (10)]
  135. public char UnicodeChar;
  136. [FieldOffset (12), MarshalAs (UnmanagedType.U4)]
  137. public ControlKeyState dwControlKeyState;
  138. }
  139. [Flags]
  140. public enum ButtonState {
  141. Button1Pressed = 1,
  142. Button2Pressed = 4,
  143. Button3Pressed = 8,
  144. Button4Pressed = 16,
  145. RightmostButtonPressed = 2,
  146. }
  147. [Flags]
  148. public enum ControlKeyState {
  149. RightAltPressed = 1,
  150. LeftAltPressed = 2,
  151. RightControlPressed = 4,
  152. LeftControlPressed = 8,
  153. ShiftPressed = 16,
  154. NumlockOn = 32,
  155. ScrolllockOn = 64,
  156. CapslockOn = 128,
  157. EnhancedKey = 256
  158. }
  159. [Flags]
  160. public enum EventFlags {
  161. MouseMoved = 1,
  162. DoubleClick = 2,
  163. MouseWheeled = 4,
  164. MouseHorizontalWheeled = 8
  165. }
  166. [StructLayout (LayoutKind.Explicit)]
  167. public struct MouseEventRecord {
  168. [FieldOffset (0)]
  169. public Coordinate MousePosition;
  170. [FieldOffset (4)]
  171. public ButtonState ButtonState;
  172. [FieldOffset (8)]
  173. public ControlKeyState ControlKeyState;
  174. [FieldOffset (12)]
  175. public EventFlags EventFlags;
  176. public override string ToString ()
  177. {
  178. return $"[Mouse({MousePosition},{ButtonState},{ControlKeyState},{EventFlags}";
  179. }
  180. }
  181. [StructLayout (LayoutKind.Sequential)]
  182. public struct Coordinate {
  183. public short X;
  184. public short Y;
  185. public Coordinate (short X, short Y)
  186. {
  187. this.X = X;
  188. this.Y = Y;
  189. }
  190. public override string ToString () => $"({X},{Y})";
  191. };
  192. internal struct WindowBufferSizeRecord {
  193. public Coordinate size;
  194. public WindowBufferSizeRecord (short x, short y)
  195. {
  196. this.size = new Coordinate (x, y);
  197. }
  198. public override string ToString () => $"[WindowBufferSize{size}";
  199. }
  200. [StructLayout (LayoutKind.Sequential)]
  201. public struct MenuEventRecord {
  202. public uint dwCommandId;
  203. }
  204. [StructLayout (LayoutKind.Sequential)]
  205. public struct FocusEventRecord {
  206. public uint bSetFocus;
  207. }
  208. public enum EventType {
  209. Focus = 0x10,
  210. Key = 0x1,
  211. Menu = 0x8,
  212. Mouse = 2,
  213. WindowBufferSize = 4
  214. }
  215. [StructLayout (LayoutKind.Explicit)]
  216. public struct InputRecord {
  217. [FieldOffset (0)]
  218. public EventType EventType;
  219. [FieldOffset (4)]
  220. public KeyEventRecord KeyEvent;
  221. [FieldOffset (4)]
  222. public MouseEventRecord MouseEvent;
  223. [FieldOffset (4)]
  224. public WindowBufferSizeRecord WindowBufferSizeEvent;
  225. [FieldOffset (4)]
  226. public MenuEventRecord MenuEvent;
  227. [FieldOffset (4)]
  228. public FocusEventRecord FocusEvent;
  229. public override string ToString ()
  230. {
  231. switch (EventType) {
  232. case EventType.Focus:
  233. return FocusEvent.ToString ();
  234. case EventType.Key:
  235. return KeyEvent.ToString ();
  236. case EventType.Menu:
  237. return MenuEvent.ToString ();
  238. case EventType.Mouse:
  239. return MouseEvent.ToString ();
  240. case EventType.WindowBufferSize:
  241. return WindowBufferSizeEvent.ToString ();
  242. default:
  243. return "Unknown event type: " + EventType;
  244. }
  245. }
  246. };
  247. [Flags]
  248. enum ShareMode : uint
  249. {
  250. FileShareRead = 1,
  251. FileShareWrite = 2,
  252. }
  253. [Flags]
  254. enum DesiredAccess : uint
  255. {
  256. GenericRead = 2147483648,
  257. GenericWrite = 1073741824,
  258. }
  259. [StructLayout(LayoutKind.Sequential)]
  260. public struct ConsoleScreenBufferInfo
  261. {
  262. public Coord dwSize;
  263. public Coord dwCursorPosition;
  264. public ushort wAttributes;
  265. public SmallRect srWindow;
  266. public Coord dwMaximumWindowSize;
  267. }
  268. [StructLayout(LayoutKind.Sequential)]
  269. public struct Coord
  270. {
  271. public short X;
  272. public short Y;
  273. public Coord(short X, short Y)
  274. {
  275. this.X = X;
  276. this.Y = Y;
  277. }
  278. };
  279. [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
  280. public struct CharUnion
  281. {
  282. [FieldOffset(0)] public char UnicodeChar;
  283. [FieldOffset(0)] public byte AsciiChar;
  284. }
  285. [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
  286. public struct CharInfo
  287. {
  288. [FieldOffset(0)] public CharUnion Char;
  289. [FieldOffset(2)] public ushort Attributes;
  290. }
  291. [StructLayout(LayoutKind.Sequential)]
  292. public struct SmallRect
  293. {
  294. public short Left;
  295. public short Top;
  296. public short Right;
  297. public short Bottom;
  298. }
  299. [DllImport ("kernel32.dll", SetLastError = true)]
  300. static extern IntPtr GetStdHandle (int nStdHandle);
  301. [DllImport ("kernel32.dll", EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
  302. public static extern bool ReadConsoleInput (
  303. IntPtr hConsoleInput,
  304. [Out] InputRecord [] lpBuffer,
  305. uint nLength,
  306. out uint lpNumberOfEventsRead);
  307. [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
  308. static extern bool ReadConsoleOutput(
  309. IntPtr hConsoleOutput,
  310. [Out] CharInfo[] lpBuffer,
  311. Coord dwBufferSize,
  312. Coord dwBufferCoord,
  313. ref SmallRect lpReadRegion
  314. );
  315. [DllImport("kernel32.dll", EntryPoint="WriteConsoleOutput", SetLastError=true, CharSet=CharSet.Unicode)]
  316. static extern bool WriteConsoleOutput(
  317. IntPtr hConsoleOutput,
  318. CharInfo[] lpBuffer,
  319. Coord dwBufferSize,
  320. Coord dwBufferCoord,
  321. ref SmallRect lpWriteRegion
  322. );
  323. [DllImport ("kernel32.dll")]
  324. static extern bool SetConsoleCursorPosition(IntPtr hConsoleOutput, Coord dwCursorPosition);
  325. [DllImport ("kernel32.dll")]
  326. static extern bool GetConsoleMode (IntPtr hConsoleHandle, out uint lpMode);
  327. [DllImport ("kernel32.dll")]
  328. static extern bool SetConsoleMode (IntPtr hConsoleHandle, uint dwMode);
  329. [DllImport("kernel32.dll", SetLastError = true)]
  330. static extern IntPtr CreateConsoleScreenBuffer(
  331. DesiredAccess dwDesiredAccess,
  332. ShareMode dwShareMode,
  333. IntPtr secutiryAttributes,
  334. UInt32 flags,
  335. IntPtr screenBufferData
  336. );
  337. internal static IntPtr INVALID_HANDLE_VALUE = new IntPtr (-1);
  338. [DllImport("kernel32.dll", SetLastError = true)]
  339. static extern bool SetConsoleActiveScreenBuffer(IntPtr Handle);
  340. }
  341. internal class WindowsDriver : ConsoleDriver {
  342. Action TerminalResized;
  343. WindowsConsole WinConsole;
  344. WindowsConsole.CharInfo[] OutputBuffer;
  345. int cols, rows;
  346. public override int Cols => cols;
  347. public override int Rows => rows;
  348. static bool sync;
  349. public WindowsDriver ()
  350. {
  351. WinConsole = new WindowsConsole();
  352. cols = Console.WindowWidth;
  353. rows = Console.WindowHeight - 1;
  354. ResizeScreen ();
  355. UpdateOffScreen ();
  356. }
  357. private WindowsConsole.ButtonState? LastMouseButtonPressed = null;
  358. private MouseEvent ToDriverMouse(WindowsConsole.MouseEventRecord mouseEvent)
  359. {
  360. MouseFlags mouseFlag = MouseFlags.AllEvents;
  361. // The ButtonState member of the MouseEvent structure has bit corresponding to each mouse button.
  362. // This will tell when a mouse button is pressed. When the button is released this event will
  363. // be fired with it's bit set to 0. So when the button is up ButtonState will be 0.
  364. // To map to the correct driver events we save the last pressed mouse button so we can
  365. // map to the correct clicked event.
  366. if (LastMouseButtonPressed != null && mouseEvent.ButtonState != 0)
  367. {
  368. LastMouseButtonPressed = null;
  369. }
  370. if (mouseEvent.EventFlags == 0 && LastMouseButtonPressed == null){
  371. switch (mouseEvent.ButtonState){
  372. case WindowsConsole.ButtonState.Button1Pressed:
  373. mouseFlag = MouseFlags.Button1Pressed;
  374. break;
  375. case WindowsConsole.ButtonState.Button2Pressed:
  376. mouseFlag = MouseFlags.Button2Pressed;
  377. break;
  378. case WindowsConsole.ButtonState.Button3Pressed:
  379. mouseFlag = MouseFlags.Button3Pressed;
  380. break;
  381. }
  382. LastMouseButtonPressed = mouseEvent.ButtonState;
  383. } else if (mouseEvent.EventFlags == 0 && LastMouseButtonPressed != null){
  384. switch (LastMouseButtonPressed){
  385. case WindowsConsole.ButtonState.Button1Pressed:
  386. mouseFlag = MouseFlags.Button1Clicked;
  387. break;
  388. case WindowsConsole.ButtonState.Button2Pressed:
  389. mouseFlag = MouseFlags.Button2Clicked;
  390. break;
  391. case WindowsConsole.ButtonState.Button3Pressed:
  392. mouseFlag = MouseFlags.Button3Clicked;
  393. break;
  394. }
  395. LastMouseButtonPressed = null;
  396. } else if(mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved){
  397. mouseFlag = MouseFlags.ReportMousePosition;
  398. }
  399. return new MouseEvent () {
  400. X = mouseEvent.MousePosition.X,
  401. Y = mouseEvent.MousePosition.Y,
  402. Flags = mouseFlag
  403. };
  404. }
  405. private ConsoleKeyInfo ToConsoleKeyInfo (WindowsConsole.KeyEventRecord keyEvent)
  406. {
  407. var state = keyEvent.dwControlKeyState;
  408. bool shift = (state & WindowsConsole.ControlKeyState.ShiftPressed) != 0;
  409. bool alt = (state & (WindowsConsole.ControlKeyState.LeftAltPressed | WindowsConsole.ControlKeyState.RightAltPressed)) != 0;
  410. bool control = (state & (WindowsConsole.ControlKeyState.LeftControlPressed | WindowsConsole.ControlKeyState.RightControlPressed)) != 0;
  411. return new ConsoleKeyInfo(keyEvent.UnicodeChar, (ConsoleKey)keyEvent.wVirtualKeyCode, shift, alt, control);
  412. }
  413. public Key MapKey (ConsoleKeyInfo keyInfo)
  414. {
  415. switch (keyInfo.Key) {
  416. case ConsoleKey.Escape:
  417. return Key.Esc;
  418. case ConsoleKey.Tab:
  419. return Key.Tab;
  420. case ConsoleKey.Home:
  421. return Key.Home;
  422. case ConsoleKey.End:
  423. return Key.End;
  424. case ConsoleKey.LeftArrow:
  425. return Key.CursorLeft;
  426. case ConsoleKey.RightArrow:
  427. return Key.CursorRight;
  428. case ConsoleKey.UpArrow:
  429. return Key.CursorUp;
  430. case ConsoleKey.DownArrow:
  431. return Key.CursorDown;
  432. case ConsoleKey.PageUp:
  433. return Key.PageUp;
  434. case ConsoleKey.PageDown:
  435. return Key.PageDown;
  436. case ConsoleKey.Enter:
  437. return Key.Enter;
  438. case ConsoleKey.Spacebar:
  439. return Key.Space;
  440. case ConsoleKey.Backspace:
  441. return Key.Backspace;
  442. case ConsoleKey.Delete:
  443. return Key.DeleteChar;
  444. case ConsoleKey.Oem1:
  445. case ConsoleKey.Oem2:
  446. case ConsoleKey.Oem3:
  447. case ConsoleKey.Oem4:
  448. case ConsoleKey.Oem5:
  449. case ConsoleKey.Oem6:
  450. case ConsoleKey.Oem7:
  451. case ConsoleKey.Oem8:
  452. case ConsoleKey.Oem102:
  453. case ConsoleKey.OemPeriod:
  454. case ConsoleKey.OemComma:
  455. case ConsoleKey.OemPlus:
  456. case ConsoleKey.OemMinus:
  457. return (Key)((uint)keyInfo.KeyChar);
  458. }
  459. var key = keyInfo.Key;
  460. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  461. var delta = key - ConsoleKey.A;
  462. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  463. return (Key)((uint)Key.ControlA + delta);
  464. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  465. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  466. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  467. return (Key)((uint)'A' + delta);
  468. else
  469. return (Key)((uint)'a' + delta);
  470. }
  471. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  472. var delta = key - ConsoleKey.D0;
  473. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  474. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  475. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  476. return (Key)((uint)keyInfo.KeyChar);
  477. return (Key)((uint)'0' + delta);
  478. }
  479. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  480. var delta = key - ConsoleKey.F1;
  481. return (Key)((int)Key.F1 + delta);
  482. }
  483. return (Key)(0xffffffff);
  484. }
  485. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler)
  486. {
  487. WinConsole.PollEvents (inputEvent =>
  488. {
  489. switch(inputEvent.EventType){
  490. case WindowsConsole.EventType.Key:
  491. if (inputEvent.KeyEvent.bKeyDown == false)
  492. return;
  493. var map = MapKey (ToConsoleKeyInfo (inputEvent.KeyEvent));
  494. if (map == (Key) 0xffffffff)
  495. return;
  496. keyHandler (new KeyEvent (map));
  497. break;
  498. case WindowsConsole.EventType.Mouse:
  499. mouseHandler (ToDriverMouse (inputEvent.MouseEvent));
  500. break;
  501. case WindowsConsole.EventType.WindowBufferSize:
  502. cols = inputEvent.WindowBufferSizeEvent.size.X;
  503. rows = inputEvent.WindowBufferSizeEvent.size.Y - 1;
  504. ResizeScreen ();
  505. UpdateOffScreen ();
  506. TerminalResized ();
  507. break;
  508. }
  509. });
  510. }
  511. public override void Init (Action terminalResized)
  512. {
  513. TerminalResized = terminalResized;
  514. Colors.Base = new ColorScheme ();
  515. Colors.Dialog = new ColorScheme ();
  516. Colors.Menu = new ColorScheme ();
  517. Colors.Error = new ColorScheme ();
  518. HLine = '\u2500';
  519. VLine = '\u2502';
  520. Stipple = '\u2592';
  521. Diamond = '\u25c6';
  522. ULCorner = '\u250C';
  523. LLCorner = '\u2514';
  524. URCorner = '\u2510';
  525. LRCorner = '\u2518';
  526. LeftTee = '\u251c';
  527. RightTee = '\u2524';
  528. TopTee = '\u22a4';
  529. BottomTee = '\u22a5';
  530. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  531. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  532. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  533. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  534. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  535. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  536. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  537. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  538. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  539. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  540. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  541. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  542. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  543. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  544. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  545. Colors.Error.HotFocus = Colors.Error.HotNormal;
  546. Console.Clear ();
  547. }
  548. void ResizeScreen ()
  549. {
  550. OutputBuffer = new WindowsConsole.CharInfo[Rows * Cols];
  551. Clip = new Rect (0, 0, Cols, Rows);
  552. }
  553. void UpdateOffScreen ()
  554. {
  555. for (int row = 0; row < rows; row++)
  556. for (int col = 0; col < cols; col++){
  557. int position = row * cols + col;
  558. OutputBuffer[position].Attributes = (ushort)MakeColor(ConsoleColor.White, ConsoleColor.Blue);
  559. OutputBuffer[position].Char.UnicodeChar = ' ';
  560. }
  561. }
  562. int ccol, crow;
  563. public override void Move (int col, int row)
  564. {
  565. ccol = col;
  566. crow = row;
  567. }
  568. public override void AddRune (Rune rune)
  569. {
  570. var position = crow * Cols + ccol;
  571. if (Clip.Contains (ccol, crow)){
  572. OutputBuffer[position].Attributes = (ushort)currentAttribute;
  573. OutputBuffer[position].Char.UnicodeChar = (char)rune;
  574. }
  575. ccol++;
  576. if (ccol == Cols) {
  577. ccol = 0;
  578. if (crow + 1 < Rows)
  579. crow++;
  580. }
  581. if (sync)
  582. UpdateScreen ();
  583. }
  584. public override void AddStr (ustring str)
  585. {
  586. foreach (var rune in str)
  587. AddRune (rune);
  588. }
  589. int currentAttribute;
  590. public override void SetAttribute (Attribute c)
  591. {
  592. currentAttribute = c.value;
  593. }
  594. private Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  595. {
  596. // Encode the colors into the int value.
  597. return new Attribute (){
  598. value = ((int)f | (int)b << 4)
  599. };
  600. }
  601. public override void Refresh()
  602. {
  603. var bufferCoords = new WindowsConsole.Coord (){
  604. X = (short)Clip.Width,
  605. Y = (short)Clip.Height
  606. };
  607. var window = new WindowsConsole.SmallRect (){
  608. Top = 0,
  609. Left = 0,
  610. Right = (short)Clip.Right,
  611. Bottom = (short)Clip.Bottom
  612. };
  613. UpdateCursor();
  614. WinConsole.WriteToConsole (OutputBuffer, bufferCoords, window);
  615. }
  616. public override void UpdateScreen ()
  617. {
  618. var bufferCoords = new WindowsConsole.Coord (){
  619. X = (short)Clip.Width,
  620. Y = (short)Clip.Height
  621. };
  622. var window = new WindowsConsole.SmallRect (){
  623. Top = 0,
  624. Left = 0,
  625. Right = (short)Clip.Right,
  626. Bottom = (short)Clip.Bottom
  627. };
  628. UpdateCursor();
  629. WinConsole.WriteToConsole (OutputBuffer, bufferCoords, window);
  630. }
  631. public override void UpdateCursor()
  632. {
  633. var position = new WindowsConsole.Coord(){
  634. X = (short)ccol,
  635. Y = (short)crow
  636. };
  637. WinConsole.SetCursorPosition(position);
  638. }
  639. public override void End ()
  640. {
  641. WinConsole.Cleanup();
  642. }
  643. #region Unused
  644. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  645. {
  646. }
  647. public override void SetColors (short foregroundColorId, short backgroundColorId)
  648. {
  649. }
  650. public override void Suspend ()
  651. {
  652. }
  653. public override void StartReportingMouseMoves ()
  654. {
  655. }
  656. public override void StopReportingMouseMoves ()
  657. {
  658. }
  659. public override void UncookMouse ()
  660. {
  661. }
  662. public override void CookMouse ()
  663. {
  664. }
  665. #endregion
  666. }
  667. }