getch.c revision 1.23 1 1.23 thorpej /* $NetBSD: getch.c,v 1.23 2000/04/22 21:14:19 thorpej 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.8 mikel #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 mikel #if 0
39 1.7 cgd static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
40 1.8 mikel #else
41 1.23 thorpej __RCSID("$NetBSD: getch.c,v 1.23 2000/04/22 21:14:19 thorpej Exp $");
42 1.8 mikel #endif
43 1.10 mrg #endif /* not lint */
44 1.1 cgd
45 1.10 mrg #include <string.h>
46 1.10 mrg #include <stdlib.h>
47 1.10 mrg #include <unistd.h>
48 1.10 mrg #include <stdio.h>
49 1.7 cgd #include "curses.h"
50 1.16 blymn #include "curses_private.h"
51 1.1 cgd
52 1.10 mrg #define DEFAULT_DELAY 2 /* default delay for timeout() */
53 1.10 mrg
54 1.10 mrg /*
55 1.10 mrg * Keyboard input handler. Do this by snarfing
56 1.10 mrg * all the info we can out of the termcap entry for TERM and putting it
57 1.10 mrg * into a set of keymaps. A keymap is an array the size of all the possible
58 1.10 mrg * single characters we can get, the contents of the array is a structure
59 1.10 mrg * that contains the type of entry this character is (i.e. part/end of a
60 1.10 mrg * multi-char sequence or a plain char) and either a pointer which will point
61 1.10 mrg * to another keymap (in the case of a multi-char sequence) OR the data value
62 1.10 mrg * that this key should return.
63 1.10 mrg *
64 1.10 mrg */
65 1.10 mrg
66 1.10 mrg /* private data structures for holding the key definitions */
67 1.10 mrg typedef struct keymap keymap_t;
68 1.10 mrg typedef struct key_entry key_entry_t;
69 1.10 mrg
70 1.10 mrg struct key_entry {
71 1.10 mrg short type; /* type of key this is */
72 1.10 mrg union {
73 1.10 mrg keymap_t *next; /* next keymap is key is multi-key sequence */
74 1.16 blymn wchar_t symbol; /* key symbol if key is a leaf entry */
75 1.12 pk } value;
76 1.10 mrg };
77 1.10 mrg /* Types of key structures we can have */
78 1.10 mrg #define KEYMAP_MULTI 1 /* part of a multi char sequence */
79 1.10 mrg #define KEYMAP_LEAF 2 /* key has a symbol associated with it, either
80 1.10 mrg * it is the end of a multi-char sequence or a
81 1.10 mrg * single char key that generates a symbol */
82 1.10 mrg
83 1.20 blymn /* allocate this many key_entry structs at once to speed start up must
84 1.20 blymn * be a power of 2.
85 1.20 blymn */
86 1.20 blymn #define KEYMAP_ALLOC_CHUNK 4
87 1.20 blymn
88 1.10 mrg /* The max number of different chars we can receive */
89 1.10 mrg #define MAX_CHAR 256
90 1.10 mrg
91 1.10 mrg struct keymap {
92 1.12 pk int count; /* count of number of key structs allocated */
93 1.12 pk short mapping[MAX_CHAR]; /* mapping of key to allocated structs */
94 1.20 blymn key_entry_t **key; /* dynamic array of keys */
95 1.20 blymn };
96 1.10 mrg
97 1.10 mrg
98 1.10 mrg /* Key buffer */
99 1.10 mrg #define INBUF_SZ 16 /* size of key buffer - must be larger than
100 1.10 mrg * longest multi-key sequence */
101 1.16 blymn static wchar_t inbuf[INBUF_SZ];
102 1.13 simonb static int start, end, working; /* pointers for manipulating inbuf data */
103 1.10 mrg
104 1.12 pk #define INC_POINTER(ptr) do { \
105 1.12 pk (ptr)++; \
106 1.12 pk ptr %= INBUF_SZ; \
107 1.10 mrg } while(/*CONSTCOND*/0)
108 1.10 mrg
109 1.13 simonb static short state; /* state of the inkey function */
110 1.10 mrg
111 1.12 pk #define INKEY_NORM 0 /* no key backlog to process */
112 1.10 mrg #define INKEY_ASSEMBLING 1 /* assembling a multi-key sequence */
113 1.12 pk #define INKEY_BACKOUT 2 /* recovering from an unrecognised key */
114 1.12 pk #define INKEY_TIMEOUT 3 /* multi-key sequence timeout */
115 1.10 mrg
116 1.10 mrg /* The termcap data we are interested in and the symbols they map to */
117 1.10 mrg struct tcdata {
118 1.20 blymn const char *name; /* name of termcap entry */
119 1.16 blymn wchar_t symbol; /* the symbol associated with it */
120 1.10 mrg };
121 1.10 mrg
122 1.13 simonb static const struct tcdata tc[] = {
123 1.10 mrg {"K1", KEY_A1},
124 1.10 mrg {"K2", KEY_B2},
125 1.10 mrg {"K3", KEY_A3},
126 1.10 mrg {"K4", KEY_C1},
127 1.10 mrg {"K5", KEY_C3},
128 1.10 mrg {"k0", KEY_F0},
129 1.10 mrg {"k1", KEY_F(1)},
130 1.10 mrg {"k2", KEY_F(2)},
131 1.10 mrg {"k3", KEY_F(3)},
132 1.10 mrg {"k4", KEY_F(4)},
133 1.10 mrg {"k5", KEY_F(5)},
134 1.10 mrg {"k6", KEY_F(6)},
135 1.10 mrg {"k7", KEY_F(7)},
136 1.10 mrg {"k8", KEY_F(8)},
137 1.10 mrg {"k9", KEY_F(9)},
138 1.10 mrg {"kA", KEY_IL},
139 1.10 mrg {"ka", KEY_CATAB},
140 1.10 mrg {"kb", KEY_BACKSPACE},
141 1.10 mrg {"kC", KEY_CLEAR},
142 1.10 mrg {"kD", KEY_DC},
143 1.10 mrg {"kd", KEY_DOWN},
144 1.10 mrg {"kE", KEY_EOL},
145 1.10 mrg {"kF", KEY_SF},
146 1.10 mrg {"kH", KEY_LL},
147 1.10 mrg {"kh", KEY_HOME},
148 1.10 mrg {"kI", KEY_IC},
149 1.10 mrg {"kL", KEY_DL},
150 1.10 mrg {"kl", KEY_LEFT},
151 1.10 mrg {"kN", KEY_NPAGE},
152 1.10 mrg {"kP", KEY_PPAGE},
153 1.10 mrg {"kR", KEY_SR},
154 1.10 mrg {"kr", KEY_RIGHT},
155 1.10 mrg {"kS", KEY_EOS},
156 1.10 mrg {"kT", KEY_STAB},
157 1.10 mrg {"kt", KEY_CTAB},
158 1.10 mrg {"ku", KEY_UP}
159 1.10 mrg };
160 1.10 mrg /* Number of TC entries .... */
161 1.13 simonb static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
162 1.10 mrg
163 1.10 mrg /* The root keymap */
164 1.10 mrg
165 1.13 simonb static keymap_t *base_keymap;
166 1.10 mrg
167 1.10 mrg /* prototypes for private functions */
168 1.20 blymn static key_entry_t *add_new_key(keymap_t *current, char chr, int key_type,
169 1.20 blymn int symbol);
170 1.13 simonb static keymap_t *new_keymap(void); /* create a new keymap */
171 1.13 simonb static key_entry_t *new_key(void); /* create a new key entry */
172 1.20 blymn static wchar_t inkey(int to, int delay);
173 1.20 blymn
174 1.20 blymn /*
175 1.20 blymn * Add a new key entry to the keymap pointed to by current. Entry
176 1.20 blymn * contains the character to add to the keymap, type is the type of
177 1.20 blymn * entry to add (either multikey or leaf) and symbol is the symbolic
178 1.20 blymn * value for a leaf type entry. The function returns a pointer to the
179 1.20 blymn * new keymap entry.
180 1.20 blymn */
181 1.20 blymn static key_entry_t *
182 1.20 blymn add_new_key(keymap_t *current, char chr, int key_type, int symbol)
183 1.20 blymn {
184 1.20 blymn key_entry_t *the_key;
185 1.20 blymn int i;
186 1.20 blymn
187 1.20 blymn #ifdef DEBUG
188 1.20 blymn __CTRACE("Adding character %s of type %d, symbol 0x%x\n", unctrl(chr),
189 1.20 blymn key_type, symbol);
190 1.20 blymn #endif
191 1.20 blymn if (current->mapping[(unsigned) chr] < 0) {
192 1.20 blymn /* first time for this char */
193 1.20 blymn current->mapping[(unsigned) chr] = current->count; /* map new entry */
194 1.20 blymn /* make sure we have room in the key array first */
195 1.20 blymn if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
196 1.20 blymn {
197 1.20 blymn if ((current->key =
198 1.20 blymn realloc(current->key,
199 1.20 blymn (current->count) * sizeof(key_entry_t *)
200 1.20 blymn + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
201 1.20 blymn fprintf(stderr,
202 1.20 blymn "Could not malloc for key entry\n");
203 1.20 blymn exit(1);
204 1.20 blymn }
205 1.20 blymn
206 1.20 blymn the_key = new_key();
207 1.20 blymn for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
208 1.20 blymn current->key[current->count + i]
209 1.20 blymn = &the_key[i];
210 1.20 blymn }
211 1.20 blymn }
212 1.20 blymn
213 1.20 blymn /* point at the current key array element to use */
214 1.20 blymn the_key = current->key[current->count];
215 1.20 blymn
216 1.20 blymn the_key->type = key_type;
217 1.20 blymn
218 1.20 blymn switch (key_type) {
219 1.20 blymn case KEYMAP_MULTI:
220 1.20 blymn /* need for next key */
221 1.20 blymn #ifdef DEBUG
222 1.20 blymn __CTRACE("Creating new keymap\n");
223 1.20 blymn #endif
224 1.20 blymn the_key->value.next = new_keymap();
225 1.20 blymn break;
226 1.20 blymn
227 1.20 blymn case KEYMAP_LEAF:
228 1.20 blymn /* the associated symbol for the key */
229 1.20 blymn #ifdef DEBUG
230 1.20 blymn __CTRACE("Adding leaf key\n");
231 1.20 blymn #endif
232 1.20 blymn the_key->value.symbol = symbol;
233 1.20 blymn break;
234 1.20 blymn
235 1.20 blymn default:
236 1.20 blymn fprintf(stderr, "add_new_key: bad type passed\n");
237 1.20 blymn exit(1);
238 1.20 blymn }
239 1.20 blymn
240 1.20 blymn current->count++;
241 1.20 blymn } else {
242 1.20 blymn /* the key is already known - just return the address. */
243 1.20 blymn #ifdef DEBUG
244 1.20 blymn __CTRACE("Keymap already known\n");
245 1.20 blymn #endif
246 1.20 blymn the_key = current->key[current->mapping[(unsigned) chr]];
247 1.20 blymn }
248 1.20 blymn
249 1.20 blymn return the_key;
250 1.20 blymn }
251 1.10 mrg
252 1.10 mrg /*
253 1.10 mrg * Init_getch - initialise all the pointers & structures needed to make
254 1.10 mrg * getch work in keypad mode.
255 1.10 mrg *
256 1.10 mrg */
257 1.10 mrg void
258 1.18 blymn __init_getch(char *sp)
259 1.10 mrg {
260 1.20 blymn static struct tinfo *termcap;
261 1.12 pk char entry[1024], termname[1024], *p;
262 1.20 blymn int i, j, length, key_ent;
263 1.20 blymn size_t limit;
264 1.20 blymn key_entry_t *tmp_key;
265 1.10 mrg keymap_t *current;
266 1.20 blymn #ifdef DEBUG
267 1.20 blymn int k;
268 1.20 blymn #endif
269 1.10 mrg
270 1.10 mrg /* init the inkey state variable */
271 1.10 mrg state = INKEY_NORM;
272 1.10 mrg
273 1.10 mrg /* init the base keymap */
274 1.10 mrg base_keymap = new_keymap();
275 1.10 mrg
276 1.10 mrg /* key input buffer pointers */
277 1.10 mrg start = end = working = 0;
278 1.10 mrg
279 1.10 mrg /* now do the termcap snarfing ... */
280 1.16 blymn (void) strncpy(termname, sp, (size_t) 1022);
281 1.10 mrg termname[1023] = 0;
282 1.10 mrg
283 1.20 blymn if (t_getent(&termcap, termname) > 0) {
284 1.20 blymn for (i = 0; i < num_tcs; i++) {
285 1.20 blymn p = entry;
286 1.20 blymn limit = 1023;
287 1.20 blymn if (t_getstr(termcap, tc[i].name, &p, &limit) != NULL) {
288 1.20 blymn current = base_keymap; /* always start with
289 1.20 blymn * base keymap. */
290 1.20 blymn length = (int) strlen(entry);
291 1.20 blymn #ifdef DEBUG
292 1.20 blymn __CTRACE("Processing termcap entry %s, sequence ",
293 1.20 blymn tc[i].name);
294 1.20 blymn for (k = 0; k <= length -1; k++)
295 1.20 blymn __CTRACE("%s", unctrl(entry[k]));
296 1.20 blymn __CTRACE("\n");
297 1.20 blymn #endif
298 1.20 blymn for (j = 0; j < length - 1; j++) {
299 1.20 blymn /* add the entry to the struct */
300 1.20 blymn tmp_key = add_new_key(current,
301 1.20 blymn entry[j],
302 1.20 blymn KEYMAP_MULTI, 0);
303 1.20 blymn
304 1.20 blymn /* index into the key array - it's
305 1.20 blymn clearer if we stash this */
306 1.20 blymn key_ent = current->mapping[
307 1.20 blymn (unsigned) entry[j]];
308 1.20 blymn
309 1.20 blymn current->key[key_ent] = tmp_key;
310 1.20 blymn
311 1.20 blymn /* next key uses this map... */
312 1.20 blymn current = current->key[key_ent]->value.next;
313 1.12 pk }
314 1.14 simonb
315 1.20 blymn /* this is the last key in the sequence (it
316 1.20 blymn * may have been the only one but that does
317 1.20 blymn * not matter) this means it is a leaf key and
318 1.20 blymn * should have a symbol associated with it.
319 1.20 blymn */
320 1.20 blymn tmp_key = add_new_key(current,
321 1.20 blymn entry[length - 1],
322 1.20 blymn KEYMAP_LEAF,
323 1.20 blymn tc[i].symbol);
324 1.20 blymn current->key[
325 1.20 blymn current->mapping[(int)entry[length - 1]]] =
326 1.20 blymn tmp_key;
327 1.12 pk }
328 1.12 pk }
329 1.10 mrg }
330 1.10 mrg }
331 1.10 mrg
332 1.10 mrg
333 1.10 mrg /*
334 1.10 mrg * new_keymap - allocates & initialises a new keymap structure. This
335 1.10 mrg * function returns a pointer to the new keymap.
336 1.10 mrg *
337 1.10 mrg */
338 1.13 simonb static keymap_t *
339 1.10 mrg new_keymap(void)
340 1.10 mrg {
341 1.10 mrg int i;
342 1.10 mrg keymap_t *new_map;
343 1.10 mrg
344 1.10 mrg if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
345 1.10 mrg perror("Inkey: Cannot allocate new keymap");
346 1.10 mrg exit(2);
347 1.10 mrg }
348 1.12 pk
349 1.12 pk /* Initialise the new map */
350 1.10 mrg new_map->count = 0;
351 1.10 mrg for (i = 0; i < MAX_CHAR; i++) {
352 1.10 mrg new_map->mapping[i] = -1; /* no mapping for char */
353 1.10 mrg }
354 1.10 mrg
355 1.23 thorpej /* key array will be allocated when first key is added */
356 1.23 thorpej new_map->key = NULL;
357 1.23 thorpej
358 1.20 blymn return new_map;
359 1.10 mrg }
360 1.10 mrg
361 1.10 mrg /*
362 1.10 mrg * new_key - allocates & initialises a new key entry. This function returns
363 1.10 mrg * a pointer to the newly allocated key entry.
364 1.10 mrg *
365 1.10 mrg */
366 1.13 simonb static key_entry_t *
367 1.10 mrg new_key(void)
368 1.10 mrg {
369 1.10 mrg key_entry_t *new_one;
370 1.20 blymn int i;
371 1.20 blymn
372 1.20 blymn if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
373 1.20 blymn == NULL) {
374 1.20 blymn perror("inkey: Cannot allocate new key entry chunk");
375 1.10 mrg exit(2);
376 1.10 mrg }
377 1.10 mrg
378 1.20 blymn for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
379 1.20 blymn new_one[i].type = 0;
380 1.20 blymn new_one[i].value.next = NULL;
381 1.20 blymn }
382 1.20 blymn
383 1.20 blymn return new_one;
384 1.10 mrg }
385 1.10 mrg
386 1.10 mrg /*
387 1.10 mrg * inkey - do the work to process keyboard input, check for multi-key
388 1.10 mrg * sequences and return the appropriate symbol if we get a match.
389 1.10 mrg *
390 1.10 mrg */
391 1.10 mrg
392 1.16 blymn wchar_t
393 1.20 blymn inkey(int to, int delay)
394 1.10 mrg {
395 1.21 jdc wchar_t k;
396 1.22 blymn int c;
397 1.21 jdc keymap_t *current = base_keymap;
398 1.10 mrg
399 1.10 mrg for (;;) { /* loop until we get a complete key sequence */
400 1.10 mrg reread:
401 1.10 mrg if (state == INKEY_NORM) {
402 1.10 mrg if (delay && __timeout(delay) == ERR)
403 1.10 mrg return ERR;
404 1.22 blymn if ((c = getchar()) == EOF) {
405 1.22 blymn clearerr(stdin);
406 1.10 mrg return ERR;
407 1.22 blymn }
408 1.22 blymn
409 1.10 mrg if (delay && (__notimeout() == ERR))
410 1.10 mrg return ERR;
411 1.22 blymn
412 1.16 blymn k = (wchar_t) c;
413 1.10 mrg #ifdef DEBUG
414 1.10 mrg __CTRACE("inkey (state normal) got '%s'\n", unctrl(k));
415 1.10 mrg #endif
416 1.10 mrg
417 1.10 mrg working = start;
418 1.10 mrg inbuf[working] = k;
419 1.10 mrg INC_POINTER(working);
420 1.10 mrg end = working;
421 1.10 mrg state = INKEY_ASSEMBLING; /* go to the assembling
422 1.10 mrg * state now */
423 1.12 pk } else if (state == INKEY_BACKOUT) {
424 1.12 pk k = inbuf[working];
425 1.12 pk INC_POINTER(working);
426 1.12 pk if (working == end) { /* see if we have run
427 1.12 pk * out of keys in the
428 1.12 pk * backlog */
429 1.12 pk
430 1.12 pk /* if we have then switch to
431 1.12 pk assembling */
432 1.12 pk state = INKEY_ASSEMBLING;
433 1.12 pk }
434 1.12 pk } else if (state == INKEY_ASSEMBLING) {
435 1.12 pk /* assembling a key sequence */
436 1.12 pk if (delay) {
437 1.12 pk if (__timeout(to ? DEFAULT_DELAY : delay) == ERR)
438 1.10 mrg return ERR;
439 1.12 pk } else {
440 1.12 pk if (to && (__timeout(DEFAULT_DELAY) == ERR))
441 1.10 mrg return ERR;
442 1.12 pk }
443 1.22 blymn
444 1.22 blymn c = getchar();
445 1.22 blymn if (ferror(stdin)) {
446 1.22 blymn clearerr(stdin);
447 1.12 pk return ERR;
448 1.22 blymn }
449 1.22 blymn
450 1.12 pk if ((to || delay) && (__notimeout() == ERR))
451 1.10 mrg return ERR;
452 1.14 simonb
453 1.16 blymn k = (wchar_t) c;
454 1.10 mrg #ifdef DEBUG
455 1.12 pk __CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
456 1.10 mrg #endif
457 1.22 blymn if (feof(stdin)) { /* inter-char timeout,
458 1.12 pk * start backing out */
459 1.22 blymn clearerr(stdin);
460 1.12 pk if (start == end)
461 1.12 pk /* no chars in the buffer, restart */
462 1.12 pk goto reread;
463 1.12 pk
464 1.12 pk k = inbuf[start];
465 1.12 pk state = INKEY_TIMEOUT;
466 1.10 mrg } else {
467 1.12 pk inbuf[working] = k;
468 1.12 pk INC_POINTER(working);
469 1.12 pk end = working;
470 1.10 mrg }
471 1.12 pk } else {
472 1.12 pk fprintf(stderr, "Inkey state screwed - exiting!!!");
473 1.12 pk exit(2);
474 1.12 pk }
475 1.10 mrg
476 1.10 mrg /* Check key has no special meaning and we have not timed out */
477 1.20 blymn if ((state == INKEY_TIMEOUT) || (current->mapping[k] < 0)) {
478 1.12 pk /* return the first key we know about */
479 1.12 pk k = inbuf[start];
480 1.10 mrg
481 1.10 mrg INC_POINTER(start);
482 1.10 mrg working = start;
483 1.10 mrg
484 1.10 mrg if (start == end) { /* only one char processed */
485 1.10 mrg state = INKEY_NORM;
486 1.10 mrg } else {/* otherwise we must have more than one char
487 1.10 mrg * to backout */
488 1.10 mrg state = INKEY_BACKOUT;
489 1.10 mrg }
490 1.10 mrg return k;
491 1.10 mrg } else { /* must be part of a multikey sequence */
492 1.10 mrg /* check for completed key sequence */
493 1.10 mrg if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
494 1.10 mrg start = working; /* eat the key sequence
495 1.10 mrg * in inbuf */
496 1.10 mrg
497 1.12 pk /* check if inbuf empty now */
498 1.12 pk if (start == end) {
499 1.12 pk /* if it is go back to normal */
500 1.12 pk state = INKEY_NORM;
501 1.12 pk } else {
502 1.12 pk /* otherwise go to backout state */
503 1.10 mrg state = INKEY_BACKOUT;
504 1.10 mrg }
505 1.10 mrg
506 1.10 mrg /* return the symbol */
507 1.10 mrg return current->key[current->mapping[k]]->value.symbol;
508 1.10 mrg
509 1.12 pk } else {
510 1.12 pk /*
511 1.12 pk * Step on to next part of the multi-key
512 1.12 pk * sequence.
513 1.12 pk */
514 1.10 mrg current = current->key[current->mapping[k]]->value.next;
515 1.10 mrg }
516 1.10 mrg }
517 1.10 mrg }
518 1.10 mrg }
519 1.10 mrg
520 1.18 blymn #ifndef _CURSES_USE_MACROS
521 1.18 blymn /*
522 1.18 blymn * getch --
523 1.18 blymn * Read in a character from stdscr.
524 1.18 blymn */
525 1.18 blymn int
526 1.18 blymn getch(void)
527 1.18 blymn {
528 1.18 blymn return wgetch(stdscr);
529 1.18 blymn }
530 1.18 blymn
531 1.18 blymn /*
532 1.18 blymn * mvgetch --
533 1.18 blymn * Read in a character from stdscr at the given location.
534 1.18 blymn */
535 1.18 blymn int
536 1.18 blymn mvgetch(int y, int x)
537 1.18 blymn {
538 1.18 blymn return mvwgetch(stdscr, y, x);
539 1.18 blymn }
540 1.18 blymn
541 1.18 blymn /*
542 1.18 blymn * mvwgetch --
543 1.18 blymn * Read in a character from stdscr at the given location in the
544 1.18 blymn * given window.
545 1.18 blymn */
546 1.18 blymn int
547 1.18 blymn mvwgetch(WINDOW *win, int y, int x)
548 1.18 blymn {
549 1.18 blymn if (wmove(win, y, x) == ERR)
550 1.18 blymn return ERR;
551 1.18 blymn
552 1.18 blymn return wgetch(win);
553 1.18 blymn }
554 1.18 blymn
555 1.18 blymn #endif
556 1.18 blymn
557 1.1 cgd /*
558 1.4 mycroft * wgetch --
559 1.4 mycroft * Read in a character from the window.
560 1.1 cgd */
561 1.4 mycroft int
562 1.18 blymn wgetch(WINDOW *win)
563 1.4 mycroft {
564 1.10 mrg int inp, weset;
565 1.10 mrg char c;
566 1.1 cgd
567 1.5 cgd if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
568 1.10 mrg && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
569 1.10 mrg && __echoit)
570 1.4 mycroft return (ERR);
571 1.4 mycroft #ifdef DEBUG
572 1.19 jdc __CTRACE("wgetch: __echoit = %d, __rawmode = %d, flags = %0.2o\n",
573 1.19 jdc __echoit, __rawmode, win->flags);
574 1.4 mycroft #endif
575 1.4 mycroft if (__echoit && !__rawmode) {
576 1.1 cgd cbreak();
577 1.4 mycroft weset = 1;
578 1.4 mycroft } else
579 1.4 mycroft weset = 0;
580 1.4 mycroft
581 1.10 mrg __save_termios();
582 1.10 mrg
583 1.10 mrg if (win->flags & __KEYPAD) {
584 1.10 mrg switch (win->delay)
585 1.10 mrg {
586 1.10 mrg case -1:
587 1.10 mrg inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
588 1.10 mrg break;
589 1.10 mrg case 0:
590 1.19 jdc if (__nodelay() == ERR) {
591 1.19 jdc __restore_termios();
592 1.19 jdc return ERR;
593 1.19 jdc }
594 1.10 mrg inp = inkey(0, 0);
595 1.10 mrg break;
596 1.10 mrg default:
597 1.10 mrg inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
598 1.10 mrg break;
599 1.10 mrg }
600 1.10 mrg } else {
601 1.10 mrg switch (win->delay)
602 1.10 mrg {
603 1.10 mrg case -1:
604 1.10 mrg break;
605 1.10 mrg case 0:
606 1.10 mrg if (__nodelay() == ERR) {
607 1.10 mrg __restore_termios();
608 1.10 mrg return ERR;
609 1.10 mrg }
610 1.10 mrg break;
611 1.10 mrg default:
612 1.10 mrg if (__timeout(win->delay) == ERR) {
613 1.10 mrg __restore_termios();
614 1.10 mrg return ERR;
615 1.10 mrg }
616 1.10 mrg break;
617 1.10 mrg }
618 1.12 pk
619 1.22 blymn c = getchar();
620 1.22 blymn if (feof(stdin)) {
621 1.22 blymn clearerr(stdin);
622 1.22 blymn __restore_termios();
623 1.22 blymn return ERR; /* we have timed out */
624 1.22 blymn }
625 1.22 blymn
626 1.22 blymn if (ferror(stdin)) {
627 1.22 blymn clearerr(stdin);
628 1.10 mrg inp = ERR;
629 1.12 pk } else {
630 1.10 mrg inp = (unsigned int) c;
631 1.10 mrg }
632 1.10 mrg }
633 1.4 mycroft #ifdef DEBUG
634 1.15 simonb if (inp > 255)
635 1.20 blymn /* we have a key symbol - treat it differently */
636 1.20 blymn /* XXXX perhaps __unctrl should be expanded to include
637 1.20 blymn * XXXX the keysyms in the table....
638 1.20 blymn */
639 1.15 simonb __CTRACE("wgetch assembled keysym 0x%x\n", inp);
640 1.15 simonb else
641 1.15 simonb __CTRACE("wgetch got '%s'\n", unctrl(inp));
642 1.4 mycroft #endif
643 1.12 pk if (win->delay > -1) {
644 1.10 mrg if (__delay() == ERR) {
645 1.10 mrg __restore_termios();
646 1.10 mrg return ERR;
647 1.10 mrg }
648 1.12 pk }
649 1.12 pk
650 1.10 mrg __restore_termios();
651 1.4 mycroft if (__echoit) {
652 1.16 blymn waddch(win, (chtype) inp);
653 1.1 cgd }
654 1.1 cgd if (weset)
655 1.1 cgd nocbreak();
656 1.12 pk
657 1.22 blymn wrefresh(win);
658 1.10 mrg return ((inp < 0) || (inp == ERR) ? ERR : inp);
659 1.22 blymn }
660 1.22 blymn
661 1.22 blymn /*
662 1.22 blymn * ungetch --
663 1.22 blymn * Put the character back into the input queue.
664 1.22 blymn */
665 1.22 blymn int
666 1.22 blymn ungetch(int c)
667 1.22 blymn {
668 1.22 blymn return ((ungetc(c, stdin) == EOF) ? ERR : OK);
669 1.1 cgd }
670