1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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.121 2010/12/25 23:24:04 tom Exp $")
47 #include <fifo_defs.h>
50 #define GetEscdelay(sp) *_nc_ptr_Escdelay(sp)
52 NCURSES_PUBLIC_VAR(ESCDELAY
) (void)
54 return *(_nc_ptr_Escdelay(CURRENT_SCREEN
));
58 _nc_ptr_Escdelay(SCREEN
*sp
)
60 return ptrEscdelay(sp
);
63 #define GetEscdelay(sp) ESCDELAY
64 NCURSES_EXPORT_VAR(int) ESCDELAY
= 1000;
69 NCURSES_SP_NAME(set_escdelay
) (NCURSES_SP_DCLx
int value
)
87 set_escdelay(int value
)
91 code
= NCURSES_SP_NAME(set_escdelay
) (CURRENT_SCREEN
, value
);
99 #endif /* NCURSES_EXT_FUNCS */
101 #if NCURSES_EXT_FUNCS
103 NCURSES_SP_NAME(get_escdelay
) (NCURSES_SP_DCL0
)
108 return GetEscdelay(SP_PARM
);
115 return NCURSES_SP_NAME(get_escdelay
) (CURRENT_SCREEN
);
118 #endif /* NCURSES_EXT_FUNCS */
121 _nc_use_meta(WINDOW
*win
)
123 SCREEN
*sp
= _nc_screen_of(win
);
124 return (sp
? sp
->_use_meta
: 0);
128 * Check for mouse activity, returning nonzero if we find any.
131 check_mouse_activity(SCREEN
*sp
, int delay
EVENTLIST_2nd(_nc_eventlist
* evl
))
135 #ifdef USE_TERM_DRIVER
136 rc
= TCBOf(sp
)->drv
->testmouse(TCBOf(sp
), delay
);
139 if ((sp
->_mouse_type
== M_SYSMOUSE
)
140 && (sp
->_sysmouse_head
< sp
->_sysmouse_tail
)) {
145 rc
= _nc_timed_wait(sp
,
151 if ((sp
->_mouse_type
== M_SYSMOUSE
)
152 && (sp
->_sysmouse_head
< sp
->_sysmouse_tail
)
154 && (errno
== EINTR
)) {
163 static NCURSES_INLINE
int
164 fifo_peek(SCREEN
*sp
)
166 int ch
= sp
->_fifo
[peek
];
167 TR(TRACE_IEVENT
, ("peeking at %d", peek
));
173 static NCURSES_INLINE
int
174 fifo_pull(SCREEN
*sp
)
177 ch
= sp
->_fifo
[head
];
178 TR(TRACE_IEVENT
, ("pulling %s from %d", _nc_tracechar(sp
, ch
), head
));
187 if (USE_TRACEF(TRACE_IEVENT
)) {
189 _nc_unlock_global(tracef
);
195 static NCURSES_INLINE
int
196 fifo_push(SCREEN
*sp
EVENTLIST_2nd(_nc_eventlist
* evl
))
211 #ifdef NCURSES_WGETCH_EVENTS
213 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
214 || (sp
->_mouse_fd
>= 0)
217 mask
= check_mouse_activity(sp
, -1 EVENTLIST_2nd(evl
));
221 if (mask
& TW_EVENT
) {
222 T(("fifo_push: ungetch KEY_EVENT"));
223 safe_ungetch(sp
, KEY_EVENT
);
226 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
227 if (sp
->_mouse_fd
>= 0) {
228 mask
= check_mouse_activity(sp
, -1 EVENTLIST_2nd(evl
));
232 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
233 if ((sp
->_mouse_fd
>= 0) && (mask
& TW_MOUSE
)) {
234 sp
->_mouse_event(sp
);
240 if ((sp
->_mouse_type
== M_SYSMOUSE
)
241 && (sp
->_sysmouse_head
< sp
->_sysmouse_tail
)) {
242 sp
->_mouse_event(sp
);
245 } else if ((sp
->_mouse_type
== M_SYSMOUSE
)
246 && (mask
<= 0) && errno
== EINTR
) {
247 sp
->_mouse_event(sp
);
252 #ifdef USE_TERM_DRIVER
253 if ((sp
->_mouse_type
== M_TERM_DRIVER
)
254 && (sp
->_drv_mouse_head
< sp
->_drv_mouse_tail
)) {
255 sp
->_mouse_event(sp
);
261 #ifdef USE_TERM_DRIVER
263 n
= CallDriver_1(sp
, read
, &buf
);
266 unsigned char c2
= 0;
267 # if USE_PTHREADS_EINTR
268 # if USE_WEAK_SYMBOLS
269 if ((pthread_self
) && (pthread_kill
) && (pthread_equal
))
271 _nc_globals
.read_thread
= pthread_self();
273 n
= (int) read(sp
->_ifd
, &c2
, 1);
274 #if USE_PTHREADS_EINTR
275 _nc_globals
.read_thread
= 0;
283 * Under System V curses with non-restarting signals, getch() returns
284 * with value ERR when a handled signal keeps it from completing.
285 * If signals restart system calls, OTOH, the signal is invisible
286 * except to its handler.
288 * We don't want this difference to show. This piece of code
289 * tries to make it look like we always have restarting signals.
291 if (n
<= 0 && errno
== EINTR
292 # if USE_PTHREADS_EINTR
293 && (_nc_globals
.have_sigwinch
== 0)
299 if ((n
== -1) || (n
== 0)) {
300 TR(TRACE_IEVENT
, ("read(%d,&ch,1)=%d, errno=%d", sp
->_ifd
, n
, errno
));
303 TR(TRACE_IEVENT
, ("read %d characters", n
));
305 sp
->_fifo
[tail
] = ch
;
310 TR(TRACE_IEVENT
, ("pushed %s at %d", _nc_tracechar(sp
, ch
), tail
));
312 if (USE_TRACEF(TRACE_IEVENT
)) {
314 _nc_unlock_global(tracef
);
320 static NCURSES_INLINE
void
321 fifo_clear(SCREEN
*sp
)
323 memset(sp
->_fifo
, 0, sizeof(sp
->_fifo
));
328 static int kgetch(SCREEN
*EVENTLIST_2nd(_nc_eventlist
* evl
));
331 recur_wrefresh(WINDOW
*win
)
334 SCREEN
*sp
= _nc_screen_of(win
);
335 if (_nc_use_pthreads
&& sp
!= CURRENT_SCREEN
) {
338 /* temporarily switch to the window's screen to check/refresh */
339 _nc_lock_global(curses
);
340 save_SP
= CURRENT_SCREEN
;
343 _nc_set_screen(save_SP
);
344 _nc_unlock_global(curses
);
347 if ((is_wintouched(win
) || (win
->_flags
& _HASMOVED
))
348 && !(win
->_flags
& _ISPAD
)) {
354 recur_wgetnstr(WINDOW
*win
, char *buf
)
356 SCREEN
*sp
= _nc_screen_of(win
);
361 if (_nc_use_pthreads
&& sp
!= CURRENT_SCREEN
) {
364 /* temporarily switch to the window's screen to get cooked input */
365 _nc_lock_global(curses
);
366 save_SP
= CURRENT_SCREEN
;
368 rc
= recur_wgetnstr(win
, buf
);
369 _nc_set_screen(save_SP
);
370 _nc_unlock_global(curses
);
374 sp
->_called_wgetch
= TRUE
;
375 rc
= wgetnstr(win
, buf
, MAXCOLUMNS
);
376 sp
->_called_wgetch
= FALSE
;
385 _nc_wgetch(WINDOW
*win
,
388 EVENTLIST_2nd(_nc_eventlist
* evl
))
393 #ifdef NCURSES_WGETCH_EVENTS
394 long event_delay
= -1;
397 T((T_CALLED("_nc_wgetch(%p)"), (void *) win
));
401 sp
= _nc_screen_of(win
);
402 if (win
== 0 || sp
== 0) {
406 if (cooked_key_in_fifo()) {
408 *result
= fifo_pull(sp
);
409 returnCode(*result
>= KEY_MIN
? KEY_CODE_YES
: OK
);
411 #ifdef NCURSES_WGETCH_EVENTS
412 if (evl
&& (evl
->count
== 0))
414 event_delay
= _nc_eventlist_timeout(evl
);
418 * Handle cooked mode. Grab a string from the screen,
419 * stuff its contents in the FIFO queue, and pop off
420 * the first character to return it.
426 !sp
->_called_wgetch
) {
427 char buf
[MAXCOLUMNS
], *bufp
;
429 TR(TRACE_IEVENT
, ("filling queue in cooked mode"));
431 /* ungetch in reverse order */
432 #ifdef NCURSES_WGETCH_EVENTS
433 rc
= recur_wgetnstr(win
, buf
);
435 safe_ungetch(sp
, '\n');
437 (void) recur_wgetnstr(win
, buf
);
438 safe_ungetch(sp
, '\n');
440 for (bufp
= buf
+ strlen(buf
); bufp
> buf
; bufp
--)
441 safe_ungetch(sp
, bufp
[-1]);
443 #ifdef NCURSES_WGETCH_EVENTS
444 /* Return it first */
445 if (rc
== KEY_EVENT
) {
449 *result
= fifo_pull(sp
);
450 returnCode(*result
>= KEY_MIN
? KEY_CODE_YES
: OK
);
453 if (win
->_use_keypad
!= sp
->_keypad_on
)
454 _nc_keypad(sp
, win
->_use_keypad
);
458 if (win
->_notimeout
|| (win
->_delay
>= 0) || (sp
->_cbreak
> 1)) {
459 if (head
== -1) { /* fifo is empty */
462 TR(TRACE_IEVENT
, ("timed delay in wgetch()"));
464 delay
= (sp
->_cbreak
- 1) * 100;
468 #ifdef NCURSES_WGETCH_EVENTS
469 if (event_delay
>= 0 && delay
> event_delay
)
473 TR(TRACE_IEVENT
, ("delay is %d milliseconds", delay
));
475 rc
= check_mouse_activity(sp
, delay
EVENTLIST_2nd(evl
));
477 #ifdef NCURSES_WGETCH_EVENTS
480 returnCode(KEY_CODE_YES
);
487 /* else go on to read data available */
490 if (win
->_use_keypad
) {
492 * This is tricky. We only want to get special-key
493 * events one at a time. But we want to accumulate
494 * mouse events until either (a) the mouse logic tells
495 * us it's picked up a complete gesture, or (b)
496 * there's a detectable time lapse after one.
498 * Note: if the mouse code starts failing to compose
499 * press/release events into clicks, you should probably
500 * increase the wait with mouseinterval().
505 ch
= kgetch(sp
EVENTLIST_2nd(evl
));
506 if (ch
== KEY_MOUSE
) {
508 if (sp
->_mouse_inline(sp
))
511 if (sp
->_maxclick
< 0)
515 && (((rc
= check_mouse_activity(sp
, sp
->_maxclick
516 EVENTLIST_2nd(evl
))) != 0
518 || !sp
->_mouse_parse(sp
, runcount
)));
519 #ifdef NCURSES_WGETCH_EVENTS
520 if ((rc
& TW_EVENT
) && !(ch
== KEY_EVENT
)) {
521 safe_ungetch(sp
, ch
);
525 if (runcount
> 0 && ch
!= KEY_MOUSE
) {
526 #ifdef NCURSES_WGETCH_EVENTS
527 /* mouse event sequence ended by an event, report event */
528 if (ch
== KEY_EVENT
) {
529 safe_ungetch(sp
, KEY_MOUSE
); /* FIXME This interrupts a gesture... */
533 /* mouse event sequence ended by keystroke, store keystroke */
534 safe_ungetch(sp
, ch
);
540 fifo_push(sp
EVENTLIST_2nd(evl
));
547 if (_nc_handle_sigwinch(sp
)) {
548 _nc_update_screensize(sp
);
549 /* resizeterm can push KEY_RESIZE */
550 if (cooked_key_in_fifo()) {
551 *result
= fifo_pull(sp
);
553 * Get the ERR from queue -- it is from WINCH,
554 * so we should take it out, the "error" is handled.
556 if (fifo_peek(sp
) == -1)
558 returnCode(*result
>= KEY_MIN
? KEY_CODE_YES
: OK
);
566 * If echo() is in effect, display the printable version of the
567 * key on the screen. Carriage return and backspace are treated
568 * specially by Solaris curses:
570 * If carriage return is defined as a function key in the
571 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
572 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
573 * We echo before translating carriage return based on nonl(),
574 * since the visual result simply moves the cursor to column 0.
576 * Backspace is a different matter. Solaris curses does not
577 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
578 * on the stty modes, but appears to be a hardcoded special case.
579 * This is a difference from ncurses, which uses the terminfo entry.
580 * However, we provide the same visual result as Solaris, moving the
581 * cursor to the left.
583 if (sp
->_echo
&& !(win
->_flags
& _ISPAD
)) {
584 chtype backup
= (chtype
) ((ch
== KEY_BACKSPACE
) ? '\b' : ch
);
585 if (backup
< KEY_MIN
)
586 wechochar(win
, backup
);
590 * Simulate ICRNL mode
592 if ((ch
== '\r') && sp
->_nl
)
595 /* Strip 8th-bit if so desired. We do this only for characters that
596 * are in the range 128-255, to provide compatibility with terminals
597 * that display only 7-bit characters. Note that 'ch' may be a
598 * function key at this point, so we mustn't strip _those_.
601 if ((ch
< KEY_MIN
) && (ch
& 0x80))
604 T(("wgetch returning : %s", _nc_tracechar(sp
, ch
)));
607 returnCode(ch
>= KEY_MIN
? KEY_CODE_YES
: OK
);
610 #ifdef NCURSES_WGETCH_EVENTS
612 wgetch_events(WINDOW
*win
, _nc_eventlist
* evl
)
617 T((T_CALLED("wgetch_events(%p,%p)"), win
, evl
));
618 code
= _nc_wgetch(win
,
634 T((T_CALLED("wgetch(%p)"), (void *) win
));
635 code
= _nc_wgetch(win
,
638 EVENTLIST_2nd((_nc_eventlist
*) 0));
648 ** Get an input character, but take care of keypad sequences, returning
649 ** an appropriate code when one matches the input. After each character
650 ** is received, set an alarm call based on ESCDELAY. If no more of the
651 ** sequence is received by the time the alarm goes off, pass through
652 ** the sequence gotten so far.
654 ** This function must be called when there are no cooked keys in queue.
655 ** (that is head==-1 || peek==head)
660 kgetch(SCREEN
*sp
EVENTLIST_2nd(_nc_eventlist
* evl
))
664 int timeleft
= GetEscdelay(sp
);
666 TR(TRACE_IEVENT
, ("kgetch() called"));
671 if (cooked_key_in_fifo() && sp
->_fifo
[head
] >= KEY_MIN
) {
673 } else if (!raw_key_in_fifo()) {
674 ch
= fifo_push(sp
EVENTLIST_2nd(evl
));
676 peek
= head
; /* the keys stay uninterpreted */
679 #ifdef NCURSES_WGETCH_EVENTS
680 else if (ch
== KEY_EVENT
) {
681 peek
= head
; /* the keys stay uninterpreted */
682 return fifo_pull(sp
); /* Remove KEY_EVENT from the queue */
689 /* If not first in queue, somebody put this key there on purpose in
690 * emergency. Consider it higher priority than the unfinished
691 * keysequence we are parsing.
694 /* assume the key is the last in fifo */
695 t_dec(); /* remove the key */
699 TR(TRACE_IEVENT
, ("ch: %s", _nc_tracechar(sp
, (unsigned char) ch
)));
700 while ((ptr
!= NULL
) && (ptr
->ch
!= (unsigned char) ch
))
704 TR(TRACE_IEVENT
, ("ptr is null"));
707 TR(TRACE_IEVENT
, ("ptr=%p, ch=%d, value=%d",
708 (void *) ptr
, ptr
->ch
, ptr
->value
));
710 if (ptr
->value
!= 0) { /* sequence terminated */
711 TR(TRACE_IEVENT
, ("end of sequence"));
721 if (!raw_key_in_fifo()) {
724 TR(TRACE_IEVENT
, ("waiting for rest of sequence"));
725 rc
= check_mouse_activity(sp
, timeleft
EVENTLIST_2nd(evl
));
726 #ifdef NCURSES_WGETCH_EVENTS
728 TR(TRACE_IEVENT
, ("interrupted by a user event"));
729 /* FIXME Should have preserved remainder timeleft for reuse... */
730 peek
= head
; /* Restart interpreting later */
735 TR(TRACE_IEVENT
, ("ran out of time"));