Home | History | Annotate | Line # | Download | only in libcurses
keymap.h revision 1.1.2.1
      1 /*   $NetBSD: keymap.h,v 1.1.2.1 2007/01/21 12:05:54 blymn Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2005 The NetBSD Foundation Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from code donated to the NetBSD Foundation
      8  * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
      9  *
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef _KEYMAP_H_
     38 #define _KEYMAP_H_
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __RCSID("$NetBSD: keymap.h,v 1.1.2.1 2007/01/21 12:05:54 blymn Exp $");
     43 #endif                          /* not lint */
     44 
     45 /* keymap related stuff */
     46 /*
     47  * moved here by Ruibiao Qiu
     48  * because it is needed by both getch() and get_wch()
     49  *
     50  * Keyboard input handler.  Do this by snarfing
     51  * all the info we can out of the termcap entry for TERM and putting it
     52  * into a set of keymaps.  A keymap is an array the size of all the possible
     53  * single characters we can get, the contents of the array is a structure
     54  * that contains the type of entry this character is (i.e. part/end of a
     55  * multi-char sequence or a plain char) and either a pointer which will point
     56  * to another keymap (in the case of a multi-char sequence) OR the data value
     57  * that this key should return.
     58  *
     59  */
     60 
     61 /* private data structures for holding the key definitions */
     62 typedef struct key_entry key_entry_t;
     63 
     64 struct key_entry {
     65 	short   type;		/* type of key this is */
     66 	bool    enable;         /* true if the key is active */
     67 	union {
     68 		keymap_t *next;	/* next keymap is key is multi-key sequence */
     69 		wchar_t   symbol;	/* key symbol if key is a leaf entry */
     70 	} value;
     71 };
     72 /* Types of key structures we can have */
     73 #define KEYMAP_MULTI  1		/* part of a multi char sequence */
     74 #define KEYMAP_LEAF   2		/* key has a symbol associated with it, either
     75 				 * it is the end of a multi-char sequence or a
     76 				 * single char key that generates a symbol */
     77 
     78 /* allocate this many key_entry structs at once to speed start up must
     79  * be a power of 2.
     80  */
     81 #define KEYMAP_ALLOC_CHUNK 4
     82 
     83 /* The max number of different chars we can receive */
     84 #define MAX_CHAR 256
     85 
     86 /*
     87  * Unused mapping flag.
     88  */
     89 #define MAPPING_UNUSED (0 - MAX_CHAR) /* never been used */
     90 
     91 struct keymap {
     92 	int	count;	       /* count of number of key structs allocated */
     93 	short	mapping[MAX_CHAR]; /* mapping of key to allocated structs */
     94 	key_entry_t **key;     /* dynamic array of keys */
     95 };
     96 
     97 #define INC_POINTER(ptr)  do {	\
     98 	(ptr)++;		\
     99 	(ptr) %= INBUF_SZ;	\
    100 } while(/*CONSTCOND*/0)
    101 
    102 static short	state;		/* state of the inkey function */
    103 
    104 #define INKEY_NORM	   0 /* no key backlog to process */
    105 #define INKEY_ASSEMBLING   1 /* assembling a multi-key sequence */
    106 #define INKEY_BACKOUT	   2 /* recovering from an unrecognised key */
    107 #define INKEY_TIMEOUT	   3 /* multi-key sequence timeout */
    108 #ifdef HAVE_WCHAR
    109 #define INKEY_WCASSEMBLING 4 /* assembling a wide char sequence */
    110 #endif /* HAVE_WCHAR */
    111 
    112 /* The termcap data we are interested in and the symbols they map to */
    113 struct tcdata {
    114 	const char	*name;	/* name of termcap entry */
    115 	wchar_t	symbol;		/* the symbol associated with it */
    116 };
    117 
    118 static const struct tcdata tc[] = {
    119 	{"!1", KEY_SSAVE},
    120 	{"!2", KEY_SSUSPEND},
    121 	{"!3", KEY_SUNDO},
    122 	{"#1", KEY_SHELP},
    123 	{"#2", KEY_SHOME},
    124 	{"#3", KEY_SIC},
    125 	{"#4", KEY_SLEFT},
    126 	{"%0", KEY_REDO},
    127 	{"%1", KEY_HELP},
    128 	{"%2", KEY_MARK},
    129 	{"%3", KEY_MESSAGE},
    130 	{"%4", KEY_MOVE},
    131 	{"%5", KEY_NEXT},
    132 	{"%6", KEY_OPEN},
    133 	{"%7", KEY_OPTIONS},
    134 	{"%8", KEY_PREVIOUS},
    135 	{"%9", KEY_PRINT},
    136 	{"%a", KEY_SMESSAGE},
    137 	{"%b", KEY_SMOVE},
    138 	{"%c", KEY_SNEXT},
    139 	{"%d", KEY_SOPTIONS},
    140 	{"%e", KEY_SPREVIOUS},
    141 	{"%f", KEY_SPRINT},
    142 	{"%g", KEY_SREDO},
    143 	{"%h", KEY_SREPLACE},
    144 	{"%i", KEY_SRIGHT},
    145 	{"%j", KEY_SRSUME},
    146 	{"&0", KEY_SCANCEL},
    147 	{"&1", KEY_REFERENCE},
    148 	{"&2", KEY_REFRESH},
    149 	{"&3", KEY_REPLACE},
    150 	{"&4", KEY_RESTART},
    151 	{"&5", KEY_RESUME},
    152 	{"&6", KEY_SAVE},
    153 	{"&7", KEY_SUSPEND},
    154 	{"&8", KEY_UNDO},
    155 	{"&9", KEY_SBEG},
    156 	{"*0", KEY_SFIND},
    157 	{"*1", KEY_SCOMMAND},
    158 	{"*2", KEY_SCOPY},
    159 	{"*3", KEY_SCREATE},
    160 	{"*4", KEY_SDC},
    161 	{"*5", KEY_SDL},
    162 	{"*6", KEY_SELECT},
    163 	{"*7", KEY_SEND},
    164 	{"*8", KEY_SEOL},
    165 	{"*9", KEY_SEXIT},
    166 	{"@0", KEY_FIND},
    167 	{"@1", KEY_BEG},
    168 	{"@2", KEY_CANCEL},
    169 	{"@3", KEY_CLOSE},
    170 	{"@4", KEY_COMMAND},
    171 	{"@5", KEY_COPY},
    172 	{"@6", KEY_CREATE},
    173 	{"@7", KEY_END},
    174 	{"@8", KEY_ENTER},
    175 	{"@9", KEY_EXIT},
    176 	{"F1", KEY_F(11)},
    177 	{"F2", KEY_F(12)},
    178 	{"F3", KEY_F(13)},
    179 	{"F4", KEY_F(14)},
    180 	{"F5", KEY_F(15)},
    181 	{"F6", KEY_F(16)},
    182 	{"F7", KEY_F(17)},
    183 	{"F8", KEY_F(18)},
    184 	{"F9", KEY_F(19)},
    185 	{"FA", KEY_F(20)},
    186 	{"FB", KEY_F(21)},
    187 	{"FC", KEY_F(22)},
    188 	{"FD", KEY_F(23)},
    189 	{"FE", KEY_F(24)},
    190 	{"FF", KEY_F(25)},
    191 	{"FG", KEY_F(26)},
    192 	{"FH", KEY_F(27)},
    193 	{"FI", KEY_F(28)},
    194 	{"FJ", KEY_F(29)},
    195 	{"FK", KEY_F(30)},
    196 	{"FL", KEY_F(31)},
    197 	{"FM", KEY_F(32)},
    198 	{"FN", KEY_F(33)},
    199 	{"FO", KEY_F(34)},
    200 	{"FP", KEY_F(35)},
    201 	{"FQ", KEY_F(36)},
    202 	{"FR", KEY_F(37)},
    203 	{"FS", KEY_F(38)},
    204 	{"FT", KEY_F(39)},
    205 	{"FU", KEY_F(40)},
    206 	{"FV", KEY_F(41)},
    207 	{"FW", KEY_F(42)},
    208 	{"FX", KEY_F(43)},
    209 	{"FY", KEY_F(44)},
    210 	{"FZ", KEY_F(45)},
    211 	{"Fa", KEY_F(46)},
    212 	{"Fb", KEY_F(47)},
    213 	{"Fc", KEY_F(48)},
    214 	{"Fd", KEY_F(49)},
    215 	{"Fe", KEY_F(50)},
    216 	{"Ff", KEY_F(51)},
    217 	{"Fg", KEY_F(52)},
    218 	{"Fh", KEY_F(53)},
    219 	{"Fi", KEY_F(54)},
    220 	{"Fj", KEY_F(55)},
    221 	{"Fk", KEY_F(56)},
    222 	{"Fl", KEY_F(57)},
    223 	{"Fm", KEY_F(58)},
    224 	{"Fn", KEY_F(59)},
    225 	{"Fo", KEY_F(60)},
    226 	{"Fp", KEY_F(61)},
    227 	{"Fq", KEY_F(62)},
    228 	{"Fr", KEY_F(63)},
    229 	{"K1", KEY_A1},
    230 	{"K2", KEY_B2},
    231 	{"K3", KEY_A3},
    232 	{"K4", KEY_C1},
    233 	{"K5", KEY_C3},
    234 	{"Km", KEY_MOUSE},
    235 	{"k0", KEY_F0},
    236 	{"k1", KEY_F(1)},
    237 	{"k2", KEY_F(2)},
    238 	{"k3", KEY_F(3)},
    239 	{"k4", KEY_F(4)},
    240 	{"k5", KEY_F(5)},
    241 	{"k6", KEY_F(6)},
    242 	{"k7", KEY_F(7)},
    243 	{"k8", KEY_F(8)},
    244 	{"k9", KEY_F(9)},
    245 	{"k;", KEY_F(10)},
    246 	{"kA", KEY_IL},
    247 	{"ka", KEY_CATAB},
    248 	{"kB", KEY_BTAB},
    249 	{"kb", KEY_BACKSPACE},
    250 	{"kC", KEY_CLEAR},
    251 	{"kD", KEY_DC},
    252 	{"kd", KEY_DOWN},
    253 	{"kE", KEY_EOL},
    254 	{"kF", KEY_SF},
    255 	{"kH", KEY_LL},
    256 	{"kh", KEY_HOME},
    257 	{"kI", KEY_IC},
    258 	{"kL", KEY_DL},
    259 	{"kl", KEY_LEFT},
    260 	{"kM", KEY_EIC},
    261 	{"kN", KEY_NPAGE},
    262 	{"kP", KEY_PPAGE},
    263 	{"kR", KEY_SR},
    264 	{"kr", KEY_RIGHT},
    265 	{"kS", KEY_EOS},
    266 	{"kT", KEY_STAB},
    267 	{"kt", KEY_CTAB},
    268 	{"ku", KEY_UP}
    269 };
    270 /* Number of TC entries .... */
    271 static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
    272 #endif /* _KEYMAP_H_ */
    273