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