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>
27 static key_code
key_string_search_table(const char *);
28 static key_code
key_string_get_modifiers(const char **);
33 } key_string_table
[] = {
35 { "F1", KEYC_F1
|KEYC_IMPLIED_META
},
36 { "F2", KEYC_F2
|KEYC_IMPLIED_META
},
37 { "F3", KEYC_F3
|KEYC_IMPLIED_META
},
38 { "F4", KEYC_F4
|KEYC_IMPLIED_META
},
39 { "F5", KEYC_F5
|KEYC_IMPLIED_META
},
40 { "F6", KEYC_F6
|KEYC_IMPLIED_META
},
41 { "F7", KEYC_F7
|KEYC_IMPLIED_META
},
42 { "F8", KEYC_F8
|KEYC_IMPLIED_META
},
43 { "F9", KEYC_F9
|KEYC_IMPLIED_META
},
44 { "F10", KEYC_F10
|KEYC_IMPLIED_META
},
45 { "F11", KEYC_F11
|KEYC_IMPLIED_META
},
46 { "F12", KEYC_F12
|KEYC_IMPLIED_META
},
47 { "IC", KEYC_IC
|KEYC_IMPLIED_META
},
48 { "Insert", KEYC_IC
|KEYC_IMPLIED_META
},
49 { "DC", KEYC_DC
|KEYC_IMPLIED_META
},
50 { "Delete", KEYC_DC
|KEYC_IMPLIED_META
},
51 { "Home", KEYC_HOME
|KEYC_IMPLIED_META
},
52 { "End", KEYC_END
|KEYC_IMPLIED_META
},
53 { "NPage", KEYC_NPAGE
|KEYC_IMPLIED_META
},
54 { "PageDown", KEYC_NPAGE
|KEYC_IMPLIED_META
},
55 { "PgDn", KEYC_NPAGE
|KEYC_IMPLIED_META
},
56 { "PPage", KEYC_PPAGE
|KEYC_IMPLIED_META
},
57 { "PageUp", KEYC_PPAGE
|KEYC_IMPLIED_META
},
58 { "PgUp", KEYC_PPAGE
|KEYC_IMPLIED_META
},
60 { "BTab", KEYC_BTAB
},
62 { "BSpace", KEYC_BSPACE
},
67 { "Up", KEYC_UP
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
68 { "Down", KEYC_DOWN
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
69 { "Left", KEYC_LEFT
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
70 { "Right", KEYC_RIGHT
|KEYC_CURSOR
|KEYC_IMPLIED_META
},
73 { "KP/", KEYC_KP_SLASH
|KEYC_KEYPAD
},
74 { "KP*", KEYC_KP_STAR
|KEYC_KEYPAD
},
75 { "KP-", KEYC_KP_MINUS
|KEYC_KEYPAD
},
76 { "KP7", KEYC_KP_SEVEN
|KEYC_KEYPAD
},
77 { "KP8", KEYC_KP_EIGHT
|KEYC_KEYPAD
},
78 { "KP9", KEYC_KP_NINE
|KEYC_KEYPAD
},
79 { "KP+", KEYC_KP_PLUS
|KEYC_KEYPAD
},
80 { "KP4", KEYC_KP_FOUR
|KEYC_KEYPAD
},
81 { "KP5", KEYC_KP_FIVE
|KEYC_KEYPAD
},
82 { "KP6", KEYC_KP_SIX
|KEYC_KEYPAD
},
83 { "KP1", KEYC_KP_ONE
|KEYC_KEYPAD
},
84 { "KP2", KEYC_KP_TWO
|KEYC_KEYPAD
},
85 { "KP3", KEYC_KP_THREE
|KEYC_KEYPAD
},
86 { "KPEnter", KEYC_KP_ENTER
|KEYC_KEYPAD
},
87 { "KP0", KEYC_KP_ZERO
|KEYC_KEYPAD
},
88 { "KP.", KEYC_KP_PERIOD
|KEYC_KEYPAD
},
91 KEYC_MOUSE_STRING(MOUSEDOWN1
, MouseDown1
),
92 KEYC_MOUSE_STRING(MOUSEDOWN2
, MouseDown2
),
93 KEYC_MOUSE_STRING(MOUSEDOWN3
, MouseDown3
),
94 KEYC_MOUSE_STRING(MOUSEDOWN6
, MouseDown6
),
95 KEYC_MOUSE_STRING(MOUSEDOWN7
, MouseDown7
),
96 KEYC_MOUSE_STRING(MOUSEDOWN8
, MouseDown8
),
97 KEYC_MOUSE_STRING(MOUSEDOWN9
, MouseDown9
),
98 KEYC_MOUSE_STRING(MOUSEDOWN10
, MouseDown10
),
99 KEYC_MOUSE_STRING(MOUSEDOWN11
, MouseDown11
),
100 KEYC_MOUSE_STRING(MOUSEUP1
, MouseUp1
),
101 KEYC_MOUSE_STRING(MOUSEUP2
, MouseUp2
),
102 KEYC_MOUSE_STRING(MOUSEUP3
, MouseUp3
),
103 KEYC_MOUSE_STRING(MOUSEUP6
, MouseUp6
),
104 KEYC_MOUSE_STRING(MOUSEUP7
, MouseUp7
),
105 KEYC_MOUSE_STRING(MOUSEUP8
, MouseUp8
),
106 KEYC_MOUSE_STRING(MOUSEUP9
, MouseUp9
),
107 KEYC_MOUSE_STRING(MOUSEUP10
, MouseUp10
),
108 KEYC_MOUSE_STRING(MOUSEUP11
, MouseUp11
),
109 KEYC_MOUSE_STRING(MOUSEDRAG1
, MouseDrag1
),
110 KEYC_MOUSE_STRING(MOUSEDRAG2
, MouseDrag2
),
111 KEYC_MOUSE_STRING(MOUSEDRAG3
, MouseDrag3
),
112 KEYC_MOUSE_STRING(MOUSEDRAG6
, MouseDrag6
),
113 KEYC_MOUSE_STRING(MOUSEDRAG7
, MouseDrag7
),
114 KEYC_MOUSE_STRING(MOUSEDRAG8
, MouseDrag8
),
115 KEYC_MOUSE_STRING(MOUSEDRAG9
, MouseDrag9
),
116 KEYC_MOUSE_STRING(MOUSEDRAG10
, MouseDrag10
),
117 KEYC_MOUSE_STRING(MOUSEDRAG11
, MouseDrag11
),
118 KEYC_MOUSE_STRING(MOUSEDRAGEND1
, MouseDragEnd1
),
119 KEYC_MOUSE_STRING(MOUSEDRAGEND2
, MouseDragEnd2
),
120 KEYC_MOUSE_STRING(MOUSEDRAGEND3
, MouseDragEnd3
),
121 KEYC_MOUSE_STRING(MOUSEDRAGEND6
, MouseDragEnd6
),
122 KEYC_MOUSE_STRING(MOUSEDRAGEND7
, MouseDragEnd7
),
123 KEYC_MOUSE_STRING(MOUSEDRAGEND8
, MouseDragEnd8
),
124 KEYC_MOUSE_STRING(MOUSEDRAGEND9
, MouseDragEnd9
),
125 KEYC_MOUSE_STRING(MOUSEDRAGEND10
, MouseDragEnd10
),
126 KEYC_MOUSE_STRING(MOUSEDRAGEND11
, MouseDragEnd11
),
127 KEYC_MOUSE_STRING(WHEELUP
, WheelUp
),
128 KEYC_MOUSE_STRING(WHEELDOWN
, WheelDown
),
129 KEYC_MOUSE_STRING(SECONDCLICK1
, SecondClick1
),
130 KEYC_MOUSE_STRING(SECONDCLICK2
, SecondClick2
),
131 KEYC_MOUSE_STRING(SECONDCLICK3
, SecondClick3
),
132 KEYC_MOUSE_STRING(SECONDCLICK6
, SecondClick6
),
133 KEYC_MOUSE_STRING(SECONDCLICK7
, SecondClick7
),
134 KEYC_MOUSE_STRING(SECONDCLICK8
, SecondClick8
),
135 KEYC_MOUSE_STRING(SECONDCLICK9
, SecondClick9
),
136 KEYC_MOUSE_STRING(SECONDCLICK10
, SecondClick10
),
137 KEYC_MOUSE_STRING(SECONDCLICK11
, SecondClick11
),
138 KEYC_MOUSE_STRING(DOUBLECLICK1
, DoubleClick1
),
139 KEYC_MOUSE_STRING(DOUBLECLICK2
, DoubleClick2
),
140 KEYC_MOUSE_STRING(DOUBLECLICK3
, DoubleClick3
),
141 KEYC_MOUSE_STRING(DOUBLECLICK6
, DoubleClick6
),
142 KEYC_MOUSE_STRING(DOUBLECLICK7
, DoubleClick7
),
143 KEYC_MOUSE_STRING(DOUBLECLICK8
, DoubleClick8
),
144 KEYC_MOUSE_STRING(DOUBLECLICK9
, DoubleClick9
),
145 KEYC_MOUSE_STRING(DOUBLECLICK10
, DoubleClick10
),
146 KEYC_MOUSE_STRING(DOUBLECLICK11
, DoubleClick11
),
147 KEYC_MOUSE_STRING(TRIPLECLICK1
, TripleClick1
),
148 KEYC_MOUSE_STRING(TRIPLECLICK2
, TripleClick2
),
149 KEYC_MOUSE_STRING(TRIPLECLICK3
, TripleClick3
),
150 KEYC_MOUSE_STRING(TRIPLECLICK6
, TripleClick6
),
151 KEYC_MOUSE_STRING(TRIPLECLICK7
, TripleClick7
),
152 KEYC_MOUSE_STRING(TRIPLECLICK8
, TripleClick8
),
153 KEYC_MOUSE_STRING(TRIPLECLICK9
, TripleClick9
),
154 KEYC_MOUSE_STRING(TRIPLECLICK10
, TripleClick10
),
155 KEYC_MOUSE_STRING(TRIPLECLICK11
, TripleClick11
)
158 /* Find key string in table. */
160 key_string_search_table(const char *string
)
164 for (i
= 0; i
< nitems(key_string_table
); i
++) {
165 if (strcasecmp(string
, key_string_table
[i
].string
) == 0)
166 return (key_string_table
[i
].key
);
169 if (sscanf(string
, "User%u", &user
) == 1 && user
< KEYC_NUSER
)
170 return (KEYC_USER
+ user
);
172 return (KEYC_UNKNOWN
);
175 /* Find modifiers. */
177 key_string_get_modifiers(const char **string
)
182 while (((*string
)[0] != '\0') && (*string
)[1] == '-') {
183 switch ((*string
)[0]) {
186 modifiers
|= KEYC_CTRL
;
190 modifiers
|= KEYC_META
;
194 modifiers
|= KEYC_SHIFT
;
205 /* Lookup a string and convert to a key value. */
207 key_string_lookup_string(const char *string
)
209 static const char *other
= "!#()+,-.0123456789:;<=>'\r\t\177`/";
210 key_code key
, modifiers
;
212 struct utf8_data ud
, *udp
;
213 enum utf8_state more
;
215 char m
[MB_LEN_MAX
+ 1];
218 /* Is this no key or any key? */
219 if (strcasecmp(string
, "None") == 0)
221 if (strcasecmp(string
, "Any") == 0)
224 /* Is this a hexadecimal value? */
225 if (string
[0] == '0' && string
[1] == 'x') {
226 if (sscanf(string
+ 2, "%x", &u
) != 1)
227 return (KEYC_UNKNOWN
);
231 if (mlen
<= 0 || mlen
> MB_LEN_MAX
)
232 return (KEYC_UNKNOWN
);
235 udp
= utf8_fromcstr(m
);
239 utf8_from_data(&udp
[0], &uc
) != UTF8_DONE
) {
241 return (KEYC_UNKNOWN
);
247 /* Check for modifiers. */
249 if (string
[0] == '^' && string
[1] != '\0') {
250 modifiers
|= KEYC_CTRL
;
253 modifiers
|= key_string_get_modifiers(&string
);
254 if (string
== NULL
|| string
[0] == '\0')
255 return (KEYC_UNKNOWN
);
257 /* Is this a standard ASCII key? */
258 if (string
[1] == '\0' && (u_char
)string
[0] <= 127) {
259 key
= (u_char
)string
[0];
261 return (KEYC_UNKNOWN
);
263 /* Try as a UTF-8 key. */
264 if ((more
= utf8_open(&ud
, (u_char
)*string
)) == UTF8_MORE
) {
265 if (strlen(string
) != ud
.size
)
266 return (KEYC_UNKNOWN
);
267 for (i
= 1; i
< ud
.size
; i
++)
268 more
= utf8_append(&ud
, (u_char
)string
[i
]);
269 if (more
!= UTF8_DONE
)
270 return (KEYC_UNKNOWN
);
271 if (utf8_from_data(&ud
, &uc
) != UTF8_DONE
)
272 return (KEYC_UNKNOWN
);
273 return (uc
|modifiers
);
276 /* Otherwise look the key up in the table. */
277 key
= key_string_search_table(string
);
278 if (key
== KEYC_UNKNOWN
)
279 return (KEYC_UNKNOWN
);
280 if (~modifiers
& KEYC_META
)
281 key
&= ~KEYC_IMPLIED_META
;
284 /* Convert the standard control keys. */
286 (modifiers
& KEYC_CTRL
) &&
287 strchr(other
, key
) == NULL
&&
291 if (key
>= 97 && key
<= 122)
293 else if (key
>= 64 && key
<= 95)
300 return (KEYC_UNKNOWN
);
301 modifiers
&= ~KEYC_CTRL
;
304 return (key
|modifiers
);
307 /* Convert a key code into string format, with prefix if necessary. */
309 key_string_lookup_key(key_code key
, int with_flags
)
311 key_code saved
= key
;
321 /* Literal keys are themselves. */
322 if (key
& KEYC_LITERAL
) {
323 snprintf(out
, sizeof out
, "%c", (int)(key
& 0xff));
327 /* Display C-@ as C-Space. */
328 if ((key
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
)) == 0)
331 /* Fill in the modifiers. */
333 strlcat(out
, "C-", sizeof out
);
335 strlcat(out
, "M-", sizeof out
);
336 if (key
& KEYC_SHIFT
)
337 strlcat(out
, "S-", sizeof out
);
338 key
&= KEYC_MASK_KEY
;
341 if (key
== KEYC_NONE
) {
346 /* Handle special keys. */
347 if (key
== KEYC_UNKNOWN
) {
351 if (key
== KEYC_ANY
) {
355 if (key
== KEYC_FOCUS_IN
) {
359 if (key
== KEYC_FOCUS_OUT
) {
363 if (key
== KEYC_PASTE_START
) {
367 if (key
== KEYC_PASTE_END
) {
371 if (key
== KEYC_MOUSE
) {
375 if (key
== KEYC_DRAGGING
) {
379 if (key
== KEYC_MOUSEMOVE_PANE
) {
383 if (key
== KEYC_MOUSEMOVE_STATUS
) {
384 s
= "MouseMoveStatus";
387 if (key
== KEYC_MOUSEMOVE_STATUS_LEFT
) {
388 s
= "MouseMoveStatusLeft";
391 if (key
== KEYC_MOUSEMOVE_STATUS_RIGHT
) {
392 s
= "MouseMoveStatusRight";
395 if (key
== KEYC_MOUSEMOVE_BORDER
) {
396 s
= "MouseMoveBorder";
399 if (key
>= KEYC_USER
&& key
< KEYC_USER
+ KEYC_NUSER
) {
400 snprintf(tmp
, sizeof tmp
, "User%u", (u_int
)(key
- KEYC_USER
));
401 strlcat(out
, tmp
, sizeof out
);
405 /* Try the key against the string table. */
406 for (i
= 0; i
< nitems(key_string_table
); i
++) {
407 if (key
== (key_string_table
[i
].key
& KEYC_MASK_KEY
))
410 if (i
!= nitems(key_string_table
)) {
411 strlcat(out
, key_string_table
[i
].string
, sizeof out
);
415 /* Is this a Unicode key? */
416 if (KEYC_IS_UNICODE(key
)) {
417 utf8_to_data(key
, &ud
);
419 memcpy(out
+ off
, ud
.data
, ud
.size
);
420 out
[off
+ ud
.size
] = '\0';
424 /* Invalid keys are errors. */
426 snprintf(out
, sizeof out
, "Invalid#%llx", saved
);
430 /* Check for standard or control key. */
432 if (key
== 0 || key
> 26)
433 xsnprintf(tmp
, sizeof tmp
, "C-%c", (int)(64 + key
));
435 xsnprintf(tmp
, sizeof tmp
, "C-%c", (int)(96 + key
));
436 } else if (key
>= 32 && key
<= 126) {
439 } else if (key
== 127)
440 xsnprintf(tmp
, sizeof tmp
, "C-?");
442 xsnprintf(tmp
, sizeof tmp
, "\\%llo", key
);
444 strlcat(out
, tmp
, sizeof out
);
448 strlcat(out
, s
, sizeof out
);
451 if (with_flags
&& (saved
& KEYC_MASK_FLAGS
) != 0) {
452 strlcat(out
, "[", sizeof out
);
453 if (saved
& KEYC_LITERAL
)
454 strlcat(out
, "L", sizeof out
);
455 if (saved
& KEYC_KEYPAD
)
456 strlcat(out
, "K", sizeof out
);
457 if (saved
& KEYC_CURSOR
)
458 strlcat(out
, "C", sizeof out
);
459 if (saved
& KEYC_IMPLIED_META
)
460 strlcat(out
, "I", sizeof out
);
461 if (saved
& KEYC_BUILD_MODIFIERS
)
462 strlcat(out
, "B", sizeof out
);
463 if (saved
& KEYC_EXTENDED
)
464 strlcat(out
, "E", sizeof out
);
465 strlcat(out
, "]", sizeof out
);