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.3 2009/09/27 17:29:39 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);
190 ** Take the real command character out of the CC environment variable
191 ** and substitute it in for the prototype given in 'command_character'.
194 do_prototype(TERMINAL * termp)
200 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
202 if ((tmp = getenv("CC")) != 0) {
203 if ((CC = *tmp) != 0) {
204 proto = *command_character;
206 for_each_string(i, &(termp->type)) {
207 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
216 #define ret_error(code, fmt, arg) if (errret) {\
220 fprintf(stderr, fmt, arg);\
224 #define ret_error0(code, msg) if (errret) {\
228 fprintf(stderr, msg);\
232 #if USE_DATABASE || USE_TERMCAP
234 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
235 * just like tgetent().
238 grab_entry(const char *const tn, TERMTYPE *const tp)
240 char filename[PATH_MAX];
241 int status = _nc_read_entry(tn, filename, tp);
244 * If we have an entry, force all of the cancelled strings to null
245 * pointers so we don't have to test them in the rest of the library.
246 * (The terminfo compiler bypasses this logic, since it must know if
247 * a string is cancelled, for merging entries).
249 if (status == TGETENT_YES) {
251 for_each_boolean(n, tp) {
252 if (!VALID_BOOLEAN(tp->Booleans[n]))
253 tp->Booleans[n] = FALSE;
255 for_each_string(n, tp) {
256 if (tp->Strings[n] == CANCELLED_STRING)
257 tp->Strings[n] = ABSENT_STRING;
265 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
272 assert(TCB != 0 && tname != 0);
273 termp = (TERMINAL *) TCB;
275 TCB->magic = TCBMAGIC;
277 #if (USE_DATABASE || USE_TERMCAP)
278 status = grab_entry(tname, &termp->type);
283 /* try fallback list if entry on disk */
284 if (status != TGETENT_YES) {
285 const TERMTYPE *fallback = _nc_fallback(tname);
288 termp->type = *fallback;
289 status = TGETENT_YES;
293 if (status != TGETENT_YES) {
294 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx termp);
295 if (status == TGETENT_ERR) {
296 ret_error0(status, "terminals database is inaccessible\n");
297 } else if (status == TGETENT_NO) {
298 ret_error(status, "'%s': unknown terminal type.\n", tname);
303 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
304 ttytype[NAMESIZE - 1] = '\0';
307 if (command_character && getenv("CC"))
311 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
314 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
321 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, bool beepFlag)
329 /* FIXME: should make sure that we are not in altchar mode */
332 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
333 NCURSES_SP_NAME(_nc_flush) (sp);
334 } else if (flash_screen) {
335 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
338 NCURSES_SP_NAME(_nc_flush) (sp);
342 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
345 NCURSES_SP_NAME(_nc_flush) (sp);
347 res = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "bell", bell);
348 NCURSES_SP_NAME(_nc_flush) (sp);
355 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
356 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
357 * also used in the FreeBSD syscons.
360 toggled_colors(int c)
363 static const int table[] =
364 {0, 4, 2, 6, 1, 5, 3, 7,
365 8, 12, 10, 14, 9, 13, 11, 15};
372 drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len)
378 #if NCURSES_EXT_FUNCS
379 return NCURSES_SP_NAME(mcprint) (TCB->csp, data, len);
386 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg)
394 if (sp != 0 && orig_pair && orig_colors && (initialize_pair != 0)) {
395 #if NCURSES_EXT_FUNCS
396 sp->_default_color = isDefaultColor(fg) || isDefaultColor(bg);
397 sp->_has_sgr_39_49 = (NCURSES_SP_NAME(tigetflag) (NCURSES_SP_ARGx
400 sp->_default_fg = isDefaultColor(fg) ? COLOR_DEFAULT : (fg & C_MASK);
401 sp->_default_bg = isDefaultColor(bg) ? COLOR_DEFAULT : (bg & C_MASK);
402 if (sp->_color_pairs != 0) {
403 bool save = sp->_default_color;
404 sp->_default_color = TRUE;
405 NCURSES_SP_NAME(init_pair) (NCURSES_SP_ARGx
409 sp->_default_color = save;
418 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB, bool fore, int color, int (*outc)
427 if (set_a_foreground) {
428 TPUTS_TRACE("set_a_foreground");
429 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
430 TPARM_1(set_a_foreground, color), 1, outc);
432 TPUTS_TRACE("set_foreground");
433 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
434 TPARM_1(set_foreground,
435 toggled_colors(color)), 1, outc);
438 if (set_a_background) {
439 TPUTS_TRACE("set_a_background");
440 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
441 TPARM_1(set_a_background, color), 1, outc);
443 TPUTS_TRACE("set_background");
444 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
445 TPARM_1(set_background,
446 toggled_colors(color)), 1, outc);
452 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
460 if (orig_pair != 0) {
461 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_pair", orig_pair);
468 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
476 if (orig_colors != 0) {
477 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "orig_colors", orig_colors);
484 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *linep, int *colp)
490 sp = TCB->csp; /* can be null here */
493 useEnv = sp->_use_env;
495 useEnv = _nc_prescreen.use_env;
497 /* figure out the size of the screen */
498 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
501 *linep = (int) lines;
502 *colp = (int) columns;
503 } else { /* usually want to query LINES and COLUMNS from environment */
508 /* first, look for environment variables */
509 if ((value = _nc_getenv_num("LINES")) > 0) {
512 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
515 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
518 if (*linep <= 0 || *colp <= 0) {
520 _scrsize(screendata);
521 *colp = screendata[0];
522 *linep = screendata[1];
523 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
528 /* if that didn't work, maybe we can try asking the OS */
529 if (*linep <= 0 || *colp <= 0) {
530 TERMINAL *termp = (TERMINAL *) TCB;
531 if (isatty(termp->Filedes)) {
536 if (ioctl(termp->Filedes, IOCTL_WINSIZE, &size) < 0
543 * Solaris lets users override either dimension with an
544 * environment variable.
547 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
549 *colp = WINSIZE_COLS(size);
554 #endif /* HAVE_SIZECHANGE */
556 /* if we can't get dynamic info about the size, use static */
558 *linep = (int) lines;
561 *colp = (int) columns;
564 /* the ultimate fallback, assume fixed 24x80 size */
573 * Put the derived values back in the screen-size caps, so
574 * tigetnum() and tgetnum() will do the right thing.
576 lines = (short) (*linep);
577 columns = (short) (*colp);
580 T(("screen size is %dx%d", *linep, *colp));
585 drv_getsize(TERMINAL_CONTROL_BLOCK * TCB, int *l, int *c)
588 assert(l != 0 && c != 0);
595 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c)
604 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
607 TERMINAL *_term = (TERMINAL *) TCB;
613 if (progFlag) /* prog mode */
618 * Turn off the XTABS bit in the tty structure if it was on.
620 if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
622 _term->Nttyb.c_oflag &= ~OFLAGS_TABS;
624 _term->Nttyb.sg_flags &= ~XTABS;
629 /* reset_prog_mode */
630 if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
633 _nc_keypad(sp, TRUE);
634 NC_BUFFERED(sp, TRUE);
639 } else { /* shell mode */
643 * If XTABS was on, remove the tab and backtab capabilities.
645 if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
647 if (_term->Ottyb.c_oflag & OFLAGS_TABS)
648 tab = back_tab = NULL;
650 if (_term->Ottyb.sg_flags & XTABS)
651 tab = back_tab = NULL;
656 /* reset_shell_mode */
658 _nc_keypad(sp, FALSE);
659 NCURSES_SP_NAME(_nc_flush) (sp);
660 NC_BUFFERED(sp, FALSE);
662 code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
673 NCURSES_SP_NAME(_nc_screen_wrap) (sp);
674 NCURSES_SP_NAME(_nc_mvcur_wrap) (sp); /* wrap up cursor addressing */
679 drv_release(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
683 # define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
686 drv_screen_init(SCREEN *sp)
688 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
693 * Check for mismatched graphic-rendition capabilities. Most SVr4
694 * terminfo trees contain entries that have rmul or rmso equated to
695 * sgr0 (Solaris curses copes with those entries). We do this only
696 * for curses, since many termcap applications assume that
697 * smso/rmso and smul/rmul are paired, and will not function
698 * properly if we remove rmso or rmul. Curses applications
699 * shouldn't be looking at this detail.
701 sp->_use_rmso = SGR0_TEST(exit_standout_mode);
702 sp->_use_rmul = SGR0_TEST(exit_underline_mode);
705 * Check whether we can optimize scrolling under dumb terminals in
706 * case we do not have any of these capabilities, scrolling
707 * optimization will be useless.
709 sp->_scrolling = ((scroll_forward && scroll_reverse) ||
717 NCURSES_SP_NAME(baudrate) (sp);
719 NCURSES_SP_NAME(_nc_mvcur_init) (sp);
720 /* initialize terminal to a sane state */
721 NCURSES_SP_NAME(_nc_screen_init) (sp);
725 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
732 trm = (TERMINAL *) TCB;
735 TCB->info.initcolor = initialize_color;
736 TCB->info.canchange = can_change;
737 TCB->info.hascolor = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
738 && (((set_foreground != NULL)
739 && (set_background != NULL))
740 || ((set_a_foreground != NULL)
741 && (set_a_background != NULL))
742 || set_color_pair)) ? TRUE : FALSE);
744 TCB->info.caninit = !(exit_ca_mode && non_rev_rmcup);
746 TCB->info.maxpairs = VALID_NUMERIC(max_pairs) ? max_pairs : 0;
747 TCB->info.maxcolors = VALID_NUMERIC(max_colors) ? max_colors : 0;
748 TCB->info.numlabels = VALID_NUMERIC(num_labels) ? num_labels : 0;
749 TCB->info.labelwidth = VALID_NUMERIC(label_width) ? label_width : 0;
750 TCB->info.labelheight = VALID_NUMERIC(label_height) ? label_height : 0;
751 TCB->info.nocolorvideo = VALID_NUMERIC(no_color_video) ? no_color_video
753 TCB->info.tabsize = VALID_NUMERIC(init_tabs) ? (int) init_tabs : 8;
755 TCB->info.defaultPalette = hue_lightness_saturation ? _nc_hls_palette : _nc_cga_palette;
758 * If an application calls setupterm() rather than initscr() or
759 * newterm(), we will not have the def_prog_mode() call in
760 * _nc_setupscreen(). Do it now anyway, so we can initialize the
763 if (isatty(trm->Filedes)) {
764 TCB->drv->mode(TCB, TRUE, TRUE);
768 #define MAX_PALETTE 8
769 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
772 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, short pair, short f, short b)
779 if ((initialize_pair != NULL) && InPalette(f) && InPalette(b)) {
780 const color_t *tp = InfoOf(sp).defaultPalette;
783 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
785 tp[f].red, tp[f].green, tp[f].blue,
786 tp[b].red, tp[b].green, tp[b].blue));
788 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
790 TPARM_7(initialize_pair,
792 tp[f].red, tp[f].green, tp[f].blue,
793 tp[b].red, tp[b].green, tp[b].blue));
798 default_fg(SCREEN *sp)
800 #if NCURSES_EXT_FUNCS
801 return (sp != 0) ? sp->_default_fg : COLOR_WHITE;
808 default_bg(SCREEN *sp)
810 #if NCURSES_EXT_FUNCS
811 return sp != 0 ? sp->_default_bg : COLOR_BLACK;
818 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
819 short color, short r, short g, short b)
821 SCREEN *sp = TCB->csp;
824 if (initialize_color != NULL) {
825 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
827 TPARM_4(initialize_color, color, r, g, b));
832 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
836 NCURSES_SP_OUTC outc)
838 SCREEN *sp = TCB->csp;
839 NCURSES_COLOR_T fg = COLOR_DEFAULT;
840 NCURSES_COLOR_T bg = COLOR_DEFAULT;
841 NCURSES_COLOR_T old_fg, old_bg;
847 if (pair < 0 || pair >= COLOR_PAIRS) {
849 } else if (pair != 0) {
850 if (set_color_pair) {
851 TPUTS_TRACE("set_color_pair");
852 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
853 TPARM_1(set_color_pair, pair), 1, outc);
855 } else if (sp != 0) {
856 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
865 && NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
869 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
870 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
871 #if NCURSES_EXT_FUNCS
873 * A minor optimization - but extension. If "AX" is specified in
874 * the terminal description, treat it as screen's indicator of ECMA
875 * SGR 39 and SGR 49, and assume the two sequences are independent.
877 if (sp->_has_sgr_39_49
878 && isDefaultColor(old_bg)
879 && !isDefaultColor(old_fg)) {
880 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
881 } else if (sp->_has_sgr_39_49
882 && isDefaultColor(old_fg)
883 && !isDefaultColor(old_bg)) {
884 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
895 #if NCURSES_EXT_FUNCS
896 if (isDefaultColor(fg))
898 if (isDefaultColor(bg))
903 NCURSES_COLOR_T xx = fg;
908 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
911 if (!isDefaultColor(fg)) {
912 drv_setcolor(TCB, TRUE, fg, outc);
914 if (!isDefaultColor(bg)) {
915 drv_setcolor(TCB, FALSE, bg, outc);
919 #define xterm_kmous "\033[M"
921 init_xterm_mouse(SCREEN *sp)
923 sp->_mouse_type = M_XTERM;
924 sp->_mouse_xtermcap = NCURSES_SP_NAME(tigetstr) (NCURSES_SP_ARGx "XM");
925 if (!VALID_STRING(sp->_mouse_xtermcap))
926 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
930 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
937 /* we know how to recognize mouse events under "xterm" */
939 if (key_mouse != 0) {
940 if (!strcmp(key_mouse, xterm_kmous)
941 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
942 init_xterm_mouse(sp);
944 } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
945 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
946 init_xterm_mouse(sp);
952 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int ynew, int xnew)
954 SCREEN *sp = TCB->csp;
956 return TINFO_MVCUR(sp, yold, xold, ynew, xnew);
960 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text)
962 SCREEN *sp = TCB->csp;
965 if (labnum > 0 && labnum <= num_labels) {
966 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
968 TPARM_2(plab_norm, labnum, text));
973 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, bool OnFlag)
975 SCREEN *sp = TCB->csp;
979 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_on", label_on);
981 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "label_off", label_off);
986 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB)
988 SCREEN *sp = TCB->csp;
989 chtype attrs = A_NORMAL;
992 if (enter_alt_charset_mode)
993 attrs |= A_ALTCHARSET;
995 if (enter_blink_mode)
1004 if (enter_reverse_mode)
1007 if (enter_standout_mode)
1008 attrs |= A_STANDOUT;
1010 if (enter_protected_mode)
1013 if (enter_secure_mode)
1016 if (enter_underline_mode)
1017 attrs |= A_UNDERLINE;
1019 if (sp && sp->_coloron)
1026 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
1031 cursor_down = parm_down_cursor = 0;
1033 cursor_up = parm_up_cursor = 0;
1035 cursor_home = carriage_return;
1039 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
1041 SCREEN *sp = TCB->csp;
1042 TERMINAL *_term = (TERMINAL *) TCB;
1048 if (SET_TTY(_term->Filedes, buf) != 0) {
1051 if (errno == ENOTTY) {
1061 if (GET_TTY(_term->Filedes, buf) != 0) {
1073 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *real_map, chtype *fake_map)
1075 SCREEN *sp = TCB->csp;
1079 if (ena_acs != NULL) {
1080 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "ena_acs", ena_acs);
1082 #if NCURSES_EXT_FUNCS
1084 * Linux console "supports" the "PC ROM" character set by the coincidence
1085 * that smpch/rmpch and smacs/rmacs have the same values. ncurses has
1086 * no codepage support (see SCO Merge for an example). Outside of the
1087 * values defined in acsc, there are no definitions for the "PC ROM"
1088 * character set (assumed by some applications to be codepage 437), but we
1089 * allow those applications to use those codepoints.
1091 * test/blue.c uses this feature.
1093 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
1094 if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
1095 PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
1097 for (i = 1; i < ACS_LEN; ++i) {
1098 if (real_map[i] == 0) {
1100 if (real_map != fake_map) {
1102 sp->_screen_acs_map[i] = TRUE;
1109 if (acs_chars != NULL) {
1111 size_t length = strlen(acs_chars);
1113 while (i + 1 < length) {
1114 if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
1115 real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
1117 sp->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
1123 /* Show the equivalent mapping, noting if it does not match the
1124 * given attribute, whether by re-ordering or duplication.
1126 if (USE_TRACEF(TRACE_CALLS)) {
1128 char show[ACS_LEN * 2 + 1];
1129 for (n = 1, m = 0; n < ACS_LEN; n++) {
1130 if (real_map[n] != 0) {
1131 show[m++] = (char) n;
1132 show[m++] = (char) ChCharOf(real_map[n]);
1136 if (acs_chars == NULL || strcmp(acs_chars, show))
1137 _tracef("%s acs_chars %s",
1138 (acs_chars == NULL) ? "NULL" : "READ",
1139 _nc_visbuf(acs_chars));
1140 _tracef("%s acs_chars %s",
1143 : (strcmp(acs_chars, show)
1148 _nc_unlock_global(tracef);
1153 #define ENSURE_TINFO(sp) (TCBOf(sp)->drv->isTerminfo)
1155 NCURSES_EXPORT(void)
1156 _nc_cookie_init(SCREEN *sp)
1158 bool support_cookies = USE_XMC_SUPPORT;
1159 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) (sp->_term);
1161 if (sp == 0 || !ENSURE_TINFO(sp))
1166 * If we have no magic-cookie support compiled-in, or if it is suppressed
1167 * in the environment, reset the support-flag.
1169 if (magic_cookie_glitch >= 0) {
1170 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
1171 support_cookies = FALSE;
1176 if (!support_cookies && magic_cookie_glitch >= 0) {
1177 T(("will disable attributes to work w/o magic cookies"));
1180 if (magic_cookie_glitch > 0) { /* tvi, wyse */
1182 sp->_xmc_triggers = sp->_ok_attributes & (
1194 * We "should" treat colors as an attribute. The wyse350 (and its
1195 * clones) appear to be the only ones that have both colors and magic
1199 sp->_xmc_triggers |= A_COLOR;
1202 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
1204 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
1206 * Supporting line-drawing may be possible. But make the regular
1207 * video attributes work first.
1209 acs_chars = ABSENT_STRING;
1210 ena_acs = ABSENT_STRING;
1211 enter_alt_charset_mode = ABSENT_STRING;
1212 exit_alt_charset_mode = ABSENT_STRING;
1215 * To keep the cookie support simple, suppress all of the optimization
1216 * hooks except for clear_screen and the cursor addressing.
1218 if (support_cookies) {
1219 clr_eol = ABSENT_STRING;
1220 clr_eos = ABSENT_STRING;
1221 set_attributes = ABSENT_STRING;
1224 } else if (magic_cookie_glitch == 0) { /* hpterm */
1228 * If magic cookies are not supported, cancel the strings that set
1231 if (!support_cookies && magic_cookie_glitch >= 0) {
1232 magic_cookie_glitch = ABSENT_NUMERIC;
1233 set_attributes = ABSENT_STRING;
1234 enter_blink_mode = ABSENT_STRING;
1235 enter_bold_mode = ABSENT_STRING;
1236 enter_dim_mode = ABSENT_STRING;
1237 enter_reverse_mode = ABSENT_STRING;
1238 enter_standout_mode = ABSENT_STRING;
1239 enter_underline_mode = ABSENT_STRING;
1242 /* initialize normal acs before wide, since we use mapping in the latter */
1243 #if !USE_WIDEC_SUPPORT
1244 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
1247 enter_alt_charset_mode = NULL;
1248 exit_alt_charset_mode = NULL;
1249 set_attributes = NULL;
1255 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1259 EVENTLIST_2nd(_nc_eventlist * evl))
1266 return _nc_timed_wait(sp, mode, milliseconds, timeleft EVENTLIST_2nd(evl));
1270 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1273 unsigned char c2 = 0;
1280 n = read(sp->_ifd, &c2, 1);
1286 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1290 struct timespec request, remaining;
1291 request.tv_sec = ms / 1000;
1292 request.tv_nsec = (ms % 1000) * 1000000;
1293 while (nanosleep(&request, &remaining) == -1
1294 && errno == EINTR) {
1295 request = remaining;
1299 _nc_timed_wait(0, 0, ms, (int *) 0 EVENTLIST_2nd(0));
1305 __nc_putp(SCREEN *sp, const char *name GCC_UNUSED, const char *value)
1310 rc = NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx name, value);
1316 __nc_putp_flush(SCREEN *sp, const char *name, const char *value)
1318 int rc = __nc_putp(sp, name, value);
1320 NCURSES_SP_NAME(_nc_flush) (sp);
1326 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag)
1337 (void) __nc_putp_flush(sp, "keypad_xmit", keypad_xmit);
1338 } else if (!flag && keypad_local) {
1339 (void) __nc_putp_flush(sp, "keypad_local", keypad_local);
1341 if (flag && !sp->_tried) {
1342 _nc_init_keytry(sp);
1352 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int c, bool flag)
1363 unsigned ch = (unsigned) c;
1365 while ((s = _nc_expand_try(sp->_key_ok, ch, &count, 0)) != 0
1366 && _nc_remove_key(&(sp->_key_ok), ch)) {
1367 code = _nc_add_to_try(&(sp->_keytry), s, ch);
1374 while ((s = _nc_expand_try(sp->_keytry, ch, &count, 0)) != 0
1375 && _nc_remove_key(&(sp->_keytry), ch)) {
1376 code = _nc_add_to_try(&(sp->_key_ok), s, ch);
1388 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int key)
1394 res = TINFO_HAS_KEY(TCB->csp, key) == 0 ? FALSE : TRUE;