2 * Copyright (C) 1984-2019 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();
223 static void tmodes();
227 * These two variables are sometimes defined in,
228 * and needed by, the termcap library.
230 #if MUST_DEFINE_OSPEED
231 extern short ospeed; /* Terminal output baud rate */
232 extern char PC; /* Pad character */
239 extern int quiet; /* If VERY_QUIET, use visual bell for bell */
240 extern int no_back_scroll;
243 extern int no_keypad;
246 extern int screen_trashed;
247 extern int top_scroll;
248 extern int quit_if_one_screen;
252 extern int hilite_search;
254 #if MSDOS_COMPILER==WIN32C
260 extern char *tgetstr();
261 extern char *tgoto();
265 * Change terminal to "raw mode", or restore to "normal" mode.
267 * 1. An outstanding read will complete on receipt of a single keystroke.
268 * 2. Input is not echoed.
269 * 3. On output, \n is mapped to \r\n.
270 * 4. \t is NOT expanded into spaces.
271 * 5. Signal-causing characters such as ctrl-C (interrupt),
272 * etc. are NOT disabled.
273 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
279 static int curr_on = 0;
283 erase2_char = '\b'; /* in case OS doesn't know about erase2 */
284 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
287 static struct termios save_term;
288 static int saved_term = 0;
293 * Get terminal modes.
298 * Save modes and set certain variables dependent on modes.
306 switch (cfgetospeed(&s))
309 case B0: ospeed = 0; break;
312 case B50: ospeed = 1; break;
315 case B75: ospeed = 2; break;
318 case B110: ospeed = 3; break;
321 case B134: ospeed = 4; break;
324 case B150: ospeed = 5; break;
327 case B200: ospeed = 6; break;
330 case B300: ospeed = 7; break;
333 case B600: ospeed = 8; break;
336 case B1200: ospeed = 9; break;
339 case B1800: ospeed = 10; break;
342 case B2400: ospeed = 11; break;
345 case B4800: ospeed = 12; break;
348 case B9600: ospeed = 13; break;
351 case EXTA: ospeed = 14; break;
354 case EXTB: ospeed = 15; break;
357 case B57600: ospeed = 16; break;
360 case B115200: ospeed = 17; break;
365 erase_char = s.c_cc[VERASE];
367 erase2_char = s.c_cc[VERASE2];
369 kill_char = s.c_cc[VKILL];
371 werase_char = s.c_cc[VWERASE];
373 werase_char = CONTROL('W');
377 * Set the modes to the way we want them.
439 #if MUST_SET_LINE_DISCIPLINE
441 * System's termios is broken; need to explicitly
442 * request TERMIODISC line discipline.
444 s.c_line = TERMIODISC;
449 * Restore saved modes.
456 tcsetattr(tty, TCSADRAIN, &s);
457 #if MUST_SET_LINE_DISCIPLINE
461 * Broken termios *ignores* any line discipline
462 * except TERMIODISC. A different old line discipline
463 * is therefore not restored, yet. Restore the old
464 * line discipline by hand.
466 ioctl(tty, TIOCSETD, &save_term.c_line);
474 static struct termio save_term;
475 static int saved_term = 0;
480 * Get terminal modes.
482 ioctl(tty, TCGETA, &s);
485 * Save modes and set certain variables dependent on modes.
493 ospeed = s.c_cflag & CBAUD;
495 erase_char = s.c_cc[VERASE];
496 kill_char = s.c_cc[VKILL];
498 werase_char = s.c_cc[VWERASE];
500 werase_char = CONTROL('W');
504 * Set the modes to the way we want them.
506 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
507 s.c_oflag |= (OPOST|ONLCR|TAB3);
508 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
514 * Restore saved modes.
518 ioctl(tty, TCSETAW, &s);
524 static struct sgttyb save_term;
525 static int saved_term = 0;
530 * Get terminal modes.
532 ioctl(tty, TIOCGETP, &s);
535 * Save modes and set certain variables dependent on modes.
543 ospeed = s.sg_ospeed;
545 erase_char = s.sg_erase;
546 kill_char = s.sg_kill;
547 werase_char = CONTROL('W');
550 * Set the modes to the way we want them.
552 s.sg_flags |= CBREAK;
553 s.sg_flags &= ~(ECHO|XTABS);
557 * Restore saved modes.
561 ioctl(tty, TIOCSETN, &s);
567 static struct sgbuf save_term;
568 static int saved_term = 0;
573 * Get terminal modes.
578 * Save modes and set certain variables dependent on modes.
585 erase_char = s.sg_bspch;
586 kill_char = s.sg_dlnch;
587 werase_char = CONTROL('W');
590 * Set the modes to the way we want them.
599 * Restore saved modes.
606 /* MS-DOS, Windows, or OS2 */
609 LSIGNAL(SIGINT, SIG_IGN);
612 #if MSDOS_COMPILER==DJGPPC
613 kill_char = CONTROL('U');
615 * So that when we shell out or run another program, its
616 * stdin is in cooked mode. We do not switch stdin to binary
617 * mode if fd0 is zero, since that means we were called before
618 * tty was reopened in open_getchr, in which case we would be
619 * changing the original stdin device outside less.
622 setmode(0, on ? O_BINARY : O_TEXT);
626 werase_char = CONTROL('W');
636 * Some glue to prevent calling termcap functions if tgetent() failed.
649 struct env { struct env *next; char *name; char *value; };
650 static struct env *envs = NULL;
652 for (p = envs; p != NULL; p = p->next)
653 if (strcmp(p->name, capname) == 0)
655 p = (struct env *) ecalloc(1, sizeof(struct env));
656 p->name = save(capname);
657 p->value = (char *) ecalloc(strlen(capname)+3, sizeof(char));
658 sprintf(p->value, "<%s>", capname);
663 SNPRINTF1(name, sizeof(name), "LESS_TERMCAP_%s", capname);
664 return (lgetenv(name));
673 if ((s = ltget_env(capname)) != NULL)
674 return (*s != '\0' && *s != '0');
677 return (tgetflag(capname));
686 if ((s = ltget_env(capname)) != NULL)
690 return (tgetnum(capname));
694 ltgetstr(capname, pp)
700 if ((s = ltget_env(capname)) != NULL)
704 return (tgetstr(capname, pp));
706 #endif /* MSDOS_COMPILER */
709 * Get size of the output screen.
721 #define DEF_SC_WIDTH 80
723 #define DEF_SC_HEIGHT 25
725 #define DEF_SC_HEIGHT 24
729 sys_width = sys_height = 0;
731 #if MSDOS_COMPILER==MSOFTC
733 struct videoconfig w;
735 sys_height = w.numtextrows;
736 sys_width = w.numtextcols;
739 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
743 sys_height = w.screenheight;
744 sys_width = w.screenwidth;
747 #if MSDOS_COMPILER==WIN32C
749 CONSOLE_SCREEN_BUFFER_INFO scr;
750 GetConsoleScreenBufferInfo(con_out, &scr);
751 sys_height = scr.srWindow.Bottom - scr.srWindow.Top + 1;
752 sys_width = scr.srWindow.Right - scr.srWindow.Left + 1;
762 * When using terminal emulators for XFree86/OS2, the
763 * _scrsize function does not work well.
764 * Call the scrsize.exe program to get the window size.
766 windowid = getenv("WINDOWID");
767 if (windowid != NULL)
769 FILE *fd = popen("scrsize", "rt");
773 fscanf(fd, "%i %i", &w, &h);
787 if (ioctl(2, TIOCGWINSZ, &w) == 0)
790 sys_height = w.ws_row;
792 sys_width = w.ws_col;
799 if (ioctl(2, WIOCGETD, &w) == 0)
802 sys_height = w.uw_height / w.uw_vs;
804 sys_width = w.uw_width / w.uw_hs;
815 sc_height = sys_height;
816 else if ((s = lgetenv("LINES")) != NULL)
819 else if ((n = ltgetnum("li")) > 0)
823 sc_height = DEF_SC_HEIGHT;
826 sc_width = sys_width;
827 else if ((s = lgetenv("COLUMNS")) != NULL)
830 else if ((n = ltgetnum("co")) > 0)
834 sc_width = DEF_SC_WIDTH;
837 #if MSDOS_COMPILER==MSOFTC
839 * Figure out how many empty loops it takes to delay a millisecond.
842 get_clock(VOID_PARAM)
847 * Get synchronized at the start of a tick.
850 while (clock() == start)
853 * Now count loops till the next tick.
857 while (clock() == start)
860 * Convert from (loops per clock) to (loops per millisecond).
862 msec_loops *= CLOCKS_PER_SEC;
867 * Delay for a specified number of milliseconds.
877 for (i = 0; i < msec_loops; i++)
884 * Return the characters actually input by a "special" key.
890 static char tbuf[40];
892 #if MSDOS_COMPILER || OS2
893 static char k_right[] = { '\340', PCK_RIGHT, 0 };
894 static char k_left[] = { '\340', PCK_LEFT, 0 };
895 static char k_ctl_right[] = { '\340', PCK_CTL_RIGHT, 0 };
896 static char k_ctl_left[] = { '\340', PCK_CTL_LEFT, 0 };
897 static char k_insert[] = { '\340', PCK_INSERT, 0 };
898 static char k_delete[] = { '\340', PCK_DELETE, 0 };
899 static char k_ctl_delete[] = { '\340', PCK_CTL_DELETE, 0 };
900 static char k_ctl_backspace[] = { '\177', 0 };
901 static char k_home[] = { '\340', PCK_HOME, 0 };
902 static char k_end[] = { '\340', PCK_END, 0 };
903 static char k_up[] = { '\340', PCK_UP, 0 };
904 static char k_down[] = { '\340', PCK_DOWN, 0 };
905 static char k_backtab[] = { '\340', PCK_SHIFT_TAB, 0 };
906 static char k_pagedown[] = { '\340', PCK_PAGEDOWN, 0 };
907 static char k_pageup[] = { '\340', PCK_PAGEUP, 0 };
908 static char k_f1[] = { '\340', PCK_F1, 0 };
918 * If windowid is not NULL, assume less is executed in
919 * the XFree86 environment.
922 s = windowid ? ltgetstr("kr", &sp) : k_right;
925 s = windowid ? ltgetstr("kl", &sp) : k_left;
928 s = windowid ? ltgetstr("ku", &sp) : k_up;
931 s = windowid ? ltgetstr("kd", &sp) : k_down;
934 s = windowid ? ltgetstr("kP", &sp) : k_pageup;
937 s = windowid ? ltgetstr("kN", &sp) : k_pagedown;
940 s = windowid ? ltgetstr("kh", &sp) : k_home;
943 s = windowid ? ltgetstr("@7", &sp) : k_end;
946 s = windowid ? ltgetstr("kD", &sp) : k_delete;
984 #if MSDOS_COMPILER || OS2
988 case SK_CTL_LEFT_ARROW:
991 case SK_CTL_RIGHT_ARROW:
994 case SK_CTL_BACKSPACE:
1007 case SK_RIGHT_ARROW:
1008 s = ltgetstr("kr", &sp);
1011 s = ltgetstr("kl", &sp);
1014 s = ltgetstr("ku", &sp);
1017 s = ltgetstr("kd", &sp);
1020 s = ltgetstr("kP", &sp);
1023 s = ltgetstr("kN", &sp);
1026 s = ltgetstr("kh", &sp);
1029 s = ltgetstr("@7", &sp);
1032 s = ltgetstr("kD", &sp);
1042 tbuf[0] = CONTROL('K');
1053 * Get terminal capabilities via termcap.
1056 get_term(VOID_PARAM)
1058 termcap_debug = !isnullenv(lgetenv("LESS_TERMCAP_DEBUG"));
1065 * Set up default colors.
1066 * The xx_s_width and xx_e_width vars are already initialized to 0.
1068 #if MSDOS_COMPILER==MSOFTC
1069 sy_bg_color = _getbkcolor();
1070 sy_fg_color = _gettextcolor();
1073 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1077 sy_bg_color = (w.attribute >> 4) & 0x0F;
1078 sy_fg_color = (w.attribute >> 0) & 0x0F;
1081 #if MSDOS_COMPILER==WIN32C
1084 CONSOLE_SCREEN_BUFFER_INFO scr;
1086 con_out_save = con_out = GetStdHandle(STD_OUTPUT_HANDLE);
1088 * Always open stdin in binary. Note this *must* be done
1089 * before any file operations have been done on fd0.
1092 GetConsoleScreenBufferInfo(con_out, &scr);
1093 ReadConsoleOutputAttribute(con_out, &curr_attr,
1094 1, scr.dwCursorPosition, &nread);
1095 sy_bg_color = (curr_attr & BG_COLORS) >> 4; /* normalize */
1096 sy_fg_color = curr_attr & FG_COLORS;
1101 nm_fg_color = sy_fg_color;
1102 nm_bg_color = sy_bg_color;
1114 * Get size of the screen.
1120 #else /* !MSDOS_COMPILER */
1125 char termbuf[TERMBUF_SIZE];
1127 static char sbuf[TERMSBUF_SIZE];
1131 * Make sure the termcap database is available.
1133 sp = lgetenv("TERMCAP");
1137 if ((sp = homefile("termcap.dat")) != NULL)
1139 termcap = (char *) ecalloc(strlen(sp)+9, sizeof(char));
1140 sprintf(termcap, "TERMCAP=%s", sp);
1147 * Find out what kind of terminal this is.
1149 if ((term = lgetenv("TERM")) == NULL)
1150 term = DEFAULT_TERM;
1152 if (tgetent(termbuf, term) != TGETENT_OK)
1154 if (ltgetflag("hc"))
1158 * Get size of the screen.
1163 auto_wrap = ltgetflag("am");
1164 ignaw = ltgetflag("xn");
1165 above_mem = ltgetflag("da");
1166 below_mem = ltgetflag("db");
1167 clear_bg = ltgetflag("ut");
1170 * Assumes termcap variable "sg" is the printing width of:
1171 * the standout sequence, the end standout sequence,
1172 * the underline sequence, the end underline sequence,
1173 * the boldface sequence, and the end boldface sequence.
1175 if ((so_s_width = ltgetnum("sg")) < 0)
1177 so_e_width = so_s_width;
1179 bo_s_width = bo_e_width = so_s_width;
1180 ul_s_width = ul_e_width = so_s_width;
1181 bl_s_width = bl_e_width = so_s_width;
1184 if (so_s_width > 0 || so_e_width > 0)
1186 * Disable highlighting by default on magic cookie terminals.
1187 * Turning on highlighting might change the displayed width
1188 * of a line, causing the display to get messed up.
1189 * The user can turn it back on with -g,
1190 * but she won't like the results.
1196 * Get various string-valued capabilities.
1201 sc_pad = ltgetstr("pc", &sp);
1206 sc_s_keypad = ltgetstr("ks", &sp);
1207 if (sc_s_keypad == NULL)
1209 sc_e_keypad = ltgetstr("ke", &sp);
1210 if (sc_e_keypad == NULL)
1212 kent = ltgetstr("@8", &sp);
1214 sc_s_mousecap = ltgetstr("MOUSE_START", &sp);
1215 if (sc_s_mousecap == NULL)
1216 sc_s_mousecap = ESCS "[?1000h" ESCS "[?1006h";
1217 sc_e_mousecap = ltgetstr("MOUSE_END", &sp);
1218 if (sc_e_mousecap == NULL)
1219 sc_e_mousecap = ESCS "[?1006l" ESCS "[?1000l";
1221 sc_init = ltgetstr("ti", &sp);
1222 if (sc_init == NULL)
1225 sc_deinit= ltgetstr("te", &sp);
1226 if (sc_deinit == NULL)
1229 sc_eol_clear = ltgetstr("ce", &sp);
1230 if (sc_eol_clear == NULL || *sc_eol_clear == '\0')
1236 sc_eos_clear = ltgetstr("cd", &sp);
1237 if (below_mem && (sc_eos_clear == NULL || *sc_eos_clear == '\0'))
1243 sc_clear = ltgetstr("cl", &sp);
1244 if (sc_clear == NULL || *sc_clear == '\0')
1250 sc_move = ltgetstr("cm", &sp);
1251 if (sc_move == NULL || *sc_move == '\0')
1254 * This is not an error here, because we don't
1255 * always need sc_move.
1256 * We need it only if we don't have home or lower-left.
1263 tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
1264 tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
1265 tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
1266 tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);
1268 sc_visual_bell = ltgetstr("vb", &sp);
1269 if (sc_visual_bell == NULL)
1270 sc_visual_bell = "";
1272 if (ltgetflag("bs"))
1273 sc_backspace = "\b";
1276 sc_backspace = ltgetstr("bc", &sp);
1277 if (sc_backspace == NULL || *sc_backspace == '\0')
1278 sc_backspace = "\b";
1282 * Choose between using "ho" and "cm" ("home" and "cursor move")
1283 * to move the cursor to the upper left corner of the screen.
1285 t1 = ltgetstr("ho", &sp);
1288 if (*sc_move == '\0')
1292 strcpy(sp, tgoto(sc_move, 0, 0));
1294 sp += strlen(sp) + 1;
1296 sc_home = cheaper(t1, t2, "|\b^");
1299 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
1300 * to move the cursor to the lower left corner of the screen.
1302 t1 = ltgetstr("ll", &sp);
1305 if (*sc_move == '\0')
1309 strcpy(sp, tgoto(sc_move, 0, sc_height-1));
1311 sp += strlen(sp) + 1;
1313 sc_lower_left = cheaper(t1, t2, "\r");
1316 * Get carriage return string.
1318 sc_return = ltgetstr("cr", &sp);
1319 if (sc_return == NULL)
1323 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
1324 * to add a line at the top of the screen.
1326 t1 = ltgetstr("al", &sp);
1329 t2 = ltgetstr("sr", &sp);
1333 if (*t1 == '\0' && *t2 == '\0')
1340 sc_addline = cheaper(t1, t2, "");
1341 if (*sc_addline == '\0')
1344 * Force repaint on any backward movement.
1349 #endif /* MSDOS_COMPILER */
1354 * Return the cost of displaying a termcap string.
1355 * We use the trick of calling tputs, but as a char printing function
1356 * we give it inc_costcount, which just increments "costcount".
1357 * This tells us how many chars would be printed by using this string.
1358 * {{ Couldn't we just use strlen? }}
1360 static int costcount;
1376 tputs(t, sc_height, inc_costcount);
1381 * Return the "best" of the two given termcap strings.
1382 * The best, if both exist, is the one with the lower
1383 * cost (see cost() function).
1386 cheaper(t1, t2, def)
1390 if (*t1 == '\0' && *t2 == '\0')
1399 if (cost(t1) < cost(t2))
1405 tmodes(incap, outcap, instr, outstr, def_instr, def_outstr, spp)
1414 *instr = ltgetstr(incap, spp);
1419 *outstr = def_outstr;
1423 *outstr = ltgetstr(outcap, spp);
1424 if (*outstr == NULL)
1425 /* No specific out capability; use "me". */
1426 *outstr = ltgetstr("me", spp);
1427 if (*outstr == NULL)
1428 /* Don't even have "me"; use a null string. */
1432 #endif /* MSDOS_COMPILER */
1436 * Below are the functions which perform all the
1437 * terminal-specific screen manipulation.
1443 #if MSDOS_COMPILER==WIN32C
1445 _settextposition(int row, int col)
1448 CONSOLE_SCREEN_BUFFER_INFO csbi;
1450 GetConsoleScreenBufferInfo(con_out, &csbi);
1451 cpos.X = csbi.srWindow.Left + (col - 1);
1452 cpos.Y = csbi.srWindow.Top + (row - 1);
1453 SetConsoleCursorPosition(con_out, cpos);
1458 * Initialize the screen to the correct color at startup.
1461 initcolor(VOID_PARAM)
1463 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1466 SETCOLORS(nm_fg_color, nm_bg_color);
1469 * This clears the screen at startup. This is different from
1470 * the behavior of other versions of less. Disable it for now.
1477 * Create a complete, blank screen using "normal" colors.
1479 SETCOLORS(nm_fg_color, nm_bg_color);
1480 blanks = (char *) ecalloc(width+1, sizeof(char));
1481 for (col = 0; col < sc_width; col++)
1483 blanks[sc_width] = '\0';
1484 for (row = 0; row < sc_height; row++)
1491 #if MSDOS_COMPILER==WIN32C
1494 * Termcap-like init with a private win32 console.
1497 win32_init_term(VOID_PARAM)
1499 CONSOLE_SCREEN_BUFFER_INFO scr;
1502 if (con_out_save == INVALID_HANDLE_VALUE)
1505 GetConsoleScreenBufferInfo(con_out_save, &scr);
1507 if (con_out_ours == INVALID_HANDLE_VALUE)
1512 * Create our own screen buffer, so that we
1513 * may restore the original when done.
1515 con_out_ours = CreateConsoleScreenBuffer(
1516 GENERIC_WRITE | GENERIC_READ,
1517 FILE_SHARE_WRITE | FILE_SHARE_READ,
1518 (LPSECURITY_ATTRIBUTES) NULL,
1519 CONSOLE_TEXTMODE_BUFFER,
1522 * Enable underline, if available.
1524 GetConsoleMode(con_out_ours, &output_mode);
1525 have_ul = SetConsoleMode(con_out_ours,
1526 output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1529 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1530 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1531 SetConsoleScreenBufferSize(con_out_ours, size);
1532 SetConsoleActiveScreenBuffer(con_out_ours);
1533 con_out = con_out_ours;
1537 * Restore the startup console.
1540 win32_deinit_term(VOID_PARAM)
1542 if (con_out_save == INVALID_HANDLE_VALUE)
1545 (void) CloseHandle(con_out_ours);
1546 SetConsoleActiveScreenBuffer(con_out_save);
1547 con_out = con_out_save;
1553 * Configure the termimal so mouse clicks and wheel moves
1554 * produce input to less.
1557 init_mouse(VOID_PARAM)
1562 tputs(sc_s_mousecap, sc_height, putchr);
1564 #if MSDOS_COMPILER==WIN32C
1565 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
1571 * Configure the terminal so mouse clicks and wheel moves
1572 * are handled by the system (so text can be selected, etc).
1575 deinit_mouse(VOID_PARAM)
1580 tputs(sc_e_mousecap, sc_height, putchr);
1582 #if MSDOS_COMPILER==WIN32C
1583 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT);
1589 * Initialize terminal
1595 if (!(quit_if_one_screen && one_screen))
1598 tputs(sc_init, sc_height, putchr);
1600 tputs(sc_s_keypad, sc_height, putchr);
1608 * This is nice to terminals with no alternate screen,
1609 * but with saved scrolled-off-the-top lines. This way,
1610 * no previous line is lost, but we start with a whole
1611 * screen to ourself.
1613 for (i = 1; i < sc_height; i++)
1618 #if MSDOS_COMPILER==WIN32C
1629 * Deinitialize terminal
1637 if (!(quit_if_one_screen && one_screen))
1641 tputs(sc_e_keypad, sc_height, putchr);
1643 tputs(sc_deinit, sc_height, putchr);
1646 /* Restore system colors. */
1647 SETCOLORS(sy_fg_color, sy_bg_color);
1648 #if MSDOS_COMPILER==WIN32C
1650 win32_deinit_term();
1652 /* Need clreol to make SETCOLORS take effect. */
1660 * Home cursor (move to upper left corner of screen).
1666 tputs(sc_home, 1, putchr);
1669 _settextposition(1,1);
1674 * Add a blank line (called with cursor at home).
1675 * Should scroll the display down.
1678 add_line(VOID_PARAM)
1681 tputs(sc_addline, sc_height, putchr);
1684 #if MSDOS_COMPILER==MSOFTC
1685 _scrolltextwindow(_GSCROLLDOWN);
1686 _settextposition(1,1);
1688 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1689 movetext(1,1, sc_width,sc_height-1, 1,2);
1693 #if MSDOS_COMPILER==WIN32C
1696 SMALL_RECT rcSrc, rcClip;
1698 CONSOLE_SCREEN_BUFFER_INFO csbi;
1700 GetConsoleScreenBufferInfo(con_out,&csbi);
1702 /* The clip rectangle is the entire visible screen. */
1703 rcClip.Left = csbi.srWindow.Left;
1704 rcClip.Top = csbi.srWindow.Top;
1705 rcClip.Right = csbi.srWindow.Right;
1706 rcClip.Bottom = csbi.srWindow.Bottom;
1708 /* The source rectangle is the visible screen minus the last line. */
1712 /* Move the top left corner of the source window down one row. */
1713 new_org.X = rcSrc.Left;
1714 new_org.Y = rcSrc.Top + 1;
1716 /* Fill the right character and attributes. */
1717 fillchar.Char.AsciiChar = ' ';
1718 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1719 fillchar.Attributes = curr_attr;
1720 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1721 _settextposition(1,1);
1731 * Remove the n topmost lines and scroll everything below it in the
1732 * window upward. This is needed to stop leaking the topmost line
1733 * into the scrollback buffer when we go down-one-line (in WIN32).
1739 #if MSDOS_COMPILER==WIN32C
1740 SMALL_RECT rcSrc, rcClip;
1743 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
1745 if (n >= sc_height - 1)
1754 GetConsoleScreenBufferInfo(con_out, &csbi);
1756 /* Get the extent of all-visible-rows-but-the-last. */
1757 rcSrc.Left = csbi.srWindow.Left;
1758 rcSrc.Top = csbi.srWindow.Top + n;
1759 rcSrc.Right = csbi.srWindow.Right;
1760 rcSrc.Bottom = csbi.srWindow.Bottom;
1762 /* Get the clip rectangle. */
1763 rcClip.Left = rcSrc.Left;
1764 rcClip.Top = csbi.srWindow.Top;
1765 rcClip.Right = rcSrc.Right;
1766 rcClip.Bottom = rcSrc.Bottom ;
1768 /* Move the source window up n rows. */
1769 new_org.X = rcSrc.Left;
1770 new_org.Y = rcSrc.Top - n;
1772 /* Fill the right character and attributes. */
1773 fillchar.Char.AsciiChar = ' ';
1774 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1775 fillchar.Attributes = curr_attr;
1777 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1779 /* Position cursor on first blank line. */
1780 goto_line(sc_height - n - 1);
1785 #if MSDOS_COMPILER==WIN32C
1790 win32_clear(VOID_PARAM)
1793 * This will clear only the currently visible rows of the NT
1794 * console buffer, which means none of the precious scrollback
1795 * rows are touched making for faster scrolling. Note that, if
1796 * the window has fewer columns than the console buffer (i.e.
1797 * there is a horizontal scrollbar as well), the entire width
1798 * of the visible rows will be cleared.
1803 CONSOLE_SCREEN_BUFFER_INFO csbi;
1805 /* get the number of cells in the current buffer */
1806 GetConsoleScreenBufferInfo(con_out, &csbi);
1807 winsz = csbi.dwSize.X * (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
1809 topleft.Y = csbi.srWindow.Top;
1811 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
1812 FillConsoleOutputCharacter(con_out, ' ', winsz, topleft, &nchars);
1813 FillConsoleOutputAttribute(con_out, curr_attr, winsz, topleft, &nchars);
1817 * Remove the n topmost lines and scroll everything below it in the
1824 SMALL_RECT rcSrc, rcClip;
1830 CONSOLE_SCREEN_BUFFER_INFO csbi;
1835 if (n >= sc_height - 1)
1838 _settextposition(1,1);
1842 /* Get the extent of what will remain visible after scrolling. */
1843 GetConsoleScreenBufferInfo(con_out, &csbi);
1844 rcSrc.Left = csbi.srWindow.Left;
1845 rcSrc.Top = csbi.srWindow.Top + n;
1846 rcSrc.Right = csbi.srWindow.Right;
1847 rcSrc.Bottom = csbi.srWindow.Bottom;
1849 /* Get the clip rectangle. */
1850 rcClip.Left = rcSrc.Left;
1851 rcClip.Top = csbi.srWindow.Top;
1852 rcClip.Right = rcSrc.Right;
1853 rcClip.Bottom = rcSrc.Bottom ;
1855 /* Move the source text to the top of the screen. */
1856 new_org.X = rcSrc.Left;
1857 new_org.Y = rcClip.Top;
1859 /* Fill the right character and attributes. */
1860 fillchar.Char.AsciiChar = ' ';
1861 fillchar.Attributes = MAKEATTR(nm_fg_color, nm_bg_color);
1863 /* Scroll the window. */
1864 SetConsoleTextAttribute(con_out, fillchar.Attributes);
1865 ScrollConsoleScreenBuffer(con_out, &rcSrc, &rcClip, new_org, &fillchar);
1867 /* Clear remaining lines at bottom. */
1868 topleft.X = csbi.dwCursorPosition.X;
1869 topleft.Y = rcSrc.Bottom - n;
1870 size = (n * csbi.dwSize.X) + (rcSrc.Right - topleft.X);
1871 FillConsoleOutputCharacter(con_out, ' ', size, topleft,
1873 FillConsoleOutputAttribute(con_out, fillchar.Attributes, size, topleft,
1875 SetConsoleTextAttribute(con_out, curr_attr);
1877 /* Move cursor n lines up from where it was. */
1878 csbi.dwCursorPosition.Y -= n;
1879 SetConsoleCursorPosition(con_out, csbi.dwCursorPosition);
1884 * Move cursor to lower left corner of screen.
1887 lower_left(VOID_PARAM)
1892 tputs(sc_lower_left, 1, putchr);
1895 _settextposition(sc_height, 1);
1900 * Move cursor to left position of current line.
1903 line_left(VOID_PARAM)
1906 tputs(sc_return, 1, putchr);
1910 #if MSDOS_COMPILER==WIN32C
1912 CONSOLE_SCREEN_BUFFER_INFO scr;
1913 GetConsoleScreenBufferInfo(con_out, &scr);
1914 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
1917 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
1921 struct rccoord tpos = _gettextposition();
1926 _settextposition(row, 1);
1931 * Check if the console size has changed and reset internals
1932 * (in lieu of SIGWINCH for WIN32).
1935 check_winch(VOID_PARAM)
1937 #if MSDOS_COMPILER==WIN32C
1938 CONSOLE_SCREEN_BUFFER_INFO scr;
1941 if (con_out == INVALID_HANDLE_VALUE)
1945 GetConsoleScreenBufferInfo(con_out, &scr);
1946 size.Y = scr.srWindow.Bottom - scr.srWindow.Top + 1;
1947 size.X = scr.srWindow.Right - scr.srWindow.Left + 1;
1948 if (size.Y != sc_height || size.X != sc_width)
1952 if (!no_init && con_out_ours == con_out)
1953 SetConsoleScreenBufferSize(con_out, size);
1955 wscroll = (sc_height + 1) / 2;
1962 * Goto a specific line on the screen.
1969 tputs(tgoto(sc_move, 0, sindex), 1, putchr);
1972 _settextposition(sindex+1, 1);
1976 #if MSDOS_COMPILER==MSOFTC || MSDOS_COMPILER==BORLANDC
1978 * Create an alternate screen which is all white.
1979 * This screen is used to create a "flash" effect, by displaying it
1980 * briefly and then switching back to the normal screen.
1981 * {{ Yuck! There must be a better way to get a visual bell. }}
1984 create_flash(VOID_PARAM)
1986 #if MSDOS_COMPILER==MSOFTC
1987 struct videoconfig w;
1991 _getvideoconfig(&w);
1992 videopages = w.numvideopages;
1995 at_enter(AT_STANDOUT);
2000 at_enter(AT_STANDOUT);
2001 blanks = (char *) ecalloc(w.numtextcols, sizeof(char));
2002 for (col = 0; col < w.numtextcols; col++)
2004 for (row = w.numtextrows; row > 0; row--)
2005 _outmem(blanks, w.numtextcols);
2012 #if MSDOS_COMPILER==BORLANDC
2015 whitescreen = (unsigned short *)
2016 malloc(sc_width * sc_height * sizeof(short));
2017 if (whitescreen == NULL)
2019 for (n = 0; n < sc_width * sc_height; n++)
2020 whitescreen[n] = 0x7020;
2025 #endif /* MSDOS_COMPILER */
2028 * Output the "visual bell", if there is one.
2034 if (*sc_visual_bell == '\0')
2036 tputs(sc_visual_bell, sc_height, putchr);
2038 #if MSDOS_COMPILER==DJGPPC
2041 #if MSDOS_COMPILER==MSOFTC
2043 * Create a flash screen on the second video page.
2044 * Switch to that page, then switch back.
2054 #if MSDOS_COMPILER==BORLANDC
2055 unsigned short *currscreen;
2058 * Get a copy of the current screen.
2059 * Display the flash screen.
2060 * Then restore the old screen.
2064 if (whitescreen == NULL)
2066 currscreen = (unsigned short *)
2067 malloc(sc_width * sc_height * sizeof(short));
2068 if (currscreen == NULL) return;
2069 gettext(1, 1, sc_width, sc_height, currscreen);
2070 puttext(1, 1, sc_width, sc_height, whitescreen);
2072 puttext(1, 1, sc_width, sc_height, currscreen);
2075 #if MSDOS_COMPILER==WIN32C
2076 /* paint screen with an inverse color */
2079 /* leave it displayed for 100 msec. */
2082 /* restore with a redraw */
2098 putchr(CONTROL('G'));
2100 #if MSDOS_COMPILER==WIN32C
2109 * Ring the terminal bell.
2114 if (quiet == VERY_QUIET)
2127 tputs(sc_clear, sc_height, putchr);
2130 #if MSDOS_COMPILER==WIN32C
2133 _clearscreen(_GCLEARSCREEN);
2139 * Clear from the cursor to the end of the cursor's line.
2140 * {{ This must not move the cursor. }}
2143 clear_eol(VOID_PARAM)
2146 tputs(sc_eol_clear, 1, putchr);
2148 #if MSDOS_COMPILER==MSOFTC
2151 struct rccoord tpos;
2155 * Save current state.
2157 tpos = _gettextposition();
2158 _gettextwindow(&top, &left, &bot, &right);
2160 * Set a temporary window to the current line,
2161 * from the cursor's position to the right edge of the screen.
2162 * Then clear that window.
2164 _settextwindow(tpos.row, tpos.col, tpos.row, sc_width);
2165 _clearscreen(_GWINDOW);
2169 _settextwindow(top, left, bot, right);
2170 _settextposition(tpos.row, tpos.col);
2172 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2176 #if MSDOS_COMPILER==WIN32C
2179 CONSOLE_SCREEN_BUFFER_INFO scr;
2182 memset(&scr, 0, sizeof(scr));
2183 GetConsoleScreenBufferInfo(con_out, &scr);
2184 cpos.X = scr.dwCursorPosition.X;
2185 cpos.Y = scr.dwCursorPosition.Y;
2186 curr_attr = MAKEATTR(nm_fg_color, nm_bg_color);
2187 FillConsoleOutputAttribute(con_out, curr_attr,
2188 scr.dwSize.X - cpos.X, cpos, &nchars);
2189 FillConsoleOutputCharacter(con_out, ' ',
2190 scr.dwSize.X - cpos.X, cpos, &nchars);
2198 * Clear the current line.
2199 * Clear the screen if there's off-screen memory below the display.
2202 clear_eol_bot(VOID_PARAM)
2208 tputs(sc_eos_clear, 1, putchr);
2210 tputs(sc_eol_clear, 1, putchr);
2215 * Clear the bottom line of the display.
2216 * Leave the cursor at the beginning of the bottom line.
2219 clear_bot(VOID_PARAM)
2222 * If we're in a non-normal attribute mode, temporarily exit
2223 * the mode while we do the clear. Some terminals fill the
2224 * cleared area with the current attribute.
2231 if (attrmode == AT_NORMAL)
2235 int saved_attrmode = attrmode;
2239 at_enter(saved_attrmode);
2247 attr = apply_at_specials(attr);
2250 /* The one with the most priority is last. */
2251 if (attr & AT_UNDERLINE)
2252 tputs(sc_u_in, 1, putchr);
2254 tputs(sc_b_in, 1, putchr);
2255 if (attr & AT_BLINK)
2256 tputs(sc_bl_in, 1, putchr);
2257 if (attr & AT_STANDOUT)
2258 tputs(sc_s_in, 1, putchr);
2261 /* The one with the most priority is first. */
2262 if (attr & AT_STANDOUT)
2264 SETCOLORS(so_fg_color, so_bg_color);
2265 } else if (attr & AT_BLINK)
2267 SETCOLORS(bl_fg_color, bl_bg_color);
2269 else if (attr & AT_BOLD)
2271 SETCOLORS(bo_fg_color, bo_bg_color);
2273 else if (attr & AT_UNDERLINE)
2275 SETCOLORS(ul_fg_color, ul_bg_color);
2286 /* Undo things in the reverse order we did them. */
2287 if (attrmode & AT_STANDOUT)
2288 tputs(sc_s_out, 1, putchr);
2289 if (attrmode & AT_BLINK)
2290 tputs(sc_bl_out, 1, putchr);
2291 if (attrmode & AT_BOLD)
2292 tputs(sc_b_out, 1, putchr);
2293 if (attrmode & AT_UNDERLINE)
2294 tputs(sc_u_out, 1, putchr);
2297 SETCOLORS(nm_fg_color, nm_bg_color);
2300 attrmode = AT_NORMAL;
2307 int new_attrmode = apply_at_specials(attr);
2308 int ignore_modes = AT_ANSI;
2310 if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes))
2318 is_at_equiv(attr1, attr2)
2322 attr1 = apply_at_specials(attr1);
2323 attr2 = apply_at_specials(attr2);
2325 return (attr1 == attr2);
2329 apply_at_specials(attr)
2332 if (attr & AT_BINARY)
2334 if (attr & AT_HILITE)
2335 attr |= AT_STANDOUT;
2336 attr &= ~(AT_BINARY|AT_HILITE);
2341 #if 0 /* No longer used */
2343 * Erase the character to the left of the cursor
2344 * and move the cursor left.
2347 backspace(VOID_PARAM)
2351 * Erase the previous character by overstriking with a space.
2353 tputs(sc_backspace, 1, putchr);
2355 tputs(sc_backspace, 1, putchr);
2357 #if MSDOS_COMPILER==MSOFTC
2358 struct rccoord tpos;
2361 tpos = _gettextposition();
2364 _settextposition(tpos.row, tpos.col-1);
2366 _settextposition(tpos.row, tpos.col-1);
2368 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2371 #if MSDOS_COMPILER==WIN32C
2374 CONSOLE_SCREEN_BUFFER_INFO scr;
2377 GetConsoleScreenBufferInfo(con_out, &scr);
2378 cpos = scr.dwCursorPosition;
2382 SetConsoleCursorPosition(con_out, cpos);
2383 FillConsoleOutputCharacter(con_out, (TCHAR)' ', 1, cpos, &cChars);
2384 SetConsoleCursorPosition(con_out, cpos);
2393 * Output a plain backspace, without erasing the previous char.
2403 tputs(sc_backspace, 1, putchr);
2409 #if MSDOS_COMPILER==MSOFTC
2410 struct rccoord tpos;
2411 tpos = _gettextposition();
2415 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
2419 #if MSDOS_COMPILER==WIN32C
2420 CONSOLE_SCREEN_BUFFER_INFO scr;
2421 GetConsoleScreenBufferInfo(con_out, &scr);
2422 row = scr.dwCursorPosition.Y - scr.srWindow.Top + 1;
2423 col = scr.dwCursorPosition.X - scr.srWindow.Left + 1;
2430 _settextposition(row, col-1);
2431 #endif /* MSDOS_COMPILER */
2435 #if MSDOS_COMPILER==WIN32C
2437 * Determine whether an input character is waiting to be read.
2440 win32_kbhit(VOID_PARAM)
2448 currentKey.ascii = 0;
2449 currentKey.scan = 0;
2451 if (x11mouseCount > 0)
2453 currentKey.ascii = x11mousebuf[x11mousePos++];
2460 * Wait for a real key-down event, but
2461 * ignore SHIFT and CONTROL key events.
2465 PeekConsoleInput(tty, &ip, 1, &read);
2468 ReadConsoleInput(tty, &ip, 1, &read);
2469 /* generate an X11 mouse sequence from the mouse event */
2470 if (mousecap && ip.EventType == MOUSE_EVENT &&
2471 ip.Event.MouseEvent.dwEventFlags != MOUSE_MOVED)
2473 x11mousebuf[3] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.X + 1;
2474 x11mousebuf[4] = X11MOUSE_OFFSET + ip.Event.MouseEvent.dwMousePosition.Y + 1;
2475 switch (ip.Event.MouseEvent.dwEventFlags)
2477 case 0: /* press or release */
2478 if (ip.Event.MouseEvent.dwButtonState == 0)
2479 x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON_REL;
2480 else if (ip.Event.MouseEvent.dwButtonState & (FROM_LEFT_3RD_BUTTON_PRESSED | FROM_LEFT_4TH_BUTTON_PRESSED))
2483 x11mousebuf[2] = X11MOUSE_OFFSET + X11MOUSE_BUTTON1 + ((int)ip.Event.MouseEvent.dwButtonState << 1);
2486 x11mousebuf[2] = X11MOUSE_OFFSET + (((int)ip.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP);
2493 currentKey.ascii = ESC;
2497 } while (ip.EventType != KEY_EVENT ||
2498 ip.Event.KeyEvent.bKeyDown != TRUE ||
2499 ip.Event.KeyEvent.wVirtualScanCode == 0 ||
2500 ip.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
2501 ip.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL ||
2502 ip.Event.KeyEvent.wVirtualKeyCode == VK_MENU);
2504 currentKey.ascii = ip.Event.KeyEvent.uChar.AsciiChar;
2505 currentKey.scan = ip.Event.KeyEvent.wVirtualScanCode;
2506 keyCount = ip.Event.KeyEvent.wRepeatCount;
2508 if (ip.Event.KeyEvent.dwControlKeyState &
2509 (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
2511 switch (currentKey.scan)
2513 case PCK_ALT_E: /* letter 'E' */
2514 currentKey.ascii = 0;
2517 } else if (ip.Event.KeyEvent.dwControlKeyState &
2518 (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
2520 switch (currentKey.scan)
2522 case PCK_RIGHT: /* right arrow */
2523 currentKey.scan = PCK_CTL_RIGHT;
2525 case PCK_LEFT: /* left arrow */
2526 currentKey.scan = PCK_CTL_LEFT;
2528 case PCK_DELETE: /* delete */
2529 currentKey.scan = PCK_CTL_DELETE;
2532 } else if (ip.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
2534 switch (currentKey.scan)
2536 case PCK_SHIFT_TAB: /* tab */
2537 currentKey.ascii = 0;
2546 * Read a character from the keyboard.
2549 WIN32getch(VOID_PARAM)
2553 if (pending_scancode)
2555 pending_scancode = 0;
2556 return ((char)(currentKey.scan & 0x00FF));
2560 while (win32_kbhit() == FALSE)
2568 ascii = currentKey.ascii;
2570 * On PC's, the extended keys return a 2 byte sequence beginning
2571 * with '00', so if the ascii code is 00, the next byte will be
2572 * the lsb of the scan code.
2574 pending_scancode = (ascii == 0x00);
2575 } while (pending_scancode &&
2576 (currentKey.scan == PCK_CAPS_LOCK || currentKey.scan == PCK_NUM_LOCK));
2578 return ((char)ascii);
2586 WIN32setcolors(fg, bg)
2596 WIN32textout(text, len)
2600 #if MSDOS_COMPILER==WIN32C
2605 * We've got UTF-8 text in a non-UTF-8 console. Convert it to
2606 * wide and use WriteConsoleW.
2609 len = MultiByteToWideChar(CP_UTF8, 0, text, len, wtext,
2610 sizeof(wtext)/sizeof(*wtext));
2611 WriteConsoleW(con_out, wtext, len, &written, NULL);
2613 WriteConsole(con_out, text, len, &written, NULL);