1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2015,2016 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
40 ** The routine getch().
44 #define NEED_KEY_EVENT
45 #include <curses.priv.h>
47 MODULE_ID("$Id: lib_getch.c,v 1.141 2020/09/05 22:50:47 tom Exp $")
49 #include <fifo_defs.h>
52 #define GetEscdelay(sp) *_nc_ptr_Escdelay(sp)
54 NCURSES_PUBLIC_VAR(ESCDELAY) (void)
56 return *(_nc_ptr_Escdelay(CURRENT_SCREEN));
60 _nc_ptr_Escdelay(SCREEN *sp)
62 return ptrEscdelay(sp);
65 #define GetEscdelay(sp) ESCDELAY
66 NCURSES_EXPORT_VAR(int) ESCDELAY = 1000;
71 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_DCLx int value)
93 set_escdelay(int value)
100 code = NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
109 #endif /* NCURSES_EXT_FUNCS */
111 #if NCURSES_EXT_FUNCS
113 NCURSES_SP_NAME(get_escdelay) (NCURSES_SP_DCL0)
118 return GetEscdelay(SP_PARM);
125 return NCURSES_SP_NAME(get_escdelay) (CURRENT_SCREEN);
128 #endif /* NCURSES_EXT_FUNCS */
131 _nc_use_meta(WINDOW *win)
133 SCREEN *sp = _nc_screen_of(win);
134 return (sp ? sp->_use_meta : 0);
137 #ifdef USE_TERM_DRIVER
138 # if defined(_NC_WINDOWS) && !defined(EXP_WIN32_DRIVER)
140 _nc_get_handle(int fd)
142 intptr_t value = _get_osfhandle(fd);
143 return (HANDLE) value;
149 * Check for mouse activity, returning nonzero if we find any.
152 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
156 #ifdef USE_TERM_DRIVER
157 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
158 rc = TCBOf(sp)->drv->td_testmouse(TCBOf(sp), delay EVENTLIST_2nd(evl));
159 # if defined(EXP_WIN32_DRIVER)
160 /* if we emulate terminfo on console, we have to use the console routine */
161 if (IsTermInfoOnConsole(sp)) {
162 rc = _nc_console_testmouse(sp,
163 _nc_console_handle(sp->_ifd),
164 delay EVENTLIST_2nd(evl));
166 # elif defined(_NC_WINDOWS)
167 /* if we emulate terminfo on console, we have to use the console routine */
168 if (IsTermInfoOnConsole(sp)) {
169 HANDLE fd = _nc_get_handle(sp->_ifd);
170 rc = _nc_mingw_testmouse(sp, fd, delay EVENTLIST_2nd(evl));
173 rc = TCB->drv->td_testmouse(TCB, delay EVENTLIST_2nd(evl));
174 #else /* !USE_TERM_DRIVER */
176 if ((sp->_mouse_type == M_SYSMOUSE)
177 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
182 # if defined(EXP_WIN32_DRIVER)
183 rc = _nc_console_testmouse(sp,
184 _nc_console_handle(sp->_ifd),
188 rc = _nc_timed_wait(sp,
195 if ((sp->_mouse_type == M_SYSMOUSE)
196 && (sp->_sysmouse_head < sp->_sysmouse_tail)
198 && (errno == EINTR)) {
203 #endif /* USE_TERM_DRIVER */
207 static NCURSES_INLINE int
208 fifo_peek(SCREEN *sp)
210 int ch = (peek >= 0) ? sp->_fifo[peek] : ERR;
211 TR(TRACE_IEVENT, ("peeking at %d", peek));
217 static NCURSES_INLINE int
218 fifo_pull(SCREEN *sp)
220 int ch = (head >= 0) ? sp->_fifo[head] : ERR;
222 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
232 if (USE_TRACEF(TRACE_IEVENT)) {
234 _nc_unlock_global(tracef);
240 static NCURSES_INLINE int
241 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
251 #ifdef NCURSES_WGETCH_EVENTS
253 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
254 || (sp->_mouse_fd >= 0)
257 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
261 if (mask & TW_EVENT) {
262 T(("fifo_push: ungetch KEY_EVENT"));
263 safe_ungetch(sp, KEY_EVENT);
266 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
267 if (sp->_mouse_fd >= 0) {
268 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
272 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
273 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
274 sp->_mouse_event(sp);
280 if ((sp->_mouse_type == M_SYSMOUSE)
281 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
282 sp->_mouse_event(sp);
285 } else if ((sp->_mouse_type == M_SYSMOUSE)
286 && (mask <= 0) && errno == EINTR) {
287 sp->_mouse_event(sp);
292 #ifdef USE_TERM_DRIVER
293 if ((sp->_mouse_type == M_TERM_DRIVER)
294 && (sp->_drv_mouse_head < sp->_drv_mouse_tail)) {
295 sp->_mouse_event(sp);
301 if (NC_ISATTY(sp->_ifd) && sp->_cbreak) {
302 ch = _read_kbd(0, 1, !sp->_raw);
303 n = (ch == -1) ? -1 : 1;
304 sp->_extended_key = (ch == 0);
308 #if defined(USE_TERM_DRIVER)
310 # if defined(EXP_WIN32_DRIVER)
311 if (NC_ISATTY(sp->_ifd) && IsTermInfoOnConsole(sp) && sp->_cbreak) {
312 # if USE_PTHREADS_EINTR
313 if ((pthread_self) && (pthread_kill) && (pthread_equal))
314 _nc_globals.read_thread = pthread_self();
316 n = _nc_console_read(sp,
317 _nc_console_handle(sp->_ifd),
319 # if USE_PTHREADS_EINTR
320 _nc_globals.read_thread = 0;
323 # elif defined(_NC_WINDOWS)
324 if (NC_ISATTY(sp->_ifd) && IsTermInfoOnConsole(sp) && sp->_cbreak)
325 n = _nc_mingw_console_read(sp,
326 _nc_get_handle(sp->_ifd),
329 # endif /* EXP_WIN32_DRIVER */
330 n = CallDriver_1(sp, td_read, &buf);
332 #else /* !USE_TERM_DRIVER */
333 #if defined(EXP_WIN32_DRIVER)
336 unsigned char c2 = 0;
337 #if USE_PTHREADS_EINTR
339 if ((pthread_self) && (pthread_kill) && (pthread_equal))
341 _nc_globals.read_thread = pthread_self();
343 #if defined(EXP_WIN32_DRIVER)
344 n = _nc_console_read(sp,
345 _nc_console_handle(sp->_ifd),
349 n = (int) read(sp->_ifd, &c2, (size_t) 1);
351 #if USE_PTHREADS_EINTR
352 _nc_globals.read_thread = 0;
355 #endif /* USE_TERM_DRIVER */
358 if ((n == -1) || (n == 0)) {
359 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
362 TR(TRACE_IEVENT, ("read %d characters", n));
364 sp->_fifo[tail] = ch;
369 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
371 if (USE_TRACEF(TRACE_IEVENT)) {
373 _nc_unlock_global(tracef);
379 static NCURSES_INLINE void
380 fifo_clear(SCREEN *sp)
382 memset(sp->_fifo, 0, sizeof(sp->_fifo));
387 static int kgetch(SCREEN *, bool EVENTLIST_2nd(_nc_eventlist *));
390 recur_wrefresh(WINDOW *win)
393 SCREEN *sp = _nc_screen_of(win);
394 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
397 /* temporarily switch to the window's screen to check/refresh */
398 _nc_lock_global(curses);
399 save_SP = CURRENT_SCREEN;
402 _nc_set_screen(save_SP);
403 _nc_unlock_global(curses);
406 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
407 && !(win->_flags & _ISPAD)) {
413 recur_wgetnstr(WINDOW *win, char *buf)
415 SCREEN *sp = _nc_screen_of(win);
420 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
423 /* temporarily switch to the window's screen to get cooked input */
424 _nc_lock_global(curses);
425 save_SP = CURRENT_SCREEN;
427 rc = recur_wgetnstr(win, buf);
428 _nc_set_screen(save_SP);
429 _nc_unlock_global(curses);
433 sp->_called_wgetch = TRUE;
434 rc = wgetnstr(win, buf, MAXCOLUMNS);
435 sp->_called_wgetch = FALSE;
444 _nc_wgetch(WINDOW *win,
447 EVENTLIST_2nd(_nc_eventlist * evl))
452 #ifdef NCURSES_WGETCH_EVENTS
453 int event_delay = -1;
456 T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
460 sp = _nc_screen_of(win);
461 if (win == 0 || sp == 0) {
465 if (cooked_key_in_fifo()) {
467 *result = fifo_pull(sp);
468 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
470 #ifdef NCURSES_WGETCH_EVENTS
471 if (evl && (evl->count == 0))
473 event_delay = _nc_eventlist_timeout(evl);
477 * Handle cooked mode. Grab a string from the screen,
478 * stuff its contents in the FIFO queue, and pop off
479 * the first character to return it.
485 !sp->_called_wgetch) {
486 char buf[MAXCOLUMNS], *bufp;
488 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
490 /* ungetch in reverse order */
491 #ifdef NCURSES_WGETCH_EVENTS
492 rc = recur_wgetnstr(win, buf);
493 if (rc != KEY_EVENT && rc != ERR)
494 safe_ungetch(sp, '\n');
496 if (recur_wgetnstr(win, buf) != ERR)
497 safe_ungetch(sp, '\n');
499 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
500 safe_ungetch(sp, bufp[-1]);
502 #ifdef NCURSES_WGETCH_EVENTS
503 /* Return it first */
504 if (rc == KEY_EVENT) {
508 *result = fifo_pull(sp);
509 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
512 if (win->_use_keypad != sp->_keypad_on)
513 _nc_keypad(sp, win->_use_keypad);
517 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
518 if (head == -1) { /* fifo is empty */
521 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
523 delay = (sp->_cbreak - 1) * 100;
527 #ifdef NCURSES_WGETCH_EVENTS
528 if (event_delay >= 0 && delay > event_delay)
532 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
534 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
536 #ifdef NCURSES_WGETCH_EVENTS
539 returnCode(KEY_CODE_YES);
546 /* else go on to read data available */
549 if (win->_use_keypad) {
551 * This is tricky. We only want to get special-key
552 * events one at a time. But we want to accumulate
553 * mouse events until either (a) the mouse logic tells
554 * us it's picked up a complete gesture, or (b)
555 * there's a detectable time lapse after one.
557 * Note: if the mouse code starts failing to compose
558 * press/release events into clicks, you should probably
559 * increase the wait with mouseinterval().
564 ch = kgetch(sp, win->_notimeout EVENTLIST_2nd(evl));
565 if (ch == KEY_MOUSE) {
567 if (sp->_mouse_inline(sp))
570 if (sp->_maxclick < 0)
574 && (((rc = check_mouse_activity(sp, sp->_maxclick
575 EVENTLIST_2nd(evl))) != 0
577 || !sp->_mouse_parse(sp, runcount)));
578 #ifdef NCURSES_WGETCH_EVENTS
579 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
580 safe_ungetch(sp, ch);
584 if (runcount > 0 && ch != KEY_MOUSE) {
585 #ifdef NCURSES_WGETCH_EVENTS
586 /* mouse event sequence ended by an event, report event */
587 if (ch == KEY_EVENT) {
588 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
592 /* mouse event sequence ended by keystroke, store keystroke */
593 safe_ungetch(sp, ch);
599 fifo_push(sp EVENTLIST_2nd(evl));
606 if (_nc_handle_sigwinch(sp)) {
607 _nc_update_screensize(sp);
608 /* resizeterm can push KEY_RESIZE */
609 if (cooked_key_in_fifo()) {
610 *result = fifo_pull(sp);
612 * Get the ERR from queue -- it is from WINCH,
613 * so we should take it out, the "error" is handled.
615 if (fifo_peek(sp) == -1)
617 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
625 * If echo() is in effect, display the printable version of the
626 * key on the screen. Carriage return and backspace are treated
627 * specially by Solaris curses:
629 * If carriage return is defined as a function key in the
630 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
631 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
632 * We echo before translating carriage return based on nonl(),
633 * since the visual result simply moves the cursor to column 0.
635 * Backspace is a different matter. Solaris curses does not
636 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
637 * on the stty modes, but appears to be a hardcoded special case.
638 * This is a difference from ncurses, which uses the terminfo entry.
639 * However, we provide the same visual result as Solaris, moving the
640 * cursor to the left.
642 if (sp->_echo && !(win->_flags & _ISPAD)) {
643 chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
644 if (backup < KEY_MIN)
645 wechochar(win, backup);
649 * Simulate ICRNL mode
651 if ((ch == '\r') && sp->_nl)
654 /* Strip 8th-bit if so desired. We do this only for characters that
655 * are in the range 128-255, to provide compatibility with terminals
656 * that display only 7-bit characters. Note that 'ch' may be a
657 * function key at this point, so we mustn't strip _those_.
660 if ((ch < KEY_MIN) && (ch & 0x80))
663 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
666 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
669 #ifdef NCURSES_WGETCH_EVENTS
671 wgetch_events(WINDOW *win, _nc_eventlist * evl)
676 T((T_CALLED("wgetch_events(%p,%p)"), (void *) win, (void *) evl));
677 code = _nc_wgetch(win,
693 T((T_CALLED("wgetch(%p)"), (void *) win));
694 code = _nc_wgetch(win,
697 EVENTLIST_2nd((_nc_eventlist *) 0));
707 ** Get an input character, but take care of keypad sequences, returning
708 ** an appropriate code when one matches the input. After each character
709 ** is received, set an alarm call based on ESCDELAY. If no more of the
710 ** sequence is received by the time the alarm goes off, pass through
711 ** the sequence gotten so far.
713 ** This function must be called when there are no cooked keys in queue.
714 ** (that is head==-1 || peek==head)
719 kgetch(SCREEN *sp, bool forever EVENTLIST_2nd(_nc_eventlist * evl))
723 int timeleft = forever ? 9999999 : GetEscdelay(sp);
725 TR(TRACE_IEVENT, ("kgetch() called"));
730 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
732 } else if (!raw_key_in_fifo()) {
733 ch = fifo_push(sp EVENTLIST_2nd(evl));
735 peek = head; /* the keys stay uninterpreted */
738 #ifdef NCURSES_WGETCH_EVENTS
739 else if (ch == KEY_EVENT) {
740 peek = head; /* the keys stay uninterpreted */
741 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
748 /* If not first in queue, somebody put this key there on purpose in
749 * emergency. Consider it higher priority than the unfinished
750 * keysequence we are parsing.
753 /* assume the key is the last in fifo */
754 t_dec(); /* remove the key */
758 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
759 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
763 TR(TRACE_IEVENT, ("ptr is null"));
766 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
767 (void *) ptr, ptr->ch, ptr->value));
769 if (ptr->value != 0) { /* sequence terminated */
770 TR(TRACE_IEVENT, ("end of sequence"));
781 if (!raw_key_in_fifo()) {
784 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
785 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
786 #ifdef NCURSES_WGETCH_EVENTS
788 TR(TRACE_IEVENT, ("interrupted by a user event"));
789 /* FIXME Should have preserved remainder timeleft for reuse... */
790 peek = head; /* Restart interpreting later */
795 TR(TRACE_IEVENT, ("ran out of time"));