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.77 2007/02/25 00:43:54 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) {
241 if (cooked_key_in_fifo()) {
242 if (wgetch_should_refresh(win))
245 *result = fifo_pull();
246 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
248 #ifdef NCURSES_WGETCH_EVENTS
249 if (evl && (evl->count == 0))
251 event_delay = _nc_eventlist_timeout(evl);
255 * Handle cooked mode. Grab a string from the screen,
256 * stuff its contents in the FIFO queue, and pop off
257 * the first character to return it.
263 !SP->_called_wgetch) {
264 char buf[MAXCOLUMNS], *sp;
267 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
269 SP->_called_wgetch = TRUE;
270 rc = wgetnstr(win, buf, MAXCOLUMNS);
271 SP->_called_wgetch = FALSE;
273 /* ungetch in reverse order */
274 #ifdef NCURSES_WGETCH_EVENTS
278 for (sp = buf + strlen(buf); sp > buf; sp--)
281 #ifdef NCURSES_WGETCH_EVENTS
282 /* Return it first */
283 if (rc == KEY_EVENT) {
287 *result = fifo_pull();
288 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
291 if (win->_use_keypad != SP->_keypad_on)
292 _nc_keypad(win->_use_keypad);
294 if (wgetch_should_refresh(win))
297 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
298 if (head == -1) { /* fifo is empty */
302 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
304 delay = (SP->_cbreak - 1) * 100;
308 #ifdef NCURSES_WGETCH_EVENTS
309 if (event_delay >= 0 && delay > event_delay)
313 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
315 rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
317 #ifdef NCURSES_WGETCH_EVENTS
320 returnCode(KEY_CODE_YES);
326 /* else go on to read data available */
329 if (win->_use_keypad) {
331 * This is tricky. We only want to get special-key
332 * events one at a time. But we want to accumulate
333 * mouse events until either (a) the mouse logic tells
334 * us it's picked up a complete gesture, or (b)
335 * there's a detectable time lapse after one.
337 * Note: if the mouse code starts failing to compose
338 * press/release events into clicks, you should probably
339 * increase the wait with mouseinterval().
345 ch = kgetch(EVENTLIST_1st(evl));
346 if (ch == KEY_MOUSE) {
348 if (SP->_mouse_inline(SP))
351 if (SP->_maxclick < 0)
355 && (((rc = check_mouse_activity(SP->_maxclick
356 EVENTLIST_2nd(evl))) != 0
358 || !SP->_mouse_parse(runcount)));
359 #ifdef NCURSES_WGETCH_EVENTS
360 if ((rc & 4) && !ch == KEY_EVENT) {
365 if (runcount > 0 && ch != KEY_MOUSE) {
366 #ifdef NCURSES_WGETCH_EVENTS
367 /* mouse event sequence ended by an event, report event */
368 if (ch == KEY_EVENT) {
369 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
373 /* mouse event sequence ended by keystroke, store keystroke */
380 fifo_push(EVENTLIST_1st(evl));
386 if (SP->_sig_winch) {
387 _nc_update_screensize();
388 /* resizeterm can push KEY_RESIZE */
389 if (cooked_key_in_fifo()) {
390 *result = fifo_pull();
391 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
399 * If echo() is in effect, display the printable version of the
400 * key on the screen. Carriage return and backspace are treated
401 * specially by Solaris curses:
403 * If carriage return is defined as a function key in the
404 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
405 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
406 * We echo before translating carriage return based on nonl(),
407 * since the visual result simply moves the cursor to column 0.
409 * Backspace is a different matter. Solaris curses does not
410 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
411 * on the stty modes, but appears to be a hardcoded special case.
412 * This is a difference from ncurses, which uses the terminfo entry.
413 * However, we provide the same visual result as Solaris, moving the
414 * cursor to the left.
416 if (SP->_echo && !(win->_flags & _ISPAD)) {
417 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
418 if (backup < KEY_MIN)
419 wechochar(win, backup);
423 * Simulate ICRNL mode
425 if ((ch == '\r') && SP->_nl)
428 /* Strip 8th-bit if so desired. We do this only for characters that
429 * are in the range 128-255, to provide compatibility with terminals
430 * that display only 7-bit characters. Note that 'ch' may be a
431 * function key at this point, so we mustn't strip _those_.
434 if ((ch < KEY_MIN) && (ch & 0x80))
437 T(("wgetch returning : %s", _tracechar(ch)));
440 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
443 #ifdef NCURSES_WGETCH_EVENTS
445 wgetch_events(WINDOW *win, _nc_eventlist * evl)
450 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
451 code = _nc_wgetch(win,
467 T((T_CALLED("wgetch(%p)"), win));
468 code = _nc_wgetch(win,
470 (SP ? SP->_use_meta : 0)
471 EVENTLIST_2nd((_nc_eventlist *) 0));
481 ** Get an input character, but take care of keypad sequences, returning
482 ** an appropriate code when one matches the input. After each character
483 ** is received, set an alarm call based on ESCDELAY. If no more of the
484 ** sequence is received by the time the alarm goes off, pass through
485 ** the sequence gotten so far.
487 ** This function must be called when there are no cooked keys in queue.
488 ** (that is head==-1 || peek==head)
493 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
497 int timeleft = ESCDELAY;
499 TR(TRACE_IEVENT, ("kgetch() called"));
504 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
506 } else if (!raw_key_in_fifo()) {
507 ch = fifo_push(EVENTLIST_1st(evl));
509 peek = head; /* the keys stay uninterpreted */
512 #ifdef NCURSES_WGETCH_EVENTS
513 else if (ch == KEY_EVENT) {
514 peek = head; /* the keys stay uninterpreted */
515 return fifo_pull(); /* Remove KEY_EVENT from the queue */
522 /* If not first in queue, somebody put this key there on purpose in
523 * emergency. Consider it higher priority than the unfinished
524 * keysequence we are parsing.
527 /* assume the key is the last in fifo */
528 t_dec(); /* remove the key */
532 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
533 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
537 TR(TRACE_IEVENT, ("ptr is null"));
540 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
541 ptr, ptr->ch, ptr->value));
543 if (ptr->value != 0) { /* sequence terminated */
544 TR(TRACE_IEVENT, ("end of sequence"));
554 if (!raw_key_in_fifo()) {
557 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
558 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
559 #ifdef NCURSES_WGETCH_EVENTS
561 TR(TRACE_IEVENT, ("interrupted by a user event"));
562 /* FIXME Should have preserved remainder timeleft for reuse... */
563 peek = head; /* Restart interpreting later */
568 TR(TRACE_IEVENT, ("ran out of time"));