]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/tinfo_driver.c
ncurses 5.7 - patch 20100619
[ncurses.git] / ncurses / tinfo / tinfo_driver.c
1 /****************************************************************************
2  * Copyright (c) 2008-2009,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Juergen Pfeifer                                                 *
31  *                                                                          *
32  ****************************************************************************/
33
34 #include <curses.priv.h>
35 #define CUR ((TERMINAL*)TCB)->type.
36 #include <tic.h>
37
38 #if HAVE_NANOSLEEP
39 #include <time.h>
40 #if HAVE_SYS_TIME_H
41 #include <sys/time.h>           /* needed for MacOS X DP3 */
42 #endif
43 #endif
44
45 #if HAVE_SIZECHANGE
46 # if !defined(sun) || !TERMIOS
47 #  if HAVE_SYS_IOCTL_H
48 #   include <sys/ioctl.h>
49 #  endif
50 # endif
51 #endif
52
53 MODULE_ID("$Id: tinfo_driver.c,v 1.11 2010/05/15 21:31:12 tom Exp $")
54
55 /*
56  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
57  * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
58  */
59 #ifdef TIOCGSIZE
60 # define IOCTL_WINSIZE TIOCGSIZE
61 # define STRUCT_WINSIZE struct ttysize
62 # define WINSIZE_ROWS(n) (int)n.ts_lines
63 # define WINSIZE_COLS(n) (int)n.ts_cols
64 #else
65 # ifdef TIOCGWINSZ
66 #  define IOCTL_WINSIZE TIOCGWINSZ
67 #  define STRUCT_WINSIZE struct winsize
68 #  define WINSIZE_ROWS(n) (int)n.ws_row
69 #  define WINSIZE_COLS(n) (int)n.ws_col
70 # endif
71 #endif
72
73 /*
74  * These should be screen structure members.  They need to be globals for
75  * historical reasons.  So we assign them in start_color() and also in
76  * set_term()'s screen-switching logic.
77  */
78 #if USE_REENTRANT
79 NCURSES_EXPORT(int)
80 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
81 {
82     return CURRENT_SCREEN ? CURRENT_SCREEN->_pair_count : -1;
83 }
84 NCURSES_EXPORT(int)
85 NCURSES_PUBLIC_VAR(COLORS) (void)
86 {
87     return CURRENT_SCREEN ? CURRENT_SCREEN->_color_count : -1;
88 }
89 #else
90 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
91 NCURSES_EXPORT_VAR(int) COLORS = 0;
92 #endif
93
94 #define TCBMAGIC NCDRV_MAGIC(NCDRV_TINFO)
95 #define AssertTCB() assert(TCB!=0 && TCB->magic==TCBMAGIC)
96 #define SetSP() assert(TCB->csp!=0); sp = TCB->csp
97
98 /*
99  * This routine needs to do all the work to make curscr look
100  * like newscr.
101  */
102 static int
103 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
104 {
105     AssertTCB();
106     return TINFO_DOUPDATE(TCB->csp);
107 }
108
109 #define ret_error(code, fmt, arg)       if (errret) {\
110                                             *errret = code;\
111                                             return(FALSE); \
112                                         } else {\
113                                             fprintf(stderr, fmt, arg);\
114                                             exit(EXIT_FAILURE);\
115                                         }
116
117 #define ret_error0(code, msg)           if (errret) {\
118                                             *errret = code;\
119                                             return(FALSE);\
120                                         } else {\
121                                             fprintf(stderr, msg);\
122                                             exit(EXIT_FAILURE);\
123                                         }
124
125 #if USE_DATABASE || USE_TERMCAP
126 /*
127  * Return 1 if entry found, 0 if not found, -1 if database not accessible,
128  * just like tgetent().
129  */
130 static int
131 grab_entry(const char *const tn, TERMTYPE *const tp)
132 {
133     char filename[PATH_MAX];
134     int status = _nc_read_entry(tn, filename, tp);
135
136     /*
137      * If we have an entry, force all of the cancelled strings to null
138      * pointers so we don't have to test them in the rest of the library.
139      * (The terminfo compiler bypasses this logic, since it must know if
140      * a string is cancelled, for merging entries).
141      */
142     if (status == TGETENT_YES) {
143         unsigned n;
144         for_each_boolean(n, tp) {
145             if (!VALID_BOOLEAN(tp->Booleans[n]))
146                 tp->Booleans[n] = FALSE;
147         }
148         for_each_string(n, tp) {
149             if (tp->Strings[n] == CANCELLED_STRING)
150                 tp->Strings[n] = ABSENT_STRING;
151         }
152     }
153     return (status);
154 }
155 #endif
156
157 static bool
158 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
159 {
160     bool result = FALSE;
161     int status;
162     TERMINAL *termp;
163     SCREEN *sp;
164
165     assert(TCB != 0 && tname != 0);
166     termp = (TERMINAL *) TCB;
167     sp = TCB->csp;
168     TCB->magic = TCBMAGIC;
169
170 #if (USE_DATABASE || USE_TERMCAP)
171     status = grab_entry(tname, &termp->type);
172 #else
173     status = TGETENT_NO;
174 #endif
175
176     /* try fallback list if entry on disk */
177     if (status != TGETENT_YES) {
178         const TERMTYPE *fallback = _nc_fallback(tname);
179
180         if (fallback) {
181             termp->type = *fallback;
182             status = TGETENT_YES;
183         }
184     }
185
186     if (status != TGETENT_YES) {
187         NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx termp);
188         if (status == TGETENT_ERR) {
189             ret_error0(status, "terminals database is inaccessible\n");
190         } else if (status == TGETENT_NO) {
191             ret_error(status, "'%s': unknown terminal type.\n", tname);
192         }
193     }
194     result = TRUE;
195 #if !USE_REENTRANT
196     strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
197     ttytype[NAMESIZE - 1] = '\0';
198 #endif
199
200     if (command_character)
201         _nc_tinfo_cmdch(termp, *command_character);
202
203     if (generic_type) {
204         ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
205     }
206     if (hard_copy) {
207         ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
208     }
209
210     return result;
211 }
212
213 static int
214 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag)
215 {
216     SCREEN *sp;
217     int res = ERR;
218
219     AssertTCB();
220     SetSP();
221
222     /* FIXME: should make sure that we are not in altchar mode */
223     if (beepFlag) {
224         if (bell) {
225             res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
226             NCURSES_SP_NAME(_nc_flush) (sp);
227         } else if (flash_screen) {
228             res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
229                                              "flash_screen",
230                                              flash_screen);
231             NCURSES_SP_NAME(_nc_flush) (sp);
232         }
233     } else {
234         if (flash_screen) {
235             res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
236                                              "flash_screen",
237                                              flash_screen);
238             NCURSES_SP_NAME(_nc_flush) (sp);
239         } else if (bell) {
240             res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
241             NCURSES_SP_NAME(_nc_flush) (sp);
242         }
243     }
244     return res;
245 }
246
247 /*
248  * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
249  * to maintain compatibility with a pre-ANSI scheme.  The same scheme is
250  * also used in the FreeBSD syscons.
251  */
252 static int
253 toggled_colors(int c)
254 {
255     if (c < 16) {
256         static const int table[] =
257         {0, 4, 2, 6, 1, 5, 3, 7,
258          8, 12, 10, 14, 9, 13, 11, 15};
259         c = table[c];
260     }
261     return c;
262 }
263
264 static int
265 drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len)
266 {
267     SCREEN *sp;
268
269     AssertTCB();
270     SetSP();
271 #if NCURSES_EXT_FUNCS
272     return NCURSES_SP_NAME(mcprint) (TCB->csp, data, len);
273 #else
274     return ERR;
275 #endif
276 }
277
278 static int
279 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg)
280 {
281     SCREEN *sp;
282     int code = ERR;
283
284     AssertTCB();
285     SetSP();
286
287     if (sp != 0 && orig_pair && orig_colors && (initialize_pair != 0)) {
288 #if NCURSES_EXT_FUNCS
289         sp->_default_color = isDefaultColor(fg) || isDefaultColor(bg);
290         sp->_has_sgr_39_49 = (NCURSES_SP_NAME(tigetflag) (NCURSES_SP_ARGx
291                                                           "AX")
292                               == TRUE);
293         sp->_default_fg = isDefaultColor(fg) ? COLOR_DEFAULT : (fg & C_MASK);
294         sp->_default_bg = isDefaultColor(bg) ? COLOR_DEFAULT : (bg & C_MASK);
295         if (sp->_color_pairs != 0) {
296             bool save = sp->_default_color;
297             sp->_default_color = TRUE;
298             NCURSES_SP_NAME(init_pair) (NCURSES_SP_ARGx
299                                         0,
300                                         (short)fg,
301                                         (short)bg);
302             sp->_default_color = save;
303         }
304 #endif
305         code = OK;
306     }
307     return (code);
308 }
309
310 static void
311 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB,
312              bool fore,
313              int color,
314              NCURSES_SP_OUTC outc)
315 {
316     SCREEN *sp;
317
318     AssertTCB();
319     SetSP();
320
321     if (fore) {
322         if (set_a_foreground) {
323             TPUTS_TRACE("set_a_foreground");
324             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
325                                     TPARM_1(set_a_foreground, color), 1, outc);
326         } else {
327             TPUTS_TRACE("set_foreground");
328             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
329                                     TPARM_1(set_foreground,
330                                             toggled_colors(color)), 1, outc);
331         }
332     } else {
333         if (set_a_background) {
334             TPUTS_TRACE("set_a_background");
335             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
336                                     TPARM_1(set_a_background, color), 1, outc);
337         } else {
338             TPUTS_TRACE("set_background");
339             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
340                                     TPARM_1(set_background,
341                                             toggled_colors(color)), 1, outc);
342         }
343     }
344 }
345
346 static bool
347 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
348 {
349     bool result = FALSE;
350     SCREEN *sp;
351
352     AssertTCB();
353     SetSP();
354
355     if (orig_pair != 0) {
356         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_pair", orig_pair);
357         result = TRUE;
358     }
359     return result;
360 }
361
362 static bool
363 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
364 {
365     int result = FALSE;
366     SCREEN *sp;
367
368     AssertTCB();
369     SetSP();
370
371     if (orig_colors != 0) {
372         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_colors", orig_colors);
373         result = TRUE;
374     }
375     return result;
376 }
377
378 static int
379 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *linep, int *colp)
380 {
381     SCREEN *sp;
382     bool useEnv = TRUE;
383
384     AssertTCB();
385     sp = TCB->csp;              /* can be null here */
386
387     if (sp) {
388         useEnv = sp->_use_env;
389     } else
390         useEnv = _nc_prescreen.use_env;
391
392     /* figure out the size of the screen */
393     T(("screen size: terminfo lines = %d columns = %d", lines, columns));
394
395     *linep = (int) lines;
396     *colp = (int) columns;
397
398     if (useEnv) {
399         int value;
400
401 #ifdef __EMX__
402         {
403             int screendata[2];
404             _scrsize(screendata);
405             *colp = screendata[0];
406             *linep = screendata[1];
407             T(("EMX screen size: environment LINES = %d COLUMNS = %d",
408                *linep, *colp));
409         }
410 #endif
411 #if HAVE_SIZECHANGE
412         /* try asking the OS */
413         {
414             TERMINAL *termp = (TERMINAL *) TCB;
415             if (isatty(termp->Filedes)) {
416                 STRUCT_WINSIZE size;
417
418                 errno = 0;
419                 do {
420                     if (ioctl(termp->Filedes, IOCTL_WINSIZE, &size) >= 0) {
421                         *linep = ((sp != 0 && sp->_filtered)
422                                   ? 1
423                                   : WINSIZE_ROWS(size));
424                         *colp = WINSIZE_COLS(size);
425                         T(("SYS screen size: environment LINES = %d COLUMNS = %d",
426                            *linep, *colp));
427                         break;
428                     }
429                 } while
430                     (errno == EINTR);
431             }
432         }
433 #endif /* HAVE_SIZECHANGE */
434
435         /*
436          * Finally, look for environment variables.
437          *
438          * Solaris lets users override either dimension with an environment
439          * variable.
440          */
441         if ((value = _nc_getenv_num("LINES")) > 0) {
442             *linep = value;
443             T(("screen size: environment LINES = %d", *linep));
444         }
445         if ((value = _nc_getenv_num("COLUMNS")) > 0) {
446             *colp = value;
447             T(("screen size: environment COLUMNS = %d", *colp));
448         }
449
450         /* if we can't get dynamic info about the size, use static */
451         if (*linep <= 0) {
452             *linep = (int) lines;
453         }
454         if (*colp <= 0) {
455             *colp = (int) columns;
456         }
457
458         /* the ultimate fallback, assume fixed 24x80 size */
459         if (*linep <= 0) {
460             *linep = 24;
461         }
462         if (*colp <= 0) {
463             *colp = 80;
464         }
465
466         /*
467          * Put the derived values back in the screen-size caps, so
468          * tigetnum() and tgetnum() will do the right thing.
469          */
470         lines = (short) (*linep);
471         columns = (short) (*colp);
472     }
473
474     T(("screen size is %dx%d", *linep, *colp));
475     return OK;
476 }
477
478 static int
479 drv_getsize(TERMINAL_CONTROL_BLOCK * TCB, int *l, int *c)
480 {
481     AssertTCB();
482     assert(l != 0 && c != 0);
483     *l = lines;
484     *c = columns;
485     return OK;
486 }
487
488 static int
489 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c)
490 {
491     AssertTCB();
492     lines = (short) l;
493     columns = (short) c;
494     return OK;
495 }
496
497 static int
498 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
499 {
500     SCREEN *sp = TCB->csp;
501     TERMINAL *_term = (TERMINAL *) TCB;
502     int result = OK;
503
504     AssertTCB();
505     if (setFlag) {
506         for (;;) {
507             if (SET_TTY(_term->Filedes, buf) != 0) {
508                 if (errno == EINTR)
509                     continue;
510                 if (errno == ENOTTY) {
511                     if (sp)
512                         sp->_notty = TRUE;
513                 }
514                 result = ERR;
515             }
516             break;
517         }
518     } else {
519         for (;;) {
520             if (GET_TTY(_term->Filedes, buf) != 0) {
521                 if (errno == EINTR)
522                     continue;
523                 result = ERR;
524             }
525             break;
526         }
527     }
528     return result;
529 }
530
531 static int
532 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
533 {
534     SCREEN *sp;
535     TERMINAL *_term = (TERMINAL *) TCB;
536     int code = ERR;
537
538     AssertTCB();
539     sp = TCB->csp;
540
541     if (progFlag)               /* prog mode */
542     {
543         if (defFlag) {
544             /* def_prog_mode */
545             /*
546              * Turn off the XTABS bit in the tty structure if it was on.
547              */
548             if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
549 #ifdef TERMIOS
550                 _term->Nttyb.c_oflag &= ~OFLAGS_TABS;
551 #else
552                 _term->Nttyb.sg_flags &= ~XTABS;
553 #endif
554                 code = OK;
555             }
556         } else {
557             /* reset_prog_mode */
558             if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
559                 if (sp) {
560                     if (sp->_keypad_on)
561                         _nc_keypad(sp, TRUE);
562                     NC_BUFFERED(sp, TRUE);
563                 }
564                 code = OK;
565             }
566         }
567     } else {                    /* shell mode */
568         if (defFlag) {
569             /* def_shell_mode */
570             /*
571              * If XTABS was on, remove the tab and backtab capabilities.
572              */
573             if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
574 #ifdef TERMIOS
575                 if (_term->Ottyb.c_oflag & OFLAGS_TABS)
576                     tab = back_tab = NULL;
577 #else
578                 if (_term->Ottyb.sg_flags & XTABS)
579                     tab = back_tab = NULL;
580 #endif
581                 code = OK;
582             }
583         } else {
584             /* reset_shell_mode */
585             if (sp) {
586                 _nc_keypad(sp, FALSE);
587                 NCURSES_SP_NAME(_nc_flush) (sp);
588                 NC_BUFFERED(sp, FALSE);
589             }
590             code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
591         }
592     }
593     return (code);
594 }
595
596 static void
597 drv_wrap(SCREEN *sp)
598 {
599     if (sp) {
600         sp->_mouse_wrap(sp);
601         NCURSES_SP_NAME(_nc_screen_wrap) (sp);
602         NCURSES_SP_NAME(_nc_mvcur_wrap) (sp);   /* wrap up cursor addressing */
603     }
604 }
605
606 static void
607 drv_release(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
608 {
609 }
610
611 #  define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
612
613 static void
614 drv_screen_init(SCREEN *sp)
615 {
616     TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
617
618     AssertTCB();
619
620     /*
621      * Check for mismatched graphic-rendition capabilities.  Most SVr4
622      * terminfo trees contain entries that have rmul or rmso equated to
623      * sgr0 (Solaris curses copes with those entries).  We do this only
624      * for curses, since many termcap applications assume that
625      * smso/rmso and smul/rmul are paired, and will not function
626      * properly if we remove rmso or rmul.  Curses applications
627      * shouldn't be looking at this detail.
628      */
629     sp->_use_rmso = SGR0_TEST(exit_standout_mode);
630     sp->_use_rmul = SGR0_TEST(exit_underline_mode);
631
632     /*
633      * Check whether we can optimize scrolling under dumb terminals in
634      * case we do not have any of these capabilities, scrolling
635      * optimization will be useless.
636      */
637     sp->_scrolling = ((scroll_forward && scroll_reverse) ||
638                       ((parm_rindex ||
639                         parm_insert_line ||
640                         insert_line) &&
641                        (parm_index ||
642                         parm_delete_line ||
643                         delete_line)));
644
645     NCURSES_SP_NAME(baudrate) (sp);
646
647     NCURSES_SP_NAME(_nc_mvcur_init) (sp);
648     /* initialize terminal to a sane state */
649     NCURSES_SP_NAME(_nc_screen_init) (sp);
650 }
651
652 static void
653 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
654 {
655     SCREEN *sp;
656     TERMINAL *trm;
657
658     AssertTCB();
659
660     trm = (TERMINAL *) TCB;
661     sp = TCB->csp;
662
663     TCB->info.initcolor = initialize_color;
664     TCB->info.canchange = can_change;
665     TCB->info.hascolor = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
666                            && (((set_foreground != NULL)
667                                 && (set_background != NULL))
668                                || ((set_a_foreground != NULL)
669                                    && (set_a_background != NULL))
670                                || set_color_pair)) ? TRUE : FALSE);
671
672     TCB->info.caninit = !(exit_ca_mode && non_rev_rmcup);
673
674     TCB->info.maxpairs = VALID_NUMERIC(max_pairs) ? max_pairs : 0;
675     TCB->info.maxcolors = VALID_NUMERIC(max_colors) ? max_colors : 0;
676     TCB->info.numlabels = VALID_NUMERIC(num_labels) ? num_labels : 0;
677     TCB->info.labelwidth = VALID_NUMERIC(label_width) ? label_width : 0;
678     TCB->info.labelheight = VALID_NUMERIC(label_height) ? label_height : 0;
679     TCB->info.nocolorvideo = VALID_NUMERIC(no_color_video) ? no_color_video
680         : 0;
681     TCB->info.tabsize = VALID_NUMERIC(init_tabs) ? (int) init_tabs : 8;
682
683     TCB->info.defaultPalette = hue_lightness_saturation ? _nc_hls_palette : _nc_cga_palette;
684
685     /*
686      * If an application calls setupterm() rather than initscr() or
687      * newterm(), we will not have the def_prog_mode() call in
688      * _nc_setupscreen().  Do it now anyway, so we can initialize the
689      * baudrate.
690      */
691     if (isatty(trm->Filedes)) {
692         TCB->drv->mode(TCB, TRUE, TRUE);
693     }
694 }
695
696 #define MAX_PALETTE     8
697 #define InPalette(n)    ((n) >= 0 && (n) < MAX_PALETTE)
698
699 static void
700 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b)
701 {
702     SCREEN *sp;
703
704     AssertTCB();
705     SetSP();
706
707     if ((initialize_pair != NULL) && InPalette(f) && InPalette(b)) {
708         const color_t *tp = InfoOf(sp).defaultPalette;
709
710         TR(TRACE_ATTRS,
711            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
712             pair,
713             tp[f].red, tp[f].green, tp[f].blue,
714             tp[b].red, tp[b].green, tp[b].blue));
715
716         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
717                                    "initialize_pair",
718                                    TPARM_7(initialize_pair,
719                                            pair,
720                                            tp[f].red, tp[f].green, tp[f].blue,
721                                            tp[b].red, tp[b].green, tp[b].blue));
722     }
723 }
724
725 static int
726 default_fg(SCREEN *sp)
727 {
728 #if NCURSES_EXT_FUNCS
729     return (sp != 0) ? sp->_default_fg : COLOR_WHITE;
730 #else
731     return COLOR_WHITE;
732 #endif
733 }
734
735 static int
736 default_bg(SCREEN *sp)
737 {
738 #if NCURSES_EXT_FUNCS
739     return sp != 0 ? sp->_default_bg : COLOR_BLACK;
740 #else
741     return COLOR_BLACK;
742 #endif
743 }
744
745 static void
746 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
747               short color, short r, short g, short b)
748 {
749     SCREEN *sp = TCB->csp;
750
751     AssertTCB();
752     if (initialize_color != NULL) {
753         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
754                                    "initialize_color",
755                                    TPARM_4(initialize_color, color, r, g, b));
756     }
757 }
758
759 static void
760 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
761              short old_pair,
762              short pair,
763              bool reverse,
764              NCURSES_SP_OUTC outc)
765 {
766     SCREEN *sp = TCB->csp;
767     NCURSES_COLOR_T fg = COLOR_DEFAULT;
768     NCURSES_COLOR_T bg = COLOR_DEFAULT;
769     NCURSES_COLOR_T old_fg, old_bg;
770
771     AssertTCB();
772     if (sp == 0)
773         return;
774
775     if (pair < 0 || pair >= COLOR_PAIRS) {
776         return;
777     } else if (pair != 0) {
778         if (set_color_pair) {
779             TPUTS_TRACE("set_color_pair");
780             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
781                                     TPARM_1(set_color_pair, pair), 1, outc);
782             return;
783         } else if (sp != 0) {
784             NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
785                                            (short) pair,
786                                            &fg,
787                                            &bg);
788         }
789     }
790
791     if (old_pair >= 0
792         && sp != 0
793         && NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
794                                           old_pair,
795                                           &old_fg,
796                                           &old_bg) !=ERR) {
797         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
798             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
799 #if NCURSES_EXT_FUNCS
800             /*
801              * A minor optimization - but extension.  If "AX" is specified in
802              * the terminal description, treat it as screen's indicator of ECMA
803              * SGR 39 and SGR 49, and assume the two sequences are independent.
804              */
805             if (sp->_has_sgr_39_49
806                 && isDefaultColor(old_bg)
807                 && !isDefaultColor(old_fg)) {
808                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
809             } else if (sp->_has_sgr_39_49
810                        && isDefaultColor(old_fg)
811                        && !isDefaultColor(old_bg)) {
812                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
813             } else
814 #endif
815                 drv_rescol(TCB);
816         }
817     } else {
818         drv_rescol(TCB);
819         if (old_pair < 0)
820             return;
821     }
822
823 #if NCURSES_EXT_FUNCS
824     if (isDefaultColor(fg))
825         fg = (NCURSES_COLOR_T) default_fg(sp);
826     if (isDefaultColor(bg))
827         bg = (NCURSES_COLOR_T) default_bg(sp);
828 #endif
829
830     if (reverse) {
831         NCURSES_COLOR_T xx = fg;
832         fg = bg;
833         bg = xx;
834     }
835
836     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
837                      fg, bg));
838
839     if (!isDefaultColor(fg)) {
840         drv_setcolor(TCB, TRUE, fg, outc);
841     }
842     if (!isDefaultColor(bg)) {
843         drv_setcolor(TCB, FALSE, bg, outc);
844     }
845 }
846
847 #define xterm_kmous "\033[M"
848 static void
849 init_xterm_mouse(SCREEN *sp)
850 {
851     sp->_mouse_type = M_XTERM;
852     sp->_mouse_xtermcap = NCURSES_SP_NAME(tigetstr) (NCURSES_SP_ARGx "XM");
853     if (!VALID_STRING(sp->_mouse_xtermcap))
854         sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
855 }
856
857 static void
858 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
859 {
860     SCREEN *sp;
861
862     AssertTCB();
863     SetSP();
864
865     /* we know how to recognize mouse events under "xterm" */
866     if (sp != 0) {
867         if (key_mouse != 0) {
868             if (!strcmp(key_mouse, xterm_kmous)
869                 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
870                 init_xterm_mouse(sp);
871             }
872         } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
873             if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
874                 init_xterm_mouse(sp);
875         }
876     }
877 }
878
879 static int
880 drv_testmouse(TERMINAL_CONTROL_BLOCK * TCB, int delay)
881 {
882     int rc = 0;
883     SCREEN *sp;
884
885     AssertTCB();
886     SetSP();
887
888 #if USE_SYSMOUSE
889     if ((sp->_mouse_type == M_SYSMOUSE)
890         && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
891         rc = TW_MOUSE;
892     } else
893 #endif
894     {
895         rc = TCBOf(sp)->drv->twait(TCBOf(sp),
896                                    TWAIT_MASK,
897                                    delay,
898                                    (int *) 0
899                                    EVENTLIST_2nd(evl));
900 #if USE_SYSMOUSE
901         if ((sp->_mouse_type == M_SYSMOUSE)
902             && (sp->_sysmouse_head < sp->_sysmouse_tail)
903             && (rc == 0)
904             && (errno == EINTR)) {
905             rc |= TW_MOUSE;
906         }
907 #endif
908     }
909     return rc;
910 }
911
912 static int
913 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int ynew, int xnew)
914 {
915     SCREEN *sp = TCB->csp;
916     AssertTCB();
917     return TINFO_MVCUR(sp, yold, xold, ynew, xnew);
918 }
919
920 static void
921 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text)
922 {
923     SCREEN *sp = TCB->csp;
924
925     AssertTCB();
926     if (labnum > 0 && labnum <= num_labels) {
927         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
928                                    "plab_norm",
929                                    TPARM_2(plab_norm, labnum, text));
930     }
931 }
932
933 static void
934 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag)
935 {
936     SCREEN *sp = TCB->csp;
937
938     AssertTCB();
939     if (OnFlag) {
940         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_on", label_on);
941     } else {
942         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_off", label_off);
943     }
944 }
945
946 static chtype
947 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB)
948 {
949     SCREEN *sp = TCB->csp;
950     chtype attrs = A_NORMAL;
951
952     AssertTCB();
953     if (enter_alt_charset_mode)
954         attrs |= A_ALTCHARSET;
955
956     if (enter_blink_mode)
957         attrs |= A_BLINK;
958
959     if (enter_bold_mode)
960         attrs |= A_BOLD;
961
962     if (enter_dim_mode)
963         attrs |= A_DIM;
964
965     if (enter_reverse_mode)
966         attrs |= A_REVERSE;
967
968     if (enter_standout_mode)
969         attrs |= A_STANDOUT;
970
971     if (enter_protected_mode)
972         attrs |= A_PROTECT;
973
974     if (enter_secure_mode)
975         attrs |= A_INVIS;
976
977     if (enter_underline_mode)
978         attrs |= A_UNDERLINE;
979
980     if (sp && sp->_coloron)
981         attrs |= A_COLOR;
982
983     return (attrs);
984 }
985
986 static void
987 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
988 {
989     AssertTCB();
990
991     clear_screen = 0;
992     cursor_down = parm_down_cursor = 0;
993     cursor_address = 0;
994     cursor_up = parm_up_cursor = 0;
995     row_address = 0;
996     cursor_home = carriage_return;
997 }
998
999 static void
1000 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *real_map, chtype *fake_map)
1001 {
1002     SCREEN *sp = TCB->csp;
1003
1004     AssertTCB();
1005     assert(sp != 0);
1006     if (ena_acs != NULL) {
1007         NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "ena_acs", ena_acs);
1008     }
1009 #if NCURSES_EXT_FUNCS
1010     /*
1011      * Linux console "supports" the "PC ROM" character set by the coincidence
1012      * that smpch/rmpch and smacs/rmacs have the same values.  ncurses has
1013      * no codepage support (see SCO Merge for an example).  Outside of the
1014      * values defined in acsc, there are no definitions for the "PC ROM"
1015      * character set (assumed by some applications to be codepage 437), but we
1016      * allow those applications to use those codepoints.
1017      *
1018      * test/blue.c uses this feature.
1019      */
1020 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
1021     if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
1022         PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
1023         size_t i;
1024         for (i = 1; i < ACS_LEN; ++i) {
1025             if (real_map[i] == 0) {
1026                 real_map[i] = i;
1027                 if (real_map != fake_map) {
1028                     if (sp != 0)
1029                         sp->_screen_acs_map[i] = TRUE;
1030                 }
1031             }
1032         }
1033     }
1034 #endif
1035
1036     if (acs_chars != NULL) {
1037         size_t i = 0;
1038         size_t length = strlen(acs_chars);
1039
1040         while (i + 1 < length) {
1041             if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
1042                 real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
1043                 if (sp != 0)
1044                     sp->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
1045             }
1046             i += 2;
1047         }
1048     }
1049 #ifdef TRACE
1050     /* Show the equivalent mapping, noting if it does not match the
1051      * given attribute, whether by re-ordering or duplication.
1052      */
1053     if (USE_TRACEF(TRACE_CALLS)) {
1054         size_t n, m;
1055         char show[ACS_LEN * 2 + 1];
1056         for (n = 1, m = 0; n < ACS_LEN; n++) {
1057             if (real_map[n] != 0) {
1058                 show[m++] = (char) n;
1059                 show[m++] = (char) ChCharOf(real_map[n]);
1060             }
1061         }
1062         show[m] = 0;
1063         if (acs_chars == NULL || strcmp(acs_chars, show))
1064             _tracef("%s acs_chars %s",
1065                     (acs_chars == NULL) ? "NULL" : "READ",
1066                     _nc_visbuf(acs_chars));
1067         _tracef("%s acs_chars %s",
1068                 (acs_chars == NULL)
1069                 ? "NULL"
1070                 : (strcmp(acs_chars, show)
1071                    ? "DIFF"
1072                    : "SAME"),
1073                 _nc_visbuf(show));
1074
1075         _nc_unlock_global(tracef);
1076     }
1077 #endif /* TRACE */
1078 }
1079
1080 #define ENSURE_TINFO(sp) (TCBOf(sp)->drv->isTerminfo)
1081
1082 NCURSES_EXPORT(void)
1083 _nc_cookie_init(SCREEN *sp)
1084 {
1085     bool support_cookies = USE_XMC_SUPPORT;
1086     TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) (sp->_term);
1087
1088     if (sp == 0 || !ENSURE_TINFO(sp))
1089         return;
1090
1091 #if USE_XMC_SUPPORT
1092     /*
1093      * If we have no magic-cookie support compiled-in, or if it is suppressed
1094      * in the environment, reset the support-flag.
1095      */
1096     if (magic_cookie_glitch >= 0) {
1097         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
1098             support_cookies = FALSE;
1099         }
1100     }
1101 #endif
1102
1103     if (!support_cookies && magic_cookie_glitch >= 0) {
1104         T(("will disable attributes to work w/o magic cookies"));
1105     }
1106
1107     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
1108
1109         sp->_xmc_triggers = sp->_ok_attributes & (
1110                                                      A_STANDOUT |
1111                                                      A_UNDERLINE |
1112                                                      A_REVERSE |
1113                                                      A_BLINK |
1114                                                      A_DIM |
1115                                                      A_BOLD |
1116                                                      A_INVIS |
1117                                                      A_PROTECT
1118             );
1119 #if 0
1120         /*
1121          * We "should" treat colors as an attribute.  The wyse350 (and its
1122          * clones) appear to be the only ones that have both colors and magic
1123          * cookies.
1124          */
1125         if (has_colors()) {
1126             sp->_xmc_triggers |= A_COLOR;
1127         }
1128 #endif
1129         sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
1130
1131         T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
1132         /*
1133          * Supporting line-drawing may be possible.  But make the regular
1134          * video attributes work first.
1135          */
1136         acs_chars = ABSENT_STRING;
1137         ena_acs = ABSENT_STRING;
1138         enter_alt_charset_mode = ABSENT_STRING;
1139         exit_alt_charset_mode = ABSENT_STRING;
1140 #if USE_XMC_SUPPORT
1141         /*
1142          * To keep the cookie support simple, suppress all of the optimization
1143          * hooks except for clear_screen and the cursor addressing.
1144          */
1145         if (support_cookies) {
1146             clr_eol = ABSENT_STRING;
1147             clr_eos = ABSENT_STRING;
1148             set_attributes = ABSENT_STRING;
1149         }
1150 #endif
1151     } else if (magic_cookie_glitch == 0) {      /* hpterm */
1152     }
1153
1154     /*
1155      * If magic cookies are not supported, cancel the strings that set
1156      * video attributes.
1157      */
1158     if (!support_cookies && magic_cookie_glitch >= 0) {
1159         magic_cookie_glitch = ABSENT_NUMERIC;
1160         set_attributes = ABSENT_STRING;
1161         enter_blink_mode = ABSENT_STRING;
1162         enter_bold_mode = ABSENT_STRING;
1163         enter_dim_mode = ABSENT_STRING;
1164         enter_reverse_mode = ABSENT_STRING;
1165         enter_standout_mode = ABSENT_STRING;
1166         enter_underline_mode = ABSENT_STRING;
1167     }
1168
1169     /* initialize normal acs before wide, since we use mapping in the latter */
1170 #if !USE_WIDEC_SUPPORT
1171     if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
1172         acs_chars = NULL;
1173         ena_acs = NULL;
1174         enter_alt_charset_mode = NULL;
1175         exit_alt_charset_mode = NULL;
1176         set_attributes = NULL;
1177     }
1178 #endif
1179 }
1180
1181 static int
1182 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1183           int mode,
1184           int milliseconds,
1185           int *timeleft
1186           EVENTLIST_2nd(_nc_eventlist * evl))
1187 {
1188     SCREEN *sp;
1189
1190     AssertTCB();
1191     SetSP();
1192
1193     return _nc_timed_wait(sp, mode, milliseconds, timeleft EVENTLIST_2nd(evl));
1194 }
1195
1196 static int
1197 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1198 {
1199     SCREEN *sp;
1200     unsigned char c2 = 0;
1201     int n;
1202
1203     AssertTCB();
1204     assert(buf);
1205     SetSP();
1206
1207 # if USE_PTHREADS_EINTR
1208     if ((pthread_self) && (pthread_kill) && (pthread_equal))
1209         _nc_globals.read_thread = pthread_self();
1210 # endif
1211     n = read(sp->_ifd, &c2, 1);
1212 #if USE_PTHREADS_EINTR
1213     _nc_globals.read_thread = 0;
1214 #endif
1215     *buf = (int) c2;
1216     return n;
1217 }
1218
1219 static int
1220 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1221 {
1222 #if HAVE_NANOSLEEP
1223     {
1224         struct timespec request, remaining;
1225         request.tv_sec = ms / 1000;
1226         request.tv_nsec = (ms % 1000) * 1000000;
1227         while (nanosleep(&request, &remaining) == -1
1228                && errno == EINTR) {
1229             request = remaining;
1230         }
1231     }
1232 #else
1233     _nc_timed_wait(0, 0, ms, (int *) 0 EVENTLIST_2nd(0));
1234 #endif
1235     return OK;
1236 }
1237
1238 static int
1239 __nc_putp(SCREEN *sp, const char *name GCC_UNUSED, const char *value)
1240 {
1241     int rc = ERR;
1242
1243     if (value) {
1244         rc = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx name, value);
1245     }
1246     return rc;
1247 }
1248
1249 static int
1250 __nc_putp_flush(SCREEN *sp, const char *name, const char *value)
1251 {
1252     int rc = __nc_putp(sp, name, value);
1253     if (rc != ERR) {
1254         NCURSES_SP_NAME(_nc_flush) (sp);
1255     }
1256     return rc;
1257 }
1258
1259 static int
1260 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag)
1261 {
1262     int ret = ERR;
1263     SCREEN *sp;
1264
1265     AssertTCB();
1266
1267     sp = TCB->csp;
1268
1269     if (sp) {
1270         if (flag) {
1271             (void) __nc_putp_flush(sp, "keypad_xmit", keypad_xmit);
1272         } else if (!flag && keypad_local) {
1273             (void) __nc_putp_flush(sp, "keypad_local", keypad_local);
1274         }
1275         if (flag && !sp->_tried) {
1276             _nc_init_keytry(sp);
1277             sp->_tried = TRUE;
1278         }
1279         ret = OK;
1280     }
1281
1282     return ret;
1283 }
1284
1285 static int
1286 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int c, bool flag)
1287 {
1288     SCREEN *sp;
1289     int code = ERR;
1290     int count = 0;
1291     char *s;
1292
1293     AssertTCB();
1294     SetSP();
1295
1296     if (c >= 0) {
1297         unsigned ch = (unsigned) c;
1298         if (flag) {
1299             while ((s = _nc_expand_try(sp->_key_ok, ch, &count, 0)) != 0
1300                    && _nc_remove_key(&(sp->_key_ok), ch)) {
1301                 code = _nc_add_to_try(&(sp->_keytry), s, ch);
1302                 free(s);
1303                 count = 0;
1304                 if (code != OK)
1305                     break;
1306             }
1307         } else {
1308             while ((s = _nc_expand_try(sp->_keytry, ch, &count, 0)) != 0
1309                    && _nc_remove_key(&(sp->_keytry), ch)) {
1310                 code = _nc_add_to_try(&(sp->_key_ok), s, ch);
1311                 free(s);
1312                 count = 0;
1313                 if (code != OK)
1314                     break;
1315             }
1316         }
1317     }
1318     return (code);
1319 }
1320
1321 static bool
1322 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int key)
1323 {
1324     bool res = FALSE;
1325
1326     AssertTCB();
1327     if (TCB->csp)
1328         res = TINFO_HAS_KEY(TCB->csp, key) == 0 ? FALSE : TRUE;
1329
1330     return res;
1331 }
1332
1333 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_TINFO_DRIVER = {
1334     TRUE,
1335         drv_CanHandle,          /* CanHandle */
1336         drv_init,               /* init */
1337         drv_release,            /* release */
1338         drv_size,               /* size */
1339         drv_sgmode,             /* sgmode */
1340         drv_conattr,            /* conattr */
1341         drv_mvcur,              /* hwcur */
1342         drv_mode,               /* mode */
1343         drv_rescol,             /* rescol */
1344         drv_rescolors,          /* rescolors */
1345         drv_setcolor,           /* color */
1346         drv_dobeepflash,        /* doBeepOrFlash */
1347         drv_initpair,           /* initpair */
1348         drv_initcolor,          /* initcolor */
1349         drv_do_color,           /* docolor */
1350         drv_initmouse,          /* initmouse */
1351         drv_testmouse,          /* testmouse */
1352         drv_setfilter,          /* setfilter */
1353         drv_hwlabel,            /* hwlabel */
1354         drv_hwlabelOnOff,       /* hwlabelOnOff */
1355         drv_doupdate,           /* update */
1356         drv_defaultcolors,      /* defaultcolors */
1357         drv_print,              /* print */
1358         drv_getsize,            /* getsize */
1359         drv_setsize,            /* setsize */
1360         drv_initacs,            /* initacs */
1361         drv_screen_init,        /* scinit */
1362         drv_wrap,               /* scexit */
1363         drv_twait,              /* twait  */
1364         drv_read,               /* read */
1365         drv_nap,                /* nap */
1366         drv_kpad,               /* kpad */
1367         drv_keyok,              /* kyOk */
1368         drv_kyExist             /* kyExist */
1369 };