xref: /netbsd-src/lib/libcurses/curses_private.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: curses_private.h,v 1.36 2004/03/28 08:58:37 jdc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998-2000 Brett Lymn
5  *                         (blymn@baea.com.au, brett_lymn@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 #include <termios.h>
33 
34 /* Private structure definitions for curses. */
35 
36 /* Termcap capabilities. */
37 extern char	__tc_am, __tc_bs, __tc_cc, __tc_da, __tc_eo,
38 		__tc_hc, __tc_hl, __tc_in, __tc_mi, __tc_ms,
39 		__tc_nc, __tc_ns, __tc_os, __tc_ul, __tc_ut,
40 		__tc_xb, __tc_xn, __tc_xt, __tc_xs, __tc_xx;
41 extern char	__CA;
42 extern int	__tc_pa, __tc_Co, __tc_NC;
43 extern char	*__tc_ac, *__tc_AB, *__tc_ae, *__tc_AF, *__tc_AL,
44 		*__tc_al, *__tc_as, *__tc_bc, *__tc_bl, *__tc_bt,
45 		*__tc_cd, *__tc_ce, *__tc_cl, *__tc_cm, *__tc_cr,
46 		*__tc_cs, *__tc_dc, *__tc_DL, *__tc_dl, *__tc_dm,
47 		*__tc_DO, *__tc_do, *__tc_eA, *__tc_ed, *__tc_ei,
48 		*__tc_ho, *__tc_Ic, *__tc_ic, *__tc_im, *__tc_Ip,
49 		*__tc_ip, *__tc_k0, *__tc_k1, *__tc_k2, *__tc_k3,
50 		*__tc_k4, *__tc_k5, *__tc_k6, *__tc_k7, *__tc_k8,
51 		*__tc_k9, *__tc_kd, *__tc_ke, *__tc_kh, *__tc_kl,
52 		*__tc_kr, *__tc_ks, *__tc_ku, *__tc_LE, *__tc_ll,
53 		*__tc_ma, *__tc_mb, *__tc_md, *__tc_me, *__tc_mh,
54 		*__tc_mk, *__tc_mm, *__tc_mo, *__tc_mp, *__tc_mr,
55 		*__tc_nd, *__tc_nl, *__tc_oc, *__tc_op,
56 		*__tc_rc, *__tc_RI, *__tc_Sb, *__tc_sc, *__tc_se,
57 		*__tc_SF, *__tc_Sf, *__tc_sf, *__tc_so, *__tc_sp,
58 		*__tc_SR, *__tc_sr, *__tc_ta, *__tc_te, *__tc_ti,
59 		*__tc_uc, *__tc_ue, *__tc_UP, *__tc_up, *__tc_us,
60 		*__tc_vb, *__tc_ve, *__tc_vi, *__tc_vs;
61 
62 /*
63  * A window an array of __LINE structures pointed to by the 'lines' pointer.
64  * A line is an array of __LDATA structures pointed to by the 'line' pointer.
65  *
66  * IMPORTANT: the __LDATA structure must NOT induce any padding, so if new
67  * fields are added -- padding fields with *constant values* should ensure
68  * that the compiler will not generate any padding when storing an array of
69  *  __LDATA structures.  This is to enable consistent use of memcmp, and memcpy
70  * for comparing and copying arrays.
71  */
72 
73 struct __ldata {
74 	wchar_t	ch;			/* Character */
75 	attr_t	attr;			/* Attributes */
76 	wchar_t	bch;			/* Background character */
77 	attr_t	battr;			/* Background attributes */
78 };
79 
80 #define __LDATASIZE	(sizeof(__LDATA))
81 
82 struct __line {
83 #ifdef DEBUG
84 #define SENTINEL_VALUE 0xaac0ffee
85 
86 	unsigned int sentinel;          /* try to catch line overflows */
87 #endif
88 #define	__ISDIRTY	0x01		/* Line is dirty. */
89 #define __ISPASTEOL	0x02		/* Cursor is past end of line */
90 	unsigned int flags;
91 	unsigned int hash;		/* Hash value for the line. */
92 	int *firstchp, *lastchp;	/* First and last chngd columns ptrs */
93 	int firstch, lastch;		/* First and last changed columns. */
94 	__LDATA *line;			/* Pointer to the line text. */
95 };
96 
97 struct __window {		/* Window structure. */
98 	struct __window	*nextp, *orig;	/* Subwindows list and parent. */
99 	int begy, begx;			/* Window home. */
100 	int cury, curx;			/* Current x, y coordinates. */
101 	int maxy, maxx;			/* Maximum values for curx, cury. */
102 	int reqy, reqx;			/* Size requested when created */
103 	int ch_off;			/* x offset for firstch/lastch. */
104 	__LINE **lines;			/* Array of pointers to the lines */
105 	__LINE  *lspace;		/* line space (for cleanup) */
106 	__LDATA *wspace;		/* window space (for cleanup) */
107 
108 #define	__ENDLINE	0x00000001	/* End of screen. */
109 #define	__FLUSH		0x00000002	/* Fflush(stdout) after refresh. */
110 #define	__FULLWIN	0x00000004	/* Window is a screen. */
111 #define	__IDLINE	0x00000008	/* Insert/delete sequences. */
112 #define	__SCROLLWIN	0x00000010	/* Last char will scroll window. */
113 #define	__SCROLLOK	0x00000020	/* Scrolling ok. */
114 #define	__CLEAROK	0x00000040	/* Clear on next refresh. */
115 #define	__LEAVEOK	0x00000100	/* If cursor left */
116 #define	__KEYPAD	0x00010000	/* If interpreting keypad codes */
117 #define	__NOTIMEOUT	0x00020000	/* Wait indefinitely for func keys */
118 #define __IDCHAR	0x00040000	/* insert/delete char sequences */
119 #define __ISPAD		0x00080000	/* "window" is a pad */
120 	unsigned int flags;
121 	int	delay;			/* delay for getch() */
122 	attr_t	wattr;			/* Character attributes */
123 	wchar_t	bch;			/* Background character */
124 	attr_t	battr;			/* Background attributes */
125 	int	scr_t, scr_b;		/* Scrolling region top, bottom */
126 	SCREEN	*screen;		/* Screen for this window */
127 	int	pbegy, pbegx,
128 		sbegy, sbegx,
129 		smaxy, smaxx;		/* Saved prefresh() values */
130 };
131 
132 /* Set of attributes unset by 'me' - 'mb', 'md', 'mh', 'mk', 'mp' and 'mr'. */
133 #define	__TERMATTR \
134 	(__REVERSE | __BLINK | __DIM | __BOLD | __BLANK | __PROTECT)
135 
136 struct __winlist {
137 	struct __window		*winp;	/* The window. */
138 	struct __winlist	*nextp;	/* Next window. */
139 };
140 
141 struct __color {
142 	short	num;
143 	short	red;
144 	short	green;
145 	short	blue;
146 	int	flags;
147 };
148 
149 /* List of colour pairs */
150 struct __pair {
151 	short	fore;
152 	short	back;
153 	int	flags;
154 };
155 
156 /* Maximum colours */
157 #define	MAX_COLORS	64
158 /* Maximum colour pairs - determined by number of colour bits in attr_t */
159 #define	MAX_PAIRS	PAIR_NUMBER(__COLOR)
160 
161 typedef struct keymap keymap_t;
162 
163 /* this is the encapsulation of the terminal definition, one for
164  * each terminal that curses talks to.
165  */
166 struct __screen {
167 	FILE    *infd, *outfd;  /* input and output file descriptors */
168 	WINDOW	*curscr;	/* Current screen. */
169 	WINDOW	*stdscr;	/* Standard screen. */
170 	WINDOW	*__virtscr;	/* Virtual screen (for doupdate()). */
171 	int      curwin;        /* current window for refresh */
172 	int      lx, ly;        /* loop parameters for refresh */
173 	int	 COLS;		/* Columns on the screen. */
174 	int	 LINES;		/* Lines on the screen. */
175 	int	 COLORS;	/* Maximum colors on the screen */
176 	int	 COLOR_PAIRS;	/* Maximum color pairs on the screen */
177 	int	 My_term;	/* Use Def_term regardless. */
178 	char	 GT;		/* Gtty indicates tabs. */
179 	char	 NONL;		/* Term can't hack LF doing a CR. */
180 	char	 UPPERCASE;	/* Terminal is uppercase only. */
181 
182         /* BEWARE!!! The order of the following terminal capabilities */
183         /* (tc_foo) is important - do not update without fixing the zap */
184         /* function in setterm.c */
185 	char	tc_am, tc_bs, tc_cc, tc_da, tc_eo, tc_hc, tc_hl, tc_in, tc_mi,
186 		tc_ms, tc_nc, tc_ns, tc_os, tc_ul, tc_ut, tc_xb, tc_xn, tc_xt,
187 		tc_xs, tc_xx;
188 	int     flag_count;
189 	int	tc_pa, tc_Co, tc_NC;
190 	int     int_count;
191 	char	*tc_AB, *tc_ac, *tc_ae, *tc_AF, *tc_AL,
192 		*tc_al, *tc_as, *tc_bc, *tc_bl, *tc_bt,
193 		*tc_cd, *tc_ce, *tc_cl, *tc_cm, *tc_cr,
194 		*tc_cs, *tc_dc, *tc_DL, *tc_dl, *tc_dm,
195 		*tc_DO, *tc_do, *tc_eA, *tc_ed, *tc_ei,
196 		*tc_ho, *tc_Ic, *tc_ic, *tc_im, *tc_Ip,
197 		*tc_ip, *tc_k0, *tc_k1, *tc_k2, *tc_k3,
198 		*tc_k4, *tc_k5, *tc_k6, *tc_k7, *tc_k8,
199 		*tc_k9, *tc_kd, *tc_ke, *tc_kh, *tc_kl,
200 		*tc_kr, *tc_ks, *tc_ku, *tc_LE, *tc_ll,
201 		*tc_ma, *tc_mb, *tc_md, *tc_me, *tc_mh,
202 		*tc_mk, *tc_mm, *tc_mo, *tc_mp, *tc_mr,
203 		*tc_nd, *tc_nl, *tc_oc, *tc_op, *tc_pc,
204 		*tc_rc, *tc_RI, *tc_Sb, *tc_sc, *tc_se,
205 		*tc_SF, *tc_Sf, *tc_sf, *tc_so, *tc_sp,
206 		*tc_SR, *tc_sr, *tc_ta, *tc_te, *tc_ti,
207 		*tc_uc, *tc_ue, *tc_UP, *tc_up, *tc_us,
208 		*tc_vb, *tc_ve, *tc_vi, *tc_vs;
209 	char CA;
210 	int str_count;
211 	chtype acs_char[NUM_ACS];
212 	struct __color colours[MAX_COLORS];
213 	struct __pair  colour_pairs[MAX_PAIRS];
214 	attr_t	nca;
215 
216 /* Style of colour manipulation */
217 #define COLOR_NONE	0
218 #define COLOR_ANSI	1	/* ANSI/DEC-style colour manipulation */
219 #define COLOR_HP	2	/* HP-style colour manipulation */
220 #define COLOR_TEK	3	/* Tektronix-style colour manipulation */
221 #define COLOR_OTHER	4	/* None of the others but can set fore/back */
222 	int color_type;
223 
224 	attr_t mask_op;
225 	attr_t mask_me;
226 	attr_t mask_ue;
227 	attr_t mask_se;
228 	struct tinfo *cursesi_genbuf;
229 	int old_mode; /* old cursor visibility state for terminal */
230 	keymap_t *base_keymap;
231 	int echoit;
232 	int pfast;
233 	int rawmode;
234 	int nl;
235 	int noqch;
236 	int clearok;
237 	int useraw;
238 	struct __winlist *winlistp;
239 	struct   termios cbreakt, rawt, *curt, save_termios;
240 	struct termios orig_termios, baset, savedtty;
241 	int ovmin;
242 	int ovtime;
243 	char *stdbuf;
244 	unsigned int len;
245 	int meta_state;
246 	char pad_char;
247 	char ttytype[128];
248 	int endwin;
249 	int notty;
250 	int half_delay;
251 	int resized;
252 };
253 
254 
255 extern char	 __GT;			/* Gtty indicates tabs. */
256 extern char	 __NONL;		/* Term can't hack LF doing a CR. */
257 extern char	 __UPPERCASE;		/* Terminal is uppercase only. */
258 extern int	 My_term;		/* Use Def_term regardless. */
259 extern const char	*Def_term;	/* Default terminal type. */
260 extern SCREEN   *_cursesi_screen;       /* The current screen in use */
261 
262 /* Private functions. */
263 #ifdef DEBUG
264 void	 __CTRACE(const char *, ...) __attribute__((__format__(__printf__, 1, 0)));
265 #endif
266 void     __cputchar_args(char, void *);
267 void     _cursesi_free_keymap(keymap_t *);
268 int      _cursesi_gettmode(SCREEN *);
269 void     _cursesi_reset_acs(SCREEN *);
270 void     _cursesi_resetterm(SCREEN *);
271 int      _cursesi_setterm(char *, SCREEN *);
272 int	 __delay(void);
273 u_int	 __hash_more(char *, size_t, u_int);
274 #define	__hash(s, len)	__hash_more((s), (len), 0u)
275 void	 __id_subwins(WINDOW *);
276 void	 __init_getch(SCREEN *);
277 void	 __init_acs(SCREEN *);
278 char	*__longname(char *, char *);	/* Original BSD version */
279 int	 __mvcur(int, int, int, int, int);
280 WINDOW  *__newwin(SCREEN *, int, int, int, int, int);
281 int	 __nodelay(void);
282 int	 __notimeout(void);
283 char	*__parse_cap(const char *, ...);
284 void	 __restartwin(void);
285 void	 __restore_colors(void);
286 void     __restore_cursor_vis(void);
287 void     __restore_meta_state(void);
288 void	 __restore_termios(void);
289 void	 __restore_stophandler(void);
290 void	 __restore_winchhandler(void);
291 void	 __save_termios(void);
292 void	 __set_color(WINDOW *win, attr_t attr);
293 void	 __set_stophandler(void);
294 void	 __set_winchhandler(void);
295 void	 __set_subwin(WINDOW *, WINDOW *);
296 void	 __startwin(SCREEN *);
297 void	 __stop_signal_handler(int);
298 int	 __stopwin(void);
299 void	 __swflags(WINDOW *);
300 int	 __timeout(int);
301 int	 __touchline(WINDOW *, int, int, int);
302 int	 __touchwin(WINDOW *);
303 char	*__tscroll(const char *, int, int);
304 void	 __unsetattr(int);
305 void	 __unset_color(WINDOW *win);
306 int	 __waddch(WINDOW *, __LDATA *);
307 int	 __wgetnstr(WINDOW *, char *, int);
308 void	 __winch_signal_handler(int);
309 
310 /* Private #defines. */
311 #define	min(a,b)	((a) < (b) ? (a) : (b))
312 #define	max(a,b)	((a) > (b) ? (a ): (b))
313 
314 /* Private externs. */
315 extern int		 __echoit;
316 extern int		 __endwin;
317 extern int		 __pfast;
318 extern int		 __rawmode;
319 extern int		 __noqch;
320 extern attr_t		 __mask_op, __mask_me, __mask_ue, __mask_se;
321 extern WINDOW		*__virtscr;
322 extern int		 __using_color;
323 extern attr_t		 __default_color;
324