]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_getch.c
ncurses 5.7 - patch 20100515
[ncurses.git] / ncurses / base / lib_getch.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 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.118 2010/05/15 21:31:12 tom 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);
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     {                           /* Can block... */
261 #ifdef USE_TERM_DRIVER
262         int buf;
263         n = CallDriver_1(sp, read, &buf);
264         ch = buf;
265 #else
266         unsigned char c2 = 0;
267 # if USE_PTHREADS_EINTR
268         if ((pthread_self) && (pthread_kill) && (pthread_equal))
269             _nc_globals.read_thread = pthread_self();
270 # endif
271         n = read(sp->_ifd, &c2, 1);
272 #if USE_PTHREADS_EINTR
273         _nc_globals.read_thread = 0;
274 #endif
275         ch = c2;
276 #endif
277     }
278
279 #ifdef HIDE_EINTR
280     /*
281      * Under System V curses with non-restarting signals, getch() returns
282      * with value ERR when a handled signal keeps it from completing.
283      * If signals restart system calls, OTOH, the signal is invisible
284      * except to its handler.
285      *
286      * We don't want this difference to show.  This piece of code
287      * tries to make it look like we always have restarting signals.
288      */
289     if (n <= 0 && errno == EINTR
290 # if USE_PTHREADS_EINTR
291         && (_nc_globals.have_sigwinch == 0)
292 # endif
293         )
294         goto again;
295 #endif
296
297     if ((n == -1) || (n == 0)) {
298         TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
299         ch = ERR;
300     }
301     TR(TRACE_IEVENT, ("read %d characters", n));
302
303     sp->_fifo[tail] = ch;
304     sp->_fifohold = 0;
305     if (head == -1)
306         head = peek = tail;
307     t_inc();
308     TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
309 #ifdef TRACE
310     if (USE_TRACEF(TRACE_IEVENT)) {
311         _nc_fifo_dump(sp);
312         _nc_unlock_global(tracef);
313     }
314 #endif
315     return ch;
316 }
317
318 static NCURSES_INLINE void
319 fifo_clear(SCREEN *sp)
320 {
321     memset(sp->_fifo, 0, sizeof(sp->_fifo));
322     head = -1;
323     tail = peek = 0;
324 }
325
326 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
327
328 static void
329 recur_wrefresh(WINDOW *win)
330 {
331 #ifdef USE_PTHREADS
332     SCREEN *sp = _nc_screen_of(win);
333     if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
334         SCREEN *save_SP;
335
336         /* temporarily switch to the window's screen to check/refresh */
337         _nc_lock_global(curses);
338         save_SP = CURRENT_SCREEN;
339         _nc_set_screen(sp);
340         recur_wrefresh(win);
341         _nc_set_screen(save_SP);
342         _nc_unlock_global(curses);
343     } else
344 #endif
345         if ((is_wintouched(win) || (win->_flags & _HASMOVED))
346             && !(win->_flags & _ISPAD)) {
347         wrefresh(win);
348     }
349 }
350
351 static int
352 recur_wgetnstr(WINDOW *win, char *buf)
353 {
354     SCREEN *sp = _nc_screen_of(win);
355     int rc;
356
357     if (sp != 0) {
358 #ifdef USE_PTHREADS
359         if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
360             SCREEN *save_SP;
361
362             /* temporarily switch to the window's screen to get cooked input */
363             _nc_lock_global(curses);
364             save_SP = CURRENT_SCREEN;
365             _nc_set_screen(sp);
366             rc = recur_wgetnstr(win, buf);
367             _nc_set_screen(save_SP);
368             _nc_unlock_global(curses);
369         } else
370 #endif
371         {
372             sp->_called_wgetch = TRUE;
373             rc = wgetnstr(win, buf, MAXCOLUMNS);
374             sp->_called_wgetch = FALSE;
375         }
376     } else {
377         rc = ERR;
378     }
379     return rc;
380 }
381
382 NCURSES_EXPORT(int)
383 _nc_wgetch(WINDOW *win,
384            unsigned long *result,
385            int use_meta
386            EVENTLIST_2nd(_nc_eventlist * evl))
387 {
388     SCREEN *sp;
389     int ch;
390     int rc = 0;
391 #ifdef NCURSES_WGETCH_EVENTS
392     long event_delay = -1;
393 #endif
394
395     T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
396
397     *result = 0;
398
399     sp = _nc_screen_of(win);
400     if (win == 0 || sp == 0) {
401         returnCode(ERR);
402     }
403
404     if (cooked_key_in_fifo()) {
405         recur_wrefresh(win);
406         *result = fifo_pull(sp);
407         returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
408     }
409 #ifdef NCURSES_WGETCH_EVENTS
410     if (evl && (evl->count == 0))
411         evl = NULL;
412     event_delay = _nc_eventlist_timeout(evl);
413 #endif
414
415     /*
416      * Handle cooked mode.  Grab a string from the screen,
417      * stuff its contents in the FIFO queue, and pop off
418      * the first character to return it.
419      */
420     if (head == -1 &&
421         !sp->_notty &&
422         !sp->_raw &&
423         !sp->_cbreak &&
424         !sp->_called_wgetch) {
425         char buf[MAXCOLUMNS], *bufp;
426
427         TR(TRACE_IEVENT, ("filling queue in cooked mode"));
428
429         /* ungetch in reverse order */
430 #ifdef NCURSES_WGETCH_EVENTS
431         rc = recur_wgetnstr(win, buf);
432         if (rc != KEY_EVENT)
433             safe_ungetch(sp, '\n');
434 #else
435         (void) recur_wgetnstr(win, buf);
436         safe_ungetch(sp, '\n');
437 #endif
438         for (bufp = buf + strlen(buf); bufp > buf; bufp--)
439             safe_ungetch(sp, bufp[-1]);
440
441 #ifdef NCURSES_WGETCH_EVENTS
442         /* Return it first */
443         if (rc == KEY_EVENT) {
444             *result = rc;
445         } else
446 #endif
447             *result = fifo_pull(sp);
448         returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
449     }
450
451     if (win->_use_keypad != sp->_keypad_on)
452         _nc_keypad(sp, win->_use_keypad);
453
454     recur_wrefresh(win);
455
456     if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
457         if (head == -1) {       /* fifo is empty */
458             int delay;
459
460             TR(TRACE_IEVENT, ("timed delay in wgetch()"));
461             if (sp->_cbreak > 1)
462                 delay = (sp->_cbreak - 1) * 100;
463             else
464                 delay = win->_delay;
465
466 #ifdef NCURSES_WGETCH_EVENTS
467             if (event_delay >= 0 && delay > event_delay)
468                 delay = event_delay;
469 #endif
470
471             TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
472
473             rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
474
475 #ifdef NCURSES_WGETCH_EVENTS
476             if (rc & TW_EVENT) {
477                 *result = KEY_EVENT;
478                 returnCode(KEY_CODE_YES);
479             }
480 #endif
481             if (!rc) {
482                 goto check_sigwinch;
483             }
484         }
485         /* else go on to read data available */
486     }
487
488     if (win->_use_keypad) {
489         /*
490          * This is tricky.  We only want to get special-key
491          * events one at a time.  But we want to accumulate
492          * mouse events until either (a) the mouse logic tells
493          * us it's picked up a complete gesture, or (b)
494          * there's a detectable time lapse after one.
495          *
496          * Note: if the mouse code starts failing to compose
497          * press/release events into clicks, you should probably
498          * increase the wait with mouseinterval().
499          */
500         int runcount = 0;
501
502         do {
503             ch = kgetch(sp EVENTLIST_2nd(evl));
504             if (ch == KEY_MOUSE) {
505                 ++runcount;
506                 if (sp->_mouse_inline(sp))
507                     break;
508             }
509             if (sp->_maxclick < 0)
510                 break;
511         } while
512             (ch == KEY_MOUSE
513              && (((rc = check_mouse_activity(sp, sp->_maxclick
514                                              EVENTLIST_2nd(evl))) != 0
515                   && !(rc & TW_EVENT))
516                  || !sp->_mouse_parse(sp, runcount)));
517 #ifdef NCURSES_WGETCH_EVENTS
518         if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
519             safe_ungetch(sp, ch);
520             ch = KEY_EVENT;
521         }
522 #endif
523         if (runcount > 0 && ch != KEY_MOUSE) {
524 #ifdef NCURSES_WGETCH_EVENTS
525             /* mouse event sequence ended by an event, report event */
526             if (ch == KEY_EVENT) {
527                 safe_ungetch(sp, KEY_MOUSE);    /* FIXME This interrupts a gesture... */
528             } else
529 #endif
530             {
531                 /* mouse event sequence ended by keystroke, store keystroke */
532                 safe_ungetch(sp, ch);
533                 ch = KEY_MOUSE;
534             }
535         }
536     } else {
537         if (head == -1)
538             fifo_push(sp EVENTLIST_2nd(evl));
539         ch = fifo_pull(sp);
540     }
541
542     if (ch == ERR) {
543       check_sigwinch:
544 #if USE_SIZECHANGE
545         if (_nc_handle_sigwinch(sp)) {
546             _nc_update_screensize(sp);
547             /* resizeterm can push KEY_RESIZE */
548             if (cooked_key_in_fifo()) {
549                 *result = fifo_pull(sp);
550                 /*
551                  * Get the ERR from queue -- it is from WINCH,
552                  * so we should take it out, the "error" is handled.
553                  */
554                 if (fifo_peek(sp) == -1)
555                     fifo_pull(sp);
556                 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
557             }
558         }
559 #endif
560         returnCode(ERR);
561     }
562
563     /*
564      * If echo() is in effect, display the printable version of the
565      * key on the screen.  Carriage return and backspace are treated
566      * specially by Solaris curses:
567      *
568      * If carriage return is defined as a function key in the
569      * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
570      * if nonl() is set) or KEY_ENTER depending on the echo() mode. 
571      * We echo before translating carriage return based on nonl(),
572      * since the visual result simply moves the cursor to column 0.
573      *
574      * Backspace is a different matter.  Solaris curses does not
575      * translate it to KEY_BACKSPACE if kbs=^H.  This does not depend
576      * on the stty modes, but appears to be a hardcoded special case.
577      * This is a difference from ncurses, which uses the terminfo entry.
578      * However, we provide the same visual result as Solaris, moving the
579      * cursor to the left.
580      */
581     if (sp->_echo && !(win->_flags & _ISPAD)) {
582         chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
583         if (backup < KEY_MIN)
584             wechochar(win, backup);
585     }
586
587     /*
588      * Simulate ICRNL mode
589      */
590     if ((ch == '\r') && sp->_nl)
591         ch = '\n';
592
593     /* Strip 8th-bit if so desired.  We do this only for characters that
594      * are in the range 128-255, to provide compatibility with terminals
595      * that display only 7-bit characters.  Note that 'ch' may be a
596      * function key at this point, so we mustn't strip _those_.
597      */
598     if (!use_meta)
599         if ((ch < KEY_MIN) && (ch & 0x80))
600             ch &= 0x7f;
601
602     T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
603
604     *result = ch;
605     returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
606 }
607
608 #ifdef NCURSES_WGETCH_EVENTS
609 NCURSES_EXPORT(int)
610 wgetch_events(WINDOW *win, _nc_eventlist * evl)
611 {
612     int code;
613     unsigned long value;
614
615     T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
616     code = _nc_wgetch(win,
617                       &value,
618                       _nc_use_meta(win)
619                       EVENTLIST_2nd(evl));
620     if (code != ERR)
621         code = value;
622     returnCode(code);
623 }
624 #endif
625
626 NCURSES_EXPORT(int)
627 wgetch(WINDOW *win)
628 {
629     int code;
630     unsigned long value;
631
632     T((T_CALLED("wgetch(%p)"), (void *) win));
633     code = _nc_wgetch(win,
634                       &value,
635                       _nc_use_meta(win)
636                       EVENTLIST_2nd((_nc_eventlist *) 0));
637     if (code != ERR)
638         code = value;
639     returnCode(code);
640 }
641
642 /*
643 **      int
644 **      kgetch()
645 **
646 **      Get an input character, but take care of keypad sequences, returning
647 **      an appropriate code when one matches the input.  After each character
648 **      is received, set an alarm call based on ESCDELAY.  If no more of the
649 **      sequence is received by the time the alarm goes off, pass through
650 **      the sequence gotten so far.
651 **
652 **      This function must be called when there are no cooked keys in queue.
653 **      (that is head==-1 || peek==head)
654 **
655 */
656
657 static int
658 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
659 {
660     TRIES *ptr;
661     int ch = 0;
662     int timeleft = GetEscdelay(sp);
663
664     TR(TRACE_IEVENT, ("kgetch() called"));
665
666     ptr = sp->_keytry;
667
668     for (;;) {
669         if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
670             break;
671         } else if (!raw_key_in_fifo()) {
672             ch = fifo_push(sp EVENTLIST_2nd(evl));
673             if (ch == ERR) {
674                 peek = head;    /* the keys stay uninterpreted */
675                 return ERR;
676             }
677 #ifdef NCURSES_WGETCH_EVENTS
678             else if (ch == KEY_EVENT) {
679                 peek = head;    /* the keys stay uninterpreted */
680                 return fifo_pull(sp);   /* Remove KEY_EVENT from the queue */
681             }
682 #endif
683         }
684
685         ch = fifo_peek(sp);
686         if (ch >= KEY_MIN) {
687             /* If not first in queue, somebody put this key there on purpose in
688              * emergency.  Consider it higher priority than the unfinished
689              * keysequence we are parsing.
690              */
691             peek = head;
692             /* assume the key is the last in fifo */
693             t_dec();            /* remove the key */
694             return ch;
695         }
696
697         TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
698         while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
699             ptr = ptr->sibling;
700
701         if (ptr == NULL) {
702             TR(TRACE_IEVENT, ("ptr is null"));
703             break;
704         }
705         TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
706                           (void *) ptr, ptr->ch, ptr->value));
707
708         if (ptr->value != 0) {  /* sequence terminated */
709             TR(TRACE_IEVENT, ("end of sequence"));
710             if (peek == tail)
711                 fifo_clear(sp);
712             else
713                 head = peek;
714             return (ptr->value);
715         }
716
717         ptr = ptr->child;
718
719         if (!raw_key_in_fifo()) {
720             int rc;
721
722             TR(TRACE_IEVENT, ("waiting for rest of sequence"));
723             rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
724 #ifdef NCURSES_WGETCH_EVENTS
725             if (rc & TW_EVENT) {
726                 TR(TRACE_IEVENT, ("interrupted by a user event"));
727                 /* FIXME Should have preserved remainder timeleft for reuse... */
728                 peek = head;    /* Restart interpreting later */
729                 return KEY_EVENT;
730             }
731 #endif
732             if (!rc) {
733                 TR(TRACE_IEVENT, ("ran out of time"));
734                 break;
735             }
736         }
737     }
738     ch = fifo_pull(sp);
739     peek = head;
740     return ch;
741 }