2 * Copyright (C) 1984-2022 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
;
45 /* The __open() system call translates "/dev/tty" to "con". */
46 return __open(dev
, OPEN_READ
);
48 return open(dev
, OPEN_READ
);
53 * Open the tty device.
54 * Try ttyname(), then try /dev/tty, then use file descriptor 2.
55 * In Unix, file descriptor 2 is usually attached to the screen,
56 * but also usually lets you read from the keyboard.
63 if (ttyin_name
!= NULL
)
64 fd
= open_tty_device(ttyin_name
);
69 constant
char *dev
= ttyname(2);
71 fd
= open_tty_device(dev
);
75 fd
= open_tty_device("/dev/tty");
80 #endif /* MSDOS_COMPILER */
83 * Open keyboard for input.
86 open_getchr(VOID_PARAM
)
88 #if MSDOS_COMPILER==WIN32C
89 /* Need this to let child processes inherit our console handle */
90 SECURITY_ATTRIBUTES sa
;
91 memset(&sa
, 0, sizeof(SECURITY_ATTRIBUTES
));
92 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
93 sa
.bInheritHandle
= TRUE
;
94 tty
= CreateFile("CONIN$", GENERIC_READ
| GENERIC_WRITE
,
96 OPEN_EXISTING
, 0L, NULL
);
97 GetConsoleMode(tty
, &console_mode
);
98 /* Make sure we get Ctrl+C events. */
99 SetConsoleMode(tty
, ENABLE_PROCESSED_INPUT
| ENABLE_MOUSE_INPUT
);
104 * Open a new handle to CON: in binary mode
105 * for unbuffered keyboard read.
109 tty
= open("CON", OPEN_READ
);
110 #if MSDOS_COMPILER==DJGPPC
112 * Setting stdin to binary causes Ctrl-C to not
113 * raise SIGINT. We must undo that side-effect.
115 (void) __djgpp_set_ctrl_c(1);
124 * Close the keyboard.
127 close_getchr(VOID_PARAM
)
129 #if MSDOS_COMPILER==WIN32C
130 SetConsoleMode(tty
, console_mode
);
135 #if MSDOS_COMPILER==WIN32C
137 * Close the pipe, restoring the keyboard (CMD resets it, losing the mouse).
146 SetConsoleMode(tty
, ENABLE_PROCESSED_INPUT
| ENABLE_MOUSE_INPUT
);
152 * Get the number of lines to scroll when mouse wheel is moved.
155 default_wheel_lines(VOID_PARAM
)
158 #if MSDOS_COMPILER==WIN32C
159 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES
, 0, &lines
, 0))
161 if (lines
== WHEEL_PAGESCROLL
)
175 lseek(rstat_file
, SEEK_SET
, 0);
176 write(rstat_file
, &st
, 1);
181 * Get a character from the keyboard.
192 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
194 * In raw read, we don't see ^C so look here for it.
196 #if MSDOS_COMPILER==WIN32C
212 result
= iread(tty
, &uc
, sizeof(char));
218 if (result
== READ_INTR
)
223 * Don't call error() here,
224 * because error calls getchr!
229 #if 0 /* allow entering arbitrary hex chars for testing */
230 /* ctrl-A followed by two hex chars makes a byte */
232 static int hex_in
= 0;
233 static int hex_value
= 0;
234 if (c
== CONTROL('A'))
243 if (c
>= '0' && c
<= '9')
245 else if (c
>= 'a' && c
<= 'f')
247 else if (c
>= 'A' && c
<= 'F')
251 hex_value
= (hex_value
<< 4) | v
;
262 * Various parts of the program cannot handle
263 * an input character of '\0'.
264 * If a '\0' was actually typed, convert it to '\340' here.
268 } while (result
!= 1);