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