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