1 /****************************************************************************
2 * Copyright (c) 1998-2012,2013 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.126 2013/02/16 18:30:37 tom Exp $")
47 #include <fifo_defs.h>
50 #define GetEscdelay(sp) *_nc_ptr_Escdelay(sp)
52 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
54 return *(_nc_ptr_Escdelay(CURRENT_SCREEN));
58 _nc_ptr_Escdelay(SCREEN *sp)
60 return ptrEscdelay(sp);
63 #define GetEscdelay(sp) ESCDELAY
64 NCURSES_EXPORT_VAR(int) ESCDELAY = 1000;
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);
128 * Check for mouse activity, returning nonzero if we find any.
131 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
135 #ifdef USE_TERM_DRIVER
136 rc = TCBOf(sp)->drv->testmouse(TCBOf(sp), delay EVENTLIST_2nd(evl));
139 if ((sp->_mouse_type == M_SYSMOUSE)
140 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
145 rc = _nc_timed_wait(sp,
151 if ((sp->_mouse_type == M_SYSMOUSE)
152 && (sp->_sysmouse_head < sp->_sysmouse_tail)
154 && (errno == EINTR)) {
163 static NCURSES_INLINE int
164 fifo_peek(SCREEN *sp)
166 int ch = (peek >= 0) ? sp->_fifo[peek] : ERR;
167 TR(TRACE_IEVENT, ("peeking at %d", peek));
173 static NCURSES_INLINE int
174 fifo_pull(SCREEN *sp)
176 int ch = (head >= 0) ? sp->_fifo[head] : ERR;
178 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
188 if (USE_TRACEF(TRACE_IEVENT)) {
190 _nc_unlock_global(tracef);
196 static NCURSES_INLINE int
197 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
212 #ifdef NCURSES_WGETCH_EVENTS
214 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
215 || (sp->_mouse_fd >= 0)
218 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
222 if (mask & TW_EVENT) {
223 T(("fifo_push: ungetch KEY_EVENT"));
224 safe_ungetch(sp, KEY_EVENT);
227 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
228 if (sp->_mouse_fd >= 0) {
229 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
233 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
234 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
235 sp->_mouse_event(sp);
241 if ((sp->_mouse_type == M_SYSMOUSE)
242 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
243 sp->_mouse_event(sp);
246 } else if ((sp->_mouse_type == M_SYSMOUSE)
247 && (mask <= 0) && errno == EINTR) {
248 sp->_mouse_event(sp);
253 #ifdef USE_TERM_DRIVER
254 if ((sp->_mouse_type == M_TERM_DRIVER)
255 && (sp->_drv_mouse_head < sp->_drv_mouse_tail)) {
256 sp->_mouse_event(sp);
262 if (isatty(sp->_ifd) && sp->_cbreak) {
263 ch = _read_kbd(0, 1, !sp->_raw);
264 n = (ch == -1) ? -1 : 1;
265 sp->_extended_key = (ch == 0);
269 #ifdef USE_TERM_DRIVER
271 n = CallDriver_1(sp, read, &buf);
274 unsigned char c2 = 0;
275 # if USE_PTHREADS_EINTR
276 # if USE_WEAK_SYMBOLS
277 if ((pthread_self) && (pthread_kill) && (pthread_equal))
279 _nc_globals.read_thread = pthread_self();
281 n = (int) read(sp->_ifd, &c2, (size_t) 1);
282 #if USE_PTHREADS_EINTR
283 _nc_globals.read_thread = 0;
291 * Under System V curses with non-restarting signals, getch() returns
292 * with value ERR when a handled signal keeps it from completing.
293 * If signals restart system calls, OTOH, the signal is invisible
294 * except to its handler.
296 * We don't want this difference to show. This piece of code
297 * tries to make it look like we always have restarting signals.
299 if (n <= 0 && errno == EINTR
300 # if USE_PTHREADS_EINTR
301 && (_nc_globals.have_sigwinch == 0)
307 if ((n == -1) || (n == 0)) {
308 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
311 TR(TRACE_IEVENT, ("read %d characters", n));
313 sp->_fifo[tail] = ch;
318 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
320 if (USE_TRACEF(TRACE_IEVENT)) {
322 _nc_unlock_global(tracef);
328 static NCURSES_INLINE void
329 fifo_clear(SCREEN *sp)
331 memset(sp->_fifo, 0, sizeof(sp->_fifo));
336 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
339 recur_wrefresh(WINDOW *win)
342 SCREEN *sp = _nc_screen_of(win);
343 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
346 /* temporarily switch to the window's screen to check/refresh */
347 _nc_lock_global(curses);
348 save_SP = CURRENT_SCREEN;
351 _nc_set_screen(save_SP);
352 _nc_unlock_global(curses);
355 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
356 && !(win->_flags & _ISPAD)) {
362 recur_wgetnstr(WINDOW *win, char *buf)
364 SCREEN *sp = _nc_screen_of(win);
369 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
372 /* temporarily switch to the window's screen to get cooked input */
373 _nc_lock_global(curses);
374 save_SP = CURRENT_SCREEN;
376 rc = recur_wgetnstr(win, buf);
377 _nc_set_screen(save_SP);
378 _nc_unlock_global(curses);
382 sp->_called_wgetch = TRUE;
383 rc = wgetnstr(win, buf, MAXCOLUMNS);
384 sp->_called_wgetch = FALSE;
393 _nc_wgetch(WINDOW *win,
396 EVENTLIST_2nd(_nc_eventlist * evl))
401 #ifdef NCURSES_WGETCH_EVENTS
402 long event_delay = -1;
405 T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
409 sp = _nc_screen_of(win);
410 if (win == 0 || sp == 0) {
414 if (cooked_key_in_fifo()) {
416 *result = fifo_pull(sp);
417 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
419 #ifdef NCURSES_WGETCH_EVENTS
420 if (evl && (evl->count == 0))
422 event_delay = _nc_eventlist_timeout(evl);
426 * Handle cooked mode. Grab a string from the screen,
427 * stuff its contents in the FIFO queue, and pop off
428 * the first character to return it.
434 !sp->_called_wgetch) {
435 char buf[MAXCOLUMNS], *bufp;
437 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
439 /* ungetch in reverse order */
440 #ifdef NCURSES_WGETCH_EVENTS
441 rc = recur_wgetnstr(win, buf);
442 if (rc != KEY_EVENT && rc != ERR)
443 safe_ungetch(sp, '\n');
445 if (recur_wgetnstr(win, buf) != ERR)
446 safe_ungetch(sp, '\n');
448 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
449 safe_ungetch(sp, bufp[-1]);
451 #ifdef NCURSES_WGETCH_EVENTS
452 /* Return it first */
453 if (rc == KEY_EVENT) {
457 *result = fifo_pull(sp);
458 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
461 if (win->_use_keypad != sp->_keypad_on)
462 _nc_keypad(sp, win->_use_keypad);
466 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
467 if (head == -1) { /* fifo is empty */
470 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
472 delay = (sp->_cbreak - 1) * 100;
476 #ifdef NCURSES_WGETCH_EVENTS
477 if (event_delay >= 0 && delay > event_delay)
481 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
483 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
485 #ifdef NCURSES_WGETCH_EVENTS
488 returnCode(KEY_CODE_YES);
495 /* else go on to read data available */
498 if (win->_use_keypad) {
500 * This is tricky. We only want to get special-key
501 * events one at a time. But we want to accumulate
502 * mouse events until either (a) the mouse logic tells
503 * us it's picked up a complete gesture, or (b)
504 * there's a detectable time lapse after one.
506 * Note: if the mouse code starts failing to compose
507 * press/release events into clicks, you should probably
508 * increase the wait with mouseinterval().
513 ch = kgetch(sp EVENTLIST_2nd(evl));
514 if (ch == KEY_MOUSE) {
516 if (sp->_mouse_inline(sp))
519 if (sp->_maxclick < 0)
523 && (((rc = check_mouse_activity(sp, sp->_maxclick
524 EVENTLIST_2nd(evl))) != 0
526 || !sp->_mouse_parse(sp, runcount)));
527 #ifdef NCURSES_WGETCH_EVENTS
528 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
529 safe_ungetch(sp, ch);
533 if (runcount > 0 && ch != KEY_MOUSE) {
534 #ifdef NCURSES_WGETCH_EVENTS
535 /* mouse event sequence ended by an event, report event */
536 if (ch == KEY_EVENT) {
537 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
541 /* mouse event sequence ended by keystroke, store keystroke */
542 safe_ungetch(sp, ch);
548 fifo_push(sp EVENTLIST_2nd(evl));
555 if (_nc_handle_sigwinch(sp)) {
556 _nc_update_screensize(sp);
557 /* resizeterm can push KEY_RESIZE */
558 if (cooked_key_in_fifo()) {
559 *result = fifo_pull(sp);
561 * Get the ERR from queue -- it is from WINCH,
562 * so we should take it out, the "error" is handled.
564 if (fifo_peek(sp) == -1)
566 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
574 * If echo() is in effect, display the printable version of the
575 * key on the screen. Carriage return and backspace are treated
576 * specially by Solaris curses:
578 * If carriage return is defined as a function key in the
579 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
580 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
581 * We echo before translating carriage return based on nonl(),
582 * since the visual result simply moves the cursor to column 0.
584 * Backspace is a different matter. Solaris curses does not
585 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
586 * on the stty modes, but appears to be a hardcoded special case.
587 * This is a difference from ncurses, which uses the terminfo entry.
588 * However, we provide the same visual result as Solaris, moving the
589 * cursor to the left.
591 if (sp->_echo && !(win->_flags & _ISPAD)) {
592 chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
593 if (backup < KEY_MIN)
594 wechochar(win, backup);
598 * Simulate ICRNL mode
600 if ((ch == '\r') && sp->_nl)
603 /* Strip 8th-bit if so desired. We do this only for characters that
604 * are in the range 128-255, to provide compatibility with terminals
605 * that display only 7-bit characters. Note that 'ch' may be a
606 * function key at this point, so we mustn't strip _those_.
609 if ((ch < KEY_MIN) && (ch & 0x80))
612 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
615 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
618 #ifdef NCURSES_WGETCH_EVENTS
620 wgetch_events(WINDOW *win, _nc_eventlist * evl)
625 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
626 code = _nc_wgetch(win,
642 T((T_CALLED("wgetch(%p)"), (void *) win));
643 code = _nc_wgetch(win,
646 EVENTLIST_2nd((_nc_eventlist *) 0));
656 ** Get an input character, but take care of keypad sequences, returning
657 ** an appropriate code when one matches the input. After each character
658 ** is received, set an alarm call based on ESCDELAY. If no more of the
659 ** sequence is received by the time the alarm goes off, pass through
660 ** the sequence gotten so far.
662 ** This function must be called when there are no cooked keys in queue.
663 ** (that is head==-1 || peek==head)
668 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
672 int timeleft = GetEscdelay(sp);
674 TR(TRACE_IEVENT, ("kgetch() called"));
679 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
681 } else if (!raw_key_in_fifo()) {
682 ch = fifo_push(sp EVENTLIST_2nd(evl));
684 peek = head; /* the keys stay uninterpreted */
687 #ifdef NCURSES_WGETCH_EVENTS
688 else if (ch == KEY_EVENT) {
689 peek = head; /* the keys stay uninterpreted */
690 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
697 /* If not first in queue, somebody put this key there on purpose in
698 * emergency. Consider it higher priority than the unfinished
699 * keysequence we are parsing.
702 /* assume the key is the last in fifo */
703 t_dec(); /* remove the key */
707 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
708 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
712 TR(TRACE_IEVENT, ("ptr is null"));
715 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
716 (void *) ptr, ptr->ch, ptr->value));
718 if (ptr->value != 0) { /* sequence terminated */
719 TR(TRACE_IEVENT, ("end of sequence"));
730 if (!raw_key_in_fifo()) {
733 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
734 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
735 #ifdef NCURSES_WGETCH_EVENTS
737 TR(TRACE_IEVENT, ("interrupted by a user event"));
738 /* FIXME Should have preserved remainder timeleft for reuse... */
739 peek = head; /* Restart interpreting later */
744 TR(TRACE_IEVENT, ("ran out of time"));