1 /****************************************************************************
2 * Copyright (c) 2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer *
32 ****************************************************************************/
34 #include <curses.priv.h>
35 #define CUR ((TERMINAL*)TCB)->type.
41 #include <sys/time.h> /* needed for MacOS X DP3 */
45 MODULE_ID("$Id: tinfo_driver.c,v 1.1 2009/05/23 22:40:30 juergen Exp $")
48 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
49 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
52 # define IOCTL_WINSIZE TIOCGSIZE
53 # define STRUCT_WINSIZE struct ttysize
54 # define WINSIZE_ROWS(n) (int)n.ts_lines
55 # define WINSIZE_COLS(n) (int)n.ts_cols
58 # define IOCTL_WINSIZE TIOCGWINSZ
59 # define STRUCT_WINSIZE struct winsize
60 # define WINSIZE_ROWS(n) (int)n.ws_row
61 # define WINSIZE_COLS(n) (int)n.ws_col
66 * These should be screen structure members. They need to be globals for
67 * historical reasons. So we assign them in start_color() and also in
68 * set_term()'s screen-switching logic.
72 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
74 return CURRENT_SCREEN ? CURRENT_SCREEN->_pair_count : -1;
77 NCURSES_PUBLIC_VAR(COLORS) (void)
79 return CURRENT_SCREEN ? CURRENT_SCREEN->_color_count : -1;
82 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
83 NCURSES_EXPORT_VAR(int) COLORS = 0;
86 static bool drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *);
87 static void drv_init(TERMINAL_CONTROL_BLOCK *);
88 static void drv_release(TERMINAL_CONTROL_BLOCK *);
89 static int drv_size(TERMINAL_CONTROL_BLOCK *, int *, int *);
90 static int drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf);
91 static chtype drv_conattr(TERMINAL_CONTROL_BLOCK * TCB);
92 static int drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int
94 static int drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag);
95 static bool drv_rescol(TERMINAL_CONTROL_BLOCK * TCB);
96 static bool drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB);
97 static void drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB, bool fore, int color, int
98 (*outc) (SCREEN *, int));
99 static int drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag);
100 static void drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b);
101 static void drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB, short color, short
102 r, short g, short b);
103 static void drv_do_color(TERMINAL_CONTROL_BLOCK * TCB, short old_pair, short
105 int (*outc) (SCREEN *, int));
106 static void drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB);
107 static void drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB);
108 static void drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text);
109 static void drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag);
110 static int drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB);
111 static int drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg);
112 static int drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len);
113 static int drv_getsize(TERMINAL_CONTROL_BLOCK *, int *, int *);
114 static int drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c);
115 static void drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *, chtype *);
116 static void drv_wrap(SCREEN *);
117 static void drv_screen_init(SCREEN *);
118 static int drv_twait(TERMINAL_CONTROL_BLOCK *, int, int, int
119 *EVENTLIST_2nd(_nc_eventlist
121 static int drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf);
122 static int drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms);
123 static int drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool);
124 static int drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int, bool);
125 static bool drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int);
127 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_TINFO_DRIVER = {
129 drv_CanHandle, /* CanHandle */
131 drv_release, /* release */
133 drv_sgmode, /* sgmode */
134 drv_conattr, /* conattr */
135 drv_mvcur, /* hwcur */
137 drv_rescol, /* rescol */
138 drv_rescolors, /* rescolors */
139 drv_setcolor, /* color */
140 drv_dobeepflash, /* doBeepOrFlash */
141 drv_initpair, /* initpair */
142 drv_initcolor, /* initcolor */
143 drv_do_color, /* docolor */
144 drv_initmouse, /* initmouse */
145 drv_setfilter, /* setfilter */
146 drv_hwlabel, /* hwlabel */
147 drv_hwlabelOnOff, /* hwlabelOnOff */
148 drv_doupdate, /* update */
149 drv_defaultcolors, /* defaultcolors */
150 drv_print, /* print */
151 drv_getsize, /* getsize */
152 drv_setsize, /* setsize */
153 drv_initacs, /* initacs */
154 drv_screen_init, /* scinit */
155 drv_wrap, /* scexit */
156 drv_twait, /* twait */
160 drv_keyok, /* kyOk */
161 drv_kyExist /* kyExist */
164 #define TCBMAGIC NCDRV_MAGIC(NCDRV_TINFO)
165 #define AssertTCB() assert(TCB!=0 && TCB->magic==TCBMAGIC)
166 #define SetSP() assert(TCB->csp!=0); sp = TCB->csp
169 * This routine needs to do all the work to make curscr look
173 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
176 return _nc_tinfo_doupdate(TCB->csp);
182 ** Take the real command character out of the CC environment variable
183 ** and substitute it in for the prototype given in 'command_character'.
186 do_prototype(TERMINAL * termp)
192 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
194 if ((tmp = getenv("CC")) != 0) {
195 if ((CC = *tmp) != 0) {
196 proto = *command_character;
198 for_each_string(i, &(termp->type)) {
199 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
208 #define ret_error(code, fmt, arg) if (errret) {\
212 fprintf(stderr, fmt, arg);\
216 #define ret_error0(code, msg) if (errret) {\
220 fprintf(stderr, msg);\
224 #if USE_DATABASE || USE_TERMCAP
226 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
227 * just like tgetent().
230 grab_entry(const char *const tn, TERMTYPE *const tp)
232 char filename[PATH_MAX];
233 int status = _nc_read_entry(tn, filename, tp);
236 * If we have an entry, force all of the cancelled strings to null
237 * pointers so we don't have to test them in the rest of the library.
238 * (The terminfo compiler bypasses this logic, since it must know if
239 * a string is cancelled, for merging entries).
241 if (status == TGETENT_YES) {
243 for_each_boolean(n, tp) {
244 if (!VALID_BOOLEAN(tp->Booleans[n]))
245 tp->Booleans[n] = FALSE;
247 for_each_string(n, tp) {
248 if (tp->Strings[n] == CANCELLED_STRING)
249 tp->Strings[n] = ABSENT_STRING;
257 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
264 assert(TCB != 0 && tname != 0);
265 termp = (TERMINAL *) TCB;
267 TCB->magic = TCBMAGIC;
269 #if (USE_DATABASE || USE_TERMCAP)
270 status = grab_entry(tname, &termp->type);
275 /* try fallback list if entry on disk */
276 if (status != TGETENT_YES) {
277 const TERMTYPE *fallback = _nc_fallback(tname);
280 termp->type = *fallback;
281 status = TGETENT_YES;
285 if (status != TGETENT_YES) {
286 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx termp);
287 if (status == TGETENT_ERR) {
288 ret_error0(status, "terminals database is inaccessible\n");
289 } else if (status == TGETENT_NO) {
290 ret_error(status, "'%s': unknown terminal type.\n", tname);
295 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
296 ttytype[NAMESIZE - 1] = '\0';
299 if (command_character && getenv("CC"))
303 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
306 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
313 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag)
321 /* FIXME: should make sure that we are not in altchar mode */
324 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
325 NCURSES_SP_NAME(_nc_flush) (sp);
326 } else if (flash_screen) {
327 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
330 NCURSES_SP_NAME(_nc_flush) (sp);
334 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
337 NCURSES_SP_NAME(_nc_flush) (sp);
339 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
340 NCURSES_SP_NAME(_nc_flush) (sp);
347 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
348 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
349 * also used in the FreeBSD syscons.
352 toggled_colors(int c)
355 static const int table[] =
356 {0, 4, 2, 6, 1, 5, 3, 7,
357 8, 12, 10, 14, 9, 13, 11, 15};
364 drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len)
370 #if NCURSES_EXT_FUNCS
371 return NCURSES_SP_NAME(mcprint) (TCB->csp, data, len);
378 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg)
386 if (sp != 0 && orig_pair && orig_colors && (initialize_pair != 0)) {
387 #if NCURSES_EXT_FUNCS
388 sp->_default_color = isDefaultColor(fg) || isDefaultColor(bg);
389 sp->_has_sgr_39_49 = (NCURSES_SP_NAME(tigetflag) (NCURSES_SP_ARGx
392 sp->_default_fg = isDefaultColor(fg) ? COLOR_DEFAULT : (fg & C_MASK);
393 sp->_default_bg = isDefaultColor(bg) ? COLOR_DEFAULT : (bg & C_MASK);
394 if (sp->_color_pairs != 0) {
395 bool save = sp->_default_color;
396 sp->_default_color = TRUE;
397 NCURSES_SP_NAME(init_pair) (NCURSES_SP_ARGx
401 sp->_default_color = save;
410 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB, bool fore, int color, int (*outc)
419 if (set_a_foreground) {
420 TPUTS_TRACE("set_a_foreground");
421 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
422 TPARM_1(set_a_foreground, color), 1, outc);
424 TPUTS_TRACE("set_foreground");
425 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
426 TPARM_1(set_foreground,
427 toggled_colors(color)), 1, outc);
430 if (set_a_background) {
431 TPUTS_TRACE("set_a_background");
432 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
433 TPARM_1(set_a_background, color), 1, outc);
435 TPUTS_TRACE("set_background");
436 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
437 TPARM_1(set_background,
438 toggled_colors(color)), 1, outc);
444 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
452 if (orig_pair != 0) {
453 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_pair", orig_pair);
460 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
468 if (orig_colors != 0) {
469 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_colors", orig_colors);
476 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *linep, int *colp)
482 sp = TCB->csp; /* can be null here */
485 useEnv = sp->_use_env;
487 useEnv = _nc_prescreen.use_env;
489 /* figure out the size of the screen */
490 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
493 *linep = (int) lines;
494 *colp = (int) columns;
495 } else { /* usually want to query LINES and COLUMNS from environment */
500 /* first, look for environment variables */
501 if ((value = _nc_getenv_num("LINES")) > 0) {
504 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
507 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
510 if (*linep <= 0 || *colp <= 0) {
512 _scrsize(screendata);
513 *colp = screendata[0];
514 *linep = screendata[1];
515 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
520 /* if that didn't work, maybe we can try asking the OS */
521 if (*linep <= 0 || *colp <= 0) {
522 TERMINAL *termp = (TERMINAL *) TCB;
523 if (isatty(termp->Filedes)) {
528 if (ioctl(termp->Filedes, IOCTL_WINSIZE, &size) < 0
535 * Solaris lets users override either dimension with an
536 * environment variable.
539 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
541 *colp = WINSIZE_COLS(size);
546 #endif /* HAVE_SIZECHANGE */
548 /* if we can't get dynamic info about the size, use static */
550 *linep = (int) lines;
553 *colp = (int) columns;
556 /* the ultimate fallback, assume fixed 24x80 size */
565 * Put the derived values back in the screen-size caps, so
566 * tigetnum() and tgetnum() will do the right thing.
568 lines = (short) (*linep);
569 columns = (short) (*colp);
572 T(("screen size is %dx%d", *linep, *colp));
577 drv_getsize(TERMINAL_CONTROL_BLOCK * TCB, int *l, int *c)
580 assert(l != 0 && c != 0);
587 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c)
596 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
599 TERMINAL *_term = (TERMINAL *) TCB;
605 if (progFlag) /* prog mode */
610 * Turn off the XTABS bit in the tty structure if it was on.
612 if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
614 _term->Nttyb.c_oflag &= ~OFLAGS_TABS;
616 _term->Nttyb.sg_flags &= ~XTABS;
621 /* reset_prog_mode */
622 if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
625 _nc_keypad(sp, TRUE);
626 NC_BUFFERED(sp, TRUE);
631 } else { /* shell mode */
635 * If XTABS was on, remove the tab and backtab capabilities.
637 if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
639 if (_term->Ottyb.c_oflag & OFLAGS_TABS)
640 tab = back_tab = NULL;
642 if (_term->Ottyb.sg_flags & XTABS)
643 tab = back_tab = NULL;
648 /* reset_shell_mode */
650 _nc_keypad(sp, FALSE);
651 NCURSES_SP_NAME(_nc_flush) (sp);
652 NC_BUFFERED(sp, FALSE);
654 code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
665 NCURSES_SP_NAME(_nc_screen_wrap) (sp);
666 NCURSES_SP_NAME(_nc_mvcur_wrap) (sp); /* wrap up cursor addressing */
671 drv_release(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
675 # define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
678 drv_screen_init(SCREEN *sp)
680 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
685 * Check for mismatched graphic-rendition capabilities. Most SVr4
686 * terminfo trees contain entries that have rmul or rmso equated to
687 * sgr0 (Solaris curses copes with those entries). We do this only
688 * for curses, since many termcap applications assume that
689 * smso/rmso and smul/rmul are paired, and will not function
690 * properly if we remove rmso or rmul. Curses applications
691 * shouldn't be looking at this detail.
693 sp->_use_rmso = SGR0_TEST(exit_standout_mode);
694 sp->_use_rmul = SGR0_TEST(exit_underline_mode);
697 * Check whether we can optimize scrolling under dumb terminals in
698 * case we do not have any of these capabilities, scrolling
699 * optimization will be useless.
701 sp->_scrolling = ((scroll_forward && scroll_reverse) ||
709 NCURSES_SP_NAME(baudrate) (sp);
711 NCURSES_SP_NAME(_nc_mvcur_init) (sp);
712 /* initialize terminal to a sane state */
713 NCURSES_SP_NAME(_nc_screen_init) (sp);
717 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
724 trm = (TERMINAL *) TCB;
727 TCB->info.initcolor = initialize_color;
728 TCB->info.canchange = can_change;
729 TCB->info.hascolor = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
730 && (((set_foreground != NULL)
731 && (set_background != NULL))
732 || ((set_a_foreground != NULL)
733 && (set_a_background != NULL))
734 || set_color_pair)) ? TRUE : FALSE);
736 TCB->info.caninit = !(exit_ca_mode && non_rev_rmcup);
738 TCB->info.maxpairs = VALID_NUMERIC(max_pairs) ? max_pairs : 0;
739 TCB->info.maxcolors = VALID_NUMERIC(max_colors) ? max_colors : 0;
740 TCB->info.numlabels = VALID_NUMERIC(num_labels) ? num_labels : 0;
741 TCB->info.labelwidth = VALID_NUMERIC(label_width) ? label_width : 0;
742 TCB->info.labelheight = VALID_NUMERIC(label_height) ? label_height : 0;
743 TCB->info.nocolorvideo = VALID_NUMERIC(no_color_video) ? no_color_video
745 TCB->info.tabsize = VALID_NUMERIC(init_tabs) ? (int) init_tabs : 8;
747 TCB->info.defaultPalette = hue_lightness_saturation ? _nc_hls_palette : _nc_cga_palette;
750 * If an application calls setupterm() rather than initscr() or
751 * newterm(), we will not have the def_prog_mode() call in
752 * _nc_setupscreen(). Do it now anyway, so we can initialize the
755 if (isatty(trm->Filedes)) {
756 TCB->drv->mode(TCB, TRUE, TRUE);
760 #define MAX_PALETTE 8
761 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
764 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b)
771 if ((initialize_pair != NULL) && InPalette(f) && InPalette(b)) {
772 const color_t *tp = InfoOf(sp).defaultPalette;
775 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
777 tp[f].red, tp[f].green, tp[f].blue,
778 tp[b].red, tp[b].green, tp[b].blue));
780 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
782 TPARM_7(initialize_pair,
784 tp[f].red, tp[f].green, tp[f].blue,
785 tp[b].red, tp[b].green, tp[b].blue));
790 default_fg(SCREEN *sp)
792 #if NCURSES_EXT_FUNCS
793 return (sp != 0) ? sp->_default_fg : COLOR_WHITE;
800 default_bg(SCREEN *sp)
802 #if NCURSES_EXT_FUNCS
803 return sp != 0 ? sp->_default_bg : COLOR_BLACK;
810 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
811 short color, short r, short g, short b)
813 SCREEN *sp = TCB->csp;
816 if (initialize_color != NULL) {
817 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
819 TPARM_4(initialize_color, color, r, g, b));
824 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
828 NCURSES_SP_OUTC outc)
830 SCREEN *sp = TCB->csp;
831 NCURSES_COLOR_T fg = COLOR_DEFAULT;
832 NCURSES_COLOR_T bg = COLOR_DEFAULT;
833 NCURSES_COLOR_T old_fg, old_bg;
839 if (pair < 0 || pair >= COLOR_PAIRS) {
841 } else if (pair != 0) {
842 if (set_color_pair) {
843 TPUTS_TRACE("set_color_pair");
844 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
845 TPARM_1(set_color_pair, pair), 1, outc);
847 } else if (sp != 0) {
848 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
857 && NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
861 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
862 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
863 #if NCURSES_EXT_FUNCS
865 * A minor optimization - but extension. If "AX" is specified in
866 * the terminal description, treat it as screen's indicator of ECMA
867 * SGR 39 and SGR 49, and assume the two sequences are independent.
869 if (sp->_has_sgr_39_49
870 && isDefaultColor(old_bg)
871 && !isDefaultColor(old_fg)) {
872 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
873 } else if (sp->_has_sgr_39_49
874 && isDefaultColor(old_fg)
875 && !isDefaultColor(old_bg)) {
876 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
887 #if NCURSES_EXT_FUNCS
888 if (isDefaultColor(fg))
890 if (isDefaultColor(bg))
895 NCURSES_COLOR_T xx = fg;
900 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
903 if (!isDefaultColor(fg)) {
904 drv_setcolor(TCB, TRUE, fg, outc);
906 if (!isDefaultColor(bg)) {
907 drv_setcolor(TCB, FALSE, bg, outc);
911 #define xterm_kmous "\033[M"
913 init_xterm_mouse(SCREEN *sp)
915 sp->_mouse_type = M_XTERM;
916 sp->_mouse_xtermcap = NCURSES_SP_NAME(tigetstr) (NCURSES_SP_ARGx "XM");
917 if (!VALID_STRING(sp->_mouse_xtermcap))
918 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
922 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
929 /* we know how to recognize mouse events under "xterm" */
931 if (key_mouse != 0) {
932 if (!strcmp(key_mouse, xterm_kmous)
933 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
934 init_xterm_mouse(sp);
936 } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
937 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
938 init_xterm_mouse(sp);
944 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int ynew, int xnew)
946 SCREEN *sp = TCB->csp;
948 return _nc_tinfo_mvcur(sp, yold, xold, ynew, xnew);
952 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text)
954 SCREEN *sp = TCB->csp;
957 if (labnum > 0 && labnum <= num_labels) {
958 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
960 TPARM_2(plab_norm, labnum, text));
965 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag)
967 SCREEN *sp = TCB->csp;
971 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_on", label_on);
973 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_off", label_off);
978 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB)
980 SCREEN *sp = TCB->csp;
981 chtype attrs = A_NORMAL;
984 if (enter_alt_charset_mode)
985 attrs |= A_ALTCHARSET;
987 if (enter_blink_mode)
996 if (enter_reverse_mode)
999 if (enter_standout_mode)
1000 attrs |= A_STANDOUT;
1002 if (enter_protected_mode)
1005 if (enter_secure_mode)
1008 if (enter_underline_mode)
1009 attrs |= A_UNDERLINE;
1011 if (sp && sp->_coloron)
1018 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
1023 cursor_down = parm_down_cursor = 0;
1025 cursor_up = parm_up_cursor = 0;
1027 cursor_home = carriage_return;
1031 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
1033 SCREEN *sp = TCB->csp;
1034 TERMINAL *_term = (TERMINAL *) TCB;
1040 if (SET_TTY(_term->Filedes, buf) != 0) {
1043 if (errno == ENOTTY) {
1053 if (GET_TTY(_term->Filedes, buf) != 0) {
1065 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *real_map, chtype *fake_map)
1067 SCREEN *sp = TCB->csp;
1071 if (ena_acs != NULL) {
1072 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "ena_acs", ena_acs);
1074 #if NCURSES_EXT_FUNCS
1076 * Linux console "supports" the "PC ROM" character set by the coincidence
1077 * that smpch/rmpch and smacs/rmacs have the same values. ncurses has
1078 * no codepage support (see SCO Merge for an example). Outside of the
1079 * values defined in acsc, there are no definitions for the "PC ROM"
1080 * character set (assumed by some applications to be codepage 437), but we
1081 * allow those applications to use those codepoints.
1083 * test/blue.c uses this feature.
1085 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
1086 if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
1087 PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
1089 for (i = 1; i < ACS_LEN; ++i) {
1090 if (real_map[i] == 0) {
1092 if (real_map != fake_map) {
1094 sp->_screen_acs_map[i] = TRUE;
1101 if (acs_chars != NULL) {
1103 size_t length = strlen(acs_chars);
1105 while (i + 1 < length) {
1106 if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
1107 real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
1109 sp->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
1115 /* Show the equivalent mapping, noting if it does not match the
1116 * given attribute, whether by re-ordering or duplication.
1118 if (USE_TRACEF(TRACE_CALLS)) {
1120 char show[ACS_LEN * 2 + 1];
1121 for (n = 1, m = 0; n < ACS_LEN; n++) {
1122 if (real_map[n] != 0) {
1123 show[m++] = (char) n;
1124 show[m++] = (char) ChCharOf(real_map[n]);
1128 if (acs_chars == NULL || strcmp(acs_chars, show))
1129 _tracef("%s acs_chars %s",
1130 (acs_chars == NULL) ? "NULL" : "READ",
1131 _nc_visbuf(acs_chars));
1132 _tracef("%s acs_chars %s",
1135 : (strcmp(acs_chars, show)
1140 _nc_unlock_global(tracef);
1145 #define ENSURE_TINFO(sp) (TCBOf(sp)->drv->isTerminfo)
1147 NCURSES_EXPORT(void)
1148 _nc_cookie_init(SCREEN *sp)
1150 bool support_cookies = USE_XMC_SUPPORT;
1151 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) (sp->_term);
1153 if (sp == 0 || !ENSURE_TINFO(sp))
1158 * If we have no magic-cookie support compiled-in, or if it is suppressed
1159 * in the environment, reset the support-flag.
1161 if (magic_cookie_glitch >= 0) {
1162 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
1163 support_cookies = FALSE;
1168 if (!support_cookies && magic_cookie_glitch >= 0) {
1169 T(("will disable attributes to work w/o magic cookies"));
1172 if (magic_cookie_glitch > 0) { /* tvi, wyse */
1174 sp->_xmc_triggers = sp->_ok_attributes & (
1186 * We "should" treat colors as an attribute. The wyse350 (and its
1187 * clones) appear to be the only ones that have both colors and magic
1191 sp->_xmc_triggers |= A_COLOR;
1194 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
1196 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
1198 * Supporting line-drawing may be possible. But make the regular
1199 * video attributes work first.
1201 acs_chars = ABSENT_STRING;
1202 ena_acs = ABSENT_STRING;
1203 enter_alt_charset_mode = ABSENT_STRING;
1204 exit_alt_charset_mode = ABSENT_STRING;
1207 * To keep the cookie support simple, suppress all of the optimization
1208 * hooks except for clear_screen and the cursor addressing.
1210 if (support_cookies) {
1211 clr_eol = ABSENT_STRING;
1212 clr_eos = ABSENT_STRING;
1213 set_attributes = ABSENT_STRING;
1216 } else if (magic_cookie_glitch == 0) { /* hpterm */
1220 * If magic cookies are not supported, cancel the strings that set
1223 if (!support_cookies && magic_cookie_glitch >= 0) {
1224 magic_cookie_glitch = ABSENT_NUMERIC;
1225 set_attributes = ABSENT_STRING;
1226 enter_blink_mode = ABSENT_STRING;
1227 enter_bold_mode = ABSENT_STRING;
1228 enter_dim_mode = ABSENT_STRING;
1229 enter_reverse_mode = ABSENT_STRING;
1230 enter_standout_mode = ABSENT_STRING;
1231 enter_underline_mode = ABSENT_STRING;
1234 /* initialize normal acs before wide, since we use mapping in the latter */
1235 #if !USE_WIDEC_SUPPORT
1236 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
1239 enter_alt_charset_mode = NULL;
1240 exit_alt_charset_mode = NULL;
1241 set_attributes = NULL;
1247 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1251 EVENTLIST_2nd(_nc_eventlist * evl))
1258 return _nc_timed_wait(sp, mode, milliseconds, timeleft EVENTLIST_2nd(evl));
1262 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1265 unsigned char c2 = 0;
1272 n = read(sp->_ifd, &c2, 1);
1278 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1282 struct timespec request, remaining;
1283 request.tv_sec = ms / 1000;
1284 request.tv_nsec = (ms % 1000) * 1000000;
1285 while (nanosleep(&request, &remaining) == -1
1286 && errno == EINTR) {
1287 request = remaining;
1291 _nc_timed_wait(0, 0, ms, (int *) 0 EVENTLIST_2nd(0));
1297 __nc_putp(SCREEN *sp, const char *name GCC_UNUSED, const char *value)
1302 rc = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx name, value);
1308 __nc_putp_flush(SCREEN *sp, const char *name, const char *value)
1310 int rc = __nc_putp(sp, name, value);
1312 NCURSES_SP_NAME(_nc_flush) (sp);
1318 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag)
1329 (void) __nc_putp_flush(sp, "keypad_xmit", keypad_xmit);
1330 } else if (!flag && keypad_local) {
1331 (void) __nc_putp_flush(sp, "keypad_local", keypad_local);
1333 if (flag && !sp->_tried) {
1334 _nc_init_keytry(sp);
1344 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int c, bool flag)
1355 unsigned ch = (unsigned) c;
1357 while ((s = _nc_expand_try(sp->_key_ok, ch, &count, 0)) != 0
1358 && _nc_remove_key(&(sp->_key_ok), ch)) {
1359 code = _nc_add_to_try(&(sp->_keytry), s, ch);
1366 while ((s = _nc_expand_try(sp->_keytry, ch, &count, 0)) != 0
1367 && _nc_remove_key(&(sp->_keytry), ch)) {
1368 code = _nc_add_to_try(&(sp->_key_ok), s, ch);
1380 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int key)
1386 res = _nc_tinfo_has_key(TCB->csp, key) == 0 ? FALSE : TRUE;