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.82 2008/01/19 21:07:30 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 */
61 set_escdelay(int value)
66 SP->_ESCDELAY = value;
77 #ifdef NCURSES_WGETCH_EVENTS
84 * Check for mouse activity, returning nonzero if we find any.
87 check_mouse_activity(int delay EVENTLIST_2nd(_nc_eventlist * evl))
92 if ((SP->_mouse_type == M_SYSMOUSE)
93 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
97 rc = _nc_timed_wait(TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
99 if ((SP->_mouse_type == M_SYSMOUSE)
100 && (SP->_sysmouse_head < SP->_sysmouse_tail)
102 && (errno == EINTR)) {
109 static NCURSES_INLINE int
112 int ch = SP->_fifo[peek];
113 TR(TRACE_IEVENT, ("peeking at %d", peek));
119 static NCURSES_INLINE int
123 ch = SP->_fifo[head];
124 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
133 if (USE_TRACEF(TRACE_IEVENT)) {
135 _nc_unlock_global(tracef);
141 static NCURSES_INLINE int
142 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
157 #ifdef NCURSES_WGETCH_EVENTS
159 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
160 || (SP->_mouse_fd >= 0)
163 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
168 T(("fifo_push: ungetch KEY_EVENT"));
172 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
173 if (SP->_mouse_fd >= 0) {
174 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
178 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
179 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
180 SP->_mouse_event(SP);
186 if ((SP->_mouse_type == M_SYSMOUSE)
187 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
188 SP->_mouse_event(SP);
191 } else if ((SP->_mouse_type == M_SYSMOUSE)
192 && (mask <= 0) && errno == EINTR) {
193 SP->_mouse_event(SP);
199 unsigned char c2 = 0;
200 n = read(SP->_ifd, &c2, 1);
206 * Under System V curses with non-restarting signals, getch() returns
207 * with value ERR when a handled signal keeps it from completing.
208 * If signals restart system calls, OTOH, the signal is invisible
209 * except to its handler.
211 * We don't want this difference to show. This piece of code
212 * tries to make it look like we always have restarting signals.
214 if (n <= 0 && errno == EINTR)
218 if ((n == -1) || (n == 0)) {
219 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
222 TR(TRACE_IEVENT, ("read %d characters", n));
224 SP->_fifo[tail] = ch;
229 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
231 if (USE_TRACEF(TRACE_IEVENT)) {
233 _nc_unlock_global(tracef);
239 static NCURSES_INLINE void
242 memset(SP->_fifo, 0, sizeof(SP->_fifo));
247 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
249 #define wgetch_should_refresh(win) (\
250 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
251 && !(win->_flags & _ISPAD))
254 _nc_wgetch(WINDOW *win,
255 unsigned long *result,
257 EVENTLIST_2nd(_nc_eventlist * evl))
260 #ifdef NCURSES_WGETCH_EVENTS
261 long event_delay = -1;
264 T((T_CALLED("_nc_wgetch(%p)"), win));
267 if (win == 0 || SP == 0) {
271 if (cooked_key_in_fifo()) {
272 if (wgetch_should_refresh(win))
275 *result = fifo_pull();
276 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
278 #ifdef NCURSES_WGETCH_EVENTS
279 if (evl && (evl->count == 0))
281 event_delay = _nc_eventlist_timeout(evl);
285 * Handle cooked mode. Grab a string from the screen,
286 * stuff its contents in the FIFO queue, and pop off
287 * the first character to return it.
293 !SP->_called_wgetch) {
294 char buf[MAXCOLUMNS], *sp;
297 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
299 SP->_called_wgetch = TRUE;
300 rc = wgetnstr(win, buf, MAXCOLUMNS);
301 SP->_called_wgetch = FALSE;
303 /* ungetch in reverse order */
304 #ifdef NCURSES_WGETCH_EVENTS
308 for (sp = buf + strlen(buf); sp > buf; sp--)
311 #ifdef NCURSES_WGETCH_EVENTS
312 /* Return it first */
313 if (rc == KEY_EVENT) {
317 *result = fifo_pull();
318 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
321 if (win->_use_keypad != SP->_keypad_on)
322 _nc_keypad(win->_use_keypad);
324 if (wgetch_should_refresh(win))
327 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
328 if (head == -1) { /* fifo is empty */
332 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
334 delay = (SP->_cbreak - 1) * 100;
338 #ifdef NCURSES_WGETCH_EVENTS
339 if (event_delay >= 0 && delay > event_delay)
343 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
345 rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
347 #ifdef NCURSES_WGETCH_EVENTS
350 returnCode(KEY_CODE_YES);
356 /* else go on to read data available */
359 if (win->_use_keypad) {
361 * This is tricky. We only want to get special-key
362 * events one at a time. But we want to accumulate
363 * mouse events until either (a) the mouse logic tells
364 * us it's picked up a complete gesture, or (b)
365 * there's a detectable time lapse after one.
367 * Note: if the mouse code starts failing to compose
368 * press/release events into clicks, you should probably
369 * increase the wait with mouseinterval().
375 ch = kgetch(EVENTLIST_1st(evl));
376 if (ch == KEY_MOUSE) {
378 if (SP->_mouse_inline(SP))
381 if (SP->_maxclick < 0)
385 && (((rc = check_mouse_activity(SP->_maxclick
386 EVENTLIST_2nd(evl))) != 0
388 || !SP->_mouse_parse(runcount)));
389 #ifdef NCURSES_WGETCH_EVENTS
390 if ((rc & 4) && !ch == KEY_EVENT) {
395 if (runcount > 0 && ch != KEY_MOUSE) {
396 #ifdef NCURSES_WGETCH_EVENTS
397 /* mouse event sequence ended by an event, report event */
398 if (ch == KEY_EVENT) {
399 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
403 /* mouse event sequence ended by keystroke, store keystroke */
410 fifo_push(EVENTLIST_1st(evl));
416 if (_nc_handle_sigwinch(FALSE)) {
417 _nc_update_screensize();
418 /* resizeterm can push KEY_RESIZE */
419 if (cooked_key_in_fifo()) {
420 *result = fifo_pull();
421 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
429 * If echo() is in effect, display the printable version of the
430 * key on the screen. Carriage return and backspace are treated
431 * specially by Solaris curses:
433 * If carriage return is defined as a function key in the
434 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
435 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
436 * We echo before translating carriage return based on nonl(),
437 * since the visual result simply moves the cursor to column 0.
439 * Backspace is a different matter. Solaris curses does not
440 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
441 * on the stty modes, but appears to be a hardcoded special case.
442 * This is a difference from ncurses, which uses the terminfo entry.
443 * However, we provide the same visual result as Solaris, moving the
444 * cursor to the left.
446 if (SP->_echo && !(win->_flags & _ISPAD)) {
447 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
448 if (backup < KEY_MIN)
449 wechochar(win, backup);
453 * Simulate ICRNL mode
455 if ((ch == '\r') && SP->_nl)
458 /* Strip 8th-bit if so desired. We do this only for characters that
459 * are in the range 128-255, to provide compatibility with terminals
460 * that display only 7-bit characters. Note that 'ch' may be a
461 * function key at this point, so we mustn't strip _those_.
464 if ((ch < KEY_MIN) && (ch & 0x80))
467 T(("wgetch returning : %s", _tracechar(ch)));
470 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
473 #ifdef NCURSES_WGETCH_EVENTS
475 wgetch_events(WINDOW *win, _nc_eventlist * evl)
480 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
481 code = _nc_wgetch(win,
497 T((T_CALLED("wgetch(%p)"), win));
498 code = _nc_wgetch(win,
500 (SP ? SP->_use_meta : 0)
501 EVENTLIST_2nd((_nc_eventlist *) 0));
511 ** Get an input character, but take care of keypad sequences, returning
512 ** an appropriate code when one matches the input. After each character
513 ** is received, set an alarm call based on ESCDELAY. If no more of the
514 ** sequence is received by the time the alarm goes off, pass through
515 ** the sequence gotten so far.
517 ** This function must be called when there are no cooked keys in queue.
518 ** (that is head==-1 || peek==head)
523 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
527 int timeleft = ESCDELAY;
529 TR(TRACE_IEVENT, ("kgetch() called"));
534 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
536 } else if (!raw_key_in_fifo()) {
537 ch = fifo_push(EVENTLIST_1st(evl));
539 peek = head; /* the keys stay uninterpreted */
542 #ifdef NCURSES_WGETCH_EVENTS
543 else if (ch == KEY_EVENT) {
544 peek = head; /* the keys stay uninterpreted */
545 return fifo_pull(); /* Remove KEY_EVENT from the queue */
552 /* If not first in queue, somebody put this key there on purpose in
553 * emergency. Consider it higher priority than the unfinished
554 * keysequence we are parsing.
557 /* assume the key is the last in fifo */
558 t_dec(); /* remove the key */
562 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
563 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
567 TR(TRACE_IEVENT, ("ptr is null"));
570 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
571 ptr, ptr->ch, ptr->value));
573 if (ptr->value != 0) { /* sequence terminated */
574 TR(TRACE_IEVENT, ("end of sequence"));
584 if (!raw_key_in_fifo()) {
587 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
588 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
589 #ifdef NCURSES_WGETCH_EVENTS
591 TR(TRACE_IEVENT, ("interrupted by a user event"));
592 /* FIXME Should have preserved remainder timeleft for reuse... */
593 peek = head; /* Restart interpreting later */
598 TR(TRACE_IEVENT, ("ran out of time"));