Home | History | Annotate | Line # | Download | only in libcurses
curses_private.h revision 1.69
      1 /*	$NetBSD: curses_private.h,v 1.69 2019/04/01 11:39:15 roy Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-2000 Brett Lymn
      5  *                         (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
      6  * All rights reserved.
      7  *
      8  * This code has been donated to The NetBSD Foundation by the Author.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *
     30  */
     31 
     32 /* Modified by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>
     33  * to add support for wide characters
     34  * Changes:
     35  * - Add a compiler variable HAVE_WCHAR for wide character only code
     36  * - Add a pointer to liked list of non-spacing characters in __ldata
     37  *   and the macro to access the width field in the attribute field
     38  * - Add a circular input character buffer in __screen to handle
     39  *   wide-character input (used in get_wch())
     40  */
     41 
     42 #include <term.h>
     43 #include <termios.h>
     44 
     45 /* Private structure definitions for curses. */
     46 
     47 /* Termcap capabilities. */
     48 #ifdef HAVE_WCHAR
     49 /*
     50  * Add a list of non-spacing characters to each spacing
     51  * character in a singly linked list
     52  */
     53 typedef struct nschar_t {
     54 	wchar_t			ch;		/* Non-spacing character */
     55 	struct nschar_t	*next;	/* Next non-spacing character */
     56 } nschar_t;
     57 #endif /* HAVE_WCHAR */
     58 
     59 /*
     60  * A window is an array of __LINE structures pointed to by the 'lines' pointer.
     61  * A line is an array of __LDATA structures pointed to by the 'line' pointer.
     62  *
     63  * IMPORTANT: the __LDATA structure must NOT induce any padding, so if new
     64  * fields are added -- padding fields with *constant values* should ensure
     65  * that the compiler will not generate any padding when storing an array of
     66  *  __LDATA structures.  This is to enable consistent use of memcmp, and memcpy
     67  * for comparing and copying arrays.
     68  */
     69 
     70 struct __ldata {
     71 	wchar_t	ch;			/* Character */
     72 	attr_t	attr;			/* Attributes */
     73 #ifdef HAVE_WCHAR
     74 	nschar_t	*nsp;	/* Foreground non-spacing character pointer */
     75 #endif /* HAVE_WCHAR */
     76 };
     77 
     78 #ifdef HAVE_WCHAR
     79 /* macros to extract the width of a wide character */
     80 #define __WCWIDTH 0xfc000000
     81 #define WCW_SHIFT 26
     82 #define WCOL(wc) ((((unsigned) (wc).attr) >> WCW_SHIFT ) > MB_LEN_MAX ? ((int)(((unsigned) (wc).attr ) >> WCW_SHIFT )) - 64 : ((int)(((unsigned) (wc).attr ) >> WCW_SHIFT)))
     83 #define SET_WCOL(c, w) do { 						\
     84 	((c).attr) = ((((c).attr) & WA_ATTRIBUTES ) | ((w) << WCW_SHIFT )); \
     85 } while(/*CONSTCOND*/0)
     86 #define BGWCOL(wc) ((((wc).battr) >> WCW_SHIFT ) > MB_LEN_MAX ? (((wc).battr ) >> WCW_SHIFT ) - 64 : (((wc).battr ) >> WCW_SHIFT ))
     87 #define SET_BGWCOL(c, w) do { 						\
     88 	((c).battr) = ((((c).battr) & WA_ATTRIBUTES ) | ((w) << WCW_SHIFT )); \
     89 } while(/*CONSTCOND*/0)
     90 #endif /* HAVE_WCHAR */
     91 
     92 #define __LDATASIZE	(sizeof(__LDATA))
     93 
     94 struct __line {
     95 #ifdef DEBUG
     96 #define SENTINEL_VALUE 0xaac0ffee
     97 
     98 	unsigned int sentinel;          /* try to catch line overflows */
     99 #endif
    100 #define	__ISDIRTY	0x01		/* Line is dirty. */
    101 #define __ISPASTEOL	0x02		/* Cursor is past end of line */
    102 #define __ISFORCED	0x04		/* Force update, no optimisation */
    103 	unsigned int flags;
    104 	unsigned int hash;		/* Hash value for the line. */
    105 	int *firstchp, *lastchp;	/* First and last chngd columns ptrs */
    106 	int firstch, lastch;		/* First and last changed columns. */
    107 	__LDATA *line;			/* Pointer to the line text. */
    108 };
    109 
    110 struct __window {		/* Window structure. */
    111 	struct __window	*nextp, *orig;	/* Subwindows list and parent. */
    112 	int begy, begx;			/* Window home. */
    113 	int cury, curx;			/* Current x, y coordinates. */
    114 	int maxy, maxx;			/* Maximum values for curx, cury. */
    115 	int reqy, reqx;			/* Size requested when created */
    116 	int ch_off;			/* x offset for firstch/lastch. */
    117 	__LINE **alines;		/* Array of pointers to the lines */
    118 	__LINE  *lspace;		/* line space (for cleanup) */
    119 	__LDATA *wspace;		/* window space (for cleanup) */
    120 
    121 #define	__ENDLINE	0x00000001	/* End of screen. */
    122 #define	__FLUSH		0x00000002	/* Fflush(stdout) after refresh. */
    123 #define	__FULLWIN	0x00000004	/* Window is a screen. */
    124 #define	__IDLINE	0x00000008	/* Insert/delete sequences. */
    125 #define	__SCROLLWIN	0x00000010	/* Last char will scroll window. */
    126 #define	__SCROLLOK	0x00000020	/* Scrolling ok. */
    127 #define	__CLEAROK	0x00000040	/* Clear on next refresh. */
    128 #define	__LEAVEOK	0x00000100	/* If cursor left */
    129 #define	__KEYPAD	0x00010000	/* If interpreting keypad codes */
    130 #define	__NOTIMEOUT	0x00020000	/* Wait indefinitely for func keys */
    131 #define __IDCHAR	0x00040000	/* insert/delete char sequences */
    132 #define __ISPAD		0x00080000	/* "window" is a pad */
    133 #define __ISDERWIN	0x00100000	/* "window" is derived from parent */
    134 #define __IMMEDOK	0x00200000	/* refreshed when changed */
    135 #define __SYNCOK	0x00400000	/* sync when changed */
    136 #define __HALFDELAY	0x00800000	/* In half delay mode */
    137 	unsigned int flags;
    138 	int	delay;			/* delay for getch() */
    139 	attr_t	wattr;			/* Character attributes */
    140 	wchar_t	bch;			/* Background character */
    141 	attr_t	battr;			/* Background attributes */
    142 	int	scr_t, scr_b;		/* Scrolling region top, bottom */
    143 	SCREEN	*screen;		/* Screen for this window */
    144 	int	pbegy, pbegx,
    145 		sbegy, sbegx,
    146 		smaxy, smaxx;		/* Saved prefresh() values */
    147 	int	dery, derx;		/* derived window coordinates
    148 					   - top left corner of source
    149 					   relative to parent win */
    150 #ifdef HAVE_WCHAR
    151 	nschar_t *bnsp;			/* Background non-spacing char list */
    152 #endif /* HAVE_WCHAR */
    153 	FILE	*fp;			/* for window formatted printf */
    154 	char	*buf;			/* buffer for window formatted printf */
    155 	size_t	 buflen;		/* length of above buffer */
    156 };
    157 
    158 /* Set of attributes unset by 'me' - 'mb', 'md', 'mh', 'mk', 'mp' and 'mr'. */
    159 #ifndef HAVE_WCHAR
    160 #define	__TERMATTR \
    161 	(__REVERSE | __BLINK | __DIM | __BOLD | __BLANK | __PROTECT)
    162 #else
    163 #define	__TERMATTR \
    164 	(__REVERSE | __BLINK | __DIM | __BOLD | __BLANK | __PROTECT \
    165 	| WA_TOP | WA_LOW | WA_LEFT | WA_RIGHT | WA_HORIZONTAL | WA_VERTICAL)
    166 #endif /* HAVE_WCHAR */
    167 
    168 struct __winlist {
    169 	struct __window		*winp;	/* The window. */
    170 	struct __winlist	*nextp;	/* Next window. */
    171 };
    172 
    173 struct __color {
    174 	short	num;
    175 	short	red;
    176 	short	green;
    177 	short	blue;
    178 	int	flags;
    179 };
    180 
    181 /* List of colour pairs */
    182 struct __pair {
    183 	short	fore;
    184 	short	back;
    185 	int	flags;
    186 };
    187 
    188 /* Maximum colours */
    189 #define	MAX_COLORS	256
    190 /* Maximum colour pairs - determined by number of colour bits in attr_t */
    191 #define	MAX_PAIRS	PAIR_NUMBER(__COLOR)
    192 
    193 typedef struct keymap keymap_t;
    194 
    195 /* POSIX allows up to 8 columns in a label. */
    196 #define	MAX_SLK_COLS	8
    197 #ifdef HAVE_WCHAR
    198 #define	MAX_SLK_LABEL	sizeof(wchar_t) * MAX_SLK_COLS
    199 #else
    200 #define	MAX_SLK_LABEL	MAX_SLK_COLS
    201 #endif
    202 struct __slk_label {
    203 	char	*text;
    204 	int	 justify;
    205 #define	SLK_JUSTIFY_LEFT	0
    206 #define	SLK_JUSTIFY_CENTER	1
    207 #define	SLK_JUSTIFY_RIGHT	2
    208 	char	 label[MAX_SLK_LABEL + 1];
    209 	int	 x;
    210 };
    211 
    212 #define	MAX_RIPS	5
    213 struct __ripoff {
    214 	int	nlines;
    215 	WINDOW	*win;
    216 };
    217 
    218 /* this is the encapsulation of the terminal definition, one for
    219  * each terminal that curses talks to.
    220  */
    221 struct __screen {
    222 	FILE    *infd, *outfd;  /* input and output file descriptors */
    223 	WINDOW	*curscr;	/* Current screen. */
    224 	WINDOW	*stdscr;	/* Standard screen. */
    225 	WINDOW	*__virtscr;	/* Virtual screen (for doupdate()). */
    226 	int      curwin;        /* current window for refresh */
    227 	int      lx, ly;        /* loop parameters for refresh */
    228 	int	 COLS;		/* Columns on the screen. */
    229 	int	 LINES;		/* Lines on the screen. */
    230 	int	 nripped;	/* Number of ripofflines. */
    231 	struct __ripoff ripped[MAX_RIPS];	/* ripofflines. */
    232 	int	 ESCDELAY;	/* Delay between keys in esc seq's. */
    233 #define	ESCDELAY_DEFAULT	300 /* milliseconds. */
    234 	int	 TABSIZE;	/* Size of a tab. */
    235 #define	TABSIZE_DEFAULT		8   /* spaces. */
    236 	int	 COLORS;	/* Maximum colors on the screen */
    237 	int	 COLOR_PAIRS;	/* Maximum color pairs on the screen */
    238 	int	 My_term;	/* Use Def_term regardless. */
    239 	char	 GT;		/* Gtty indicates tabs. */
    240 	char	 NONL;		/* Term can't hack LF doing a CR. */
    241 	char	 UPPERCASE;	/* Terminal is uppercase only. */
    242 
    243 	chtype acs_char[NUM_ACS];
    244 #ifdef HAVE_WCHAR
    245 	cchar_t wacs_char[ NUM_ACS ];
    246 #endif /* HAVE_WCHAR */
    247 	struct __color colours[MAX_COLORS];
    248 	struct __pair  colour_pairs[MAX_PAIRS];
    249 	attr_t	nca;
    250 
    251 /* Style of colour manipulation */
    252 #define COLOR_NONE	0
    253 #define COLOR_ANSI	1	/* ANSI/DEC-style colour manipulation */
    254 #define COLOR_HP	2	/* HP-style colour manipulation */
    255 #define COLOR_TEK	3	/* Tektronix-style colour manipulation */
    256 #define COLOR_OTHER	4	/* None of the others but can set fore/back */
    257 	int color_type;
    258 
    259 	attr_t mask_op;
    260 	attr_t mask_me;
    261 	attr_t mask_ue;
    262 	attr_t mask_se;
    263 	TERMINAL *term;
    264 	int old_mode; /* old cursor visibility state for terminal */
    265 	keymap_t *base_keymap;
    266 	int echoit;
    267 	int pfast;
    268 	int rawmode;
    269 	int nl;
    270 	int noqch;
    271 	int clearok;
    272 	int useraw;
    273 	struct __winlist *winlistp;
    274 	struct   termios cbreakt, rawt, *curt, save_termios;
    275 	struct termios orig_termios, baset, savedtty;
    276 	int ovmin;
    277 	int ovtime;
    278 	char *stdbuf;
    279 	unsigned int len;
    280 	int meta_state;
    281 	char padchar;
    282 	int endwin;
    283 	int notty;
    284 	int resized;
    285 	wchar_t *unget_list;
    286 	int unget_len, unget_pos;
    287 	int filtered;
    288 	int checkfd;
    289 
    290 	/* soft label key */
    291 	bool		 is_term_slk;
    292 	WINDOW		*slk_window;
    293 	int		 slk_format;
    294 #define	SLK_FMT_INVAL	-1
    295 #define	SLK_FMT_3_2_3	0
    296 #define	SLK_FMT_4_4	1
    297 	int		 slk_nlabels;
    298 	int		 slk_label_len;
    299 	bool		 slk_hidden;
    300 	struct __slk_label *slk_labels;
    301 
    302 #ifdef HAVE_WCHAR
    303 #define MB_LEN_MAX 8
    304 #define MAX_CBUF_SIZE MB_LEN_MAX
    305 	int		cbuf_head;		/* header to cbuf */
    306 	int		cbuf_tail;		/* tail to cbuf */
    307 	int		cbuf_cur;		/* the current char in cbuf */
    308 	mbstate_t	sp;			/* wide char processing state */
    309 	char		cbuf[ MAX_CBUF_SIZE ];	/* input character buffer */
    310 #endif /* HAVE_WCHAR */
    311 };
    312 
    313 
    314 extern char	 __GT;			/* Gtty indicates tabs. */
    315 extern char	 __NONL;		/* Term can't hack LF doing a CR. */
    316 extern char	 __UPPERCASE;		/* Terminal is uppercase only. */
    317 extern int	 My_term;		/* Use Def_term regardless. */
    318 extern const char	*Def_term;	/* Default terminal type. */
    319 extern SCREEN   *_cursesi_screen;       /* The current screen in use */
    320 
    321 /* Debugging options/functions. */
    322 #ifdef DEBUG
    323 #define __CTRACE_TSTAMP		0x00000001
    324 #define __CTRACE_MISC		0x00000002
    325 #define __CTRACE_INIT		0x00000004
    326 #define __CTRACE_SCREEN		0x00000008
    327 #define __CTRACE_WINDOW		0x00000010
    328 #define __CTRACE_REFRESH	0x00000020
    329 #define __CTRACE_COLOR		0x00000040
    330 #define __CTRACE_INPUT		0x00000080
    331 #define __CTRACE_OUTPUT		0x00000100
    332 #define __CTRACE_LINE		0x00000200
    333 #define __CTRACE_ATTR		0x00000400
    334 #define __CTRACE_ERASE		0x00000800
    335 #define __CTRACE_FILEIO		0x00001000
    336 #define __CTRACE_ALL		0x7fffffff
    337 void	 __CTRACE(int, const char *, ...) __attribute__((__format__(__printf__, 2, 3)));
    338 #endif
    339 
    340 /* Private functions. */
    341 int     __cputchar_args(int, void *);
    342 void     _cursesi_free_keymap(keymap_t *);
    343 int      _cursesi_gettmode(SCREEN *);
    344 void     _cursesi_reset_acs(SCREEN *);
    345 int	_cursesi_addbyte(WINDOW *, __LINE **, int *, int *, int , attr_t, int);
    346 int	_cursesi_addwchar(WINDOW *, __LINE **, int *, int *, const cchar_t *,
    347 			  int);
    348 int	_cursesi_waddbytes(WINDOW *, const char *, int, attr_t, int);
    349 #ifdef HAVE_WCHAR
    350 void     _cursesi_reset_wacs(SCREEN *);
    351 #endif /* HAVE_WCHAR */
    352 void     _cursesi_resetterm(SCREEN *);
    353 int      _cursesi_setterm(char *, SCREEN *);
    354 int	 __delay(void);
    355 unsigned int	 __hash_more(const void *, size_t, unsigned int);
    356 #define	__hash(s, len)	__hash_more((s), (len), 0u)
    357 void	 __id_subwins(WINDOW *);
    358 void	 __init_getch(SCREEN *);
    359 void	 __init_acs(SCREEN *);
    360 #ifdef HAVE_WCHAR
    361 void	 __init_get_wch(SCREEN *);
    362 void	 __init_wacs(SCREEN *);
    363 int	__cputwchar_args( wchar_t, void * );
    364 int     _cursesi_copy_nsp(nschar_t *, struct __ldata *);
    365 void	__cursesi_free_nsp(nschar_t *);
    366 void	__cursesi_win_free_nsp(WINDOW *);
    367 void	__cursesi_putnsp(nschar_t *, const int, const int);
    368 void	__cursesi_chtype_to_cchar(chtype, cchar_t *);
    369 #endif /* HAVE_WCHAR */
    370 int	 __fgetc_resize(FILE *);
    371 int	 __unget(wint_t);
    372 int	 __mvcur(int, int, int, int, int);
    373 WINDOW  *__newwin(SCREEN *, int, int, int, int, int, int);
    374 int	 __nodelay(void);
    375 int	 __notimeout(void);
    376 void	 __restartwin(void);
    377 void	 __restore_colors(void);
    378 void     __restore_cursor_vis(void);
    379 void     __restore_meta_state(void);
    380 void	 __restore_termios(void);
    381 void	 __restore_stophandler(void);
    382 void	 __restore_winchhandler(void);
    383 int	 __ripoffscreen(SCREEN *);
    384 int	 __ripoffresize(SCREEN *);
    385 void	 __ripofftouch(SCREEN *);
    386 int	 __rippedlines(const SCREEN *, int);
    387 void	 __save_termios(void);
    388 void	 __set_color(WINDOW *win, attr_t attr);
    389 void	 __set_stophandler(void);
    390 void	 __set_winchhandler(void);
    391 void	 __set_subwin(WINDOW *, WINDOW *);
    392 int	 __slk_init(SCREEN *);
    393 void	 __slk_free(SCREEN *);
    394 int	 __slk_resize(SCREEN *, int cols);
    395 int	 __slk_noutrefresh(SCREEN *);
    396 void	 __startwin(SCREEN *);
    397 void	 __stop_signal_handler(int);
    398 int	 __stopwin(void);
    399 void	 __swflags(WINDOW *);
    400 void	 __sync(WINDOW *);
    401 int	 __timeout(int);
    402 int	 __touchline(WINDOW *, int, int, int);
    403 int	 __touchwin(WINDOW *);
    404 int	 __unripoffline(int (*)(WINDOW *, int));
    405 void	 __unsetattr(int);
    406 void	 __unset_color(WINDOW *win);
    407 int	 __waddch(WINDOW *, __LDATA *);
    408 int	 __wgetnstr(WINDOW *, char *, int);
    409 void	 __winch_signal_handler(int);
    410 
    411 /* Private #defines. */
    412 #define	min(a,b)	((a) < (b) ? (a) : (b))
    413 #define	max(a,b)	((a) > (b) ? (a ): (b))
    414 
    415 /* Private externs. */
    416 extern int		 __echoit;
    417 extern int		 __endwin;
    418 extern int		 __pfast;
    419 extern int		 __rawmode;
    420 extern int		 __noqch;
    421 extern attr_t		 __mask_op, __mask_me, __mask_ue, __mask_se;
    422 extern WINDOW		*__virtscr;
    423 extern int		 __using_color;
    424 extern attr_t		 __default_color;
    425