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