1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
37 ** The routine getch().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_getch.c,v 1.40 1998/02/11 12:13:58 tom Exp $")
45 #include <fifo_defs.h>
47 int ESCDELAY = 1000; /* max interval betw. chars in funkeys, in millisecs */
49 static inline int fifo_peek(void)
51 int ch = SP->_fifo[peek];
52 T(("peeking at %d", peek));
59 static inline int fifo_pull(void)
63 T(("pulling %d from %d", ch, head));
74 if (_nc_tracing & TRACE_IEVENT) _nc_fifo_dump();
79 static inline int fifo_push(void)
84 if (tail == -1) return ERR;
92 if ((SP->_mouse_fd >= 0)
93 && (_nc_timed_wait(3, -1, (int *)0) & 2))
102 n = read(SP->_ifd, &c2, 1);
108 * Under System V curses with non-restarting signals, getch() returns
109 * with value ERR when a handled signal keeps it from completing.
110 * If signals restart system calls, OTOH, the signal is invisible
111 * except to its handler.
113 * We don't want this difference to show. This piece of code
114 * tries to make it look like we always have restarting signals.
116 if (n <= 0 && errno == EINTR)
120 if ((n == -1) || (n == 0))
122 T(("read(%d,&ch,1)=%d, errno=%d", SP->_ifd, n, errno));
125 T(("read %d characters", n));
127 SP->_fifo[tail] = ch;
132 T(("pushed %#x at %d", ch, tail));
134 if (_nc_tracing & TRACE_IEVENT) _nc_fifo_dump();
139 static inline void fifo_clear(void)
142 for (i = 0; i < FIFO_SIZE; i++)
144 head = -1; tail = peek = 0;
147 static int kgetch(WINDOW *);
149 #define wgetch_should_refresh(win) (\
150 (is_wintouched(win) || (win->_flags & _HASMOVED)) \
151 && !(win->_flags & _ISPAD))
158 T((T_CALLED("wgetch(%p)"), win));
163 if (cooked_key_in_fifo())
165 if (wgetch_should_refresh(win))
169 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch));)
174 * Handle cooked mode. Grab a string from the screen,
175 * stuff its contents in the FIFO queue, and pop off
176 * the first character to return it.
178 if (head == -1 && !SP->_raw && !SP->_cbreak)
180 char buf[MAXCOLUMNS], *sp;
182 T(("filling queue in cooked mode"));
184 wgetnstr(win, buf, MAXCOLUMNS);
186 /* ungetch in reverse order */
188 for (sp = buf+strlen(buf); sp>buf; sp--)
191 returnCode(fifo_pull());
194 if (wgetch_should_refresh(win))
197 if (!win->_notimeout && (win->_delay >= 0 || SP->_cbreak > 1)) {
200 T(("timed delay in wgetch()"));
202 delay = (SP->_cbreak - 1) * 100;
206 T(("delay is %d milliseconds", delay));
208 if (head == -1) /* fifo is empty */
209 if (!_nc_timed_wait(3, delay, (int *)0))
211 /* else go on to read data available */
214 if (win->_use_keypad) {
216 * This is tricky. We only want to get special-key
217 * events one at a time. But we want to accumulate
218 * mouse events until either (a) the mouse logic tells
219 * us it's picked up a complete gesture, or (b)
220 * there's a detectable time lapse after one.
222 * Note: if the mouse code starts failing to compose
223 * press/release events into clicks, you should probably
224 * increase the wait with mouseinterval().
233 if (SP->_mouse_inline(SP))
238 && (_nc_timed_wait(3, SP->_maxclick, (int *)0)
239 || !SP->_mouse_parse(runcount)));
240 if (runcount > 0 && ch != KEY_MOUSE)
242 /* mouse event sequence ended by keystroke, push it */
257 _nc_update_screensize();
258 /* resizeterm can push KEY_RESIZE */
259 if(cooked_key_in_fifo())
262 T(("wgetch returning (pre-cooked): %#x = %s", ch, _trace_key(ch));)
267 T(("wgetch returning ERR"));
272 * Simulate ICRNL mode
274 if ((ch == '\r') && SP->_nl)
277 /* Strip 8th-bit if so desired. We do this only for characters that
278 * are in the range 128-255, to provide compatibility with terminals
279 * that display only 7-bit characters. Note that 'ch' may be a
280 * function key at this point, so we mustn't strip _those_.
282 if ((ch < KEY_MIN) && (ch & 0x80))
286 if (SP->_echo && ch < KEY_MIN && !(win->_flags & _ISPAD))
287 wechochar(win, (chtype)ch);
289 T(("wgetch returning : %#x = %s", ch, _trace_key(ch)));
299 ** Get an input character, but take care of keypad sequences, returning
300 ** an appropriate code when one matches the input. After each character
301 ** is received, set an alarm call based on ESCDELAY. If no more of the
302 ** sequence is received by the time the alarm goes off, pass through
303 ** the sequence gotten so far.
305 ** This function must be called when there is no cooked keys in queue.
306 ** (that is head==-1 || peek==head)
311 kgetch(WINDOW *win GCC_UNUSED)
315 int timeleft = ESCDELAY;
317 TR(TRACE_IEVENT, ("kgetch(%p) called", win));
323 if (!raw_key_in_fifo())
325 if(fifo_push() == ERR)
327 peek = head; /* the keys stay uninterpreted */
335 /* assume the key is the last in fifo */
336 t_dec(); /* remove the key */
340 TR(TRACE_IEVENT, ("ch: %s", _trace_key((unsigned char)ch)));
341 while ((ptr != NULL) && (ptr->ch != (unsigned char)ch))
345 {TR(TRACE_IEVENT, ("ptr is null"));}
347 TR(TRACE_IEVENT, ("ptr=%p, ch=%d, value=%d",
348 ptr, ptr->ch, ptr->value));
354 if (ptr->value != 0) { /* sequence terminated */
355 TR(TRACE_IEVENT, ("end of sequence"));
365 if (!raw_key_in_fifo())
367 TR(TRACE_IEVENT, ("waiting for rest of sequence"));
368 if (!_nc_timed_wait(3, timeleft, &timeleft)) {
369 TR(TRACE_IEVENT, ("ran out of time"));