2 * Copyright (C) 1984-2020 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Routines which deal with the characteristics of the terminal.
13 * Uses termcap to be as terminal-independent as possible.
21 #if MSDOS_COMPILER==MSOFTC
24 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
26 #if MSDOS_COMPILER==DJGPPC
31 #if MSDOS_COMPILER==WIN32C
41 #include <sys/ioctl.h>
44 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
65 #include <sys/signal.h>
69 #include <sys/stream.h>
75 #endif /* MSDOS_COMPILER */
78 * Check for broken termios package that forces you to manually
79 * set the line discipline.
82 #define MUST_SET_LINE_DISCIPLINE 1
84 #define MUST_SET_LINE_DISCIPLINE 0
88 #define DEFAULT_TERM "ansi"
89 static char *windowid;
91 #define DEFAULT_TERM "unknown"
94 #if MSDOS_COMPILER==MSOFTC
95 static int videopages;
96 static long msec_loops;
97 static int flash_created = 0;
98 #define SETCOLORS(fg,bg) { _settextcolor(fg); _setbkcolor(bg); }
101 #if MSDOS_COMPILER==BORLANDC
102 static unsigned short *whitescreen;
103 static int flash_created = 0;
105 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
106 #define _settextposition(y,x) gotoxy(x,y)
107 #define _clearscreen(m) clrscr()
108 #define _outtext(s) cputs(s)
109 #define SETCOLORS(fg,bg) { textcolor(fg); textbackground(bg); }
110 extern int sc_height;
113 #if MSDOS_COMPILER==WIN32C
120 static int keyCount = 0;
121 static WORD curr_attr;
122 static int pending_scancode = 0;
123 static char x11mousebuf[] = "[M???"; /* Mouse report, after ESC */
124 static int x11mousePos, x11mouseCount;
126 static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
127 static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
128 HANDLE con_out = INVALID_HANDLE_VALUE; /* current console */
132 static void win32_init_term();
133 static void win32_deinit_term();
135 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
136 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
139 #define FG_COLORS (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
140 #define BG_COLORS (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
141 #define MAKEATTR(fg,bg) ((WORD)((fg)|((bg)<<4)))
142 #define SETCOLORS(fg,bg) { curr_attr = MAKEATTR(fg,bg); \
143 if (SetConsoleTextAttribute(con_out, curr_attr) == 0) \
144 error("SETCOLORS failed", NULL_PARG); }
148 public int nm_fg_color; /* Color of normal text */
149 public int nm_bg_color;
150 public int bo_fg_color; /* Color of bold text */
151 public int bo_bg_color;
152 public int ul_fg_color; /* Color of underlined text */
153 public int ul_bg_color;
154 public int so_fg_color; /* Color of standout text */
155 public int so_bg_color;
156 public int bl_fg_color; /* Color of blinking text */
157 public int bl_bg_color;
158 static int sy_fg_color; /* Color of system text (before less) */
159 static int sy_bg_color;
160 public int sgr_mode; /* Honor ANSI sequences rather than using above */
161 #if MSDOS_COMPILER==WIN32C
162 public int have_ul; /* Is underline available? */
167 * Strings passed to tputs() to do various terminal functions.
170 *sc_pad, /* Pad string */
171 *sc_home, /* Cursor home */
172 *sc_addline, /* Add line, scroll down following lines */
173 *sc_lower_left, /* Cursor to last line, first column */
174 *sc_return, /* Cursor to beginning of current line */
175 *sc_move, /* General cursor positioning */
176 *sc_clear, /* Clear screen */
177 *sc_eol_clear, /* Clear to end of line */
178 *sc_eos_clear, /* Clear to end of screen */
179 *sc_s_in, /* Enter standout (highlighted) mode */
180 *sc_s_out, /* Exit standout mode */
181 *sc_u_in, /* Enter underline mode */
182 *sc_u_out, /* Exit underline mode */
183 *sc_b_in, /* Enter bold mode */
184 *sc_b_out, /* Exit bold mode */
185 *sc_bl_in, /* Enter blink mode */
186 *sc_bl_out, /* Exit blink mode */
187 *sc_visual_bell, /* Visual bell (flash screen) sequence */
188 *sc_backspace, /* Backspace cursor */
189 *sc_s_keypad, /* Start keypad mode */
190 *sc_e_keypad, /* End keypad mode */
191 *sc_s_mousecap, /* Start mouse capture mode */
192 *sc_e_mousecap, /* End mouse capture mode */
193 *sc_init, /* Startup terminal initialization */
194 *sc_deinit; /* Exit terminal de-initialization */
197 static int init_done = 0;
199 public int auto_wrap; /* Terminal does \r\n when write past margin */
200 public int ignaw; /* Terminal ignores \n immediately after wrap */
201 public int erase_char; /* The user's erase char */
202 public int erase2_char; /* The user's other erase char */
203 public int kill_char; /* The user's line-kill char */
204 public int werase_char; /* The user's word-erase char */
205 public int sc_width, sc_height; /* Height & width of screen */
206 public int bo_s_width, bo_e_width; /* Printing width of boldface seq */
207 public int ul_s_width, ul_e_width; /* Printing width of underline seq */
208 public int so_s_width, so_e_width; /* Printing width of standout seq */
209 public int bl_s_width, bl_e_width; /* Printing width of blink seq */
210 public int above_mem, below_mem; /* Memory retained above/below screen */
211 public int can_goto_line; /* Can move cursor to any line */
212 public int clear_bg; /* Clear fills with background color */
213 public int missing_cap = 0; /* Some capability is missing */
214 public char *kent = NULL; /* Keypad ENTER sequence */
216 static int attrmode = AT_NORMAL;
217 static int termcap_debug = -1;
219 extern int one_screen;
222 static char *cheaper LESSPARAMS((char *t1, char *t2, char *def));
223 static void tmodes LESSPARAMS((char *incap, char *outcap, char **instr,
224 char **outstr, char *def_instr, char *def_outstr, char **spp));
228 * These two variables are sometimes defined in,
229 * and needed by, the termcap library.
231 #if MUST_DEFINE_OSPEED
232 extern short ospeed; /* Terminal output baud rate */
233 extern char PC; /* Pad character */
240 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
241 extern int no_back_scroll;
244 extern int no_keypad;
247 extern int screen_trashed;
248 extern int top_scroll;
249 extern int quit_if_one_screen;
253 extern int hilite_search;
255 #if MSDOS_COMPILER==WIN32C
257 extern DWORD console_mode;
258 #ifndef ENABLE_EXTENDED_FLAGS
259 #define ENABLE_EXTENDED_FLAGS 0x80
260 #define ENABLE_QUICK_EDIT_MODE 0x40
266 extern char *tgetstr();
267 extern char *tgoto();
271 * Change terminal to "raw mode", or restore to "normal" mode.
273 * 1. An outstanding read will complete on receipt of a single keystroke.
274 * 2. Input is not echoed.
275 * 3. On output, \n is mapped to \r\n.
276 * 4. \t is NOT expanded into spaces.
277 * 5. Signal-causing characters such as ctrl-C (interrupt),
278 * etc. are NOT disabled.
279 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
285 static int curr_on = 0;
289 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
290 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
293 static struct termios save_term;
294 static int saved_term = 0;
299 * Get terminal modes.
304 * Save modes and set certain variables dependent on modes.
312 switch (cfgetospeed(&s))
315 case B0: ospeed = 0; break;
318 case B50: ospeed = 1; break;
321 case B75: ospeed = 2; break;
324 case B110: ospeed = 3; break;
327 case B134: ospeed = 4; break;
330 case B150: ospeed = 5; break;
333 case B200: ospeed = 6; break;
336 case B300: ospeed = 7; break;
339 case B600: ospeed = 8; break;
342 case B1200: ospeed = 9; break;
345 case B1800: ospeed = 10; break;
348 case B2400: ospeed = 11; break;
351 case B4800: ospeed = 12; break;
354 case B9600: ospeed = 13; break;
357 case EXTA: ospeed = 14; break;
360 case EXTB: ospeed = 15; break;
363 case B57600: ospeed = 16; break;
366 case B115200: ospeed = 17; break;
371 erase_char = s.c_cc[VERASE];
373 erase2_char = s.c_cc[VERASE2];
375 kill_char = s.c_cc[VKILL];
377 werase_char = s.c_cc[VWERASE];
379 werase_char = CONTROL('W');
383 * Set the modes to the way we want them.
445 #if MUST_SET_LINE_DISCIPLINE
447 * System's termios is broken; need to explicitly
448 * request TERMIODISC line discipline.
450 s.c_line = TERMIODISC;
455 * Restore saved modes.
462 tcsetattr(tty, TCSADRAIN, &s);
463 #if MUST_SET_LINE_DISCIPLINE
467 * Broken termios *ignores* any line discipline
468 * except TERMIODISC. A different old line discipline
469 * is therefore not restored, yet. Restore the old
470 * line discipline by hand.
472 ioctl(tty, TIOCSETD, &save_term.c_line);
480 static struct termio save_term;
481 static int saved_term = 0;
486 * Get terminal modes.
488 ioctl(tty, TCGETA, &s);
491 * Save modes and set certain variables dependent on modes.
499 ospeed = s.c_cflag & CBAUD;
501 erase_char = s.c_cc[VERASE];
502 kill_char = s.c_cc[VKILL];
504 werase_char = s.c_cc[VWERASE];
506 werase_char = CONTROL('W');
510 * Set the modes to the way we want them.
512 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
513 s.c_oflag |= (OPOST|ONLCR|TAB3);
514 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
520 * Restore saved modes.
524 ioctl(tty, TCSETAW, &s);
530 static struct sgttyb save_term;
531 static int saved_term = 0;
536 * Get terminal modes.
538 ioctl(tty, TIOCGETP, &s);
541 * Save modes and set certain variables dependent on modes.
549 ospeed = s.sg_ospeed;
551 erase_char = s.sg_erase;
552 kill_char = s.sg_kill;
553 werase_char = CONTROL('W');
556 * Set the modes to the way we want them.
558 s.sg_flags |= CBREAK;
559 s.sg_flags &= ~(ECHO|XTABS);
563 * Restore saved modes.
567 ioctl(tty, TIOCSETN, &s);
573 static struct sgbuf save_term;
574 static int saved_term = 0;
579 * Get terminal modes.
584 * Save modes and set certain variables dependent on modes.
591 erase_char = s.sg_bspch;
592 kill_char = s.sg_dlnch;
593 werase_char = CONTROL('W');
596 * Set the modes to the way we want them.
605 * Restore saved modes.
612 /* MS-DOS, Windows, or OS2 */
615 LSIGNAL(SIGINT, SIG_IGN);
618 #if MSDOS_COMPILER==DJGPPC
619 kill_char = CONTROL('U');
621 * So that when we shell out or run another program, its
622 * stdin is in cooked mode. We do not switch stdin to binary
623 * mode if fd0 is zero, since that means we were called before
624 * tty was reopened in open_getchr, in which case we would be
625 * changing the original stdin device outside less.
628 setmode(0, on ? O_BINARY : O_TEXT);
632 werase_char = CONTROL('W');
642 * Some glue to prevent calling termcap functions if tgetent() failed.
654 struct env { struct env *next; char *name; char *value; };
655 static struct env *envs = NULL;
657 for (p = envs; p != NULL; p = p->next)
658 if (strcmp(p->name, capname) == 0)
660 p = (struct env *) ecalloc(1, sizeof(struct env));
661 p->name = save(capname);
662 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
663 sprintf(p->value, "<%s>", capname);
668 SNPRINTF1(name, sizeof(name), "LESS_TERMCAP_%s", capname);
669 return (lgetenv(name));
678 if ((s = ltget_env(capname)) != NULL)
679 return (*s != '\0' && *s != '0');
682 return (tgetflag(capname));
691 if ((s = ltget_env(capname)) != NULL)
695 return (tgetnum(capname));
699 ltgetstr(capname, pp)
705 if ((s = ltget_env(capname)) != NULL)
709 return (tgetstr(capname, pp));
711 #endif /* MSDOS_COMPILER */
714 * Get size of the output screen.
726 #define DEF_SC_WIDTH 80
728 #define DEF_SC_HEIGHT 25
730 #define DEF_SC_HEIGHT 24
734 sys_width = sys_height = 0;
736 #if MSDOS_COMPILER==MSOFTC
738 struct videoconfig w;
740 sys_height = w.numtextrows;
741 sys_width = w.numtextcols;
744 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
748 sys_height = w.screenheight;
749 sys_width = w.screenwidth;
752 #if MSDOS_COMPILER==WIN32C
754 CONSOLE_SCREEN_BUFFER_INFO scr;
755 GetConsoleScreenBufferInfo(con_out, &scr);
756 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
757 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
767 * When using terminal emulators for XFree86/OS2, the
768 * _scrsize function does not work well.
769 * Call the scrsize.exe program to get the window size.
771 windowid = getenv("WINDOWID");
772 if (windowid != NULL)
774 FILE *fd = popen("scrsize", "rt");
778 fscanf(fd, "%i %i", &w, &h);
792 if (ioctl(2, TIOCGWINSZ, &w) == 0)
795 sys_height = w.ws_row;
797 sys_width = w.ws_col;
804 if (ioctl(2, WIOCGETD, &w) == 0)
807 sys_height = w.uw_height / w.uw_vs;
809 sys_width = w.uw_width / w.uw_hs;
820 sc_height = sys_height;
821 else if ((s = lgetenv("LINES")) != NULL)
824 else if ((n = ltgetnum("li")) > 0)
828 sc_height = DEF_SC_HEIGHT;
831 sc_width = sys_width;
832 else if ((s = lgetenv("COLUMNS")) != NULL)
835 else if ((n = ltgetnum("co")) > 0)
839 sc_width = DEF_SC_WIDTH;
842 #if MSDOS_COMPILER==MSOFTC
844 * Figure out how many empty loops it takes to delay a millisecond.
847 get_clock(VOID_PARAM)
852 * Get synchronized at the start of a tick.
855 while (clock() == start)
858 * Now count loops till the next tick.
862 while (clock() == start)
865 * Convert from (loops per clock) to (loops per millisecond).
867 msec_loops *= CLOCKS_PER_SEC;
872 * Delay for a specified number of milliseconds.
882 for (i = 0; i < msec_loops; i++)
889 * Return the characters actually input by a "special" key.
895 static char tbuf[40];
897 #if MSDOS_COMPILER || OS2
898 static char k_right[] = { '\340', PCK_RIGHT, 0 };
899 static char k_left[] = { '\340', PCK_LEFT, 0 };
900 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
901 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
902 static char k_insert[] = { '\340', PCK_INSERT, 0 };
903 static char k_delete[] = { '\340', PCK_DELETE, 0 };
904 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
905 static char k_ctl_backspace[] = { '\177', 0 };
906 static char k_home[] = { '\340', PCK_HOME, 0 };
907 static char k_end[] = { '\340', PCK_END, 0 };
908 static char k_up[] = { '\340', PCK_UP, 0 };
909 static char k_down[] = { '\340', PCK_DOWN, 0 };
910 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
911 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
912 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
913 static char k_f1[] = { '\340', PCK_F1, 0 };
923 * If windowid is not NULL, assume less is executed in
924 * the XFree86 environment.
927 s = windowid ? ltgetstr("kr", &sp) : k_right;
930 s = windowid ? ltgetstr("kl", &sp) : k_left;
933 s = windowid ? ltgetstr("ku", &sp) : k_up;
936 s = windowid ? ltgetstr("kd", &sp) : k_down;
939 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
942 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
945 s = windowid ? ltgetstr("kh", &sp) : k_home;
948 s = windowid ? ltgetstr("@7", &sp) : k_end;
951 s = windowid ? ltgetstr("kD", &sp) : k_delete;
989 #if MSDOS_COMPILER || OS2
993 case SK_CTL_LEFT_ARROW:
996 case SK_CTL_RIGHT_ARROW:
999 case SK_CTL_BACKSPACE:
1000 s = k_ctl_backspace;
1012 case SK_RIGHT_ARROW:
1013 s = ltgetstr("kr", &sp);
1016 s = ltgetstr("kl", &sp);
1019 s = ltgetstr("ku", &sp);
1022 s = ltgetstr("kd", &sp);
1025 s = ltgetstr("kP", &sp);
1028 s = ltgetstr("kN", &sp);
1031 s = ltgetstr("kh", &sp);
1034 s = ltgetstr("@7", &sp);
1037 s = ltgetstr("kD", &sp);
1047 tbuf[0] = CONTROL('K');
1058 * Get terminal capabilities via termcap.
1061 get_term(VOID_PARAM)
1063 termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG"));
1070 * Set up default colors.
1071 * The xx_s_width and xx_e_width vars are already initialized to 0.
1073 #if MSDOS_COMPILER==MSOFTC
1074 sy_bg_color = _getbkcolor();
1075 sy_fg_color = _gettextcolor();
1078 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1082 sy_bg_color = (w.attribute >> 4) & 0x0F;
1083 sy_fg_color = (w.attribute >> 0) & 0x0F;
1086 #if MSDOS_COMPILER==WIN32C
1088 CONSOLE_SCREEN_BUFFER_INFO scr;
1090 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1092 * Always open stdin in binary. Note this *must* be done
1093 * before any file operations have been done on fd0.
1096 GetConsoleScreenBufferInfo(con_out, &scr);
1097 curr_attr = scr.wAttributes;
1098 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1099 sy_fg_color = curr_attr & FG_COLORS;
1104 nm_fg_color = sy_fg_color;
1105 nm_bg_color = sy_bg_color;
1117 * Get size of the screen.
1123 #else /* !MSDOS_COMPILER */
1129 * Some termcap libraries assume termbuf is static
1130 * (accessible after tgetent returns).
1132 static char termbuf[TERMBUF_SIZE];
1133 static char sbuf[TERMSBUF_SIZE];
1137 * Make sure the termcap database is available.
1139 sp = lgetenv("TERMCAP");
1143 if ((sp = homefile("termcap.dat")) != NULL)
1145 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1146 sprintf(termcap, "TERMCAP=%s", sp);
1153 * Find out what kind of terminal this is.
1155 if ((term = lgetenv("TERM")) == NULL)
1156 term = DEFAULT_TERM;
1158 /* {{ Should probably just pass NULL instead of termbuf. }} */
1159 if (tgetent(termbuf, term) != TGETENT_OK)
1161 if (ltgetflag("hc"))
1165 * Get size of the screen.
1170 auto_wrap = ltgetflag("am");
1171 ignaw = ltgetflag("xn");
1172 above_mem = ltgetflag("da");
1173 below_mem = ltgetflag("db");
1174 clear_bg = ltgetflag("ut");
1177 * Assumes termcap variable "sg" is the printing width of:
1178 * the standout sequence, the end standout sequence,
1179 * the underline sequence, the end underline sequence,
1180 * the boldface sequence, and the end boldface sequence.
1182 if ((so_s_width = ltgetnum("sg")) < 0)
1184 so_e_width = so_s_width;
1186 bo_s_width = bo_e_width = so_s_width;
1187 ul_s_width = ul_e_width = so_s_width;
1188 bl_s_width = bl_e_width = so_s_width;
1191 if (so_s_width > 0 || so_e_width > 0)
1193 * Disable highlighting by default on magic cookie terminals.
1194 * Turning on highlighting might change the displayed width
1195 * of a line, causing the display to get messed up.
1196 * The user can turn it back on with -g,
1197 * but she won't like the results.
1203 * Get various string-valued capabilities.
1208 sc_pad = ltgetstr("pc", &sp);
1213 sc_s_keypad = ltgetstr("ks", &sp);
1214 if (sc_s_keypad == NULL)
1216 sc_e_keypad = ltgetstr("ke", &sp);
1217 if (sc_e_keypad == NULL)
1219 kent = ltgetstr("@8", &sp);
1221 sc_s_mousecap = ltgetstr("MOUSE_START", &sp);
1222 if (sc_s_mousecap == NULL)
1223 sc_s_mousecap = ESCS "[?1000h" ESCS "[?1006h";
1224 sc_e_mousecap = ltgetstr("MOUSE_END", &sp);
1225 if (sc_e_mousecap == NULL)
1226 sc_e_mousecap = ESCS "[?1006l" ESCS "[?1000l";
1228 sc_init = ltgetstr("ti", &sp);
1229 if (sc_init == NULL)
1232 sc_deinit= ltgetstr("te", &sp);
1233 if (sc_deinit == NULL)
1236 sc_eol_clear = ltgetstr("ce", &sp);
1237 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1243 sc_eos_clear = ltgetstr("cd", &sp);
1244 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1250 sc_clear = ltgetstr("cl", &sp);
1251 if (sc_clear == NULL || *sc_clear == '\0')
1257 sc_move = ltgetstr("cm", &sp);
1258 if (sc_move == NULL || *sc_move == '\0')
1261 * This is not an error here, because we don't
1262 * always need sc_move.
1263 * We need it only if we don't have home or lower-left.
1270 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1271 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1272 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1273 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1275 sc_visual_bell = ltgetstr("vb", &sp);
1276 if (sc_visual_bell == NULL)
1277 sc_visual_bell = "";
1279 if (ltgetflag("bs"))
1280 sc_backspace = "\b";
1283 sc_backspace = ltgetstr("bc", &sp);
1284 if (sc_backspace == NULL || *sc_backspace == '\0')
1285 sc_backspace = "\b";
1289 * Choose between using "ho" and "cm" ("home" and "cursor move")
1290 * to move the cursor to the upper left corner of the screen.
1292 t1 = ltgetstr("ho", &sp);
1295 if (*sc_move == '\0')
1299 strcpy(sp, tgoto(sc_move, 0, 0));
1301 sp += strlen(sp) + 1;
1303 sc_home = cheaper(t1, t2, "|\b^");
1306 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1307 * to move the cursor to the lower left corner of the screen.
1309 t1 = ltgetstr("ll", &sp);
1312 if (*sc_move == '\0')
1316 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1318 sp += strlen(sp) + 1;
1320 sc_lower_left = cheaper(t1, t2, "\r");
1323 * Get carriage return string.
1325 sc_return = ltgetstr("cr", &sp);
1326 if (sc_return == NULL)
1330 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1331 * to add a line at the top of the screen.
1333 t1 = ltgetstr("al", &sp);
1336 t2 = ltgetstr("sr", &sp);
1340 if (*t1 == '\0' && *t2 == '\0')
1347 sc_addline = cheaper(t1, t2, "");
1348 if (*sc_addline == '\0')
1351 * Force repaint on any backward movement.
1356 #endif /* MSDOS_COMPILER */
1361 * Return the cost of displaying a termcap string.
1362 * We use the trick of calling tputs, but as a char printing function
1363 * we give it inc_costcount, which just increments "costcount".
1364 * This tells us how many chars would be printed by using this string.
1365 * {{ Couldn't we just use strlen? }}
1367 static int costcount;
1383 tputs(t, sc_height, inc_costcount);
1388 * Return the "best" of the two given termcap strings.
1389 * The best, if both exist, is the one with the lower
1390 * cost (see cost() function).
1393 cheaper(t1, t2, def)
1397 if (*t1 == '\0' && *t2 == '\0')
1406 if (cost(t1) < cost(t2))
1412 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1421 *instr = ltgetstr(incap, spp);
1426 *outstr = def_outstr;
1430 *outstr = ltgetstr(outcap, spp);
1431 if (*outstr == NULL)
1432 /* No specific out capability; use "me". */
1433 *outstr = ltgetstr("me", spp);
1434 if (*outstr == NULL)
1435 /* Don't even have "me"; use a null string. */
1439 #endif /* MSDOS_COMPILER */
1443 * Below are the functions which perform all the
1444 * terminal-specific screen manipulation.
1450 #if MSDOS_COMPILER==WIN32C
1452 _settextposition(int row, int col)
1455 CONSOLE_SCREEN_BUFFER_INFO csbi;
1457 GetConsoleScreenBufferInfo(con_out, &csbi);
1458 cpos.X = csbi.srWindow.Left + (col - 1);
1459 cpos.Y = csbi.srWindow.Top + (row - 1);
1460 SetConsoleCursorPosition(con_out, cpos);
1465 * Initialize the screen to the correct color at startup.
1468 initcolor(VOID_PARAM)
1470 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1473 SETCOLORS(nm_fg_color, nm_bg_color);
1476 * This clears the screen at startup. This is different from
1477 * the behavior of other versions of less. Disable it for now.
1484 * Create a complete, blank screen using "normal" colors.
1486 SETCOLORS(nm_fg_color, nm_bg_color);
1487 blanks = (char *) ecalloc(width+1, sizeof(char));
1488 for (col = 0; col < sc_width; col++)
1490 blanks[sc_width] = '\0';
1491 for (row = 0; row < sc_height; row++)
1498 #if MSDOS_COMPILER==WIN32C
1501 * Termcap-like init with a private win32 console.
1504 win32_init_term(VOID_PARAM)
1506 CONSOLE_SCREEN_BUFFER_INFO scr;
1509 if (con_out_save == INVALID_HANDLE_VALUE)
1512 GetConsoleScreenBufferInfo(con_out_save, &scr);
1514 if (con_out_ours == INVALID_HANDLE_VALUE)
1519 * Create our own screen buffer, so that we
1520 * may restore the original when done.
1522 con_out_ours = CreateConsoleScreenBuffer(
1523 GENERIC_WRITE | GENERIC_READ,
1524 FILE_SHARE_WRITE | FILE_SHARE_READ,
1525 (LPSECURITY_ATTRIBUTES) NULL,
1526 CONSOLE_TEXTMODE_BUFFER,
1529 * Enable underline, if available.
1531 GetConsoleMode(con_out_ours, &output_mode);
1532 have_ul = SetConsoleMode(con_out_ours,
1533 output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1536 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1537 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1538 SetConsoleScreenBufferSize(con_out_ours, size);
1539 SetConsoleActiveScreenBuffer(con_out_ours);
1540 con_out = con_out_ours;
1544 * Restore the startup console.
1547 win32_deinit_term(VOID_PARAM)
1549 if (con_out_save == INVALID_HANDLE_VALUE)
1552 (void) CloseHandle(con_out_ours);
1553 SetConsoleActiveScreenBuffer(con_out_save);
1554 con_out = con_out_save;
1560 * Configure the termimal so mouse clicks and wheel moves
1561 * produce input to less.
1564 init_mouse(VOID_PARAM)
1569 tputs(sc_s_mousecap, sc_height, putchr);
1571 #if MSDOS_COMPILER==WIN32C
1572 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT
1573 | ENABLE_EXTENDED_FLAGS /* disable quick edit */);
1580 * Configure the terminal so mouse clicks and wheel moves
1581 * are handled by the system (so text can be selected, etc).
1584 deinit_mouse(VOID_PARAM)
1589 tputs(sc_e_mousecap, sc_height, putchr);
1591 #if MSDOS_COMPILER==WIN32C
1592 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_EXTENDED_FLAGS
1593 | (console_mode & ENABLE_QUICK_EDIT_MODE));
1599 * Initialize terminal
1605 if (!(quit_if_one_screen && one_screen))
1608 tputs(sc_init, sc_height, putchr);
1610 tputs(sc_s_keypad, sc_height, putchr);
1618 * This is nice to terminals with no alternate screen,
1619 * but with saved scrolled-off-the-top lines. This way,
1620 * no previous line is lost, but we start with a whole
1621 * screen to ourself.
1623 for (i = 1; i < sc_height; i++)
1628 #if MSDOS_COMPILER==WIN32C
1629 if (!(quit_if_one_screen && one_screen))
1644 * Deinitialize terminal
1652 if (!(quit_if_one_screen && one_screen))
1656 tputs(sc_e_keypad, sc_height, putchr);
1658 tputs(sc_deinit, sc_height, putchr);
1661 /* Restore system colors. */
1662 SETCOLORS(sy_fg_color, sy_bg_color);
1663 #if MSDOS_COMPILER==WIN32C
1664 if (!(quit_if_one_screen && one_screen))
1668 win32_deinit_term();
1671 /* Need clreol to make SETCOLORS take effect. */
1679 * Home cursor (move to upper left corner of screen).
1685 tputs(sc_home, 1, putchr);
1688 _settextposition(1,1);
1693 * Add a blank line (called with cursor at home).
1694 * Should scroll the display down.
1697 add_line(VOID_PARAM)
1700 tputs(sc_addline, sc_height, putchr);
1703 #if MSDOS_COMPILER==MSOFTC
1704 _scrolltextwindow(_GSCROLLDOWN);
1705 _settextposition(1,1);
1707 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1708 movetext(1,1, sc_width,sc_height-1, 1,2);
1712 #if MSDOS_COMPILER==WIN32C
1715 SMALL_RECT rcSrc, rcClip;
1717 CONSOLE_SCREEN_BUFFER_INFO csbi;
1719 GetConsoleScreenBufferInfo(con_out,&csbi);
1721 /* The clip rectangle is the entire visible screen. */
1722 rcClip.Left = csbi.srWindow.Left;
1723 rcClip.Top = csbi.srWindow.Top;
1724 rcClip.Right = csbi.srWindow.Right;
1725 rcClip.Bottom = csbi.srWindow.Bottom;
1727 /* The source rectangle is the visible screen minus the last line. */
1731 /* Move the top left corner of the source window down one row. */
1732 new_org.X = rcSrc.Left;
1733 new_org.Y = rcSrc.Top + 1;
1735 /* Fill the right character and attributes. */
1736 fillchar.Char.AsciiChar = ' ';
1737 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1738 fillchar.Attributes = curr_attr;
1739 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1740 _settextposition(1,1);
1750 * Remove the n topmost lines and scroll everything below it in the
1751 * window upward. This is needed to stop leaking the topmost line
1752 * into the scrollback buffer when we go down-one-line (in WIN32).
1758 #if MSDOS_COMPILER==WIN32C
1759 SMALL_RECT rcSrc, rcClip;
1762 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1764 if (n >= sc_height - 1)
1773 GetConsoleScreenBufferInfo(con_out, &csbi);
1775 /* Get the extent of all-visible-rows-but-the-last. */
1776 rcSrc.Left = csbi.srWindow.Left;
1777 rcSrc.Top = csbi.srWindow.Top + n;
1778 rcSrc.Right = csbi.srWindow.Right;
1779 rcSrc.Bottom = csbi.srWindow.Bottom;
1781 /* Get the clip rectangle. */
1782 rcClip.Left = rcSrc.Left;
1783 rcClip.Top = csbi.srWindow.Top;
1784 rcClip.Right = rcSrc.Right;
1785 rcClip.Bottom = rcSrc.Bottom ;
1787 /* Move the source window up n rows. */
1788 new_org.X = rcSrc.Left;
1789 new_org.Y = rcSrc.Top - n;
1791 /* Fill the right character and attributes. */
1792 fillchar.Char.AsciiChar = ' ';
1793 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1794 fillchar.Attributes = curr_attr;
1796 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1798 /* Position cursor on first blank line. */
1799 goto_line(sc_height - n - 1);
1804 #if MSDOS_COMPILER==WIN32C
1809 win32_clear(VOID_PARAM)
1812 * This will clear only the currently visible rows of the NT
1813 * console buffer, which means none of the precious scrollback
1814 * rows are touched making for faster scrolling. Note that, if
1815 * the window has fewer columns than the console buffer (i.e.
1816 * there is a horizontal scrollbar as well), the entire width
1817 * of the visible rows will be cleared.
1822 CONSOLE_SCREEN_BUFFER_INFO csbi;
1824 /* get the number of cells in the current buffer */
1825 GetConsoleScreenBufferInfo(con_out, &csbi);
1826 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1828 topleft.Y = csbi.srWindow.Top;
1830 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1831 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1832 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1836 * Remove the n topmost lines and scroll everything below it in the
1843 SMALL_RECT rcSrc, rcClip;
1849 CONSOLE_SCREEN_BUFFER_INFO csbi;
1854 if (n >= sc_height - 1)
1857 _settextposition(1,1);
1861 /* Get the extent of what will remain visible after scrolling. */
1862 GetConsoleScreenBufferInfo(con_out, &csbi);
1863 rcSrc.Left = csbi.srWindow.Left;
1864 rcSrc.Top = csbi.srWindow.Top + n;
1865 rcSrc.Right = csbi.srWindow.Right;
1866 rcSrc.Bottom = csbi.srWindow.Bottom;
1868 /* Get the clip rectangle. */
1869 rcClip.Left = rcSrc.Left;
1870 rcClip.Top = csbi.srWindow.Top;
1871 rcClip.Right = rcSrc.Right;
1872 rcClip.Bottom = rcSrc.Bottom ;
1874 /* Move the source text to the top of the screen. */
1875 new_org.X = rcSrc.Left;
1876 new_org.Y = rcClip.Top;
1878 /* Fill the right character and attributes. */
1879 fillchar.Char.AsciiChar = ' ';
1880 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1882 /* Scroll the window. */
1883 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1884 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1886 /* Clear remaining lines at bottom. */
1887 topleft.X = csbi.dwCursorPosition.X;
1888 topleft.Y = rcSrc.Bottom - n;
1889 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1890 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1892 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1894 SetConsoleTextAttribute(con_out, curr_attr);
1896 /* Move cursor n lines up from where it was. */
1897 csbi.dwCursorPosition.Y -= n;
1898 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1903 * Move cursor to lower left corner of screen.
1906 lower_left(VOID_PARAM)
1911 tputs(sc_lower_left, 1, putchr);
1914 _settextposition(sc_height, 1);
1919 * Move cursor to left position of current line.
1922 line_left(VOID_PARAM)
1925 tputs(sc_return, 1, putchr);
1929 #if MSDOS_COMPILER==WIN32C
1931 CONSOLE_SCREEN_BUFFER_INFO scr;
1932 GetConsoleScreenBufferInfo(con_out, &scr);
1933 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1936 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1940 struct rccoord tpos = _gettextposition();
1945 _settextposition(row, 1);
1950 * Check if the console size has changed and reset internals
1951 * (in lieu of SIGWINCH for WIN32).
1954 check_winch(VOID_PARAM)
1956 #if MSDOS_COMPILER==WIN32C
1957 CONSOLE_SCREEN_BUFFER_INFO scr;
1960 if (con_out == INVALID_HANDLE_VALUE)
1964 GetConsoleScreenBufferInfo(con_out, &scr);
1965 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1966 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1967 if (size.Y != sc_height || size.X != sc_width)
1971 if (!no_init && con_out_ours == con_out)
1972 SetConsoleScreenBufferSize(con_out, size);
1974 wscroll = (sc_height + 1) / 2;
1981 * Goto a specific line on the screen.
1988 tputs(tgoto(sc_move, 0, sindex), 1, putchr);
1991 _settextposition(sindex+1, 1);
1995 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1997 * Create an alternate screen which is all white.
1998 * This screen is used to create a "flash" effect, by displaying it
1999 * briefly and then switching back to the normal screen.
2000 * {{ Yuck! There must be a better way to get a visual bell. }}
2003 create_flash(VOID_PARAM)
2005 #if MSDOS_COMPILER==MSOFTC
2006 struct videoconfig w;
2010 _getvideoconfig(&w);
2011 videopages = w.numvideopages;
2014 at_enter(AT_STANDOUT);
2019 at_enter(AT_STANDOUT);
2020 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
2021 for (col = 0; col < w.numtextcols; col++)
2023 for (row = w.numtextrows; row > 0; row--)
2024 _outmem(blanks, w.numtextcols);
2031 #if MSDOS_COMPILER==BORLANDC
2034 whitescreen = (unsigned short *)
2035 malloc(sc_width * sc_height * sizeof(short));
2036 if (whitescreen == NULL)
2038 for (n = 0; n < sc_width * sc_height; n++)
2039 whitescreen[n] = 0x7020;
2044 #endif /* MSDOS_COMPILER */
2047 * Output the "visual bell", if there is one.
2053 if (*sc_visual_bell == '\0')
2055 tputs(sc_visual_bell, sc_height, putchr);
2057 #if MSDOS_COMPILER==DJGPPC
2060 #if MSDOS_COMPILER==MSOFTC
2062 * Create a flash screen on the second video page.
2063 * Switch to that page, then switch back.
2073 #if MSDOS_COMPILER==BORLANDC
2074 unsigned short *currscreen;
2077 * Get a copy of the current screen.
2078 * Display the flash screen.
2079 * Then restore the old screen.
2083 if (whitescreen == NULL)
2085 currscreen = (unsigned short *)
2086 malloc(sc_width * sc_height * sizeof(short));
2087 if (currscreen == NULL) return;
2088 gettext(1, 1, sc_width, sc_height, currscreen);
2089 puttext(1, 1, sc_width, sc_height, whitescreen);
2091 puttext(1, 1, sc_width, sc_height, currscreen);
2094 #if MSDOS_COMPILER==WIN32C
2095 /* paint screen with an inverse color */
2098 /* leave it displayed for 100 msec. */
2101 /* restore with a redraw */
2117 putchr(CONTROL('G'));
2119 #if MSDOS_COMPILER==WIN32C
2128 * Ring the terminal bell.
2133 if (quiet == VERY_QUIET)
2146 tputs(sc_clear, sc_height, putchr);
2149 #if MSDOS_COMPILER==WIN32C
2152 _clearscreen(_GCLEARSCREEN);
2158 * Clear from the cursor to the end of the cursor's line.
2159 * {{ This must not move the cursor. }}
2162 clear_eol(VOID_PARAM)
2165 tputs(sc_eol_clear, 1, putchr);
2167 #if MSDOS_COMPILER==MSOFTC
2170 struct rccoord tpos;
2174 * Save current state.
2176 tpos = _gettextposition();
2177 _gettextwindow(&top, &left, &bot, &right);
2179 * Set a temporary window to the current line,
2180 * from the cursor's position to the right edge of the screen.
2181 * Then clear that window.
2183 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2184 _clearscreen(_GWINDOW);
2188 _settextwindow(top, left, bot, right);
2189 _settextposition(tpos.row, tpos.col);
2191 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2195 #if MSDOS_COMPILER==WIN32C
2198 CONSOLE_SCREEN_BUFFER_INFO scr;
2201 memset(&scr, 0, sizeof(scr));
2202 GetConsoleScreenBufferInfo(con_out, &scr);
2203 cpos.X = scr.dwCursorPosition.X;
2204 cpos.Y = scr.dwCursorPosition.Y;
2205 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2206 FillConsoleOutputAttribute(con_out, curr_attr,
2207 scr.dwSize.X - cpos.X, cpos, &nchars);
2208 FillConsoleOutputCharacter(con_out, ' ',
2209 scr.dwSize.X - cpos.X, cpos, &nchars);
2217 * Clear the current line.
2218 * Clear the screen if there's off-screen memory below the display.
2221 clear_eol_bot(VOID_PARAM)
2227 tputs(sc_eos_clear, 1, putchr);
2229 tputs(sc_eol_clear, 1, putchr);
2234 * Clear the bottom line of the display.
2235 * Leave the cursor at the beginning of the bottom line.
2238 clear_bot(VOID_PARAM)
2241 * If we're in a non-normal attribute mode, temporarily exit
2242 * the mode while we do the clear. Some terminals fill the
2243 * cleared area with the current attribute.
2250 if (attrmode == AT_NORMAL)
2254 int saved_attrmode = attrmode;
2258 at_enter(saved_attrmode);
2266 attr = apply_at_specials(attr);
2269 /* The one with the most priority is last. */
2270 if (attr & AT_UNDERLINE)
2271 tputs(sc_u_in, 1, putchr);
2273 tputs(sc_b_in, 1, putchr);
2274 if (attr & AT_BLINK)
2275 tputs(sc_bl_in, 1, putchr);
2276 if (attr & AT_STANDOUT)
2277 tputs(sc_s_in, 1, putchr);
2280 /* The one with the most priority is first. */
2281 if (attr & AT_STANDOUT)
2283 SETCOLORS(so_fg_color, so_bg_color);
2284 } else if (attr & AT_BLINK)
2286 SETCOLORS(bl_fg_color, bl_bg_color);
2288 else if (attr & AT_BOLD)
2290 SETCOLORS(bo_fg_color, bo_bg_color);
2292 else if (attr & AT_UNDERLINE)
2294 SETCOLORS(ul_fg_color, ul_bg_color);
2305 /* Undo things in the reverse order we did them. */
2306 if (attrmode & AT_STANDOUT)
2307 tputs(sc_s_out, 1, putchr);
2308 if (attrmode & AT_BLINK)
2309 tputs(sc_bl_out, 1, putchr);
2310 if (attrmode & AT_BOLD)
2311 tputs(sc_b_out, 1, putchr);
2312 if (attrmode & AT_UNDERLINE)
2313 tputs(sc_u_out, 1, putchr);
2316 SETCOLORS(nm_fg_color, nm_bg_color);
2319 attrmode = AT_NORMAL;
2326 int new_attrmode = apply_at_specials(attr);
2327 int ignore_modes = AT_ANSI;
2329 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2337 is_at_equiv(attr1, attr2)
2341 attr1 = apply_at_specials(attr1);
2342 attr2 = apply_at_specials(attr2);
2344 return (attr1 == attr2);
2348 apply_at_specials(attr)
2351 if (attr & AT_BINARY)
2353 if (attr & AT_HILITE)
2354 attr |= AT_STANDOUT;
2355 attr &= ~(AT_BINARY|AT_HILITE);
2360 #if 0 /* No longer used */
2362 * Erase the character to the left of the cursor
2363 * and move the cursor left.
2366 backspace(VOID_PARAM)
2370 * Erase the previous character by overstriking with a space.
2372 tputs(sc_backspace, 1, putchr);
2374 tputs(sc_backspace, 1, putchr);
2376 #if MSDOS_COMPILER==MSOFTC
2377 struct rccoord tpos;
2380 tpos = _gettextposition();
2383 _settextposition(tpos.row, tpos.col-1);
2385 _settextposition(tpos.row, tpos.col-1);
2387 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2390 #if MSDOS_COMPILER==WIN32C
2393 CONSOLE_SCREEN_BUFFER_INFO scr;
2396 GetConsoleScreenBufferInfo(con_out, &scr);
2397 cpos = scr.dwCursorPosition;
2401 SetConsoleCursorPosition(con_out, cpos);
2402 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2403 SetConsoleCursorPosition(con_out, cpos);
2412 * Output a plain backspace, without erasing the previous char.
2422 tputs(sc_backspace, 1, putchr);
2428 #if MSDOS_COMPILER==MSOFTC
2429 struct rccoord tpos;
2430 tpos = _gettextposition();
2434 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2438 #if MSDOS_COMPILER==WIN32C
2439 CONSOLE_SCREEN_BUFFER_INFO scr;
2440 GetConsoleScreenBufferInfo(con_out, &scr);
2441 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2442 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2449 _settextposition(row, col-1);
2450 #endif /* MSDOS_COMPILER */
2454 #if MSDOS_COMPILER==WIN32C
2456 * Determine whether an input character is waiting to be read.
2459 win32_kbhit(VOID_PARAM)
2467 currentKey.ascii = 0;
2468 currentKey.scan = 0;
2470 if (x11mouseCount > 0)
2472 currentKey.ascii = x11mousebuf[x11mousePos++];
2479 * Wait for a real key-down event, but
2480 * ignore SHIFT and CONTROL key events.
2484 PeekConsoleInput(tty, &ip, 1, &read);
2487 ReadConsoleInput(tty, &ip, 1, &read);
2488 /* generate an X11 mouse sequence from the mouse event */
2489 if (mousecap && ip.EventType == MOUSE_EVENT &&
2490 ip.Event.MouseEvent.dwEventFlags != MOUSE_MOVED)
2492 x11mousebuf[3] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.X + 1;
2493 x11mousebuf[4] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.Y + 1;
2494 switch (ip.Event.MouseEvent.dwEventFlags)
2496 case 0: /* press or release */
2497 if (ip.Event.MouseEvent.dwButtonState == 0)
2498 x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL;
2499 else if (ip.Event.MouseEvent.dwButtonState & (FROM_LEFT_3RD_BUTTON_PRESSED | FROM_LEFT_4TH_BUTTON_PRESSED))
2502 x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON1 + ((int)ip.Event.MouseEvent.dwButtonState << 1);
2505 x11mousebuf[2] = X11MOUSE_OFFSET + (((int)ip.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP);
2512 currentKey.ascii = ESC;
2516 } while (ip.EventType != KEY_EVENT ||
2517 ip.Event.KeyEvent.bKeyDown != TRUE ||
2518 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2519 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2520 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2521 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2523 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2524 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2525 keyCount = ip.Event.KeyEvent.wRepeatCount;
2527 if (ip.Event.KeyEvent.dwControlKeyState &
2528 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2530 switch (currentKey.scan)
2532 case PCK_ALT_E: /* letter 'E' */
2533 currentKey.ascii = 0;
2536 } else if (ip.Event.KeyEvent.dwControlKeyState &
2537 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2539 switch (currentKey.scan)
2541 case PCK_RIGHT: /* right arrow */
2542 currentKey.scan = PCK_CTL_RIGHT;
2544 case PCK_LEFT: /* left arrow */
2545 currentKey.scan = PCK_CTL_LEFT;
2547 case PCK_DELETE: /* delete */
2548 currentKey.scan = PCK_CTL_DELETE;
2551 } else if (ip.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
2553 switch (currentKey.scan)
2555 case PCK_SHIFT_TAB: /* tab */
2556 currentKey.ascii = 0;
2565 * Read a character from the keyboard.
2568 WIN32getch(VOID_PARAM)
2572 if (pending_scancode)
2574 pending_scancode = 0;
2575 return ((char)(currentKey.scan & 0x00FF));
2579 while (win32_kbhit() == FALSE)
2587 ascii = currentKey.ascii;
2589 * On PC's, the extended keys return a 2 byte sequence beginning
2590 * with '00', so if the ascii code is 00, the next byte will be
2591 * the lsb of the scan code.
2593 pending_scancode = (ascii == 0x00);
2594 } while (pending_scancode &&
2595 (currentKey.scan == PCK_CAPS_LOCK || currentKey.scan == PCK_NUM_LOCK));
2597 return ((char)ascii);
2605 WIN32setcolors(fg, bg)
2615 WIN32textout(text, len)
2619 #if MSDOS_COMPILER==WIN32C
2624 * We've got UTF-8 text in a non-UTF-8 console. Convert it to
2625 * wide and use WriteConsoleW.
2628 len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext,
2629 sizeof(wtext)/sizeof(*wtext));
2630 WriteConsoleW(con_out, wtext, len, &written, NULL);
2632 WriteConsole(con_out, text, len, &written, NULL);