1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.80 2007/09/29 20:39:34 tom Exp $")
46 #include <fifo_defs.h>
50 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
52 return SP ? SP->_ESCDELAY : 1000;
55 NCURSES_EXPORT_VAR(int)
56 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
59 #ifdef NCURSES_WGETCH_EVENTS
66 * Check for mouse activity, returning nonzero if we find any.
69 check_mouse_activity(int delay EVENTLIST_2nd(_nc_eventlist * evl))
74 if ((SP->_mouse_type == M_SYSMOUSE)
75 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
79 rc = _nc_timed_wait(TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
81 if ((SP->_mouse_type == M_SYSMOUSE)
82 && (SP->_sysmouse_head < SP->_sysmouse_tail)
84 && (errno == EINTR)) {
91 static NCURSES_INLINE int
94 int ch = SP->_fifo[peek];
95 TR(TRACE_IEVENT, ("peeking at %d", peek));
101 static NCURSES_INLINE int
105 ch = SP->_fifo[head];
106 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
115 if (USE_TRACEF(TRACE_IEVENT)) {
117 _nc_unlock_global(tracef);
123 static NCURSES_INLINE int
124 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
139 #ifdef NCURSES_WGETCH_EVENTS
141 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
142 || (SP->_mouse_fd >= 0)
145 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
150 T(("fifo_push: ungetch KEY_EVENT"));
154 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
155 if (SP->_mouse_fd >= 0) {
156 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
160 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
161 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
162 SP->_mouse_event(SP);
168 if ((SP->_mouse_type == M_SYSMOUSE)
169 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
170 SP->_mouse_event(SP);
173 } else if ((SP->_mouse_type == M_SYSMOUSE)
174 && (mask <= 0) && errno == EINTR) {
175 SP->_mouse_event(SP);
181 unsigned char c2 = 0;
182 n = read(SP->_ifd, &c2, 1);
188 * Under System V curses with non-restarting signals, getch() returns
189 * with value ERR when a handled signal keeps it from completing.
190 * If signals restart system calls, OTOH, the signal is invisible
191 * except to its handler.
193 * We don't want this difference to show. This piece of code
194 * tries to make it look like we always have restarting signals.
196 if (n <= 0 && errno == EINTR)
200 if ((n == -1) || (n == 0)) {
201 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
204 TR(TRACE_IEVENT, ("read %d characters", n));
206 SP->_fifo[tail] = ch;
211 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
213 if (USE_TRACEF(TRACE_IEVENT)) {
215 _nc_unlock_global(tracef);
221 static NCURSES_INLINE void
224 memset(SP->_fifo, 0, sizeof(SP->_fifo));
229 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
231 #define wgetch_should_refresh(win) (\
232 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
233 && !(win->_flags & _ISPAD))
236 _nc_wgetch(WINDOW *win,
237 unsigned long *result,
239 EVENTLIST_2nd(_nc_eventlist * evl))
242 #ifdef NCURSES_WGETCH_EVENTS
243 long event_delay = -1;
246 T((T_CALLED("_nc_wgetch(%p)"), win));
249 if (win == 0 || SP == 0) {
253 if (cooked_key_in_fifo()) {
254 if (wgetch_should_refresh(win))
257 *result = fifo_pull();
258 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
260 #ifdef NCURSES_WGETCH_EVENTS
261 if (evl && (evl->count == 0))
263 event_delay = _nc_eventlist_timeout(evl);
267 * Handle cooked mode. Grab a string from the screen,
268 * stuff its contents in the FIFO queue, and pop off
269 * the first character to return it.
275 !SP->_called_wgetch) {
276 char buf[MAXCOLUMNS], *sp;
279 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
281 SP->_called_wgetch = TRUE;
282 rc = wgetnstr(win, buf, MAXCOLUMNS);
283 SP->_called_wgetch = FALSE;
285 /* ungetch in reverse order */
286 #ifdef NCURSES_WGETCH_EVENTS
290 for (sp = buf + strlen(buf); sp > buf; sp--)
293 #ifdef NCURSES_WGETCH_EVENTS
294 /* Return it first */
295 if (rc == KEY_EVENT) {
299 *result = fifo_pull();
300 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
303 if (win->_use_keypad != SP->_keypad_on)
304 _nc_keypad(win->_use_keypad);
306 if (wgetch_should_refresh(win))
309 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
310 if (head == -1) { /* fifo is empty */
314 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
316 delay = (SP->_cbreak - 1) * 100;
320 #ifdef NCURSES_WGETCH_EVENTS
321 if (event_delay >= 0 && delay > event_delay)
325 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
327 rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
329 #ifdef NCURSES_WGETCH_EVENTS
332 returnCode(KEY_CODE_YES);
338 /* else go on to read data available */
341 if (win->_use_keypad) {
343 * This is tricky. We only want to get special-key
344 * events one at a time. But we want to accumulate
345 * mouse events until either (a) the mouse logic tells
346 * us it's picked up a complete gesture, or (b)
347 * there's a detectable time lapse after one.
349 * Note: if the mouse code starts failing to compose
350 * press/release events into clicks, you should probably
351 * increase the wait with mouseinterval().
357 ch = kgetch(EVENTLIST_1st(evl));
358 if (ch == KEY_MOUSE) {
360 if (SP->_mouse_inline(SP))
363 if (SP->_maxclick < 0)
367 && (((rc = check_mouse_activity(SP->_maxclick
368 EVENTLIST_2nd(evl))) != 0
370 || !SP->_mouse_parse(runcount)));
371 #ifdef NCURSES_WGETCH_EVENTS
372 if ((rc & 4) && !ch == KEY_EVENT) {
377 if (runcount > 0 && ch != KEY_MOUSE) {
378 #ifdef NCURSES_WGETCH_EVENTS
379 /* mouse event sequence ended by an event, report event */
380 if (ch == KEY_EVENT) {
381 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
385 /* mouse event sequence ended by keystroke, store keystroke */
392 fifo_push(EVENTLIST_1st(evl));
398 if (_nc_handle_sigwinch(FALSE)) {
399 _nc_update_screensize();
400 /* resizeterm can push KEY_RESIZE */
401 if (cooked_key_in_fifo()) {
402 *result = fifo_pull();
403 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
411 * If echo() is in effect, display the printable version of the
412 * key on the screen. Carriage return and backspace are treated
413 * specially by Solaris curses:
415 * If carriage return is defined as a function key in the
416 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
417 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
418 * We echo before translating carriage return based on nonl(),
419 * since the visual result simply moves the cursor to column 0.
421 * Backspace is a different matter. Solaris curses does not
422 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
423 * on the stty modes, but appears to be a hardcoded special case.
424 * This is a difference from ncurses, which uses the terminfo entry.
425 * However, we provide the same visual result as Solaris, moving the
426 * cursor to the left.
428 if (SP->_echo && !(win->_flags & _ISPAD)) {
429 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
430 if (backup < KEY_MIN)
431 wechochar(win, backup);
435 * Simulate ICRNL mode
437 if ((ch == '\r') && SP->_nl)
440 /* Strip 8th-bit if so desired. We do this only for characters that
441 * are in the range 128-255, to provide compatibility with terminals
442 * that display only 7-bit characters. Note that 'ch' may be a
443 * function key at this point, so we mustn't strip _those_.
446 if ((ch < KEY_MIN) && (ch & 0x80))
449 T(("wgetch returning : %s", _tracechar(ch)));
452 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
455 #ifdef NCURSES_WGETCH_EVENTS
457 wgetch_events(WINDOW *win, _nc_eventlist * evl)
462 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
463 code = _nc_wgetch(win,
479 T((T_CALLED("wgetch(%p)"), win));
480 code = _nc_wgetch(win,
482 (SP ? SP->_use_meta : 0)
483 EVENTLIST_2nd((_nc_eventlist *) 0));
493 ** Get an input character, but take care of keypad sequences, returning
494 ** an appropriate code when one matches the input. After each character
495 ** is received, set an alarm call based on ESCDELAY. If no more of the
496 ** sequence is received by the time the alarm goes off, pass through
497 ** the sequence gotten so far.
499 ** This function must be called when there are no cooked keys in queue.
500 ** (that is head==-1 || peek==head)
505 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
509 int timeleft = ESCDELAY;
511 TR(TRACE_IEVENT, ("kgetch() called"));
516 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
518 } else if (!raw_key_in_fifo()) {
519 ch = fifo_push(EVENTLIST_1st(evl));
521 peek = head; /* the keys stay uninterpreted */
524 #ifdef NCURSES_WGETCH_EVENTS
525 else if (ch == KEY_EVENT) {
526 peek = head; /* the keys stay uninterpreted */
527 return fifo_pull(); /* Remove KEY_EVENT from the queue */
534 /* If not first in queue, somebody put this key there on purpose in
535 * emergency. Consider it higher priority than the unfinished
536 * keysequence we are parsing.
539 /* assume the key is the last in fifo */
540 t_dec(); /* remove the key */
544 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
545 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
549 TR(TRACE_IEVENT, ("ptr is null"));
552 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
553 ptr, ptr->ch, ptr->value));
555 if (ptr->value != 0) { /* sequence terminated */
556 TR(TRACE_IEVENT, ("end of sequence"));
566 if (!raw_key_in_fifo()) {
569 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
570 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
571 #ifdef NCURSES_WGETCH_EVENTS
573 TR(TRACE_IEVENT, ("interrupted by a user event"));
574 /* FIXME Should have preserved remainder timeleft for reuse... */
575 peek = head; /* Restart interpreting later */
580 TR(TRACE_IEVENT, ("ran out of time"));