1 /****************************************************************************
2 * Copyright (c) 1998 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.43 1999/03/08 02:35:10 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->_checkfd, &fdset);
60 if (SP->_mouse_fd >= 0) {
61 FD_SET(SP->_mouse_fd, &fdset);
62 if (SP->_mouse_fd > SP->_checkfd)
63 nums = SP->_mouse_fd+1;
65 if (select(nums, &fdset, NULL, NULL, NULL) >= 0) {
68 if (FD_ISSET(SP->_mouse_fd, &fdset)) /* Prefer mouse */
69 n = read(SP->_mouse_fd, p, 1);
71 n = read(SP->_ifd, p, 1);
78 #endif /* USE_EMX_MOUSE */
80 static inline int fifo_peek(void)
82 int ch = SP->_fifo[peek];
83 T(("peeking at %d", peek));
90 static inline int fifo_pull(void)
94 T(("pulling %d from %d", ch, head));
105 if (_nc_tracing & TRACE_IEVENT) _nc_fifo_dump();
110 static inline int fifo_push(void)
115 if (tail == -1) return ERR;
123 if ((SP->_mouse_fd >= 0)
124 && (_nc_timed_wait(3, -1, (int *)0) & 2))
126 SP->_mouse_event(SP);
134 n = kbd_mouse_read(&c2);
136 n = read(SP->_ifd, &c2, 1);
143 * Under System V curses with non-restarting signals, getch() returns
144 * with value ERR when a handled signal keeps it from completing.
145 * If signals restart system calls, OTOH, the signal is invisible
146 * except to its handler.
148 * We don't want this difference to show. This piece of code
149 * tries to make it look like we always have restarting signals.
151 if (n <= 0 && errno == EINTR)
155 if ((n == -1) || (n == 0))
157 T(("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
160 T(("read %d characters", n));
162 SP->_fifo[tail] = ch;
167 T(("pushed %#x at %d", ch, tail));
169 if (_nc_tracing & TRACE_IEVENT) _nc_fifo_dump();
174 static inline void fifo_clear(void)
177 for (i = 0; i < FIFO_SIZE; i++)
179 head = -1; tail = peek = 0;
182 static int kgetch(WINDOW *);
184 #define wgetch_should_refresh(win) (\
185 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
186 && !(win->_flags & _ISPAD))
193 T((T_CALLED("wgetch(%p)"), win));
198 if (cooked_key_in_fifo())
200 if (wgetch_should_refresh(win))
204 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch));)
209 * Handle cooked mode. Grab a string from the screen,
210 * stuff its contents in the FIFO queue, and pop off
211 * the first character to return it.
213 if (head == -1 && !SP->_raw && !SP->_cbreak)
215 char buf[MAXCOLUMNS], *sp;
217 T(("filling queue in cooked mode"));
219 wgetnstr(win, buf, MAXCOLUMNS);
221 /* ungetch in reverse order */
223 for (sp = buf+strlen(buf); sp>buf; sp--)
226 returnCode(fifo_pull());
229 if (wgetch_should_refresh(win))
232 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1))
236 T(("timed delay in wgetch()"));
238 delay = (SP->_cbreak - 1) * 100;
242 T(("delay is %d milliseconds", delay));
244 if (head == -1) /* fifo is empty */
245 if (!_nc_timed_wait(3, delay, (int *)0))
247 /* else go on to read data available */
250 if (win->_use_keypad)
253 * This is tricky. We only want to get special-key
254 * events one at a time. But we want to accumulate
255 * mouse events until either (a) the mouse logic tells
256 * us it's picked up a complete gesture, or (b)
257 * there's a detectable time lapse after one.
259 * Note: if the mouse code starts failing to compose
260 * press/release events into clicks, you should probably
261 * increase the wait with mouseinterval().
270 if (SP->_mouse_inline(SP))
275 && (_nc_timed_wait(3, SP->_maxclick, (int *)0)
276 || !SP->_mouse_parse(runcount)));
277 if (runcount > 0 && ch != KEY_MOUSE)
279 /* mouse event sequence ended by keystroke, push it */
294 _nc_update_screensize();
295 /* resizeterm can push KEY_RESIZE */
296 if(cooked_key_in_fifo())
299 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch));)
304 T(("wgetch returning ERR"));
309 * Simulate ICRNL mode
311 if ((ch == '\r') && SP->_nl)
314 /* Strip 8th-bit if so desired. We do this only for characters that
315 * are in the range 128-255, to provide compatibility with terminals
316 * that display only 7-bit characters. Note that 'ch' may be a
317 * function key at this point, so we mustn't strip _those_.
319 if ((ch < KEY_MIN) && (ch & 0x80))
323 if (SP->_echo && ch < KEY_MIN && !(win->_flags & _ISPAD))
324 wechochar(win, (chtype)ch);
326 T(("wgetch returning : %#x = %s", ch, _trace_key(ch)));
336 ** Get an input character, but take care of keypad sequences, returning
337 ** an appropriate code when one matches the input. After each character
338 ** is received, set an alarm call based on ESCDELAY. If no more of the
339 ** sequence is received by the time the alarm goes off, pass through
340 ** the sequence gotten so far.
342 ** This function must be called when there is no cooked keys in queue.
343 ** (that is head==-1 || peek==head)
348 kgetch(WINDOW *win GCC_UNUSED)
352 int timeleft = ESCDELAY;
354 TR(TRACE_IEVENT, ("kgetch(%p) called", win));
360 if (!raw_key_in_fifo())
362 if(fifo_push() == ERR)
364 peek = head; /* the keys stay uninterpreted */
372 /* assume the key is the last in fifo */
373 t_dec(); /* remove the key */
377 TR(TRACE_IEVENT, ("ch: %s", _trace_key((unsigned char)ch)));
378 while ((ptr != NULL) && (ptr->ch != (unsigned char)ch))
382 {TR(TRACE_IEVENT, ("ptr is null"));}
384 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
385 ptr, ptr->ch, ptr->value));
391 if (ptr->value != 0) { /* sequence terminated */
392 TR(TRACE_IEVENT, ("end of sequence"));
402 if (!raw_key_in_fifo())
404 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
405 if (!_nc_timed_wait(3, timeleft, &timeleft)) {
406 TR(TRACE_IEVENT, ("ran out of time"));