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.78 2007/03/10 19:22:28 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 (_nc_tracing & TRACE_IEVENT)
121 static NCURSES_INLINE int
122 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
137 #ifdef NCURSES_WGETCH_EVENTS
139 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
140 || (SP->_mouse_fd >= 0)
143 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
148 T(("fifo_push: ungetch KEY_EVENT"));
152 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
153 if (SP->_mouse_fd >= 0) {
154 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
158 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
159 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
160 SP->_mouse_event(SP);
166 if ((SP->_mouse_type == M_SYSMOUSE)
167 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
168 SP->_mouse_event(SP);
171 } else if ((SP->_mouse_type == M_SYSMOUSE)
172 && (mask <= 0) && errno == EINTR) {
173 SP->_mouse_event(SP);
179 unsigned char c2 = 0;
180 n = read(SP->_ifd, &c2, 1);
186 * Under System V curses with non-restarting signals, getch() returns
187 * with value ERR when a handled signal keeps it from completing.
188 * If signals restart system calls, OTOH, the signal is invisible
189 * except to its handler.
191 * We don't want this difference to show. This piece of code
192 * tries to make it look like we always have restarting signals.
194 if (n <= 0 && errno == EINTR)
198 if ((n == -1) || (n == 0)) {
199 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
202 TR(TRACE_IEVENT, ("read %d characters", n));
204 SP->_fifo[tail] = ch;
209 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
211 if (_nc_tracing & TRACE_IEVENT)
217 static NCURSES_INLINE void
220 memset(SP->_fifo, 0, sizeof(SP->_fifo));
225 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
227 #define wgetch_should_refresh(win) (\
228 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
229 && !(win->_flags & _ISPAD))
232 _nc_wgetch(WINDOW *win,
233 unsigned long *result,
235 EVENTLIST_2nd(_nc_eventlist * evl))
238 #ifdef NCURSES_WGETCH_EVENTS
239 long event_delay = -1;
242 T((T_CALLED("_nc_wgetch(%p)"), win));
245 if (win == 0 || SP == 0) {
249 if (cooked_key_in_fifo()) {
250 if (wgetch_should_refresh(win))
253 *result = fifo_pull();
254 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
256 #ifdef NCURSES_WGETCH_EVENTS
257 if (evl && (evl->count == 0))
259 event_delay = _nc_eventlist_timeout(evl);
263 * Handle cooked mode. Grab a string from the screen,
264 * stuff its contents in the FIFO queue, and pop off
265 * the first character to return it.
271 !SP->_called_wgetch) {
272 char buf[MAXCOLUMNS], *sp;
275 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
277 SP->_called_wgetch = TRUE;
278 rc = wgetnstr(win, buf, MAXCOLUMNS);
279 SP->_called_wgetch = FALSE;
281 /* ungetch in reverse order */
282 #ifdef NCURSES_WGETCH_EVENTS
286 for (sp = buf + strlen(buf); sp > buf; sp--)
289 #ifdef NCURSES_WGETCH_EVENTS
290 /* Return it first */
291 if (rc == KEY_EVENT) {
295 *result = fifo_pull();
296 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
299 if (win->_use_keypad != SP->_keypad_on)
300 _nc_keypad(win->_use_keypad);
302 if (wgetch_should_refresh(win))
305 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
306 if (head == -1) { /* fifo is empty */
310 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
312 delay = (SP->_cbreak - 1) * 100;
316 #ifdef NCURSES_WGETCH_EVENTS
317 if (event_delay >= 0 && delay > event_delay)
321 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
323 rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
325 #ifdef NCURSES_WGETCH_EVENTS
328 returnCode(KEY_CODE_YES);
334 /* else go on to read data available */
337 if (win->_use_keypad) {
339 * This is tricky. We only want to get special-key
340 * events one at a time. But we want to accumulate
341 * mouse events until either (a) the mouse logic tells
342 * us it's picked up a complete gesture, or (b)
343 * there's a detectable time lapse after one.
345 * Note: if the mouse code starts failing to compose
346 * press/release events into clicks, you should probably
347 * increase the wait with mouseinterval().
353 ch = kgetch(EVENTLIST_1st(evl));
354 if (ch == KEY_MOUSE) {
356 if (SP->_mouse_inline(SP))
359 if (SP->_maxclick < 0)
363 && (((rc = check_mouse_activity(SP->_maxclick
364 EVENTLIST_2nd(evl))) != 0
366 || !SP->_mouse_parse(runcount)));
367 #ifdef NCURSES_WGETCH_EVENTS
368 if ((rc & 4) && !ch == KEY_EVENT) {
373 if (runcount > 0 && ch != KEY_MOUSE) {
374 #ifdef NCURSES_WGETCH_EVENTS
375 /* mouse event sequence ended by an event, report event */
376 if (ch == KEY_EVENT) {
377 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
381 /* mouse event sequence ended by keystroke, store keystroke */
388 fifo_push(EVENTLIST_1st(evl));
394 if (SP->_sig_winch) {
395 _nc_update_screensize();
396 /* resizeterm can push KEY_RESIZE */
397 if (cooked_key_in_fifo()) {
398 *result = fifo_pull();
399 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
407 * If echo() is in effect, display the printable version of the
408 * key on the screen. Carriage return and backspace are treated
409 * specially by Solaris curses:
411 * If carriage return is defined as a function key in the
412 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
413 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
414 * We echo before translating carriage return based on nonl(),
415 * since the visual result simply moves the cursor to column 0.
417 * Backspace is a different matter. Solaris curses does not
418 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
419 * on the stty modes, but appears to be a hardcoded special case.
420 * This is a difference from ncurses, which uses the terminfo entry.
421 * However, we provide the same visual result as Solaris, moving the
422 * cursor to the left.
424 if (SP->_echo && !(win->_flags & _ISPAD)) {
425 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
426 if (backup < KEY_MIN)
427 wechochar(win, backup);
431 * Simulate ICRNL mode
433 if ((ch == '\r') && SP->_nl)
436 /* Strip 8th-bit if so desired. We do this only for characters that
437 * are in the range 128-255, to provide compatibility with terminals
438 * that display only 7-bit characters. Note that 'ch' may be a
439 * function key at this point, so we mustn't strip _those_.
442 if ((ch < KEY_MIN) && (ch & 0x80))
445 T(("wgetch returning : %s", _tracechar(ch)));
448 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
451 #ifdef NCURSES_WGETCH_EVENTS
453 wgetch_events(WINDOW *win, _nc_eventlist * evl)
458 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
459 code = _nc_wgetch(win,
475 T((T_CALLED("wgetch(%p)"), win));
476 code = _nc_wgetch(win,
478 (SP ? SP->_use_meta : 0)
479 EVENTLIST_2nd((_nc_eventlist *) 0));
489 ** Get an input character, but take care of keypad sequences, returning
490 ** an appropriate code when one matches the input. After each character
491 ** is received, set an alarm call based on ESCDELAY. If no more of the
492 ** sequence is received by the time the alarm goes off, pass through
493 ** the sequence gotten so far.
495 ** This function must be called when there are no cooked keys in queue.
496 ** (that is head==-1 || peek==head)
501 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
505 int timeleft = ESCDELAY;
507 TR(TRACE_IEVENT, ("kgetch() called"));
512 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
514 } else if (!raw_key_in_fifo()) {
515 ch = fifo_push(EVENTLIST_1st(evl));
517 peek = head; /* the keys stay uninterpreted */
520 #ifdef NCURSES_WGETCH_EVENTS
521 else if (ch == KEY_EVENT) {
522 peek = head; /* the keys stay uninterpreted */
523 return fifo_pull(); /* Remove KEY_EVENT from the queue */
530 /* If not first in queue, somebody put this key there on purpose in
531 * emergency. Consider it higher priority than the unfinished
532 * keysequence we are parsing.
535 /* assume the key is the last in fifo */
536 t_dec(); /* remove the key */
540 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
541 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
545 TR(TRACE_IEVENT, ("ptr is null"));
548 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
549 ptr, ptr->ch, ptr->value));
551 if (ptr->value != 0) { /* sequence terminated */
552 TR(TRACE_IEVENT, ("end of sequence"));
562 if (!raw_key_in_fifo()) {
565 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
566 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
567 #ifdef NCURSES_WGETCH_EVENTS
569 TR(TRACE_IEVENT, ("interrupted by a user event"));
570 /* FIXME Should have preserved remainder timeleft for reuse... */
571 peek = head; /* Restart interpreting later */
576 TR(TRACE_IEVENT, ("ran out of time"));