From 4a17f5cd5d28b57cf1eb71302ce60afed434baa6 Mon Sep 17 00:00:00 2001 From: miguel Date: Tue, 18 Sep 2018 22:56:02 -0400 Subject: [PATCH] Use nativemethods --- Terminal.Gui/MonoCurses/binding.cs | 196 +++++++++++++++++++---------- 1 file changed, 130 insertions(+), 66 deletions(-) diff --git a/Terminal.Gui/MonoCurses/binding.cs b/Terminal.Gui/MonoCurses/binding.cs index 7c4c4a7f2..3791a2bd1 100644 --- a/Terminal.Gui/MonoCurses/binding.cs +++ b/Terminal.Gui/MonoCurses/binding.cs @@ -1,4 +1,10 @@ - +// +// TODO: +// * FindNCurses needs to remove the old probing code +// * Removal of that proxy code +// * Need to implement reading pointers with the new API +// * Can remove the manual Dlopen features +// * initscr() diagnostics based on DLL can be fixed // // binding.cs.in: Core binding for curses. // @@ -154,6 +160,65 @@ namespace Unix.Terminal { public readonly Delegates.getmouse getmouse; public readonly Delegates.ungetmouse ungetmouse; public readonly Delegates.mouseinterval mouseinterval; + + public void NativeMethods (UnmanagedLibrary lib) + { + initscr = lib.GetMethodDelegate ("initscr"); + endwin = lib.GetMethodDelegate ("endwin"); + isendwin = lib.GetMethodDelegate ("isendwin"); + cbreak = lib.GetMethodDelegate ("cbreak"); + nocbreak = lib.GetMethodDelegate ("nocbreak"); + echo = lib.GetMethodDelegate ("echo"); + noecho = lib.GetMethodDelegate ("noecho"); + halfdelay = lib.GetMethodDelegate ("halfdelay"); + raw = lib.GetMethodDelegate ("raw"); + noraw = lib.GetMethodDelegate ("noraw"); + noqiflush = lib.GetMethodDelegate ("noqiflush"); + qiflush = lib.GetMethodDelegate ("qiflush"); + typeahead = lib.GetMethodDelegate ("typeahead"); + timeout = lib.GetMethodDelegate ("timeout"); + wtimeout = lib.GetMethodDelegate ("wtimeout"); + notimeout = lib.GetMethodDelegate ("notimeout"); + keypad = lib.GetMethodDelegate ("keypad"); + meta = lib.GetMethodDelegate ("meta"); + intrflush = lib.GetMethodDelegate ("intrflush"); + clearok = lib.GetMethodDelegate ("clearok"); + idlok = lib.GetMethodDelegate ("idlok"); + idcok = lib.GetMethodDelegate ("idcok"); + immedok = lib.GetMethodDelegate ("immedok"); + leaveok = lib.GetMethodDelegate ("leaveok"); + wsetscrreg = lib.GetMethodDelegate ("wsetscrreg"); + scrollok = lib.GetMethodDelegate ("scrollok"); + nl = lib.GetMethodDelegate ("nl"); + nonl = lib.GetMethodDelegate ("nonl"); + setscrreg = lib.GetMethodDelegate ("setscrreg"); + refresh = lib.GetMethodDelegate ("refresh"); + doupdate = lib.GetMethodDelegate ("doupdate"); + wrefresh = lib.GetMethodDelegate ("wrefresh"); + redrawwin = lib.GetMethodDelegate ("redrawwin"); + wredrawwin = lib.GetMethodDelegate ("wredrawwin"); + wnoutrefresh = lib.GetMethodDelegate ("wnoutrefresh"); + move = lib.GetMethodDelegate ("move"); + addch = lib.GetMethodDelegate ("addch"); + addstr = lib.GetMethodDelegate ("addstr"); + wmove = lib.GetMethodDelegate ("wmove"); + waddch = lib.GetMethodDelegate ("waddch"); + attron = lib.GetMethodDelegate ("attron"); + attroff = lib.GetMethodDelegate ("attroff"); + attrset = lib.GetMethodDelegate ("attrset"); + getch = lib.GetMethodDelegate ("getch"); + get_wch = lib.GetMethodDelegate ("get_wch"); + ungetch = lib.GetMethodDelegate ("ungetch"); + mvgetch = lib.GetMethodDelegate ("mvgetch"); + has_colors = lib.GetMethodDelegate ("has_colors"); + start_color = lib.GetMethodDelegate ("start_color"); + init_pair = lib.GetMethodDelegate ("init_pair"); + use_default_colors = lib.GetMethodDelegate ("use_default_colors"); + COLOR_PAIR = lib.GetMethodDelegate ("COLOR_PAIR"); + getmouse = lib.GetMethodDelegate ("getmouse"); + ungetmouse = lib.GetMethodDelegate ("ungetmouse"); + mouseinterval = lib.GetMethodDelegate ("mouseinterval"); + } } internal partial class Curses { @@ -171,10 +236,13 @@ namespace Unix.Terminal { // If true, uses the DllImport into "ncurses", otherwise "libncursesw.so.5" static bool use_naked_driver; + NativeMethods methods; + static void LoadMethods () { var libs = UnmanagedLibrary.IsMacOSPlatform ? new string [] { "libncurses.dylib" } : new string { "libncursesw.so.6", "libncursesw.so.5" }; var lib = new UnmanagedLibrary (libs); + methods = new NativeMethods (lib); } // @@ -220,9 +288,9 @@ namespace Unix.Terminal { { FindNCurses (); - main_window = new Window (real_initscr ()); + main_window = new Window (methods.initscr ()); try { - console_sharp_get_dims (out lines, out cols); + console_sharp_get_dims (out lines, out bcols); } catch (DllNotFoundException){ endwin (); Console.Error.WriteLine ("Unable to find the @MONO_CURSES@ native library\n" + @@ -333,7 +401,7 @@ namespace Unix.Terminal { public static Event mousemask (Event newmask, out Event oldmask) { IntPtr e; - var ret = (Event) (use_naked_driver ? RegularCurses.call_mousemask ((IntPtr) newmask, out e) : CursesLinux.call_mousemask ((IntPtr) newmask, out e)); + var ret = (Event) (methods.call_mousemask ((IntPtr) newmask, out e)); oldmask = (Event) e; return ret; } @@ -348,74 +416,70 @@ namespace Unix.Terminal { return key & ~KeyAlt; return 0; } - public static int StartColor () => start_color (); - public static bool HasColors => has_colors (); - public static int InitColorPair (short pair, short foreground, short background) => init_pair (pair, foreground, background); - public static int UseDefaultColors () => use_default_colors (); - public static int ColorPairs => COLOR_PAIRS(); - + public static int StartColor () => methods.start_color (); + public static bool HasColors => methods.has_colors (); + public static int InitColorPair (short pair, short foreground, short background) => methods.init_pair (pair, foreground, background); + public static int UseDefaultColors () => methods.use_default_colors (); + public static int ColorPairs => methods.COLOR_PAIRS(); // // The proxy methods to call into each version // - static public IntPtr real_initscr () => use_naked_driver ? RegularCurses.real_initscr () : CursesLinux.real_initscr (); - static public int endwin () => use_naked_driver ? RegularCurses.endwin () : CursesLinux.endwin (); - static public bool isendwin () => use_naked_driver ? RegularCurses.isendwin () : CursesLinux.isendwin (); - static public IntPtr internal_newterm (string type, IntPtr file_outfd, IntPtr file_infd) => use_naked_driver ? RegularCurses.internal_newterm (type, file_outfd, file_infd) : CursesLinux.internal_newterm (type, file_outfd, file_infd); - static public IntPtr internal_set_term (IntPtr newscreen) => use_naked_driver ? RegularCurses.internal_set_term (newscreen) : CursesLinux.internal_set_term (newscreen); - static public void internal_delscreen (IntPtr sp) { if (use_naked_driver) RegularCurses.internal_delscreen (sp); else CursesLinux.internal_delscreen (sp); } - static public int cbreak () => use_naked_driver ? RegularCurses.cbreak () : CursesLinux.cbreak (); - static public int nocbreak () => use_naked_driver ? RegularCurses.nocbreak () : CursesLinux.nocbreak (); - static public int echo () => use_naked_driver ? RegularCurses.echo () : CursesLinux.echo (); - static public int noecho () => use_naked_driver ? RegularCurses.noecho () : CursesLinux.noecho (); - static public int halfdelay (int t) => use_naked_driver ? RegularCurses.halfdelay (t) : CursesLinux.halfdelay (t); - static public int raw () => use_naked_driver ? RegularCurses.raw () : CursesLinux.raw (); - static public int noraw () => use_naked_driver ? RegularCurses.noraw () : CursesLinux.noraw (); - static public void noqiflush () { if (use_naked_driver) RegularCurses.noqiflush (); else CursesLinux.noqiflush (); } - static public void qiflush () { if (use_naked_driver) RegularCurses.qiflush (); else CursesLinux.qiflush (); } - static public int typeahead (IntPtr fd) => use_naked_driver ? RegularCurses.typeahead (fd) : CursesLinux.typeahead (fd); - static public int timeout (int delay) => use_naked_driver ? RegularCurses.timeout (delay) : CursesLinux.timeout (delay); - static public int wtimeout (IntPtr win, int delay) => use_naked_driver ? RegularCurses.wtimeout (win, delay) : CursesLinux.wtimeout (win, delay); - static public int notimeout (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.notimeout (win, bf) : CursesLinux.notimeout (win, bf); - static public int keypad (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.keypad (win, bf) : CursesLinux.keypad (win, bf); - static public int meta (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.meta (win, bf) : CursesLinux.meta (win, bf); - static public int intrflush (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.intrflush (win, bf) : CursesLinux.intrflush (win, bf); - static public int clearok (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.clearok (win, bf) : CursesLinux.clearok (win, bf); - static public int idlok (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.idlok (win, bf) : CursesLinux.idlok (win, bf); + static public IntPtr real_initscr () => methods.real_initscr (); + static public int endwin () => methods.endwin (); + static public bool isendwin () => methods.isendwin (); + static public int cbreak () => methods.cbreak (); + static public int nocbreak () => methods.nocbreak (); + static public int echo () => methods.echo (); + static public int noecho () => methods.noecho (); + static public int halfdelay (int t) => methods.halfdelay (t); + static public int raw () => methods.raw (); + static public int noraw () => methods.noraw (); + static public void noqiflush () => { methods.noqiflush (); }; + static public void qiflush () => { methods.qiflush (); }; + static public int typeahead (IntPtr fd) => methods.typeahead (fd); + static public int timeout (int delay) => methods.timeout (delay); + static public int wtimeout (IntPtr win, int delay) => methods.wtimeout (win, delay); + static public int notimeout (IntPtr win, bool bf) => methods.notimeout (win, bf); + static public int keypad (IntPtr win, bool bf) => methods.keypad (win, bf); + static public int meta (IntPtr win, bool bf) => methods.meta (win, bf); + static public int intrflush (IntPtr win, bool bf) => methods.intrflush (win, bf); + static public int clearok (IntPtr win, bool bf) => methods.clearok (win, bf); + static public int idlok (IntPtr win, bool bf) => methods.idlok (win, bf); static public void idcok (IntPtr win, bool bf) { if (use_naked_driver) RegularCurses.idcok (win, bf); else CursesLinux.idcok (win, bf);} static public void immedok (IntPtr win, bool bf) { if (use_naked_driver) RegularCurses.immedok (win, bf); else CursesLinux.immedok (win, bf);} - static public int leaveok (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.leaveok (win, bf) : CursesLinux.leaveok (win, bf); - static public int wsetscrreg (IntPtr win, int top, int bot) => use_naked_driver ? RegularCurses.wsetscrreg (win, top, bot) : CursesLinux.wsetscrreg (win, top, bot); - static public int scrollok (IntPtr win, bool bf) => use_naked_driver ? RegularCurses.scrollok (win, bf) : CursesLinux.scrollok (win, bf); - static public int nl() => use_naked_driver ? RegularCurses.nl() : CursesLinux.nl(); - static public int nonl() => use_naked_driver ? RegularCurses.nonl() : CursesLinux.nonl(); - static public int setscrreg (int top, int bot) => use_naked_driver ? RegularCurses.setscrreg (top, bot) : CursesLinux.setscrreg (top, bot); - static public int refresh () => use_naked_driver ? RegularCurses.refresh () : CursesLinux.refresh (); - static public int doupdate() => use_naked_driver ? RegularCurses.doupdate() : CursesLinux.doupdate(); - static public int wrefresh (IntPtr win) => use_naked_driver ? RegularCurses.wrefresh (win) : CursesLinux.wrefresh (win); - static public int redrawwin (IntPtr win) => use_naked_driver ? RegularCurses.redrawwin (win) : CursesLinux.redrawwin (win); - static public int wredrawwin (IntPtr win, int beg_line, int num_lines) => use_naked_driver ? RegularCurses.wredrawwin (win, beg_line, num_lines) : CursesLinux.wredrawwin (win, beg_line, lines); - static public int wnoutrefresh (IntPtr win) => use_naked_driver ? RegularCurses.wnoutrefresh (win) : CursesLinux.wnoutrefresh (win); - static public int move (int line, int col) => use_naked_driver ? RegularCurses.move (line, col) : CursesLinux.move (line, col); - static public int _addch (int ch) => use_naked_driver ? RegularCurses._addch (ch) : CursesLinux._addch (ch); - static public int addstr (string s) => use_naked_driver ? RegularCurses.addstr (s) : CursesLinux.addstr (s); - static public int wmove (IntPtr win, int line, int col) => use_naked_driver ? RegularCurses.wmove (win, line, col) : CursesLinux.wmove (win, line, col); - static public int waddch (IntPtr win, int ch) => use_naked_driver ? RegularCurses.waddch (win, ch) : CursesLinux.waddch (win, ch); - static public int attron (int attrs) => use_naked_driver ? RegularCurses.attron (attrs) : CursesLinux.attron (attrs); - static public int attroff (int attrs) => use_naked_driver ? RegularCurses.attroff (attrs) : CursesLinux.attroff (attrs); - static public int attrset (int attrs) => use_naked_driver ? RegularCurses.attrset (attrs) : CursesLinux.attrset (attrs); - static public int getch () => use_naked_driver ? RegularCurses.getch () : CursesLinux.getch (); - static public int get_wch (out int sequence) => use_naked_driver ? RegularCurses.get_wch (out sequence) : CursesLinux.get_wch (out sequence); - static public int ungetch (int ch) => use_naked_driver ? RegularCurses.ungetch (ch) : CursesLinux.ungetch (ch); - static public int mvgetch (int y, int x) => use_naked_driver ? RegularCurses.mvgetch (y, x) : CursesLinux.mvgetch (y, x); - static public bool has_colors () => use_naked_driver ? RegularCurses.has_colors () : CursesLinux.has_colors (); - static public int start_color () => use_naked_driver ? RegularCurses.start_color () : CursesLinux.start_color (); - static public int init_pair (short pair, short f, short b) => use_naked_driver ? RegularCurses.init_pair (pair, f, b) : CursesLinux.init_pair (pair, f, b); - static public int use_default_colors () => use_naked_driver ? RegularCurses.use_default_colors () : CursesLinux.use_default_colors (); - static public int COLOR_PAIRS() => use_naked_driver ? RegularCurses.COLOR_PAIRS() : CursesLinux.COLOR_PAIRS(); - static public uint getmouse (out MouseEvent ev) => use_naked_driver ? RegularCurses.getmouse (out ev) : CursesLinux.getmouse (out ev); - static public uint ungetmouse (ref MouseEvent ev) => use_naked_driver ? RegularCurses.ungetmouse (ref ev) : CursesLinux.ungetmouse (ref ev); - static public int mouseinterval (int interval) => use_naked_driver ? RegularCurses.mouseinterval (interval) : CursesLinux.mouseinterval (interval); + static public int leaveok (IntPtr win, bool bf) => methods.leaveok (win, bf); + static public int wsetscrreg (IntPtr win, int top, int bot) => methods.wsetscrreg (win, top, bot); + static public int scrollok (IntPtr win, bool bf) => methods.scrollok (win, bf); + static public int nl() => methods.nl(); + static public int nonl() => methods.nonl(); + static public int setscrreg (int top, int bot) => methods.setscrreg (top, bot); + static public int refresh () => methods.refresh (); + static public int doupdate() => methods.doupdate(); + static public int wrefresh (IntPtr win) => methods.wrefresh (win); + static public int redrawwin (IntPtr win) => methods.redrawwin (win); + static public int wredrawwin (IntPtr win, int beg_line, int num_lines) => methods.wredrawwin (win, beg_line, num_lines); + static public int wnoutrefresh (IntPtr win) => methods.wnoutrefresh (win); + static public int move (int line, int col) => methods.move (line, col); + static public int _addch (int ch) => methods._addch (ch); + static public int addstr (string s) => methods.addstr (s); + static public int wmove (IntPtr win, int line, int col) => methods.wmove (win, line, col); + static public int waddch (IntPtr win, int ch) => methods.waddch (win, ch); + static public int attron (int attrs) => methods.attron (attrs); + static public int attroff (int attrs) => methods.attroff (attrs); + static public int attrset (int attrs) => methods.attrset (attrs); + static public int getch () => methods.getch (); + static public int get_wch (out int sequence) => methods.get_wch (out sequence); + static public int ungetch (int ch) => methods.ungetch (ch); + static public int mvgetch (int y, int x) => methods.mvgetch (y, x); + static public bool has_colors () => methods.has_colors (); + static public int start_color () => methods.start_color (); + static public int init_pair (short pair, short f, short b) => methods.init_pair (pair, f, b); + static public int use_default_colors () => methods.use_default_colors (); + static public int COLOR_PAIRS() => methods.COLOR_PAIRS(); + static public uint getmouse (out MouseEvent ev) => methods.getmouse (out ev); + static public uint ungetmouse (ref MouseEvent ev) => methods.ungetmouse (ref ev); + static public int mouseinterval (int interval) => methods.mouseinterval (interval); } //