1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
39 ** The routine getch().
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_getch.c,v 1.104 2009/07/04 20:41:13 tom Exp $")
47 #include <fifo_defs.h>
50 #define GetEscdelay(sp) *_nc_ptr_Escdelay(sp)
52 _nc_ptr_Escdelay(SCREEN *sp)
54 return ptrEscdelay(sp);
57 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
59 return *(_nc_ptr_Escdelay(CURRENT_SCREEN));
62 #define GetEscdelay(sp) ESCDELAY
63 NCURSES_EXPORT_VAR (int)
64 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
69 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_DCLx int value)
87 set_escdelay(int value)
91 code = NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
99 #endif /* NCURSES_EXT_FUNCS */
101 #if NCURSES_EXT_FUNCS
103 NCURSES_SP_NAME(get_escdelay) (NCURSES_SP_DCL0)
108 return GetEscdelay(SP_PARM);
115 return NCURSES_SP_NAME(get_escdelay)(CURRENT_SCREEN);
118 #endif /* NCURSES_EXT_FUNCS */
121 _nc_use_meta(WINDOW *win)
123 SCREEN *sp = _nc_screen_of(win);
124 return (sp ? sp->_use_meta : 0);
127 #ifdef NCURSES_WGETCH_EVENTS
128 #define TWAIT_MASK (TW_ANY | TW_EVENT)
130 #define TWAIT_MASK TW_ANY
134 * Check for mouse activity, returning nonzero if we find any.
137 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
142 if ((sp->_mouse_type == M_SYSMOUSE)
143 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
147 #ifdef USE_TERM_DRIVER
148 rc = TCBOf(sp)->drv->twait(TCBOf(sp),
154 rc = _nc_timed_wait(sp,
161 if ((sp->_mouse_type == M_SYSMOUSE)
162 && (sp->_sysmouse_head < sp->_sysmouse_tail)
164 && (errno == EINTR)) {
171 static NCURSES_INLINE int
172 fifo_peek(SCREEN *sp)
174 int ch = sp->_fifo[peek];
175 TR(TRACE_IEVENT, ("peeking at %d", peek));
181 static NCURSES_INLINE int
182 fifo_pull(SCREEN *sp)
185 ch = sp->_fifo[head];
186 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
195 if (USE_TRACEF(TRACE_IEVENT)) {
197 _nc_unlock_global(tracef);
203 static NCURSES_INLINE int
204 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
219 #ifdef NCURSES_WGETCH_EVENTS
221 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
222 || (sp->_mouse_fd >= 0)
225 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
229 if (mask & TW_EVENT) {
230 T(("fifo_push: ungetch KEY_EVENT"));
231 safe_ungetch(sp, KEY_EVENT);
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));
240 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
241 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
242 sp->_mouse_event(sp);
248 if ((sp->_mouse_type == M_SYSMOUSE)
249 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
250 sp->_mouse_event(sp);
253 } else if ((sp->_mouse_type == M_SYSMOUSE)
254 && (mask <= 0) && errno == EINTR) {
255 sp->_mouse_event(sp);
261 #ifdef USE_TERM_DRIVER
263 n = CallDriver_1(sp, read, &buf);
266 unsigned char c2 = 0;
267 n = read(sp->_ifd, &c2, 1);
274 * Under System V curses with non-restarting signals, getch() returns
275 * with value ERR when a handled signal keeps it from completing.
276 * If signals restart system calls, OTOH, the signal is invisible
277 * except to its handler.
279 * We don't want this difference to show. This piece of code
280 * tries to make it look like we always have restarting signals.
282 if (n <= 0 && errno == EINTR)
286 if ((n == -1) || (n == 0)) {
287 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
290 TR(TRACE_IEVENT, ("read %d characters", n));
292 sp->_fifo[tail] = ch;
297 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
299 if (USE_TRACEF(TRACE_IEVENT)) {
301 _nc_unlock_global(tracef);
307 static NCURSES_INLINE void
308 fifo_clear(SCREEN *sp)
310 memset(sp->_fifo, 0, sizeof(sp->_fifo));
315 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
318 recur_wrefresh(WINDOW *win)
321 SCREEN *sp = _nc_screen_of(win);
322 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
325 /* temporarily switch to the window's screen to check/refresh */
326 _nc_lock_global(curses);
327 save_SP = CURRENT_SCREEN;
330 _nc_set_screen(save_SP);
331 _nc_unlock_global(curses);
334 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
335 && !(win->_flags & _ISPAD)) {
341 recur_wgetnstr(WINDOW *win, char *buf)
343 SCREEN *sp = _nc_screen_of(win);
348 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
351 /* temporarily switch to the window's screen to get cooked input */
352 _nc_lock_global(curses);
353 save_SP = CURRENT_SCREEN;
355 rc = recur_wgetnstr(win, buf);
356 _nc_set_screen(save_SP);
357 _nc_unlock_global(curses);
361 sp->_called_wgetch = TRUE;
362 rc = wgetnstr(win, buf, MAXCOLUMNS);
363 sp->_called_wgetch = FALSE;
372 _nc_wgetch(WINDOW *win,
373 unsigned long *result,
375 EVENTLIST_2nd(_nc_eventlist * evl))
379 #ifdef NCURSES_WGETCH_EVENTS
380 long event_delay = -1;
383 T((T_CALLED("_nc_wgetch(%p)"), win));
387 sp = _nc_screen_of(win);
388 if (win == 0 || sp == 0) {
392 if (cooked_key_in_fifo()) {
394 *result = fifo_pull(sp);
395 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
397 #ifdef NCURSES_WGETCH_EVENTS
398 if (evl && (evl->count == 0))
400 event_delay = _nc_eventlist_timeout(evl);
404 * Handle cooked mode. Grab a string from the screen,
405 * stuff its contents in the FIFO queue, and pop off
406 * the first character to return it.
412 !sp->_called_wgetch) {
413 char buf[MAXCOLUMNS], *bufp;
416 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
418 rc = recur_wgetnstr(win, buf);
420 /* ungetch in reverse order */
421 #ifdef NCURSES_WGETCH_EVENTS
424 safe_ungetch(sp, '\n');
425 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
426 safe_ungetch(sp, bufp[-1]);
428 #ifdef NCURSES_WGETCH_EVENTS
429 /* Return it first */
430 if (rc == KEY_EVENT) {
434 *result = fifo_pull(sp);
435 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
438 if (win->_use_keypad != sp->_keypad_on)
439 _nc_keypad(sp, win->_use_keypad);
443 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
444 if (head == -1) { /* fifo is empty */
448 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
450 delay = (sp->_cbreak - 1) * 100;
454 #ifdef NCURSES_WGETCH_EVENTS
455 if (event_delay >= 0 && delay > event_delay)
459 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
461 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
463 #ifdef NCURSES_WGETCH_EVENTS
466 returnCode(KEY_CODE_YES);
473 /* else go on to read data available */
476 if (win->_use_keypad) {
478 * This is tricky. We only want to get special-key
479 * events one at a time. But we want to accumulate
480 * mouse events until either (a) the mouse logic tells
481 * us it's picked up a complete gesture, or (b)
482 * there's a detectable time lapse after one.
484 * Note: if the mouse code starts failing to compose
485 * press/release events into clicks, you should probably
486 * increase the wait with mouseinterval().
492 ch = kgetch(sp EVENTLIST_2nd(evl));
493 if (ch == KEY_MOUSE) {
495 if (sp->_mouse_inline(sp))
498 if (sp->_maxclick < 0)
502 && (((rc = check_mouse_activity(sp, sp->_maxclick
503 EVENTLIST_2nd(evl))) != 0
505 || !sp->_mouse_parse(sp, runcount)));
506 #ifdef NCURSES_WGETCH_EVENTS
507 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
508 safe_ungetch(sp, ch);
512 if (runcount > 0 && ch != KEY_MOUSE) {
513 #ifdef NCURSES_WGETCH_EVENTS
514 /* mouse event sequence ended by an event, report event */
515 if (ch == KEY_EVENT) {
516 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
520 /* mouse event sequence ended by keystroke, store keystroke */
521 safe_ungetch(sp, ch);
527 fifo_push(sp EVENTLIST_2nd(evl));
533 if (_nc_handle_sigwinch(sp)) {
534 _nc_update_screensize(sp);
535 /* resizeterm can push KEY_RESIZE */
536 if (cooked_key_in_fifo()) {
537 *result = fifo_pull(sp);
538 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
546 * If echo() is in effect, display the printable version of the
547 * key on the screen. Carriage return and backspace are treated
548 * specially by Solaris curses:
550 * If carriage return is defined as a function key in the
551 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
552 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
553 * We echo before translating carriage return based on nonl(),
554 * since the visual result simply moves the cursor to column 0.
556 * Backspace is a different matter. Solaris curses does not
557 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
558 * on the stty modes, but appears to be a hardcoded special case.
559 * This is a difference from ncurses, which uses the terminfo entry.
560 * However, we provide the same visual result as Solaris, moving the
561 * cursor to the left.
563 if (sp->_echo && !(win->_flags & _ISPAD)) {
564 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
565 if (backup < KEY_MIN)
566 wechochar(win, backup);
570 * Simulate ICRNL mode
572 if ((ch == '\r') && sp->_nl)
575 /* Strip 8th-bit if so desired. We do this only for characters that
576 * are in the range 128-255, to provide compatibility with terminals
577 * that display only 7-bit characters. Note that 'ch' may be a
578 * function key at this point, so we mustn't strip _those_.
581 if ((ch < KEY_MIN) && (ch & 0x80))
584 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
587 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
590 #ifdef NCURSES_WGETCH_EVENTS
592 wgetch_events(WINDOW *win, _nc_eventlist * evl)
597 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
598 code = _nc_wgetch(win,
614 T((T_CALLED("wgetch(%p)"), win));
615 code = _nc_wgetch(win,
618 EVENTLIST_2nd((_nc_eventlist *) 0));
628 ** Get an input character, but take care of keypad sequences, returning
629 ** an appropriate code when one matches the input. After each character
630 ** is received, set an alarm call based on ESCDELAY. If no more of the
631 ** sequence is received by the time the alarm goes off, pass through
632 ** the sequence gotten so far.
634 ** This function must be called when there are no cooked keys in queue.
635 ** (that is head==-1 || peek==head)
640 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
644 int timeleft = GetEscdelay(sp);
646 TR(TRACE_IEVENT, ("kgetch() called"));
651 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
653 } else if (!raw_key_in_fifo()) {
654 ch = fifo_push(sp EVENTLIST_2nd(evl));
656 peek = head; /* the keys stay uninterpreted */
659 #ifdef NCURSES_WGETCH_EVENTS
660 else if (ch == KEY_EVENT) {
661 peek = head; /* the keys stay uninterpreted */
662 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
669 /* If not first in queue, somebody put this key there on purpose in
670 * emergency. Consider it higher priority than the unfinished
671 * keysequence we are parsing.
674 /* assume the key is the last in fifo */
675 t_dec(); /* remove the key */
679 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
680 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
684 TR(TRACE_IEVENT, ("ptr is null"));
687 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
688 ptr, ptr->ch, ptr->value));
690 if (ptr->value != 0) { /* sequence terminated */
691 TR(TRACE_IEVENT, ("end of sequence"));
701 if (!raw_key_in_fifo()) {
704 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
705 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
706 #ifdef NCURSES_WGETCH_EVENTS
708 TR(TRACE_IEVENT, ("interrupted by a user event"));
709 /* FIXME Should have preserved remainder timeleft for reuse... */
710 peek = head; /* Restart interpreting later */
715 TR(TRACE_IEVENT, ("ran out of time"));