keymap.h revision 1.5 1 1.5 rillig /* $NetBSD: keymap.h,v 1.5 2022/04/19 20:32:16 rillig Exp $ */
2 1.1 blymn
3 1.1 blymn /*
4 1.1 blymn * Copyright (c) 2005 The NetBSD Foundation Inc.
5 1.1 blymn * All rights reserved.
6 1.1 blymn *
7 1.1 blymn * This code is derived from code donated to the NetBSD Foundation
8 1.1 blymn * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
9 1.1 blymn *
10 1.1 blymn *
11 1.1 blymn * Redistribution and use in source and binary forms, with or without
12 1.1 blymn * modification, are permitted provided that the following conditions
13 1.1 blymn * are met:
14 1.1 blymn * 1. Redistributions of source code must retain the above copyright
15 1.1 blymn * notice, this list of conditions and the following disclaimer.
16 1.1 blymn * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 blymn * notice, this list of conditions and the following disclaimer in the
18 1.1 blymn * documentation and/or other materials provided with the distribution.
19 1.1 blymn * 3. Neither the name of the NetBSD Foundation nor the names of its
20 1.1 blymn * contributors may be used to endorse or promote products derived
21 1.1 blymn * from this software without specific prior written permission.
22 1.1 blymn *
23 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24 1.1 blymn * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 1.1 blymn * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 1.1 blymn * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 blymn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 blymn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 blymn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 blymn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 blymn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 blymn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 blymn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 blymn * SUCH DAMAGE.
35 1.1 blymn */
36 1.1 blymn
37 1.1 blymn #ifndef _KEYMAP_H_
38 1.1 blymn #define _KEYMAP_H_
39 1.1 blymn
40 1.1 blymn #include <sys/cdefs.h>
41 1.1 blymn #ifndef lint
42 1.5 rillig __RCSID("$NetBSD: keymap.h,v 1.5 2022/04/19 20:32:16 rillig Exp $");
43 1.1 blymn #endif /* not lint */
44 1.1 blymn
45 1.1 blymn /* keymap related stuff */
46 1.1 blymn /*
47 1.1 blymn * moved here by Ruibiao Qiu
48 1.1 blymn * because it is needed by both getch() and get_wch()
49 1.1 blymn *
50 1.1 blymn * Keyboard input handler. Do this by snarfing
51 1.4 roy * all the info we can out of the terminfo entry for TERM and putting it
52 1.1 blymn * into a set of keymaps. A keymap is an array the size of all the possible
53 1.1 blymn * single characters we can get, the contents of the array is a structure
54 1.1 blymn * that contains the type of entry this character is (i.e. part/end of a
55 1.1 blymn * multi-char sequence or a plain char) and either a pointer which will point
56 1.1 blymn * to another keymap (in the case of a multi-char sequence) OR the data value
57 1.1 blymn * that this key should return.
58 1.1 blymn *
59 1.1 blymn */
60 1.1 blymn
61 1.1 blymn /* private data structures for holding the key definitions */
62 1.1 blymn typedef struct key_entry key_entry_t;
63 1.1 blymn
64 1.1 blymn struct key_entry {
65 1.1 blymn short type; /* type of key this is */
66 1.1 blymn bool enable; /* true if the key is active */
67 1.1 blymn union {
68 1.1 blymn keymap_t *next; /* next keymap is key is multi-key sequence */
69 1.1 blymn wchar_t symbol; /* key symbol if key is a leaf entry */
70 1.1 blymn } value;
71 1.1 blymn };
72 1.1 blymn /* Types of key structures we can have */
73 1.1 blymn #define KEYMAP_MULTI 1 /* part of a multi char sequence */
74 1.1 blymn #define KEYMAP_LEAF 2 /* key has a symbol associated with it, either
75 1.1 blymn * it is the end of a multi-char sequence or a
76 1.1 blymn * single char key that generates a symbol */
77 1.1 blymn
78 1.1 blymn /* allocate this many key_entry structs at once to speed start up must
79 1.1 blymn * be a power of 2.
80 1.1 blymn */
81 1.1 blymn #define KEYMAP_ALLOC_CHUNK 4
82 1.1 blymn
83 1.1 blymn /* The max number of different chars we can receive */
84 1.1 blymn #define MAX_CHAR 256
85 1.1 blymn
86 1.1 blymn /*
87 1.1 blymn * Unused mapping flag.
88 1.1 blymn */
89 1.1 blymn #define MAPPING_UNUSED (0 - MAX_CHAR) /* never been used */
90 1.1 blymn
91 1.1 blymn struct keymap {
92 1.1 blymn int count; /* count of number of key structs allocated */
93 1.1 blymn short mapping[MAX_CHAR]; /* mapping of key to allocated structs */
94 1.1 blymn key_entry_t **key; /* dynamic array of keys */
95 1.1 blymn };
96 1.1 blymn
97 1.1 blymn #define INC_POINTER(ptr) do { \
98 1.1 blymn (ptr)++; \
99 1.1 blymn (ptr) %= INBUF_SZ; \
100 1.5 rillig } while (0)
101 1.1 blymn
102 1.1 blymn #define INKEY_NORM 0 /* no key backlog to process */
103 1.1 blymn #define INKEY_ASSEMBLING 1 /* assembling a multi-key sequence */
104 1.1 blymn #define INKEY_BACKOUT 2 /* recovering from an unrecognised key */
105 1.1 blymn #define INKEY_TIMEOUT 3 /* multi-key sequence timeout */
106 1.1 blymn #ifdef HAVE_WCHAR
107 1.1 blymn #define INKEY_WCASSEMBLING 4 /* assembling a wide char sequence */
108 1.1 blymn #endif /* HAVE_WCHAR */
109 1.1 blymn
110 1.4 roy /* The terminfo data we are interested in and the symbols they map to */
111 1.1 blymn struct tcdata {
112 1.3 roy int code; /* code of the terminfo entry */
113 1.1 blymn wchar_t symbol; /* the symbol associated with it */
114 1.1 blymn };
115 1.1 blymn
116 1.1 blymn #endif /* _KEYMAP_H_ */
117