2 * Copyright (C) 1984-2021 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 dealing with getting input from the keyboard (i.e. from the user).
20 #if MSDOS_COMPILER==WIN32C
21 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_WINNT 0x400
26 public DWORD console_mode;
32 public char *ttyin_name = NULL;
33 public int rstat_file = -1;
37 extern int wheel_lines;
40 * Get name of tty device.
44 tty_device(VOID_PARAM)
53 if (ttyin_name != NULL)
58 #endif /* MSDOS_COMPILER */
61 * Open keyboard for input.
64 open_getchr(VOID_PARAM)
66 #if MSDOS_COMPILER==WIN32C
67 /* Need this to let child processes inherit our console handle */
68 SECURITY_ATTRIBUTES sa;
69 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
70 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
71 sa.bInheritHandle = TRUE;
72 tty = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
74 OPEN_EXISTING, 0L, NULL);
75 GetConsoleMode(tty, &console_mode);
76 /* Make sure we get Ctrl+C events. */
77 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
82 * Open a new handle to CON: in binary mode
83 * for unbuffered keyboard read.
87 tty = open("CON", OPEN_READ);
88 #if MSDOS_COMPILER==DJGPPC
90 * Setting stdin to binary causes Ctrl-C to not
91 * raise SIGINT. We must undo that side-effect.
93 (void) __djgpp_set_ctrl_c(1);
98 * If that doesn't work, use file descriptor 2,
99 * which in Unix is usually attached to the screen,
100 * but also usually lets you read from the keyboard.
103 /* The __open() system call translates "/dev/tty" to "con". */
104 tty = __open(tty_device(), OPEN_READ);
106 tty = open(tty_device(), OPEN_READ);
115 * Close the keyboard.
118 close_getchr(VOID_PARAM)
120 #if MSDOS_COMPILER==WIN32C
121 SetConsoleMode(tty, console_mode);
126 #if MSDOS_COMPILER==WIN32C
128 * Close the pipe, restoring the keyboard (CMD resets it, losing the mouse).
137 SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
143 * Get the number of lines to scroll when mouse wheel is moved.
146 default_wheel_lines(VOID_PARAM)
149 #if MSDOS_COMPILER==WIN32C
150 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &lines, 0))
152 if (lines == WHEEL_PAGESCROLL)
166 lseek(rstat_file, SEEK_SET, 0);
167 write(rstat_file, &st, 1);
172 * Get a character from the keyboard.
183 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
185 * In raw read, we don't see ^C so look here for it.
187 #if MSDOS_COMPILER==WIN32C
203 result = iread(tty, &uc, sizeof(char));
209 if (result == READ_INTR)
214 * Don't call error() here,
215 * because error calls getchr!
220 #if 0 /* allow entering arbitrary hex chars for testing */
221 /* ctrl-A followed by two hex chars makes a byte */
223 static int hex_in = 0;
224 static int hex_value = 0;
225 if (c == CONTROL('A'))
234 if (c >= '0' && c <= '9')
236 else if (c >= 'a' && c <= 'f')
238 else if (c >= 'A' && c <= 'F')
242 hex_value = (hex_value << 4) | v;
253 * Various parts of the program cannot handle
254 * an input character of '\0'.
255 * If a '\0' was actually typed, convert it to '\340' here.
259 } while (result != 1);