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 */
46 # if !defined(sun) || !TERMIOS
48 # include <sys/ioctl.h>
53 MODULE_ID("$Id: tinfo_driver.c,v 1.5 2009/10/31 20:32:01 tom Exp $")
56 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
57 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
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
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
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.
80 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
82 return CURRENT_SCREEN ? CURRENT_SCREEN->_pair_count : -1;
85 NCURSES_PUBLIC_VAR(COLORS) (void)
87 return CURRENT_SCREEN ? CURRENT_SCREEN->_color_count : -1;
90 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
91 NCURSES_EXPORT_VAR(int) COLORS = 0;
94 static bool drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *);
95 static void drv_init(TERMINAL_CONTROL_BLOCK *);
96 static void drv_release(TERMINAL_CONTROL_BLOCK *);
97 static int drv_size(TERMINAL_CONTROL_BLOCK *, int *, int *);
98 static int drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf);
99 static chtype drv_conattr(TERMINAL_CONTROL_BLOCK * TCB);
100 static int drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int
102 static int drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag);
103 static bool drv_rescol(TERMINAL_CONTROL_BLOCK * TCB);
104 static bool drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB);
105 static void drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB, bool fore, int color, int
106 (*outc) (SCREEN *, int));
107 static int drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag);
108 static void drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b);
109 static void drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB, short color, short
110 r, short g, short b);
111 static void drv_do_color(TERMINAL_CONTROL_BLOCK * TCB, short old_pair, short
113 int (*outc) (SCREEN *, int));
114 static void drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB);
115 static void drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB);
116 static void drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text);
117 static void drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag);
118 static int drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB);
119 static int drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg);
120 static int drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len);
121 static int drv_getsize(TERMINAL_CONTROL_BLOCK *, int *, int *);
122 static int drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c);
123 static void drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *, chtype *);
124 static void drv_wrap(SCREEN *);
125 static void drv_screen_init(SCREEN *);
126 static int drv_twait(TERMINAL_CONTROL_BLOCK *, int, int, int
127 *EVENTLIST_2nd(_nc_eventlist
129 static int drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf);
130 static int drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms);
131 static int drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool);
132 static int drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int, bool);
133 static bool drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int);
135 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_TINFO_DRIVER = {
137 drv_CanHandle, /* CanHandle */
139 drv_release, /* release */
141 drv_sgmode, /* sgmode */
142 drv_conattr, /* conattr */
143 drv_mvcur, /* hwcur */
145 drv_rescol, /* rescol */
146 drv_rescolors, /* rescolors */
147 drv_setcolor, /* color */
148 drv_dobeepflash, /* doBeepOrFlash */
149 drv_initpair, /* initpair */
150 drv_initcolor, /* initcolor */
151 drv_do_color, /* docolor */
152 drv_initmouse, /* initmouse */
153 drv_setfilter, /* setfilter */
154 drv_hwlabel, /* hwlabel */
155 drv_hwlabelOnOff, /* hwlabelOnOff */
156 drv_doupdate, /* update */
157 drv_defaultcolors, /* defaultcolors */
158 drv_print, /* print */
159 drv_getsize, /* getsize */
160 drv_setsize, /* setsize */
161 drv_initacs, /* initacs */
162 drv_screen_init, /* scinit */
163 drv_wrap, /* scexit */
164 drv_twait, /* twait */
168 drv_keyok, /* kyOk */
169 drv_kyExist /* kyExist */
172 #define TCBMAGIC NCDRV_MAGIC(NCDRV_TINFO)
173 #define AssertTCB() assert(TCB!=0 && TCB->magic==TCBMAGIC)
174 #define SetSP() assert(TCB->csp!=0); sp = TCB->csp
177 * This routine needs to do all the work to make curscr look
181 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
184 return TINFO_DOUPDATE(TCB->csp);
187 #define ret_error(code, fmt, arg) if (errret) {\
191 fprintf(stderr, fmt, arg);\
195 #define ret_error0(code, msg) if (errret) {\
199 fprintf(stderr, msg);\
203 #if USE_DATABASE || USE_TERMCAP
205 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
206 * just like tgetent().
209 grab_entry(const char *const tn, TERMTYPE *const tp)
211 char filename[PATH_MAX];
212 int status = _nc_read_entry(tn, filename, tp);
215 * If we have an entry, force all of the cancelled strings to null
216 * pointers so we don't have to test them in the rest of the library.
217 * (The terminfo compiler bypasses this logic, since it must know if
218 * a string is cancelled, for merging entries).
220 if (status == TGETENT_YES) {
222 for_each_boolean(n, tp) {
223 if (!VALID_BOOLEAN(tp->Booleans[n]))
224 tp->Booleans[n] = FALSE;
226 for_each_string(n, tp) {
227 if (tp->Strings[n] == CANCELLED_STRING)
228 tp->Strings[n] = ABSENT_STRING;
236 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
243 assert(TCB != 0 && tname != 0);
244 termp = (TERMINAL *) TCB;
246 TCB->magic = TCBMAGIC;
248 #if (USE_DATABASE || USE_TERMCAP)
249 status = grab_entry(tname, &termp->type);
254 /* try fallback list if entry on disk */
255 if (status != TGETENT_YES) {
256 const TERMTYPE *fallback = _nc_fallback(tname);
259 termp->type = *fallback;
260 status = TGETENT_YES;
264 if (status != TGETENT_YES) {
265 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx termp);
266 if (status == TGETENT_ERR) {
267 ret_error0(status, "terminals database is inaccessible\n");
268 } else if (status == TGETENT_NO) {
269 ret_error(status, "'%s': unknown terminal type.\n", tname);
274 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
275 ttytype[NAMESIZE - 1] = '\0';
278 if (command_character)
279 _nc_tinfo_cmdch(termp, *command_character);
282 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
285 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
292 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag)
300 /* FIXME: should make sure that we are not in altchar mode */
303 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
304 NCURSES_SP_NAME(_nc_flush) (sp);
305 } else if (flash_screen) {
306 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
309 NCURSES_SP_NAME(_nc_flush) (sp);
313 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
316 NCURSES_SP_NAME(_nc_flush) (sp);
318 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
319 NCURSES_SP_NAME(_nc_flush) (sp);
326 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
327 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
328 * also used in the FreeBSD syscons.
331 toggled_colors(int c)
334 static const int table[] =
335 {0, 4, 2, 6, 1, 5, 3, 7,
336 8, 12, 10, 14, 9, 13, 11, 15};
343 drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len)
349 #if NCURSES_EXT_FUNCS
350 return NCURSES_SP_NAME(mcprint) (TCB->csp, data, len);
357 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg)
365 if (sp != 0 && orig_pair && orig_colors && (initialize_pair != 0)) {
366 #if NCURSES_EXT_FUNCS
367 sp->_default_color = isDefaultColor(fg) || isDefaultColor(bg);
368 sp->_has_sgr_39_49 = (NCURSES_SP_NAME(tigetflag) (NCURSES_SP_ARGx
371 sp->_default_fg = isDefaultColor(fg) ? COLOR_DEFAULT : (fg & C_MASK);
372 sp->_default_bg = isDefaultColor(bg) ? COLOR_DEFAULT : (bg & C_MASK);
373 if (sp->_color_pairs != 0) {
374 bool save = sp->_default_color;
375 sp->_default_color = TRUE;
376 NCURSES_SP_NAME(init_pair) (NCURSES_SP_ARGx
380 sp->_default_color = save;
389 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB, bool fore, int color, int (*outc)
398 if (set_a_foreground) {
399 TPUTS_TRACE("set_a_foreground");
400 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
401 TPARM_1(set_a_foreground, color), 1, outc);
403 TPUTS_TRACE("set_foreground");
404 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
405 TPARM_1(set_foreground,
406 toggled_colors(color)), 1, outc);
409 if (set_a_background) {
410 TPUTS_TRACE("set_a_background");
411 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
412 TPARM_1(set_a_background, color), 1, outc);
414 TPUTS_TRACE("set_background");
415 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
416 TPARM_1(set_background,
417 toggled_colors(color)), 1, outc);
423 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
431 if (orig_pair != 0) {
432 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_pair", orig_pair);
439 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
447 if (orig_colors != 0) {
448 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_colors", orig_colors);
455 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *linep, int *colp)
461 sp = TCB->csp; /* can be null here */
464 useEnv = sp->_use_env;
466 useEnv = _nc_prescreen.use_env;
468 /* figure out the size of the screen */
469 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
472 *linep = (int) lines;
473 *colp = (int) columns;
474 } else { /* usually want to query LINES and COLUMNS from environment */
479 /* first, look for environment variables */
480 if ((value = _nc_getenv_num("LINES")) > 0) {
483 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
486 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
489 if (*linep <= 0 || *colp <= 0) {
491 _scrsize(screendata);
492 *colp = screendata[0];
493 *linep = screendata[1];
494 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
499 /* if that didn't work, maybe we can try asking the OS */
500 if (*linep <= 0 || *colp <= 0) {
501 TERMINAL *termp = (TERMINAL *) TCB;
502 if (isatty(termp->Filedes)) {
507 if (ioctl(termp->Filedes, IOCTL_WINSIZE, &size) < 0
514 * Solaris lets users override either dimension with an
515 * environment variable.
518 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
520 *colp = WINSIZE_COLS(size);
525 #endif /* HAVE_SIZECHANGE */
527 /* if we can't get dynamic info about the size, use static */
529 *linep = (int) lines;
532 *colp = (int) columns;
535 /* the ultimate fallback, assume fixed 24x80 size */
544 * Put the derived values back in the screen-size caps, so
545 * tigetnum() and tgetnum() will do the right thing.
547 lines = (short) (*linep);
548 columns = (short) (*colp);
551 T(("screen size is %dx%d", *linep, *colp));
556 drv_getsize(TERMINAL_CONTROL_BLOCK * TCB, int *l, int *c)
559 assert(l != 0 && c != 0);
566 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c)
575 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
578 TERMINAL *_term = (TERMINAL *) TCB;
584 if (progFlag) /* prog mode */
589 * Turn off the XTABS bit in the tty structure if it was on.
591 if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
593 _term->Nttyb.c_oflag &= ~OFLAGS_TABS;
595 _term->Nttyb.sg_flags &= ~XTABS;
600 /* reset_prog_mode */
601 if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
604 _nc_keypad(sp, TRUE);
605 NC_BUFFERED(sp, TRUE);
610 } else { /* shell mode */
614 * If XTABS was on, remove the tab and backtab capabilities.
616 if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
618 if (_term->Ottyb.c_oflag & OFLAGS_TABS)
619 tab = back_tab = NULL;
621 if (_term->Ottyb.sg_flags & XTABS)
622 tab = back_tab = NULL;
627 /* reset_shell_mode */
629 _nc_keypad(sp, FALSE);
630 NCURSES_SP_NAME(_nc_flush) (sp);
631 NC_BUFFERED(sp, FALSE);
633 code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
644 NCURSES_SP_NAME(_nc_screen_wrap) (sp);
645 NCURSES_SP_NAME(_nc_mvcur_wrap) (sp); /* wrap up cursor addressing */
650 drv_release(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
654 # define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
657 drv_screen_init(SCREEN *sp)
659 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
664 * Check for mismatched graphic-rendition capabilities. Most SVr4
665 * terminfo trees contain entries that have rmul or rmso equated to
666 * sgr0 (Solaris curses copes with those entries). We do this only
667 * for curses, since many termcap applications assume that
668 * smso/rmso and smul/rmul are paired, and will not function
669 * properly if we remove rmso or rmul. Curses applications
670 * shouldn't be looking at this detail.
672 sp->_use_rmso = SGR0_TEST(exit_standout_mode);
673 sp->_use_rmul = SGR0_TEST(exit_underline_mode);
676 * Check whether we can optimize scrolling under dumb terminals in
677 * case we do not have any of these capabilities, scrolling
678 * optimization will be useless.
680 sp->_scrolling = ((scroll_forward && scroll_reverse) ||
688 NCURSES_SP_NAME(baudrate) (sp);
690 NCURSES_SP_NAME(_nc_mvcur_init) (sp);
691 /* initialize terminal to a sane state */
692 NCURSES_SP_NAME(_nc_screen_init) (sp);
696 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
703 trm = (TERMINAL *) TCB;
706 TCB->info.initcolor = initialize_color;
707 TCB->info.canchange = can_change;
708 TCB->info.hascolor = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
709 && (((set_foreground != NULL)
710 && (set_background != NULL))
711 || ((set_a_foreground != NULL)
712 && (set_a_background != NULL))
713 || set_color_pair)) ? TRUE : FALSE);
715 TCB->info.caninit = !(exit_ca_mode && non_rev_rmcup);
717 TCB->info.maxpairs = VALID_NUMERIC(max_pairs) ? max_pairs : 0;
718 TCB->info.maxcolors = VALID_NUMERIC(max_colors) ? max_colors : 0;
719 TCB->info.numlabels = VALID_NUMERIC(num_labels) ? num_labels : 0;
720 TCB->info.labelwidth = VALID_NUMERIC(label_width) ? label_width : 0;
721 TCB->info.labelheight = VALID_NUMERIC(label_height) ? label_height : 0;
722 TCB->info.nocolorvideo = VALID_NUMERIC(no_color_video) ? no_color_video
724 TCB->info.tabsize = VALID_NUMERIC(init_tabs) ? (int) init_tabs : 8;
726 TCB->info.defaultPalette = hue_lightness_saturation ? _nc_hls_palette : _nc_cga_palette;
729 * If an application calls setupterm() rather than initscr() or
730 * newterm(), we will not have the def_prog_mode() call in
731 * _nc_setupscreen(). Do it now anyway, so we can initialize the
734 if (isatty(trm->Filedes)) {
735 TCB->drv->mode(TCB, TRUE, TRUE);
739 #define MAX_PALETTE 8
740 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
743 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b)
750 if ((initialize_pair != NULL) && InPalette(f) && InPalette(b)) {
751 const color_t *tp = InfoOf(sp).defaultPalette;
754 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
756 tp[f].red, tp[f].green, tp[f].blue,
757 tp[b].red, tp[b].green, tp[b].blue));
759 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
761 TPARM_7(initialize_pair,
763 tp[f].red, tp[f].green, tp[f].blue,
764 tp[b].red, tp[b].green, tp[b].blue));
769 default_fg(SCREEN *sp)
771 #if NCURSES_EXT_FUNCS
772 return (sp != 0) ? sp->_default_fg : COLOR_WHITE;
779 default_bg(SCREEN *sp)
781 #if NCURSES_EXT_FUNCS
782 return sp != 0 ? sp->_default_bg : COLOR_BLACK;
789 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
790 short color, short r, short g, short b)
792 SCREEN *sp = TCB->csp;
795 if (initialize_color != NULL) {
796 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
798 TPARM_4(initialize_color, color, r, g, b));
803 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
807 NCURSES_SP_OUTC outc)
809 SCREEN *sp = TCB->csp;
810 NCURSES_COLOR_T fg = COLOR_DEFAULT;
811 NCURSES_COLOR_T bg = COLOR_DEFAULT;
812 NCURSES_COLOR_T old_fg, old_bg;
818 if (pair < 0 || pair >= COLOR_PAIRS) {
820 } else if (pair != 0) {
821 if (set_color_pair) {
822 TPUTS_TRACE("set_color_pair");
823 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
824 TPARM_1(set_color_pair, pair), 1, outc);
826 } else if (sp != 0) {
827 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
836 && NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
840 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
841 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
842 #if NCURSES_EXT_FUNCS
844 * A minor optimization - but extension. If "AX" is specified in
845 * the terminal description, treat it as screen's indicator of ECMA
846 * SGR 39 and SGR 49, and assume the two sequences are independent.
848 if (sp->_has_sgr_39_49
849 && isDefaultColor(old_bg)
850 && !isDefaultColor(old_fg)) {
851 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
852 } else if (sp->_has_sgr_39_49
853 && isDefaultColor(old_fg)
854 && !isDefaultColor(old_bg)) {
855 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
866 #if NCURSES_EXT_FUNCS
867 if (isDefaultColor(fg))
869 if (isDefaultColor(bg))
874 NCURSES_COLOR_T xx = fg;
879 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
882 if (!isDefaultColor(fg)) {
883 drv_setcolor(TCB, TRUE, fg, outc);
885 if (!isDefaultColor(bg)) {
886 drv_setcolor(TCB, FALSE, bg, outc);
890 #define xterm_kmous "\033[M"
892 init_xterm_mouse(SCREEN *sp)
894 sp->_mouse_type = M_XTERM;
895 sp->_mouse_xtermcap = NCURSES_SP_NAME(tigetstr) (NCURSES_SP_ARGx "XM");
896 if (!VALID_STRING(sp->_mouse_xtermcap))
897 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
901 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
908 /* we know how to recognize mouse events under "xterm" */
910 if (key_mouse != 0) {
911 if (!strcmp(key_mouse, xterm_kmous)
912 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
913 init_xterm_mouse(sp);
915 } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
916 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
917 init_xterm_mouse(sp);
923 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int ynew, int xnew)
925 SCREEN *sp = TCB->csp;
927 return TINFO_MVCUR(sp, yold, xold, ynew, xnew);
931 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text)
933 SCREEN *sp = TCB->csp;
936 if (labnum > 0 && labnum <= num_labels) {
937 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
939 TPARM_2(plab_norm, labnum, text));
944 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag)
946 SCREEN *sp = TCB->csp;
950 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_on", label_on);
952 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_off", label_off);
957 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB)
959 SCREEN *sp = TCB->csp;
960 chtype attrs = A_NORMAL;
963 if (enter_alt_charset_mode)
964 attrs |= A_ALTCHARSET;
966 if (enter_blink_mode)
975 if (enter_reverse_mode)
978 if (enter_standout_mode)
981 if (enter_protected_mode)
984 if (enter_secure_mode)
987 if (enter_underline_mode)
988 attrs |= A_UNDERLINE;
990 if (sp && sp->_coloron)
997 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
1002 cursor_down = parm_down_cursor = 0;
1004 cursor_up = parm_up_cursor = 0;
1006 cursor_home = carriage_return;
1010 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
1012 SCREEN *sp = TCB->csp;
1013 TERMINAL *_term = (TERMINAL *) TCB;
1019 if (SET_TTY(_term->Filedes, buf) != 0) {
1022 if (errno == ENOTTY) {
1032 if (GET_TTY(_term->Filedes, buf) != 0) {
1044 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *real_map, chtype *fake_map)
1046 SCREEN *sp = TCB->csp;
1050 if (ena_acs != NULL) {
1051 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "ena_acs", ena_acs);
1053 #if NCURSES_EXT_FUNCS
1055 * Linux console "supports" the "PC ROM" character set by the coincidence
1056 * that smpch/rmpch and smacs/rmacs have the same values. ncurses has
1057 * no codepage support (see SCO Merge for an example). Outside of the
1058 * values defined in acsc, there are no definitions for the "PC ROM"
1059 * character set (assumed by some applications to be codepage 437), but we
1060 * allow those applications to use those codepoints.
1062 * test/blue.c uses this feature.
1064 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
1065 if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
1066 PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
1068 for (i = 1; i < ACS_LEN; ++i) {
1069 if (real_map[i] == 0) {
1071 if (real_map != fake_map) {
1073 sp->_screen_acs_map[i] = TRUE;
1080 if (acs_chars != NULL) {
1082 size_t length = strlen(acs_chars);
1084 while (i + 1 < length) {
1085 if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
1086 real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
1088 sp->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
1094 /* Show the equivalent mapping, noting if it does not match the
1095 * given attribute, whether by re-ordering or duplication.
1097 if (USE_TRACEF(TRACE_CALLS)) {
1099 char show[ACS_LEN * 2 + 1];
1100 for (n = 1, m = 0; n < ACS_LEN; n++) {
1101 if (real_map[n] != 0) {
1102 show[m++] = (char) n;
1103 show[m++] = (char) ChCharOf(real_map[n]);
1107 if (acs_chars == NULL || strcmp(acs_chars, show))
1108 _tracef("%s acs_chars %s",
1109 (acs_chars == NULL) ? "NULL" : "READ",
1110 _nc_visbuf(acs_chars));
1111 _tracef("%s acs_chars %s",
1114 : (strcmp(acs_chars, show)
1119 _nc_unlock_global(tracef);
1124 #define ENSURE_TINFO(sp) (TCBOf(sp)->drv->isTerminfo)
1126 NCURSES_EXPORT(void)
1127 _nc_cookie_init(SCREEN *sp)
1129 bool support_cookies = USE_XMC_SUPPORT;
1130 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) (sp->_term);
1132 if (sp == 0 || !ENSURE_TINFO(sp))
1137 * If we have no magic-cookie support compiled-in, or if it is suppressed
1138 * in the environment, reset the support-flag.
1140 if (magic_cookie_glitch >= 0) {
1141 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
1142 support_cookies = FALSE;
1147 if (!support_cookies && magic_cookie_glitch >= 0) {
1148 T(("will disable attributes to work w/o magic cookies"));
1151 if (magic_cookie_glitch > 0) { /* tvi, wyse */
1153 sp->_xmc_triggers = sp->_ok_attributes & (
1165 * We "should" treat colors as an attribute. The wyse350 (and its
1166 * clones) appear to be the only ones that have both colors and magic
1170 sp->_xmc_triggers |= A_COLOR;
1173 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
1175 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
1177 * Supporting line-drawing may be possible. But make the regular
1178 * video attributes work first.
1180 acs_chars = ABSENT_STRING;
1181 ena_acs = ABSENT_STRING;
1182 enter_alt_charset_mode = ABSENT_STRING;
1183 exit_alt_charset_mode = ABSENT_STRING;
1186 * To keep the cookie support simple, suppress all of the optimization
1187 * hooks except for clear_screen and the cursor addressing.
1189 if (support_cookies) {
1190 clr_eol = ABSENT_STRING;
1191 clr_eos = ABSENT_STRING;
1192 set_attributes = ABSENT_STRING;
1195 } else if (magic_cookie_glitch == 0) { /* hpterm */
1199 * If magic cookies are not supported, cancel the strings that set
1202 if (!support_cookies && magic_cookie_glitch >= 0) {
1203 magic_cookie_glitch = ABSENT_NUMERIC;
1204 set_attributes = ABSENT_STRING;
1205 enter_blink_mode = ABSENT_STRING;
1206 enter_bold_mode = ABSENT_STRING;
1207 enter_dim_mode = ABSENT_STRING;
1208 enter_reverse_mode = ABSENT_STRING;
1209 enter_standout_mode = ABSENT_STRING;
1210 enter_underline_mode = ABSENT_STRING;
1213 /* initialize normal acs before wide, since we use mapping in the latter */
1214 #if !USE_WIDEC_SUPPORT
1215 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
1218 enter_alt_charset_mode = NULL;
1219 exit_alt_charset_mode = NULL;
1220 set_attributes = NULL;
1226 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1230 EVENTLIST_2nd(_nc_eventlist * evl))
1237 return _nc_timed_wait(sp, mode, milliseconds, timeleft EVENTLIST_2nd(evl));
1241 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1244 unsigned char c2 = 0;
1251 n = read(sp->_ifd, &c2, 1);
1257 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1261 struct timespec request, remaining;
1262 request.tv_sec = ms / 1000;
1263 request.tv_nsec = (ms % 1000) * 1000000;
1264 while (nanosleep(&request, &remaining) == -1
1265 && errno == EINTR) {
1266 request = remaining;
1270 _nc_timed_wait(0, 0, ms, (int *) 0 EVENTLIST_2nd(0));
1276 __nc_putp(SCREEN *sp, const char *name GCC_UNUSED, const char *value)
1281 rc = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx name, value);
1287 __nc_putp_flush(SCREEN *sp, const char *name, const char *value)
1289 int rc = __nc_putp(sp, name, value);
1291 NCURSES_SP_NAME(_nc_flush) (sp);
1297 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag)
1308 (void) __nc_putp_flush(sp, "keypad_xmit", keypad_xmit);
1309 } else if (!flag && keypad_local) {
1310 (void) __nc_putp_flush(sp, "keypad_local", keypad_local);
1312 if (flag && !sp->_tried) {
1313 _nc_init_keytry(sp);
1323 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int c, bool flag)
1334 unsigned ch = (unsigned) c;
1336 while ((s = _nc_expand_try(sp->_key_ok, ch, &count, 0)) != 0
1337 && _nc_remove_key(&(sp->_key_ok), ch)) {
1338 code = _nc_add_to_try(&(sp->_keytry), s, ch);
1345 while ((s = _nc_expand_try(sp->_keytry, ch, &count, 0)) != 0
1346 && _nc_remove_key(&(sp->_keytry), ch)) {
1347 code = _nc_add_to_try(&(sp->_key_ok), s, ch);
1359 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int key)
1365 res = TINFO_HAS_KEY(TCB->csp, key) == 0 ? FALSE : TRUE;