2 * Copyright (C) 1984-2023 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
;
36 extern int wheel_lines
;
39 static int open_tty_device(constant
char* dev
)
42 /* The __open() system call translates "/dev/tty" to "con". */
43 return __open(dev
, OPEN_READ
);
45 return open(dev
, OPEN_READ
);
50 * Open the tty device.
51 * Try ttyname(), then try /dev/tty, then use file descriptor 2.
52 * In Unix, file descriptor 2 is usually attached to the screen,
53 * but also usually lets you read from the keyboard.
55 public int open_tty(void)
59 if (ttyin_name
!= NULL
)
60 fd
= open_tty_device(ttyin_name
);
65 constant
char *dev
= ttyname(2);
67 fd
= open_tty_device(dev
);
71 fd
= open_tty_device("/dev/tty");
76 #endif /* MSDOS_COMPILER */
79 * Open keyboard for input.
81 public void open_getchr(void)
83 #if MSDOS_COMPILER==WIN32C
84 /* Need this to let child processes inherit our console handle */
85 SECURITY_ATTRIBUTES sa
;
86 memset(&sa
, 0, sizeof(SECURITY_ATTRIBUTES
));
87 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
88 sa
.bInheritHandle
= TRUE
;
89 tty
= CreateFile("CONIN$", GENERIC_READ
| GENERIC_WRITE
,
91 OPEN_EXISTING
, 0L, NULL
);
92 GetConsoleMode(tty
, &console_mode
);
93 /* Make sure we get Ctrl+C events. */
94 SetConsoleMode(tty
, ENABLE_PROCESSED_INPUT
| ENABLE_MOUSE_INPUT
);
99 * Open a new handle to CON: in binary mode
100 * for unbuffered keyboard read.
104 tty
= open("CON", OPEN_READ
);
105 #if MSDOS_COMPILER==DJGPPC
107 * Setting stdin to binary causes Ctrl-C to not
108 * raise SIGINT. We must undo that side-effect.
110 (void) __djgpp_set_ctrl_c(1);
119 * Close the keyboard.
121 public void close_getchr(void)
123 #if MSDOS_COMPILER==WIN32C
124 SetConsoleMode(tty
, console_mode
);
129 #if MSDOS_COMPILER==WIN32C
131 * Close the pipe, restoring the keyboard (CMD resets it, losing the mouse).
133 public int pclose(FILE *f
)
138 SetConsoleMode(tty
, ENABLE_PROCESSED_INPUT
| ENABLE_MOUSE_INPUT
);
144 * Get the number of lines to scroll when mouse wheel is moved.
146 public int default_wheel_lines(void)
149 #if MSDOS_COMPILER==WIN32C
150 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES
, 0, &lines
, 0))
152 if (lines
== WHEEL_PAGESCROLL
)
160 * Get a character from the keyboard.
162 public int getchr(void)
170 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
172 * In raw read, we don't see ^C so look here for it.
174 #if MSDOS_COMPILER==WIN32C
187 result
= iread(tty
, &uc
, sizeof(char));
190 if (result
== READ_INTR
)
195 * Don't call error() here,
196 * because error calls getchr!
202 if (c
== LESS_DUMP_CHAR
)
209 #if 0 /* allow entering arbitrary hex chars for testing */
210 /* ctrl-A followed by two hex chars makes a byte */
212 static int hex_in
= 0;
213 static int hex_value
= 0;
214 if (c
== CONTROL('A'))
223 if (c
>= '0' && c
<= '9')
225 else if (c
>= 'a' && c
<= 'f')
227 else if (c
>= 'A' && c
<= 'F')
231 hex_value
= (hex_value
<< 4) | v
;
242 * Various parts of the program cannot handle
243 * an input character of '\0'.
244 * If a '\0' was actually typed, convert it to '\340' here.
248 } while (result
!= 1);