1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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 ****************************************************************************/
37 ** The routine getch().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_getch.c,v 1.71 2003/05/17 23:49:28 tom Exp $")
45 #include <fifo_defs.h>
47 NCURSES_EXPORT_VAR(int)
48 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
50 #ifdef NCURSES_WGETCH_EVENTS
57 * Check for mouse activity, returning nonzero if we find any.
60 check_mouse_activity(int delay EVENTLIST_2nd(_nc_eventlist * evl))
65 if ((SP->_mouse_type == M_SYSMOUSE)
66 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
70 rc = _nc_timed_wait(TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
72 if ((SP->_mouse_type == M_SYSMOUSE)
73 && (SP->_sysmouse_head < SP->_sysmouse_tail)
75 && (errno == EINTR)) {
85 int ch = SP->_fifo[peek];
86 TR(TRACE_IEVENT, ("peeking at %d", peek));
97 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
106 if (_nc_tracing & TRACE_IEVENT)
113 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
128 #ifdef NCURSES_WGETCH_EVENTS
130 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
131 || (SP->_mouse_fd >= 0)
134 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
139 T(("fifo_push: ungetch KEY_EVENT"));
143 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
144 if (SP->_mouse_fd >= 0) {
145 mask = check_mouse_activity(-1 EVENTLIST_2nd(evl));
149 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
150 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
151 SP->_mouse_event(SP);
157 if ((SP->_mouse_type == M_SYSMOUSE)
158 && (SP->_sysmouse_head < SP->_sysmouse_tail)) {
159 SP->_mouse_event(SP);
162 } else if ((SP->_mouse_type == M_SYSMOUSE)
163 && (mask <= 0) && errno == EINTR) {
164 SP->_mouse_event(SP);
170 unsigned char c2 = 0;
171 n = read(SP->_ifd, &c2, 1);
177 * Under System V curses with non-restarting signals, getch() returns
178 * with value ERR when a handled signal keeps it from completing.
179 * If signals restart system calls, OTOH, the signal is invisible
180 * except to its handler.
182 * We don't want this difference to show. This piece of code
183 * tries to make it look like we always have restarting signals.
185 if (n <= 0 && errno == EINTR)
189 if ((n == -1) || (n == 0)) {
190 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
193 TR(TRACE_IEVENT, ("read %d characters", n));
195 SP->_fifo[tail] = ch;
200 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
202 if (_nc_tracing & TRACE_IEVENT)
211 memset(SP->_fifo, 0, sizeof(SP->_fifo));
216 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
218 #define wgetch_should_refresh(win) (\
219 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
220 && !(win->_flags & _ISPAD))
223 _nc_wgetch(WINDOW *win,
224 unsigned long *result,
226 EVENTLIST_2nd(_nc_eventlist * evl))
229 #ifdef NCURSES_WGETCH_EVENTS
230 long event_delay = -1;
233 T((T_CALLED("_nc_wgetch(%p)"), win));
239 if (cooked_key_in_fifo()) {
240 if (wgetch_should_refresh(win))
243 *result = fifo_pull();
246 #ifdef NCURSES_WGETCH_EVENTS
247 if (evl && (evl->count == 0))
249 event_delay = _nc_eventlist_timeout(evl);
253 * Handle cooked mode. Grab a string from the screen,
254 * stuff its contents in the FIFO queue, and pop off
255 * the first character to return it.
261 !SP->_called_wgetch) {
262 char buf[MAXCOLUMNS], *sp;
265 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
267 SP->_called_wgetch = TRUE;
268 rc = wgetnstr(win, buf, MAXCOLUMNS);
269 SP->_called_wgetch = FALSE;
271 /* ungetch in reverse order */
272 #ifdef NCURSES_WGETCH_EVENTS
276 for (sp = buf + strlen(buf); sp > buf; sp--)
279 #ifdef NCURSES_WGETCH_EVENTS
280 /* Return it first */
281 if (rc == KEY_EVENT) {
287 *result = fifo_pull();
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)) {
300 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
302 delay = (SP->_cbreak - 1) * 100;
306 #ifdef NCURSES_WGETCH_EVENTS
307 if (event_delay >= 0 && delay > event_delay)
311 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
313 if (head == -1) { /* fifo is empty */
314 int rc = check_mouse_activity(delay EVENTLIST_2nd(evl));
316 #ifdef NCURSES_WGETCH_EVENTS
325 /* else go on to read data available */
328 if (win->_use_keypad) {
330 * This is tricky. We only want to get special-key
331 * events one at a time. But we want to accumulate
332 * mouse events until either (a) the mouse logic tells
333 * us it's picked up a complete gesture, or (b)
334 * there's a detectable time lapse after one.
336 * Note: if the mouse code starts failing to compose
337 * press/release events into clicks, you should probably
338 * increase the wait with mouseinterval().
344 ch = kgetch(EVENTLIST_1st(evl));
345 if (ch == KEY_MOUSE) {
347 if (SP->_mouse_inline(SP))
350 if (SP->_maxclick < 0)
354 && (((rc = check_mouse_activity(SP->_maxclick
355 EVENTLIST_2nd(evl))) != 0
357 || !SP->_mouse_parse(runcount)));
358 #ifdef NCURSES_WGETCH_EVENTS
359 if ((rc & 4) && !ch == KEY_EVENT) {
364 if (runcount > 0 && ch != KEY_MOUSE) {
365 #ifdef NCURSES_WGETCH_EVENTS
366 /* mouse event sequence ended by an event, report event */
367 if (ch == KEY_EVENT) {
368 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
372 /* mouse event sequence ended by keystroke, store keystroke */
379 fifo_push(EVENTLIST_1st(evl));
385 if (SP->_sig_winch) {
386 _nc_update_screensize();
387 /* resizeterm can push KEY_RESIZE */
388 if (cooked_key_in_fifo()) {
389 *result = fifo_pull();
390 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
398 * If echo() is in effect, display the printable version of the
399 * key on the screen. Carriage return and backspace are treated
400 * specially by Solaris curses:
402 * If carriage return is defined as a function key in the
403 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
404 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
405 * We echo before translating carriage return based on nonl(),
406 * since the visual result simply moves the cursor to column 0.
408 * Backspace is a different matter. Solaris curses does not
409 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
410 * on the stty modes, but appears to be a hardcoded special case.
411 * This is a difference from ncurses, which uses the terminfo entry.
412 * However, we provide the same visual result as Solaris, moving the
413 * cursor to the left.
415 if (SP->_echo && !(win->_flags & _ISPAD)) {
416 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
417 if (backup < KEY_MIN)
418 wechochar(win, backup);
422 * Simulate ICRNL mode
424 if ((ch == '\r') && SP->_nl)
427 /* Strip 8th-bit if so desired. We do this only for characters that
428 * are in the range 128-255, to provide compatibility with terminals
429 * that display only 7-bit characters. Note that 'ch' may be a
430 * function key at this point, so we mustn't strip _those_.
433 if ((ch < KEY_MIN) && (ch & 0x80))
436 T(("wgetch returning : %s", _tracechar(ch)));
439 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
442 #ifdef NCURSES_WGETCH_EVENTS
444 wgetch_events(WINDOW *win, _nc_eventlist * evl)
449 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
450 code = _nc_wgetch(win,
466 T((T_CALLED("wgetch(%p)"), win));
467 code = _nc_wgetch(win,
470 EVENTLIST_2nd((_nc_eventlist *) 0));
480 ** Get an input character, but take care of keypad sequences, returning
481 ** an appropriate code when one matches the input. After each character
482 ** is received, set an alarm call based on ESCDELAY. If no more of the
483 ** sequence is received by the time the alarm goes off, pass through
484 ** the sequence gotten so far.
486 ** This function must be called when there are no cooked keys in queue.
487 ** (that is head==-1 || peek==head)
492 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
496 int timeleft = ESCDELAY;
498 TR(TRACE_IEVENT, ("kgetch() called"));
503 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
505 } else if (!raw_key_in_fifo()) {
506 ch = fifo_push(EVENTLIST_1st(evl));
508 peek = head; /* the keys stay uninterpreted */
511 #ifdef NCURSES_WGETCH_EVENTS
512 else if (ch == KEY_EVENT) {
513 peek = head; /* the keys stay uninterpreted */
514 return fifo_pull(); /* Remove KEY_EVENT from the queue */
521 /* If not first in queue, somebody put this key there on purpose in
522 * emergency. Consider it higher priority than the unfinished
523 * keysequence we are parsing.
526 /* assume the key is the last in fifo */
527 t_dec(); /* remove the key */
531 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
532 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
536 TR(TRACE_IEVENT, ("ptr is null"));
539 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
540 ptr, ptr->ch, ptr->value));
542 if (ptr->value != 0) { /* sequence terminated */
543 TR(TRACE_IEVENT, ("end of sequence"));
553 if (!raw_key_in_fifo()) {
556 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
557 rc = check_mouse_activity(timeleft EVENTLIST_2nd(evl));
558 #ifdef NCURSES_WGETCH_EVENTS
560 TR(TRACE_IEVENT, ("interrupted by a user event"));
561 /* FIXME Should have preserved remainder timeleft for reusal... */
562 peek = head; /* Restart interpreting later */
567 TR(TRACE_IEVENT, ("ran out of time"));