]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/curses.priv.h
ncurses 5.7 - patch 20090815
[ncurses.git] / ncurses / curses.priv.h
index a1ea1d49c86e0c3e927606db8324608661fb5872..caf71161f2dc395d26e6bea145a97e3499077562 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Juergen Pfeifer                                                 *
  ****************************************************************************/
 
 
 /*
- * $Id: curses.priv.h,v 1.348 2007/11/03 20:24:15 tom Exp $
+ * $Id: curses.priv.h,v 1.430 2009/07/04 20:40:42 tom Exp $
  *
  *     curses.priv.h
  *
@@ -97,8 +98,6 @@ extern "C" {
 extern int errno;
 #endif
 
-#include <nc_panel.h>
-
 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
 #if HAVE_WORKING_POLL
 #define USE_FUNC_POLL 1
@@ -147,7 +146,7 @@ extern int errno;
 #define HAVE_SIZECHANGE 0
 #endif
 
-#if HAVE_SIZECHANGE && defined(SIGWINCH)
+#if HAVE_SIZECHANGE && USE_SIGWINCH && defined(SIGWINCH)
 #define USE_SIZECHANGE 1
 #else
 #define USE_SIZECHANGE 0
@@ -194,6 +193,16 @@ extern NCURSES_EXPORT(void *) _nc_memmove (void *, const void *, size_t);
 #define if_USE_SCROLL_HINTS(stmt) /*nothing*/
 #endif
 
+/*
+ * Options for terminal drivers, etc...
+ */
+#if 0
+#define USE_SP_RIPOFF     1
+#define USE_SP_TERMTYPE   1
+#define USE_SP_WINDOWLIST 1
+#define USE_TERM_DRIVER   1
+#endif
+
 /*
  * Note:  ht/cbt expansion flakes out randomly under Linux 1.1.47, but only
  * when we're throwing control codes at the screen at high volume.  To see
@@ -257,8 +266,59 @@ color_t;
 #define NCURSES_OPAQUE 0
 
 #include <curses.h>    /* we'll use -Ipath directive to get the right one! */
+
+/*
+ * If curses.h did not expose the SCREEN-functions, then we do not need the
+ * parameter in the corresponding unextended functions.
+ */
+#if NCURSES_SP_FUNCS
+#define SP_PARM         sp     /* use parameter */
+#define NCURSES_SP_ARG          SP_PARM
+#define NCURSES_SP_DCL  SCREEN *NCURSES_SP_ARG
+#define NCURSES_SP_DCL0 NCURSES_SP_DCL
+#define NCURSES_SP_ARGx         NCURSES_SP_ARG,
+#define NCURSES_SP_DCLx SCREEN *NCURSES_SP_ARGx
+#else
+#define SP_PARM         SP     /* use global variable */
+#define NCURSES_SP_ARG
+#define NCURSES_SP_DCL
+#define NCURSES_SP_DCL0 void
+#define NCURSES_SP_ARGx
+#define NCURSES_SP_DCLx
+#endif
+
+#include <nc_panel.h>
+
+#define IsPreScreen(sp)      (((sp) != 0) && sp->_prescreen)
+#define HasTerminal(sp)      (((sp) != 0) && (0 != ((sp)->_term)))
+#define IsValidScreen(sp)    (HasTerminal(sp) && !IsPreScreen(sp))
+
+#if BROKEN_LINKER || USE_REENTRANT
+#define CurTerm              _nc_prescreen._cur_term
+#else
+#define CurTerm              cur_term
+#endif
+
+#if NCURSES_SP_FUNCS
+#define TerminalOf(sp)       ((sp) ? ((sp)->_term ? (sp)->_term : CurTerm) : CurTerm)
+#else
+#define TerminalOf(sp)       CurTerm
+#endif
+
 #include <term.h>
+
+/*
+ * Reduce dependency on cur_term global by using terminfo data from SCREEN's
+ * pointer to this data.
+ */
+#ifdef USE_SP_TERMTYPE
+#undef CUR
+#endif
+
+#define SP_TERMTYPE TerminalOf(sp)->type.
+
 #include <term_entry.h>
+
 #include <nc_tparm.h>
 
 #if NCURSES_EXT_COLORS && USE_WIDEC_SUPPORT
@@ -272,8 +332,15 @@ color_t;
 #define GET_WINDOW_PAIR(w)     (w)->_color
 #define SET_WINDOW_PAIR(w,p)   (w)->_color = (p)
 #define SameAttrOf(a,b)                (AttrOf(a) == AttrOf(b) && GetPair(a) == GetPair(b))
-#define VIDATTR(attr, pair)    vid_attr(attr, pair, 0)
+
+#if NCURSES_SP_FUNCS
+#define VIDATTR(sp,attr,pair)  NCURSES_SP_NAME(vid_attr)(sp, attr, pair, 0)
 #else
+#define VIDATTR(sp,attr,pair)  vid_attr(attr, pair, 0)
+#endif
+
+#else /* !(NCURSES_EXT_COLORS && USE_WIDEC_SUPPORT) */
+
 #define if_EXT_COLORS(stmt)    /* nothing */
 #define SetPair(value,p)       RemAttr(value, A_COLOR), \
                                SetAttr(value, AttrOf(value) | (A_COLOR & COLOR_PAIR(p)))
@@ -283,7 +350,22 @@ color_t;
 #define SET_WINDOW_PAIR(w,p)   WINDOW_ATTRS(w) &= ALL_BUT_COLOR, \
                                WINDOW_ATTRS(w) |= (A_COLOR & COLOR_PAIR(p))
 #define SameAttrOf(a,b)                (AttrOf(a) == AttrOf(b))
-#define VIDATTR(attr, pair)    vidattr(attr)
+
+#if NCURSES_SP_FUNCS
+#define VIDATTR(sp,attr,pair)  NCURSES_SP_NAME(vidattr)(sp, attr)
+#else
+#define VIDATTR(sp,attr,pair)  vidattr(attr)
+#endif
+
+#endif /* NCURSES_EXT_COLORS && USE_WIDEC_SUPPORT */
+
+#if NCURSES_NO_PADDING
+#define GetNoPadding(sp)       ((sp) ? (sp)->_no_padding : _nc_prescreen._no_padding)
+#define SetNoPadding(sp)       _nc_set_no_padding(sp)
+extern NCURSES_EXPORT(void)     _nc_set_no_padding(SCREEN *);
+#else
+#define GetNoPadding(sp)       FALSE
+#define SetNoPadding(sp)       /*nothing*/
 #endif
 
 #define WINDOW_ATTRS(w)                ((w)->_attrs)
@@ -293,37 +375,118 @@ color_t;
 #define SET_SCREEN_PAIR(s,p)   SetPair(SCREEN_ATTRS(s), p)
 
 #if USE_REENTRANT
