1 /****************************************************************************
2 * Copyright (c) 1998-2018,2019 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.138 2019/11/03 00:11: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)
91 set_escdelay(int value)
98 code = NCURSES_SP_NAME(set_escdelay) (CURRENT_SCREEN, value);
107 #endif /* NCURSES_EXT_FUNCS */
109 #if NCURSES_EXT_FUNCS
111 NCURSES_SP_NAME(get_escdelay) (NCURSES_SP_DCL0)
116 return GetEscdelay(SP_PARM);
123 return NCURSES_SP_NAME(get_escdelay) (CURRENT_SCREEN);
126 #endif /* NCURSES_EXT_FUNCS */
129 _nc_use_meta(WINDOW *win)
131 SCREEN *sp = _nc_screen_of(win);
132 return (sp ? sp->_use_meta : 0);
135 #ifdef USE_TERM_DRIVER
138 _nc_get_handle(int fd)
140 intptr_t value = _get_osfhandle(fd);
141 return (HANDLE) value;
147 * Check for mouse activity, returning nonzero if we find any.
150 check_mouse_activity(SCREEN *sp, int delay EVENTLIST_2nd(_nc_eventlist * evl))
154 #ifdef USE_TERM_DRIVER
155 TERMINAL_CONTROL_BLOCK *TCB = TCBOf(sp);
156 rc = TCBOf(sp)->drv->td_testmouse(TCBOf(sp), delay EVENTLIST_2nd(evl));
158 /* if we emulate terminfo on console, we have to use the console routine */
159 if (IsTermInfoOnConsole(sp)) {
160 HANDLE fd = _nc_get_handle(sp->_ifd);
161 rc = _nc_mingw_testmouse(sp, fd, delay EVENTLIST_2nd(evl));
164 rc = TCB->drv->td_testmouse(TCB, delay EVENTLIST_2nd(evl));
167 if ((sp->_mouse_type == M_SYSMOUSE)
168 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
173 rc = _nc_timed_wait(sp,
179 if ((sp->_mouse_type == M_SYSMOUSE)
180 && (sp->_sysmouse_head < sp->_sysmouse_tail)
182 && (errno == EINTR)) {
191 static NCURSES_INLINE int
192 fifo_peek(SCREEN *sp)
194 int ch = (peek >= 0) ? sp->_fifo[peek] : ERR;
195 TR(TRACE_IEVENT, ("peeking at %d", peek));
201 static NCURSES_INLINE int
202 fifo_pull(SCREEN *sp)
204 int ch = (head >= 0) ? sp->_fifo[head] : ERR;
206 TR(TRACE_IEVENT, ("pulling %s from %d", _nc_tracechar(sp, ch), head));
216 if (USE_TRACEF(TRACE_IEVENT)) {
218 _nc_unlock_global(tracef);
224 static NCURSES_INLINE int
225 fifo_push(SCREEN *sp EVENTLIST_2nd(_nc_eventlist * evl))
235 #ifdef NCURSES_WGETCH_EVENTS
237 #if USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
238 || (sp->_mouse_fd >= 0)
241 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
245 if (mask & TW_EVENT) {
246 T(("fifo_push: ungetch KEY_EVENT"));
247 safe_ungetch(sp, KEY_EVENT);
250 #elif USE_GPM_SUPPORT || USE_EMX_MOUSE || USE_SYSMOUSE
251 if (sp->_mouse_fd >= 0) {
252 mask = check_mouse_activity(sp, -1 EVENTLIST_2nd(evl));
256 #if USE_GPM_SUPPORT || USE_EMX_MOUSE
257 if ((sp->_mouse_fd >= 0) && (mask & TW_MOUSE)) {
258 sp->_mouse_event(sp);
264 if ((sp->_mouse_type == M_SYSMOUSE)
265 && (sp->_sysmouse_head < sp->_sysmouse_tail)) {
266 sp->_mouse_event(sp);
269 } else if ((sp->_mouse_type == M_SYSMOUSE)
270 && (mask <= 0) && errno == EINTR) {
271 sp->_mouse_event(sp);
276 #ifdef USE_TERM_DRIVER
277 if ((sp->_mouse_type == M_TERM_DRIVER)
278 && (sp->_drv_mouse_head < sp->_drv_mouse_tail)) {
279 sp->_mouse_event(sp);
285 if (NC_ISATTY(sp->_ifd) && sp->_cbreak) {
286 ch = _read_kbd(0, 1, !sp->_raw);
287 n = (ch == -1) ? -1 : 1;
288 sp->_extended_key = (ch == 0);
292 #ifdef USE_TERM_DRIVER
295 if (NC_ISATTY(sp->_ifd) && IsTermInfoOnConsole(sp) && sp->_cbreak)
296 n = _nc_mingw_console_read(sp,
297 _nc_get_handle(sp->_ifd),
301 n = CallDriver_1(sp, td_read, &buf);
304 unsigned char c2 = 0;
305 # if USE_PTHREADS_EINTR
306 # if USE_WEAK_SYMBOLS
307 if ((pthread_self) && (pthread_kill) && (pthread_equal))
309 _nc_globals.read_thread = pthread_self();
311 n = (int) read(sp->_ifd, &c2, (size_t) 1);
312 #if USE_PTHREADS_EINTR
313 _nc_globals.read_thread = 0;
319 if ((n == -1) || (n == 0)) {
320 TR(TRACE_IEVENT, ("read(%d,&ch,1)=%d, errno=%d", sp->_ifd, n, errno));
323 TR(TRACE_IEVENT, ("read %d characters", n));
325 sp->_fifo[tail] = ch;
330 TR(TRACE_IEVENT, ("pushed %s at %d", _nc_tracechar(sp, ch), tail));
332 if (USE_TRACEF(TRACE_IEVENT)) {
334 _nc_unlock_global(tracef);
340 static NCURSES_INLINE void
341 fifo_clear(SCREEN *sp)
343 memset(sp->_fifo, 0, sizeof(sp->_fifo));
348 static int kgetch(SCREEN *, bool EVENTLIST_2nd(_nc_eventlist *));
351 recur_wrefresh(WINDOW *win)
354 SCREEN *sp = _nc_screen_of(win);
355 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
358 /* temporarily switch to the window's screen to check/refresh */
359 _nc_lock_global(curses);
360 save_SP = CURRENT_SCREEN;
363 _nc_set_screen(save_SP);
364 _nc_unlock_global(curses);
367 if ((is_wintouched(win) || (win->_flags & _HASMOVED))
368 && !(win->_flags & _ISPAD)) {
374 recur_wgetnstr(WINDOW *win, char *buf)
376 SCREEN *sp = _nc_screen_of(win);
381 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
384 /* temporarily switch to the window's screen to get cooked input */
385 _nc_lock_global(curses);
386 save_SP = CURRENT_SCREEN;
388 rc = recur_wgetnstr(win, buf);
389 _nc_set_screen(save_SP);
390 _nc_unlock_global(curses);
394 sp->_called_wgetch = TRUE;
395 rc = wgetnstr(win, buf, MAXCOLUMNS);
396 sp->_called_wgetch = FALSE;
405 _nc_wgetch(WINDOW *win,
408 EVENTLIST_2nd(_nc_eventlist * evl))
413 #ifdef NCURSES_WGETCH_EVENTS
414 int event_delay = -1;
417 T((T_CALLED("_nc_wgetch(%p)"), (void *) win));
421 sp = _nc_screen_of(win);
422 if (win == 0 || sp == 0) {
426 if (cooked_key_in_fifo()) {
428 *result = fifo_pull(sp);
429 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
431 #ifdef NCURSES_WGETCH_EVENTS
432 if (evl && (evl->count == 0))
434 event_delay = _nc_eventlist_timeout(evl);
438 * Handle cooked mode. Grab a string from the screen,
439 * stuff its contents in the FIFO queue, and pop off
440 * the first character to return it.
446 !sp->_called_wgetch) {
447 char buf[MAXCOLUMNS], *bufp;
449 TR(TRACE_IEVENT, ("filling queue in cooked mode"));
451 /* ungetch in reverse order */
452 #ifdef NCURSES_WGETCH_EVENTS
453 rc = recur_wgetnstr(win, buf);
454 if (rc != KEY_EVENT && rc != ERR)
455 safe_ungetch(sp, '\n');
457 if (recur_wgetnstr(win, buf) != ERR)
458 safe_ungetch(sp, '\n');
460 for (bufp = buf + strlen(buf); bufp > buf; bufp--)
461 safe_ungetch(sp, bufp[-1]);
463 #ifdef NCURSES_WGETCH_EVENTS
464 /* Return it first */
465 if (rc == KEY_EVENT) {
469 *result = fifo_pull(sp);
470 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
473 if (win->_use_keypad != sp->_keypad_on)
474 _nc_keypad(sp, win->_use_keypad);
478 if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
479 if (head == -1) { /* fifo is empty */
482 TR(TRACE_IEVENT, ("timed delay in wgetch()"));
484 delay = (sp->_cbreak - 1) * 100;
488 #ifdef NCURSES_WGETCH_EVENTS
489 if (event_delay >= 0 && delay > event_delay)
493 TR(TRACE_IEVENT, ("delay is %d milliseconds", delay));
495 rc = check_mouse_activity(sp, delay EVENTLIST_2nd(evl));
497 #ifdef NCURSES_WGETCH_EVENTS
500 returnCode(KEY_CODE_YES);
507 /* else go on to read data available */
510 if (win->_use_keypad) {
512 * This is tricky. We only want to get special-key
513 * events one at a time. But we want to accumulate
514 * mouse events until either (a) the mouse logic tells
515 * us it's picked up a complete gesture, or (b)
516 * there's a detectable time lapse after one.
518 * Note: if the mouse code starts failing to compose
519 * press/release events into clicks, you should probably
520 * increase the wait with mouseinterval().
525 ch = kgetch(sp, win->_notimeout EVENTLIST_2nd(evl));
526 if (ch == KEY_MOUSE) {
528 if (sp->_mouse_inline(sp))
531 if (sp->_maxclick < 0)
535 && (((rc = check_mouse_activity(sp, sp->_maxclick
536 EVENTLIST_2nd(evl))) != 0
538 || !sp->_mouse_parse(sp, runcount)));
539 #ifdef NCURSES_WGETCH_EVENTS
540 if ((rc & TW_EVENT) && !(ch == KEY_EVENT)) {
541 safe_ungetch(sp, ch);
545 if (runcount > 0 && ch != KEY_MOUSE) {
546 #ifdef NCURSES_WGETCH_EVENTS
547 /* mouse event sequence ended by an event, report event */
548 if (ch == KEY_EVENT) {
549 safe_ungetch(sp, KEY_MOUSE); /* FIXME This interrupts a gesture... */
553 /* mouse event sequence ended by keystroke, store keystroke */
554 safe_ungetch(sp, ch);
560 fifo_push(sp EVENTLIST_2nd(evl));
567 if (_nc_handle_sigwinch(sp)) {
568 _nc_update_screensize(sp);
569 /* resizeterm can push KEY_RESIZE */
570 if (cooked_key_in_fifo()) {
571 *result = fifo_pull(sp);
573 * Get the ERR from queue -- it is from WINCH,
574 * so we should take it out, the "error" is handled.
576 if (fifo_peek(sp) == -1)
578 returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
586 * If echo() is in effect, display the printable version of the
587 * key on the screen. Carriage return and backspace are treated
588 * specially by Solaris curses:
590 * If carriage return is defined as a function key in the
591 * terminfo, e.g., kent, then Solaris may return either ^J (or ^M
592 * if nonl() is set) or KEY_ENTER depending on the echo() mode.
593 * We echo before translating carriage return based on nonl(),
594 * since the visual result simply moves the cursor to column 0.
596 * Backspace is a different matter. Solaris curses does not
597 * translate it to KEY_BACKSPACE if kbs=^H. This does not depend
598 * on the stty modes, but appears to be a hardcoded special case.
599 * This is a difference from ncurses, which uses the terminfo entry.
600 * However, we provide the same visual result as Solaris, moving the
601 * cursor to the left.
603 if (sp->_echo && !(win->_flags & _ISPAD)) {
604 chtype backup = (chtype) ((ch == KEY_BACKSPACE) ? '\b' : ch);
605 if (backup < KEY_MIN)
606 wechochar(win, backup);
610 * Simulate ICRNL mode
612 if ((ch == '\r') && sp->_nl)
615 /* Strip 8th-bit if so desired. We do this only for characters that
616 * are in the range 128-255, to provide compatibility with terminals
617 * that display only 7-bit characters. Note that 'ch' may be a
618 * function key at this point, so we mustn't strip _those_.
621 if ((ch < KEY_MIN) && (ch & 0x80))
624 T(("wgetch returning : %s", _nc_tracechar(sp, ch)));
627 returnCode(ch >= KEY_MIN ? KEY_CODE_YES : OK);
630 #ifdef NCURSES_WGETCH_EVENTS
632 wgetch_events(WINDOW *win, _nc_eventlist * evl)
637 T((T_CALLED("wgetch_events(%p,%p)"), (void *) win, (void *) evl));
638 code = _nc_wgetch(win,
654 T((T_CALLED("wgetch(%p)"), (void *) win));
655 code = _nc_wgetch(win,
658 EVENTLIST_2nd((_nc_eventlist *) 0));
668 ** Get an input character, but take care of keypad sequences, returning
669 ** an appropriate code when one matches the input. After each character
670 ** is received, set an alarm call based on ESCDELAY. If no more of the
671 ** sequence is received by the time the alarm goes off, pass through
672 ** the sequence gotten so far.
674 ** This function must be called when there are no cooked keys in queue.
675 ** (that is head==-1 || peek==head)
680 kgetch(SCREEN *sp, bool forever EVENTLIST_2nd(_nc_eventlist * evl))
684 int timeleft = forever ? 9999999 : GetEscdelay(sp);
686 TR(TRACE_IEVENT, ("kgetch() called"));
691 if (cooked_key_in_fifo() && sp->_fifo[head] >= KEY_MIN) {
693 } else if (!raw_key_in_fifo()) {
694 ch = fifo_push(sp EVENTLIST_2nd(evl));
696 peek = head; /* the keys stay uninterpreted */
699 #ifdef NCURSES_WGETCH_EVENTS
700 else if (ch == KEY_EVENT) {
701 peek = head; /* the keys stay uninterpreted */
702 return fifo_pull(sp); /* Remove KEY_EVENT from the queue */
709 /* If not first in queue, somebody put this key there on purpose in
710 * emergency. Consider it higher priority than the unfinished
711 * keysequence we are parsing.
714 /* assume the key is the last in fifo */
715 t_dec(); /* remove the key */
719 TR(TRACE_IEVENT, ("ch: %s", _nc_tracechar(sp, (unsigned char) ch)));
720 while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
724 TR(TRACE_IEVENT, ("ptr is null"));
727 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
728 (void *) ptr, ptr->ch, ptr->value));
730 if (ptr->value != 0) { /* sequence terminated */
731 TR(TRACE_IEVENT, ("end of sequence"));
742 if (!raw_key_in_fifo()) {
745 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
746 rc = check_mouse_activity(sp, timeleft EVENTLIST_2nd(evl));
747 #ifdef NCURSES_WGETCH_EVENTS
749 TR(TRACE_IEVENT, ("interrupted by a user event"));
750 /* FIXME Should have preserved remainder timeleft for reuse... */
751 peek = head; /* Restart interpreting later */
756 TR(TRACE_IEVENT, ("ran out of time"));