1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
37 ** The routine getch().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_getch.c,v 1.48 2000/07/08 11:21:51 tom Exp $")
45 #include <fifo_defs.h>
47 int ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
50 # include <sys/select.h>
52 kbd_mouse_read(unsigned char *p)
55 int nums = SP->_ifd + 1;
59 FD_SET(SP->_ifd, &fdset);
60 if (SP->_checkfd >= 0) {
61 FD_SET(SP->_checkfd, &fdset);
62 if (SP->_checkfd >= nums)
63 nums = SP->_checkfd + 1;
65 if (SP->_mouse_fd >= 0) {
66 FD_SET(SP->_mouse_fd, &fdset);
67 if (SP->_mouse_fd >= nums)
68 nums = SP->_mouse_fd + 1;
70 if (select(nums, &fdset, NULL, NULL, NULL) >= 0) {
73 if (SP->_mouse_fd >= 0
74 && FD_ISSET(SP->_mouse_fd, &fdset)) { /* Prefer mouse */
75 n = read(SP->_mouse_fd, p, 1);
77 n = read(SP->_ifd, p, 1);
86 #endif /* USE_EMX_MOUSE */
91 int ch = SP->_fifo[peek];
92 T(("peeking at %d", peek));
102 ch = SP->_fifo[head];
103 T(("pulling %d from %d", ch, head));
112 if (_nc_tracing & TRACE_IEVENT)
133 if ((SP->_mouse_fd >= 0)
134 && (_nc_timed_wait(3, -1, (int *) 0) & 2)) {
135 SP->_mouse_event(SP);
141 unsigned char c2 = 0;
143 n = kbd_mouse_read(&c2);
145 n = read(SP->_ifd, &c2, 1);
152 * Under System V curses with non-restarting signals, getch() returns
153 * with value ERR when a handled signal keeps it from completing.
154 * If signals restart system calls, OTOH, the signal is invisible
155 * except to its handler.
157 * We don't want this difference to show. This piece of code
158 * tries to make it look like we always have restarting signals.
160 if (n <= 0 && errno == EINTR)
164 if ((n == -1) || (n == 0)) {
165 T(("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
168 T(("read %d characters", n));
170 SP->_fifo[tail] = ch;
175 T(("pushed %#x at %d", ch, tail));
177 if (_nc_tracing & TRACE_IEVENT)
187 for (i = 0; i < FIFO_SIZE; i++)
193 static int kgetch(WINDOW *);
195 #define wgetch_should_refresh(win) (\
196 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
197 && !(win->_flags & _ISPAD))
204 T((T_CALLED("wgetch(%p)"), win));
209 if (cooked_key_in_fifo()) {
210 if (wgetch_should_refresh(win))
214 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch)));
219 * Handle cooked mode. Grab a string from the screen,
220 * stuff its contents in the FIFO queue, and pop off
221 * the first character to return it.
223 if (head == -1 && !SP->_raw && !SP->_cbreak) {
224 char buf[MAXCOLUMNS], *sp;
226 T(("filling queue in cooked mode"));
228 wgetnstr(win, buf, MAXCOLUMNS);
230 /* ungetch in reverse order */
232 for (sp = buf + strlen(buf); sp > buf; sp--)
235 returnCode(fifo_pull());
238 if (wgetch_should_refresh(win))
241 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
244 T(("timed delay in wgetch()"));
246 delay = (SP->_cbreak - 1) * 100;
250 T(("delay is %d milliseconds", delay));
252 if (head == -1) /* fifo is empty */
253 if (!_nc_timed_wait(3, delay, (int *) 0))
255 /* else go on to read data available */
258 if (win->_use_keypad) {
260 * This is tricky. We only want to get special-key
261 * events one at a time. But we want to accumulate
262 * mouse events until either (a) the mouse logic tells
263 * us it's picked up a complete gesture, or (b)
264 * there's a detectable time lapse after one.
266 * Note: if the mouse code starts failing to compose
267 * press/release events into clicks, you should probably
268 * increase the wait with mouseinterval().
274 if (ch == KEY_MOUSE) {
276 if (SP->_mouse_inline(SP))
281 && (_nc_timed_wait(3, SP->_maxclick, (int *) 0)
282 || !SP->_mouse_parse(runcount)));
283 if (runcount > 0 && ch != KEY_MOUSE) {
284 /* mouse event sequence ended by keystroke, push it */
296 if (SP->_sig_winch) {
297 _nc_update_screensize();
298 /* resizeterm can push KEY_RESIZE */
299 if (cooked_key_in_fifo()) {
301 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch)));
306 T(("wgetch returning ERR"));
311 * If echo() is in effect, display the printable version of the
312 * key on the screen. Carriage return and backspace are treated
313 * specially by Solaris curses:
315 * If carriage return is defined as a function key in the
316 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
317 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
318 * We echo before translating carriage return based on nonl(),
319 * since the visual result simply moves the cursor to column 0.
321 * Backspace is a different matter. Solaris curses does not
322 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
323 * on the stty modes, but appears to be a hardcoded special case.
324 * This is a difference from ncurses, which uses the terminfo entry.
325 * However, we provide the same visual result as Solaris, moving the
326 * cursor to the left.
328 if (SP->_echo && !(win->_flags & _ISPAD)) {
329 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
330 if (backup < KEY_MIN)
331 wechochar(win, backup);
335 * Simulate ICRNL mode
337 if ((ch == '\r') && SP->_nl)
340 /* Strip 8th-bit if so desired. We do this only for characters that
341 * are in the range 128-255, to provide compatibility with terminals
342 * that display only 7-bit characters. Note that 'ch' may be a
343 * function key at this point, so we mustn't strip _those_.
345 if ((ch < KEY_MIN) && (ch & 0x80))
349 T(("wgetch returning : %#x = %s", ch, _trace_key(ch)));
358 ** Get an input character, but take care of keypad sequences, returning
359 ** an appropriate code when one matches the input. After each character
360 ** is received, set an alarm call based on ESCDELAY. If no more of the
361 ** sequence is received by the time the alarm goes off, pass through
362 ** the sequence gotten so far.
364 ** This function must be called when there is no cooked keys in queue.
365 ** (that is head==-1 || peek==head)
370 kgetch(WINDOW *win GCC_UNUSED)
374 int timeleft = ESCDELAY;
376 TR(TRACE_IEVENT, ("kgetch(%p) called", win));
381 if (!raw_key_in_fifo()) {
382 if (fifo_push() == ERR) {
383 peek = head; /* the keys stay uninterpreted */
390 /* assume the key is the last in fifo */
391 t_dec(); /* remove the key */
395 TR(TRACE_IEVENT, ("ch: %s", _trace_key((unsigned char) ch)));
396 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
400 TR(TRACE_IEVENT, ("ptr is null"));
402 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
403 ptr, ptr->ch, ptr->value));
409 if (ptr->value != 0) { /* sequence terminated */
410 TR(TRACE_IEVENT, ("end of sequence"));
420 if (!raw_key_in_fifo()) {
421 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
422 if (!_nc_timed_wait(3, timeleft, &timeleft)) {
423 TR(TRACE_IEVENT, ("ran out of time"));