1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 2008-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer *
32 * and: Thomas E. Dickey *
33 ****************************************************************************/
35 #include <curses.priv.h>
36 #define CUR TerminalType((TERMINAL*)TCB).
38 #include <termcap.h> /* ospeed */
43 #include <sys/time.h> /* needed for MacOS X DP3 */
48 # if !defined(sun) || !TERMIOS
50 # include <sys/ioctl.h>
55 MODULE_ID("$Id: tinfo_driver.c,v 1.73 2022/08/13 14:36:43 tom Exp $")
58 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
59 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
62 # define IOCTL_WINSIZE TIOCGSIZE
63 # define STRUCT_WINSIZE struct ttysize
64 # define WINSIZE_ROWS(n) (int)n.ts_lines
65 # define WINSIZE_COLS(n) (int)n.ts_cols
68 # define IOCTL_WINSIZE TIOCGWINSZ
69 # define STRUCT_WINSIZE struct winsize
70 # define WINSIZE_ROWS(n) (int)n.ws_row
71 # define WINSIZE_COLS(n) (int)n.ws_col
76 * These should be screen structure members. They need to be globals for
77 * historical reasons. So we assign them in start_color() and also in
78 * set_term()'s screen-switching logic.
82 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
84 return CURRENT_SCREEN ? CURRENT_SCREEN->_pair_count : -1;
87 NCURSES_PUBLIC_VAR(COLORS) (void)
89 return CURRENT_SCREEN ? CURRENT_SCREEN->_color_count : -1;
92 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
93 NCURSES_EXPORT_VAR(int) COLORS = 0;
96 #define TCBMAGIC NCDRV_MAGIC(NCDRV_TINFO)
97 #define AssertTCB() assert(TCB!=0 && TCB->magic==TCBMAGIC)
98 #define SetSP() assert(TCB->csp!=0); sp = TCB->csp; (void) sp
101 * This routine needs to do all the work to make curscr look
105 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
108 return TINFO_DOUPDATE(TCB->csp);
112 drv_Name(TERMINAL_CONTROL_BLOCK * TCB)
119 get_baudrate(TERMINAL *termp)
123 if (GET_TTY(termp->Filedes, &termp->Nttyb) == OK) {
125 termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
126 #elif defined(EXP_WIN32_DRIVER)
129 termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
133 result = (int) cfgetospeed(&(termp->Nttyb));
134 my_ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
135 #else /* !USE_OLD_TTY */
137 my_ospeed = (NCURSES_OSPEED) cfgetospeed(&(termp->Nttyb));
138 #elif defined(EXP_WIN32_DRIVER)
142 my_ospeed = (NCURSES_OSPEED) termp->Nttyb.sg_ospeed;
144 result = _nc_baudrate(my_ospeed);
146 termp->_baudrate = result;
147 ospeed = (NCURSES_OSPEED) my_ospeed;
151 #define SETUP_FAIL FALSE
156 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
164 T((T_CALLED("tinfo::drv_CanHandle(%p)"), (void *) TCB));
166 assert(TCB != 0 && tname != 0);
167 termp = (TERMINAL *) TCB;
169 TCB->magic = TCBMAGIC;
171 #if (NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP)
172 status = _nc_setup_tinfo(tname, &TerminalType(termp));
173 T(("_nc_setup_tinfo returns %d", status));
175 T(("no database available"));
179 /* try fallback list if entry on disk */
180 if (status != TGETENT_YES) {
181 const TERMTYPE2 *fallback = _nc_fallback2(tname);
184 T(("found fallback entry"));
185 TerminalType(termp) = *fallback;
186 status = TGETENT_YES;
190 if (status != TGETENT_YES) {
191 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx termp);
192 if (status == TGETENT_ERR) {
193 ret_error0(status, "terminals database is inaccessible\n");
194 } else if (status == TGETENT_NO) {
195 ret_error1(status, "unknown terminal type.\n",
198 ret_error0(status, "unexpected return-code\n");
202 #if NCURSES_EXT_NUMBERS
203 _nc_export_termtype2(&termp->type, &TerminalType(termp));
209 if (command_character)
210 _nc_tinfo_cmdch(termp, *command_character);
213 * If an application calls setupterm() rather than initscr() or
214 * newterm(), we will not have the def_prog_mode() call in
215 * _nc_setupscreen(). Do it now anyway, so we can initialize the
218 if (sp == 0 && NC_ISATTY(termp->Filedes)) {
221 #if NCURSES_EXT_NUMBERS
222 #define cleanup_termtype() \
223 _nc_free_termtype2(&TerminalType(termp)); \
224 _nc_free_termtype(&termp->type)
226 #define cleanup_termtype() \
227 _nc_free_termtype2(&TerminalType(termp))
232 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
233 * check before giving up.
235 if ((VALID_STRING(cursor_address)
236 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
237 && VALID_STRING(clear_screen)) {
239 ret_error1(TGETENT_YES, "terminal is not really generic.\n",
243 ret_error1(TGETENT_NO, "I need something more specific.\n",
249 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n",
257 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB, int beepFlag)
265 /* FIXME: should make sure that we are not in altchar mode */
268 res = NCURSES_PUTP2("bell", bell);
269 NCURSES_SP_NAME(_nc_flush) (sp);
270 } else if (flash_screen) {
271 res = NCURSES_PUTP2("flash_screen", flash_screen);
272 NCURSES_SP_NAME(_nc_flush) (sp);
276 res = NCURSES_PUTP2("flash_screen", flash_screen);
277 NCURSES_SP_NAME(_nc_flush) (sp);
279 res = NCURSES_PUTP2("bell", bell);
280 NCURSES_SP_NAME(_nc_flush) (sp);
287 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
288 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
289 * also used in the FreeBSD syscons.
292 toggled_colors(int c)
295 static const int table[] =
296 {0, 4, 2, 6, 1, 5, 3, 7,
297 8, 12, 10, 14, 9, 13, 11, 15};
304 drv_print(TERMINAL_CONTROL_BLOCK * TCB, char *data, int len)
310 #if NCURSES_EXT_FUNCS
311 return NCURSES_SP_NAME(mcprint) (TCB->csp, data, len);
318 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB, int fg, int bg)
326 if (sp != 0 && orig_pair && orig_colors && (initialize_pair != 0)) {
327 #if NCURSES_EXT_FUNCS
328 sp->_default_color = isDefaultColor(fg) || isDefaultColor(bg);
329 sp->_has_sgr_39_49 = (NCURSES_SP_NAME(tigetflag) (NCURSES_SP_ARGx
332 sp->_default_fg = isDefaultColor(fg) ? COLOR_DEFAULT : fg;
333 sp->_default_bg = isDefaultColor(bg) ? COLOR_DEFAULT : bg;
334 if (sp->_color_pairs != 0) {
335 bool save = sp->_default_color;
336 sp->_default_color = TRUE;
337 NCURSES_SP_NAME(init_pair) (NCURSES_SP_ARGx
341 sp->_default_color = save;
350 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB,
353 NCURSES_SP_OUTC outc)
361 if (set_a_foreground) {
362 TPUTS_TRACE("set_a_foreground");
363 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
364 TIPARM_1(set_a_foreground, color), 1, outc);
366 TPUTS_TRACE("set_foreground");
367 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
368 TIPARM_1(set_foreground,
369 toggled_colors(color)), 1, outc);
372 if (set_a_background) {
373 TPUTS_TRACE("set_a_background");
374 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
375 TIPARM_1(set_a_background, color), 1, outc);
377 TPUTS_TRACE("set_background");
378 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
379 TIPARM_1(set_background,
380 toggled_colors(color)), 1, outc);
386 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
394 if (orig_pair != 0) {
395 NCURSES_PUTP2("orig_pair", orig_pair);
402 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
410 if (orig_colors != 0) {
411 NCURSES_PUTP2("orig_colors", orig_colors);
418 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *linep, int *colp)
422 bool useTioctl = TRUE;
425 sp = TCB->csp; /* can be null here */
428 useEnv = sp->_use_env;
429 useTioctl = sp->use_tioctl;
431 useEnv = _nc_prescreen.use_env;
432 useTioctl = _nc_prescreen.use_tioctl;
435 #ifdef EXP_WIN32_DRIVER
436 /* If we are here, then Windows console is used in terminfo mode.
437 We need to figure out the size using the console API
439 _nc_console_size(linep, colp);
440 T(("screen size: winconsole lines = %d columns = %d", *linep, *colp));
442 /* figure out the size of the screen */
443 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
445 *linep = (int) lines;
446 *colp = (int) columns;
448 if (useEnv || useTioctl) {
454 _scrsize(screendata);
455 *colp = screendata[0];
456 *linep = ((sp != 0 && sp->_filtered)
459 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
464 /* try asking the OS */
466 TERMINAL *termp = (TERMINAL *) TCB;
467 if (NC_ISATTY(termp->Filedes)) {
472 if (ioctl(termp->Filedes, IOCTL_WINSIZE, &size) >= 0) {
473 *linep = ((sp != 0 && sp->_filtered)
475 : WINSIZE_ROWS(size));
476 *colp = WINSIZE_COLS(size);
477 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
485 #endif /* HAVE_SIZECHANGE */
490 * If environment variables are used, update them.
492 if ((sp == 0 || !sp->_filtered) && _nc_getenv_num("LINES") > 0) {
493 _nc_setenv_num("LINES", *linep);
495 if (_nc_getenv_num("COLUMNS") > 0) {
496 _nc_setenv_num("COLUMNS", *colp);
501 * Finally, look for environment variables.
503 * Solaris lets users override either dimension with an environment
506 if ((value = _nc_getenv_num("LINES")) > 0) {
508 T(("screen size: environment LINES = %d", *linep));
510 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
512 T(("screen size: environment COLUMNS = %d", *colp));
516 /* if we can't get dynamic info about the size, use static */
518 *linep = (int) lines;
521 *colp = (int) columns;
524 /* the ultimate fallback, assume fixed 24x80 size */
533 * Put the derived values back in the screen-size caps, so
534 * tigetnum() and tgetnum() will do the right thing.
536 lines = (short) (*linep);
537 columns = (short) (*colp);
540 T(("screen size is %dx%d", *linep, *colp));
545 drv_getsize(TERMINAL_CONTROL_BLOCK * TCB, int *l, int *c)
548 assert(l != 0 && c != 0);
555 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB, int l, int c)
564 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, int setFlag, TTY * buf)
566 SCREEN *sp = TCB->csp;
567 TERMINAL *_term = (TERMINAL *) TCB;
573 if (SET_TTY(_term->Filedes, buf) != 0) {
576 if (errno == ENOTTY) {
586 if (GET_TTY(_term->Filedes, buf) != 0) {
598 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, int progFlag, int defFlag)
601 TERMINAL *_term = (TERMINAL *) TCB;
607 if (progFlag) /* prog mode */
612 * Turn off the XTABS bit in the tty structure if it was on.
614 if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
616 _term->Nttyb.c_oflag &= (unsigned) ~OFLAGS_TABS;
617 #elif defined(EXP_WIN32_DRIVER)
620 _term->Nttyb.sg_flags &= (unsigned) ~XTABS;
625 /* reset_prog_mode */
626 if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
629 _nc_keypad(sp, TRUE);
634 } else { /* shell mode */
638 * If XTABS was on, remove the tab and backtab capabilities.
640 if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
642 if (_term->Ottyb.c_oflag & OFLAGS_TABS)
643 tab = back_tab = NULL;
644 #elif defined(EXP_WIN32_DRIVER)
647 if (_term->Ottyb.sg_flags & XTABS)
648 tab = back_tab = NULL;
653 /* reset_shell_mode */
655 _nc_keypad(sp, FALSE);
656 NCURSES_SP_NAME(_nc_flush) (sp);
658 code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
669 NCURSES_SP_NAME(_nc_screen_wrap) (sp);
670 NCURSES_SP_NAME(_nc_mvcur_wrap) (sp); /* wrap up cursor addressing */
675 drv_release(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
679 # define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
682 drv_screen_init(SCREEN *sp)
684 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
689 * Check for mismatched graphic-rendition capabilities. Most SVr4
690 * terminfo trees contain entries that have rmul or rmso equated to
691 * sgr0 (Solaris curses copes with those entries). We do this only
692 * for curses, since many termcap applications assume that
693 * smso/rmso and smul/rmul are paired, and will not function
694 * properly if we remove rmso or rmul. Curses applications
695 * shouldn't be looking at this detail.
697 sp->_use_rmso = SGR0_TEST(exit_standout_mode);
698 sp->_use_rmul = SGR0_TEST(exit_underline_mode);
701 * Check whether we can optimize scrolling under dumb terminals in
702 * case we do not have any of these capabilities, scrolling
703 * optimization will be useless.
705 sp->_scrolling = ((scroll_forward && scroll_reverse) ||
713 NCURSES_SP_NAME(baudrate) (sp);
715 NCURSES_SP_NAME(_nc_mvcur_init) (sp);
716 /* initialize terminal to a sane state */
717 NCURSES_SP_NAME(_nc_screen_init) (sp);
721 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
727 trm = (TERMINAL *) TCB;
729 TCB->info.initcolor = VALID_STRING(initialize_color);
730 TCB->info.canchange = can_change;
731 TCB->info.hascolor = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
732 && (((set_foreground != NULL)
733 && (set_background != NULL))
734 || ((set_a_foreground != NULL)
735 && (set_a_background != NULL))
736 || set_color_pair)) ? TRUE : FALSE);
738 TCB->info.caninit = !(exit_ca_mode && non_rev_rmcup);
740 TCB->info.maxpairs = VALID_NUMERIC(max_pairs) ? max_pairs : 0;
741 TCB->info.maxcolors = VALID_NUMERIC(max_colors) ? max_colors : 0;
742 TCB->info.numlabels = VALID_NUMERIC(num_labels) ? num_labels : 0;
743 TCB->info.labelwidth = VALID_NUMERIC(label_width) ? label_width : 0;
744 TCB->info.labelheight = VALID_NUMERIC(label_height) ? label_height : 0;
745 TCB->info.nocolorvideo = VALID_NUMERIC(no_color_video) ? no_color_video
747 TCB->info.tabsize = VALID_NUMERIC(init_tabs) ? (int) init_tabs : 8;
749 TCB->info.defaultPalette = hue_lightness_saturation ? _nc_hls_palette : _nc_cga_palette;
752 * If an application calls setupterm() rather than initscr() or
753 * newterm(), we will not have the def_prog_mode() call in
754 * _nc_setupscreen(). Do it now anyway, so we can initialize the
757 if (NC_ISATTY(trm->Filedes)) {
758 TCB->drv->td_mode(TCB, TRUE, TRUE);
762 #define MAX_PALETTE 8
763 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
766 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB, int pair, int f, int b)
773 if ((initialize_pair != NULL) && InPalette(f) && InPalette(b)) {
774 const color_t *tp = InfoOf(sp).defaultPalette;
777 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
779 tp[f].red, tp[f].green, tp[f].blue,
780 tp[b].red, tp[b].green, tp[b].blue));
782 NCURSES_PUTP2("initialize_pair",
783 TIPARM_7(initialize_pair,
785 tp[f].red, tp[f].green, tp[f].blue,
786 tp[b].red, tp[b].green, tp[b].blue));
791 default_fg(SCREEN *sp)
793 #if NCURSES_EXT_FUNCS
794 return (sp != 0) ? sp->_default_fg : COLOR_WHITE;
801 default_bg(SCREEN *sp)
803 #if NCURSES_EXT_FUNCS
804 return sp != 0 ? sp->_default_bg : COLOR_BLACK;
811 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
812 int color, int r, int g, int b)
814 SCREEN *sp = TCB->csp;
817 if (initialize_color != NULL) {
818 NCURSES_PUTP2("initialize_color",
819 TIPARM_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 int fg = COLOR_DEFAULT;
832 int bg = COLOR_DEFAULT;
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 TIPARM_1(set_color_pair, pair), 1, outc);
847 } else if (sp != 0) {
848 _nc_pair_content(SP_PARM, pair, &fg, &bg);
854 && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
855 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
856 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
857 #if NCURSES_EXT_FUNCS
859 * A minor optimization - but extension. If "AX" is specified in
860 * the terminal description, treat it as screen's indicator of ECMA
861 * SGR 39 and SGR 49, and assume the two sequences are independent.
863 if (sp->_has_sgr_39_49
864 && isDefaultColor(old_bg)
865 && !isDefaultColor(old_fg)) {
866 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
867 } else if (sp->_has_sgr_39_49
868 && isDefaultColor(old_fg)
869 && !isDefaultColor(old_bg)) {
870 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
881 #if NCURSES_EXT_FUNCS
882 if (isDefaultColor(fg))
884 if (isDefaultColor(bg))
894 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
897 if (!isDefaultColor(fg)) {
898 drv_setcolor(TCB, TRUE, fg, outc);
900 if (!isDefaultColor(bg)) {
901 drv_setcolor(TCB, FALSE, bg, outc);
905 #define xterm_kmous "\033[M"
907 init_xterm_mouse(SCREEN *sp)
909 sp->_mouse_type = M_XTERM;
910 sp->_mouse_xtermcap = NCURSES_SP_NAME(tigetstr) (NCURSES_SP_ARGx "XM");
911 if (!VALID_STRING(sp->_mouse_xtermcap))
912 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
916 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
923 /* we know how to recognize mouse events under "xterm" */
925 if (NonEmpty(key_mouse)) {
926 init_xterm_mouse(sp);
927 } else if (strstr(SP_TERMTYPE term_names, "xterm") != 0) {
928 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
929 init_xterm_mouse(sp);
935 drv_testmouse(TERMINAL_CONTROL_BLOCK * TCB,
937 EVENTLIST_2nd(_nc_eventlist * evl))
946 if ((sp->_mouse_type == M_SYSMOUSE)
947 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
952 #ifdef EXP_WIN32_DRIVER
953 rc = _nc_console_testmouse(sp,
954 _nc_console_handle(sp->_ifd),
958 rc = TCBOf(sp)->drv->td_twait(TCBOf(sp),
965 if ((sp->_mouse_type == M_SYSMOUSE)
966 && (sp->_sysmouse_head < sp->_sysmouse_tail)
968 && (errno == EINTR)) {
977 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB, int yold, int xold, int ynew, int xnew)
979 SCREEN *sp = TCB->csp;
981 return NCURSES_SP_NAME(_nc_mvcur) (sp, yold, xold, ynew, xnew);
985 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB, int labnum, char *text)
987 SCREEN *sp = TCB->csp;
990 if (labnum > 0 && labnum <= num_labels) {
991 NCURSES_PUTP2("plab_norm",
992 TPARM_2(plab_norm, labnum, text));
997 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB, int OnFlag)
999 SCREEN *sp = TCB->csp;
1003 NCURSES_PUTP2("label_on", label_on);
1005 NCURSES_PUTP2("label_off", label_off);
1010 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB)
1012 SCREEN *sp = TCB->csp;
1013 chtype attrs = A_NORMAL;
1016 if (enter_alt_charset_mode)
1017 attrs |= A_ALTCHARSET;
1019 if (enter_blink_mode)
1022 if (enter_bold_mode)
1028 if (enter_reverse_mode)
1031 if (enter_standout_mode)
1032 attrs |= A_STANDOUT;
1034 if (enter_protected_mode)
1037 if (enter_secure_mode)
1040 if (enter_underline_mode)
1041 attrs |= A_UNDERLINE;
1043 if (sp && sp->_coloron)
1047 if (enter_italics_mode)
1055 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
1060 clear_screen = ABSENT_STRING;
1061 cursor_address = ABSENT_STRING;
1062 cursor_down = ABSENT_STRING;
1063 cursor_up = ABSENT_STRING;
1064 parm_down_cursor = ABSENT_STRING;
1065 parm_up_cursor = ABSENT_STRING;
1066 row_address = ABSENT_STRING;
1067 cursor_home = carriage_return;
1069 if (back_color_erase)
1070 clr_eos = ABSENT_STRING;
1074 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB, chtype *real_map, chtype *fake_map)
1076 SCREEN *sp = TCB->csp;
1080 if (ena_acs != NULL) {
1081 NCURSES_PUTP2("ena_acs", ena_acs);
1083 #if NCURSES_EXT_FUNCS
1085 * Linux console "supports" the "PC ROM" character set by the coincidence
1086 * that smpch/rmpch and smacs/rmacs have the same values. ncurses has
1087 * no codepage support (see SCO Merge for an example). Outside of the
1088 * values defined in acsc, there are no definitions for the "PC ROM"
1089 * character set (assumed by some applications to be codepage 437), but we
1090 * allow those applications to use those codepoints.
1092 * test/blue.c uses this feature.
1094 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
1095 if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
1096 PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
1098 for (i = 1; i < ACS_LEN; ++i) {
1099 if (real_map[i] == 0) {
1100 real_map[i] = (chtype) i;
1101 if (real_map != fake_map) {
1103 sp->_screen_acs_map[i] = TRUE;
1110 if (acs_chars != NULL) {
1112 size_t length = strlen(acs_chars);
1114 while (i + 1 < length) {
1115 if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
1116 real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
1117 T(("#%d real_map[%s] = %s",
1119 _tracechar(UChar(acs_chars[i])),
1120 _tracechtype(real_map[UChar(acs_chars[i])])));
1122 sp->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
1129 /* Show the equivalent mapping, noting if it does not match the
1130 * given attribute, whether by re-ordering or duplication.
1132 if (USE_TRACEF(TRACE_CALLS)) {
1134 char show[ACS_LEN * 2 + 1];
1135 for (n = 1, m = 0; n < ACS_LEN; n++) {
1136 if (real_map[n] != 0) {
1137 show[m++] = (char) n;
1138 show[m++] = (char) ChCharOf(real_map[n]);
1142 if (acs_chars == NULL || strcmp(acs_chars, show))
1143 _tracef("%s acs_chars %s",
1144 (acs_chars == NULL) ? "NULL" : "READ",
1145 _nc_visbuf(acs_chars));
1146 _tracef("%s acs_chars %s",
1149 : (strcmp(acs_chars, show)
1153 _nc_unlock_global(tracef);
1158 #define ENSURE_TINFO(sp) (TCBOf(sp)->drv->isTerminfo)
1160 NCURSES_EXPORT(void)
1161 _nc_cookie_init(SCREEN *sp)
1163 bool support_cookies = USE_XMC_SUPPORT;
1164 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) (sp->_term);
1166 if (sp == 0 || !ENSURE_TINFO(sp))
1171 * If we have no magic-cookie support compiled-in, or if it is suppressed
1172 * in the environment, reset the support-flag.
1174 if (magic_cookie_glitch >= 0) {
1175 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
1176 support_cookies = FALSE;
1181 if (!support_cookies && magic_cookie_glitch >= 0) {
1182 T(("will disable attributes to work w/o magic cookies"));
1185 if (magic_cookie_glitch > 0) { /* tvi, wyse */
1187 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
1190 * We "should" treat colors as an attribute. The wyse350 (and its
1191 * clones) appear to be the only ones that have both colors and magic
1195 sp->_xmc_triggers |= A_COLOR;
1198 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
1200 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
1202 * Supporting line-drawing may be possible. But make the regular
1203 * video attributes work first.
1205 acs_chars = ABSENT_STRING;
1206 ena_acs = ABSENT_STRING;
1207 enter_alt_charset_mode = ABSENT_STRING;
1208 exit_alt_charset_mode = ABSENT_STRING;
1211 * To keep the cookie support simple, suppress all of the optimization
1212 * hooks except for clear_screen and the cursor addressing.
1214 if (support_cookies) {
1215 clr_eol = ABSENT_STRING;
1216 clr_eos = ABSENT_STRING;
1217 set_attributes = ABSENT_STRING;
1220 } else if (magic_cookie_glitch == 0) { /* hpterm */
1224 * If magic cookies are not supported, cancel the strings that set
1227 if (!support_cookies && magic_cookie_glitch >= 0) {
1228 magic_cookie_glitch = ABSENT_NUMERIC;
1229 set_attributes = ABSENT_STRING;
1230 enter_blink_mode = ABSENT_STRING;
1231 enter_bold_mode = ABSENT_STRING;
1232 enter_dim_mode = ABSENT_STRING;
1233 enter_reverse_mode = ABSENT_STRING;
1234 enter_standout_mode = ABSENT_STRING;
1235 enter_underline_mode = ABSENT_STRING;
1238 /* initialize normal acs before wide, since we use mapping in the latter */
1239 #if !USE_WIDEC_SUPPORT
1240 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
1243 enter_alt_charset_mode = NULL;
1244 exit_alt_charset_mode = NULL;
1245 set_attributes = NULL;
1251 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1255 EVENTLIST_2nd(_nc_eventlist * evl))
1261 #ifdef EXP_WIN32_DRIVER
1262 return _nc_console_twait(sp,
1263 _nc_console_handle(sp->_ifd),
1266 timeleft EVENTLIST_2nd(evl));
1268 return _nc_timed_wait(sp, mode, milliseconds, timeleft EVENTLIST_2nd(evl));
1273 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1277 #ifndef EXP_WIN32_DRIVER
1278 unsigned char c2 = 0;
1285 _nc_set_read_thread(TRUE);
1286 #ifdef EXP_WIN32_DRIVER
1287 n = _nc_console_read(sp,
1288 _nc_console_handle(sp->_ifd),
1291 n = (int) read(sp->_ifd, &c2, (size_t) 1);
1293 _nc_set_read_thread(FALSE);
1294 #ifndef EXP_WIN32_DRIVER
1301 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1305 struct timespec request, remaining;
1306 request.tv_sec = ms / 1000;
1307 request.tv_nsec = (ms % 1000) * 1000000;
1308 while (nanosleep(&request, &remaining) == -1
1309 && errno == EINTR) {
1310 request = remaining;
1313 #elif defined(EXP_WIN32_DRIVER)
1316 _nc_timed_wait(0, 0, ms, (int *) 0 EVENTLIST_2nd(0));
1322 __nc_putp(SCREEN *sp, const char *name GCC_UNUSED, const char *value)
1327 rc = NCURSES_PUTP2(name, value);
1333 __nc_putp_flush(SCREEN *sp, const char *name, const char *value)
1335 int rc = __nc_putp(sp, name, value);
1337 NCURSES_SP_NAME(_nc_flush) (sp);
1343 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, int flag)
1354 (void) __nc_putp_flush(sp, "keypad_xmit", keypad_xmit);
1355 } else if (!flag && keypad_local) {
1356 (void) __nc_putp_flush(sp, "keypad_local", keypad_local);
1358 if (flag && !sp->_tried) {
1359 _nc_init_keytry(sp);
1369 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int c, int flag)
1380 unsigned ch = (unsigned) c;
1382 while ((s = _nc_expand_try(sp->_key_ok,
1383 ch, &count, (size_t) 0)) != 0) {
1384 if (_nc_remove_key(&(sp->_key_ok), ch)) {
1385 code = _nc_add_to_try(&(sp->_keytry), s, ch);
1395 while ((s = _nc_expand_try(sp->_keytry,
1396 ch, &count, (size_t) 0)) != 0) {
1397 if (_nc_remove_key(&(sp->_keytry), ch)) {
1398 code = _nc_add_to_try(&(sp->_key_ok), s, ch);
1413 drv_cursorSet(TERMINAL_CONTROL_BLOCK * TCB, int vis)
1421 T((T_CALLED("tinfo:drv_cursorSet(%p,%d)"), (void *) SP_PARM, vis));
1423 if (SP_PARM != 0 && IsTermInfo(SP_PARM)) {
1426 code = NCURSES_PUTP2_FLUSH("cursor_visible", cursor_visible);
1429 code = NCURSES_PUTP2_FLUSH("cursor_normal", cursor_normal);
1432 code = NCURSES_PUTP2_FLUSH("cursor_invisible", cursor_invisible);
1442 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int key)
1448 res = TINFO_HAS_KEY(TCB->csp, key) == 0 ? FALSE : TRUE;
1453 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_TINFO_DRIVER = {
1455 drv_Name, /* Name */
1456 drv_CanHandle, /* CanHandle */
1457 drv_init, /* init */
1458 drv_release, /* release */
1459 drv_size, /* size */
1460 drv_sgmode, /* sgmode */
1461 drv_conattr, /* conattr */
1462 drv_mvcur, /* hwcur */
1463 drv_mode, /* mode */
1464 drv_rescol, /* rescol */
1465 drv_rescolors, /* rescolors */
1466 drv_setcolor, /* color */
1467 drv_dobeepflash, /* doBeepOrFlash */
1468 drv_initpair, /* initpair */
1469 drv_initcolor, /* initcolor */
1470 drv_do_color, /* docolor */
1471 drv_initmouse, /* initmouse */
1472 drv_testmouse, /* testmouse */
1473 drv_setfilter, /* setfilter */
1474 drv_hwlabel, /* hwlabel */
1475 drv_hwlabelOnOff, /* hwlabelOnOff */
1476 drv_doupdate, /* update */
1477 drv_defaultcolors, /* defaultcolors */
1478 drv_print, /* print */
1479 drv_getsize, /* getsize */
1480 drv_setsize, /* setsize */
1481 drv_initacs, /* initacs */
1482 drv_screen_init, /* scinit */
1483 drv_wrap, /* scexit */
1484 drv_twait, /* twait */
1485 drv_read, /* read */
1487 drv_kpad, /* kpad */
1488 drv_keyok, /* kyOk */
1489 drv_kyExist, /* kyExist */
1490 drv_cursorSet /* cursorSet */
1493 #ifdef EXP_WIN32_DRIVER
1495 * The terminfo driver is mandatory and must always be present.
1496 * So this is the natural place for the driver initialisation
1500 typedef struct DriverEntry {
1502 TERM_DRIVER *driver;
1505 static DRIVER_ENTRY DriverTable[] =
1508 {"win32console", &_nc_WIN_DRIVER},
1510 {"tinfo", &_nc_TINFO_DRIVER} /* must be last */
1514 _nc_get_driver(TERMINAL_CONTROL_BLOCK * TCB, const char *name, int *errret)
1518 TERM_DRIVER *res = (TERM_DRIVER *) 0;
1519 TERM_DRIVER *use = 0;
1521 T((T_CALLED("_nc_get_driver(%p, %s, %p)"),
1522 (void *) TCB, NonNull(name), (void *) errret));
1526 for (i = 0; i < SIZEOF(DriverTable); i++) {
1527 res = DriverTable[i].driver;
1529 if ((i + 1) == SIZEOF(DriverTable)) {
1530 /* For Windows >= 10.0.17763 Windows Console interface implements
1531 virtual Terminal functionality.
1532 If on Windows td_CanHandle returned FALSE although the terminal
1533 name is empty, we default to ms-terminal as tinfo TERM type.
1535 if (name == 0 || *name == 0 || (strcmp(name, "unknown") == 0)) {
1537 T(("Set TERM=%s", name));
1541 if (strcmp(DriverTable[i].name, res->td_name(TCB)) == 0) {
1542 if (res->td_CanHandle(TCB, name, errret)) {
1554 #endif /* EXP_WIN32_DRIVER */