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.103 2009/05/30 15:50:47 tom Exp $")
47 #include <fifo_defs.h>
50 #define GetEscdelay(sp) (sp)->_ESCDELAY
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)
74 SP_PARM->_ESCDELAY = value;
87 set_escdelay(int value)
89 return NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
92 #endif /* NCURSES_EXT_FUNCS */
95 _nc_use_meta(WINDOW *win)
97 SCREEN *sp = _nc_screen_of(win);
98 return (sp ? sp->_use_meta : 0);
101 #ifdef NCURSES_WGETCH_EVENTS
102 #define TWAIT_MASK (TW_ANY | TW_EVENT)
104 #define TWAIT_MASK TW_ANY
108 * Check for mouse activity, returning nonzero if we find any.
111 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
116 if ((sp->_mouse_type == M_SYSMOUSE)
117 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
121 rc = _nc_timed_wait(sp, TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
123 if ((sp->_mouse_type == M_SYSMOUSE)
124 && (sp->_sysmouse_head < sp->_sysmouse_tail)
126 && (errno == EINTR)) {
133 static NCURSES_INLINE int
134 fifo_peek(SCREEN *sp)
136 int ch = sp->_fifo[peek];
137 TR(TRACE_IEVENT, ("peeking at %d", peek));
143 static NCURSES_INLINE int
144 fifo_pull(SCREEN *sp)
147 ch = sp->_fifo[head];
148 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
157 if (USE_TRACEF(TRACE_IEVENT)) {
159 _nc_unlock_global(tracef);
165 static NCURSES_INLINE int
166 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
181 #ifdef NCURSES_WGETCH_EVENTS
183 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
184 || (sp->_mouse_fd >= 0)
187 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
191 if (mask & TW_EVENT) {
192 T(("fifo_push: ungetch KEY_EVENT"));
193 safe_ungetch(sp, KEY_EVENT);
196 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
197 if (sp->_mouse_fd >= 0) {
198 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
202 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
203 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
204 sp->_mouse_event(sp);
210 if ((sp->_mouse_type == M_SYSMOUSE)
211 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
212 sp->_mouse_event(sp);
215 } else if ((sp->_mouse_type == M_SYSMOUSE)
216 && (mask <= 0) && errno == EINTR) {
217 sp->_mouse_event(sp);
223 unsigned char c2 = 0;
224 n = read(sp->_ifd, &c2, 1);
230 * Under System V curses with non-restarting signals, getch() returns
231 * with value ERR when a handled signal keeps it from completing.
232 * If signals restart system calls, OTOH, the signal is invisible
233 * except to its handler.
235 * We don't want this difference to show. This piece of code
236 * tries to make it look like we always have restarting signals.
238 if (n <= 0 && errno == EINTR)
242 if ((n == -1) || (n == 0)) {
243 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
246 TR(TRACE_IEVENT, ("read %d characters", n));
248 sp->_fifo[tail] = ch;
253 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
255 if (USE_TRACEF(TRACE_IEVENT)) {
257 _nc_unlock_global(tracef);
263 static NCURSES_INLINE void
264 fifo_clear(SCREEN *sp)
266 memset(sp->_fifo, 0, sizeof(sp->_fifo));
271 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
274 recur_wrefresh(WINDOW *win)
277 SCREEN *sp = _nc_screen_of(win);
278 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
281 /* temporarily switch to the window's screen to check/refresh */
282 _nc_lock_global(curses);
283 save_SP = CURRENT_SCREEN;
286 _nc_set_screen(save_SP);
287 _nc_unlock_global(curses);
290 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
291 && !(win->_flags & _ISPAD)) {
297 recur_wgetnstr(WINDOW *win, char *buf)
299 SCREEN *sp = _nc_screen_of(win);
304 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
307 /* temporarily switch to the window's screen to get cooked input */
308 _nc_lock_global(curses);
309 save_SP = CURRENT_SCREEN;
311 rc = recur_wgetnstr(win, buf);
312 _nc_set_screen(save_SP);
313 _nc_unlock_global(curses);
317 sp->_called_wgetch = TRUE;
318 rc = wgetnstr(win, buf, MAXCOLUMNS);
319 sp->_called_wgetch = FALSE;
328 _nc_wgetch(WINDOW *win,
329 unsigned long *result,
331 EVENTLIST_2nd(_nc_eventlist * evl))
335 #ifdef NCURSES_WGETCH_EVENTS
336 long event_delay = -1;
339 T((T_CALLED("_nc_wgetch(%p)"), win));
343 sp = _nc_screen_of(win);
344 if (win == 0 || sp == 0) {
348 if (cooked_key_in_fifo()) {
350 *result = fifo_pull(sp);
351 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
353 #ifdef NCURSES_WGETCH_EVENTS
354 if (evl && (evl->count == 0))
356 event_delay = _nc_eventlist_timeout(evl);
360 * Handle cooked mode. Grab a string from the screen,
361 * stuff its contents in the FIFO queue, and pop off
362 * the first character to return it.
368 !sp->_called_wgetch) {
369 char buf[MAXCOLUMNS], *bufp;
372 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
374 rc = recur_wgetnstr(win, buf);
376 /* ungetch in reverse order */
377 #ifdef NCURSES_WGETCH_EVENTS
380 safe_ungetch(sp, '\n');
381 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
382 safe_ungetch(sp, bufp[-1]);
384 #ifdef NCURSES_WGETCH_EVENTS
385 /* Return it first */
386 if (rc == KEY_EVENT) {
390 *result = fifo_pull(sp);
391 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
394 if (win->_use_keypad != sp->_keypad_on)
395 _nc_keypad(sp, win->_use_keypad);
399 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
400 if (head == -1) { /* fifo is empty */
404 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
406 delay = (sp->_cbreak - 1) * 100;
410 #ifdef NCURSES_WGETCH_EVENTS
411 if (event_delay >= 0 && delay > event_delay)
415 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
417 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
419 #ifdef NCURSES_WGETCH_EVENTS
422 returnCode(KEY_CODE_YES);
429 /* else go on to read data available */
432 if (win->_use_keypad) {
434 * This is tricky. We only want to get special-key
435 * events one at a time. But we want to accumulate
436 * mouse events until either (a) the mouse logic tells
437 * us it's picked up a complete gesture, or (b)
438 * there's a detectable time lapse after one.
440 * Note: if the mouse code starts failing to compose
441 * press/release events into clicks, you should probably
442 * increase the wait with mouseinterval().
448 ch = kgetch(sp EVENTLIST_2nd(evl));
449 if (ch == KEY_MOUSE) {
451 if (sp->_mouse_inline(sp))
454 if (sp->_maxclick < 0)
458 && (((rc = check_mouse_activity(sp, sp->_maxclick
459 EVENTLIST_2nd(evl))) != 0
461 || !sp->_mouse_parse(sp, runcount)));
462 #ifdef NCURSES_WGETCH_EVENTS
463 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
464 safe_ungetch(sp, ch);
468 if (runcount > 0 && ch != KEY_MOUSE) {
469 #ifdef NCURSES_WGETCH_EVENTS
470 /* mouse event sequence ended by an event, report event */
471 if (ch == KEY_EVENT) {
472 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
476 /* mouse event sequence ended by keystroke, store keystroke */
477 safe_ungetch(sp, ch);
483 fifo_push(sp EVENTLIST_2nd(evl));
489 if (_nc_handle_sigwinch(sp)) {
490 _nc_update_screensize(sp);
491 /* resizeterm can push KEY_RESIZE */
492 if (cooked_key_in_fifo()) {
493 *result = fifo_pull(sp);
494 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
502 * If echo() is in effect, display the printable version of the
503 * key on the screen. Carriage return and backspace are treated
504 * specially by Solaris curses:
506 * If carriage return is defined as a function key in the
507 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
508 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
509 * We echo before translating carriage return based on nonl(),
510 * since the visual result simply moves the cursor to column 0.
512 * Backspace is a different matter. Solaris curses does not
513 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
514 * on the stty modes, but appears to be a hardcoded special case.
515 * This is a difference from ncurses, which uses the terminfo entry.
516 * However, we provide the same visual result as Solaris, moving the
517 * cursor to the left.
519 if (sp->_echo && !(win->_flags & _ISPAD)) {
520 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
521 if (backup < KEY_MIN)
522 wechochar(win, backup);
526 * Simulate ICRNL mode
528 if ((ch == '\r') && sp->_nl)
531 /* Strip 8th-bit if so desired. We do this only for characters that
532 * are in the range 128-255, to provide compatibility with terminals
533 * that display only 7-bit characters. Note that 'ch' may be a
534 * function key at this point, so we mustn't strip _those_.
537 if ((ch < KEY_MIN) && (ch & 0x80))
540 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
543 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
546 #ifdef NCURSES_WGETCH_EVENTS
548 wgetch_events(WINDOW *win, _nc_eventlist * evl)
553 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
554 code = _nc_wgetch(win,
570 T((T_CALLED("wgetch(%p)"), win));
571 code = _nc_wgetch(win,
574 EVENTLIST_2nd((_nc_eventlist *) 0));
584 ** Get an input character, but take care of keypad sequences, returning
585 ** an appropriate code when one matches the input. After each character
586 ** is received, set an alarm call based on ESCDELAY. If no more of the
587 ** sequence is received by the time the alarm goes off, pass through
588 ** the sequence gotten so far.
590 ** This function must be called when there are no cooked keys in queue.
591 ** (that is head==-1 || peek==head)
596 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
600 int timeleft = GetEscdelay(sp);
602 TR(TRACE_IEVENT, ("kgetch() called"));
607 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
609 } else if (!raw_key_in_fifo()) {
610 ch = fifo_push(sp EVENTLIST_2nd(evl));
612 peek = head; /* the keys stay uninterpreted */
615 #ifdef NCURSES_WGETCH_EVENTS
616 else if (ch == KEY_EVENT) {
617 peek = head; /* the keys stay uninterpreted */
618 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
625 /* If not first in queue, somebody put this key there on purpose in
626 * emergency. Consider it higher priority than the unfinished
627 * keysequence we are parsing.
630 /* assume the key is the last in fifo */
631 t_dec(); /* remove the key */
635 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
636 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
640 TR(TRACE_IEVENT, ("ptr is null"));
643 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
644 ptr, ptr->ch, ptr->value));
646 if (ptr->value != 0) { /* sequence terminated */
647 TR(TRACE_IEVENT, ("end of sequence"));
657 if (!raw_key_in_fifo()) {
660 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
661 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
662 #ifdef NCURSES_WGETCH_EVENTS
664 TR(TRACE_IEVENT, ("interrupted by a user event"));
665 /* FIXME Should have preserved remainder timeleft for reuse... */
666 peek = head; /* Restart interpreting later */
671 TR(TRACE_IEVENT, ("ran out of time"));