1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 ****************************************************************************/
38 ** The routine getch().
42 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_getch.c,v 1.99 2008/09/20 19:46:13 tom Exp $")
46 #include <fifo_defs.h>
49 #define GetEscdelay(sp) (sp)->_ESCDELAY
51 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
53 return SP ? GetEscdelay(SP) : 1000;
56 #define GetEscdelay(sp) ESCDELAY
57 NCURSES_EXPORT_VAR(int)
58 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
63 set_escdelay(int value)
68 SP->_ESCDELAY = value;
80 _nc_use_meta(WINDOW *win)
82 SCREEN *sp = _nc_screen_of(win);
83 return (sp ? sp->_use_meta : 0);
86 #ifdef NCURSES_WGETCH_EVENTS
93 * Check for mouse activity, returning nonzero if we find any.
96 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
101 if ((sp->_mouse_type == M_SYSMOUSE)
102 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
106 rc = _nc_timed_wait(sp, TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
108 if ((sp->_mouse_type == M_SYSMOUSE)
109 && (sp->_sysmouse_head < sp->_sysmouse_tail)
111 && (errno == EINTR)) {
118 static NCURSES_INLINE int
119 fifo_peek(SCREEN *sp)
121 int ch = sp->_fifo[peek];
122 TR(TRACE_IEVENT, ("peeking at %d", peek));
128 static NCURSES_INLINE int
129 fifo_pull(SCREEN *sp)
132 ch = sp->_fifo[head];
133 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
142 if (USE_TRACEF(TRACE_IEVENT)) {
144 _nc_unlock_global(tracef);
150 static NCURSES_INLINE int
151 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
166 #ifdef NCURSES_WGETCH_EVENTS
168 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
169 || (sp->_mouse_fd >= 0)
172 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
177 T(("fifo_push: ungetch KEY_EVENT"));
178 _nc_ungetch(sp, KEY_EVENT);
181 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
182 if (sp->_mouse_fd >= 0) {
183 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
187 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
188 if ((sp->_mouse_fd >= 0) && (mask & 2)) {
189 sp->_mouse_event(sp);
195 if ((sp->_mouse_type == M_SYSMOUSE)
196 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
197 sp->_mouse_event(sp);
200 } else if ((sp->_mouse_type == M_SYSMOUSE)
201 && (mask <= 0) && errno == EINTR) {
202 sp->_mouse_event(sp);
208 unsigned char c2 = 0;
209 n = read(sp->_ifd, &c2, 1);
215 * Under System V curses with non-restarting signals, getch() returns
216 * with value ERR when a handled signal keeps it from completing.
217 * If signals restart system calls, OTOH, the signal is invisible
218 * except to its handler.
220 * We don't want this difference to show. This piece of code
221 * tries to make it look like we always have restarting signals.
223 if (n <= 0 && errno == EINTR)
227 if ((n == -1) || (n == 0)) {
228 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
231 TR(TRACE_IEVENT, ("read %d characters", n));
233 sp->_fifo[tail] = ch;
238 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
240 if (USE_TRACEF(TRACE_IEVENT)) {
242 _nc_unlock_global(tracef);
248 static NCURSES_INLINE void
249 fifo_clear(SCREEN *sp)
251 memset(sp->_fifo, 0, sizeof(sp->_fifo));
256 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
259 recur_wrefresh(WINDOW *win)
262 SCREEN *sp = _nc_screen_of(win);
263 if (_nc_use_pthreads && sp != SP) {
266 /* temporarily switch to the window's screen to check/refresh */
267 _nc_lock_global(curses);
271 _nc_set_screen(save_SP);
272 _nc_unlock_global(curses);
275 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
276 && !(win->_flags & _ISPAD)) {
282 recur_wgetnstr(WINDOW *win, char *buf)
284 SCREEN *sp = _nc_screen_of(win);
289 if (_nc_use_pthreads && sp != SP) {
292 /* temporarily switch to the window's screen to get cooked input */
293 _nc_lock_global(curses);
296 rc = recur_wgetnstr(win, buf);
297 _nc_set_screen(save_SP);
298 _nc_unlock_global(curses);
302 sp->_called_wgetch = TRUE;
303 rc = wgetnstr(win, buf, MAXCOLUMNS);
304 sp->_called_wgetch = FALSE;
313 _nc_wgetch(WINDOW *win,
314 unsigned long *result,
316 EVENTLIST_2nd(_nc_eventlist * evl))
320 #ifdef NCURSES_WGETCH_EVENTS
321 long event_delay = -1;
324 T((T_CALLED("_nc_wgetch(%p)"), win));
328 sp = _nc_screen_of(win);
329 if (win == 0 || sp == 0) {
333 if (cooked_key_in_fifo()) {
335 *result = fifo_pull(sp);
336 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
338 #ifdef NCURSES_WGETCH_EVENTS
339 if (evl && (evl->count == 0))
341 event_delay = _nc_eventlist_timeout(evl);
345 * Handle cooked mode. Grab a string from the screen,
346 * stuff its contents in the FIFO queue, and pop off
347 * the first character to return it.
353 !sp->_called_wgetch) {
354 char buf[MAXCOLUMNS], *bufp;
357 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
359 rc = recur_wgetnstr(win, buf);
361 /* ungetch in reverse order */
362 #ifdef NCURSES_WGETCH_EVENTS
365 _nc_ungetch(sp, '\n');
366 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
367 _nc_ungetch(sp, bufp[-1]);
369 #ifdef NCURSES_WGETCH_EVENTS
370 /* Return it first */
371 if (rc == KEY_EVENT) {
375 *result = fifo_pull(sp);
376 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
379 if (win->_use_keypad != sp->_keypad_on)
380 _nc_keypad(sp, win->_use_keypad);
384 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
385 if (head == -1) { /* fifo is empty */
389 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
391 delay = (sp->_cbreak - 1) * 100;
395 #ifdef NCURSES_WGETCH_EVENTS
396 if (event_delay >= 0 && delay > event_delay)
400 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
402 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
404 #ifdef NCURSES_WGETCH_EVENTS
407 returnCode(KEY_CODE_YES);
414 /* else go on to read data available */
417 if (win->_use_keypad) {
419 * This is tricky. We only want to get special-key
420 * events one at a time. But we want to accumulate
421 * mouse events until either (a) the mouse logic tells
422 * us it's picked up a complete gesture, or (b)
423 * there's a detectable time lapse after one.
425 * Note: if the mouse code starts failing to compose
426 * press/release events into clicks, you should probably
427 * increase the wait with mouseinterval().
433 ch = kgetch(sp EVENTLIST_2nd(evl));
434 if (ch == KEY_MOUSE) {
436 if (sp->_mouse_inline(sp))
439 if (sp->_maxclick < 0)
443 && (((rc = check_mouse_activity(sp, sp->_maxclick
444 EVENTLIST_2nd(evl))) != 0
446 || !sp->_mouse_parse(sp, runcount)));
447 #ifdef NCURSES_WGETCH_EVENTS
448 if ((rc & 4) && !ch == KEY_EVENT) {
453 if (runcount > 0 && ch != KEY_MOUSE) {
454 #ifdef NCURSES_WGETCH_EVENTS
455 /* mouse event sequence ended by an event, report event */
456 if (ch == KEY_EVENT) {
457 _nc_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
461 /* mouse event sequence ended by keystroke, store keystroke */
468 fifo_push(sp EVENTLIST_2nd(evl));
474 if (_nc_handle_sigwinch(sp)) {
475 _nc_update_screensize(sp);
476 /* resizeterm can push KEY_RESIZE */
477 if (cooked_key_in_fifo()) {
478 *result = fifo_pull(sp);
480 * Get the ERR from queue -- it is from WINCH,
481 * so we should take it out, the "error" is handled.
483 if (fifo_peek(sp) == -1)
485 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
493 * If echo() is in effect, display the printable version of the
494 * key on the screen. Carriage return and backspace are treated
495 * specially by Solaris curses:
497 * If carriage return is defined as a function key in the
498 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
499 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
500 * We echo before translating carriage return based on nonl(),
501 * since the visual result simply moves the cursor to column 0.
503 * Backspace is a different matter. Solaris curses does not
504 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
505 * on the stty modes, but appears to be a hardcoded special case.
506 * This is a difference from ncurses, which uses the terminfo entry.
507 * However, we provide the same visual result as Solaris, moving the
508 * cursor to the left.
510 if (sp->_echo && !(win->_flags & _ISPAD)) {
511 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
512 if (backup < KEY_MIN)
513 wechochar(win, backup);
517 * Simulate ICRNL mode
519 if ((ch == '\r') && sp->_nl)
522 /* Strip 8th-bit if so desired. We do this only for characters that
523 * are in the range 128-255, to provide compatibility with terminals
524 * that display only 7-bit characters. Note that 'ch' may be a
525 * function key at this point, so we mustn't strip _those_.
528 if ((ch < KEY_MIN) && (ch & 0x80))
531 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
534 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
537 #ifdef NCURSES_WGETCH_EVENTS
539 wgetch_events(WINDOW *win, _nc_eventlist * evl)
544 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
545 code = _nc_wgetch(win,
561 T((T_CALLED("wgetch(%p)"), win));
562 code = _nc_wgetch(win,
565 EVENTLIST_2nd((_nc_eventlist *) 0));
575 ** Get an input character, but take care of keypad sequences, returning
576 ** an appropriate code when one matches the input. After each character
577 ** is received, set an alarm call based on ESCDELAY. If no more of the
578 ** sequence is received by the time the alarm goes off, pass through
579 ** the sequence gotten so far.
581 ** This function must be called when there are no cooked keys in queue.
582 ** (that is head==-1 || peek==head)
587 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
591 int timeleft = GetEscdelay(sp);
593 TR(TRACE_IEVENT, ("kgetch() called"));
598 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
600 } else if (!raw_key_in_fifo()) {
601 ch = fifo_push(sp EVENTLIST_2nd(evl));
603 peek = head; /* the keys stay uninterpreted */
606 #ifdef NCURSES_WGETCH_EVENTS
607 else if (ch == KEY_EVENT) {
608 peek = head; /* the keys stay uninterpreted */
609 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
616 /* If not first in queue, somebody put this key there on purpose in
617 * emergency. Consider it higher priority than the unfinished
618 * keysequence we are parsing.
621 /* assume the key is the last in fifo */
622 t_dec(); /* remove the key */
626 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
627 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
631 TR(TRACE_IEVENT, ("ptr is null"));
634 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
635 ptr, ptr->ch, ptr->value));
637 if (ptr->value != 0) { /* sequence terminated */
638 TR(TRACE_IEVENT, ("end of sequence"));
648 if (!raw_key_in_fifo()) {
651 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
652 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
653 #ifdef NCURSES_WGETCH_EVENTS
655 TR(TRACE_IEVENT, ("interrupted by a user event"));
656 /* FIXME Should have preserved remainder timeleft for reuse... */
657 peek = head; /* Restart interpreting later */
662 TR(TRACE_IEVENT, ("ran out of time"));