2
0

CursesDriver.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. //
  2. // Driver.cs: Curses-based Driver
  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.Runtime.InteropServices;
  11. using System.Threading.Tasks;
  12. using NStack;
  13. using Unix.Terminal;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// This is the Curses driver for the gui.cs/Terminal framework.
  17. /// </summary>
  18. internal class CursesDriver : ConsoleDriver {
  19. public override int Cols => Curses.Cols;
  20. public override int Rows => Curses.Lines;
  21. public override int Left => 0;
  22. public override int Top => 0;
  23. public override bool HeightAsBuffer { get; set; }
  24. public override IClipboard Clipboard { get => clipboard; }
  25. CursorVisibility? initialCursorVisibility = null;
  26. CursorVisibility? currentCursorVisibility = null;
  27. IClipboard clipboard;
  28. int [,,] contents;
  29. internal override int [,,] Contents => contents;
  30. // Current row, and current col, tracked by Move/AddRune only
  31. int ccol, crow;
  32. bool needMove;
  33. public override void Move (int col, int row)
  34. {
  35. ccol = col;
  36. crow = row;
  37. if (Clip.Contains (col, row)) {
  38. Curses.move (row, col);
  39. needMove = false;
  40. } else {
  41. Curses.move (Clip.Y, Clip.X);
  42. needMove = true;
  43. }
  44. }
  45. static bool sync = false;
  46. public override void AddRune (Rune rune)
  47. {
  48. rune = MakePrintable (rune);
  49. var runeWidth = Rune.ColumnWidth (rune);
  50. var validClip = IsValidContent (ccol, crow, Clip);
  51. if (validClip) {
  52. if (needMove) {
  53. Curses.move (crow, ccol);
  54. needMove = false;
  55. }
  56. if (runeWidth < 2 && ccol > 0
  57. && Rune.ColumnWidth ((char)contents [crow, ccol - 1, 0]) > 1) {
  58. var curAtttib = currentAttribute;
  59. Curses.attrset (contents [crow, ccol - 1, 1]);
  60. Curses.mvaddch (crow, ccol - 1, (int)(uint)' ');
  61. contents [crow, ccol - 1, 0] = (int)(uint)' ';
  62. Curses.move (crow, ccol);
  63. Curses.attrset (curAtttib);
  64. } else if (runeWidth < 2 && ccol <= Clip.Right - 1
  65. && Rune.ColumnWidth ((char)contents [crow, ccol, 0]) > 1) {
  66. var curAtttib = currentAttribute;
  67. Curses.attrset (contents [crow, ccol + 1, 1]);
  68. Curses.mvaddch (crow, ccol + 1, (int)(uint)' ');
  69. contents [crow, ccol + 1, 0] = (int)(uint)' ';
  70. Curses.move (crow, ccol);
  71. Curses.attrset (curAtttib);
  72. }
  73. if (runeWidth > 1 && ccol == Clip.Right - 1) {
  74. Curses.addch ((int)(uint)' ');
  75. contents [crow, ccol, 0] = (int)(uint)' ';
  76. } else {
  77. Curses.addch ((int)(uint)rune);
  78. contents [crow, ccol, 0] = (int)(uint)rune;
  79. }
  80. contents [crow, ccol, 1] = currentAttribute;
  81. contents [crow, ccol, 2] = 1;
  82. } else
  83. needMove = true;
  84. ccol++;
  85. if (runeWidth > 1) {
  86. if (validClip && ccol < Clip.Right) {
  87. contents [crow, ccol, 1] = currentAttribute;
  88. contents [crow, ccol, 2] = 0;
  89. }
  90. ccol++;
  91. }
  92. if (sync)
  93. UpdateScreen ();
  94. }
  95. public override void AddStr (ustring str)
  96. {
  97. // TODO; optimize this to determine if the str fits in the clip region, and if so, use Curses.addstr directly
  98. foreach (var rune in str)
  99. AddRune (rune);
  100. }
  101. public override void Refresh ()
  102. {
  103. Curses.raw ();
  104. Curses.noecho ();
  105. Curses.refresh ();
  106. if (Curses.CheckWinChange ()) {
  107. Clip = new Rect (0, 0, Cols, Rows);
  108. UpdateOffScreen ();
  109. TerminalResized?.Invoke ();
  110. }
  111. }
  112. public override void UpdateCursor () => Refresh ();
  113. public override void End ()
  114. {
  115. if (reportableMouseEvents.HasFlag (Curses.Event.ReportMousePosition)) {
  116. StopReportingMouseMoves ();
  117. }
  118. SetCursorVisibility (CursorVisibility.Default);
  119. Curses.endwin ();
  120. // I'm commenting this because was used in a trying to fix the Linux hanging and forgot to exclude it.
  121. // Clear and reset entire screen.
  122. //Console.Out.Write ("\x1b[2J");
  123. //Console.Out.Flush ();
  124. // Set top and bottom lines of a window.
  125. //Console.Out.Write ("\x1b[1;25r");
  126. //Console.Out.Flush ();
  127. //Set cursor key to cursor.
  128. //Console.Out.Write ("\x1b[?1l");
  129. //Console.Out.Flush ();
  130. }
  131. public override void UpdateScreen () => window.redrawwin ();
  132. Attribute currentAttribute;
  133. public override void SetAttribute (Attribute c)
  134. {
  135. currentAttribute = c;
  136. Curses.attrset (currentAttribute);
  137. }
  138. public Curses.Window window;
  139. //static short last_color_pair = 16;
  140. /// <summary>
  141. /// Creates a curses color from the provided foreground and background colors
  142. /// </summary>
  143. /// <param name="foreground">Contains the curses attributes for the foreground (color, plus any attributes)</param>
  144. /// <param name="background">Contains the curses attributes for the background (color, plus any attributes)</param>
  145. /// <returns></returns>
  146. public static Attribute MakeColor (short foreground, short background)
  147. {
  148. var v = (short)((int)foreground | background << 4);
  149. //Curses.InitColorPair (++last_color_pair, foreground, background);
  150. Curses.InitColorPair (v, foreground, background);
  151. return new Attribute (
  152. //value: Curses.ColorPair (last_color_pair),
  153. value: Curses.ColorPair (v),
  154. //foreground: (Color)foreground,
  155. foreground: MapCursesColor (foreground),
  156. //background: (Color)background);
  157. background: MapCursesColor (background));
  158. }
  159. static Attribute MakeColor (Color fore, Color back)
  160. {
  161. return MakeColor ((short)MapColor (fore), (short)MapColor (back));
  162. }
  163. int [,] colorPairs = new int [16, 16];
  164. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  165. {
  166. int f = (short)foreground;
  167. int b = (short)background;
  168. var v = colorPairs [f, b];
  169. if ((v & 0x10000) == 0) {
  170. b &= 0x7;
  171. bool bold = (f & 0x8) != 0;
  172. f &= 0x7;
  173. v = MakeColor ((short)f, (short)b) | (bold ? Curses.A_BOLD : 0);
  174. colorPairs [(int)foreground, (int)background] = v | 0x1000;
  175. }
  176. SetAttribute (v & 0xffff);
  177. }
  178. Dictionary<int, int> rawPairs = new Dictionary<int, int> ();
  179. public override void SetColors (short foreColorId, short backgroundColorId)
  180. {
  181. int key = ((ushort)foreColorId << 16) | (ushort)backgroundColorId;
  182. if (!rawPairs.TryGetValue (key, out var v)) {
  183. v = MakeColor (foreColorId, backgroundColorId);
  184. rawPairs [key] = v;
  185. }
  186. SetAttribute (v);
  187. }
  188. static Key MapCursesKey (int cursesKey)
  189. {
  190. switch (cursesKey) {
  191. case Curses.KeyF1: return Key.F1;
  192. case Curses.KeyF2: return Key.F2;
  193. case Curses.KeyF3: return Key.F3;
  194. case Curses.KeyF4: return Key.F4;
  195. case Curses.KeyF5: return Key.F5;
  196. case Curses.KeyF6: return Key.F6;
  197. case Curses.KeyF7: return Key.F7;
  198. case Curses.KeyF8: return Key.F8;
  199. case Curses.KeyF9: return Key.F9;
  200. case Curses.KeyF10: return Key.F10;
  201. case Curses.KeyF11: return Key.F11;
  202. case Curses.KeyF12: return Key.F12;
  203. case Curses.KeyUp: return Key.CursorUp;
  204. case Curses.KeyDown: return Key.CursorDown;
  205. case Curses.KeyLeft: return Key.CursorLeft;
  206. case Curses.KeyRight: return Key.CursorRight;
  207. case Curses.KeyHome: return Key.Home;
  208. case Curses.KeyEnd: return Key.End;
  209. case Curses.KeyNPage: return Key.PageDown;
  210. case Curses.KeyPPage: return Key.PageUp;
  211. case Curses.KeyDeleteChar: return Key.DeleteChar;
  212. case Curses.KeyInsertChar: return Key.InsertChar;
  213. case Curses.KeyTab: return Key.Tab;
  214. case Curses.KeyBackTab: return Key.BackTab;
  215. case Curses.KeyBackspace: return Key.Backspace;
  216. case Curses.ShiftKeyUp: return Key.CursorUp | Key.ShiftMask;
  217. case Curses.ShiftKeyDown: return Key.CursorDown | Key.ShiftMask;
  218. case Curses.ShiftKeyLeft: return Key.CursorLeft | Key.ShiftMask;
  219. case Curses.ShiftKeyRight: return Key.CursorRight | Key.ShiftMask;
  220. case Curses.ShiftKeyHome: return Key.Home | Key.ShiftMask;
  221. case Curses.ShiftKeyEnd: return Key.End | Key.ShiftMask;
  222. case Curses.ShiftKeyNPage: return Key.PageDown | Key.ShiftMask;
  223. case Curses.ShiftKeyPPage: return Key.PageUp | Key.ShiftMask;
  224. case Curses.AltKeyUp: return Key.CursorUp | Key.AltMask;
  225. case Curses.AltKeyDown: return Key.CursorDown | Key.AltMask;
  226. case Curses.AltKeyLeft: return Key.CursorLeft | Key.AltMask;
  227. case Curses.AltKeyRight: return Key.CursorRight | Key.AltMask;
  228. case Curses.AltKeyHome: return Key.Home | Key.AltMask;
  229. case Curses.AltKeyEnd: return Key.End | Key.AltMask;
  230. case Curses.AltKeyNPage: return Key.PageDown | Key.AltMask;
  231. case Curses.AltKeyPPage: return Key.PageUp | Key.AltMask;
  232. case Curses.CtrlKeyUp: return Key.CursorUp | Key.CtrlMask;
  233. case Curses.CtrlKeyDown: return Key.CursorDown | Key.CtrlMask;
  234. case Curses.CtrlKeyLeft: return Key.CursorLeft | Key.CtrlMask;
  235. case Curses.CtrlKeyRight: return Key.CursorRight | Key.CtrlMask;
  236. case Curses.CtrlKeyHome: return Key.Home | Key.CtrlMask;
  237. case Curses.CtrlKeyEnd: return Key.End | Key.CtrlMask;
  238. case Curses.CtrlKeyNPage: return Key.PageDown | Key.CtrlMask;
  239. case Curses.CtrlKeyPPage: return Key.PageUp | Key.CtrlMask;
  240. case Curses.ShiftCtrlKeyUp: return Key.CursorUp | Key.ShiftMask | Key.CtrlMask;
  241. case Curses.ShiftCtrlKeyDown: return Key.CursorDown | Key.ShiftMask | Key.CtrlMask;
  242. case Curses.ShiftCtrlKeyLeft: return Key.CursorLeft | Key.ShiftMask | Key.CtrlMask;
  243. case Curses.ShiftCtrlKeyRight: return Key.CursorRight | Key.ShiftMask | Key.CtrlMask;
  244. case Curses.ShiftCtrlKeyHome: return Key.Home | Key.ShiftMask | Key.CtrlMask;
  245. case Curses.ShiftCtrlKeyEnd: return Key.End | Key.ShiftMask | Key.CtrlMask;
  246. case Curses.ShiftCtrlKeyNPage: return Key.PageDown | Key.ShiftMask | Key.CtrlMask;
  247. case Curses.ShiftCtrlKeyPPage: return Key.PageUp | Key.ShiftMask | Key.CtrlMask;
  248. case Curses.ShiftAltKeyUp: return Key.CursorUp | Key.ShiftMask | Key.AltMask;
  249. case Curses.ShiftAltKeyDown: return Key.CursorDown | Key.ShiftMask | Key.AltMask;
  250. case Curses.ShiftAltKeyLeft: return Key.CursorLeft | Key.ShiftMask | Key.AltMask;
  251. case Curses.ShiftAltKeyRight: return Key.CursorRight | Key.ShiftMask | Key.AltMask;
  252. case Curses.ShiftAltKeyNPage: return Key.PageDown | Key.ShiftMask | Key.AltMask;
  253. case Curses.ShiftAltKeyPPage: return Key.PageUp | Key.ShiftMask | Key.AltMask;
  254. case Curses.ShiftAltKeyHome: return Key.Home | Key.ShiftMask | Key.AltMask;
  255. case Curses.ShiftAltKeyEnd: return Key.End | Key.ShiftMask | Key.AltMask;
  256. case Curses.AltCtrlKeyNPage: return Key.PageDown | Key.AltMask | Key.CtrlMask;
  257. case Curses.AltCtrlKeyPPage: return Key.PageUp | Key.AltMask | Key.CtrlMask;
  258. case Curses.AltCtrlKeyHome: return Key.Home | Key.AltMask | Key.CtrlMask;
  259. case Curses.AltCtrlKeyEnd: return Key.End | Key.AltMask | Key.CtrlMask;
  260. default: return Key.Unknown;
  261. }
  262. }
  263. Curses.Event? lastMouseButtonPressed;
  264. bool isButtonPressed;
  265. bool cancelButtonClicked;
  266. bool isReportMousePosition;
  267. Point point;
  268. int buttonPressedCount;
  269. MouseEvent ToDriverMouse (Curses.MouseEvent cev)
  270. {
  271. MouseFlags mouseFlag = MouseFlags.AllEvents;
  272. if (lastMouseButtonPressed != null && cev.ButtonState != Curses.Event.ReportMousePosition) {
  273. lastMouseButtonPressed = null;
  274. isButtonPressed = false;
  275. }
  276. if (cev.ButtonState == Curses.Event.Button1Pressed
  277. || cev.ButtonState == Curses.Event.Button2Pressed
  278. || cev.ButtonState == Curses.Event.Button3Pressed) {
  279. isButtonPressed = true;
  280. buttonPressedCount++;
  281. } else {
  282. buttonPressedCount = 0;
  283. }
  284. //System.Diagnostics.Debug.WriteLine ($"buttonPressedCount: {buttonPressedCount}");
  285. if (buttonPressedCount == 2
  286. && (cev.ButtonState == Curses.Event.Button1Pressed
  287. || cev.ButtonState == Curses.Event.Button2Pressed
  288. || cev.ButtonState == Curses.Event.Button3Pressed)) {
  289. switch (cev.ButtonState) {
  290. case Curses.Event.Button1Pressed:
  291. mouseFlag = MouseFlags.Button1DoubleClicked;
  292. break;
  293. case Curses.Event.Button2Pressed:
  294. mouseFlag = MouseFlags.Button2DoubleClicked;
  295. break;
  296. case Curses.Event.Button3Pressed:
  297. mouseFlag = MouseFlags.Button3DoubleClicked;
  298. break;
  299. }
  300. cancelButtonClicked = true;
  301. } else if (buttonPressedCount == 3
  302. && (cev.ButtonState == Curses.Event.Button1Pressed
  303. || cev.ButtonState == Curses.Event.Button2Pressed
  304. || cev.ButtonState == Curses.Event.Button3Pressed)) {
  305. switch (cev.ButtonState) {
  306. case Curses.Event.Button1Pressed:
  307. mouseFlag = MouseFlags.Button1TripleClicked;
  308. break;
  309. case Curses.Event.Button2Pressed:
  310. mouseFlag = MouseFlags.Button2TripleClicked;
  311. break;
  312. case Curses.Event.Button3Pressed:
  313. mouseFlag = MouseFlags.Button3TripleClicked;
  314. break;
  315. }
  316. buttonPressedCount = 0;
  317. } else if ((cev.ButtonState == Curses.Event.Button1Clicked || cev.ButtonState == Curses.Event.Button2Clicked ||
  318. cev.ButtonState == Curses.Event.Button3Clicked) &&
  319. lastMouseButtonPressed == null) {
  320. isButtonPressed = false;
  321. mouseFlag = ProcessButtonClickedEvent (cev);
  322. } else if (((cev.ButtonState == Curses.Event.Button1Pressed || cev.ButtonState == Curses.Event.Button2Pressed ||
  323. cev.ButtonState == Curses.Event.Button3Pressed) && lastMouseButtonPressed == null) ||
  324. isButtonPressed && lastMouseButtonPressed != null && cev.ButtonState == Curses.Event.ReportMousePosition) {
  325. mouseFlag = MapCursesButton (cev.ButtonState);
  326. if (cev.ButtonState != Curses.Event.ReportMousePosition)
  327. lastMouseButtonPressed = cev.ButtonState;
  328. isButtonPressed = true;
  329. isReportMousePosition = false;
  330. if (cev.ButtonState == Curses.Event.ReportMousePosition) {
  331. mouseFlag = MapCursesButton ((Curses.Event)lastMouseButtonPressed) | MouseFlags.ReportMousePosition;
  332. point = new Point ();
  333. cancelButtonClicked = true;
  334. } else {
  335. point = new Point () {
  336. X = cev.X,
  337. Y = cev.Y
  338. };
  339. }
  340. if ((mouseFlag & MouseFlags.ReportMousePosition) == 0) {
  341. Application.MainLoop.AddIdle (() => {
  342. Task.Run (async () => await ProcessContinuousButtonPressedAsync (cev, mouseFlag));
  343. return false;
  344. });
  345. }
  346. } else if ((cev.ButtonState == Curses.Event.Button1Released || cev.ButtonState == Curses.Event.Button2Released ||
  347. cev.ButtonState == Curses.Event.Button3Released)) {
  348. mouseFlag = ProcessButtonReleasedEvent (cev);
  349. isButtonPressed = false;
  350. } else if (cev.ButtonState == Curses.Event.ButtonWheeledUp) {
  351. mouseFlag = MouseFlags.WheeledUp;
  352. } else if (cev.ButtonState == Curses.Event.ButtonWheeledDown) {
  353. mouseFlag = MouseFlags.WheeledDown;
  354. } else if ((cev.ButtonState & (Curses.Event.ButtonWheeledUp & Curses.Event.ButtonShift)) != 0) {
  355. mouseFlag = MouseFlags.WheeledLeft;
  356. } else if ((cev.ButtonState & (Curses.Event.ButtonWheeledDown & Curses.Event.ButtonShift)) != 0) {
  357. mouseFlag = MouseFlags.WheeledRight;
  358. } else if (cev.ButtonState == Curses.Event.ReportMousePosition) {
  359. if (cev.X != point.X || cev.Y != point.Y) {
  360. mouseFlag = MouseFlags.ReportMousePosition;
  361. isReportMousePosition = true;
  362. point = new Point ();
  363. } else {
  364. mouseFlag = 0;
  365. }
  366. } else {
  367. mouseFlag = 0;
  368. var eFlags = cev.ButtonState;
  369. foreach (Enum value in Enum.GetValues (eFlags.GetType ())) {
  370. if (eFlags.HasFlag (value)) {
  371. mouseFlag |= MapCursesButton ((Curses.Event)value);
  372. }
  373. }
  374. }
  375. mouseFlag = SetControlKeyStates (cev, mouseFlag);
  376. return new MouseEvent () {
  377. X = cev.X,
  378. Y = cev.Y,
  379. //Flags = MapCursesButton (cev.ButtonState)
  380. Flags = mouseFlag
  381. };
  382. }
  383. MouseFlags ProcessButtonClickedEvent (Curses.MouseEvent cev)
  384. {
  385. lastMouseButtonPressed = cev.ButtonState;
  386. var mf = GetButtonState (cev, true);
  387. mouseHandler (ProcessButtonState (cev, mf));
  388. if (lastMouseButtonPressed != null && lastMouseButtonPressed == cev.ButtonState) {
  389. mf = GetButtonState (cev, false);
  390. mouseHandler (ProcessButtonState (cev, mf));
  391. if (lastMouseButtonPressed != null && lastMouseButtonPressed == cev.ButtonState) {
  392. mf = MapCursesButton (cev.ButtonState);
  393. }
  394. }
  395. lastMouseButtonPressed = null;
  396. isButtonPressed = false;
  397. return mf;
  398. }
  399. MouseFlags ProcessButtonReleasedEvent (Curses.MouseEvent cev)
  400. {
  401. var mf = MapCursesButton (cev.ButtonState);
  402. if (!cancelButtonClicked && lastMouseButtonPressed == null && !isReportMousePosition) {
  403. mouseHandler (ProcessButtonState (cev, mf));
  404. mf = GetButtonState (cev);
  405. } else if (isReportMousePosition) {
  406. mf = MouseFlags.ReportMousePosition;
  407. }
  408. cancelButtonClicked = false;
  409. return mf;
  410. }
  411. async Task ProcessContinuousButtonPressedAsync (Curses.MouseEvent cev, MouseFlags mouseFlag)
  412. {
  413. await Task.Delay (200);
  414. while (isButtonPressed && lastMouseButtonPressed != null) {
  415. await Task.Delay (100);
  416. var me = new MouseEvent () {
  417. X = cev.X,
  418. Y = cev.Y,
  419. Flags = mouseFlag
  420. };
  421. var view = Application.wantContinuousButtonPressedView;
  422. if (view == null)
  423. break;
  424. if (isButtonPressed && lastMouseButtonPressed != null && (mouseFlag & MouseFlags.ReportMousePosition) == 0) {
  425. Application.MainLoop.Invoke (() => mouseHandler (me));
  426. }
  427. }
  428. }
  429. MouseFlags GetButtonState (Curses.MouseEvent cev, bool pressed = false)
  430. {
  431. MouseFlags mf = default;
  432. switch (cev.ButtonState) {
  433. case Curses.Event.Button1Clicked:
  434. if (pressed)
  435. mf = MouseFlags.Button1Pressed;
  436. else
  437. mf = MouseFlags.Button1Released;
  438. break;
  439. case Curses.Event.Button2Clicked:
  440. if (pressed)
  441. mf = MouseFlags.Button2Pressed;
  442. else
  443. mf = MouseFlags.Button2Released;
  444. break;
  445. case Curses.Event.Button3Clicked:
  446. if (pressed)
  447. mf = MouseFlags.Button3Pressed;
  448. else
  449. mf = MouseFlags.Button3Released;
  450. break;
  451. case Curses.Event.Button1Released:
  452. mf = MouseFlags.Button1Clicked;
  453. break;
  454. case Curses.Event.Button2Released:
  455. mf = MouseFlags.Button2Clicked;
  456. break;
  457. case Curses.Event.Button3Released:
  458. mf = MouseFlags.Button3Clicked;
  459. break;
  460. }
  461. return mf;
  462. }
  463. MouseEvent ProcessButtonState (Curses.MouseEvent cev, MouseFlags mf)
  464. {
  465. return new MouseEvent () {
  466. X = cev.X,
  467. Y = cev.Y,
  468. Flags = mf
  469. };
  470. }
  471. MouseFlags MapCursesButton (Curses.Event cursesButton)
  472. {
  473. switch (cursesButton) {
  474. case Curses.Event.Button1Pressed: return MouseFlags.Button1Pressed;
  475. case Curses.Event.Button1Released: return MouseFlags.Button1Released;
  476. case Curses.Event.Button1Clicked: return MouseFlags.Button1Clicked;
  477. case Curses.Event.Button1DoubleClicked: return MouseFlags.Button1DoubleClicked;
  478. case Curses.Event.Button1TripleClicked: return MouseFlags.Button1TripleClicked;
  479. case Curses.Event.Button2Pressed: return MouseFlags.Button2Pressed;
  480. case Curses.Event.Button2Released: return MouseFlags.Button2Released;
  481. case Curses.Event.Button2Clicked: return MouseFlags.Button2Clicked;
  482. case Curses.Event.Button2DoubleClicked: return MouseFlags.Button2DoubleClicked;
  483. case Curses.Event.Button2TrippleClicked: return MouseFlags.Button2TripleClicked;
  484. case Curses.Event.Button3Pressed: return MouseFlags.Button3Pressed;
  485. case Curses.Event.Button3Released: return MouseFlags.Button3Released;
  486. case Curses.Event.Button3Clicked: return MouseFlags.Button3Clicked;
  487. case Curses.Event.Button3DoubleClicked: return MouseFlags.Button3DoubleClicked;
  488. case Curses.Event.Button3TripleClicked: return MouseFlags.Button3TripleClicked;
  489. case Curses.Event.ButtonWheeledUp: return MouseFlags.WheeledUp;
  490. case Curses.Event.ButtonWheeledDown: return MouseFlags.WheeledDown;
  491. case Curses.Event.Button4Pressed: return MouseFlags.Button4Pressed;
  492. case Curses.Event.Button4Released: return MouseFlags.Button4Released;
  493. case Curses.Event.Button4Clicked: return MouseFlags.Button4Clicked;
  494. case Curses.Event.Button4DoubleClicked: return MouseFlags.Button4DoubleClicked;
  495. case Curses.Event.Button4TripleClicked: return MouseFlags.Button4TripleClicked;
  496. case Curses.Event.ButtonShift: return MouseFlags.ButtonShift;
  497. case Curses.Event.ButtonCtrl: return MouseFlags.ButtonCtrl;
  498. case Curses.Event.ButtonAlt: return MouseFlags.ButtonAlt;
  499. case Curses.Event.ReportMousePosition: return MouseFlags.ReportMousePosition;
  500. case Curses.Event.AllEvents: return MouseFlags.AllEvents;
  501. default: return 0;
  502. }
  503. }
  504. static MouseFlags SetControlKeyStates (Curses.MouseEvent cev, MouseFlags mouseFlag)
  505. {
  506. if ((cev.ButtonState & Curses.Event.ButtonCtrl) != 0 && (mouseFlag & MouseFlags.ButtonCtrl) == 0)
  507. mouseFlag |= MouseFlags.ButtonCtrl;
  508. if ((cev.ButtonState & Curses.Event.ButtonShift) != 0 && (mouseFlag & MouseFlags.ButtonShift) == 0)
  509. mouseFlag |= MouseFlags.ButtonShift;
  510. if ((cev.ButtonState & Curses.Event.ButtonAlt) != 0 && (mouseFlag & MouseFlags.ButtonAlt) == 0)
  511. mouseFlag |= MouseFlags.ButtonAlt;
  512. return mouseFlag;
  513. }
  514. KeyModifiers keyModifiers;
  515. KeyModifiers MapKeyModifiers (Key key)
  516. {
  517. if (keyModifiers == null)
  518. keyModifiers = new KeyModifiers ();
  519. if (!keyModifiers.Shift && (key & Key.ShiftMask) != 0)
  520. keyModifiers.Shift = true;
  521. if (!keyModifiers.Alt && (key & Key.AltMask) != 0)
  522. keyModifiers.Alt = true;
  523. if (!keyModifiers.Ctrl && (key & Key.CtrlMask) != 0)
  524. keyModifiers.Ctrl = true;
  525. return keyModifiers;
  526. }
  527. void ProcessInput (Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  528. {
  529. int wch;
  530. var code = Curses.get_wch (out wch);
  531. //System.Diagnostics.Debug.WriteLine ($"code: {code}; wch: {wch}");
  532. if (code == Curses.ERR)
  533. return;
  534. keyModifiers = new KeyModifiers ();
  535. Key k = Key.Null;
  536. if (code == Curses.KEY_CODE_YES) {
  537. if (wch == Curses.KeyResize) {
  538. if (Curses.CheckWinChange ()) {
  539. TerminalResized?.Invoke ();
  540. return;
  541. }
  542. }
  543. if (wch == Curses.KeyMouse) {
  544. Curses.getmouse (out Curses.MouseEvent ev);
  545. //System.Diagnostics.Debug.WriteLine ($"ButtonState: {ev.ButtonState}; ID: {ev.ID}; X: {ev.X}; Y: {ev.Y}; Z: {ev.Z}");
  546. mouseHandler (ToDriverMouse (ev));
  547. return;
  548. }
  549. k = MapCursesKey (wch);
  550. if (wch >= 277 && wch <= 288) { // Shift+(F1 - F12)
  551. wch -= 12;
  552. k = Key.ShiftMask | MapCursesKey (wch);
  553. } else if (wch >= 289 && wch <= 300) { // Ctrl+(F1 - F12)
  554. wch -= 24;
  555. k = Key.CtrlMask | MapCursesKey (wch);
  556. } else if (wch >= 301 && wch <= 312) { // Ctrl+Shift+(F1 - F12)
  557. wch -= 36;
  558. k = Key.CtrlMask | Key.ShiftMask | MapCursesKey (wch);
  559. } else if (wch >= 313 && wch <= 324) { // Alt+(F1 - F12)
  560. wch -= 48;
  561. k = Key.AltMask | MapCursesKey (wch);
  562. } else if (wch >= 325 && wch <= 327) { // Shift+Alt+(F1 - F3)
  563. wch -= 60;
  564. k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch);
  565. }
  566. keyDownHandler (new KeyEvent (k, MapKeyModifiers (k)));
  567. keyHandler (new KeyEvent (k, MapKeyModifiers (k)));
  568. keyUpHandler (new KeyEvent (k, MapKeyModifiers (k)));
  569. return;
  570. }
  571. // Special handling for ESC, we want to try to catch ESC+letter to simulate alt-letter as well as Alt-Fkey
  572. if (wch == 27) {
  573. Curses.timeout (200);
  574. code = Curses.get_wch (out int wch2);
  575. if (code == Curses.KEY_CODE_YES) {
  576. k = Key.AltMask | MapCursesKey (wch);
  577. }
  578. if (code == 0) {
  579. KeyEvent key;
  580. // The ESC-number handling, debatable.
  581. // Simulates the AltMask itself by pressing Alt + Space.
  582. if (wch2 == (int)Key.Space) {
  583. k = Key.AltMask;
  584. } else if (wch2 - (int)Key.Space >= (uint)Key.A && wch2 - (int)Key.Space <= (uint)Key.Z) {
  585. k = (Key)((uint)Key.AltMask + (wch2 - (int)Key.Space));
  586. } else if (wch2 >= (uint)Key.A - 64 && wch2 <= (uint)Key.Z - 64) {
  587. k = (Key)((uint)(Key.AltMask | Key.CtrlMask) + (wch2 + 64));
  588. } else if (wch2 >= (uint)Key.D0 && wch2 <= (uint)Key.D9) {
  589. k = (Key)((uint)Key.AltMask + (uint)Key.D0 + (wch2 - (uint)Key.D0));
  590. } else if (wch2 == 27) {
  591. k = (Key)wch2;
  592. } else if (wch2 == Curses.KEY_CODE_SEQ) {
  593. int [] c = null;
  594. while (code == 0) {
  595. code = Curses.get_wch (out wch2);
  596. if (wch2 > 0) {
  597. Array.Resize (ref c, c == null ? 1 : c.Length + 1);
  598. c [c.Length - 1] = wch2;
  599. }
  600. }
  601. if (c [0] == 49 && c [1] == 59 && c [2] == 55 && c [3] >= 80 && c [3] <= 83) { // Ctrl+Alt+(F1 - F4)
  602. wch2 = c [3] + 185;
  603. k = Key.CtrlMask | Key.AltMask | MapCursesKey (wch2);
  604. } else if (c [0] == 49 && c [2] == 59 && c [3] == 55 && c [4] == 126 && c [1] >= 53 && c [1] <= 57) { // Ctrl+Alt+(F5 - F8)
  605. wch2 = c [1] == 53 ? c [1] + 216 : c [1] + 215;
  606. k = Key.CtrlMask | Key.AltMask | MapCursesKey (wch2);
  607. } else if (c [0] == 50 && c [2] == 59 && c [3] == 55 && c [4] == 126 && c [1] >= 48 && c [1] <= 52) { // Ctrl+Alt+(F9 - F12)
  608. wch2 = c [1] < 51 ? c [1] + 225 : c [1] + 224;
  609. k = Key.CtrlMask | Key.AltMask | MapCursesKey (wch2);
  610. } else if (c [0] == 49 && c [1] == 59 && c [2] == 56 && c [3] >= 80 && c [3] <= 83) { // Ctrl+Shift+Alt+(F1 - F4)
  611. wch2 = c [3] + 185;
  612. k = Key.CtrlMask | Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  613. } else if (c [0] == 49 && c [2] == 59 && c [3] == 56 && c [4] == 126 && c [1] >= 53 && c [1] <= 57) { // Ctrl+Shift+Alt+(F5 - F8)
  614. wch2 = c [1] == 53 ? c [1] + 216 : c [1] + 215;
  615. k = Key.CtrlMask | Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  616. } else if (c [0] == 50 && c [2] == 59 && c [3] == 56 && c [4] == 126 && c [1] >= 48 && c [1] <= 52) { // Ctrl+Shift+Alt+(F9 - F12)
  617. wch2 = c [1] < 51 ? c [1] + 225 : c [1] + 224;
  618. k = Key.CtrlMask | Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  619. } else if (c [0] == 49 && c [1] == 59 && c [2] == 52 && c [3] == 83) { // Shift+Alt+(F4)
  620. wch2 = 268;
  621. k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  622. } else if (c [0] == 49 && c [2] == 59 && c [3] == 52 && c [4] == 126 && c [1] >= 53 && c [1] <= 57) { // Shift+Alt+(F5 - F8)
  623. wch2 = c [1] < 55 ? c [1] + 216 : c [1] + 215;
  624. k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  625. } else if (c [0] == 50 && c [2] == 59 && c [3] == 52 && c [4] == 126 && c [1] >= 48 && c [1] <= 52) { // Shift+Alt+(F9 - F12)
  626. wch2 = c [1] < 51 ? c [1] + 225 : c [1] + 224;
  627. k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch2);
  628. } else if (c [0] == 54 && c [1] == 59 && c [2] == 56 && c [3] == 126) { // Shift+Ctrl+Alt+KeyNPage
  629. k = Key.ShiftMask | Key.CtrlMask | Key.AltMask | Key.PageDown;
  630. } else if (c [0] == 53 && c [1] == 59 && c [2] == 56 && c [3] == 126) { // Shift+Ctrl+Alt+KeyPPage
  631. k = Key.ShiftMask | Key.CtrlMask | Key.AltMask | Key.PageUp;
  632. } else if (c [0] == 49 && c [1] == 59 && c [2] == 56 && c [3] == 72) { // Shift+Ctrl+Alt+KeyHome
  633. k = Key.ShiftMask | Key.CtrlMask | Key.AltMask | Key.Home;
  634. } else if (c [0] == 49 && c [1] == 59 && c [2] == 56 && c [3] == 70) { // Shift+Ctrl+Alt+KeyEnd
  635. k = Key.ShiftMask | Key.CtrlMask | Key.AltMask | Key.End;
  636. } else {
  637. k = MapCursesKey (wch2);
  638. }
  639. } else {
  640. // Unfortunately there are no way to differentiate Ctrl+Alt+alfa and Ctrl+Shift+Alt+alfa.
  641. if (((Key)wch2 & Key.CtrlMask) != 0) {
  642. keyModifiers.Ctrl = true;
  643. }
  644. if (wch2 == 0) {
  645. k = Key.CtrlMask | Key.AltMask | Key.Space;
  646. } else if (wch >= (uint)Key.A && wch <= (uint)Key.Z) {
  647. keyModifiers.Shift = true;
  648. keyModifiers.Alt = true;
  649. } else if (wch2 < 256) {
  650. k = (Key)wch2;
  651. keyModifiers.Alt = true;
  652. } else {
  653. k = (Key)((uint)(Key.AltMask | Key.CtrlMask) + wch2);
  654. }
  655. }
  656. key = new KeyEvent (k, MapKeyModifiers (k));
  657. keyDownHandler (key);
  658. keyHandler (key);
  659. } else {
  660. k = Key.Esc;
  661. keyHandler (new KeyEvent (k, MapKeyModifiers (k)));
  662. }
  663. } else if (wch == Curses.KeyTab) {
  664. k = MapCursesKey (wch);
  665. keyDownHandler (new KeyEvent (k, MapKeyModifiers (k)));
  666. keyHandler (new KeyEvent (k, MapKeyModifiers (k)));
  667. } else {
  668. // Unfortunately there are no way to differentiate Ctrl+alfa and Ctrl+Shift+alfa.
  669. k = (Key)wch;
  670. if (wch == 0) {
  671. k = Key.CtrlMask | Key.Space;
  672. } else if (wch >= (uint)Key.A - 64 && wch <= (uint)Key.Z - 64) {
  673. if ((Key)(wch + 64) != Key.J) {
  674. k = Key.CtrlMask | (Key)(wch + 64);
  675. }
  676. } else if (wch >= (uint)Key.A && wch <= (uint)Key.Z) {
  677. keyModifiers.Shift = true;
  678. }
  679. keyDownHandler (new KeyEvent (k, MapKeyModifiers (k)));
  680. keyHandler (new KeyEvent (k, MapKeyModifiers (k)));
  681. keyUpHandler (new KeyEvent (k, MapKeyModifiers (k)));
  682. }
  683. // Cause OnKeyUp and OnKeyPressed. Note that the special handling for ESC above
  684. // will not impact KeyUp.
  685. // This is causing ESC firing even if another keystroke was handled.
  686. //if (wch == Curses.KeyTab) {
  687. // keyUpHandler (new KeyEvent (MapCursesKey (wch), keyModifiers));
  688. //} else {
  689. // keyUpHandler (new KeyEvent ((Key)wch, keyModifiers));
  690. //}
  691. }
  692. Action<KeyEvent> keyHandler;
  693. Action<MouseEvent> mouseHandler;
  694. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  695. {
  696. // Note: Curses doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  697. Curses.timeout (0);
  698. this.keyHandler = keyHandler;
  699. this.mouseHandler = mouseHandler;
  700. var mLoop = mainLoop.Driver as UnixMainLoop;
  701. mLoop.AddWatch (0, UnixMainLoop.Condition.PollIn, x => {
  702. ProcessInput (keyHandler, keyDownHandler, keyUpHandler, mouseHandler);
  703. return true;
  704. });
  705. mLoop.WinChanged += () => {
  706. if (Curses.CheckWinChange ()) {
  707. Clip = new Rect (0, 0, Cols, Rows);
  708. UpdateOffScreen ();
  709. TerminalResized?.Invoke ();
  710. }
  711. };
  712. }
  713. Curses.Event oldMouseEvents, reportableMouseEvents;
  714. public override void Init (Action terminalResized)
  715. {
  716. if (window != null)
  717. return;
  718. try {
  719. //Set cursor key to application.
  720. //Console.Out.Write ("\x1b[?1h");
  721. //Console.Out.Flush ();
  722. window = Curses.initscr ();
  723. } catch (Exception e) {
  724. Console.WriteLine ("Curses failed to initialize, the exception is: " + e);
  725. }
  726. // Ensures that all procedures are performed at some previous closing.
  727. Curses.doupdate ();
  728. //
  729. // We are setting Invisible as default so we could ignore XTerm DECSUSR setting
  730. //
  731. switch (Curses.curs_set (0)) {
  732. case 0:
  733. currentCursorVisibility = initialCursorVisibility = CursorVisibility.Invisible;
  734. break;
  735. case 1:
  736. currentCursorVisibility = initialCursorVisibility = CursorVisibility.Underline;
  737. Curses.curs_set (1);
  738. break;
  739. case 2:
  740. currentCursorVisibility = initialCursorVisibility = CursorVisibility.Box;
  741. Curses.curs_set (2);
  742. break;
  743. default:
  744. currentCursorVisibility = initialCursorVisibility = null;
  745. break;
  746. }
  747. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  748. clipboard = new MacOSXClipboard ();
  749. } else {
  750. if (Is_WSL_Platform ()) {
  751. clipboard = new WSLClipboard ();
  752. } else {
  753. clipboard = new CursesClipboard ();
  754. }
  755. }
  756. Curses.raw ();
  757. Curses.noecho ();
  758. Curses.Window.Standard.keypad (true);
  759. reportableMouseEvents = Curses.mousemask (Curses.Event.AllEvents | Curses.Event.ReportMousePosition, out oldMouseEvents);
  760. TerminalResized = terminalResized;
  761. if (reportableMouseEvents.HasFlag (Curses.Event.ReportMousePosition))
  762. StartReportingMouseMoves ();
  763. //HLine = Curses.ACS_HLINE;
  764. //VLine = Curses.ACS_VLINE;
  765. //Stipple = Curses.ACS_CKBOARD;
  766. //Diamond = Curses.ACS_DIAMOND;
  767. //ULCorner = Curses.ACS_ULCORNER;
  768. //LLCorner = Curses.ACS_LLCORNER;
  769. //URCorner = Curses.ACS_URCORNER;
  770. //LRCorner = Curses.ACS_LRCORNER;
  771. //LeftTee = Curses.ACS_LTEE;
  772. //RightTee = Curses.ACS_RTEE;
  773. //TopTee = Curses.ACS_TTEE;
  774. //BottomTee = Curses.ACS_BTEE;
  775. //RightArrow = Curses.ACS_RARROW;
  776. //LeftArrow = Curses.ACS_LARROW;
  777. //UpArrow = Curses.ACS_UARROW;
  778. //DownArrow = Curses.ACS_DARROW;
  779. Colors.TopLevel = new ColorScheme ();
  780. Colors.Base = new ColorScheme ();
  781. Colors.Dialog = new ColorScheme ();
  782. Colors.Menu = new ColorScheme ();
  783. Colors.Error = new ColorScheme ();
  784. Clip = new Rect (0, 0, Cols, Rows);
  785. UpdateOffScreen ();
  786. if (Curses.HasColors) {
  787. Curses.StartColor ();
  788. Curses.UseDefaultColors ();
  789. Colors.TopLevel.Normal = MakeColor (Color.Green, Color.Black);
  790. Colors.TopLevel.Focus = MakeColor (Color.White, Color.Cyan);
  791. Colors.TopLevel.HotNormal = MakeColor (Color.Brown, Color.Black);
  792. Colors.TopLevel.HotFocus = MakeColor (Color.Blue, Color.Cyan);
  793. Colors.TopLevel.Disabled = MakeColor (Color.DarkGray, Color.Black);
  794. Colors.Base.Normal = MakeColor (Color.White, Color.Blue);
  795. Colors.Base.Focus = MakeColor (Color.Black, Color.Gray);
  796. Colors.Base.HotNormal = MakeColor (Color.Cyan, Color.Blue);
  797. Colors.Base.HotFocus = MakeColor (Color.BrightBlue, Color.Gray);
  798. Colors.Base.Disabled = MakeColor (Color.DarkGray, Color.Blue);
  799. // Focused,
  800. // Selected, Hot: Yellow on Black
  801. // Selected, text: white on black
  802. // Unselected, hot: yellow on cyan
  803. // unselected, text: same as unfocused
  804. Colors.Menu.Normal = MakeColor (Color.White, Color.DarkGray);
  805. Colors.Menu.Focus = MakeColor (Color.White, Color.Black);
  806. Colors.Menu.HotNormal = MakeColor (Color.BrightYellow, Color.DarkGray);
  807. Colors.Menu.HotFocus = MakeColor (Color.BrightYellow, Color.Black);
  808. Colors.Menu.Disabled = MakeColor (Color.Gray, Color.DarkGray);
  809. Colors.Dialog.Normal = MakeColor (Color.Black, Color.Gray);
  810. Colors.Dialog.Focus = MakeColor (Color.White, Color.DarkGray);
  811. Colors.Dialog.HotNormal = MakeColor (Color.Blue, Color.Gray);
  812. Colors.Dialog.HotFocus = MakeColor (Color.Blue, Color.DarkGray);
  813. Colors.Dialog.Disabled = MakeColor (Color.DarkGray, Color.Gray);
  814. Colors.Error.Normal = MakeColor (Color.Red, Color.White);
  815. Colors.Error.Focus = MakeColor (Color.White, Color.Red);
  816. Colors.Error.HotNormal = MakeColor (Color.Black, Color.White);
  817. Colors.Error.HotFocus = MakeColor (Color.Black, Color.Red);
  818. Colors.Error.Disabled = MakeColor (Color.DarkGray, Color.White);
  819. } else {
  820. Colors.TopLevel.Normal = Curses.COLOR_GREEN;
  821. Colors.TopLevel.Focus = Curses.COLOR_WHITE;
  822. Colors.TopLevel.HotNormal = Curses.COLOR_YELLOW;
  823. Colors.TopLevel.HotFocus = Curses.COLOR_YELLOW;
  824. Colors.TopLevel.Disabled = Curses.A_BOLD | Curses.COLOR_GRAY;
  825. Colors.Base.Normal = Curses.A_NORMAL;
  826. Colors.Base.Focus = Curses.A_REVERSE;
  827. Colors.Base.HotNormal = Curses.A_BOLD;
  828. Colors.Base.HotFocus = Curses.A_BOLD | Curses.A_REVERSE;
  829. Colors.Base.Disabled = Curses.A_BOLD | Curses.COLOR_GRAY;
  830. Colors.Menu.Normal = Curses.A_REVERSE;
  831. Colors.Menu.Focus = Curses.A_NORMAL;
  832. Colors.Menu.HotNormal = Curses.A_BOLD;
  833. Colors.Menu.HotFocus = Curses.A_NORMAL;
  834. Colors.Menu.Disabled = Curses.A_BOLD | Curses.COLOR_GRAY;
  835. Colors.Dialog.Normal = Curses.A_REVERSE;
  836. Colors.Dialog.Focus = Curses.A_NORMAL;
  837. Colors.Dialog.HotNormal = Curses.A_BOLD;
  838. Colors.Dialog.HotFocus = Curses.A_NORMAL;
  839. Colors.Dialog.Disabled = Curses.A_BOLD | Curses.COLOR_GRAY;
  840. Colors.Error.Normal = Curses.A_BOLD;
  841. Colors.Error.Focus = Curses.A_BOLD | Curses.A_REVERSE;
  842. Colors.Error.HotNormal = Curses.A_BOLD | Curses.A_REVERSE;
  843. Colors.Error.HotFocus = Curses.A_REVERSE;
  844. Colors.Error.Disabled = Curses.A_BOLD | Curses.COLOR_GRAY;
  845. }
  846. }
  847. public override void UpdateOffScreen ()
  848. {
  849. contents = new int [Rows, Cols, 3];
  850. for (int row = 0; row < Rows; row++) {
  851. for (int col = 0; col < Cols; col++) {
  852. //Curses.move (row, col);
  853. //Curses.attrset (Colors.TopLevel.Normal);
  854. //Curses.addch ((int)(uint)' ');
  855. contents [row, col, 0] = ' ';
  856. contents [row, col, 1] = Colors.TopLevel.Normal;
  857. contents [row, col, 2] = 0;
  858. }
  859. }
  860. }
  861. public static bool Is_WSL_Platform ()
  862. {
  863. var result = BashRunner.Run ("uname -a", runCurses: false);
  864. if (result.Contains ("microsoft") && result.Contains ("WSL")) {
  865. return true;
  866. }
  867. return false;
  868. }
  869. static int MapColor (Color color)
  870. {
  871. switch (color) {
  872. case Color.Black:
  873. return Curses.COLOR_BLACK;
  874. case Color.Blue:
  875. return Curses.COLOR_BLUE;
  876. case Color.Green:
  877. return Curses.COLOR_GREEN;
  878. case Color.Cyan:
  879. return Curses.COLOR_CYAN;
  880. case Color.Red:
  881. return Curses.COLOR_RED;
  882. case Color.Magenta:
  883. return Curses.COLOR_MAGENTA;
  884. case Color.Brown:
  885. return Curses.COLOR_YELLOW;
  886. case Color.Gray:
  887. return Curses.COLOR_WHITE;
  888. case Color.DarkGray:
  889. //return Curses.COLOR_BLACK | Curses.A_BOLD;
  890. return Curses.COLOR_GRAY;
  891. case Color.BrightBlue:
  892. return Curses.COLOR_BLUE | Curses.A_BOLD | Curses.COLOR_GRAY;
  893. case Color.BrightGreen:
  894. return Curses.COLOR_GREEN | Curses.A_BOLD | Curses.COLOR_GRAY;
  895. case Color.BrightCyan:
  896. return Curses.COLOR_CYAN | Curses.A_BOLD | Curses.COLOR_GRAY;
  897. case Color.BrightRed:
  898. return Curses.COLOR_RED | Curses.A_BOLD | Curses.COLOR_GRAY;
  899. case Color.BrightMagenta:
  900. return Curses.COLOR_MAGENTA | Curses.A_BOLD | Curses.COLOR_GRAY;
  901. case Color.BrightYellow:
  902. return Curses.COLOR_YELLOW | Curses.A_BOLD | Curses.COLOR_GRAY;
  903. case Color.White:
  904. return Curses.COLOR_WHITE | Curses.A_BOLD | Curses.COLOR_GRAY;
  905. }
  906. throw new ArgumentException ("Invalid color code");
  907. }
  908. static Color MapCursesColor (int color)
  909. {
  910. switch (color) {
  911. case Curses.COLOR_BLACK:
  912. return Color.Black;
  913. case Curses.COLOR_BLUE:
  914. return Color.Blue;
  915. case Curses.COLOR_GREEN:
  916. return Color.Green;
  917. case Curses.COLOR_CYAN:
  918. return Color.Cyan;
  919. case Curses.COLOR_RED:
  920. return Color.Red;
  921. case Curses.COLOR_MAGENTA:
  922. return Color.Magenta;
  923. case Curses.COLOR_YELLOW:
  924. return Color.Brown;
  925. case Curses.COLOR_WHITE:
  926. return Color.Gray;
  927. case Curses.COLOR_GRAY:
  928. return Color.DarkGray;
  929. case Curses.COLOR_BLUE | Curses.COLOR_GRAY:
  930. return Color.BrightBlue;
  931. case Curses.COLOR_GREEN | Curses.COLOR_GRAY:
  932. return Color.BrightGreen;
  933. case Curses.COLOR_CYAN | Curses.COLOR_GRAY:
  934. return Color.BrightCyan;
  935. case Curses.COLOR_RED | Curses.COLOR_GRAY:
  936. return Color.BrightRed;
  937. case Curses.COLOR_MAGENTA | Curses.COLOR_GRAY:
  938. return Color.BrightMagenta;
  939. case Curses.COLOR_YELLOW | Curses.COLOR_GRAY:
  940. return Color.BrightYellow;
  941. case Curses.COLOR_WHITE | Curses.COLOR_GRAY:
  942. return Color.White;
  943. }
  944. throw new ArgumentException ("Invalid curses color code");
  945. }
  946. public override Attribute MakeAttribute (Color fore, Color back)
  947. {
  948. var f = MapColor (fore);
  949. //return MakeColor ((short)(f & 0xffff), (short)MapColor (back)) | ((f & Curses.A_BOLD) != 0 ? Curses.A_BOLD : 0);
  950. return MakeColor ((short)(f & 0xffff), (short)MapColor (back));
  951. }
  952. public override void Suspend ()
  953. {
  954. if (reportableMouseEvents.HasFlag (Curses.Event.ReportMousePosition))
  955. StopReportingMouseMoves ();
  956. Platform.Suspend ();
  957. Curses.Window.Standard.redrawwin ();
  958. Curses.refresh ();
  959. if (reportableMouseEvents.HasFlag (Curses.Event.ReportMousePosition))
  960. StartReportingMouseMoves ();
  961. }
  962. public override void StartReportingMouseMoves ()
  963. {
  964. Console.Out.Write ("\x1b[?1003h");
  965. Console.Out.Flush ();
  966. }
  967. public override void StopReportingMouseMoves ()
  968. {
  969. Console.Out.Write ("\x1b[?1003l");
  970. Console.Out.Flush ();
  971. }
  972. //int lastMouseInterval;
  973. //bool mouseGrabbed;
  974. public override void UncookMouse ()
  975. {
  976. //if (mouseGrabbed)
  977. // return;
  978. //lastMouseInterval = Curses.mouseinterval (0);
  979. //mouseGrabbed = true;
  980. }
  981. public override void CookMouse ()
  982. {
  983. //mouseGrabbed = false;
  984. //Curses.mouseinterval (lastMouseInterval);
  985. }
  986. public override Attribute GetAttribute ()
  987. {
  988. return currentAttribute;
  989. }
  990. /// <inheritdoc/>
  991. public override bool GetCursorVisibility (out CursorVisibility visibility)
  992. {
  993. visibility = CursorVisibility.Invisible;
  994. if (!currentCursorVisibility.HasValue)
  995. return false;
  996. visibility = currentCursorVisibility.Value;
  997. return true;
  998. }
  999. /// <inheritdoc/>
  1000. public override bool SetCursorVisibility (CursorVisibility visibility)
  1001. {
  1002. if (initialCursorVisibility.HasValue == false)
  1003. return false;
  1004. Curses.curs_set (((int)visibility >> 16) & 0x000000FF);
  1005. if (visibility != CursorVisibility.Invisible) {
  1006. Console.Out.Write ("\x1b[{0} q", ((int)visibility >> 24) & 0xFF);
  1007. Console.Out.Flush ();
  1008. }
  1009. currentCursorVisibility = visibility;
  1010. return true;
  1011. }
  1012. /// <inheritdoc/>
  1013. public override bool EnsureCursorVisibility ()
  1014. {
  1015. return false;
  1016. }
  1017. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  1018. {
  1019. Key k;
  1020. if ((shift || alt || control)
  1021. && keyChar - (int)Key.Space >= (uint)Key.A && keyChar - (int)Key.Space <= (uint)Key.Z) {
  1022. k = (Key)(keyChar - (uint)Key.Space);
  1023. } else {
  1024. k = (Key)keyChar;
  1025. }
  1026. if (shift) {
  1027. k |= Key.ShiftMask;
  1028. }
  1029. if (alt) {
  1030. k |= Key.AltMask;
  1031. }
  1032. if (control) {
  1033. k |= Key.CtrlMask;
  1034. }
  1035. keyHandler (new KeyEvent (k, MapKeyModifiers (k)));
  1036. }
  1037. public override bool GetColors (int value, out Color foreground, out Color background)
  1038. {
  1039. bool hasColor = false;
  1040. foreground = default;
  1041. background = default;
  1042. int back = -1;
  1043. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  1044. .OfType<ConsoleColor> ()
  1045. .Select (s => (int)s);
  1046. if (values.Contains ((value >> 12) & 0xffff)) {
  1047. hasColor = true;
  1048. back = (value >> 12) & 0xffff;
  1049. background = MapCursesColor (back);
  1050. }
  1051. if (values.Contains ((value - (back << 12)) >> 8)) {
  1052. hasColor = true;
  1053. foreground = MapCursesColor ((value - (back << 12)) >> 8);
  1054. }
  1055. return hasColor;
  1056. }
  1057. }
  1058. internal static class Platform {
  1059. [DllImport ("libc")]
  1060. static extern int uname (IntPtr buf);
  1061. [DllImport ("libc")]
  1062. static extern int killpg (int pgrp, int pid);
  1063. static int suspendSignal;
  1064. static int GetSuspendSignal ()
  1065. {
  1066. if (suspendSignal != 0)
  1067. return suspendSignal;
  1068. IntPtr buf = Marshal.AllocHGlobal (8192);
  1069. if (uname (buf) != 0) {
  1070. Marshal.FreeHGlobal (buf);
  1071. suspendSignal = -1;
  1072. return suspendSignal;
  1073. }
  1074. try {
  1075. switch (Marshal.PtrToStringAnsi (buf)) {
  1076. case "Darwin":
  1077. case "DragonFly":
  1078. case "FreeBSD":
  1079. case "NetBSD":
  1080. case "OpenBSD":
  1081. suspendSignal = 18;
  1082. break;
  1083. case "Linux":
  1084. // TODO: should fetch the machine name and
  1085. // if it is MIPS return 24
  1086. suspendSignal = 20;
  1087. break;
  1088. case "Solaris":
  1089. suspendSignal = 24;
  1090. break;
  1091. default:
  1092. suspendSignal = -1;
  1093. break;
  1094. }
  1095. return suspendSignal;
  1096. } finally {
  1097. Marshal.FreeHGlobal (buf);
  1098. }
  1099. }
  1100. /// <summary>
  1101. /// Suspends the process by sending SIGTSTP to itself
  1102. /// </summary>
  1103. /// <returns>The suspend.</returns>
  1104. static public bool Suspend ()
  1105. {
  1106. int signal = GetSuspendSignal ();
  1107. if (signal == -1)
  1108. return false;
  1109. killpg (0, signal);
  1110. return true;
  1111. }
  1112. }
  1113. class CursesClipboard : ClipboardBase {
  1114. public CursesClipboard ()
  1115. {
  1116. IsSupported = CheckSupport ();
  1117. }
  1118. public override bool IsSupported { get; }
  1119. bool CheckSupport ()
  1120. {
  1121. try {
  1122. var result = BashRunner.Run ("which xclip", runCurses: false);
  1123. return result.FileExists ();
  1124. } catch (Exception) {
  1125. // Permissions issue.
  1126. return false;
  1127. }
  1128. }
  1129. protected override string GetClipboardDataImpl ()
  1130. {
  1131. var tempFileName = System.IO.Path.GetTempFileName ();
  1132. try {
  1133. // BashRunner.Run ($"xsel -o --clipboard > {tempFileName}");
  1134. BashRunner.Run ($"xclip -selection clipboard -o > {tempFileName}");
  1135. return System.IO.File.ReadAllText (tempFileName);
  1136. } finally {
  1137. System.IO.File.Delete (tempFileName);
  1138. }
  1139. }
  1140. protected override void SetClipboardDataImpl (string text)
  1141. {
  1142. // var tempFileName = System.IO.Path.GetTempFileName ();
  1143. // System.IO.File.WriteAllText (tempFileName, text);
  1144. // try {
  1145. // // BashRunner.Run ($"cat {tempFileName} | xsel -i --clipboard");
  1146. // BashRunner.Run ($"cat {tempFileName} | xclip -selection clipboard");
  1147. // } finally {
  1148. // System.IO.File.Delete (tempFileName);
  1149. // }
  1150. BashRunner.Run ("xclip -selection clipboard -i", false, text);
  1151. }
  1152. }
  1153. static class BashRunner {
  1154. public static string Run (string commandLine, bool output = true, string inputText = "", bool runCurses = true)
  1155. {
  1156. var arguments = $"-c \"{commandLine}\"";
  1157. if (output) {
  1158. var errorBuilder = new System.Text.StringBuilder ();
  1159. var outputBuilder = new System.Text.StringBuilder ();
  1160. using (var process = new System.Diagnostics.Process {
  1161. StartInfo = new System.Diagnostics.ProcessStartInfo {
  1162. FileName = "bash",
  1163. Arguments = arguments,
  1164. RedirectStandardOutput = true,
  1165. RedirectStandardError = true,
  1166. UseShellExecute = false,
  1167. CreateNoWindow = false,
  1168. }
  1169. }) {
  1170. process.Start ();
  1171. process.OutputDataReceived += (sender, args) => { outputBuilder.AppendLine (args.Data); };
  1172. process.BeginOutputReadLine ();
  1173. process.ErrorDataReceived += (sender, args) => { errorBuilder.AppendLine (args.Data); };
  1174. process.BeginErrorReadLine ();
  1175. if (!process.DoubleWaitForExit ()) {
  1176. var timeoutError = $@"Process timed out. Command line: bash {arguments}.
  1177. Output: {outputBuilder}
  1178. Error: {errorBuilder}";
  1179. throw new Exception (timeoutError);
  1180. }
  1181. if (process.ExitCode == 0) {
  1182. if (runCurses && Application.Driver is CursesDriver) {
  1183. Curses.raw ();
  1184. Curses.noecho ();
  1185. }
  1186. return outputBuilder.ToString ();
  1187. }
  1188. var error = $@"Could not execute process. Command line: bash {arguments}.
  1189. Output: {outputBuilder}
  1190. Error: {errorBuilder}";
  1191. throw new Exception (error);
  1192. }
  1193. } else {
  1194. using (var process = new System.Diagnostics.Process {
  1195. StartInfo = new System.Diagnostics.ProcessStartInfo {
  1196. FileName = "bash",
  1197. Arguments = arguments,
  1198. RedirectStandardInput = true,
  1199. UseShellExecute = false,
  1200. CreateNoWindow = false
  1201. }
  1202. }) {
  1203. process.Start ();
  1204. process.StandardInput.Write (inputText);
  1205. process.StandardInput.Close ();
  1206. process.WaitForExit ();
  1207. if (runCurses && Application.Driver is CursesDriver) {
  1208. Curses.raw ();
  1209. Curses.noecho ();
  1210. }
  1211. return inputText;
  1212. }
  1213. }
  1214. }
  1215. public static bool DoubleWaitForExit (this System.Diagnostics.Process process)
  1216. {
  1217. var result = process.WaitForExit (500);
  1218. if (result) {
  1219. process.WaitForExit ();
  1220. }
  1221. return result;
  1222. }
  1223. public static bool FileExists (this string value)
  1224. {
  1225. return !string.IsNullOrEmpty (value) && !value.Contains ("not found");
  1226. }
  1227. }
  1228. class MacOSXClipboard : ClipboardBase {
  1229. IntPtr nsString = objc_getClass ("NSString");
  1230. IntPtr nsPasteboard = objc_getClass ("NSPasteboard");
  1231. IntPtr utfTextType;
  1232. IntPtr generalPasteboard;
  1233. IntPtr initWithUtf8Register = sel_registerName ("initWithUTF8String:");
  1234. IntPtr allocRegister = sel_registerName ("alloc");
  1235. IntPtr setStringRegister = sel_registerName ("setString:forType:");
  1236. IntPtr stringForTypeRegister = sel_registerName ("stringForType:");
  1237. IntPtr utf8Register = sel_registerName ("UTF8String");
  1238. IntPtr nsStringPboardType;
  1239. IntPtr generalPasteboardRegister = sel_registerName ("generalPasteboard");
  1240. IntPtr clearContentsRegister = sel_registerName ("clearContents");
  1241. public MacOSXClipboard ()
  1242. {
  1243. utfTextType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "public.utf8-plain-text");
  1244. nsStringPboardType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "NSStringPboardType");
  1245. generalPasteboard = objc_msgSend (nsPasteboard, generalPasteboardRegister);
  1246. IsSupported = CheckSupport ();
  1247. }
  1248. public override bool IsSupported { get; }
  1249. bool CheckSupport ()
  1250. {
  1251. var result = BashRunner.Run ("which pbcopy");
  1252. if (!result.FileExists ()) {
  1253. return false;
  1254. }
  1255. result = BashRunner.Run ("which pbpaste");
  1256. return result.FileExists ();
  1257. }
  1258. protected override string GetClipboardDataImpl ()
  1259. {
  1260. var ptr = objc_msgSend (generalPasteboard, stringForTypeRegister, nsStringPboardType);
  1261. var charArray = objc_msgSend (ptr, utf8Register);
  1262. return Marshal.PtrToStringAnsi (charArray);
  1263. }
  1264. protected override void SetClipboardDataImpl (string text)
  1265. {
  1266. IntPtr str = default;
  1267. try {
  1268. str = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, text);
  1269. objc_msgSend (generalPasteboard, clearContentsRegister);
  1270. objc_msgSend (generalPasteboard, setStringRegister, str, utfTextType);
  1271. } finally {
  1272. if (str != default) {
  1273. objc_msgSend (str, sel_registerName ("release"));
  1274. }
  1275. }
  1276. }
  1277. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1278. static extern IntPtr objc_getClass (string className);
  1279. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1280. static extern IntPtr objc_msgSend (IntPtr receiver, IntPtr selector);
  1281. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1282. static extern IntPtr objc_msgSend (IntPtr receiver, IntPtr selector, string arg1);
  1283. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1284. static extern IntPtr objc_msgSend (IntPtr receiver, IntPtr selector, IntPtr arg1);
  1285. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1286. static extern IntPtr objc_msgSend (IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);
  1287. [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
  1288. static extern IntPtr sel_registerName (string selectorName);
  1289. }
  1290. class WSLClipboard : ClipboardBase {
  1291. public WSLClipboard ()
  1292. {
  1293. IsSupported = CheckSupport ();
  1294. }
  1295. public override bool IsSupported { get; }
  1296. bool CheckSupport ()
  1297. {
  1298. try {
  1299. var result = BashRunner.Run ("which powershell.exe");
  1300. return result.FileExists ();
  1301. } catch (System.Exception) {
  1302. return false;
  1303. }
  1304. //var result = BashRunner.Run ("which powershell.exe");
  1305. //if (!result.FileExists ()) {
  1306. // return false;
  1307. //}
  1308. //result = BashRunner.Run ("which clip.exe");
  1309. //return result.FileExists ();
  1310. }
  1311. protected override string GetClipboardDataImpl ()
  1312. {
  1313. using (var powershell = new System.Diagnostics.Process {
  1314. StartInfo = new System.Diagnostics.ProcessStartInfo {
  1315. RedirectStandardOutput = true,
  1316. FileName = "powershell.exe",
  1317. Arguments = "-noprofile -command \"Get-Clipboard\"",
  1318. UseShellExecute = Application.Driver is CursesDriver,
  1319. CreateNoWindow = true
  1320. }
  1321. }) {
  1322. powershell.Start ();
  1323. var result = powershell.StandardOutput.ReadToEnd ();
  1324. powershell.StandardOutput.Close ();
  1325. powershell.WaitForExit ();
  1326. if (!powershell.DoubleWaitForExit ()) {
  1327. var timeoutError = $@"Process timed out. Command line: bash {powershell.StartInfo.Arguments}.
  1328. Output: {powershell.StandardOutput.ReadToEnd ()}
  1329. Error: {powershell.StandardError.ReadToEnd ()}";
  1330. throw new Exception (timeoutError);
  1331. }
  1332. if (Application.Driver is CursesDriver) {
  1333. Curses.raw ();
  1334. Curses.noecho ();
  1335. }
  1336. if (result.EndsWith ("\r\n")) {
  1337. result = result.Substring (0, result.Length - 2);
  1338. }
  1339. return result;
  1340. }
  1341. }
  1342. protected override void SetClipboardDataImpl (string text)
  1343. {
  1344. using (var powershell = new System.Diagnostics.Process {
  1345. StartInfo = new System.Diagnostics.ProcessStartInfo {
  1346. FileName = "powershell.exe",
  1347. Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{text}\\\"\""
  1348. }
  1349. }) {
  1350. powershell.Start ();
  1351. powershell.WaitForExit ();
  1352. if (!powershell.DoubleWaitForExit ()) {
  1353. var timeoutError = $@"Process timed out. Command line: bash {powershell.StartInfo.Arguments}.
  1354. Output: {powershell.StandardOutput.ReadToEnd ()}
  1355. Error: {powershell.StandardError.ReadToEnd ()}";
  1356. throw new Exception (timeoutError);
  1357. }
  1358. if (Application.Driver is CursesDriver) {
  1359. Curses.raw ();
  1360. Curses.noecho ();
  1361. }
  1362. }
  1363. //using (var clipExe = new System.Diagnostics.Process {
  1364. // StartInfo = new System.Diagnostics.ProcessStartInfo {
  1365. // FileName = "clip.exe",
  1366. // RedirectStandardInput = true
  1367. // }
  1368. //}) {
  1369. // clipExe.Start ();
  1370. // clipExe.StandardInput.Write (text);
  1371. // clipExe.StandardInput.Close ();
  1372. // clipExe.WaitForExit ();
  1373. //}
  1374. }
  1375. }
  1376. }