1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
39 ** The routine getch().
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_getch.c,v 1.101 2009/02/28 19:16:40 tom Exp $")
47 #include <fifo_defs.h>
50 #define GetEscdelay(sp) (sp)->_ESCDELAY
52 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
54 return SP ? GetEscdelay(SP) : 1000;
57 #define GetEscdelay(sp) ESCDELAY
58 NCURSES_EXPORT_VAR (int)
59 ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
64 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_DCLx int value)
69 SP_PARM->_ESCDELAY = value;
82 set_escdelay(int value)
84 return NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
87 #endif /* NCURSES_EXT_FUNCS */
90 _nc_use_meta(WINDOW *win)
92 SCREEN *sp = _nc_screen_of(win);
93 return (sp ? sp->_use_meta : 0);
96 #ifdef NCURSES_WGETCH_EVENTS
97 #define TWAIT_MASK (TW_ANY | TW_EVENT)
99 #define TWAIT_MASK TW_ANY
103 * Check for mouse activity, returning nonzero if we find any.
106 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
111 if ((sp->_mouse_type == M_SYSMOUSE)
112 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
116 rc = _nc_timed_wait(sp, TWAIT_MASK, delay, (int *) 0 EVENTLIST_2nd(evl));
118 if ((sp->_mouse_type == M_SYSMOUSE)
119 && (sp->_sysmouse_head < sp->_sysmouse_tail)
121 && (errno == EINTR)) {
128 static NCURSES_INLINE int
129 fifo_peek(SCREEN *sp)
131 int ch = sp->_fifo[peek];
132 TR(TRACE_IEVENT, ("peeking at %d", peek));
138 static NCURSES_INLINE int
139 fifo_pull(SCREEN *sp)
142 ch = sp->_fifo[head];
143 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
152 if (USE_TRACEF(TRACE_IEVENT)) {
154 _nc_unlock_global(tracef);
160 static NCURSES_INLINE int
161 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
176 #ifdef NCURSES_WGETCH_EVENTS
178 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
179 || (sp->_mouse_fd >= 0)
182 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
186 if (mask & TW_EVENT) {
187 T(("fifo_push: ungetch KEY_EVENT"));
188 _nc_ungetch(sp, KEY_EVENT);
191 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
192 if (sp->_mouse_fd >= 0) {
193 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
197 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
198 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
199 sp->_mouse_event(sp);
205 if ((sp->_mouse_type == M_SYSMOUSE)
206 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
207 sp->_mouse_event(sp);
210 } else if ((sp->_mouse_type == M_SYSMOUSE)
211 && (mask <= 0) && errno == EINTR) {
212 sp->_mouse_event(sp);
218 unsigned char c2 = 0;
219 n = read(sp->_ifd, &c2, 1);
225 * Under System V curses with non-restarting signals, getch() returns
226 * with value ERR when a handled signal keeps it from completing.
227 * If signals restart system calls, OTOH, the signal is invisible
228 * except to its handler.
230 * We don't want this difference to show. This piece of code
231 * tries to make it look like we always have restarting signals.
233 if (n <= 0 && errno == EINTR)
237 if ((n == -1) || (n == 0)) {
238 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
241 TR(TRACE_IEVENT, ("read %d characters", n));
243 sp->_fifo[tail] = ch;
248 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
250 if (USE_TRACEF(TRACE_IEVENT)) {
252 _nc_unlock_global(tracef);
258 static NCURSES_INLINE void
259 fifo_clear(SCREEN *sp)
261 memset(sp->_fifo, 0, sizeof(sp->_fifo));
266 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
269 recur_wrefresh(WINDOW *win)
272 SCREEN *sp = _nc_screen_of(win);
273 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
276 /* temporarily switch to the window's screen to check/refresh */
277 _nc_lock_global(curses);
278 save_SP = CURRENT_SCREEN;
281 _nc_set_screen(save_SP);
282 _nc_unlock_global(curses);
285 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
286 && !(win->_flags & _ISPAD)) {
292 recur_wgetnstr(WINDOW *win, char *buf)
294 SCREEN *sp = _nc_screen_of(win);
299 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
302 /* temporarily switch to the window's screen to get cooked input */
303 _nc_lock_global(curses);
304 save_SP = CURRENT_SCREEN;
306 rc = recur_wgetnstr(win, buf);
307 _nc_set_screen(save_SP);
308 _nc_unlock_global(curses);
312 sp->_called_wgetch = TRUE;
313 rc = wgetnstr(win, buf, MAXCOLUMNS);
314 sp->_called_wgetch = FALSE;
323 _nc_wgetch(WINDOW *win,
324 unsigned long *result,
326 EVENTLIST_2nd(_nc_eventlist * evl))
330 #ifdef NCURSES_WGETCH_EVENTS
331 long event_delay = -1;
334 T((T_CALLED("_nc_wgetch(%p)"), win));
338 sp = _nc_screen_of(win);
339 if (win == 0 || sp == 0) {
343 if (cooked_key_in_fifo()) {
345 *result = fifo_pull(sp);
346 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
348 #ifdef NCURSES_WGETCH_EVENTS
349 if (evl && (evl->count == 0))
351 event_delay = _nc_eventlist_timeout(evl);
355 * Handle cooked mode. Grab a string from the screen,
356 * stuff its contents in the FIFO queue, and pop off
357 * the first character to return it.
363 !sp->_called_wgetch) {
364 char buf[MAXCOLUMNS], *bufp;
367 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
369 rc = recur_wgetnstr(win, buf);
371 /* ungetch in reverse order */
372 #ifdef NCURSES_WGETCH_EVENTS
375 _nc_ungetch(sp, '\n');
376 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
377 _nc_ungetch(sp, bufp[-1]);
379 #ifdef NCURSES_WGETCH_EVENTS
380 /* Return it first */
381 if (rc == KEY_EVENT) {
385 *result = fifo_pull(sp);
386 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
389 if (win->_use_keypad != sp->_keypad_on)
390 _nc_keypad(sp, win->_use_keypad);
394 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
395 if (head == -1) { /* fifo is empty */
399 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
401 delay = (sp->_cbreak - 1) * 100;
405 #ifdef NCURSES_WGETCH_EVENTS
406 if (event_delay >= 0 && delay > event_delay)
410 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
412 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
414 #ifdef NCURSES_WGETCH_EVENTS
417 returnCode(KEY_CODE_YES);
424 /* else go on to read data available */
427 if (win->_use_keypad) {
429 * This is tricky. We only want to get special-key
430 * events one at a time. But we want to accumulate
431 * mouse events until either (a) the mouse logic tells
432 * us it's picked up a complete gesture, or (b)
433 * there's a detectable time lapse after one.
435 * Note: if the mouse code starts failing to compose
436 * press/release events into clicks, you should probably
437 * increase the wait with mouseinterval().
443 ch = kgetch(sp EVENTLIST_2nd(evl));
444 if (ch == KEY_MOUSE) {
446 if (sp->_mouse_inline(sp))
449 if (sp->_maxclick < 0)
453 && (((rc = check_mouse_activity(sp, sp->_maxclick
454 EVENTLIST_2nd(evl))) != 0
456 || !sp->_mouse_parse(sp, runcount)));
457 #ifdef NCURSES_WGETCH_EVENTS
458 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
463 if (runcount > 0 && ch != KEY_MOUSE) {
464 #ifdef NCURSES_WGETCH_EVENTS
465 /* mouse event sequence ended by an event, report event */
466 if (ch == KEY_EVENT) {
467 _nc_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
471 /* mouse event sequence ended by keystroke, store keystroke */
478 fifo_push(sp EVENTLIST_2nd(evl));
484 if (_nc_handle_sigwinch(sp)) {
485 _nc_update_screensize(sp);
486 /* resizeterm can push KEY_RESIZE */
487 if (cooked_key_in_fifo()) {
488 *result = fifo_pull(sp);
489 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
497 * If echo() is in effect, display the printable version of the
498 * key on the screen. Carriage return and backspace are treated
499 * specially by Solaris curses:
501 * If carriage return is defined as a function key in the
502 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
503 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
504 * We echo before translating carriage return based on nonl(),
505 * since the visual result simply moves the cursor to column 0.
507 * Backspace is a different matter. Solaris curses does not
508 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
509 * on the stty modes, but appears to be a hardcoded special case.
510 * This is a difference from ncurses, which uses the terminfo entry.
511 * However, we provide the same visual result as Solaris, moving the
512 * cursor to the left.
514 if (sp->_echo && !(win->_flags & _ISPAD)) {
515 chtype backup = (ch == KEY_BACKSPACE) ? '\b' : ch;
516 if (backup < KEY_MIN)
517 wechochar(win, backup);
521 * Simulate ICRNL mode
523 if ((ch == '\r') && sp->_nl)
526 /* Strip 8th-bit if so desired. We do this only for characters that
527 * are in the range 128-255, to provide compatibility with terminals
528 * that display only 7-bit characters. Note that 'ch' may be a
529 * function key at this point, so we mustn't strip _those_.
532 if ((ch < KEY_MIN) && (ch & 0x80))
535 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
538 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
541 #ifdef NCURSES_WGETCH_EVENTS
543 wgetch_events(WINDOW *win, _nc_eventlist * evl)
548 T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
549 code = _nc_wgetch(win,
565 T((T_CALLED("wgetch(%p)"), win));
566 code = _nc_wgetch(win,
569 EVENTLIST_2nd((_nc_eventlist *) 0));
579 ** Get an input character, but take care of keypad sequences, returning
580 ** an appropriate code when one matches the input. After each character
581 ** is received, set an alarm call based on ESCDELAY. If no more of the
582 ** sequence is received by the time the alarm goes off, pass through
583 ** the sequence gotten so far.
585 ** This function must be called when there are no cooked keys in queue.
586 ** (that is head==-1 || peek==head)
591 kgetch(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
595 int timeleft = GetEscdelay(sp);
597 TR(TRACE_IEVENT, ("kgetch() called"));
602 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
604 } else if (!raw_key_in_fifo()) {
605 ch = fifo_push(sp EVENTLIST_2nd(evl));
607 peek = head; /* the keys stay uninterpreted */
610 #ifdef NCURSES_WGETCH_EVENTS
611 else if (ch == KEY_EVENT) {
612 peek = head; /* the keys stay uninterpreted */
613 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
620 /* If not first in queue, somebody put this key there on purpose in
621 * emergency. Consider it higher priority than the unfinished
622 * keysequence we are parsing.
625 /* assume the key is the last in fifo */
626 t_dec(); /* remove the key */
630 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
631 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
635 TR(TRACE_IEVENT, ("ptr is null"));
638 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
639 ptr, ptr->ch, ptr->value));
641 if (ptr->value != 0) { /* sequence terminated */
642 TR(TRACE_IEVENT, ("end of sequence"));
652 if (!raw_key_in_fifo()) {
655 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
656 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
657 #ifdef NCURSES_WGETCH_EVENTS
659 TR(TRACE_IEVENT, ("interrupted by a user event"));
660 /* FIXME Should have preserved remainder timeleft for reuse... */
661 peek = head; /* Restart interpreting later */
666 TR(TRACE_IEVENT, ("ran out of time"));