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