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