1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 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.136 2016/09/10 21:59:16 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);
127 #ifdef USE_TERM_DRIVER
130 _nc_get_handle(int fd)
132 intptr_t value = _get_osfhandle(fd);
133 return (HANDLE) value;
139 * Check for mouse activity, returning nonzero if we find any.
142 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
146 #ifdef USE_TERM_DRIVER
147 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
148 rc = TCBOf(sp)->drv->td_testmouse(TCBOf(sp), delay EVENTLIST_2nd(evl));
150 /* if we emulate terminfo on console, we have to use the console routine */
151 if (IsTermInfoOnConsole(sp)) {
152 HANDLE fd = _nc_get_handle(sp->_ifd);
153 rc = _nc_mingw_testmouse(sp, fd, delay EVENTLIST_2nd(evl));
156 rc = TCB->drv->td_testmouse(TCB, delay EVENTLIST_2nd(evl));
159 if ((sp->_mouse_type == M_SYSMOUSE)
160 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
165 rc = _nc_timed_wait(sp,
171 if ((sp->_mouse_type == M_SYSMOUSE)
172 && (sp->_sysmouse_head < sp->_sysmouse_tail)
174 && (errno == EINTR)) {
183 static NCURSES_INLINE int
184 fifo_peek(SCREEN *sp)
186 int ch = (peek >= 0) ? sp->_fifo[peek] : ERR;
187 TR(TRACE_IEVENT, ("peeking at %d", peek));
193 static NCURSES_INLINE int
194 fifo_pull(SCREEN *sp)
196 int ch = (head >= 0) ? sp->_fifo[head] : ERR;
198 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
208 if (USE_TRACEF(TRACE_IEVENT)) {
210 _nc_unlock_global(tracef);
216 static NCURSES_INLINE int
217 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
227 #ifdef NCURSES_WGETCH_EVENTS
229 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
230 || (sp->_mouse_fd >= 0)
233 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
237 if (mask & TW_EVENT) {
238 T(("fifo_push: ungetch KEY_EVENT"));
239 safe_ungetch(sp, KEY_EVENT);
242 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
243 if (sp->_mouse_fd >= 0) {
244 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
248 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
249 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
250 sp->_mouse_event(sp);
256 if ((sp->_mouse_type == M_SYSMOUSE)
257 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
258 sp->_mouse_event(sp);
261 } else if ((sp->_mouse_type == M_SYSMOUSE)
262 && (mask <= 0) && errno == EINTR) {
263 sp->_mouse_event(sp);
268 #ifdef USE_TERM_DRIVER
269 if ((sp->_mouse_type == M_TERM_DRIVER)
270 && (sp->_drv_mouse_head < sp->_drv_mouse_tail)) {
271 sp->_mouse_event(sp);
277 if (NC_ISATTY(sp->_ifd) && sp->_cbreak) {
278 ch = _read_kbd(0, 1, !sp->_raw);
279 n = (ch == -1) ? -1 : 1;
280 sp->_extended_key = (ch == 0);
284 #ifdef USE_TERM_DRIVER
287 if (NC_ISATTY(sp->_ifd) && IsTermInfoOnConsole(sp) && sp->_cbreak)
288 n = _nc_mingw_console_read(sp,
289 _nc_get_handle(sp->_ifd),
293 n = CallDriver_1(sp, td_read, &buf);
296 unsigned char c2 = 0;
297 # if USE_PTHREADS_EINTR
298 # if USE_WEAK_SYMBOLS
299 if ((pthread_self) && (pthread_kill) && (pthread_equal))
301 _nc_globals.read_thread = pthread_self();
303 n = (int) read(sp->_ifd, &c2, (size_t) 1);
304 #if USE_PTHREADS_EINTR
305 _nc_globals.read_thread = 0;
311 if ((n == -1) || (n == 0)) {
312 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
315 TR(TRACE_IEVENT, ("read %d characters", n));
317 sp->_fifo[tail] = ch;
322 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
324 if (USE_TRACEF(TRACE_IEVENT)) {
326 _nc_unlock_global(tracef);
332 static NCURSES_INLINE void
333 fifo_clear(SCREEN *sp)
335 memset(sp->_fifo, 0, sizeof(sp->_fifo));
340 static int kgetch(SCREEN *, bool EVENTLIST_2nd(_nc_eventlist *));
343 recur_wrefresh(WINDOW *win)
346 SCREEN *sp = _nc_screen_of(win);
347 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
350 /* temporarily switch to the window's screen to check/refresh */
351 _nc_lock_global(curses);
352 save_SP = CURRENT_SCREEN;
355 _nc_set_screen(save_SP);
356 _nc_unlock_global(curses);
359 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
360 && !(win->_flags & _ISPAD)) {
366 recur_wgetnstr(WINDOW *win, char *buf)
368 SCREEN *sp = _nc_screen_of(win);
373 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
376 /* temporarily switch to the window's screen to get cooked input */
377 _nc_lock_global(curses);
378 save_SP = CURRENT_SCREEN;
380 rc = recur_wgetnstr(win, buf);
381 _nc_set_screen(save_SP);
382 _nc_unlock_global(curses);
386 sp->_called_wgetch = TRUE;
387 rc = wgetnstr(win, buf, MAXCOLUMNS);
388 sp->_called_wgetch = FALSE;
397 _nc_wgetch(WINDOW *win,
400 EVENTLIST_2nd(_nc_eventlist * evl))
405 #ifdef NCURSES_WGETCH_EVENTS
406 int event_delay = -1;
409 T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
413 sp = _nc_screen_of(win);
414 if (win == 0 || sp == 0) {
418 if (cooked_key_in_fifo()) {
420 *result = fifo_pull(sp);
421 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
423 #ifdef NCURSES_WGETCH_EVENTS
424 if (evl && (evl->count == 0))
426 event_delay = _nc_eventlist_timeout(evl);
430 * Handle cooked mode. Grab a string from the screen,
431 * stuff its contents in the FIFO queue, and pop off
432 * the first character to return it.
438 !sp->_called_wgetch) {
439 char buf[MAXCOLUMNS], *bufp;
441 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
443 /* ungetch in reverse order */
444 #ifdef NCURSES_WGETCH_EVENTS
445 rc = recur_wgetnstr(win, buf);
446 if (rc != KEY_EVENT && rc != ERR)
447 safe_ungetch(sp, '\n');
449 if (recur_wgetnstr(win, buf) != ERR)
450 safe_ungetch(sp, '\n');
452 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
453 safe_ungetch(sp, bufp[-1]);
455 #ifdef NCURSES_WGETCH_EVENTS
456 /* Return it first */
457 if (rc == KEY_EVENT) {
461 *result = fifo_pull(sp);
462 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
465 if (win->_use_keypad != sp->_keypad_on)
466 _nc_keypad(sp, win->_use_keypad);
470 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
471 if (head == -1) { /* fifo is empty */
474 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
476 delay = (sp->_cbreak - 1) * 100;
480 #ifdef NCURSES_WGETCH_EVENTS
481 if (event_delay >= 0 && delay > event_delay)
485 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
487 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
489 #ifdef NCURSES_WGETCH_EVENTS
492 returnCode(KEY_CODE_YES);
499 /* else go on to read data available */
502 if (win->_use_keypad) {
504 * This is tricky. We only want to get special-key
505 * events one at a time. But we want to accumulate
506 * mouse events until either (a) the mouse logic tells
507 * us it's picked up a complete gesture, or (b)
508 * there's a detectable time lapse after one.
510 * Note: if the mouse code starts failing to compose
511 * press/release events into clicks, you should probably
512 * increase the wait with mouseinterval().
517 ch = kgetch(sp, win->_notimeout EVENTLIST_2nd(evl));
518 if (ch == KEY_MOUSE) {
520 if (sp->_mouse_inline(sp))
523 if (sp->_maxclick < 0)
527 && (((rc = check_mouse_activity(sp, sp->_maxclick
528 EVENTLIST_2nd(evl))) != 0
530 || !sp->_mouse_parse(sp, runcount)));
531 #ifdef NCURSES_WGETCH_EVENTS
532 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
533 safe_ungetch(sp, ch);
537 if (runcount > 0 && ch != KEY_MOUSE) {
538 #ifdef NCURSES_WGETCH_EVENTS
539 /* mouse event sequence ended by an event, report event */
540 if (ch == KEY_EVENT) {
541 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
545 /* mouse event sequence ended by keystroke, store keystroke */
546 safe_ungetch(sp, ch);
552 fifo_push(sp EVENTLIST_2nd(evl));
559 if (_nc_handle_sigwinch(sp)) {
560 _nc_update_screensize(sp);
561 /* resizeterm can push KEY_RESIZE */
562 if (cooked_key_in_fifo()) {
563 *result = fifo_pull(sp);
565 * Get the ERR from queue -- it is from WINCH,
566 * so we should take it out, the "error" is handled.
568 if (fifo_peek(sp) == -1)
570 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
578 * If echo() is in effect, display the printable version of the
579 * key on the screen. Carriage return and backspace are treated
580 * specially by Solaris curses:
582 * If carriage return is defined as a function key in the
583 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
584 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
585 * We echo before translating carriage return based on nonl(),
586 * since the visual result simply moves the cursor to column 0.
588 * Backspace is a different matter. Solaris curses does not
589 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
590 * on the stty modes, but appears to be a hardcoded special case.
591 * This is a difference from ncurses, which uses the terminfo entry.
592 * However, we provide the same visual result as Solaris, moving the
593 * cursor to the left.
595 if (sp->_echo && !(win->_flags & _ISPAD)) {
596 chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
597 if (backup < KEY_MIN)
598 wechochar(win, backup);
602 * Simulate ICRNL mode
604 if ((ch == '\r') && sp->_nl)
607 /* Strip 8th-bit if so desired. We do this only for characters that
608 * are in the range 128-255, to provide compatibility with terminals
609 * that display only 7-bit characters. Note that 'ch' may be a
610 * function key at this point, so we mustn't strip _those_.
613 if ((ch < KEY_MIN) && (ch & 0x80))
616 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
619 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
622 #ifdef NCURSES_WGETCH_EVENTS
624 wgetch_events(WINDOW *win, _nc_eventlist * evl)
629 T((T_CALLED("wgetch_events(%p,%p)"), (void *) win, (void *) evl));
630 code = _nc_wgetch(win,
646 T((T_CALLED("wgetch(%p)"), (void *) win));
647 code = _nc_wgetch(win,
650 EVENTLIST_2nd((_nc_eventlist *) 0));
660 ** Get an input character, but take care of keypad sequences, returning
661 ** an appropriate code when one matches the input. After each character
662 ** is received, set an alarm call based on ESCDELAY. If no more of the
663 ** sequence is received by the time the alarm goes off, pass through
664 ** the sequence gotten so far.
666 ** This function must be called when there are no cooked keys in queue.
667 ** (that is head==-1 || peek==head)
672 kgetch(SCREEN *sp, bool forever EVENTLIST_2nd(_nc_eventlist * evl))
676 int timeleft = forever ? 9999999 : GetEscdelay(sp);
678 TR(TRACE_IEVENT, ("kgetch() called"));
683 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
685 } else if (!raw_key_in_fifo()) {
686 ch = fifo_push(sp EVENTLIST_2nd(evl));
688 peek = head; /* the keys stay uninterpreted */
691 #ifdef NCURSES_WGETCH_EVENTS
692 else if (ch == KEY_EVENT) {
693 peek = head; /* the keys stay uninterpreted */
694 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
701 /* If not first in queue, somebody put this key there on purpose in
702 * emergency. Consider it higher priority than the unfinished
703 * keysequence we are parsing.
706 /* assume the key is the last in fifo */
707 t_dec(); /* remove the key */
711 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
712 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
716 TR(TRACE_IEVENT, ("ptr is null"));
719 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
720 (void *) ptr, ptr->ch, ptr->value));
722 if (ptr->value != 0) { /* sequence terminated */
723 TR(TRACE_IEVENT, ("end of sequence"));
734 if (!raw_key_in_fifo()) {
737 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
738 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
739 #ifdef NCURSES_WGETCH_EVENTS
741 TR(TRACE_IEVENT, ("interrupted by a user event"));
742 /* FIXME Should have preserved remainder timeleft for reuse... */
743 peek = head; /* Restart interpreting later */
748 TR(TRACE_IEVENT, ("ran out of time"));