-#define SET_LINES(value) SP->_LINES = value
-#define SET_COLS(value)  SP->_COLS = value
+
+NCURSES_EXPORT(int *)        _nc_ptr_Lines (SCREEN *);
+NCURSES_EXPORT(int *)        _nc_ptr_Cols (SCREEN *);
+NCURSES_EXPORT(int *)        _nc_ptr_Tabsize (SCREEN *);
+NCURSES_EXPORT(int *)        _nc_ptr_Escdelay (SCREEN *);
+
+#define ptrLines(sp)         (sp ? &(sp->_LINES) : &(_nc_prescreen._LINES))
+#define ptrCols(sp)          (sp ? &(sp->_COLS) : &(_nc_prescreen._COLS))
+#define ptrTabsize(sp)       (sp ? &(sp->_TABSIZE) : &(_nc_prescreen._TABSIZE))
+#define ptrEscdelay(sp)      (sp ? &(sp->_ESCDELAY) : &(_nc_prescreen._ESCDELAY))
+
+#define SET_LINES(value)     *_nc_ptr_Lines(SP_PARM) = value
+#define SET_COLS(value)      *_nc_ptr_Cols(SP_PARM) = value
+#define SET_TABSIZE(value)   *_nc_ptr_Tabsize(SP_PARM) = value
+#define SET_ESCDELAY(value)  *_nc_ptr_Escdelay(SP_PARM) = value
+
 #else
-#define SET_LINES(value) LINES = value
-#define SET_COLS(value)  COLS = value
+
+#define ptrLines(sp)         &LINES
+#define ptrCols(sp)          &COLS
+#define ptrTabsize(sp)       &TABSIZE
+#define ptrEscdelay(sp)      &ESCDELAY
+
+#define SET_LINES(value)     LINES = value
+#define SET_COLS(value)      COLS = value
+#define SET_TABSIZE(value)   TABSIZE = value
+#define SET_ESCDELAY(value)  ESCDELAY = value
+
 #endif
 
+#define TR_MUTEX(data) _tracef("%s@%d: me:%08lX COUNT:%2u/%2d/%6d/%2d/%s%9u: " #data, \
+           __FILE__, __LINE__, \
+           (unsigned long) (pthread_self()), \
+           data.__data.__lock, \
+           data.__data.__count, \
+           data.__data.__owner, \
+           data.__data.__kind, \
+           (data.__data.__nusers > 5) ? " OOPS " : "", \
+           data.__data.__nusers)
+#define TR_GLOBAL_MUTEX(name) TR_MUTEX(_nc_globals.mutex_##name)
+
 #ifdef USE_PTHREADS
+
 #if USE_REENTRANT
 #include <pthread.h>
-#define _nc_lock_global(name)  pthread_mutex_lock(&_nc_globals.mutex_##name)
-#define _nc_unlock_global(name)        pthread_mutex_unlock(&_nc_globals.mutex_##name)
-
-extern NCURSES_EXPORT(void) _nc_lock_window(WINDOW *);
-extern NCURSES_EXPORT(void) _nc_unlock_window(WINDOW *);
+extern NCURSES_EXPORT(void) _nc_init_pthreads(void);
+extern NCURSES_EXPORT(void) _nc_mutex_init(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_lock(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_trylock(pthread_mutex_t *);
+extern NCURSES_EXPORT(int) _nc_mutex_unlock(pthread_mutex_t *);
+#define _nc_lock_global(name)  _nc_mutex_lock(&_nc_globals.mutex_##name)
+#define _nc_try_global(name)    _nc_mutex_trylock(&_nc_globals.mutex_##name)
+#define _nc_unlock_global(name)        _nc_mutex_unlock(&_nc_globals.mutex_##name)
 
 #else
 #error POSIX threads requires --enable-reentrant option
 #endif
-#else
+
+#if USE_WEAK_SYMBOLS
+#if defined(__GNUC__)
+#  if defined __USE_ISOC99
+#    define _cat_pragma(exp)   _Pragma(#exp)
+#    define _weak_pragma(exp)  _cat_pragma(weak name)
+#  else
+#    define _weak_pragma(exp)
+#  endif
+#  define _declare(name)       __extension__ extern __typeof__(name) name
+#  define weak_symbol(name)    _weak_pragma(name) _declare(name) __attribute__((weak))
+#endif
+#endif
+
+#ifdef USE_PTHREADS
+#  if USE_WEAK_SYMBOLS
+weak_symbol(pthread_sigmask);
+weak_symbol(pthread_self);
+weak_symbol(pthread_equal);
+weak_symbol(pthread_mutex_init);
+weak_symbol(pthread_mutex_lock);
+weak_symbol(pthread_mutex_unlock);
+weak_symbol(pthread_mutex_trylock);
+weak_symbol(pthread_mutexattr_settype);
+weak_symbol(pthread_mutexattr_init);
+extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
+#    undef  sigprocmask
+#    define sigprocmask _nc_sigprocmask
+#  endif
+#endif
+
+#if HAVE_NANOSLEEP
+#undef HAVE_NANOSLEEP
+#define HAVE_NANOSLEEP 0       /* nanosleep suspends all threads */
+#endif
+
+#else /* !USE_PTHREADS */
+
+#define _nc_init_pthreads()    /* nothing */
+#define _nc_mutex_init(obj)    /* nothing */
+
 #define _nc_lock_global(name)  /* nothing */
+#define _nc_try_global(name)    0
 #define _nc_unlock_global(name)        /* nothing */
 
-#define _nc_lock_window(name)  TRUE
-#define _nc_unlock_window(name)        /* nothing */
+#endif /* USE_PTHREADS */
 
+#if HAVE_GETTIMEOFDAY
+# define PRECISE_GETTIME 1
+# define TimeType struct timeval
+#else
+# define PRECISE_GETTIME 0
+# define TimeType time_t
 #endif
 
-#define _nc_lock_screen(name)  /* nothing */
-#define _nc_unlock_screen(name)        /* nothing */
-
 /*
  * Definitions for color pairs
  */
@@ -331,6 +494,8 @@ typedef unsigned colorpair_t;       /* type big enough to store PAIR_OF() */
 #define C_SHIFT 9              /* we need more bits than there are colors */
 #define C_MASK                 ((1 << C_SHIFT) - 1)
 #define PAIR_OF(fg, bg)                ((((fg) & C_MASK) << C_SHIFT) | ((bg) & C_MASK))
+#define FORE_OF(c)             (((c) >> C_SHIFT) & C_MASK)
+#define BACK_OF(c)             ((c) & C_MASK)
 #define isDefaultColor(c)      ((c) >= COLOR_DEFAULT || (c) < 0)
 
 #define COLOR_DEFAULT          C_MASK
@@ -391,19 +556,20 @@ typedef struct
 } slk_ent;
 
 typedef struct _SLK {
-       char dirty;             /* all labels have changed */
-       char hidden;            /* soft labels are hidden */
-       WINDOW *win;
+       bool    dirty;          /* all labels have changed */
+       bool    hidden;         /* soft labels are hidden */
+       WINDOW  *win;
        slk_ent *ent;
-       short  maxlab;          /* number of available labels */
-       short  labcnt;          /* number of allocated labels */
-       short  maxlen;          /* length of labels */
+       short   maxlab;         /* number of available labels */
+       short   labcnt;         /* number of allocated labels */
+       short   maxlen;         /* length of labels */
        NCURSES_CH_T attr;      /* soft label attribute */
 } SLK;
 
 #endif /* USE_TERMLIB */
 
 typedef        struct {
+       WINDOW *win;            /* the window used in the hook      */
        int     line;           /* lines to take, < 0 => from bottom*/
        int     (*hook)(WINDOW *, int); /* callback for user        */
 } ripoff_t;
@@ -419,10 +585,10 @@ typedef int (*TYPE_Gpm_Open) (Gpm_Connect *, int);
 typedef int (*TYPE_Gpm_Close) (void);
 typedef int (*TYPE_Gpm_GetEvent) (Gpm_Event *);
 
-#define my_gpm_fd       SP->_mouse_gpm_fd
-#define my_Gpm_Open     SP->_mouse_Gpm_Open
-#define my_Gpm_Close    SP->_mouse_Gpm_Close
-#define my_Gpm_GetEvent SP->_mouse_Gpm_GetEvent
+#define my_gpm_fd       SP_PARM->_mouse_gpm_fd
+#define my_Gpm_Open     SP_PARM->_mouse_Gpm_Open
+#define my_Gpm_Close    SP_PARM->_mouse_Gpm_Close
+#define my_Gpm_GetEvent SP_PARM->_mouse_Gpm_GetEvent
 #else
 /* link statically to GPM */
 #define my_gpm_fd       &gpm_fd
@@ -482,6 +648,17 @@ typedef struct {
     size_t size;
 } TRACEBUF;
 
+/*
+ * The filesystem database normally uses a single-letter for the lower level
+ * of directories.  Use a hexadecimal code for filesystems which do not
+ * preserve mixed-case names.
+ */
+#if MIXEDCASE_FILENAMES
+#define LEAF_FMT "%c"
+#else
+#define LEAF_FMT "%02x"
+#endif
+
 /*
  * TRACEMSE_FMT is no longer than 80 columns, there are 5 numbers that
  * could at most have 10 digits, and the mask contains no more than 32 bits
@@ -515,6 +692,8 @@ typedef struct {
        char            *first_name;
        char            **keyname_table;
 
+       int             slk_format;
+
        char            *safeprint_buf;
        size_t          safeprint_used;
 
@@ -522,6 +701,11 @@ typedef struct {
        int             tgetent_index;
        long            tgetent_sequence;
 
+#ifndef USE_SP_WINDOWLIST
+       WINDOWLIST      *_nc_windowlist;
+#define WindowList(sp) _nc_globals._nc_windowlist
+#endif
+
 #if USE_HOME_TERMINFO
        char            *home_terminfo;
 #endif
@@ -548,27 +732,24 @@ typedef struct {
        char            *tracedmp_buf;
        size_t          tracedmp_used;
 
-       char            tracemse_buf[TRACEMSE_MAX];
-
        unsigned char   *tracetry_buf;
        size_t          tracetry_used;
 
-#ifndef USE_TERMLIB
        char            traceatr_color_buf[2][80];
        int             traceatr_color_sel;
        int             traceatr_color_last;
-#endif /* USE_TERMLIB */
-
+#if !defined(USE_PTHREADS) && USE_REENTRANT
+       int             nested_tracef;
+#endif
 #endif /* TRACE */
 
 #ifdef USE_PTHREADS
-       pthread_mutex_t mutex_set_SP;
-       pthread_mutex_t mutex_use_screen;
-       pthread_mutex_t mutex_use_window;
-       pthread_mutex_t mutex_windowlist;
-       pthread_mutex_t mutex_tst_tracef;
-       pthread_mutex_t mutex_tracef;
-       int             nested_tracef;
+       pthread_mutex_t mutex_curses;
+       pthread_mutex_t mutex_tst_tracef;
+       pthread_mutex_t mutex_tracef;
+       int             nested_tracef;
+       int             use_pthreads;
+#define _nc_use_pthreads       _nc_globals.use_pthreads
 #endif
 } NCURSES_GLOBALS;
 
