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