]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/curses.priv.h
5ebf73b902aa0d901e5a6b20c473fc982e6c67df
[ncurses.git] / ncurses / curses.priv.h
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23 /*
24  * $Id: curses.priv.h,v 1.61 1997/04/26 20:54:54 tom Exp $
25  *
26  *      curses.priv.h
27  *
28  *      Header file for curses library objects which are private to
29  *      the library.
30  *
31  */
32
33 #ifndef CURSES_PRIV_H
34 #define CURSES_PRIV_H 1
35
36 #include <ncurses_cfg.h>
37
38 #ifdef USE_RCS_IDS
39 #define MODULE_ID(id) static const char Ident[] = id;
40 #else
41 #define MODULE_ID(id) /*nothing*/
42 #endif
43
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/types.h>
47
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51
52 #if HAVE_SYS_BSDTYPES_H
53 #include <sys/bsdtypes.h>       /* needed for ISC */
54 #endif
55
56 #if HAVE_LIMITS_H
57 # include <limits.h>
58 #elif HAVE_SYS_PARAM_H
59 # include <sys/param.h>
60 #endif
61
62 #ifndef PATH_MAX
63 # if defined(_POSIX_PATH_MAX)
64 #  define PATH_MAX _POSIX_PATH_MAX
65 # elif defined(MAXPATHLEN)
66 #  define PATH_MAX MAXPATHLEN
67 # else
68 #  define PATH_MAX 255  /* the Posix minimum path-size */
69 # endif
70 #endif
71
72 #include <assert.h>
73 #include <stdio.h>
74
75 #include <errno.h>
76
77 #if !HAVE_EXTERN_ERRNO
78 extern int errno;
79 #endif
80
81 /* Some systems have a broken 'select()', but workable 'poll()'.  Use that */
82 #if HAVE_POLL && HAVE_SYS_STROPTS_H && HAVE_POLL_H
83 #define USE_FUNC_POLL 1
84 #else
85 #define USE_FUNC_POLL 0
86 #endif
87
88 /* Alessandro Rubini's GPM (general-purpose mouse) */
89 #if HAVE_LIBGPM && HAVE_GPM_H
90 #define USE_GPM_SUPPORT 1
91 #else
92 #define USE_GPM_SUPPORT 0
93 #endif
94
95 /*
96  * ht/cbt expansion flakes out randomly under Linux 1.1.47, but only when
97  * we're throwing control codes at the screen at high volume.  To see this,
98  * re-enable TABS_OK and run worm for a while.  Other systems probably don't
99  * want to define this either due to uncertainties about tab delays and
100  * expansion in raw mode.
101  */
102 #undef TABS_OK  /* OK to use tab/backtab for local motions? */
103
104 /*
105  * The internal types (e.g., struct screen) must precede <curses.h>, otherwise
106  * we cannot construct lint-libraries (structures must be fully-defined).
107  */
108
109 struct tries {
110         struct tries    *child;     /* ptr to child.  NULL if none          */
111         struct tries    *sibling;   /* ptr to sibling.  NULL if none        */
112         unsigned char    ch;        /* character at this node               */
113         unsigned short   value;     /* code of string so far.  0 if none.   */
114 };
115
116 /*
117  * Structure for soft labels.
118  */
119
120 typedef struct
121 {
122         char *text;             /* text for the label */
123         char *form_text;        /* formatted text (left/center/...) */
124         int x;                  /* x coordinate of this field */
125         char dirty;             /* this label has changed */
126         char visible;           /* field is visible */
127 } slk_ent;
128
129 typedef struct {
130         char dirty;             /* all labels have changed */
131         char hidden;            /* soft labels are hidden */
132         struct _win_st *win;
133         slk_ent *ent;
134         char *buffer;           /* buffer for labels */
135         short maxlab;           /* number of available labels */
136         short labcnt;           /* number of allocated labels */
137         short maxlen;           /* length of labels */
138 } SLK;
139
140 /*
141  * Definitions for color pairs
142  */
143 #define C_SHIFT 8               /* we need more bits than there are colors */
144 #define C_MASK  ((1 << C_SHIFT) - 1)
145
146 #define PAIR_OF(fg, bg) ((((fg) & C_MASK) << C_SHIFT) | ((bg) & C_MASK))
147
148 /*
149  * Structure for palette tables
150  */
151
152 typedef struct
153 {
154     short red, green, blue;
155 }
156 color_t;
157
158 #define MAXCOLUMNS    135
159 #define MAXLINES      66
160 #define FIFO_SIZE     MAXLINES
161
162 struct screen {
163         int             _ifd;           /* input file ptr for screen        */
164         FILE            *_ofp;          /* output file ptr for screen       */
165         char            *_setbuf;       /* buffered I/O for output          */
166         int             _checkfd;       /* filedesc for typeahead check     */
167         struct term     *_term;         /* terminal type information        */
168         short           _lines;         /* screen lines                     */
169         short           _columns;       /* screen columns                   */
170         short           _lines_avail;   /* lines available for stdscr       */
171         short           _topstolen;     /* lines stolen from top            */
172         struct _win_st  *_curscr;       /* current screen                   */
173         struct _win_st  *_newscr;       /* virtual screen to be updated to  */
174         struct _win_st  *_stdscr;       /* screen's full-window context     */
175         struct tries    *_keytry;       /* "Try" for use with keypad mode   */
176         unsigned int    _fifo[FIFO_SIZE];       /* input push-back buffer   */
177         signed char     _fifohead,      /* head of fifo queue               */
178                         _fifotail,      /* tail of fifo queue               */
179                         _fifopeek;      /* where to peek for next char      */
180         int             _endwin;        /* are we out of window mode?       */
181         unsigned long   _current_attr;  /* terminal attribute current set   */
182         int             _coloron;       /* is color enabled?                */
183         int             _cursor;        /* visibility of the cursor         */
184         int             _cursrow;       /* physical cursor row              */
185         int             _curscol;       /* physical cursor column           */
186         int             _nl;            /* True if NL -> CR/NL is on        */
187         int             _raw;           /* True if in raw mode              */
188         int             _cbreak;        /* 1 if in cbreak mode              */
189                                         /* > 1 if in halfdelay mode         */
190         int             _echo;          /* True if echo on                  */
191         int             _use_meta;      /* use the meta key?                */
192         SLK             *_slk;          /* ptr to soft key struct / NULL    */
193         int             _baudrate;      /* used to compute padding          */
194
195         /* cursor movement costs; units are 10ths of milliseconds */
196         int             _char_padding;  /* cost of character put            */
197         int             _cr_cost;       /* cost of (carriage_return)        */
198         int             _cup_cost;      /* cost of (cursor_address)         */
199         int             _home_cost;     /* cost of (cursor_home)            */
200         int             _ll_cost;       /* cost of (cursor_to_ll)           */
201 #ifdef TABS_OK
202         int             _ht_cost;       /* cost of (tab)                    */
203         int             _cbt_cost;      /* cost of (backtab)                */
204 #endif /* TABS_OK */
205         int             _cub1_cost;     /* cost of (cursor_left)            */
206         int             _cuf1_cost;     /* cost of (cursor_right)           */
207         int             _cud1_cost;     /* cost of (cursor_down)            */
208         int             _cuu1_cost;     /* cost of (cursor_up)              */
209         int             _cub_cost;      /* cost of (parm_cursor_left)       */
210         int             _cuf_cost;      /* cost of (parm_cursor_right)      */
211         int             _cud_cost;      /* cost of (parm_cursor_down)       */
212         int             _cuu_cost;      /* cost of (parm_cursor_up)         */
213         int             _hpa_cost;      /* cost of (column_address)         */
214         int             _vpa_cost;      /* cost of (row_address)            */
215         /* used in lib_doupdate.c, must be chars */
216         int             _ed_cost;       /* cost of (clr_eos)                */
217         int             _el_cost;       /* cost of (clr_eol)                */
218         int             _el1_cost;      /* cost of (clr_bol)                */
219         int             _dch1_cost;     /* cost of (delete_character)       */
220         int             _ich1_cost;     /* cost of (insert_character)       */
221         int             _dch_cost;      /* cost of (parm_dch)               */
222         int             _ich_cost;      /* cost of (parm_ich)               */
223         int             _ech_cost;      /* cost of (erase_chars)            */
224         int             _rep_cost;      /* cost of (repeat_char)            */
225         int             _hpa_ch_cost;   /* cost of (column_address)         */
226         int             _cup_ch_cost;   /* cost of (cursor_address)         */
227         /* used in lib_mvcur.c */
228         char *          _address_cursor;
229         int             _carriage_return_length;
230         int             _cursor_home_length;
231         int             _cursor_to_ll_length;
232         /* used in lib_color.c */
233         color_t         *_color_table;  /* screen's color palette            */
234         int             _color_count;   /* count of colors in palette        */
235         unsigned short  *_color_pairs;  /* screen's color pair list          */
236         int             _pair_count;    /* count of color pairs              */
237         int             _default_color; /* use default colors                */
238 };
239
240 /* Ncurses' public interface follows the internal types */
241 #include <curses.h>     /* we'll use -Ipath directive to get the right one! */
242
243 #ifdef NCURSES_NOMACROS
244 #include <nomacros.h>
245 #endif
246
247 #define WINDOWLIST struct _win_list
248         WINDOWLIST {
249         WINDOWLIST *next;
250         WINDOW  *win;
251 };
252
253 typedef struct {
254         int     line;                   /* lines to take, < 0 => from bottom*/
255         int     (*hook)(struct _win_st *, int); /* callback for user        */
256         struct _win_st *w;              /* maybe we need this for cleanup   */
257 } ripoff_t;
258
259 /* The terminfo source is assumed to be 7-bit ASCII */
260 #define is7bits(c)      ((unsigned)(c) < 128)
261
262 #ifndef min
263 #define min(a,b)        ((a) > (b)  ?  (b)  :  (a))
264 #endif
265
266 #ifndef max
267 #define max(a,b)        ((a) < (b)  ?  (b)  :  (a))
268 #endif
269
270 /* usually in <unistd.h> */
271 #ifndef STDOUT_FILENO
272 #define STDOUT_FILENO 1
273 #endif
274
275 #ifndef STDERR_FILENO
276 #define STDERR_FILENO 2
277 #endif
278
279 #ifndef EXIT_SUCCESS
280 #define EXIT_SUCCESS 0
281 #endif
282
283 #ifndef EXIT_FAILURE
284 #define EXIT_FAILURE 1
285 #endif
286
287 #ifndef R_OK
288 #define R_OK    4               /* Test for read permission.  */
289 #endif
290 #ifndef W_OK
291 #define W_OK    2               /* Test for write permission.  */
292 #endif
293 #ifndef X_OK
294 #define X_OK    1               /* Test for execute permission.  */
295 #endif
296 #ifndef F_OK
297 #define F_OK    0               /* Test for existence.  */
298 #endif
299
300 #define TextOf(c)    ((c) & (chtype)A_CHARTEXT)
301 #define AttrOf(c)    ((c) & (chtype)A_ATTRIBUTES)
302
303 #define BLANK        (' '|A_NORMAL)
304
305 #define CHANGED     -1
306
307 #define typeCalloc(type,elts) (type *)calloc(elts,sizeof(type))
308 #define FreeIfNeeded(p)  if(p != 0) free(p)
309 #define FreeAndNull(p)   free(p); p = 0
310
311 #include <nc_alloc.h>
312
313 /*
314  * Prefixes for call/return points of library function traces.  We use these to
315  * instrument the public functions so that the traces can be easily transformed
316  * into regression scripts.
317  */
318 #define T_CALLED(fmt) "called " fmt
319 #define T_CREATE(fmt) "create " fmt
320 #define T_RETURN(fmt) "return " fmt
321
322 #ifdef TRACE
323 #define TR(n, a)        if (_nc_tracing & (n)) _tracef a
324 #define T(a)            TR(TRACE_CALLS, a)
325 #define TPUTS_TRACE(s)  _nc_tputs_trace = s;
326 #define TRACE_RETURN(value,type) return _nc_retrace_##type(value)
327 #define returnWin(code)  TRACE_RETURN(code,win)
328 #define returnPtr(code)  TRACE_RETURN(code,ptr)
329 #define returnCode(code) TRACE_RETURN(code,int)
330 #define returnVoid       T((T_RETURN(""))); return
331 extern unsigned _nc_tracing;
332 extern WINDOW * _nc_retrace_win(WINDOW *);
333 extern char *_nc_retrace_ptr(char *);
334 extern const char *_nc_tputs_trace;
335 extern const char *_nc_visbuf(const char *);
336 extern const char *_nc_visbuf2(int, const char *);
337 extern int _nc_retrace_int(int);
338 extern long _nc_outchars;
339 #else
340 #define T(a)
341 #define TR(n, a)
342 #define TPUTS_TRACE(s)
343 #define returnWin(code)  return code
344 #define returnCode(code) return code
345 #define returnVoid       return
346 #endif
347
348 #define _trace_key(ch) ((ch > KEY_MIN) ? keyname(ch) : _tracechar((unsigned char)ch))
349
350 #define ALL_BUT_COLOR ((chtype)~(A_COLOR))
351 #define IGNORE_COLOR_OFF FALSE
352
353
354 /* Macro to put together character and attribute info and return it.
355    If colors are in the attribute, they have precedence. */
356 #define ch_or_attr(ch,at) \
357     ((PAIR_NUMBER(at) > 0) ? \
358      ((((chtype)ch) & ALL_BUT_COLOR) | (at)) : ((((chtype)ch) | (at))))
359
360
361 #define toggle_attr_on(S,at) \
362    if (PAIR_NUMBER(at) > 0)\
363       (S) = ((S) & ALL_BUT_COLOR) | (at);\
364    else\
365       (S) |= (at);\
366    T(("new attribute is %s", _traceattr((S))))
367
368
369 #define toggle_attr_off(S,at) \
370    if (IGNORE_COLOR_OFF == TRUE) {\
371       if (PAIR_NUMBER(at) == 0xff) /* turn off color */\
372          (S) &= ~(at);\
373       else /* leave color alone */\
374          (S) &= ~((at)&ALL_BUT_COLOR);\
375    } else {\
376       if (PAIR_NUMBER(at) > 0x00) /* turn off color */\
377          (S) &= ~(at|A_COLOR);\
378       else /* leave color alone */\
379          (S) &= ~(at);\
380    }\
381    T(("new attribute is %s", _traceattr((S))));
382
383 #define DelCharCost(count) \
384                 ((parm_dch != 0) \
385                 ? SP->_dch_cost \
386                 : ((delete_character != 0) \
387                         ? (SP->_dch1_cost * count) \
388                         : INFINITY))
389
390 #define InsCharCost(count) \
391                 ((parm_ich != 0) \
392                 ? SP->_ich_cost \
393                 : ((insert_character != 0) \
394                         ? (SP->_ich1_cost * count) \
395                         : INFINITY))
396
397 #define UpdateAttrs(c)  if (SP->_current_attr != AttrOf(c)) \
398                                 vidattr(AttrOf(c));
399
400 /*
401  * Check whether the given character can be output by clearing commands.  This
402  * includes test for being a space and not including any 'bad' attributes, such
403  * as A_REVERSE.  All attribute flags which don't affect appearance of a space
404  * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
405  */
406 #define NONBLANK_ATTR (A_BOLD|A_DIM|A_BLINK)
407 #define can_clear_with(ch) \
408         ((ch & ~(NONBLANK_ATTR|(back_color_erase ? A_COLOR:0))) == BLANK)
409
410 #ifdef NCURSES_EXPANDED
411
412 #undef  ch_or_attr
413 #define ch_or_attr(ch,at) _nc_ch_or_attr(ch,at)
414 extern chtype _nc_ch_or_attr(chtype, attr_t);
415
416 #undef  toggle_attr_on
417 #define toggle_attr_on(S,at) _nc_toggle_attr_on(&(S), at)
418 extern void _nc_toggle_attr_on(attr_t *, attr_t);
419
420 #undef  toggle_attr_off
421 #define toggle_attr_off(S,at) _nc_toggle_attr_off(&(S), at)
422 extern void _nc_toggle_attr_off(attr_t *, attr_t);
423
424 #undef  can_clear_with
425 #define can_clear_with(ch) _nc_can_clear_with(ch)
426 extern int _nc_can_clear_with(chtype);
427
428 #undef  DelCharCost
429 #define DelCharCost(count) _nc_DelCharCost(count)
430 extern int _nc_DelCharCost(int);
431
432 #undef  InsCharCost
433 #define InsCharCost(count) _nc_InsCharCost(count)
434 extern int _nc_InsCharCost(int);
435
436 #undef  UpdateAttrs
437 #define UpdateAttrs(c) _nc_UpdateAttrs(c)
438 extern void _nc_UpdateAttrs(chtype);
439
440 #else
441
442 extern void _nc_expanded(void);
443
444 #endif
445
446 /* lib_acs.c */
447 extern void init_acs(void);     /* no prefix, this name is traditional */
448
449 /* lib_mvcur.c */
450 #define INFINITY        1000000 /* cost: too high to use */
451
452 extern void _nc_mvcur_init(void);
453 extern void _nc_mvcur_resume(void);
454 extern void _nc_mvcur_wrap(void);
455 extern int _nc_mvcur_scrolln(int, int, int, int);
456
457 /* lib_mouse.c */
458 extern void _nc_mouse_init(SCREEN *);
459 extern bool _nc_mouse_event(SCREEN *);
460 extern bool _nc_mouse_inline(SCREEN *);
461 extern bool _nc_mouse_parse(int);
462 extern void _nc_mouse_wrap(SCREEN *);
463 extern void _nc_mouse_resume(SCREEN *);
464 extern int _nc_max_click_interval;
465 extern int _nc_mouse_fd(void);
466
467 /* elsewhere ... */
468 extern WINDOW *_nc_makenew(int, int, int, int, int);
469 extern chtype _nc_background(WINDOW *);
470 extern chtype _nc_render(WINDOW *, chtype);
471 extern char *_nc_trace_buf(int, size_t);
472 extern int _nc_initscr(void);
473 extern int _nc_keypad(bool);
474 extern int _nc_outch(int);
475 extern int _nc_setupscreen(short, short const, FILE *);
476 extern int _nc_timed_wait(int, int, int *);
477 extern int _nc_waddch_nosync(WINDOW *, const chtype);
478 extern void _nc_backspace(WINDOW *win);
479 extern void _nc_do_color(int, int (*)(int));
480 extern void _nc_freeall(void);
481 extern void _nc_free_and_exit(int);
482 extern void _nc_freewin(WINDOW *win);
483 extern void _nc_get_screensize(void);
484 extern void _nc_hash_map(void);
485 extern void _nc_outstr(const char *str);
486 extern void _nc_scroll_optimize(void);
487 extern void _nc_scroll_window(WINDOW *, int const, short const, short const);
488 extern void _nc_set_buffer(FILE *ofp, bool buffered);
489 extern void _nc_signal_handler(bool);
490 extern void _nc_synchook(WINDOW *win);
491
492 /*
493  * On systems with a broken linker, define 'SP' as a function to force the
494  * linker to pull in the data-only module with 'SP'.
495  */
496 #ifndef BROKEN_LINKER
497 #define BROKEN_LINKER 0
498 #endif
499
500 #if BROKEN_LINKER
501 #define SP _nc_screen()
502 extern SCREEN *_nc_screen(void);
503 extern int _nc_alloc_screen(void);
504 extern void _nc_set_screen(SCREEN *);
505 #else
506 extern SCREEN *SP;
507 #define _nc_alloc_screen() ((SP = typeCalloc(SCREEN, 1)) != 0)
508 #define _nc_set_screen(sp) SP = sp
509 #endif
510
511 #if !HAVE_USLEEP
512 extern int _nc_usleep(unsigned int);
513 #define usleep(msecs) _nc_usleep(msecs)
514 #endif
515
516 /*
517  * We don't want to use the lines or columns capabilities internally,
518  * because if the application is running multiple screens under
519  * X windows, it's quite possible they could all have type xterm
520  * but have different sizes!  So...
521  */
522 #define screen_lines    SP->_lines
523 #define screen_columns  SP->_columns
524
525 extern int _nc_slk_format;  /* != 0 if slk_init() called */
526 extern int _nc_slk_initialize(WINDOW *, int);
527
528 /* Macro to check whether or not we use a standard format */
529 #define SLK_STDFMT (_nc_slk_format < 3)
530 /* Macro to determine height of label window */
531 #define SLK_LINES  (SLK_STDFMT ? 1 : (_nc_slk_format - 2))
532
533 extern int _nc_ripoffline(int line, int (*init)(WINDOW *,int));
534
535 #define UNINITIALISED ((struct tries * ) -1)
536
537 extern bool _nc_idlok, _nc_idcok;
538
539 extern WINDOWLIST *_nc_windows;
540
541 #endif /* CURSES_PRIV_H */