1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
38 ** The routine getch().
42 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_getch.c,v 1.87 2008/05/03 22:42:10 tom Exp $")
46 #include <fifo_defs.h>
49 #define GetEscdelay(sp) (sp)->_ESCDELAY
51 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
53 return SP ? GetEscdelay(SP) : 1000;
56 #define GetEscdelay(sp) ESCDELAY
57 NCURSES_EXPORT_VAR(int)
58 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
63 set_escdelay(int value)
68 SP->_ESCDELAY = value;
79 #ifdef NCURSES_WGETCH_EVENTS
86 * Check for mouse activity, returning nonzero if we find any.
89 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
94 if ((sp->_mouse_type == M_SYSMOUSE)
95 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
99 rc = _nc_timed_wait(sp, TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
101 if ((sp->_mouse_type == M_SYSMOUSE)
102 && (sp->_sysmouse_head < sp->_sysmouse_tail)
104 && (errno == EINTR)) {
111 static NCURSES_INLINE int
112 fifo_peek(SCREEN *sp)
114 int ch = sp->_fifo[peek];
115 TR(TRACE_IEVENT, ("peeking at %d", peek));
121 static NCURSES_INLINE int
122 fifo_pull(SCREEN *sp)
125 ch = sp->_fifo[head];
126 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
135 if (USE_TRACEF(TRACE_IEVENT)) {
137 _nc_unlock_global(tracef);
143 static NCURSES_INLINE int
144 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
159 #ifdef NCURSES_WGETCH_EVENTS
161 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
162 || (sp->_mouse_fd >= 0)
165 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
170 T(("fifo_push: ungetch KEY_EVENT"));
171 _nc_ungetch(sp, KEY_EVENT);
174 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
175 if (sp->_mouse_fd >= 0) {
176 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
180 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
181 if ((sp->_mouse_fd >= 0) && (mask & 2)) {
182 sp->_mouse_event(sp);
188 if ((sp->_mouse_type == M_SYSMOUSE)
189 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
190 sp->_mouse_event(sp);
193 } else if ((sp->_mouse_type == M_SYSMOUSE)
194 && (mask <= 0) && errno == EINTR) {
195 sp->_mouse_event(sp);
201 unsigned char c2 = 0;
202 n = read(sp->_ifd, &c2, 1);
208 * Under System V curses with non-restarting signals, getch() returns
209 * with value ERR when a handled signal keeps it from completing.
210 * If signals restart system calls, OTOH, the signal is invisible
211 * except to its handler.
213 * We don't want this difference to show. This piece of code
214 * tries to make it look like we always have restarting signals.
216 if (n <= 0 && errno == EINTR)
220 if ((n == -1) || (n == 0)) {
221 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
224 TR(TRACE_IEVENT, ("read %d characters", n));
226 sp->_fifo[tail] = ch;
231 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
233 if (USE_TRACEF(TRACE_IEVENT)) {
235 _nc_unlock_global(tracef);
241 static NCURSES_INLINE void
242 fifo_clear(SCREEN *sp)
244 memset(sp->_fifo, 0, sizeof(sp->_fifo));
249 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
251 #define wgetch_should_refresh(win) (\
252 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
253 && !(win->_flags & _ISPAD))
256 _nc_wgetch(WINDOW *win,
257 unsigned long *result,
259 EVENTLIST_2nd(_nc_eventlist * evl))
263 #ifdef NCURSES_WGETCH_EVENTS
264 long event_delay = -1;
267 T((T_CALLED("_nc_wgetch(%p)"), win));
270 if (win == 0 || sp == 0) {
274 if (cooked_key_in_fifo()) {
275 if (wgetch_should_refresh(win))
278 *result = fifo_pull(sp);
279 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
281 #ifdef NCURSES_WGETCH_EVENTS
282 if (evl && (evl->count == 0))
284 event_delay = _nc_eventlist_timeout(evl);
288 * Handle cooked mode. Grab a string from the screen,
289 * stuff its contents in the FIFO queue, and pop off
290 * the first character to return it.
296 !sp->_called_wgetch) {
297 char buf[MAXCOLUMNS], *bufp;
300 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
302 sp->_called_wgetch = TRUE;
303 rc = wgetnstr(win, buf, MAXCOLUMNS);
304 sp->_called_wgetch = FALSE;
306 /* ungetch in reverse order */
307 #ifdef NCURSES_WGETCH_EVENTS
310 _nc_ungetch(sp, '\n');
311 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
312 _nc_ungetch(sp, bufp[-1]);
314 #ifdef NCURSES_WGETCH_EVENTS
315 /* Return it first */
316 if (rc == KEY_EVENT) {
320 *result = fifo_pull(sp);
321 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
324 if (win->_use_keypad != sp->_keypad_on)
325 _nc_keypad(sp, win->_use_keypad);
327 if (wgetch_should_refresh(win))
330 if (!win->_notimeout && (win->_delay >= 0 || sp->_cbreak > 1)) {
331 if (head == -1) { /* fifo is empty */
335 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
337 delay = (sp->_cbreak - 1) * 100;
341 #ifdef NCURSES_WGETCH_EVENTS
342 if (event_delay >= 0 && delay > event_delay)
346 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
348 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
350 #ifdef NCURSES_WGETCH_EVENTS
353 returnCode(KEY_CODE_YES);
359 /* else go on to read data available */
362 if (win->_use_keypad) {
364 * This is tricky. We only want to get special-key
365 * events one at a time. But we want to accumulate
366 * mouse events until either (a) the mouse logic tells
367 * us it's picked up a complete gesture, or (b)
368 * there's a detectable time lapse after one.
370 * Note: if the mouse code starts failing to compose
371 * press/release events into clicks, you should probably
372 * increase the wait with mouseinterval().
378 ch = kgetch(sp EVENTLIST_2nd(evl));
379 if (ch == KEY_MOUSE) {
381 if (sp->_mouse_inline(sp))
384 if (sp->_maxclick < 0)
388 && (((rc = check_mouse_activity(sp, sp->_maxclick
389 EVENTLIST_2nd(evl))) != 0
391 || !sp->_mouse_parse(runcount)));
392 #ifdef NCURSES_WGETCH_EVENTS
393 if ((rc & 4) && !ch == KEY_EVENT) {
398 if (runcount > 0 && ch != KEY_MOUSE) {
399 #ifdef NCURSES_WGETCH_EVENTS
400 /* mouse event sequence ended by an event, report event */
401 if (ch == KEY_EVENT) {
402 _nc_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
406 /* mouse event sequence ended by keystroke, store keystroke */
413 fifo_push(sp EVENTLIST_2nd(evl));
419 if (_nc_handle_sigwinch(sp)) {
420 _nc_update_screensize(sp);
421 /* resizeterm can push KEY_RESIZE */
422 if (cooked_key_in_fifo()) {
423 *result = fifo_pull(sp);
424 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
432 * If echo() is in effect, display the printable version of the
433 * key on the screen. Carriage return and backspace are treated
434 * specially by Solaris curses:
436 * If carriage return is defined as a function key in the
437 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
438 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
439 * We echo before translating carriage return based on nonl(),
440 * since the visual result simply moves the cursor to column 0.
442 * Backspace is a different matter. Solaris curses does not
443 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
444 * on the stty modes, but appears to be a hardcoded special case.
445 * This is a difference from ncurses, which uses the terminfo entry.
446 * However, we provide the same visual result as Solaris, moving the
447 * cursor to the left.
449 if (sp->_echo && !(win->_flags & _ISPAD)) {
450 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
451 if (backup < KEY_MIN)
452 wechochar(win, backup);
456 * Simulate ICRNL mode
458 if ((ch == '\r') && sp->_nl)
461 /* Strip 8th-bit if so desired. We do this only for characters that
462 * are in the range 128-255, to provide compatibility with terminals
463 * that display only 7-bit characters. Note that 'ch' may be a
464 * function key at this point, so we mustn't strip _those_.
467 if ((ch < KEY_MIN) && (ch & 0x80))
470 T(("wgetch returning : %s", _tracechar(ch)));
473 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
476 #ifdef NCURSES_WGETCH_EVENTS
478 wgetch_events(WINDOW *win, _nc_eventlist * evl)
484 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
485 code = _nc_wgetch(win,
502 T((T_CALLED("wgetch(%p)"), win));
503 code = _nc_wgetch(win,
505 (sp ? sp->_use_meta : 0)
506 EVENTLIST_2nd((_nc_eventlist *) 0));
516 ** Get an input character, but take care of keypad sequences, returning
517 ** an appropriate code when one matches the input. After each character
518 ** is received, set an alarm call based on ESCDELAY. If no more of the
519 ** sequence is received by the time the alarm goes off, pass through
520 ** the sequence gotten so far.
522 ** This function must be called when there are no cooked keys in queue.
523 ** (that is head==-1 || peek==head)
528 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
532 int timeleft = GetEscdelay(sp);
534 TR(TRACE_IEVENT, ("kgetch() called"));
539 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
541 } else if (!raw_key_in_fifo()) {
542 ch = fifo_push(sp EVENTLIST_2nd(evl));
544 peek = head; /* the keys stay uninterpreted */
547 #ifdef NCURSES_WGETCH_EVENTS
548 else if (ch == KEY_EVENT) {
549 peek = head; /* the keys stay uninterpreted */
550 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
557 /* If not first in queue, somebody put this key there on purpose in
558 * emergency. Consider it higher priority than the unfinished
559 * keysequence we are parsing.
562 /* assume the key is the last in fifo */
563 t_dec(); /* remove the key */
567 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
568 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
572 TR(TRACE_IEVENT, ("ptr is null"));
575 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
576 ptr, ptr->ch, ptr->value));
578 if (ptr->value != 0) { /* sequence terminated */
579 TR(TRACE_IEVENT, ("end of sequence"));
589 if (!raw_key_in_fifo()) {
592 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
593 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
594 #ifdef NCURSES_WGETCH_EVENTS
596 TR(TRACE_IEVENT, ("interrupted by a user event"));
597 /* FIXME Should have preserved remainder timeleft for reuse... */
598 peek = head; /* Restart interpreting later */
603 TR(TRACE_IEVENT, ("ran out of time"));