xref: /netbsd-src/lib/libcurses/getch.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: getch.c,v 1.34 2001/11/01 16:06:59 tron Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)getch.c	8.2 (Berkeley) 5/4/94";
40 #else
41 __RCSID("$NetBSD: getch.c,v 1.34 2001/11/01 16:06:59 tron Exp $");
42 #endif
43 #endif					/* not lint */
44 
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include "curses.h"
50 #include "curses_private.h"
51 
52 /* defined in setterm.c */
53 extern struct tinfo *_cursesi_genbuf;
54 
55 #define DEFAULT_DELAY 2			/* default delay for timeout() */
56 
57 /*
58  * Keyboard input handler.  Do this by snarfing
59  * all the info we can out of the termcap entry for TERM and putting it
60  * into a set of keymaps.  A keymap is an array the size of all the possible
61  * single characters we can get, the contents of the array is a structure
62  * that contains the type of entry this character is (i.e. part/end of a
63  * multi-char sequence or a plain char) and either a pointer which will point
64  * to another keymap (in the case of a multi-char sequence) OR the data value
65  * that this key should return.
66  *
67  */
68 
69 /* private data structures for holding the key definitions */
70 typedef struct keymap keymap_t;
71 typedef struct key_entry key_entry_t;
72 
73 struct key_entry {
74 	short   type;		/* type of key this is */
75 	union {
76 		keymap_t *next;	/* next keymap is key is multi-key sequence */
77 		wchar_t   symbol;	/* key symbol if key is a leaf entry */
78 	} value;
79 };
80 /* Types of key structures we can have */
81 #define KEYMAP_MULTI  1		/* part of a multi char sequence */
82 #define KEYMAP_LEAF   2		/* key has a symbol associated with it, either
83 				 * it is the end of a multi-char sequence or a
84 				 * single char key that generates a symbol */
85 
86 /* allocate this many key_entry structs at once to speed start up must
87  * be a power of 2.
88  */
89 #define KEYMAP_ALLOC_CHUNK 4
90 
91 /* The max number of different chars we can receive */
92 #define MAX_CHAR 256
93 
94 struct keymap {
95 	int	count;		/* count of number of key structs allocated */
96 	short	mapping[MAX_CHAR]; /* mapping of key to allocated structs */
97 	key_entry_t **key;	/* dynamic array of keys */
98 };
99 
100 
101 /* Key buffer */
102 #define INBUF_SZ 16		/* size of key buffer - must be larger than
103 				 * longest multi-key sequence */
104 static wchar_t  inbuf[INBUF_SZ];
105 static int     start, end, working; /* pointers for manipulating inbuf data */
106 
107 #define INC_POINTER(ptr)  do {	\
108 	(ptr)++;		\
109 	ptr %= INBUF_SZ;	\
110 } while(/*CONSTCOND*/0)
111 
112 static short	state;		/* state of the inkey function */
113 
114 #define INKEY_NORM	 0	/* no key backlog to process */
115 #define INKEY_ASSEMBLING 1	/* assembling a multi-key sequence */
116 #define INKEY_BACKOUT	 2	/* recovering from an unrecognised key */
117 #define INKEY_TIMEOUT	 3	/* multi-key sequence timeout */
118 
119 /* The termcap data we are interested in and the symbols they map to */
120 struct tcdata {
121 	const char	*name;	/* name of termcap entry */
122 	wchar_t	symbol;		/* the symbol associated with it */
123 };
124 
125 static const struct tcdata tc[] = {
126 	{"!1", KEY_SSAVE},
127 	{"!2", KEY_SSUSPEND},
128 	{"!3", KEY_SUNDO},
129 	{"#1", KEY_SHELP},
130 	{"#2", KEY_SHOME},
131 	{"#3", KEY_SIC},
132 	{"#4", KEY_SLEFT},
133 	{"%0", KEY_REDO},
134 	{"%1", KEY_HELP},
135 	{"%2", KEY_MARK},
136 	{"%3", KEY_MESSAGE},
137 	{"%4", KEY_MOVE},
138 	{"%5", KEY_NEXT},
139 	{"%6", KEY_OPEN},
140 	{"%7", KEY_OPTIONS},
141 	{"%8", KEY_PREVIOUS},
142 	{"%9", KEY_PRINT},
143 	{"%a", KEY_SMESSAGE},
144 	{"%b", KEY_SMOVE},
145 	{"%c", KEY_SNEXT},
146 	{"%d", KEY_SOPTIONS},
147 	{"%e", KEY_SPREVIOUS},
148 	{"%f", KEY_SPRINT},
149 	{"%g", KEY_SREDO},
150 	{"%h", KEY_SREPLACE},
151 	{"%i", KEY_SRIGHT},
152 	{"%j", KEY_SRSUME},
153 	{"&0", KEY_SCANCEL},
154 	{"&1", KEY_REFERENCE},
155 	{"&2", KEY_REFRESH},
156 	{"&3", KEY_REPLACE},
157 	{"&4", KEY_RESTART},
158 	{"&5", KEY_RESUME},
159 	{"&6", KEY_SAVE},
160 	{"&7", KEY_SUSPEND},
161 	{"&8", KEY_UNDO},
162 	{"&9", KEY_SBEG},
163 	{"*0", KEY_SFIND},
164 	{"*1", KEY_SCOMMAND},
165 	{"*2", KEY_SCOPY},
166 	{"*3", KEY_SCREATE},
167 	{"*4", KEY_SDC},
168 	{"*5", KEY_SDL},
169 	{"*6", KEY_SELECT},
170 	{"*7", KEY_SEND},
171 	{"*8", KEY_SEOL},
172 	{"*9", KEY_SEXIT},
173 	{"@0", KEY_FIND},
174 	{"@1", KEY_BEG},
175 	{"@2", KEY_CANCEL},
176 	{"@3", KEY_CLOSE},
177 	{"@4", KEY_COMMAND},
178 	{"@5", KEY_COPY},
179 	{"@6", KEY_CREATE},
180 	{"@7", KEY_END},
181 	{"@8", KEY_ENTER},
182 	{"@9", KEY_EXIT},
183 	{"F1", KEY_F(11)},
184 	{"F2", KEY_F(12)},
185 	{"F3", KEY_F(13)},
186 	{"F4", KEY_F(14)},
187 	{"F5", KEY_F(15)},
188 	{"F6", KEY_F(16)},
189 	{"F7", KEY_F(17)},
190 	{"F8", KEY_F(18)},
191 	{"F9", KEY_F(19)},
192 	{"FA", KEY_F(20)},
193 	{"FB", KEY_F(21)},
194 	{"FC", KEY_F(22)},
195 	{"FD", KEY_F(23)},
196 	{"FE", KEY_F(24)},
197 	{"FF", KEY_F(25)},
198 	{"FG", KEY_F(26)},
199 	{"FH", KEY_F(27)},
200 	{"FI", KEY_F(28)},
201 	{"FJ", KEY_F(29)},
202 	{"FK", KEY_F(30)},
203 	{"FL", KEY_F(31)},
204 	{"FM", KEY_F(32)},
205 	{"FN", KEY_F(33)},
206 	{"FO", KEY_F(34)},
207 	{"FP", KEY_F(35)},
208 	{"FQ", KEY_F(36)},
209 	{"FR", KEY_F(37)},
210 	{"FS", KEY_F(38)},
211 	{"FT", KEY_F(39)},
212 	{"FU", KEY_F(40)},
213 	{"FV", KEY_F(41)},
214 	{"FW", KEY_F(42)},
215 	{"FX", KEY_F(43)},
216 	{"FY", KEY_F(44)},
217 	{"FZ", KEY_F(45)},
218 	{"Fa", KEY_F(46)},
219 	{"Fb", KEY_F(47)},
220 	{"Fc", KEY_F(48)},
221 	{"Fd", KEY_F(49)},
222 	{"Fe", KEY_F(50)},
223 	{"Ff", KEY_F(51)},
224 	{"Fg", KEY_F(52)},
225 	{"Fh", KEY_F(53)},
226 	{"Fi", KEY_F(54)},
227 	{"Fj", KEY_F(55)},
228 	{"Fk", KEY_F(56)},
229 	{"Fl", KEY_F(57)},
230 	{"Fm", KEY_F(58)},
231 	{"Fn", KEY_F(59)},
232 	{"Fo", KEY_F(60)},
233 	{"Fp", KEY_F(61)},
234 	{"Fq", KEY_F(62)},
235 	{"Fr", KEY_F(63)},
236 	{"K1", KEY_A1},
237 	{"K2", KEY_B2},
238 	{"K3", KEY_A3},
239 	{"K4", KEY_C1},
240 	{"K5", KEY_C3},
241 	{"Km", KEY_MOUSE},
242 	{"k0", KEY_F0},
243 	{"k1", KEY_F(1)},
244 	{"k2", KEY_F(2)},
245 	{"k3", KEY_F(3)},
246 	{"k4", KEY_F(4)},
247 	{"k5", KEY_F(5)},
248 	{"k6", KEY_F(6)},
249 	{"k7", KEY_F(7)},
250 	{"k8", KEY_F(8)},
251 	{"k9", KEY_F(9)},
252 	{"k;", KEY_F(10)},
253 	{"kA", KEY_IL},
254 	{"ka", KEY_CATAB},
255 	{"kB", KEY_BTAB},
256 	{"kb", KEY_BACKSPACE},
257 	{"kC", KEY_CLEAR},
258 	{"kD", KEY_DC},
259 	{"kd", KEY_DOWN},
260 	{"kE", KEY_EOL},
261 	{"kF", KEY_SF},
262 	{"kH", KEY_LL},
263 	{"kh", KEY_HOME},
264 	{"kI", KEY_IC},
265 	{"kL", KEY_DL},
266 	{"kl", KEY_LEFT},
267 	{"kM", KEY_EIC},
268 	{"kN", KEY_NPAGE},
269 	{"kP", KEY_PPAGE},
270 	{"kR", KEY_SR},
271 	{"kr", KEY_RIGHT},
272 	{"kS", KEY_EOS},
273 	{"kT", KEY_STAB},
274 	{"kt", KEY_CTAB},
275 	{"ku", KEY_UP}
276 };
277 /* Number of TC entries .... */
278 static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
279 
280 /* The root keymap */
281 
282 static keymap_t *base_keymap;
283 
284 /* prototypes for private functions */
285 static key_entry_t *add_new_key(keymap_t *current, char chr, int key_type,
286 				int symbol);
287 static keymap_t		*new_keymap(void);	/* create a new keymap */
288 static key_entry_t	*new_key(void);		/* create a new key entry */
289 static wchar_t		inkey(int to, int delay);
290 
291 /*
292  * Add a new key entry to the keymap pointed to by current.  Entry
293  * contains the character to add to the keymap, type is the type of
294  * entry to add (either multikey or leaf) and symbol is the symbolic
295  * value for a leaf type entry.  The function returns a pointer to the
296  * new keymap entry.
297  */
298 static key_entry_t *
299 add_new_key(keymap_t *current, char chr, int key_type, int symbol)
300 {
301 	key_entry_t *the_key;
302         int i;
303 
304 #ifdef DEBUG
305 	__CTRACE("Adding character %s of type %d, symbol 0x%x\n", unctrl(chr),
306 		 key_type, symbol);
307 #endif
308 	if (current->mapping[(unsigned char) chr] < 0) {
309 		  /* first time for this char */
310 		current->mapping[(unsigned char) chr] = current->count;	/* map new entry */
311 		  /* make sure we have room in the key array first */
312 		if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
313 		{
314 			if ((current->key =
315 			     realloc(current->key,
316 				     (current->count) * sizeof(key_entry_t *)
317 				     + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
318 				fprintf(stderr,
319 					"Could not malloc for key entry\n");
320 				exit(1);
321 			}
322 
323 			the_key = new_key();
324                         for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
325                                 current->key[current->count + i]
326 					= &the_key[i];
327                         }
328                 }
329 
330                   /* point at the current key array element to use */
331                 the_key = current->key[current->count];
332 
333 		the_key->type = key_type;
334 
335 		switch (key_type) {
336 		  case KEYMAP_MULTI:
337 			    /* need for next key */
338 #ifdef DEBUG
339 			  __CTRACE("Creating new keymap\n");
340 #endif
341 			  the_key->value.next = new_keymap();
342 			  break;
343 
344 		  case KEYMAP_LEAF:
345 				/* the associated symbol for the key */
346 #ifdef DEBUG
347 			  __CTRACE("Adding leaf key\n");
348 #endif
349 			  the_key->value.symbol = symbol;
350 			  break;
351 
352 		  default:
353 			  fprintf(stderr, "add_new_key: bad type passed\n");
354 			  exit(1);
355 		}
356 
357 		current->count++;
358 	} else {
359 		  /* the key is already known - just return the address. */
360 #ifdef DEBUG
361 		__CTRACE("Keymap already known\n");
362 #endif
363 		the_key = current->key[current->mapping[(unsigned char) chr]];
364 	}
365 
366         return the_key;
367 }
368 
369 /*
370  * Init_getch - initialise all the pointers & structures needed to make
371  * getch work in keypad mode.
372  *
373  */
374 void
375 __init_getch(void)
376 {
377 	char entry[1024], *p;
378 	int     i, j, length, key_ent;
379 	size_t limit;
380 	key_entry_t *tmp_key;
381 	keymap_t *current;
382 	char *cp;
383 #ifdef DEBUG
384 	int k;
385 #endif
386 
387 	/* init the inkey state variable */
388 	state = INKEY_NORM;
389 
390 	/* init the base keymap */
391 	base_keymap = new_keymap();
392 
393 	/* key input buffer pointers */
394 	start = end = working = 0;
395 
396 	/* now do the termcap snarfing ... */
397 	for (i = 0; i < num_tcs; i++) {
398 		p = entry;
399 		limit = 1023;
400 		cp = t_getstr(_cursesi_genbuf, tc[i].name, &p, &limit);
401 		if (cp != NULL) {
402 			current = base_keymap;	/* always start with
403 						 * base keymap. */
404 			length = (int) strlen(entry);
405 #ifdef DEBUG
406 			__CTRACE("Processing termcap entry %s, sequence ",
407 				 tc[i].name);
408 			for (k = 0; k <= length -1; k++)
409 				__CTRACE("%s", unctrl(entry[k]));
410 			__CTRACE("\n");
411 #endif
412 			for (j = 0; j < length - 1; j++) {
413 				  /* add the entry to the struct */
414 				tmp_key = add_new_key(current,
415 						      entry[j],
416 						      KEYMAP_MULTI, 0);
417 
418 				  /* index into the key array - it's
419 				     clearer if we stash this */
420 				key_ent = current->mapping[
421 					(unsigned char) entry[j]];
422 
423 				current->key[key_ent] = tmp_key;
424 
425 				  /* next key uses this map... */
426 				current = current->key[key_ent]->value.next;
427 			}
428 
429 				/* this is the last key in the sequence (it
430 				 * may have been the only one but that does
431 				 * not matter) this means it is a leaf key and
432 				 * should have a symbol associated with it.
433 				 */
434 			tmp_key = add_new_key(current,
435 					      entry[length - 1],
436 					      KEYMAP_LEAF,
437 					      tc[i].symbol);
438 			current->key[
439 				current->mapping[(int)entry[length - 1]]] =
440 			tmp_key;
441 		}
442 	}
443 }
444 
445 
446 /*
447  * new_keymap - allocates & initialises a new keymap structure.  This
448  * function returns a pointer to the new keymap.
449  *
450  */
451 static keymap_t *
452 new_keymap(void)
453 {
454 	int     i;
455 	keymap_t *new_map;
456 
457 	if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
458 		perror("Inkey: Cannot allocate new keymap");
459 		exit(2);
460 	}
461 
462 	/* Initialise the new map */
463 	new_map->count = 0;
464 	for (i = 0; i < MAX_CHAR; i++) {
465 		new_map->mapping[i] = -1;	/* no mapping for char */
466 	}
467 
468 	/* key array will be allocated when first key is added */
469 	new_map->key = NULL;
470 
471 	return new_map;
472 }
473 
474 /*
475  * new_key - allocates & initialises a new key entry.  This function returns
476  * a pointer to the newly allocated key entry.
477  *
478  */
479 static key_entry_t *
480 new_key(void)
481 {
482 	key_entry_t *new_one;
483 	int i;
484 
485 	if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
486 	    == NULL) {
487 		perror("inkey: Cannot allocate new key entry chunk");
488 		exit(2);
489 	}
490 
491 	for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
492 		new_one[i].type = 0;
493 		new_one[i].value.next = NULL;
494 	}
495 
496 	return new_one;
497 }
498 
499 /*
500  * inkey - do the work to process keyboard input, check for multi-key
501  * sequences and return the appropriate symbol if we get a match.
502  *
503  */
504 
505 wchar_t
506 inkey(int to, int delay)
507 {
508 	wchar_t		 k;
509 	int              c;
510 	keymap_t	*current = base_keymap;
511 
512 	k = 0;		/* XXX gcc -Wuninitialized */
513 
514 	for (;;) {		/* loop until we get a complete key sequence */
515 reread:
516 		if (state == INKEY_NORM) {
517 			if (delay && __timeout(delay) == ERR)
518 				return ERR;
519 			if ((c = getchar()) == EOF) {
520 				clearerr(stdin);
521 				return ERR;
522 			}
523 
524 			if (delay && (__notimeout() == ERR))
525 				return ERR;
526 
527 			k = (wchar_t) c;
528 #ifdef DEBUG
529 			__CTRACE("inkey (state normal) got '%s'\n", unctrl(k));
530 #endif
531 
532 			working = start;
533 			inbuf[working] = k;
534 			INC_POINTER(working);
535 			end = working;
536 			state = INKEY_ASSEMBLING;	/* go to the assembling
537 							 * state now */
538 		} else if (state == INKEY_BACKOUT) {
539 			k = inbuf[working];
540 			INC_POINTER(working);
541 			if (working == end) {	/* see if we have run
542 						 * out of keys in the
543 						 * backlog */
544 
545 				/* if we have then switch to
546 				   assembling */
547 				state = INKEY_ASSEMBLING;
548 			}
549 		} else if (state == INKEY_ASSEMBLING) {
550 			/* assembling a key sequence */
551 			if (delay) {
552 				if (__timeout(to ? DEFAULT_DELAY : delay) == ERR)
553 						return ERR;
554 			} else {
555 				if (to && (__timeout(DEFAULT_DELAY) == ERR))
556 					return ERR;
557 			}
558 
559 			c = getchar();
560 			if (ferror(stdin)) {
561 				clearerr(stdin);
562 				return ERR;
563 			}
564 
565 			if ((to || delay) && (__notimeout() == ERR))
566 					return ERR;
567 
568 			k = (wchar_t) c;
569 #ifdef DEBUG
570 			__CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
571 #endif
572 			if (feof(stdin)) {	/* inter-char timeout,
573 						 * start backing out */
574 				clearerr(stdin);
575 				if (start == end)
576 					/* no chars in the buffer, restart */
577 					goto reread;
578 
579 				k = inbuf[start];
580 				state = INKEY_TIMEOUT;
581 			} else {
582 				inbuf[working] = k;
583 				INC_POINTER(working);
584 				end = working;
585 			}
586 		} else {
587 			fprintf(stderr, "Inkey state screwed - exiting!!!");
588 			exit(2);
589 		}
590 
591 		/* Check key has no special meaning and we have not timed out */
592 		if ((state == INKEY_TIMEOUT) || (current->mapping[k] < 0)) {
593 			/* return the first key we know about */
594 			k = inbuf[start];
595 
596 			INC_POINTER(start);
597 			working = start;
598 
599 			if (start == end) {	/* only one char processed */
600 				state = INKEY_NORM;
601 			} else {/* otherwise we must have more than one char
602 				 * to backout */
603 				state = INKEY_BACKOUT;
604 			}
605 			return k;
606 		} else {	/* must be part of a multikey sequence */
607 			/* check for completed key sequence */
608 			if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
609 				start = working;	/* eat the key sequence
610 							 * in inbuf */
611 
612 				/* check if inbuf empty now */
613 				if (start == end) {
614 					/* if it is go back to normal */
615 					state = INKEY_NORM;
616 				} else {
617 					/* otherwise go to backout state */
618 					state = INKEY_BACKOUT;
619 				}
620 
621 				/* return the symbol */
622 				return current->key[current->mapping[k]]->value.symbol;
623 
624 			} else {
625 				/*
626 				 * Step on to next part of the multi-key
627 				 * sequence.
628 				 */
629 				current = current->key[current->mapping[k]]->value.next;
630 			}
631 		}
632 	}
633 }
634 
635 #ifndef _CURSES_USE_MACROS
636 /*
637  * getch --
638  *	Read in a character from stdscr.
639  */
640 int
641 getch(void)
642 {
643 	return wgetch(stdscr);
644 }
645 
646 /*
647  * mvgetch --
648  *      Read in a character from stdscr at the given location.
649  */
650 int
651 mvgetch(int y, int x)
652 {
653 	return mvwgetch(stdscr, y, x);
654 }
655 
656 /*
657  * mvwgetch --
658  *      Read in a character from stdscr at the given location in the
659  *      given window.
660  */
661 int
662 mvwgetch(WINDOW *win, int y, int x)
663 {
664 	if (wmove(win, y, x) == ERR)
665 		return ERR;
666 
667 	return wgetch(win);
668 }
669 
670 #endif
671 
672 /*
673  * wgetch --
674  *	Read in a character from the window.
675  */
676 int
677 wgetch(WINDOW *win)
678 {
679 	int inp, weset;
680 	int c;
681 
682 	if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
683 	    && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
684 	    && __echoit)
685 		return (ERR);
686 
687 	if (is_wintouched(win))
688 		wrefresh(win);
689 #ifdef DEBUG
690 	__CTRACE("wgetch: __echoit = %d, __rawmode = %d, flags = %0.2o\n",
691 	    __echoit, __rawmode, win->flags);
692 #endif
693 	if (__echoit && !__rawmode) {
694 		cbreak();
695 		weset = 1;
696 	} else
697 		weset = 0;
698 
699 	__save_termios();
700 
701 	if (win->flags & __KEYPAD) {
702 		switch (win->delay)
703 		{
704 		case -1:
705 			inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
706 			break;
707 		case 0:
708 			if (__nodelay() == ERR) {
709 				__restore_termios();
710 				return ERR;
711 			}
712 			inp = inkey(0, 0);
713 			break;
714 		default:
715 			inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
716 			break;
717 		}
718 	} else {
719 		switch (win->delay)
720 		{
721 		case -1:
722 			break;
723 		case 0:
724 			if (__nodelay() == ERR) {
725 				__restore_termios();
726 				return ERR;
727 			}
728 			break;
729 		default:
730 			if (__timeout(win->delay) == ERR) {
731 				__restore_termios();
732 				return ERR;
733 			}
734 			break;
735 		}
736 
737 		c = getchar();
738 		if (feof(stdin)) {
739 			clearerr(stdin);
740 			__restore_termios();
741 			return ERR;	/* we have timed out */
742 		}
743 
744 		if (ferror(stdin)) {
745 			clearerr(stdin);
746 			inp = ERR;
747 		} else {
748 			inp = c;
749 		}
750 	}
751 #ifdef DEBUG
752 	if (inp > 255)
753 		  /* we have a key symbol - treat it differently */
754 		  /* XXXX perhaps __unctrl should be expanded to include
755 		   * XXXX the keysyms in the table....
756 		   */
757 		__CTRACE("wgetch assembled keysym 0x%x\n", inp);
758 	else
759 		__CTRACE("wgetch got '%s'\n", unctrl(inp));
760 #endif
761 	if (win->delay > -1) {
762 		if (__delay() == ERR) {
763 			__restore_termios();
764 			return ERR;
765 		}
766 	}
767 
768 	__restore_termios();
769 
770 	if (__echoit)
771 		waddch(win, (chtype) inp);
772 
773 	if (weset)
774 		nocbreak();
775 
776 	return ((inp < 0) || (inp == ERR) ? ERR : inp);
777 }
778 
779 /*
780  * ungetch --
781  *     Put the character back into the input queue.
782  */
783 int
784 ungetch(int c)
785 {
786 	return ((ungetc(c, stdin) == EOF) ? ERR : OK);
787 }
788