1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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.75 2006/03/04 20:06:09 tom Exp $")
46 #include <fifo_defs.h>
48 NCURSES_EXPORT_VAR(int)
49 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
51 #ifdef NCURSES_WGETCH_EVENTS
58 * Check for mouse activity, returning nonzero if we find any.
61 check_mouse_activity(int delay EVENTLIST_2nd(_nc_eventlist * evl))
66 if ((SP->_mouse_type == M_SYSMOUSE)
67 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
71 rc = _nc_timed_wait(TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
73 if ((SP->_mouse_type == M_SYSMOUSE)
74 && (SP->_sysmouse_head < SP->_sysmouse_tail)
76 && (errno == EINTR)) {
83 static NCURSES_INLINE int
86 int ch = SP->_fifo[peek];
87 TR(TRACE_IEVENT, ("peeking at %d", peek));
93 static NCURSES_INLINE int
98 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
107 if (_nc_tracing & TRACE_IEVENT)
113 static NCURSES_INLINE int
114 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
129 #ifdef NCURSES_WGETCH_EVENTS
131 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
132 || (SP->_mouse_fd >= 0)
135 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
140 T(("fifo_push: ungetch KEY_EVENT"));
144 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
145 if (SP->_mouse_fd >= 0) {
146 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
150 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
151 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
152 SP->_mouse_event(SP);
158 if ((SP->_mouse_type == M_SYSMOUSE)
159 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
160 SP->_mouse_event(SP);
163 } else if ((SP->_mouse_type == M_SYSMOUSE)
164 && (mask <= 0) && errno == EINTR) {
165 SP->_mouse_event(SP);
171 unsigned char c2 = 0;
172 n = read(SP->_ifd, &c2, 1);
178 * Under System V curses with non-restarting signals, getch() returns
179 * with value ERR when a handled signal keeps it from completing.
180 * If signals restart system calls, OTOH, the signal is invisible
181 * except to its handler.
183 * We don't want this difference to show. This piece of code
184 * tries to make it look like we always have restarting signals.
186 if (n <= 0 && errno == EINTR)
190 if ((n == -1) || (n == 0)) {
191 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
194 TR(TRACE_IEVENT, ("read %d characters", n));
196 SP->_fifo[tail] = ch;
201 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
203 if (_nc_tracing & TRACE_IEVENT)
209 static NCURSES_INLINE void
212 memset(SP->_fifo, 0, sizeof(SP->_fifo));
217 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
219 #define wgetch_should_refresh(win) (\
220 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
221 && !(win->_flags & _ISPAD))
224 _nc_wgetch(WINDOW *win,
225 unsigned long *result,
227 EVENTLIST_2nd(_nc_eventlist * evl))
230 #ifdef NCURSES_WGETCH_EVENTS
231 long event_delay = -1;
234 T((T_CALLED("_nc_wgetch(%p)"), win));
237 if (win == 0 || SP == 0)
240 if (cooked_key_in_fifo()) {
241 if (wgetch_should_refresh(win))
244 *result = fifo_pull();
247 #ifdef NCURSES_WGETCH_EVENTS
248 if (evl && (evl->count == 0))
250 event_delay = _nc_eventlist_timeout(evl);
254 * Handle cooked mode. Grab a string from the screen,
255 * stuff its contents in the FIFO queue, and pop off
256 * the first character to return it.
262 !SP->_called_wgetch) {
263 char buf[MAXCOLUMNS], *sp;
266 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
268 SP->_called_wgetch = TRUE;
269 rc = wgetnstr(win, buf, MAXCOLUMNS);
270 SP->_called_wgetch = FALSE;
272 /* ungetch in reverse order */
273 #ifdef NCURSES_WGETCH_EVENTS
277 for (sp = buf + strlen(buf); sp > buf; sp--)
280 #ifdef NCURSES_WGETCH_EVENTS
281 /* Return it first */
282 if (rc == KEY_EVENT) {
288 *result = fifo_pull();
292 if (win->_use_keypad != SP->_keypad_on)
293 _nc_keypad(win->_use_keypad);
295 if (wgetch_should_refresh(win))
298 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
299 if (head == -1) { /* fifo is empty */
303 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
305 delay = (SP->_cbreak - 1) * 100;
309 #ifdef NCURSES_WGETCH_EVENTS
310 if (event_delay >= 0 && delay > event_delay)
314 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
316 rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
318 #ifdef NCURSES_WGETCH_EVENTS
327 /* else go on to read data available */
330 if (win->_use_keypad) {
332 * This is tricky. We only want to get special-key
333 * events one at a time. But we want to accumulate
334 * mouse events until either (a) the mouse logic tells
335 * us it's picked up a complete gesture, or (b)
336 * there's a detectable time lapse after one.
338 * Note: if the mouse code starts failing to compose
339 * press/release events into clicks, you should probably
340 * increase the wait with mouseinterval().
346 ch = kgetch(EVENTLIST_1st(evl));
347 if (ch == KEY_MOUSE) {
349 if (SP->_mouse_inline(SP))
352 if (SP->_maxclick < 0)
356 && (((rc = check_mouse_activity(SP->_maxclick
357 EVENTLIST_2nd(evl))) != 0
359 || !SP->_mouse_parse(runcount)));
360 #ifdef NCURSES_WGETCH_EVENTS
361 if ((rc & 4) && !ch == KEY_EVENT) {
366 if (runcount > 0 && ch != KEY_MOUSE) {
367 #ifdef NCURSES_WGETCH_EVENTS
368 /* mouse event sequence ended by an event, report event */
369 if (ch == KEY_EVENT) {
370 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
374 /* mouse event sequence ended by keystroke, store keystroke */
381 fifo_push(EVENTLIST_1st(evl));
387 if (SP->_sig_winch) {
388 _nc_update_screensize();
389 /* resizeterm can push KEY_RESIZE */
390 if (cooked_key_in_fifo()) {
391 *result = fifo_pull();
392 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
400 * If echo() is in effect, display the printable version of the
401 * key on the screen. Carriage return and backspace are treated
402 * specially by Solaris curses:
404 * If carriage return is defined as a function key in the
405 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
406 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
407 * We echo before translating carriage return based on nonl(),
408 * since the visual result simply moves the cursor to column 0.
410 * Backspace is a different matter. Solaris curses does not
411 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
412 * on the stty modes, but appears to be a hardcoded special case.
413 * This is a difference from ncurses, which uses the terminfo entry.
414 * However, we provide the same visual result as Solaris, moving the
415 * cursor to the left.
417 if (SP->_echo && !(win->_flags & _ISPAD)) {
418 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
419 if (backup < KEY_MIN)
420 wechochar(win, backup);
424 * Simulate ICRNL mode
426 if ((ch == '\r') && SP->_nl)
429 /* Strip 8th-bit if so desired. We do this only for characters that
430 * are in the range 128-255, to provide compatibility with terminals
431 * that display only 7-bit characters. Note that 'ch' may be a
432 * function key at this point, so we mustn't strip _those_.
435 if ((ch < KEY_MIN) && (ch & 0x80))
438 T(("wgetch returning : %s", _tracechar(ch)));
441 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
444 #ifdef NCURSES_WGETCH_EVENTS
446 wgetch_events(WINDOW *win, _nc_eventlist * evl)
451 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
452 code = _nc_wgetch(win,
468 T((T_CALLED("wgetch(%p)"), win));
469 code = _nc_wgetch(win,
471 (SP ? SP->_use_meta : 0)
472 EVENTLIST_2nd((_nc_eventlist *) 0));
482 ** Get an input character, but take care of keypad sequences, returning
483 ** an appropriate code when one matches the input. After each character
484 ** is received, set an alarm call based on ESCDELAY. If no more of the
485 ** sequence is received by the time the alarm goes off, pass through
486 ** the sequence gotten so far.
488 ** This function must be called when there are no cooked keys in queue.
489 ** (that is head==-1 || peek==head)
494 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
498 int timeleft = ESCDELAY;
500 TR(TRACE_IEVENT, ("kgetch() called"));
505 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
507 } else if (!raw_key_in_fifo()) {
508 ch = fifo_push(EVENTLIST_1st(evl));
510 peek = head; /* the keys stay uninterpreted */
513 #ifdef NCURSES_WGETCH_EVENTS
514 else if (ch == KEY_EVENT) {
515 peek = head; /* the keys stay uninterpreted */
516 return fifo_pull(); /* Remove KEY_EVENT from the queue */
523 /* If not first in queue, somebody put this key there on purpose in
524 * emergency. Consider it higher priority than the unfinished
525 * keysequence we are parsing.
528 /* assume the key is the last in fifo */
529 t_dec(); /* remove the key */
533 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
534 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
538 TR(TRACE_IEVENT, ("ptr is null"));
541 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
542 ptr, ptr->ch, ptr->value));
544 if (ptr->value != 0) { /* sequence terminated */
545 TR(TRACE_IEVENT, ("end of sequence"));
555 if (!raw_key_in_fifo()) {
558 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
559 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
560 #ifdef NCURSES_WGETCH_EVENTS
562 TR(TRACE_IEVENT, ("interrupted by a user event"));
563 /* FIXME Should have preserved remainder timeleft for reuse... */
564 peek = head; /* Restart interpreting later */
569 TR(TRACE_IEVENT, ("ran out of time"));