]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_getch.c
ncurses 5.9 - patch 20120225
[ncurses.git] / ncurses / base / lib_getch.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37 **      lib_getch.c
38 **
39 **      The routine getch().
40 **
41 */
42
43 #include <curses.priv.h>
44
45 MODULE_ID("$Id: lib_getch.c,v 1.124 2012/01/21 19:21:29 KO.Myung-Hun Exp $")
46
47 #include <fifo_defs.h>
48
49 #if USE_REENTRANT
50 #define GetEscdelay(sp) *_nc_ptr_Escdelay(sp)
51 NCURSES_EXPORT(int)
52 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
53 {
54     return *(_nc_ptr_Escdelay(CURRENT_SCREEN));
55 }
56
57 NCURSES_EXPORT(int *)
58 _nc_ptr_Escdelay(SCREEN *sp)
59 {
60     return ptrEscdelay(sp);
61 }
62 #else
63 #define GetEscdelay(sp) ESCDELAY
64 NCURSES_EXPORT_VAR(int) ESCDELAY = 1000;
65 #endif
66
67 #if NCURSES_EXT_FUNCS
68 NCURSES_EXPORT(int)
69 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_DCLx int value)
70 {
71     int code = OK;
72 #if USE_REENTRANT
73     if (SP_PARM) {
74         SET_ESCDELAY(value);
75     } else {
76         code = ERR;
77     }
78 #else
79     (void) SP_PARM;
80     ESCDELAY = value;
81 #endif
82     return code;
83 }
84
85 #if NCURSES_SP_FUNCS
86 NCURSES_EXPORT(int)
87 set_escdelay(int value)
88 {
89     int code;
90 #if USE_REENTRANT
91     code = NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
92 #else
93     ESCDELAY = value;
94     code = OK;
95 #endif
96     return code;
97 }
98 #endif
99 #endif /* NCURSES_EXT_FUNCS */
100
101 #if NCURSES_EXT_FUNCS
102 NCURSES_EXPORT(int)
103 NCURSES_SP_NAME(get_escdelay) (NCURSES_SP_DCL0)
104 {
105 #if !USE_REENTRANT
106     (void) SP_PARM;
107 #endif
108     return GetEscdelay(SP_PARM);
109 }
110
111 #if NCURSES_SP_FUNCS
112 NCURSES_EXPORT(int)
113 get_escdelay(void)
114 {
115     return NCURSES_SP_NAME(get_escdelay) (CURRENT_SCREEN);
116 }
117 #endif
118 #endif /* NCURSES_EXT_FUNCS */
119
120 static int
121 _nc_use_meta(WINDOW *win)
122 {
123     SCREEN *sp = _nc_screen_of(win);
124     return (sp ? sp->_use_meta : 0);
125 }
126
127 /*
128  * Check for mouse activity, returning nonzero if we find any.
129  */
130 static int
131 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
132 {
133     int rc;
134
135 #ifdef USE_TERM_DRIVER
136     rc = TCBOf(sp)->drv->testmouse(TCBOf(sp), delay EVENTLIST_2nd(evl));
137 #else
138 #if USE_SYSMOUSE
139     if ((sp->_mouse_type == M_SYSMOUSE)
140         && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
141         rc = TW_MOUSE;
142     } else
143 #endif
144     {
145         rc = _nc_timed_wait(sp,
146                             TWAIT_MASK,
147                             delay,
148                             (int *) 0
149                             EVENTLIST_2nd(evl));
150 #if USE_SYSMOUSE
151         if ((sp->_mouse_type == M_SYSMOUSE)
152             && (sp->_sysmouse_head < sp->_sysmouse_tail)
153             && (rc == 0)
154             && (errno == EINTR)) {
155             rc |= TW_MOUSE;
156         }
157 #endif
158     }
159 #endif
160     return rc;
161 }
162
163 static NCURSES_INLINE int
164 fifo_peek(SCREEN *sp)
165 {
166     int ch = sp->_fifo[peek];
167     TR(TRACE_IEVENT, ("peeking at %d", peek));
168
169     p_inc();
170     return ch;
171 }
172
173 static NCURSES_INLINE int
174 fifo_pull(SCREEN *sp)
175 {
176     int ch;
177     ch = sp->_fifo[head];
178     TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
179
180     if (peek == head) {
181         h_inc();
182         peek = head;
183     } else
184         h_inc();
185
186 #ifdef TRACE
187     if (USE_TRACEF(TRACE_IEVENT)) {
188         _nc_fifo_dump(sp);
189         _nc_unlock_global(tracef);
190     }
191 #endif
192     return ch;
193 }
194
195 static NCURSES_INLINE int
196 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
197 {
198     int n;
199     int ch = 0;
200     int mask = 0;
201
202     (void) mask;
203     if (tail == -1)
204         return ERR;
205
206 #ifdef HIDE_EINTR
207   again:
208     errno = 0;
209 #endif
210
211 #ifdef NCURSES_WGETCH_EVENTS
212     if (evl
213 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
214         || (sp->_mouse_fd >= 0)
215 #endif
216         ) {
217         mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
218     } else
219         mask = 0;
220
221     if (mask & TW_EVENT) {
222         T(("fifo_push: ungetch KEY_EVENT"));
223         safe_ungetch(sp, KEY_EVENT);
224         return KEY_EVENT;
225     }
226 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
227     if (sp->_mouse_fd >= 0) {
228         mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
229     }
230 #endif
231
232 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
233     if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
234         sp->_mouse_event(sp);
235         ch = KEY_MOUSE;
236         n = 1;
237     } else
238 #endif
239 #if USE_SYSMOUSE
240         if ((sp->_mouse_type == M_SYSMOUSE)
241             && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
242         sp->_mouse_event(sp);
243         ch = KEY_MOUSE;
244         n = 1;
245     } else if ((sp->_mouse_type == M_SYSMOUSE)
246                && (mask <= 0) && errno == EINTR) {
247         sp->_mouse_event(sp);
248         ch = KEY_MOUSE;
249         n = 1;
250     } else
251 #endif
252 #ifdef USE_TERM_DRIVER
253         if ((sp->_mouse_type == M_TERM_DRIVER)
254             && (sp->_drv_mouse_head < sp->_drv_mouse_tail)) {
255         sp->_mouse_event(sp);
256         ch = KEY_MOUSE;
257         n = 1;
258     } else
259 #endif
260 #if USE_KLIBC_KBD
261     if (isatty(sp->_ifd) && sp->_cbreak) {
262         ch = _read_kbd(0, 1, !sp->_raw);
263         n = (ch == -1) ? -1 : 1;
264         sp->_extended_key = (ch == 0);
265     } else
266 #endif
267     {                           /* Can block... */
268 #ifdef USE_TERM_DRIVER
269         int buf;
270         n = CallDriver_1(sp, read, &buf);
271         ch = buf;
272 #else
273         unsigned char c2 = 0;
274 # if USE_PTHREADS_EINTR
275 #  if USE_WEAK_SYMBOLS
276         if ((pthread_self) && (pthread_kill) && (pthread_equal))
277 #  endif
278             _nc_globals.read_thread = pthread_self();
279 # endif
280         n = (int) read(sp->_ifd, &c2, (size_t) 1);
281 #if USE_PTHREADS_EINTR
282         _nc_globals.read_thread = 0;
283 #endif
284         ch = c2;
285 #endif
286     }
287
288 #ifdef HIDE_EINTR
289     /*
290      * Under System V curses with non-restarting signals, getch() returns
291      * with value ERR when a handled signal keeps it from completing.
292      * If signals restart system calls, OTOH, the signal is invisible
293      * except to its handler.
294      *
295      * We don't want this difference to show.  This piece of code
296      * tries to make it look like we always have restarting signals.
297      */
298     if (n <= 0 && errno == EINTR
299 # if USE_PTHREADS_EINTR
300         && (_nc_globals.have_sigwinch == 0)
301 # endif
302         )
303         goto again;
304 #endif
305
306     if ((n == -1) || (n == 0)) {
307         TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
308         ch = ERR;
309     }
310     TR(TRACE_IEVENT, ("read %d characters", n));
311
312     sp->_fifo[tail] = ch;
313     sp->_fifohold = 0;
314     if (head == -1)
315         head = peek = tail;
316     t_inc();
317     TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
318 #ifdef TRACE
319     if (USE_TRACEF(TRACE_IEVENT)) {
320         _nc_fifo_dump(sp);
321         _nc_unlock_global(tracef);
322     }
323 #endif
324     return ch;
325 }
326
327 static NCURSES_INLINE void
328 fifo_clear(SCREEN *sp)
329 {
330     memset(sp->_fifo, 0, sizeof(sp->_fifo));
331     head = -1;
332     tail = peek = 0;
333 }
334
335 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
336
337 static void
338 recur_wrefresh(WINDOW *win)
339 {
340 #ifdef USE_PTHREADS
341     SCREEN *sp = _nc_screen_of(win);
342     if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
343         SCREEN *save_SP;
344
345         /* temporarily switch to the window's screen to check/refresh */
346         _nc_lock_global(curses);
347         save_SP = CURRENT_SCREEN;
348         _nc_set_screen(sp);
349         recur_wrefresh(win);
350         _nc_set_screen(save_SP);
351         _nc_unlock_global(curses);
352     } else
353 #endif
354         if ((is_wintouched(win) || (win->_flags & _HASMOVED))
355             && !(win->_flags & _ISPAD)) {
356         wrefresh(win);
357     }
358 }
359
360 static int
361 recur_wgetnstr(WINDOW *win, char *buf)
362 {
363     SCREEN *sp = _nc_screen_of(win);
364     int rc;
365
366     if (sp != 0) {
367 #ifdef USE_PTHREADS
368         if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
369             SCREEN *save_SP;
370
371             /* temporarily switch to the window's screen to get cooked input */
372             _nc_lock_global(curses);
373             save_SP = CURRENT_SCREEN;
374             _nc_set_screen(sp);
375             rc = recur_wgetnstr(win, buf);
376             _nc_set_screen(save_SP);
377             _nc_unlock_global(curses);
378         } else
379 #endif
380         {
381             sp->_called_wgetch = TRUE;
382             rc = wgetnstr(win, buf, MAXCOLUMNS);
383             sp->_called_wgetch = FALSE;
384         }
385     } else {
386         rc = ERR;
387     }
388     return rc;
389 }
390
391 NCURSES_EXPORT(int)
392 _nc_wgetch(WINDOW *win,
393            int *result,
394            int use_meta
395            EVENTLIST_2nd(_nc_eventlist * evl))
396 {
397     SCREEN *sp;
398     int ch;
399     int rc = 0;
400 #ifdef NCURSES_WGETCH_EVENTS
401     long event_delay = -1;
402 #endif
403
404     T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
405
406     *result = 0;
407
408     sp = _nc_screen_of(win);
409     if (win == 0 || sp == 0) {
410         returnCode(ERR);
411     }
412
413     if (cooked_key_in_fifo()) {
414         recur_wrefresh(win);
415         *result = fifo_pull(sp);
416         returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
417     }
418 #ifdef NCURSES_WGETCH_EVENTS
419     if (evl && (evl->count == 0))
420         evl = NULL;
421     event_delay = _nc_eventlist_timeout(evl);
422 #endif
423
424     /*
425      * Handle cooked mode.  Grab a string from the screen,
426      * stuff its contents in the FIFO queue, and pop off
427      * the first character to return it.
428      */
429     if (head == -1 &&
430         !sp->_notty &&
431         !sp->_raw &&
432         !sp->_cbreak &&
433         !sp->_called_wgetch) {
434         char buf[MAXCOLUMNS], *bufp;
435
436         TR(TRACE_IEVENT, ("filling queue in cooked mode"));
437
438         /* ungetch in reverse order */
439 #ifdef NCURSES_WGETCH_EVENTS
440         rc = recur_wgetnstr(win, buf);
441         if (rc != KEY_EVENT)
442             safe_ungetch(sp, '\n');
443 #else
444         (void) recur_wgetnstr(win, buf);
445         safe_ungetch(sp, '\n');
446 #endif
447         for (bufp = buf + strlen(buf); bufp > buf; bufp--)
448             safe_ungetch(sp, bufp[-1]);
449
450 #ifdef NCURSES_WGETCH_EVENTS
451         /* Return it first */
452         if (rc == KEY_EVENT) {
453             *result = rc;
454         } else
455 #endif
456             *result = fifo_pull(sp);
457         returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
458     }
459
460     if (win->_use_keypad != sp->_keypad_on)
461         _nc_keypad(sp, win->_use_keypad);
462
463     recur_wrefresh(win);
464
465     if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
466         if (head == -1) {       /* fifo is empty */
467             int delay;
468
469             TR(TRACE_IEVENT, ("timed delay in wgetch()"));
470             if (sp->_cbreak > 1)
471                 delay = (sp->_cbreak - 1) * 100;
472             else
473                 delay = win->_delay;
474
475 #ifdef NCURSES_WGETCH_EVENTS
476             if (event_delay >= 0 && delay > event_delay)
477                 delay = event_delay;
478 #endif
479
480             TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
481
482             rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
483
484 #ifdef NCURSES_WGETCH_EVENTS
485             if (rc & TW_EVENT) {
486                 *result = KEY_EVENT;
487                 returnCode(KEY_CODE_YES);
488             }
489 #endif
490             if (!rc) {
491                 goto check_sigwinch;
492             }
493         }
494         /* else go on to read data available */
495     }
496
497     if (win->_use_keypad) {
498         /*
499          * This is tricky.  We only want to get special-key
500          * events one at a time.  But we want to accumulate
501          * mouse events until either (a) the mouse logic tells
502          * us it's picked up a complete gesture, or (b)
503          * there's a detectable time lapse after one.
504          *
505          * Note: if the mouse code starts failing to compose
506          * press/release events into clicks, you should probably
507          * increase the wait with mouseinterval().
508          */
509         int runcount = 0;
510
511         do {
512             ch = kgetch(sp EVENTLIST_2nd(evl));
513             if (ch == KEY_MOUSE) {
514                 ++runcount;
515                 if (sp->_mouse_inline(sp))
516                     break;
517             }
518             if (sp->_maxclick < 0)
519                 break;
520         } while
521             (ch == KEY_MOUSE
522              && (((rc = check_mouse_activity(sp, sp->_maxclick
523                                              EVENTLIST_2nd(evl))) != 0
524                   && !(rc & TW_EVENT))
525                  || !sp->_mouse_parse(sp, runcount)));
526 #ifdef NCURSES_WGETCH_EVENTS
527         if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
528             safe_ungetch(sp, ch);
529             ch = KEY_EVENT;
530         }
531 #endif
532         if (runcount > 0 && ch != KEY_MOUSE) {
533 #ifdef NCURSES_WGETCH_EVENTS
534             /* mouse event sequence ended by an event, report event */
535             if (ch == KEY_EVENT) {
536                 safe_ungetch(sp, KEY_MOUSE);    /* FIXME This interrupts a gesture... */
537             } else
538 #endif
539             {
540                 /* mouse event sequence ended by keystroke, store keystroke */
541                 safe_ungetch(sp, ch);
542                 ch = KEY_MOUSE;
543             }
544         }
545     } else {
546         if (head == -1)
547             fifo_push(sp EVENTLIST_2nd(evl));
548         ch = fifo_pull(sp);
549     }
550
551     if (ch == ERR) {
552       check_sigwinch:
553 #if USE_SIZECHANGE
554         if (_nc_handle_sigwinch(sp)) {
555             _nc_update_screensize(sp);
556             /* resizeterm can push KEY_RESIZE */
557             if (cooked_key_in_fifo()) {
558                 *result = fifo_pull(sp);
559                 /*
560                  * Get the ERR from queue -- it is from WINCH,
561                  * so we should take it out, the "error" is handled.
562                  */
563                 if (fifo_peek(sp) == -1)
564                     fifo_pull(sp);
565                 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
566             }
567         }
568 #endif
569         returnCode(ERR);
570     }
571
572     /*
573      * If echo() is in effect, display the printable version of the
574      * key on the screen.  Carriage return and backspace are treated
575      * specially by Solaris curses:
576      *
577      * If carriage return is defined as a function key in the
578      * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
579      * if nonl() is set) or KEY_ENTER depending on the echo() mode.
580      * We echo before translating carriage return based on nonl(),
581      * since the visual result simply moves the cursor to column 0.
582      *
583      * Backspace is a different matter.  Solaris curses does not
584      * translate it to KEY_BACKSPACE if kbs=^H.  This does not depend
585      * on the stty modes, but appears to be a hardcoded special case.
586      * This is a difference from ncurses, which uses the terminfo entry.
587      * However, we provide the same visual result as Solaris, moving the
588      * cursor to the left.
589      */
590     if (sp->_echo && !(win->_flags & _ISPAD)) {
591         chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
592         if (backup < KEY_MIN)
593             wechochar(win, backup);
594     }
595
596     /*
597      * Simulate ICRNL mode
598      */
599     if ((ch == '\r') && sp->_nl)
600         ch = '\n';
601
602     /* Strip 8th-bit if so desired.  We do this only for characters that
603      * are in the range 128-255, to provide compatibility with terminals
604      * that display only 7-bit characters.  Note that 'ch' may be a
605      * function key at this point, so we mustn't strip _those_.
606      */
607     if (!use_meta)
608         if ((ch < KEY_MIN) && (ch & 0x80))
609             ch &= 0x7f;
610
611     T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
612
613     *result = ch;
614     returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
615 }
616
617 #ifdef NCURSES_WGETCH_EVENTS
618 NCURSES_EXPORT(int)
619 wgetch_events(WINDOW *win, _nc_eventlist * evl)
620 {
621     int code;
622     int value;
623
624     T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
625     code = _nc_wgetch(win,
626                       &value,
627                       _nc_use_meta(win)
628                       EVENTLIST_2nd(evl));
629     if (code != ERR)
630         code = value;
631     returnCode(code);
632 }
633 #endif
634
635 NCURSES_EXPORT(int)
636 wgetch(WINDOW *win)
637 {
638     int code;
639     int value;
640
641     T((T_CALLED("wgetch(%p)"), (void *) win));
642     code = _nc_wgetch(win,
643                       &value,
644                       _nc_use_meta(win)
645                       EVENTLIST_2nd((_nc_eventlist *) 0));
646     if (code != ERR)
647         code = value;
648     returnCode(code);
649 }
650
651 /*
652 **      int
653 **      kgetch()
654 **
655 **      Get an input character, but take care of keypad sequences, returning
656 **      an appropriate code when one matches the input.  After each character
657 **      is received, set an alarm call based on ESCDELAY.  If no more of the
658 **      sequence is received by the time the alarm goes off, pass through
659 **      the sequence gotten so far.
660 **
661 **      This function must be called when there are no cooked keys in queue.
662 **      (that is head==-1 || peek==head)
663 **
664 */
665
666 static int
667 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
668 {
669     TRIES *ptr;
670     int ch = 0;
671     int timeleft = GetEscdelay(sp);
672
673     TR(TRACE_IEVENT, ("kgetch() called"));
674
675     ptr = sp->_keytry;
676
677     for (;;) {
678         if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
679             break;
680         } else if (!raw_key_in_fifo()) {
681             ch = fifo_push(sp EVENTLIST_2nd(evl));
682             if (ch == ERR) {
683                 peek = head;    /* the keys stay uninterpreted */
684                 return ERR;
685             }
686 #ifdef NCURSES_WGETCH_EVENTS
687             else if (ch == KEY_EVENT) {
688                 peek = head;    /* the keys stay uninterpreted */
689                 return fifo_pull(sp);   /* Remove KEY_EVENT from the queue */
690             }
691 #endif
692         }
693
694         ch = fifo_peek(sp);
695         if (ch >= KEY_MIN) {
696             /* If not first in queue, somebody put this key there on purpose in
697              * emergency.  Consider it higher priority than the unfinished
698              * keysequence we are parsing.
699              */
700             peek = head;
701             /* assume the key is the last in fifo */
702             t_dec();            /* remove the key */
703             return ch;
704         }
705
706         TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
707         while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
708             ptr = ptr->sibling;
709
710         if (ptr == NULL) {
711             TR(TRACE_IEVENT, ("ptr is null"));
712             break;
713         }
714         TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
715                           (void *) ptr, ptr->ch, ptr->value));
716
717         if (ptr->value != 0) {  /* sequence terminated */
718             TR(TRACE_IEVENT, ("end of sequence"));
719             if (peek == tail)
720                 fifo_clear(sp);
721             else
722                 head = peek;
723             return (ptr->value);
724         }
725
726         ptr = ptr->child;
727
728         if (!raw_key_in_fifo()) {
729             int rc;
730
731             TR(TRACE_IEVENT, ("waiting for rest of sequence"));
732             rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
733 #ifdef NCURSES_WGETCH_EVENTS
734             if (rc & TW_EVENT) {
735                 TR(TRACE_IEVENT, ("interrupted by a user event"));
736                 /* FIXME Should have preserved remainder timeleft for reuse... */
737                 peek = head;    /* Restart interpreting later */
738                 return KEY_EVENT;
739             }
740 #endif
741             if (!rc) {
742                 TR(TRACE_IEVENT, ("ran out of time"));
743                 break;
744             }
745         }
746     }
747     ch = fifo_pull(sp);
748     peek = head;
749     return ch;
750 }