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.67 2002/09/07 17:17:59 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
59 int ch = SP->_fifo[peek];
60 TR(TRACE_IEVENT, ("peeking at %d", peek));
71 TR(TRACE_IEVENT, ("pulling %s from %d", _tracechar(ch), head));
80 if (_nc_tracing & TRACE_IEVENT)
87 fifo_push(EVENTLIST_0th(_nc_eventlist * evl))
102 #ifdef NCURSES_WGETCH_EVENTS
104 #if USE_GPM_SUPPORT || defined(USE_EMX_MOUSE)
105 || (SP->_mouse_fd >= 0)
108 mask = _nc_timed_wait(TWAIT_MASK, -1, (int *) 0, evl);
113 T(("fifo_push: ungetch KEY_EVENT"));
117 #elif USE_GPM_SUPPORT || defined(USE_EMX_MOUSE)
118 if (SP->_mouse_fd >= 0)
119 mask = _nc_timed_wait(TWAIT_MASK, -1, (int *) 0 EVENTLIST_2nd(evl));
122 #if USE_GPM_SUPPORT || defined(USE_EMX_MOUSE)
123 if ((SP->_mouse_fd >= 0) && (mask & 2)) {
124 SP->_mouse_event(SP);
130 unsigned char c2 = 0;
131 n = read(SP->_ifd, &c2, 1);
137 * Under System V curses with non-restarting signals, getch() returns
138 * with value ERR when a handled signal keeps it from completing.
139 * If signals restart system calls, OTOH, the signal is invisible
140 * except to its handler.
142 * We don't want this difference to show. This piece of code
143 * tries to make it look like we always have restarting signals.
145 if (n <= 0 && errno == EINTR)
149 if ((n == -1) || (n == 0)) {
150 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
153 TR(TRACE_IEVENT, ("read %d characters", n));
155 SP->_fifo[tail] = ch;
160 TR(TRACE_IEVENT, ("pushed %s at %d", _tracechar(ch), tail));
162 if (_nc_tracing & TRACE_IEVENT)
171 memset(SP->_fifo, 0, sizeof(SP->_fifo));
176 static int kgetch(EVENTLIST_0th(_nc_eventlist * evl));
178 #define wgetch_should_refresh(win) (\
179 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
180 && !(win->_flags & _ISPAD))
183 _nc_wgetch(WINDOW *win,
184 unsigned long *result,
186 EVENTLIST_2nd(_nc_eventlist * evl))
189 #ifdef NCURSES_WGETCH_EVENTS
190 long event_delay = -1;
193 T((T_CALLED("_nc_wgetch(%p)"), win));
199 if (cooked_key_in_fifo()) {
200 if (wgetch_should_refresh(win))
203 *result = fifo_pull();
206 #ifdef NCURSES_WGETCH_EVENTS
207 if (evl && (evl->count == 0))
209 event_delay = _nc_eventlist_timeout(evl);
213 * Handle cooked mode. Grab a string from the screen,
214 * stuff its contents in the FIFO queue, and pop off
215 * the first character to return it.
217 if (head == -1 && !SP->_raw && !SP->_cbreak) {
218 char buf[MAXCOLUMNS], *sp;
221 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
223 rc = wgetnstr(win, buf, MAXCOLUMNS);
225 /* ungetch in reverse order */
226 #ifdef NCURSES_WGETCH_EVENTS
230 for (sp = buf + strlen(buf); sp > buf; sp--)
233 #ifdef NCURSES_WGETCH_EVENTS
234 /* Return it first */
235 if (rc == KEY_EVENT) {
241 *result = fifo_pull();
245 if (win->_use_keypad != SP->_keypad_on)
246 _nc_keypad(win->_use_keypad);
248 if (wgetch_should_refresh(win))
251 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
254 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
256 delay = (SP->_cbreak - 1) * 100;
260 #ifdef NCURSES_WGETCH_EVENTS
261 if (event_delay >= 0 && delay > event_delay)
265 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
267 if (head == -1) { /* fifo is empty */
268 int rc = _nc_timed_wait(TWAIT_MASK,
273 #ifdef NCURSES_WGETCH_EVENTS
282 /* else go on to read data available */
285 if (win->_use_keypad) {
287 * This is tricky. We only want to get special-key
288 * events one at a time. But we want to accumulate
289 * mouse events until either (a) the mouse logic tells
290 * us it's picked up a complete gesture, or (b)
291 * there's a detectable time lapse after one.
293 * Note: if the mouse code starts failing to compose
294 * press/release events into clicks, you should probably
295 * increase the wait with mouseinterval().
301 ch = kgetch(EVENTLIST_1st(evl));
302 if (ch == KEY_MOUSE) {
304 if (SP->_mouse_inline(SP))
307 if (SP->_maxclick < 0)
311 && (((rc = _nc_timed_wait(TWAIT_MASK,
314 EVENTLIST_2nd(evl))) != 0
316 || !SP->_mouse_parse(runcount)));
317 #ifdef NCURSES_WGETCH_EVENTS
318 if ((rc & 4) && !ch == KEY_EVENT) {
323 if (runcount > 0 && ch != KEY_MOUSE) {
324 #ifdef NCURSES_WGETCH_EVENTS
325 /* mouse event sequence ended by an event, report event */
326 if (ch == KEY_EVENT) {
327 ungetch(KEY_MOUSE); /* FIXME This interrupts a gesture... */
331 /* mouse event sequence ended by keystroke, store keystroke */
338 fifo_push(EVENTLIST_1st(evl));
344 if (SP->_sig_winch) {
345 _nc_update_screensize();
346 /* resizeterm can push KEY_RESIZE */
347 if (cooked_key_in_fifo()) {
348 *result = fifo_pull();
349 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
357 * If echo() is in effect, display the printable version of the
358 * key on the screen. Carriage return and backspace are treated
359 * specially by Solaris curses:
361 * If carriage return is defined as a function key in the
362 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
363 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
364 * We echo before translating carriage return based on nonl(),
365 * since the visual result simply moves the cursor to column 0.
367 * Backspace is a different matter. Solaris curses does not
368 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
369 * on the stty modes, but appears to be a hardcoded special case.
370 * This is a difference from ncurses, which uses the terminfo entry.
371 * However, we provide the same visual result as Solaris, moving the
372 * cursor to the left.
374 if (SP->_echo && !(win->_flags & _ISPAD)) {
375 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
376 if (backup < KEY_MIN)
377 wechochar(win, backup);
381 * Simulate ICRNL mode
383 if ((ch == '\r') && SP->_nl)
386 /* Strip 8th-bit if so desired. We do this only for characters that
387 * are in the range 128-255, to provide compatibility with terminals
388 * that display only 7-bit characters. Note that 'ch' may be a
389 * function key at this point, so we mustn't strip _those_.
392 if ((ch < KEY_MIN) && (ch & 0x80))
395 T(("wgetch returning : %s", _tracechar(ch)));
398 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
401 #ifdef NCURSES_WGETCH_EVENTS
403 wgetch_events(WINDOW *win, _nc_eventlist * evl)
408 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
409 code = _nc_wgetch(win,
425 T((T_CALLED("wgetch(%p)"), win));
426 code = _nc_wgetch(win,
429 EVENTLIST_2nd((_nc_eventlist *) 0));
439 ** Get an input character, but take care of keypad sequences, returning
440 ** an appropriate code when one matches the input. After each character
441 ** is received, set an alarm call based on ESCDELAY. If no more of the
442 ** sequence is received by the time the alarm goes off, pass through
443 ** the sequence gotten so far.
445 ** This function must be called when there are no cooked keys in queue.
446 ** (that is head==-1 || peek==head)
451 kgetch(EVENTLIST_0th(_nc_eventlist * evl))
455 int timeleft = ESCDELAY;
457 TR(TRACE_IEVENT, ("kgetch() called"));
462 if (cooked_key_in_fifo() && SP->_fifo[head] >= KEY_MIN) {
464 } else if (!raw_key_in_fifo()) {
465 ch = fifo_push(EVENTLIST_1st(evl));
467 peek = head; /* the keys stay uninterpreted */
470 #ifdef NCURSES_WGETCH_EVENTS
471 else if (ch == KEY_EVENT) {
472 peek = head; /* the keys stay uninterpreted */
473 return fifo_pull(); /* Remove KEY_EVENT from the queue */
480 /* If not first in queue, somebody put this key there on purpose in
481 * emergency. Consider it higher priority than the unfinished
482 * keysequence we are parsing.
485 /* assume the key is the last in fifo */
486 t_dec(); /* remove the key */
490 TR(TRACE_IEVENT, ("ch: %s", _tracechar((unsigned char) ch)));
491 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
495 TR(TRACE_IEVENT, ("ptr is null"));
498 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
499 ptr, ptr->ch, ptr->value));
501 if (ptr->value != 0) { /* sequence terminated */
502 TR(TRACE_IEVENT, ("end of sequence"));
512 if (!raw_key_in_fifo()) {
515 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
516 rc = _nc_timed_wait(TWAIT_MASK,
520 #ifdef NCURSES_WGETCH_EVENTS
522 TR(TRACE_IEVENT, ("interrupted by a user event"));
523 /* FIXME Should have preserved timeleft for reusal... */
524 peek = head; /* Restart interpreting later */
529 TR(TRACE_IEVENT, ("ran out of time"));