binding.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // binding.cs.in: Core binding for curses.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Copyright (C) 2007 Novell (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. namespace Unix.Terminal {
  32. internal partial class Curses {
  33. [StructLayout (LayoutKind.Sequential)]
  34. internal struct MouseEvent {
  35. public short ID;
  36. public int X, Y, Z;
  37. public Event ButtonState;
  38. }
  39. #region Screen initialization
  40. [DllImport ("ncurses", EntryPoint="initscr")]
  41. extern static internal IntPtr real_initscr ();
  42. static int lines, cols;
  43. static Window main_window;
  44. static IntPtr curses_handle, curscr_ptr, lines_ptr, cols_ptr;
  45. static void FindNCurses ()
  46. {
  47. if (File.Exists ("/usr/lib/libncurses.dylib"))
  48. curses_handle = dlopen ("libncurses.dylib", 1);
  49. else
  50. curses_handle = dlopen ("libncurses.so", 1);
  51. if (curses_handle == IntPtr.Zero)
  52. throw new Exception ("Could not dlopen ncurses");
  53. stdscr = read_static_ptr ("stdscr");
  54. curscr_ptr = get_ptr ("curscr");
  55. lines_ptr = get_ptr ("LINES");
  56. cols_ptr = get_ptr ("COLS");
  57. }
  58. static public Window initscr ()
  59. {
  60. FindNCurses ();
  61. main_window = new Window (real_initscr ());
  62. try {
  63. console_sharp_get_dims (out lines, out cols);
  64. } catch (DllNotFoundException){
  65. endwin ();
  66. Console.Error.WriteLine ("Unable to find the @MONO_CURSES@ native library\n" +
  67. "this is different than the managed mono-curses.dll\n\n" +
  68. "Typically you need to install to a LD_LIBRARY_PATH directory\n" +
  69. "or DYLD_LIBRARY_PATH directory or run /sbin/ldconfig");
  70. Environment.Exit (1);
  71. }
  72. return main_window;
  73. }
  74. public static int Lines {
  75. get {
  76. return lines;
  77. }
  78. }
  79. public static int Cols {
  80. get {
  81. return cols;
  82. }
  83. }
  84. //
  85. // Returns true if the window changed since the last invocation, as a
  86. // side effect, the Lines and Cols properties are updated
  87. //
  88. public static bool CheckWinChange ()
  89. {
  90. int l, c;
  91. console_sharp_get_dims (out l, out c);
  92. if (l != lines || c != cols){
  93. lines = l;
  94. cols = c;
  95. return true;
  96. }
  97. return false;
  98. }
  99. [DllImport ("ncurses")]
  100. extern static public int endwin ();
  101. [DllImport ("ncurses")]
  102. extern static public bool isendwin ();
  103. //
  104. // Screen operations are flagged as internal, as we need to
  105. // catch all changes so we can update newscr, curscr, stdscr
  106. //
  107. [DllImport ("ncurses")]
  108. extern static public IntPtr internal_newterm (string type, IntPtr file_outfd, IntPtr file_infd);
  109. [DllImport ("ncurses")]
  110. extern static public IntPtr internal_set_term (IntPtr newscreen);
  111. [DllImport ("ncurses")]
  112. extern static internal void internal_delscreen (IntPtr sp);
  113. #endregion
  114. #region Input Options
  115. [DllImport ("ncurses")]
  116. extern static public int cbreak ();
  117. [DllImport ("ncurses")]
  118. extern static public int nocbreak ();
  119. [DllImport ("ncurses")]
  120. extern static public int echo ();
  121. [DllImport ("ncurses")]
  122. extern static public int noecho ();
  123. [DllImport ("ncurses")]
  124. extern static public int halfdelay (int t);
  125. [DllImport ("ncurses")]
  126. extern static public int raw ();
  127. [DllImport ("ncurses")]
  128. extern static public int noraw ();
  129. [DllImport ("ncurses")]
  130. extern static public void noqiflush ();
  131. [DllImport ("ncurses")]
  132. extern static public void qiflush ();
  133. [DllImport ("ncurses")]
  134. extern static public int typeahead (IntPtr fd);
  135. [DllImport ("ncurses")]
  136. extern static public int timeout (int delay);
  137. //
  138. // Internal, as they are exposed in Window
  139. //
  140. [DllImport ("ncurses")]
  141. extern static internal int wtimeout (IntPtr win, int delay);
  142. [DllImport ("ncurses")]
  143. extern static internal int notimeout (IntPtr win, bool bf);
  144. [DllImport ("ncurses")]
  145. extern static internal int keypad (IntPtr win, bool bf);
  146. [DllImport ("ncurses")]
  147. extern static internal int meta (IntPtr win, bool bf);
  148. [DllImport ("ncurses")]
  149. extern static internal int intrflush (IntPtr win, bool bf);
  150. #endregion
  151. #region Output Options
  152. [DllImport ("ncurses")]
  153. extern internal static int clearok (IntPtr win, bool bf);
  154. [DllImport ("ncurses")]
  155. extern internal static int idlok (IntPtr win, bool bf);
  156. [DllImport ("ncurses")]
  157. extern internal static void idcok (IntPtr win, bool bf);
  158. [DllImport ("ncurses")]
  159. extern internal static void immedok (IntPtr win, bool bf);
  160. [DllImport ("ncurses")]
  161. extern internal static int leaveok (IntPtr win, bool bf);
  162. [DllImport ("ncurses")]
  163. extern internal static int wsetscrreg (IntPtr win, int top, int bot);
  164. [DllImport ("ncurses")]
  165. extern internal static int scrollok (IntPtr win, bool bf);
  166. [DllImport ("ncurses")]
  167. extern public static int nl();
  168. [DllImport ("ncurses")]
  169. extern public static int nonl();
  170. [DllImport ("ncurses")]
  171. extern public static int setscrreg (int top, int bot);
  172. #endregion
  173. #region refresh functions
  174. [DllImport ("ncurses")]
  175. extern public static int refresh ();
  176. [DllImport ("ncurses")]
  177. extern public static int doupdate();
  178. [DllImport ("ncurses")]
  179. extern internal static int wrefresh (IntPtr win);
  180. [DllImport ("ncurses")]
  181. extern internal static int redrawwin (IntPtr win);
  182. [DllImport ("ncurses")]
  183. extern internal static int wredrawwin (IntPtr win, int beg_line, int num_lines);
  184. [DllImport ("ncurses")]
  185. extern internal static int wnoutrefresh (IntPtr win);
  186. #endregion
  187. #region Output
  188. [DllImport ("ncurses")]
  189. extern public static int move (int line, int col);
  190. [DllImport ("ncurses", EntryPoint="addch")]
  191. extern internal static int _addch (int ch);
  192. [DllImport ("ncurses")]
  193. extern public static int addstr (string s);
  194. public static int addstr (string format, params object [] args)
  195. {
  196. var s = string.Format (format, args);
  197. return addstr (s);
  198. }
  199. static char [] r = new char [1];
  200. //
  201. // Have to wrap the native addch, as it can not
  202. // display unicode characters, we have to use addstr
  203. // for that. but we need addch to render special ACS
  204. // characters
  205. //
  206. public static int addch (int ch)
  207. {
  208. if (ch < 127 || ch > 0xffff )
  209. return _addch (ch);
  210. char c = (char) ch;
  211. return addstr (new String (c, 1));
  212. }
  213. [DllImport ("ncurses")]
  214. extern internal static int wmove (IntPtr win, int line, int col);
  215. [DllImport ("ncurses")]
  216. extern internal static int waddch (IntPtr win, int ch);
  217. #endregion
  218. #region Attributes
  219. [DllImport ("ncurses")]
  220. extern public static int attron (int attrs);
  221. [DllImport ("ncurses")]
  222. extern public static int attroff (int attrs);
  223. [DllImport ("ncurses")]
  224. extern public static int attrset (int attrs);
  225. #endregion
  226. #region Input
  227. [DllImport ("ncurses")]
  228. extern public static int getch ();
  229. [DllImport ("ncurses")]
  230. extern public static int get_wch (out int sequence);
  231. [DllImport ("ncurses")]
  232. extern public static int ungetch (int ch);
  233. [DllImport ("ncurses")]
  234. extern public static int mvgetch (int y, int x);
  235. #endregion
  236. #region Colors
  237. [DllImport ("ncurses")]
  238. extern internal static bool has_colors ();
  239. public static bool HasColors => has_colors ();
  240. [DllImport ("ncurses")]
  241. extern internal static int start_color ();
  242. public static int StartColor () => start_color ();
  243. [DllImport ("ncurses")]
  244. extern internal static int init_pair (short pair, short f, short b);
  245. public static int InitColorPair (short pair, short foreground, short background) => init_pair (pair, foreground, background);
  246. [DllImport ("ncurses")]
  247. extern internal static int use_default_colors ();
  248. public static int UseDefaultColors () => use_default_colors ();
  249. [DllImport ("ncurses")]
  250. extern internal static int COLOR_PAIRS();
  251. public static int ColorPairs => COLOR_PAIRS();
  252. #endregion
  253. [DllImport ("dl")]
  254. extern static IntPtr dlopen (string file, int mode);
  255. [DllImport ("dl")]
  256. extern static IntPtr dlsym (IntPtr handle, string symbol);
  257. static IntPtr stdscr;
  258. static IntPtr get_ptr (string key)
  259. {
  260. var ptr = dlsym (curses_handle, key);
  261. if (ptr == IntPtr.Zero)
  262. throw new Exception ("Could not load the key " + key);
  263. return ptr;
  264. }
  265. internal static IntPtr read_static_ptr (string key)
  266. {
  267. var ptr = get_ptr (key);
  268. return Marshal.ReadIntPtr (ptr);
  269. }
  270. internal static IntPtr console_sharp_get_stdscr () => stdscr;
  271. #region Helpers
  272. internal static IntPtr console_sharp_get_curscr ()
  273. {
  274. return Marshal.ReadIntPtr (curscr_ptr);
  275. }
  276. internal static void console_sharp_get_dims (out int lines, out int cols)
  277. {
  278. lines = Marshal.ReadInt32 (lines_ptr);
  279. cols = Marshal.ReadInt32 (cols_ptr);
  280. }
  281. [DllImport ("ncurses", EntryPoint="mousemask")]
  282. extern static IntPtr call_mousemask (IntPtr newmask, out IntPtr oldmask);
  283. public static Event mousemask (Event newmask, out Event oldmask)
  284. {
  285. IntPtr e;
  286. var ret = (Event) call_mousemask ((IntPtr) newmask, out e);
  287. oldmask = (Event) e;
  288. return ret;
  289. }
  290. [DllImport ("ncurses")]
  291. public extern static uint getmouse (out MouseEvent ev);
  292. [DllImport ("ncurses")]
  293. public extern static uint ungetmouse (ref MouseEvent ev);
  294. [DllImport ("ncurses")]
  295. public extern static int mouseinterval (int interval);
  296. #endregion
  297. // We encode ESC + char (what Alt-char generates) as 0x2000 + char
  298. public const int KeyAlt = 0x2000;
  299. static public int IsAlt (int key)
  300. {
  301. if ((key & KeyAlt) != 0)
  302. return key & ~KeyAlt;
  303. return 0;
  304. }
  305. }
  306. }