+ * If curses.h did not expose the SCREEN-functions, then we do not need the
+ * parameter in the corresponding unextended functions.
+ */
+
+#define USE_SP_FUNC_SUPPORT NCURSES_SP_FUNCS
+#define USE_EXT_SP_FUNC_SUPPORT (NCURSES_SP_FUNCS && NCURSES_EXT_FUNCS)
+
+#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>
+
+/*
+ * Use these macros internally, to make tracing less verbose. But leave the
+ * option for compiling the tracing into the library.
+ */
+#if 1
+#define ColorPair(n) NCURSES_BITS(n, 0)
+#define PairNumber(a) (NCURSES_CAST(int,(((a) & A_COLOR) >> NCURSES_ATTR_SHIFT)))
+#else
+#define ColorPair(pair) COLOR_PAIR(pair)
+#define PairNumber(attr) PAIR_NUMBER(attr)
+#endif
+
+#define unColor(n) unColor2(AttrOf(n))
+#define unColor2(a) ((a) & ALL_BUT_COLOR)
+
+/*
+ * Extended-colors stores the color pair in a separate struct-member than the
+ * attributes. But for compatibility, we handle most cases where a program
+ * written for non-extended colors stores the color in the attributes by
+ * checking for a color pair in both places.
+ */
+#if NCURSES_EXT_COLORS
+#define if_EXT_COLORS(stmt) stmt
+#define SetPair(value,p) SetPair2((value).ext_color, AttrOf(value), p)
+#define SetPair2(c,a,p) c = (p), \
+ a = (unColor2(a) | (A_COLOR & ColorPair(oldColor(c))))
+#define GetPair(value) GetPair2((value).ext_color, AttrOf(value))
+#define GetPair2(c,a) ((c) ? (c) : PairNumber(a))
+#define oldColor(p) (((p) > 255) ? 255 : (p))
+#define GET_WINDOW_PAIR(w) GetPair2((w)->_color, (w)->_attrs)
+#define SET_WINDOW_PAIR(w,p) (w)->_color = (p)
+#define SameAttrOf(a,b) (AttrOf(a) == AttrOf(b) && GetPair(a) == GetPair(b))
+
+#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 */
+
+#define if_EXT_COLORS(stmt) /* nothing */
+#define SetPair(value,p) RemAttr(value, A_COLOR), \
+ SetAttr(value, AttrOf(value) | (A_COLOR & ColorPair(p)))
+#define GetPair(value) PairNumber(AttrOf(value))
+#define GET_WINDOW_PAIR(w) PairNumber(WINDOW_ATTRS(w))
+#define SET_WINDOW_PAIR(w,p) WINDOW_ATTRS(w) &= ALL_BUT_COLOR, \
+ WINDOW_ATTRS(w) |= (A_COLOR & ColorPair(p))
+#define SameAttrOf(a,b) (AttrOf(a) == AttrOf(b))
+
+#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 */
+
+#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)
+
+#define SCREEN_ATTRS(s) (*((s)->_current_attr))
+#define GET_SCREEN_PAIR(s) GetPair(SCREEN_ATTRS(s))
+#define SET_SCREEN_PAIR(s,p) SetPair(SCREEN_ATTRS(s), p)
+
+#if USE_REENTRANT || NCURSES_SP_FUNCS
+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 *);
+#endif
+
+#if USE_REENTRANT
+
+#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 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)
+
+#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_REENTRANT
+#include <pthread.h>
+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
+
+#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 */
+
+#if USE_PTHREADS_EINTR
+# if USE_WEAK_SYMBOLS
+#include <pthread.h>
+weak_symbol(pthread_sigmask);
+weak_symbol(pthread_kill);
+weak_symbol(pthread_self);
+weak_symbol(pthread_equal);
+extern NCURSES_EXPORT(int) _nc_sigprocmask(int, const sigset_t *, sigset_t *);
+# undef sigprocmask
+# define sigprocmask _nc_sigprocmask
+# endif
+#endif /* USE_PTHREADS_EINTR */
+
+#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 */
+
+#endif /* USE_PTHREADS */
+
+/*
+ * When using sp-funcs, locks are targeted to SCREEN-level granularity.
+ * So the locking is done in the non-sp-func (which calls the sp-func) rather
+ * than in the sp-func itself.
+ *
+ * Use the _nc_nonsp_XXX functions in the function using "NCURSES_SP_NAME()".
+ * Use the _nc_sp_XXX functions in the function using "#if NCURSES_SP_FUNCS".
+ */
+#if NCURSES_SP_FUNCS
+
+#define _nc_nonsp_lock_global(name) /* nothing */
+#define _nc_nonsp_try_global(name) 0
+#define _nc_nonsp_unlock_global(name) /* nothing */
+
+#define _nc_sp_lock_global(name) _nc_lock_global(name)
+#define _nc_sp_try_global(name) _nc_try_global(name)
+#define _nc_sp_unlock_global(name) _nc_unlock_global(name)
+
+#else
+
+#define _nc_nonsp_lock_global(name) _nc_lock_global(name)
+#define _nc_nonsp_try_global(name) _nc_try_global(name)
+#define _nc_nonsp_unlock_global(name) _nc_unlock_global(name)
+
+#define _nc_sp_lock_global(name) /* nothing */
+#define _nc_sp_try_global(name) 0
+#define _nc_sp_unlock_global(name) /* nothing */
+
+#endif
+
+#if HAVE_GETTIMEOFDAY
+# define PRECISE_GETTIME 1
+# define TimeType struct timeval
+#else
+# define PRECISE_GETTIME 0
+# define TimeType time_t
+#endif
+
+/*
+ * Definitions for color pairs