@@ -577,21 +758,29 @@ extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
 #define N_RIPS 5
 
 /*
- * Global data which is swept up into a SCREEN when one is created.
+ * Global data which can be swept up into a SCREEN when one is created.
  * It may be modified before the next SCREEN is created.
  */
 typedef struct {
        bool            use_env;
        bool            filter_mode;
        attr_t          previous_attr;
+#ifndef USE_SP_RIPOFF
        ripoff_t        rippedoff[N_RIPS];
        ripoff_t        *rsp;
+#endif
        TPARM_STATE     tparm_state;
        TTY             *saved_tty;     /* savetty/resetty information      */
+#if NCURSES_NO_PADDING
+       bool            _no_padding;    /* flag to set if padding disabled  */
+#endif
 #if BROKEN_LINKER || USE_REENTRANT
        chtype          *real_acs_map;
        int             _LINES;
        int             _COLS;
+       int             _TABSIZE;
+       int             _ESCDELAY;
+       TERMINAL        *_cur_term;
 #ifdef TRACE
        long            _outchars;
        const char      *_tputs_trace;
@@ -599,6 +788,17 @@ typedef struct {
 #endif
 } NCURSES_PRESCREEN;
 
+/*
+ * Use screen-specific ripoff data (for softkeys) rather than global.
+ */
+#ifdef USE_SP_RIPOFF
+#define safe_ripoff_sp     (sp)->rsp
+#define safe_ripoff_stack  (sp)->rippedoff
+#else
+#define safe_ripoff_sp    _nc_prescreen.rsp
+#define safe_ripoff_stack  _nc_prescreen.rippedoff
+#endif
+
 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
 
 /*
@@ -611,6 +811,8 @@ struct screen {
        char            *_setbuf;       /* buffered I/O for output          */
        bool            _filtered;      /* filter() was called              */
        bool            _buffered;      /* setvbuf uses _setbuf data        */
+       bool            _prescreen;     /* is in prescreen phase            */
+       bool            _use_env;       /* LINES & COLS from environment?   */
        int             _checkfd;       /* filedesc for typeahead check     */
        TERMINAL        *_term;         /* terminal type information        */
        TTY             _saved_tty;     /* savetty/resetty information      */
@@ -624,6 +826,10 @@ struct screen {
        WINDOW          *_newscr;       /* virtual screen to be updated to  */
        WINDOW          *_stdscr;       /* screen's full-window context     */
 
+#define CurScreen(sp)  (sp)->_curscr
+#define NewScreen(sp)  (sp)->_newscr
+#define StdScreen(sp)  (sp)->_stdscr
+
        TRIES           *_keytry;       /* "Try" for use with keypad mode   */
        TRIES           *_key_ok;       /* Disabled keys via keyok(,FALSE)  */
        bool            _tried;         /* keypad mode was initialized      */
@@ -654,7 +860,7 @@ struct screen {
        int             slk_format;     /* selected format for this screen  */
        /* cursor movement costs; units are 10ths of milliseconds */
 #if NCURSES_NO_PADDING
-       int             _no_padding;    /* flag to set if padding disabled  */
+       bool            _no_padding;    /* flag to set if padding disabled  */
 #endif
        int             _char_padding;  /* cost of character put            */
        int             _cr_cost;       /* cost of (carriage_return)        */
@@ -702,11 +908,13 @@ struct screen {
        int             _color_count;   /* count of colors in palette        */
        colorpair_t     *_color_pairs;  /* screen's color pair list          */
        int             _pair_count;    /* count of color pairs              */
+       int             _pair_limit;    /* actual limit of color-pairs       */
 #if NCURSES_EXT_FUNCS
        bool            _default_color; /* use default colors                */
        bool            _has_sgr_39_49; /* has ECMA default color support    */
        int             _default_fg;    /* assumed default foreground        */
        int             _default_bg;    /* assumed default background        */
+       int             _default_pairs; /* count pairs using default color   */
 #endif
        chtype          _ok_attributes; /* valid attributes for terminal     */
        chtype          _xmc_suppress;  /* attributes to suppress if xmc     */
@@ -728,8 +936,9 @@ struct screen {
         */
        bool            _nc_sp_idlok;
        bool            _nc_sp_idcok;
-#define _nc_idlok SP->_nc_sp_idlok
-#define _nc_idcok SP->_nc_sp_idcok
+
+#define _nc_idlok SP_PARM->_nc_sp_idlok
+#define _nc_idcok SP_PARM->_nc_sp_idcok
 
        /*
         * These are the data that support the mouse interface.
@@ -739,7 +948,7 @@ struct screen {
        int             _maxclick;
        bool            (*_mouse_event) (SCREEN *);
        bool            (*_mouse_inline)(SCREEN *);
-       bool            (*_mouse_parse) (int);
+       bool            (*_mouse_parse) (SCREEN *, int);
        void            (*_mouse_resume)(SCREEN *);
        void            (*_mouse_wrap)  (SCREEN *);
        int             _mouse_fd;      /* file-descriptor, if any */
@@ -753,6 +962,7 @@ struct screen {
        bool            _mouse_gpm_loaded;
        bool            _mouse_gpm_found;
 #ifdef HAVE_LIBDL
+       void            *_dlopen_gpm;
        TYPE_gpm_fd     _mouse_gpm_fd;
        TYPE_Gpm_Open   _mouse_Gpm_Open;
        TYPE_Gpm_Close  _mouse_Gpm_Close;
@@ -782,7 +992,7 @@ struct screen {
         * This supports automatic resizing
         */
 #if USE_SIZECHANGE
-       int             (*_resize)(int,int);
+       int             (*_resize)(NCURSES_SP_DCLx int y, int x);
 #endif
 
        /*
@@ -790,12 +1000,6 @@ struct screen {
         * per screen basis.
         */
        struct panelhook _panelHook;
-       /*
-        * Linked-list of all windows, to support '_nc_resizeall()' and
-        * '_nc_freeall()'
-        */
-       WINDOWLIST      *_nc_sp_windows;
-#define _nc_windows SP->_nc_sp_windows
 
        bool            _sig_winch;
        SCREEN          *_next_screen;
@@ -808,7 +1012,7 @@ struct screen {
        int             _oldnum_size;
 
        bool            _cleanup;       /* cleanup after int/quit signal */
-       int             (*_outch)(int); /* output handler if not putc */
+       NCURSES_SP_OUTC _outch;         /* output handler if not putc */
 
        int             _legacy_coding; /* see use_legacy_coding() */
 
@@ -819,10 +1023,24 @@ struct screen {
        int             _LINES;
        int             _COLS;
 #ifdef TRACE
-       int             _outchars;
+       long            _outchars;
        const char      *_tputs_trace;
 #endif
 #endif
+
+#ifdef TRACE
+       char            tracechr_buf[40];
+       char            tracemse_buf[TRACEMSE_MAX];
+#endif
+#ifdef USE_SP_WINDOWLIST
+       WINDOWLIST*     _windowlist;
+#define WindowList(sp)  (sp)->_windowlist
+#endif
+       NCURSES_OUTC    jump;
+
+       ripoff_t        rippedoff[N_RIPS];
+       ripoff_t        *rsp;
+
        /*
         * ncurses/ncursesw are the same up to this point.
         */
@@ -838,21 +1056,33 @@ extern NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain;
 extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 
        WINDOWLIST {
-       WINDOW  win;    /* first, so WINDOW_EXT() works */
+       WINDOW  win;            /* first, so WINDOW_EXT() works */
        WINDOWLIST *next;
+       SCREEN *screen;         /* screen containing the window */
 #ifdef _XOPEN_SOURCE_EXTENDED
        char addch_work[(MB_LEN_MAX * 9) + 1];
        unsigned addch_used;    /* number of bytes in addch_work[] */
        int addch_x;            /* x-position for addch_work[] */
        int addch_y;            /* y-position for addch_work[] */
 #endif
-#ifdef USE_PTHREADS
-       pthread_mutex_t mutex_use_window;
-#endif
 };
 
 #define WINDOW_EXT(win,field) (((WINDOWLIST *)(win))->field)
 
+#define SP_PRE_INIT(sp)                         \
+    sp->_cursrow = -1;                          \
+    sp->_curscol = -1;                          \
+    sp->_nl = TRUE;                             \
+    sp->_raw = FALSE;                           \
+    sp->_cbreak = 0;                            \
+    sp->_echo = TRUE;                           \
+    sp->_fifohead = -1;                         \
+    sp->_endwin = TRUE;                         \
+    sp->_cursor = -1;                           \
+    WindowList(sp) = 0;                         \
+    sp->_outch = NCURSES_SP_NAME(_nc_outch);    \
+    sp->jump = 0                                \
+
 /* usually in <limits.h> */
 #ifndef UCHAR_MAX
 #define UCHAR_MAX 255
@@ -935,7 +1165,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 #endif
 
 #if USE_WIDEC_SUPPORT /* { */
-#define isEILSEQ(status) ((status == (size_t)-1) && (errno == EILSEQ))
+#define isEILSEQ(status) (((size_t)status == (size_t)-1) && (errno == EILSEQ))
 
 #define init_mb(state) memset(&state, 0, sizeof(state))
 
@@ -1022,7 +1252,7 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
                                AttrOf(dst) |= (ext + 1)
 
 #define if_WIDEC(code)  code
-#define Charable(ch)   ((SP != 0 && SP->_legacy_coding)                \
+#define Charable(ch)   ((SP_PARM != 0 && SP_PARM->_legacy_coding)      \
                         || (AttrOf(ch) & A_ALTCHARSET)                 \
                         || (!isWidecExt(ch) &&                         \
                             (ch).chars[1] == L'\0' &&                  \
@@ -1120,6 +1350,13 @@ extern NCURSES_EXPORT_VAR(SIG_ATOMIC_T) _nc_have_sigwinch;
 # endif
 #endif
 
+/*
+ * Standardize/simplify common loops
+ */
+#define each_screen(p) p = _nc_screen_chain; p != 0; p = (p)->_next_screen
+#define each_window(sp,p) p = WindowList(sp); p != 0; p = (p)->next
+#define each_ripoff(p) p = safe_ripoff_stack; (p - safe_ripoff_stack) < N_RIPS; ++p
+
 /*
  * Prefixes for call/return points of library function traces.  We use these to
  * instrument the public functions so that the traces can be easily transformed
@@ -1166,7 +1403,7 @@ extern NCURSES_EXPORT(void)       _nc_locked_tracef (const char *, ...) GCC_PRINTFLIKE
 #define returnBool(code)       TRACE_RETURN(code,bool)
 #define returnCPtr(code)       TRACE_RETURN(code,cptr)
 #define returnCVoidPtr(code)   TRACE_RETURN(code,cvoid_ptr)
-#define returnChar(code)       TRACE_RETURN(code,chtype)
+#define returnChtype(code)     TRACE_RETURN(code,chtype)
 #define returnCode(code)       TRACE_RETURN(code,int)
 #define returnPtr(code)                TRACE_RETURN(code,ptr)
 #define returnSP(code)         TRACE_RETURN(code,sp)
@@ -1188,7 +1425,7 @@ extern NCURSES_EXPORT(const char *)     _nc_retrace_cptr (const char *);
 extern NCURSES_EXPORT(int)              _nc_retrace_int (int);
 extern NCURSES_EXPORT(unsigned)         _nc_retrace_unsigned (unsigned);
 extern NCURSES_EXPORT(void *)           _nc_retrace_void_ptr (void *);
-extern NCURSES_EXPORT(void)             _nc_fifo_dump (void);
+extern NCURSES_EXPORT(void)             _nc_fifo_dump (SCREEN *);
 
 #if USE_REENTRANT
 NCURSES_WRAPPED_VAR(long, _nc_outchars);
@@ -1225,7 +1462,7 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 #define returnBool(code)       return code
 #define returnCPtr(code)       return code
 #define returnCVoidPtr(code)   return code
-#define returnChar(code)       return code
+#define returnChtype(code)     return code
 #define returnCode(code)       return code
 #define returnPtr(code)                return code
 #define returnSP(code)         return code
@@ -1245,13 +1482,13 @@ extern NCURSES_EXPORT(const char *) _nc_viscbuf (const NCURSES_CH_T *, int);
 extern NCURSES_EXPORT(const char *) _nc_visbuf2 (int, const char *);
 extern NCURSES_EXPORT(const char *) _nc_visbufn (const char *, int);
 
-#define empty_module(name) \
+#define EMPTY_MODULE(name) \
 extern NCURSES_EXPORT(void) name (void); \
        NCURSES_EXPORT(void) name (void) { }
 
 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
 #define NONBLANK_ATTR (A_NORMAL|A_BOLD|A_DIM|A_BLINK)
-#define XMC_CHANGES(c) ((c) & SP->_xmc_suppress)
+#define XMC_CHANGES(c) ((c) & SP_PARM->_xmc_suppress)
 
 #define toggle_attr_on(S,at) {\
    if (PAIR_NUMBER(at) > 0) {\
@@ -1270,38 +1507,38 @@ extern  NCURSES_EXPORT(void) name (void); \
    }\
    TR(TRACE_ATTRS, ("new attribute is %s", _traceattr((S))));}
 
-#define DelCharCost(count) \
+#define DelCharCost(sp,count) \
                ((parm_dch != 0) \
-               ? SP->_dch_cost \
+               ? sp->_dch_cost \
                : ((delete_character != 0) \
-                       ? (SP->_dch1_cost * count) \
+                       ? (sp->_dch1_cost * count) \
                        : INFINITY))
 
-#define InsCharCost(count) \
+#define InsCharCost(sp,count) \
                ((parm_ich != 0) \
-               ? SP->_ich_cost \
+               ? sp->_ich_cost \
                : ((enter_insert_mode && exit_insert_mode) \
-                 ? SP->_smir_cost + SP->_rmir_cost + (SP->_ip_cost * count) \
+                 ? sp->_smir_cost + sp->_rmir_cost + (sp->_ip_cost * count) \
                  : ((insert_character != 0) \
-                   ? ((SP->_ich1_cost + SP->_ip_cost) * count) \
+                   ? ((sp->_ich1_cost + sp->_ip_cost) * count) \
                    : INFINITY)))
 
 #if USE_XMC_SUPPORT
-#define UpdateAttrs(c) if (!SameAttrOf(SCREEN_ATTRS(SP), c)) { \
-                               attr_t chg = AttrOf(SCREEN_ATTRS(SP)); \
-                               VIDATTR(AttrOf(c), GetPair(c)); \
+#define UpdateAttrs(sp,c)      if (!SameAttrOf(SCREEN_ATTRS(sp), c)) { \
+                               attr_t chg = AttrOf(SCREEN_ATTRS(sp)); \
+                               VIDATTR(sp, AttrOf(c), GetPair(c)); \
                                if (magic_cookie_glitch > 0 \
-                                && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(SP))))) { \
+                                && XMC_CHANGES((chg ^ AttrOf(SCREEN_ATTRS(sp))))) { \
                                        T(("%s @%d before glitch %d,%d", \
                                                __FILE__, __LINE__, \
-                                               SP->_cursrow, \
-                                               SP->_curscol)); \
-                                       _nc_do_xmc_glitch(chg); \
+                                               sp->_cursrow, \
+                                               sp->_curscol)); \
+                                       NCURSES_SP_NAME(_nc_do_xmc_glitch)(NCURSES_SP_ARGx chg); \
                                } \
                        }
 #else
-#define UpdateAttrs(c) if (!SameAttrOf(SCREEN_ATTRS(SP), c)) \
-                               VIDATTR(AttrOf(c), GetPair(c));
+#define UpdateAttrs(sp,c)      if (!SameAttrOf(SCREEN_ATTRS(sp), c)) \
+                                   VIDATTR(sp, AttrOf(c), GetPair(c));
 #endif
 
 /*
@@ -1328,16 +1565,28 @@ extern NCURSES_EXPORT(void) _nc_toggle_attr_on (attr_t *, attr_t);
 extern NCURSES_EXPORT(void) _nc_toggle_attr_off (attr_t *, attr_t);
 
 #undef  DelCharCost
-#define DelCharCost(count) _nc_DelCharCost(count)
-extern NCURSES_EXPORT(int) _nc_DelCharCost (int);
+#define DelCharCost(sp, count) NCURSES_SP_NAME(_nc_DelCharCost)(NCURSES_SP_ARGx count)
 
 #undef  InsCharCost
-#define InsCharCost(count) _nc_InsCharCost(count)
-extern NCURSES_EXPORT(int) _nc_InsCharCost (int);
+#define InsCharCost(sp, count) NCURSES_SP_NAME(_nc_InsCharCost)(NCURSES_SP_ARGx count)
+
+extern NCURSES_EXPORT(int) NCURSES_SP_NAME(_nc_DelCharCost) (NCURSES_SP_DCLx int _c);
+extern NCURSES_EXPORT(int) NCURSES_SP_NAME(_nc_InsCharCost) (NCURSES_SP_DCLx int _c);
 
 #undef  UpdateAttrs
-#define UpdateAttrs(c) _nc_UpdateAttrs(c)
-extern NCURSES_EXPORT(void) _nc_UpdateAttrs (NCURSES_CH_T);
+#define UpdateAttrs(sp,c) NCURSES_SP_NAME(_nc_UpdateAttrs)(NCURSES_SP_ARGx CHREF(c))
+
+#if defined(NEED_NCURSES_CH_T)
+extern NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_UpdateAttrs) (NCURSES_SP_DCLx CARG_CH_T _c);
+#else
+extern NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_UpdateAttrs) (NCURSES_SP_DCLx chtype c);
+#endif
+
+#if NCURSES_SP_FUNCS
+extern NCURSES_EXPORT(int) _nc_DelCharCost (int);
+extern NCURSES_EXPORT(int) _nc_InsCharCost (int);
+extern NCURSES_EXPORT(void) _nc_UpdateAttrs (CARG_CH_T);
+#endif /* NCURSES_SP_FUNCS */
 
 #else
 
@@ -1345,6 +1594,10 @@ extern NCURSES_EXPORT(void) _nc_expanded (void);
 
 #endif
 
+#if !NCURSES_EXT_FUNCS
+#define set_escdelay(value) ESCDELAY = value
+#endif
+
 #if !HAVE_GETCWD
 #define getcwd(buf,len) getwd(buf)
 #endif
@@ -1382,8 +1635,8 @@ extern NCURSES_EXPORT(void) _nc_linedump (void);
 #endif
 
 /* lib_acs.c */
-extern NCURSES_EXPORT(void) _nc_init_acs (void);       /* corresponds to traditional 'init_acs()' */
-extern NCURSES_EXPORT(int) _nc_msec_cost (const char *const, int);  /* used by 'tack' program */
+extern NCURSES_EXPORT(void) _nc_init_acs (void); /* corresponds to traditional 'init_acs()' */
+extern NCURSES_EXPORT(int)  _nc_msec_cost (const char *const, int);  /* used by 'tack' program */
 
 /* lib_addch.c */
 #if USE_WIDEC_SUPPORT
@@ -1402,7 +1655,7 @@ extern NCURSES_EXPORT(bool) _nc_reset_colors(void);
 extern NCURSES_EXPORT(int) _nc_wgetch(WINDOW *, unsigned long *, int EVENTLIST_2nd(_nc_eventlist *));
 
 /* lib_insch.c */
-extern NCURSES_EXPORT(int) _nc_insert_ch(WINDOW *, chtype);
+extern NCURSES_EXPORT(int) _nc_insert_ch(SCREEN *, WINDOW *, chtype);
 
 /* lib_mvcur.c */
 #define INFINITY       1000000 /* cost: too high to use */
@@ -1418,7 +1671,7 @@ extern NCURSES_EXPORT(void) _nc_screen_resume (void);
 extern NCURSES_EXPORT(void) _nc_screen_wrap (void);
 
 /* lib_mouse.c */
-extern NCURSES_EXPORT(int) _nc_has_mouse (void);
+extern NCURSES_EXPORT(bool) _nc_has_mouse (SCREEN *);
 
 /* lib_mvcur.c */
 #define INFINITY       1000000 /* cost: too high to use */
@@ -1426,14 +1679,17 @@ extern NCURSES_EXPORT(int) _nc_has_mouse (void);
 
 /* lib_setup.c */
 extern NCURSES_EXPORT(char *) _nc_get_locale(void);
-extern NCURSES_EXPORT(int) _nc_unicode_locale(void);
-extern NCURSES_EXPORT(int) _nc_locale_breaks_acs(void);
-extern NCURSES_EXPORT(int) _nc_setupterm(NCURSES_CONST char *, int, int *, bool);
-extern NCURSES_EXPORT(void) _nc_get_screensize(int *, int *);
+extern NCURSES_EXPORT(int)    _nc_unicode_locale(void);
+extern NCURSES_EXPORT(int)    _nc_locale_breaks_acs(TERMINAL *);
+extern NCURSES_EXPORT(int)    _nc_setupterm(NCURSES_CONST char *, int, int *, bool);
+extern NCURSES_EXPORT(void)   _nc_get_screensize(SCREEN *, int *, int *);
+
+/* lib_set_term.c */
+extern NCURSES_EXPORT(int)    _nc_ripoffline(int, int(*)(WINDOW*, int));
 
 /* lib_tstp.c */
 #if USE_SIGWINCH
-extern NCURSES_EXPORT(int) _nc_handle_sigwinch(int);
+extern NCURSES_EXPORT(int) _nc_handle_sigwinch(SCREEN *);
 #else
 #define _nc_handle_sigwinch(a) /* nothing */
 #endif
@@ -1472,26 +1728,32 @@ extern NCURSES_EXPORT(int) _nc_remove_key (TRIES **, unsigned);
 extern NCURSES_EXPORT(int) _nc_remove_string (TRIES **, const char *);
 
 /* elsewhere ... */
-extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry(ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(ENTRY *) _nc_delink_entry (ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(SCREEN *) _nc_screen_of (WINDOW *);
 extern NCURSES_EXPORT(WINDOW *) _nc_makenew (int, int, int, int, int);
 extern NCURSES_EXPORT(char *) _nc_trace_buf (int, size_t);
 extern NCURSES_EXPORT(char *) _nc_trace_bufcat (int, const char *);
+extern NCURSES_EXPORT(char *) _nc_tracechar (SCREEN *, int);
+extern NCURSES_EXPORT(char *) _nc_tracemouse (SCREEN *, MEVENT const *);
 extern NCURSES_EXPORT(int) _nc_access (const char *, int);
 extern NCURSES_EXPORT(int) _nc_baudrate (int);
 extern NCURSES_EXPORT(int) _nc_freewin (WINDOW *);
 extern NCURSES_EXPORT(int) _nc_getenv_num (const char *);
-extern NCURSES_EXPORT(int) _nc_keypad (bool);
+extern NCURSES_EXPORT(int) _nc_keypad (SCREEN *, bool);
 extern NCURSES_EXPORT(int) _nc_ospeed (int);
 extern NCURSES_EXPORT(int) _nc_outch (int);
+extern NCURSES_EXPORT(int) _nc_putp(const char *, const char *);
+extern NCURSES_EXPORT(int) _nc_putp_flush(const char *, const char *);
 extern NCURSES_EXPORT(int) _nc_read_termcap_entry (const char *const, TERMTYPE *const);
 extern NCURSES_EXPORT(int) _nc_setupscreen (int, int, FILE *, bool, int);
-extern NCURSES_EXPORT(int) _nc_timed_wait(int, int, int * EVENTLIST_2nd(_nc_eventlist *));
-extern NCURSES_EXPORT(void) _nc_do_color (short, short, bool, int (*)(int));
+extern NCURSES_EXPORT(int) _nc_timed_wait (SCREEN *, int, int, int * EVENTLIST_2nd(_nc_eventlist *));
+extern NCURSES_EXPORT(void) _nc_do_color (short, short, bool, NCURSES_OUTC);
 extern NCURSES_EXPORT(void) _nc_flush (void);
-extern NCURSES_EXPORT(void) _nc_free_entry(ENTRY *, TERMTYPE *);
+extern NCURSES_EXPORT(void) _nc_free_and_exit (int) GCC_NORETURN;
+extern NCURSES_EXPORT(void) _nc_free_entry (ENTRY *, TERMTYPE *);
 extern NCURSES_EXPORT(void) _nc_freeall (void);
 extern NCURSES_EXPORT(void) _nc_hash_map (void);
-extern NCURSES_EXPORT(void) _nc_init_keytry (void);
+extern NCURSES_EXPORT(void) _nc_init_keytry (SCREEN *);
 extern NCURSES_EXPORT(void) _nc_keep_tic_dir (const char *);
 extern NCURSES_EXPORT(void) _nc_make_oldhash (int i);
 extern NCURSES_EXPORT(void) _nc_scroll_oldhash (int n, int top, int bot);
@@ -1519,15 +1781,11 @@ extern NCURSES_EXPORT(void) _nc_scroll_window (WINDOW *, int const, NCURSES_SIZE
 #endif
 
 #if USE_WIDEC_SUPPORT && !defined(USE_TERMLIB)
-#ifdef linux
 extern NCURSES_EXPORT(size_t) _nc_wcrtomb (char *, wchar_t, mbstate_t *);
-#else
-#define _nc_wcrtomb(s,wc,ps) wcrtomb(s,wc,ps)
-#endif
 #endif
 
 #if USE_SIZECHANGE
-extern NCURSES_EXPORT(void) _nc_update_screensize (void);
+extern NCURSES_EXPORT(void) _nc_update_screensize (SCREEN *);
 #endif
 
 #if HAVE_RESIZETERM
@@ -1555,24 +1813,33 @@ extern NCURSES_EXPORT_VAR(int *) _nc_oldnums;
 
 #define USE_SETBUF_0 0
 
-#define NC_BUFFERED(flag) _nc_set_buffer(SP->_ofp, flag)
+#define NC_BUFFERED(sp,flag) NCURSES_SP_NAME(_nc_set_buffer)(NCURSES_SP_ARGx sp->_ofp, flag)
 
-#define NC_OUTPUT ((SP != 0) ? SP->_ofp : stdout)
+#define NC_OUTPUT(sp) ((sp != 0) ? sp->_ofp : stdout)
 
 /*
  * On systems with a broken linker, define 'SP' as a function to force the
  * linker to pull in the data-only module with 'SP'.
  */
+#define _nc_alloc_screen_sp() typeCalloc(SCREEN, 1)
+
 #if BROKEN_LINKER
-#define SP _nc_screen()
 extern NCURSES_EXPORT(SCREEN *) _nc_screen (void);
-extern NCURSES_EXPORT(int) _nc_alloc_screen (void);
-extern NCURSES_EXPORT(void) _nc_set_screen (SCREEN *);
+extern NCURSES_EXPORT(int)      _nc_alloc_screen (void);
+extern NCURSES_EXPORT(void)     _nc_set_screen (SCREEN *);
+#define CURRENT_SCREEN          _nc_screen()
 #else
 /* current screen is private data; avoid possible linking conflicts too */
 extern NCURSES_EXPORT_VAR(SCREEN *) SP;
-#define _nc_alloc_screen() ((SP = typeCalloc(SCREEN, 1)) != 0)
-#define _nc_set_screen(sp) SP = sp
+#define CURRENT_SCREEN SP
+#define _nc_alloc_screen()      ((SP = _nc_alloc_screen_sp()) != 0)
+#define _nc_set_screen(sp)      SP = sp
+#endif
+
+#if NCURSES_SP_FUNCS && 0
+#define CURRENT_SCREEN_PRE      (IsPreScreen(CURRENT_SCREEN) ? CURRENT_SCREEN : new_prescr())
+#else
+#define CURRENT_SCREEN_PRE      CURRENT_SCREEN
 #endif
 
 /*
@@ -1580,11 +1847,11 @@ extern NCURSES_EXPORT_VAR(SCREEN *) SP;
  * if the application is running multiple screens under X, it's quite possible
  * they could all have type xterm but have different sizes!  So...
  */
-#define screen_lines   SP->_lines
-#define screen_columns SP->_columns
+#define screen_lines(sp)        (sp)->_lines
+#define screen_columns(sp)      (sp)->_columns
 
-extern NCURSES_EXPORT_VAR(int) _nc_slk_format;  /* != 0 if slk_init() called */
 extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
+extern NCURSES_EXPORT(int) _nc_format_slks (NCURSES_SP_DCLx int _c);
 
 /*
  * Some constants related to SLK's
@@ -1602,14 +1869,240 @@ extern NCURSES_EXPORT(int) _nc_slk_initialize (WINDOW *, int);
 #define MAX_SKEY(fmt)     (SLK_STDFMT(fmt)? MAX_SKEY_OLD : MAX_SKEY_PC)
 #define MAX_SKEY_LEN(fmt) (SLK_STDFMT(fmt)? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
 
-extern NCURSES_EXPORT(int) _nc_ripoffline (int line, int (*init)(WINDOW *,int));
-
 /*
  * Common error messages
  */
 #define MSG_NO_MEMORY "Out of memory"
 #define MSG_NO_INPUTS "Premature EOF"
 
+extern NCURSES_EXPORT(int) _nc_set_tty_mode(TTY *);
+extern NCURSES_EXPORT(int) _nc_get_tty_mode(TTY *);
+
+/* timed_wait flag definitions */
+#define TW_NONE    0
+#define TW_INPUT   1
+#define TW_MOUSE   2
+#define TW_ANY     (TW_INPUT | TW_MOUSE)
+#define TW_EVENT   4
+
+#define SetSafeOutcWrapper(outc)           \
+    SCREEN* sp = CURRENT_SCREEN;            \
+    if (sp==0) {                            \
+       struct screen dummy;                \
+       sp = &dummy;                        \
+       memset(sp,0,sizeof(struct screen)); \
+       sp->_outch = _nc_outc_wrapper;      \
+    }\
+    sp->jump = outc
+
+struct DriverTCB; /* Terminal Control Block forward declaration */
+typedef void* TERM_HANDLE;
+
+typedef struct _termInfo
+{
+    bool caninit;
+
+    bool hascolor;
+    bool initcolor;
+    bool canchange;
+
+    int  tabsize;
+
+    int  maxcolors;
+    int  maxpairs;
+    int  nocolorvideo;
+
+    int  numlabels;
+    int  labelwidth;
+    int  labelheight;
+
+    const color_t* defaultPalette;
+} TerminalInfo;
+
+typedef struct term_driver {
+    bool   isTerminfo;
+    bool   (*CanHandle)(struct DriverTCB*,const char*,int*);
+    void   (*init)(struct DriverTCB*);
+    void   (*release)(struct DriverTCB*);
+    int    (*size)(struct DriverTCB*, int* Line, int *Cols);
+    int    (*sgmode)(struct DriverTCB*, bool setFlag, TTY*);
+    chtype (*conattr)(struct DriverTCB*);
+    int    (*hwcur)(struct DriverTCB*, int yold, int xold, int y, int x);
+    int    (*mode)(struct DriverTCB*, bool progFlag, bool defFlag);
+    bool   (*rescol)(struct DriverTCB*);
+    bool   (*rescolors)(struct DriverTCB*);
+    void   (*color)(struct DriverTCB*,bool fore, int color, int(*)(SCREEN*,int));
+    int    (*doBeepOrFlash)(struct DriverTCB*, bool);
+    void   (*initpair)(struct DriverTCB*,short,short,short);
+    void   (*initcolor)(struct DriverTCB*,short,short,short,short);
+    void   (*docolor)(struct DriverTCB*,short,short,bool,int(*)(SCREEN*,int));
+    void   (*initmouse)(struct DriverTCB*);
+    void   (*setfilter)(struct DriverTCB*);
+    void   (*hwlabel)(struct DriverTCB*,int,char*);
+    void   (*hwlabelOnOff)(struct DriverTCB*,bool);
+    int    (*update)(struct DriverTCB*);
+    int    (*defaultcolors)(struct DriverTCB*,int,int);
+    int    (*print)(struct DriverTCB*,char*,int);
+    int    (*getsize)(struct DriverTCB*,int*,int*);
+    int    (*setsize)(struct DriverTCB*,int,int);
+    void   (*initacs)(struct DriverTCB*,chtype*,chtype*);
+    void   (*scinit)(SCREEN *);
+    void   (*scexit)(SCREEN *);
+    int    (*twait)(struct DriverTCB*,int,int,int* EVENTLIST_2nd(_nc_eventlist*));
+    int    (*read)(struct DriverTCB*,int*);
+    int    (*nap)(struct DriverTCB*,int);
+    int    (*kpad)(struct DriverTCB*, bool);
+    int    (*kyOk)(struct DriverTCB*, int, bool);
+    bool   (*kyExist)(struct DriverTCB*, int);
+} TERM_DRIVER;
+
+
+typedef struct DriverTCB
+{
+    TERMINAL      term;   /* needs to be the first Element !!! */
+    TERM_HANDLE   inp;    /* The input handle of the Terminal */
+    TERM_HANDLE   out;    /* The output handle of the Terminal in shell mode */
+    TERM_HANDLE   hdl;    /* The output handle of the Terminal in prog  mode */
+    TERM_DRIVER*  drv;    /* The driver for that Terminal */
+    SCREEN*       csp;    /* The screen that owns that Terminal */
+    TerminalInfo  info;   /* Driver independent core capabilities of the Terminal */
+    void*         prop;   /* Driver dependent property storage to be used by the Driver */
+    long          magic;
+} TERMINAL_CONTROL_BLOCK;
+
+#define NCDRV_MAGIC(id) (0x47110000 | (id&0xffff))
+#define NCDRV_TINFO      0x01
+#define NCDRV_WINCONSOLE 0x02
+
+#define TCBOf(sp)    ((TERMINAL_CONTROL_BLOCK*)(TerminalOf(sp)))
+#define InfoOf(sp)   TCBOf(sp)->info
+#define CallDriver(sp,method)                        TCBOf(sp)->drv->method(TCBOf(sp))
+#define CallDriver_1(sp,method,arg1)                 TCBOf(sp)->drv->method(TCBOf(sp),arg1)
+#define CallDriver_2(sp,method,arg1,arg2)            TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2)
+#define CallDriver_3(sp,method,arg1,arg2,arg3)       TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2,arg3)
+#define CallDriver_4(sp,method,arg1,arg2,arg3,arg4)  TCBOf(sp)->drv->method(TCBOf(sp),arg1,arg2,arg3,arg4)
+
+extern NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette;
+extern NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette;
+
+#ifdef USE_TERM_DRIVER
+extern NCURSES_EXPORT(int)      _nc_tinfo_has_key(SCREEN*, int);
+extern NCURSES_EXPORT(int)      _nc_tinfo_doupdate(SCREEN *sp);
+extern NCURSES_EXPORT(int)      _nc_tinfo_mvcur(SCREEN*,int,int,int,int);
+extern NCURSES_EXPORT(int)      _nc_get_driver(TERMINAL_CONTROL_BLOCK*, const char*, int*);
+#endif
+
+#ifdef __MINGW32__
+#include <nc_mingw.h>
+extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_WIN_DRIVER;
+#endif
+extern NCURSES_EXPORT_VAR(TERM_DRIVER) _nc_TINFO_DRIVER;
+
+#ifdef USE_TERM_DRIVER
+#define IsTermInfo(sp)       (TCBOf(sp) && ((TCBOf(sp)->drv->isTerminfo)))
+#else
+#define IsTermInfo(sp)       TRUE
+#endif
+
+#define HasTInfoTerminal(sp) ((0 != TerminalOf(sp)) && IsTermInfo(sp))
+#define IsValidTIScreen(sp)  (HasTInfoTerminal(sp))
+
+/*
+ * Exported entrypoints beyond the published API
+ */
+#if NCURSES_SP_FUNCS
+extern NCURSES_EXPORT(WINDOW *) _nc_curscr_of(SCREEN*);
+extern NCURSES_EXPORT(WINDOW *) _nc_newscr_of(SCREEN*);
+extern NCURSES_EXPORT(WINDOW *) _nc_stdscr_of(SCREEN*);
+extern NCURSES_EXPORT(int)      _nc_outc_wrapper(SCREEN*,int);
+
+#if USE_REENTRANT
+extern NCURSES_EXPORT(char *)    NCURSES_SP_NAME(_nc_ttytype)(SCREEN*);
+extern NCURSES_EXPORT(int)       NCURSES_SP_NAME(_nc_TABSIZE)(SCREEN*);
+extern NCURSES_EXPORT(char *)    NCURSES_SP_NAME(longname)(SCREEN*);
+#endif
+
+#if NCURSES_EXT_FUNCS
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tabsize)(SCREEN*, int);
+#endif
+
+/*
+ * We put the safe versions of various calls here as they are not published
+ * part of the API up to now
+ */
+extern NCURSES_EXPORT(TERMINAL*) NCURSES_SP_NAME(cur_term)(SCREEN *sp);
+extern NCURSES_EXPORT(WINDOW *) NCURSES_SP_NAME(_nc_makenew) (SCREEN*, int, int, int, int, int);
+extern NCURSES_EXPORT(bool)     NCURSES_SP_NAME(_nc_reset_colors)(SCREEN*);
+extern NCURSES_EXPORT(char *)   NCURSES_SP_NAME(_nc_printf_string)(SCREEN*, const char *, va_list);
+extern NCURSES_EXPORT(chtype)   NCURSES_SP_NAME(_nc_acs_char)(SCREEN*,int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_curs_set)(SCREEN*,int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_get_tty_mode)(SCREEN*,TTY*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_mcprint)(SCREEN*,char*, int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_msec_cost)(SCREEN*, const char *, int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_outch)(SCREEN*, int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp)(SCREEN*, const char *, const char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_putp_flush)(SCREEN*, const char *, const char *);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resetty)(SCREEN*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_resize_term)(SCREEN*,int,int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_ripoffline)(SCREEN*, int, int (*)(WINDOW *,int));
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_savetty)(SCREEN*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_init)(SCREEN*,const char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scr_restore)(SCREEN*, const char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_scrolln)(SCREEN*, int, int, int, int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_set_tty_mode)(SCREEN*, TTY*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_setupscreen)(SCREEN**, int, int, FILE *, bool, int);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tgetent)(SCREEN*,char*,const char *);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_tigetnum)(SCREEN*,NCURSES_CONST char*);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vid_attr)(SCREEN *, attr_t, short, void *);
+extern NCURSES_EXPORT(int)      NCURSES_SP_NAME(_nc_vidputs)(SCREEN*,chtype,int(*) (SCREEN*, int));
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_color)(SCREEN*, short, short, bool, NCURSES_SP_OUTC);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_do_xmc_glitch)(SCREEN*, attr_t);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_flush)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_free_and_exit)(SCREEN*, int) GCC_NORETURN;
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_freeall)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_hash_map)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_init_acs)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_make_oldhash)(SCREEN*, int i);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_init)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_resume)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_mvcur_wrap)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_init)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_resume)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_screen_wrap)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_oldhash)(SCREEN*, int n, int top, int bot);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_scroll_optimize)(SCREEN*);
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_set_buffer)(SCREEN*, FILE *, bool);
+
+extern NCURSES_EXPORT(void)     _nc_cookie_init(SCREEN *sp);
+
+#if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
+extern NCURSES_EXPORT(void)     NCURSES_SP_NAME(_nc_linedump)(SCREEN*);
+#endif
+
+#if USE_WIDEC_SUPPORT
+extern NCURSES_EXPORT(wchar_t *) NCURSES_SP_NAME(_nc_wunctrl)(SCREEN*, cchar_t *);
+#endif
+
+#endif /* NCURSES_SP_FUNCS */
+
+#if NCURSES_SP_FUNCS
+
+#define safe_keyname NCURSES_SP_NAME(keyname)
+#define safe_unctrl  NCURSES_SP_NAME(unctrl)
+#define safe_ungetch NCURSES_SP_NAME(ungetch)
+
+#else
+
+#define safe_keyname _nc_keyname
+#define safe_unctrl  _nc_unctrl
+#define safe_ungetch _nc_ungetch
+
+extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *, int);
+extern NCURSES_EXPORT(int) _nc_ungetch (SCREEN *, int);
+extern NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *, chtype);
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif