4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 static key_code
key_string_search_table(const char *);
29 static key_code
key_string_get_modifiers(const char **);
34 } key_string_table
[] = {
36 { "F1", KEYC_F1
|KEYC_IMPLIED_META
},
37 { "F2", KEYC_F2
|KEYC_IMPLIED_META
},
38 { "F3", KEYC_F3
|KEYC_IMPLIED_META
},
39 { "F4", KEYC_F4
|KEYC_IMPLIED_META
},
40 { "F5", KEYC_F5
|KEYC_IMPLIED_META
},
41 { "F6", KEYC_F6
|KEYC_IMPLIED_META
},
42 { "F7", KEYC_F7
|KEYC_IMPLIED_META
},
43 { "F8", KEYC_F8
|KEYC_IMPLIED_META
},
44 { "F9", KEYC_F9
|KEYC_IMPLIED_META
},
45 { "F10", KEYC_F10
|KEYC_IMPLIED_META
},
46 { "F11", KEYC_F11
|KEYC_IMPLIED_META
},
47 { "F12", KEYC_F12
|KEYC_IMPLIED_META
},
48 { "IC", KEYC_IC
|KEYC_IMPLIED_META
},
49 { "Insert", KEYC_IC
|KEYC_IMPLIED_META
},
50 { "DC", KEYC_DC
|KEYC_IMPLIED_META
},
51 { "Delete", KEYC_DC
|KEYC_IMPLIED_META
},
52 { "Home", KEYC_HOME
|KEYC_IMPLIED_META
},
53 { "End", KEYC_END
|KEYC_IMPLIED_META
},
54 { "NPage", KEYC_NPAGE
|KEYC_IMPLIED_META
},
55 { "PageDown", KEYC_NPAGE
|KEYC_IMPLIED_META
},
56 { "PgDn", KEYC_NPAGE
|KEYC_IMPLIED_META
},
57 { "PPage", KEYC_PPAGE
|KEYC_IMPLIED_META
},
58 { "PageUp", KEYC_PPAGE
|KEYC_IMPLIED_META
},
59 { "PgUp", KEYC_PPAGE
|KEYC_IMPLIED_META
},
60 { "BTab", KEYC_BTAB
},
62 { "BSpace", KEYC_BSPACE
},
65 * C0 control characters, with the exception of Tab, Enter,
66 * and Esc, should never appear as keys. We still render them,
67 * so to be able to spot them in logs in case of an abnormality.
103 { "Up", KEYC_UP
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
104 { "Down", KEYC_DOWN
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
105 { "Left", KEYC_LEFT
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
106 { "Right", KEYC_RIGHT
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
108 /* Numeric keypad. */
109 { "KP/", KEYC_KP_SLASH
|KEYC_KEYPAD
},
110 { "KP*", KEYC_KP_STAR
|KEYC_KEYPAD
},
111 { "KP-", KEYC_KP_MINUS
|KEYC_KEYPAD
},
112 { "KP7", KEYC_KP_SEVEN
|KEYC_KEYPAD
},
113 { "KP8", KEYC_KP_EIGHT
|KEYC_KEYPAD
},
114 { "KP9", KEYC_KP_NINE
|KEYC_KEYPAD
},
115 { "KP+", KEYC_KP_PLUS
|KEYC_KEYPAD
},
116 { "KP4", KEYC_KP_FOUR
|KEYC_KEYPAD
},
117 { "KP5", KEYC_KP_FIVE
|KEYC_KEYPAD
},
118 { "KP6", KEYC_KP_SIX
|KEYC_KEYPAD
},
119 { "KP1", KEYC_KP_ONE
|KEYC_KEYPAD
},
120 { "KP2", KEYC_KP_TWO
|KEYC_KEYPAD
},
121 { "KP3", KEYC_KP_THREE
|KEYC_KEYPAD
},
122 { "KPEnter", KEYC_KP_ENTER
|KEYC_KEYPAD
},
123 { "KP0", KEYC_KP_ZERO
|KEYC_KEYPAD
},
124 { "KP.", KEYC_KP_PERIOD
|KEYC_KEYPAD
},
127 KEYC_MOUSE_STRING(MOUSEDOWN1
, MouseDown1
),
128 KEYC_MOUSE_STRING(MOUSEDOWN2
, MouseDown2
),
129 KEYC_MOUSE_STRING(MOUSEDOWN3
, MouseDown3
),
130 KEYC_MOUSE_STRING(MOUSEDOWN6
, MouseDown6
),
131 KEYC_MOUSE_STRING(MOUSEDOWN7
, MouseDown7
),
132 KEYC_MOUSE_STRING(MOUSEDOWN8
, MouseDown8
),
133 KEYC_MOUSE_STRING(MOUSEDOWN9
, MouseDown9
),
134 KEYC_MOUSE_STRING(MOUSEDOWN10
, MouseDown10
),
135 KEYC_MOUSE_STRING(MOUSEDOWN11
, MouseDown11
),
136 KEYC_MOUSE_STRING(MOUSEUP1
, MouseUp1
),
137 KEYC_MOUSE_STRING(MOUSEUP2
, MouseUp2
),
138 KEYC_MOUSE_STRING(MOUSEUP3
, MouseUp3
),
139 KEYC_MOUSE_STRING(MOUSEUP6
, MouseUp6
),
140 KEYC_MOUSE_STRING(MOUSEUP7
, MouseUp7
),
141 KEYC_MOUSE_STRING(MOUSEUP8
, MouseUp8
),
142 KEYC_MOUSE_STRING(MOUSEUP9
, MouseUp9
),
143 KEYC_MOUSE_STRING(MOUSEUP10
, MouseUp10
),
144 KEYC_MOUSE_STRING(MOUSEUP11
, MouseUp11
),
145 KEYC_MOUSE_STRING(MOUSEDRAG1
, MouseDrag1
),
146 KEYC_MOUSE_STRING(MOUSEDRAG2
, MouseDrag2
),
147 KEYC_MOUSE_STRING(MOUSEDRAG3
, MouseDrag3
),
148 KEYC_MOUSE_STRING(MOUSEDRAG6
, MouseDrag6
),
149 KEYC_MOUSE_STRING(MOUSEDRAG7
, MouseDrag7
),
150 KEYC_MOUSE_STRING(MOUSEDRAG8
, MouseDrag8
),
151 KEYC_MOUSE_STRING(MOUSEDRAG9
, MouseDrag9
),
152 KEYC_MOUSE_STRING(MOUSEDRAG10
, MouseDrag10
),
153 KEYC_MOUSE_STRING(MOUSEDRAG11
, MouseDrag11
),
154 KEYC_MOUSE_STRING(MOUSEDRAGEND1
, MouseDragEnd1
),
155 KEYC_MOUSE_STRING(MOUSEDRAGEND2
, MouseDragEnd2
),
156 KEYC_MOUSE_STRING(MOUSEDRAGEND3
, MouseDragEnd3
),
157 KEYC_MOUSE_STRING(MOUSEDRAGEND6
, MouseDragEnd6
),
158 KEYC_MOUSE_STRING(MOUSEDRAGEND7
, MouseDragEnd7
),
159 KEYC_MOUSE_STRING(MOUSEDRAGEND8
, MouseDragEnd8
),
160 KEYC_MOUSE_STRING(MOUSEDRAGEND9
, MouseDragEnd9
),
161 KEYC_MOUSE_STRING(MOUSEDRAGEND10
, MouseDragEnd10
),
162 KEYC_MOUSE_STRING(MOUSEDRAGEND11
, MouseDragEnd11
),
163 KEYC_MOUSE_STRING(WHEELUP
, WheelUp
),
164 KEYC_MOUSE_STRING(WHEELDOWN
, WheelDown
),
165 KEYC_MOUSE_STRING(SECONDCLICK1
, SecondClick1
),
166 KEYC_MOUSE_STRING(SECONDCLICK2
, SecondClick2
),
167 KEYC_MOUSE_STRING(SECONDCLICK3
, SecondClick3
),
168 KEYC_MOUSE_STRING(SECONDCLICK6
, SecondClick6
),
169 KEYC_MOUSE_STRING(SECONDCLICK7
, SecondClick7
),
170 KEYC_MOUSE_STRING(SECONDCLICK8
, SecondClick8
),
171 KEYC_MOUSE_STRING(SECONDCLICK9
, SecondClick9
),
172 KEYC_MOUSE_STRING(SECONDCLICK10
, SecondClick10
),
173 KEYC_MOUSE_STRING(SECONDCLICK11
, SecondClick11
),
174 KEYC_MOUSE_STRING(DOUBLECLICK1
, DoubleClick1
),
175 KEYC_MOUSE_STRING(DOUBLECLICK2
, DoubleClick2
),
176 KEYC_MOUSE_STRING(DOUBLECLICK3
, DoubleClick3
),
177 KEYC_MOUSE_STRING(DOUBLECLICK6
, DoubleClick6
),
178 KEYC_MOUSE_STRING(DOUBLECLICK7
, DoubleClick7
),
179 KEYC_MOUSE_STRING(DOUBLECLICK8
, DoubleClick8
),
180 KEYC_MOUSE_STRING(DOUBLECLICK9
, DoubleClick9
),
181 KEYC_MOUSE_STRING(DOUBLECLICK10
, DoubleClick10
),
182 KEYC_MOUSE_STRING(DOUBLECLICK11
, DoubleClick11
),
183 KEYC_MOUSE_STRING(TRIPLECLICK1
, TripleClick1
),
184 KEYC_MOUSE_STRING(TRIPLECLICK2
, TripleClick2
),
185 KEYC_MOUSE_STRING(TRIPLECLICK3
, TripleClick3
),
186 KEYC_MOUSE_STRING(TRIPLECLICK6
, TripleClick6
),
187 KEYC_MOUSE_STRING(TRIPLECLICK7
, TripleClick7
),
188 KEYC_MOUSE_STRING(TRIPLECLICK8
, TripleClick8
),
189 KEYC_MOUSE_STRING(TRIPLECLICK9
, TripleClick9
),
190 KEYC_MOUSE_STRING(TRIPLECLICK10
, TripleClick10
),
191 KEYC_MOUSE_STRING(TRIPLECLICK11
, TripleClick11
)
194 /* Find key string in table. */
196 key_string_search_table(const char *string
)
200 for (i
= 0; i
< nitems(key_string_table
); i
++) {
201 if (strcasecmp(string
, key_string_table
[i
].string
) == 0)
202 return (key_string_table
[i
].key
);
205 if (sscanf(string
, "User%u", &user
) == 1 && user
< KEYC_NUSER
)
206 return (KEYC_USER
+ user
);
208 return (KEYC_UNKNOWN
);
211 /* Find modifiers. */
213 key_string_get_modifiers(const char **string
)
218 while (((*string
)[0] != '\0') && (*string
)[1] == '-') {
219 switch ((*string
)[0]) {
222 modifiers
|= KEYC_CTRL
;
226 modifiers
|= KEYC_META
;
230 modifiers
|= KEYC_SHIFT
;
241 /* Lookup a string and convert to a key value. */
243 key_string_lookup_string(const char *string
)
245 key_code key
, modifiers
= 0;
247 struct utf8_data ud
, *udp
;
248 enum utf8_state more
;
250 char m
[MB_LEN_MAX
+ 1];
253 /* Is this no key or any key? */
254 if (strcasecmp(string
, "None") == 0)
256 if (strcasecmp(string
, "Any") == 0)
259 /* Is this a hexadecimal value? */
260 if (string
[0] == '0' && string
[1] == 'x') {
261 if (sscanf(string
+ 2, "%x", &u
) != 1)
262 return (KEYC_UNKNOWN
);
266 if (mlen
<= 0 || mlen
> MB_LEN_MAX
)
267 return (KEYC_UNKNOWN
);
270 udp
= utf8_fromcstr(m
);
274 utf8_from_data(&udp
[0], &uc
) != UTF8_DONE
) {
276 return (KEYC_UNKNOWN
);
282 /* Check for short Ctrl key. */
283 if (string
[0] == '^' && string
[1] != '\0') {
284 if (string
[2] == '\0')
285 return (tolower((u_char
)string
[1])|KEYC_CTRL
);
286 modifiers
|= KEYC_CTRL
;
290 /* Check for modifiers. */
291 modifiers
|= key_string_get_modifiers(&string
);
292 if (string
== NULL
|| string
[0] == '\0')
293 return (KEYC_UNKNOWN
);
295 /* Is this a standard ASCII key? */
296 if (string
[1] == '\0' && (u_char
)string
[0] <= 127) {
297 key
= (u_char
)string
[0];
299 return (KEYC_UNKNOWN
);
301 /* Try as a UTF-8 key. */
302 if ((more
= utf8_open(&ud
, (u_char
)*string
)) == UTF8_MORE
) {
303 if (strlen(string
) != ud
.size
)
304 return (KEYC_UNKNOWN
);
305 for (i
= 1; i
< ud
.size
; i
++)
306 more
= utf8_append(&ud
, (u_char
)string
[i
]);
307 if (more
!= UTF8_DONE
)
308 return (KEYC_UNKNOWN
);
309 if (utf8_from_data(&ud
, &uc
) != UTF8_DONE
)
310 return (KEYC_UNKNOWN
);
311 return (uc
|modifiers
);
314 /* Otherwise look the key up in the table. */
315 key
= key_string_search_table(string
);
316 if (key
== KEYC_UNKNOWN
)
317 return (KEYC_UNKNOWN
);
318 if (~modifiers
& KEYC_META
)
319 key
&= ~KEYC_IMPLIED_META
;
322 return (key
|modifiers
);
325 /* Convert a key code into string format, with prefix if necessary. */
327 key_string_lookup_key(key_code key
, int with_flags
)
329 key_code saved
= key
;
339 /* Literal keys are themselves. */
340 if (key
& KEYC_LITERAL
) {
341 snprintf(out
, sizeof out
, "%c", (int)(key
& 0xff));
345 /* Fill in the modifiers. */
347 strlcat(out
, "C-", sizeof out
);
349 strlcat(out
, "M-", sizeof out
);
350 if (key
& KEYC_SHIFT
)
351 strlcat(out
, "S-", sizeof out
);
352 key
&= KEYC_MASK_KEY
;
355 if (key
== KEYC_NONE
) {
360 /* Handle special keys. */
361 if (key
== KEYC_UNKNOWN
) {
365 if (key
== KEYC_ANY
) {
369 if (key
== KEYC_FOCUS_IN
) {
373 if (key
== KEYC_FOCUS_OUT
) {
377 if (key
== KEYC_PASTE_START
) {
381 if (key
== KEYC_PASTE_END
) {
385 if (key
== KEYC_MOUSE
) {
389 if (key
== KEYC_DRAGGING
) {
393 if (key
== KEYC_MOUSEMOVE_PANE
) {
397 if (key
== KEYC_MOUSEMOVE_STATUS
) {
398 s
= "MouseMoveStatus";
401 if (key
== KEYC_MOUSEMOVE_STATUS_LEFT
) {
402 s
= "MouseMoveStatusLeft";
405 if (key
== KEYC_MOUSEMOVE_STATUS_RIGHT
) {
406 s
= "MouseMoveStatusRight";
409 if (key
== KEYC_MOUSEMOVE_BORDER
) {
410 s
= "MouseMoveBorder";
413 if (key
>= KEYC_USER
&& key
< KEYC_USER_END
) {
414 snprintf(tmp
, sizeof tmp
, "User%u", (u_int
)(key
- KEYC_USER
));
415 strlcat(out
, tmp
, sizeof out
);
419 /* Try the key against the string table. */
420 for (i
= 0; i
< nitems(key_string_table
); i
++) {
421 if (key
== (key_string_table
[i
].key
& KEYC_MASK_KEY
))
424 if (i
!= nitems(key_string_table
)) {
425 strlcat(out
, key_string_table
[i
].string
, sizeof out
);
429 /* Is this a Unicode key? */
430 if (KEYC_IS_UNICODE(key
)) {
431 utf8_to_data(key
, &ud
);
433 memcpy(out
+ off
, ud
.data
, ud
.size
);
434 out
[off
+ ud
.size
] = '\0';
438 /* Invalid keys are errors. */
440 snprintf(out
, sizeof out
, "Invalid#%llx", saved
);
444 /* Printable ASCII keys. */
445 if (key
> 32 && key
<= 126) {
448 } else if (key
== 127)
449 xsnprintf(tmp
, sizeof tmp
, "C-?");
451 xsnprintf(tmp
, sizeof tmp
, "\\%llo", key
);
453 strlcat(out
, tmp
, sizeof out
);
457 strlcat(out
, s
, sizeof out
);
460 if (with_flags
&& (saved
& KEYC_MASK_FLAGS
) != 0) {
461 strlcat(out
, "[", sizeof out
);
462 if (saved
& KEYC_LITERAL
)
463 strlcat(out
, "L", sizeof out
);
464 if (saved
& KEYC_KEYPAD
)
465 strlcat(out
, "K", sizeof out
);
466 if (saved
& KEYC_CURSOR
)
467 strlcat(out
, "C", sizeof out
);
468 if (saved
& KEYC_IMPLIED_META
)
469 strlcat(out
, "I", sizeof out
);
470 if (saved
& KEYC_BUILD_MODIFIERS
)
471 strlcat(out
, "B", sizeof out
);
472 if (saved
& KEYC_SENT
)
473 strlcat(out
, "S", sizeof out
);
474 strlcat(out
, "]", sizeof out
);