binding.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // TODO:
  3. // * FindNCurses needs to remove the old probing code
  4. // * Removal of that proxy code
  5. // * Need to implement reading pointers with the new API
  6. // * Can remove the manual Dlopen features
  7. // * initscr() diagnostics based on DLL can be fixed
  8. //
  9. // binding.cs.in: Core binding for curses.
  10. //
  11. // This file attempts to call into ncurses without relying on Mono's
  12. // dllmap, so it will work with .NET Core. This means that it needs
  13. // two sets of bindings, one for "ncurses" which works on OSX, and one
  14. // that works against "libncursesw.so.5" which is what you find on
  15. // assorted Linux systems.
  16. //
  17. // Additionally, I do not want to rely on an external native library
  18. // which is why all this pain to bind two separate ncurses is here.
  19. //
  20. // Authors:
  21. // Miguel de Icaza ([email protected])
  22. //
  23. // Copyright (C) 2007 Novell (http://www.novell.com)
  24. //
  25. // Permission is hereby granted, free of charge, to any person obtaining
  26. // a copy of this software and associated documentation files (the
  27. // "Software"), to deal in the Software without restriction, including
  28. // without limitation the rights to use, copy, modify, merge, publish,
  29. // distribute, sublicense, and/or sell copies of the Software, and to
  30. // permit persons to whom the Software is furnished to do so, subject to
  31. // the following conditions:
  32. //
  33. // The above copyright notice and this permission notice shall be
  34. // included in all copies or substantial portions of the Software.
  35. //
  36. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  37. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  38. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  39. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  40. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  41. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  42. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. //
  44. using System;
  45. using System.IO;
  46. using System.Runtime.InteropServices;
  47. using Mono.Terminal.Internal;
  48. namespace Unix.Terminal {
  49. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  50. public partial class Curses {
  51. [StructLayout (LayoutKind.Sequential)]
  52. public struct MouseEvent {
  53. public short ID;
  54. public int X, Y, Z;
  55. public Event ButtonState;
  56. }
  57. static int lines, cols;
  58. static Window main_window;
  59. static IntPtr curses_handle, curscr_ptr, lines_ptr, cols_ptr;
  60. // If true, uses the DllImport into "ncurses", otherwise "libncursesw.so.5"
  61. //static bool use_naked_driver;
  62. static UnmanagedLibrary curses_library;
  63. static NativeMethods methods;
  64. [DllImport("libc")]
  65. public extern static int setlocale(int cate, [MarshalAs(UnmanagedType.LPStr)] string locale);
  66. static void LoadMethods ()
  67. {
  68. var libs = UnmanagedLibrary.IsMacOSPlatform ? new string [] { "libncurses.dylib" } : new string [] { "libncursesw.so.6", "libncursesw.so.5" };
  69. curses_library = new UnmanagedLibrary (libs, false);
  70. methods = new NativeMethods (curses_library);
  71. }
  72. static void FindNCurses ()
  73. {
  74. LoadMethods ();
  75. curses_handle = methods.UnmanagedLibrary.NativeLibraryHandle;
  76. stdscr = read_static_ptr ("stdscr");
  77. curscr_ptr = get_ptr ("curscr");
  78. lines_ptr = get_ptr ("LINES");
  79. cols_ptr = get_ptr ("COLS");
  80. }
  81. static public Window initscr ()
  82. {
  83. setlocale(LC_ALL, "");
  84. FindNCurses ();
  85. main_window = new Window (methods.initscr ());
  86. try {
  87. console_sharp_get_dims (out lines, out cols);
  88. } catch (DllNotFoundException){
  89. endwin ();
  90. Console.Error.WriteLine ("Unable to find the @MONO_CURSES@ native library\n" +
  91. "this is different than the managed mono-curses.dll\n\n" +
  92. "Typically you need to install to a LD_LIBRARY_PATH directory\n" +
  93. "or DYLD_LIBRARY_PATH directory or run /sbin/ldconfig");
  94. Environment.Exit (1);
  95. }
  96. return main_window;
  97. }
  98. public static int Lines {
  99. get {
  100. return lines;
  101. }
  102. }
  103. public static int Cols {
  104. get {
  105. return cols;
  106. }
  107. }
  108. //
  109. // Returns true if the window changed since the last invocation, as a
  110. // side effect, the Lines and Cols properties are updated
  111. //
  112. public static bool CheckWinChange ()
  113. {
  114. int l, c;
  115. console_sharp_get_dims (out l, out c);
  116. if (l != lines || c != cols){
  117. lines = l;
  118. cols = c;
  119. return true;
  120. }
  121. return false;
  122. }
  123. public static int addstr (string format, params object [] args)
  124. {
  125. var s = string.Format (format, args);
  126. return addwstr (s);
  127. }
  128. static char [] r = new char [1];
  129. //
  130. // Have to wrap the native addch, as it can not
  131. // display unicode characters, we have to use addstr
  132. // for that. but we need addch to render special ACS
  133. // characters
  134. //
  135. public static int addch (int ch)
  136. {
  137. if (ch < 127 || ch > 0xffff )
  138. return methods.addch (ch);
  139. char c = (char) ch;
  140. return addwstr (new String (c, 1));
  141. }
  142. static IntPtr stdscr;
  143. static IntPtr get_ptr (string key)
  144. {
  145. var ptr = curses_library.LoadSymbol (key);
  146. if (ptr == IntPtr.Zero)
  147. throw new Exception ("Could not load the key " + key);
  148. return ptr;
  149. }
  150. internal static IntPtr read_static_ptr (string key)
  151. {
  152. var ptr = get_ptr (key);
  153. return Marshal.ReadIntPtr (ptr);
  154. }
  155. internal static IntPtr console_sharp_get_stdscr () => stdscr;
  156. internal static IntPtr console_sharp_get_curscr ()
  157. {
  158. return Marshal.ReadIntPtr (curscr_ptr);
  159. }
  160. internal static void console_sharp_get_dims (out int lines, out int cols)
  161. {
  162. lines = Marshal.ReadInt32 (lines_ptr);
  163. cols = Marshal.ReadInt32 (cols_ptr);
  164. }
  165. public static Event mousemask (Event newmask, out Event oldmask)
  166. {
  167. IntPtr e;
  168. var ret = (Event) (methods.mousemask ((IntPtr) newmask, out e));
  169. oldmask = (Event) e;
  170. return ret;
  171. }
  172. // We encode ESC + char (what Alt-char generates) as 0x2000 + char
  173. public const int KeyAlt = 0x2000;
  174. static public int IsAlt (int key)
  175. {
  176. if ((key & KeyAlt) != 0)
  177. return key & ~KeyAlt;
  178. return 0;
  179. }
  180. public static int StartColor () => methods.start_color ();
  181. public static bool HasColors => methods.has_colors ();
  182. public static int InitColorPair (short pair, short foreground, short background) => methods.init_pair (pair, foreground, background);
  183. public static int UseDefaultColors () => methods.use_default_colors ();
  184. public static int ColorPairs => methods.COLOR_PAIRS();
  185. //
  186. // The proxy methods to call into each version
  187. //
  188. static public int endwin () => methods.endwin ();
  189. static public bool isendwin () => methods.isendwin ();
  190. static public int cbreak () => methods.cbreak ();
  191. static public int nocbreak () => methods.nocbreak ();
  192. static public int echo () => methods.echo ();
  193. static public int noecho () => methods.noecho ();
  194. static public int halfdelay (int t) => methods.halfdelay (t);
  195. static public int raw () => methods.raw ();
  196. static public int noraw () => methods.noraw ();
  197. static public void noqiflush () => methods.noqiflush ();
  198. static public void qiflush () => methods.qiflush ();
  199. static public int typeahead (IntPtr fd) => methods.typeahead (fd);
  200. static public int timeout (int delay) => methods.timeout (delay);
  201. static public int wtimeout (IntPtr win, int delay) => methods.wtimeout (win, delay);
  202. static public int notimeout (IntPtr win, bool bf) => methods.notimeout (win, bf);
  203. static public int keypad (IntPtr win, bool bf) => methods.keypad (win, bf);
  204. static public int meta (IntPtr win, bool bf) => methods.meta (win, bf);
  205. static public int intrflush (IntPtr win, bool bf) => methods.intrflush (win, bf);
  206. static public int clearok (IntPtr win, bool bf) => methods.clearok (win, bf);
  207. static public int idlok (IntPtr win, bool bf) => methods.idlok (win, bf);
  208. static public void idcok (IntPtr win, bool bf) => methods.idcok (win, bf);
  209. static public void immedok (IntPtr win, bool bf) => methods.immedok (win, bf);
  210. static public int leaveok (IntPtr win, bool bf) => methods.leaveok (win, bf);
  211. static public int wsetscrreg (IntPtr win, int top, int bot) => methods.wsetscrreg (win, top, bot);
  212. static public int scrollok (IntPtr win, bool bf) => methods.scrollok (win, bf);
  213. static public int nl() => methods.nl();
  214. static public int nonl() => methods.nonl();
  215. static public int setscrreg (int top, int bot) => methods.setscrreg (top, bot);
  216. static public int refresh () => methods.refresh ();
  217. static public int doupdate() => methods.doupdate();
  218. static public int wrefresh (IntPtr win) => methods.wrefresh (win);
  219. static public int redrawwin (IntPtr win) => methods.redrawwin (win);
  220. //static public int wredrawwin (IntPtr win, int beg_line, int num_lines) => methods.wredrawwin (win, beg_line, num_lines);
  221. static public int wnoutrefresh (IntPtr win) => methods.wnoutrefresh (win);
  222. static public int move (int line, int col) => methods.move (line, col);
  223. //static public int addch (int ch) => methods.addch (ch);
  224. static public int addwstr (string s) => methods.addwstr (s);
  225. static public int wmove (IntPtr win, int line, int col) => methods.wmove (win, line, col);
  226. static public int waddch (IntPtr win, int ch) => methods.waddch (win, ch);
  227. static public int attron (int attrs) => methods.attron (attrs);
  228. static public int attroff (int attrs) => methods.attroff (attrs);
  229. static public int attrset (int attrs) => methods.attrset (attrs);
  230. static public int getch () => methods.getch ();
  231. static public int get_wch (out int sequence) => methods.get_wch (out sequence);
  232. static public int ungetch (int ch) => methods.ungetch (ch);
  233. static public int mvgetch (int y, int x) => methods.mvgetch (y, x);
  234. static public bool has_colors () => methods.has_colors ();
  235. static public int start_color () => methods.start_color ();
  236. static public int init_pair (short pair, short f, short b) => methods.init_pair (pair, f, b);
  237. static public int use_default_colors () => methods.use_default_colors ();
  238. static public int COLOR_PAIRS() => methods.COLOR_PAIRS();
  239. static public uint getmouse (out MouseEvent ev) => methods.getmouse (out ev);
  240. static public uint ungetmouse (ref MouseEvent ev) => methods.ungetmouse (ref ev);
  241. static public int mouseinterval (int interval) => methods.mouseinterval (interval);
  242. }
  243. internal class Delegates {
  244. public delegate IntPtr initscr ();
  245. public delegate int endwin ();
  246. public delegate bool isendwin ();
  247. public delegate int cbreak ();
  248. public delegate int nocbreak ();
  249. public delegate int echo ();
  250. public delegate int noecho ();
  251. public delegate int halfdelay (int t);
  252. public delegate int raw ();
  253. public delegate int noraw ();
  254. public delegate void noqiflush ();
  255. public delegate void qiflush ();
  256. public delegate int typeahead (IntPtr fd);
  257. public delegate int timeout (int delay);
  258. public delegate int wtimeout (IntPtr win, int delay);
  259. public delegate int notimeout (IntPtr win, bool bf);
  260. public delegate int keypad (IntPtr win, bool bf);
  261. public delegate int meta (IntPtr win, bool bf);
  262. public delegate int intrflush (IntPtr win, bool bf);
  263. public delegate int clearok (IntPtr win, bool bf);
  264. public delegate int idlok (IntPtr win, bool bf);
  265. public delegate void idcok (IntPtr win, bool bf);
  266. public delegate void immedok (IntPtr win, bool bf);
  267. public delegate int leaveok (IntPtr win, bool bf);
  268. public delegate int wsetscrreg (IntPtr win, int top, int bot);
  269. public delegate int scrollok (IntPtr win, bool bf);
  270. public delegate int nl ();
  271. public delegate int nonl ();
  272. public delegate int setscrreg (int top, int bot);
  273. public delegate int refresh ();
  274. public delegate int doupdate ();
  275. public delegate int wrefresh (IntPtr win);
  276. public delegate int redrawwin (IntPtr win);
  277. //public delegate int wredrawwin (IntPtr win, int beg_line, int num_lines);
  278. public delegate int wnoutrefresh (IntPtr win);
  279. public delegate int move (int line, int col);
  280. public delegate int addch (int ch);
  281. public delegate int addwstr([MarshalAs(UnmanagedType.LPWStr)]string s);
  282. public delegate int wmove (IntPtr win, int line, int col);
  283. public delegate int waddch (IntPtr win, int ch);
  284. public delegate int attron (int attrs);
  285. public delegate int attroff (int attrs);
  286. public delegate int attrset (int attrs);
  287. public delegate int getch ();
  288. public delegate int get_wch (out int sequence);
  289. public delegate int ungetch (int ch);
  290. public delegate int mvgetch (int y, int x);
  291. public delegate bool has_colors ();
  292. public delegate int start_color ();
  293. public delegate int init_pair (short pair, short f, short b);
  294. public delegate int use_default_colors ();
  295. public delegate int COLOR_PAIRS ();
  296. public delegate uint getmouse (out Curses.MouseEvent ev);
  297. public delegate uint ungetmouse (ref Curses.MouseEvent ev);
  298. public delegate int mouseinterval (int interval);
  299. public delegate IntPtr mousemask (IntPtr newmask, out IntPtr oldMask);
  300. }
  301. internal class NativeMethods {
  302. public readonly Delegates.initscr initscr;
  303. public readonly Delegates.endwin endwin;
  304. public readonly Delegates.isendwin isendwin;
  305. public readonly Delegates.cbreak cbreak;
  306. public readonly Delegates.nocbreak nocbreak;
  307. public readonly Delegates.echo echo;
  308. public readonly Delegates.noecho noecho;
  309. public readonly Delegates.halfdelay halfdelay;
  310. public readonly Delegates.raw raw;
  311. public readonly Delegates.noraw noraw;
  312. public readonly Delegates.noqiflush noqiflush;
  313. public readonly Delegates.qiflush qiflush;
  314. public readonly Delegates.typeahead typeahead;
  315. public readonly Delegates.timeout timeout;
  316. public readonly Delegates.wtimeout wtimeout;
  317. public readonly Delegates.notimeout notimeout;
  318. public readonly Delegates.keypad keypad;
  319. public readonly Delegates.meta meta;
  320. public readonly Delegates.intrflush intrflush;
  321. public readonly Delegates.clearok clearok;
  322. public readonly Delegates.idlok idlok;
  323. public readonly Delegates.idcok idcok;
  324. public readonly Delegates.immedok immedok;
  325. public readonly Delegates.leaveok leaveok;
  326. public readonly Delegates.wsetscrreg wsetscrreg;
  327. public readonly Delegates.scrollok scrollok;
  328. public readonly Delegates.nl nl;
  329. public readonly Delegates.nonl nonl;
  330. public readonly Delegates.setscrreg setscrreg;
  331. public readonly Delegates.refresh refresh;
  332. public readonly Delegates.doupdate doupdate;
  333. public readonly Delegates.wrefresh wrefresh;
  334. public readonly Delegates.redrawwin redrawwin;
  335. //public readonly Delegates.wredrawwin wredrawwin;
  336. public readonly Delegates.wnoutrefresh wnoutrefresh;
  337. public readonly Delegates.move move;
  338. public readonly Delegates.addch addch;
  339. public readonly Delegates.addwstr addwstr;
  340. public readonly Delegates.wmove wmove;
  341. public readonly Delegates.waddch waddch;
  342. public readonly Delegates.attron attron;
  343. public readonly Delegates.attroff attroff;
  344. public readonly Delegates.attrset attrset;
  345. public readonly Delegates.getch getch;
  346. public readonly Delegates.get_wch get_wch;
  347. public readonly Delegates.ungetch ungetch;
  348. public readonly Delegates.mvgetch mvgetch;
  349. public readonly Delegates.has_colors has_colors;
  350. public readonly Delegates.start_color start_color;
  351. public readonly Delegates.init_pair init_pair;
  352. public readonly Delegates.use_default_colors use_default_colors;
  353. public readonly Delegates.COLOR_PAIRS COLOR_PAIRS;
  354. public readonly Delegates.getmouse getmouse;
  355. public readonly Delegates.ungetmouse ungetmouse;
  356. public readonly Delegates.mouseinterval mouseinterval;
  357. public readonly Delegates.mousemask mousemask;
  358. public UnmanagedLibrary UnmanagedLibrary;
  359. public NativeMethods (UnmanagedLibrary lib)
  360. {
  361. this.UnmanagedLibrary = lib;
  362. initscr = lib.GetNativeMethodDelegate<Delegates.initscr> ("initscr");
  363. endwin = lib.GetNativeMethodDelegate<Delegates.endwin> ("endwin");
  364. isendwin = lib.GetNativeMethodDelegate<Delegates.isendwin> ("isendwin");
  365. cbreak = lib.GetNativeMethodDelegate<Delegates.cbreak> ("cbreak");
  366. nocbreak = lib.GetNativeMethodDelegate<Delegates.nocbreak> ("nocbreak");
  367. echo = lib.GetNativeMethodDelegate<Delegates.echo> ("echo");
  368. noecho = lib.GetNativeMethodDelegate<Delegates.noecho> ("noecho");
  369. halfdelay = lib.GetNativeMethodDelegate<Delegates.halfdelay> ("halfdelay");
  370. raw = lib.GetNativeMethodDelegate<Delegates.raw> ("raw");
  371. noraw = lib.GetNativeMethodDelegate<Delegates.noraw> ("noraw");
  372. noqiflush = lib.GetNativeMethodDelegate<Delegates.noqiflush> ("noqiflush");
  373. qiflush = lib.GetNativeMethodDelegate<Delegates.qiflush> ("qiflush");
  374. typeahead = lib.GetNativeMethodDelegate<Delegates.typeahead> ("typeahead");
  375. timeout = lib.GetNativeMethodDelegate<Delegates.timeout> ("timeout");
  376. wtimeout = lib.GetNativeMethodDelegate<Delegates.wtimeout> ("wtimeout");
  377. notimeout = lib.GetNativeMethodDelegate<Delegates.notimeout> ("notimeout");
  378. keypad = lib.GetNativeMethodDelegate<Delegates.keypad> ("keypad");
  379. meta = lib.GetNativeMethodDelegate<Delegates.meta> ("meta");
  380. intrflush = lib.GetNativeMethodDelegate<Delegates.intrflush> ("intrflush");
  381. clearok = lib.GetNativeMethodDelegate<Delegates.clearok> ("clearok");
  382. idlok = lib.GetNativeMethodDelegate<Delegates.idlok> ("idlok");
  383. idcok = lib.GetNativeMethodDelegate<Delegates.idcok> ("idcok");
  384. immedok = lib.GetNativeMethodDelegate<Delegates.immedok> ("immedok");
  385. leaveok = lib.GetNativeMethodDelegate<Delegates.leaveok> ("leaveok");
  386. wsetscrreg = lib.GetNativeMethodDelegate<Delegates.wsetscrreg> ("wsetscrreg");
  387. scrollok = lib.GetNativeMethodDelegate<Delegates.scrollok> ("scrollok");
  388. nl = lib.GetNativeMethodDelegate<Delegates.nl> ("nl");
  389. nonl = lib.GetNativeMethodDelegate<Delegates.nonl> ("nonl");
  390. setscrreg = lib.GetNativeMethodDelegate<Delegates.setscrreg> ("setscrreg");
  391. refresh = lib.GetNativeMethodDelegate<Delegates.refresh> ("refresh");
  392. doupdate = lib.GetNativeMethodDelegate<Delegates.doupdate> ("doupdate");
  393. wrefresh = lib.GetNativeMethodDelegate<Delegates.wrefresh> ("wrefresh");
  394. redrawwin = lib.GetNativeMethodDelegate<Delegates.redrawwin> ("redrawwin");
  395. //wredrawwin = lib.GetNativeMethodDelegate<Delegates.wredrawwin> ("wredrawwin");
  396. wnoutrefresh = lib.GetNativeMethodDelegate<Delegates.wnoutrefresh> ("wnoutrefresh");
  397. move = lib.GetNativeMethodDelegate<Delegates.move> ("move");
  398. addch = lib.GetNativeMethodDelegate<Delegates.addch>("addch");
  399. addwstr = lib.GetNativeMethodDelegate<Delegates.addwstr> ("addwstr");
  400. wmove = lib.GetNativeMethodDelegate<Delegates.wmove> ("wmove");
  401. waddch = lib.GetNativeMethodDelegate<Delegates.waddch> ("waddch");
  402. attron = lib.GetNativeMethodDelegate<Delegates.attron> ("attron");
  403. attroff = lib.GetNativeMethodDelegate<Delegates.attroff> ("attroff");
  404. attrset = lib.GetNativeMethodDelegate<Delegates.attrset> ("attrset");
  405. getch = lib.GetNativeMethodDelegate<Delegates.getch> ("getch");
  406. get_wch = lib.GetNativeMethodDelegate<Delegates.get_wch> ("get_wch");
  407. ungetch = lib.GetNativeMethodDelegate<Delegates.ungetch> ("ungetch");
  408. mvgetch = lib.GetNativeMethodDelegate<Delegates.mvgetch> ("mvgetch");
  409. has_colors = lib.GetNativeMethodDelegate<Delegates.has_colors> ("has_colors");
  410. start_color = lib.GetNativeMethodDelegate<Delegates.start_color> ("start_color");
  411. init_pair = lib.GetNativeMethodDelegate<Delegates.init_pair> ("init_pair");
  412. use_default_colors = lib.GetNativeMethodDelegate<Delegates.use_default_colors> ("use_default_colors");
  413. COLOR_PAIRS = lib.GetNativeMethodDelegate<Delegates.COLOR_PAIRS> ("COLOR_PAIRS");
  414. getmouse = lib.GetNativeMethodDelegate<Delegates.getmouse> ("getmouse");
  415. ungetmouse = lib.GetNativeMethodDelegate<Delegates.ungetmouse> ("ungetmouse");
  416. mouseinterval = lib.GetNativeMethodDelegate<Delegates.mouseinterval> ("mouseinterval");
  417. mousemask = lib.GetNativeMethodDelegate<Delegates.mousemask> ("mousemask");
  418. }
  419. }
  420. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  421. }