Home | History | Annotate | Line # | Download | only in libcurses
curses_private.h revision 1.77
      1 /*	$NetBSD: curses_private.h,v 1.77 2022/01/25 03:05:06 blymn 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 #define WCA_CONTINUATION	0x0001	/* a continuation cell */
     76 	int16_t		wflags;		/* internal attributes for wide char */
     77 	int16_t		wcols;		/* display width of a wide char */
     78 #endif /* HAVE_WCHAR */
     79 };
     80 
     81 #define __LDATASIZE	(sizeof(__LDATA))
     82 
     83 struct __line {
     84 #ifdef DEBUG
     85 #define SENTINEL_VALUE 0xaac0ffee
     86 
     87 	unsigned int sentinel;          /* try to catch line overflows */
     88 #endif
     89 #define	__ISDIRTY	0x01		/* Line is dirty. */
     90 #define __ISPASTEOL	0x02		/* Cursor is past end of line */
     91 #define __ISFORCED	0x04		/* Force update, no optimisation */
     92 	unsigned int flags;
     93 	unsigned int hash;		/* Hash value for the line. */
     94 	int *firstchp, *lastchp;	/* First and last chngd columns ptrs */
     95 	int firstch, lastch;		/* First and last changed columns. */
     96 	__LDATA *line;			/* Pointer to the line text. */
     97 };
     98 
     99 struct __window {		/* Window structure. */
    100 	struct __window	*nextp, *orig;	/* Subwindows list and parent. */
    101 	int begy, begx;			/* Window home. */
    102 	int cury, curx;			/* Current x, y coordinates. */
    103 	int maxy, maxx;			/* Maximum values for curx, cury. */
    104 	int reqy, reqx;			/* Size requested when created */
    105 	int ch_off;			/* x offset for firstch/lastch. */
    106 	__LINE **alines;		/* Array of pointers to the lines */
    107 	__LINE  *lspace;		/* line space (for cleanup) */
    108 	__LDATA *wspace;		/* window space (for cleanup) */
    109 
    110 #define	__ENDLINE	0x00000001	/* End of screen. */
    111 #define	__FLUSH		0x00000002	/* Fflush(stdout) after refresh. */
    112 #define	__FULLWIN	0x00000004	/* Window is a screen. */
    113 #define	__IDLINE	0x00000008	/* Insert/delete sequences. */
    114 #define	__SCROLLWIN	0x00000010	/* Last char will scroll window. */
    115 #define	__SCROLLOK	0x00000020	/* Scrolling ok. */
    116 #define	__CLEAROK	0x00000040	/* Clear on next refresh. */
    117 #define	__LEAVEOK	0x00000100	/* If cursor left */
    118 #define	__KEYPAD	0x00010000	/* If interpreting keypad codes */
    119 #define	__NOTIMEOUT	0x00020000	/* Wait indefinitely for func keys */
    120 #define __IDCHAR	0x00040000	/* insert/delete char sequences */
    121 #define __ISPAD		0x00080000	/* "window" is a pad */
    122 #define __ISDERWIN	0x00100000	/* "window" is derived from parent */
    123 #define __IMMEDOK	0x00200000	/* refreshed when changed */
    124 #define __SYNCOK	0x00400000	/* sync when changed */
    125 #define __HALFDELAY	0x00800000	/* In half delay mode */
    126 	unsigned int flags;
    127 	int	delay;			/* delay for getch() */
    128 	attr_t	wattr;			/* Character attributes */
    129 	wchar_t	bch;			/* Background character */
    130 	attr_t	battr;			/* Background attributes */
    131 	uint32_t wcols;			/* Background column width */
    132 	int	scr_t, scr_b;		/* Scrolling region top, bottom */
    133 	SCREEN	*screen;		/* Screen for this window */
    134 	int	pbegy, pbegx,
    135 		sbegy, sbegx,
    136 		smaxy, smaxx;		/* Saved prefresh() values */
    137 	int	dery, derx;		/* derived window coordinates
    138 					   - top left corner of source
    139 					   relative to parent win */
    140 #ifdef HAVE_WCHAR
    141 	nschar_t *bnsp;			/* Background non-spacing char list */
    142 #endif /* HAVE_WCHAR */
    143 	FILE	*fp;			/* for window formatted printf */
    144 	char	*buf;			/* buffer for window formatted printf */
    145 	size_t	 buflen;		/* length of above buffer */
    146 };
    147 
    148 /* Set of attributes unset by 'me' - 'mb', 'md', 'mh', 'mk', 'mp' and 'mr'. */
    149 #ifndef HAVE_WCHAR
    150 #define	__TERMATTR \
    151 	(__REVERSE | __BLINK | __DIM | __BOLD | __BLANK | __PROTECT)
    152 #else
    153 #define	__TERMATTR \
    154 	(__REVERSE | __BLINK | __DIM | __BOLD | __BLANK | __PROTECT \
    155 	| WA_TOP | WA_LOW | WA_LEFT | WA_RIGHT | WA_HORIZONTAL | WA_VERTICAL)
    156 #endif /* HAVE_WCHAR */
    157 
    158 struct __winlist {
    159 	struct __window		*winp;	/* The window. */
    160 	struct __winlist	*nextp;	/* Next window. */
    161 };
    162 
    163 struct __color {
    164 	short	num;
    165 	short	red;
    166 	short	green;
    167 	short	blue;
    168 	int	flags;
    169 };
    170 
    171 /* List of colour pairs */
    172 struct __pair {
    173 	short	fore;
    174 	short	back;
    175 	int	flags;
    176 };
    177 
    178 /* Maximum colours */
    179 #define	MAX_COLORS	256
    180 /* Maximum colour pairs - determined by number of colour bits in attr_t */
    181 #define	MAX_PAIRS	PAIR_NUMBER(__COLOR)
    182 
    183 typedef struct keymap keymap_t;
    184 
    185 /* POSIX allows up to 8 columns in a label. */
    186 #define	MAX_SLK_COLS	8
    187 #ifdef HAVE_WCHAR
    188 #define	MAX_SLK_LABEL	sizeof(wchar_t) * MAX_SLK_COLS
    189 #else
    190 #define	MAX_SLK_LABEL	MAX_SLK_COLS
    191 #endif
    192 struct __slk_label {
    193 	char	*text;
    194 	int	 justify;
    195 #define	SLK_JUSTIFY_LEFT	0
    196 #define	SLK_JUSTIFY_CENTER	1
    197 #define	SLK_JUSTIFY_RIGHT	2
    198 	char	 label[MAX_SLK_LABEL + 1];
    199 	int	 x;
    200 };
    201 
    202 #define	MAX_RIPS	5
    203 struct __ripoff {
    204 	int	nlines;
    205 	WINDOW	*win;
    206 };
    207 
    208 /* this is the encapsulation of the terminal definition, one for
    209  * each terminal that curses talks to.
    210  */
    211 struct __screen {
    212 	FILE    *infd, *outfd;  /* input and output file descriptors */
    213 	WINDOW	*curscr;	/* Current screen. */
    214 	WINDOW	*stdscr;	/* Standard screen. */
    215 	WINDOW	*__virtscr;	/* Virtual screen (for doupdate()). */
    216 	int      curwin;        /* current window for refresh */
    217 	int      lx, ly;        /* loop parameters for refresh */
    218 	int	 COLS;		/* Columns on the screen. */
    219 	int	 LINES;		/* Lines on the screen. */
    220 	int	 nripped;	/* Number of ripofflines. */
    221 	struct __ripoff ripped[MAX_RIPS];	/* ripofflines. */
    222 	int	 ESCDELAY;	/* Delay between keys in esc seq's. */
    223 #define	ESCDELAY_DEFAULT	300 /* milliseconds. */
    224 	int	 TABSIZE;	/* Size of a tab. */
    225 #define	TABSIZE_DEFAULT		8   /* spaces. */
    226 	int	 COLORS;	/* Maximum colors on the screen */
    227 	int	 COLOR_PAIRS;	/* Maximum color pairs on the screen */
    228 	int	 My_term;	/* Use Def_term regardless. */
    229 	char	 GT;		/* Gtty indicates tabs. */
    230 	char	 NONL;		/* Term can't hack LF doing a CR. */
    231 	char	 UPPERCASE;	/* Terminal is uppercase only. */
    232 
    233 	chtype acs_char[NUM_ACS];
    234 #ifdef HAVE_WCHAR
    235 	cchar_t wacs_char[ NUM_ACS ];
    236 #endif /* HAVE_WCHAR */
    237 	struct __color colours[MAX_COLORS];
    238 	struct __pair  colour_pairs[MAX_PAIRS];
    239 	attr_t	nca;
    240 
    241 /* Style of colour manipulation */
    242 #define COLOR_NONE	0
    243 #define COLOR_ANSI	1	/* ANSI/DEC-style colour manipulation */
    244 #define COLOR_HP	2	/* HP-style colour manipulation */
    245 #define COLOR_TEK	3	/* Tektronix-style colour manipulation */
    246 #define COLOR_OTHER	4	/* None of the others but can set fore/back */
    247 	int color_type;
    248 
    249 	attr_t mask_op;
    250 	attr_t mask_me;
    251 	attr_t mask_ue;
    252 	attr_t mask_se;
    253 	TERMINAL *term;
    254 	int old_mode; /* old cursor visibility state for terminal */
    255 	keymap_t *base_keymap;
    256 	int echoit;
    257 	int pfast;
    258 	int rawmode;
    259 	int nl;
    260 	int noqch;
    261 	int clearok;
    262 	int useraw;
    263 	struct __winlist *winlistp;
    264 	struct   termios cbreakt, rawt, *curt, save_termios;
    265 	struct termios orig_termios, baset, savedtty;
    266 	int ovmin;
    267 	int ovtime;
    268 	char *stdbuf;
    269 	unsigned int len;
    270 	int meta_state;
    271 	char padchar;
    272 	int endwin;
    273 	int notty;
    274 	int resized;
    275 	wchar_t *unget_list;
    276 	int unget_len, unget_pos;
    277 	int filtered;
    278 	int checkfd;
    279 
    280 	/* soft label key */
    281 	bool		 is_term_slk;
    282 	WINDOW		*slk_window;
    283 	int		 slk_format;
    284 #define	SLK_FMT_INVAL	-1
    285 #define	SLK_FMT_3_2_3	0
    286 #define	SLK_FMT_4_4	1
    287 	int		 slk_nlabels;
    288 	int		 slk_label_len;
    289 	bool		 slk_hidden;
    290 	struct __slk_label *slk_labels;
    291 
    292 #define MAX_CBUF_SIZE 8
    293 #ifdef HAVE_WCHAR
    294 	int		cbuf_head;		/* header to cbuf */
    295 	int		cbuf_tail;		/* tail to cbuf */
    296 	int		cbuf_cur;		/* the current char in cbuf */
    297 	mbstate_t	sp;			/* wide char processing state */
    298 	char		cbuf[ MAX_CBUF_SIZE ];	/* input character buffer */
    299 #endif /* HAVE_WCHAR */
    300 };
    301 
    302 
    303 extern char	 __GT;			/* Gtty indicates tabs. */
    304 extern char	 __NONL;		/* Term can't hack LF doing a CR. */
    305 extern char	 __UPPERCASE;		/* Terminal is uppercase only. */
    306 extern int	 My_term;		/* Use Def_term regardless. */
    307 extern const char	*Def_term;	/* Default terminal type. */
    308 extern SCREEN   *_cursesi_screen;       /* The current screen in use */
    309 
    310 /* Debugging options/functions. */
    311 #ifdef DEBUG
    312 #define __CTRACE_TSTAMP		0x00000001
    313 #define __CTRACE_MISC		0x00000002
    314 #define __CTRACE_INIT		0x00000004
    315 #define __CTRACE_SCREEN		0x00000008
    316 #define __CTRACE_WINDOW		0x00000010
    317 #define __CTRACE_REFRESH	0x00000020
    318 #define __CTRACE_COLOR		0x00000040
    319 #define __CTRACE_INPUT		0x00000080
    320 #define __CTRACE_OUTPUT		0x00000100
    321 #define __CTRACE_LINE		0x00000200
    322 #define __CTRACE_ATTR		0x00000400
    323 #define __CTRACE_ERASE		0x00000800
    324 #define __CTRACE_FILEIO		0x00001000
    325 #define __CTRACE_ALL		0x7fffffff
    326 void	 __CTRACE(int, const char *, ...) __attribute__((__format__(__printf__, 2, 3)));
    327 #else
    328 #define	__CTRACE(area, fmt, ...)	__nothing
    329 #endif
    330 
    331 /* Common erase logic */
    332 #ifdef HAVE_WCHAR
    333 #define __NEED_ERASE(_sp, _bch, _battr)				\
    334 	((_sp)->ch != (_bch) ||					\
    335 	    ((_sp)->attr & WA_ATTRIBUTES) != (_battr) ||	\
    336 	    (_sp)->nsp != NULL ||				\
    337 	    (_sp)->wcols < 0)
    338 #else
    339 #define __NEED_ERASE(_sp, _bch, _battr)				\
    340 	((_sp)->ch != (_bch) || (_sp)->attr != (_battr))
    341 #endif
    342 
    343 /* Private functions. */
    344 int     __cputchar_args(int, void *);
    345 void     _cursesi_free_keymap(keymap_t *);
    346 int      _cursesi_gettmode(SCREEN *);
    347 void     _cursesi_reset_acs(SCREEN *);
    348 int	_cursesi_addbyte(WINDOW *, __LINE **, int *, int *, int , attr_t, int);
    349 int	_cursesi_addwchar(WINDOW *, __LINE **, int *, int *, const cchar_t *,
    350 			  int);
    351 int	_cursesi_waddbytes(WINDOW *, const char *, int, attr_t, int);
    352 #ifdef HAVE_WCHAR
    353 void     _cursesi_reset_wacs(SCREEN *);
    354 #endif /* HAVE_WCHAR */
    355 void     _cursesi_resetterm(SCREEN *);
    356 int      _cursesi_setterm(char *, SCREEN *);
    357 int	 __delay(void);
    358 unsigned int	 __hash_more(const void *, size_t, unsigned int);
    359 unsigned int	 __hash_line(const __LDATA *, int);
    360 #define	__hash(s, len)	__hash_more((s), (len), 0u)
    361 void	 __id_subwins(WINDOW *);
    362 void	 __init_getch(SCREEN *);
    363 void	 __init_acs(SCREEN *);
    364 #ifdef HAVE_WCHAR
    365 void	 __init_get_wch(SCREEN *);
    366 void	 __init_wacs(SCREEN *);
    367 int	__cputwchar_args( wchar_t, void * );
    368 int     _cursesi_copy_nsp(nschar_t *, struct __ldata *);
    369 void	__cursesi_free_nsp(nschar_t *);
    370 void	__cursesi_win_free_nsp(WINDOW *);
    371 void	__cursesi_putnsp(nschar_t *, const int, const int);
    372 void	__cursesi_chtype_to_cchar(chtype, cchar_t *);
    373 #endif /* HAVE_WCHAR */
    374 int	 __fgetc_resize(FILE *);
    375 int	 __unget(wint_t);
    376 int	 __mvcur(int, int, int, int, int);
    377 WINDOW  *__newwin(SCREEN *, int, int, int, int, int, int);
    378 int	 __nodelay(void);
    379 int	 __notimeout(void);
    380 void	 __restartwin(void);
    381 void	 __restore_colors(void);
    382 void     __restore_cursor_vis(void);
    383 void     __restore_meta_state(void);
    384 void	 __restore_termios(void);
    385 void	 __restore_stophandler(void);
    386 void	 __restore_winchhandler(void);
    387 int	 __ripoffscreen(SCREEN *);
    388 int	 __ripoffresize(SCREEN *);
    389 void	 __ripofftouch(SCREEN *);
    390 int	 __rippedlines(const SCREEN *, int);
    391 void	 __save_termios(void);
    392 void	 __set_color(WINDOW *win, attr_t attr);
    393 void	 __set_stophandler(void);
    394 void	 __set_winchhandler(void);
    395 void	 __set_subwin(WINDOW *, WINDOW *);
    396 int	 __slk_init(SCREEN *);
    397 void	 __slk_free(SCREEN *);
    398 int	 __slk_resize(SCREEN *, int cols);
    399 int	 __slk_noutrefresh(SCREEN *);
    400 void	 __startwin(SCREEN *);
    401 void	 __stop_signal_handler(int);
    402 int	 __stopwin(void);
    403 void	 __swflags(WINDOW *);
    404 void	 __sync(WINDOW *);
    405 int	 __timeout(int);
    406 int	 __touchline(WINDOW *, int, int, int);
    407 int	 __touchwin(WINDOW *);
    408 int	 __unripoffline(int (*)(WINDOW *, int));
    409 void	 __unsetattr(int);
    410 void	 __unset_color(WINDOW *win);
    411 int	 __waddch(WINDOW *, __LDATA *);
    412 int	 __wgetnstr(WINDOW *, char *, int);
    413 void	 __winch_signal_handler(int);
    414 
    415 /* Private #defines. */
    416 #define	min(a,b)	((a) < (b) ? (a) : (b))
    417 #define	max(a,b)	((a) > (b) ? (a ): (b))
    418 
    419 /* Private externs. */
    420 extern int		 __echoit;
    421 extern int		 __endwin;
    422 extern int		 __pfast;
    423 extern int		 __rawmode;
    424 extern int		 __noqch;
    425 extern attr_t		 __mask_op, __mask_me, __mask_ue, __mask_se;
    426 extern WINDOW		*__virtscr;
    427 extern int		 __using_color;
    428 extern int		 __do_color_init;
    429 extern attr_t		 __default_color;
    430