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(MOUSEUP1
, MouseUp1
),
95 KEYC_MOUSE_STRING(MOUSEUP2
, MouseUp2
),
96 KEYC_MOUSE_STRING(MOUSEUP3
, MouseUp3
),
97 KEYC_MOUSE_STRING(MOUSEDRAG1
, MouseDrag1
),
98 KEYC_MOUSE_STRING(MOUSEDRAG2
, MouseDrag2
),
99 KEYC_MOUSE_STRING(MOUSEDRAG3
, MouseDrag3
),
100 KEYC_MOUSE_STRING(MOUSEDRAGEND1
, MouseDragEnd1
),
101 KEYC_MOUSE_STRING(MOUSEDRAGEND2
, MouseDragEnd2
),
102 KEYC_MOUSE_STRING(MOUSEDRAGEND3
, MouseDragEnd3
),
103 KEYC_MOUSE_STRING(WHEELUP
, WheelUp
),
104 KEYC_MOUSE_STRING(WHEELDOWN
, WheelDown
),
105 KEYC_MOUSE_STRING(SECONDCLICK1
, SecondClick1
),
106 KEYC_MOUSE_STRING(SECONDCLICK2
, SecondClick2
),
107 KEYC_MOUSE_STRING(SECONDCLICK3
, SecondClick3
),
108 KEYC_MOUSE_STRING(DOUBLECLICK1
, DoubleClick1
),
109 KEYC_MOUSE_STRING(DOUBLECLICK2
, DoubleClick2
),
110 KEYC_MOUSE_STRING(DOUBLECLICK3
, DoubleClick3
),
111 KEYC_MOUSE_STRING(TRIPLECLICK1
, TripleClick1
),
112 KEYC_MOUSE_STRING(TRIPLECLICK2
, TripleClick2
),
113 KEYC_MOUSE_STRING(TRIPLECLICK3
, TripleClick3
),
116 /* Find key string in table. */
118 key_string_search_table(const char *string
)
122 for (i
= 0; i
< nitems(key_string_table
); i
++) {
123 if (strcasecmp(string
, key_string_table
[i
].string
) == 0)
124 return (key_string_table
[i
].key
);
127 if (sscanf(string
, "User%u", &user
) == 1 && user
< KEYC_NUSER
)
128 return (KEYC_USER
+ user
);
130 return (KEYC_UNKNOWN
);
133 /* Find modifiers. */
135 key_string_get_modifiers(const char **string
)
140 while (((*string
)[0] != '\0') && (*string
)[1] == '-') {
141 switch ((*string
)[0]) {
144 modifiers
|= KEYC_CTRL
;
148 modifiers
|= KEYC_META
;
152 modifiers
|= KEYC_SHIFT
;
163 /* Lookup a string and convert to a key value. */
165 key_string_lookup_string(const char *string
)
167 static const char *other
= "!#()+,-.0123456789:;<=>'\r\t\177`/";
168 key_code key
, modifiers
;
170 struct utf8_data ud
, *udp
;
171 enum utf8_state more
;
173 char m
[MB_LEN_MAX
+ 1];
176 /* Is this no key or any key? */
177 if (strcasecmp(string
, "None") == 0)
179 if (strcasecmp(string
, "Any") == 0)
182 /* Is this a hexadecimal value? */
183 if (string
[0] == '0' && string
[1] == 'x') {
184 if (sscanf(string
+ 2, "%x", &u
) != 1)
185 return (KEYC_UNKNOWN
);
189 if (mlen
<= 0 || mlen
> MB_LEN_MAX
)
190 return (KEYC_UNKNOWN
);
193 udp
= utf8_fromcstr(m
);
197 utf8_from_data(&udp
[0], &uc
) != UTF8_DONE
) {
199 return (KEYC_UNKNOWN
);
205 /* Check for modifiers. */
207 if (string
[0] == '^' && string
[1] != '\0') {
208 modifiers
|= KEYC_CTRL
;
211 modifiers
|= key_string_get_modifiers(&string
);
212 if (string
== NULL
|| string
[0] == '\0')
213 return (KEYC_UNKNOWN
);
215 /* Is this a standard ASCII key? */
216 if (string
[1] == '\0' && (u_char
)string
[0] <= 127) {
217 key
= (u_char
)string
[0];
219 return (KEYC_UNKNOWN
);
221 /* Try as a UTF-8 key. */
222 if ((more
= utf8_open(&ud
, (u_char
)*string
)) == UTF8_MORE
) {
223 if (strlen(string
) != ud
.size
)
224 return (KEYC_UNKNOWN
);
225 for (i
= 1; i
< ud
.size
; i
++)
226 more
= utf8_append(&ud
, (u_char
)string
[i
]);
227 if (more
!= UTF8_DONE
)
228 return (KEYC_UNKNOWN
);
229 if (utf8_from_data(&ud
, &uc
) != UTF8_DONE
)
230 return (KEYC_UNKNOWN
);
231 return (uc
|modifiers
);
234 /* Otherwise look the key up in the table. */
235 key
= key_string_search_table(string
);
236 if (key
== KEYC_UNKNOWN
)
237 return (KEYC_UNKNOWN
);
238 if (~modifiers
& KEYC_META
)
239 key
&= ~KEYC_IMPLIED_META
;
242 /* Convert the standard control keys. */
244 (modifiers
& KEYC_CTRL
) &&
245 strchr(other
, key
) == NULL
&&
249 if (key
>= 97 && key
<= 122)
251 else if (key
>= 64 && key
<= 95)
258 return (KEYC_UNKNOWN
);
259 modifiers
&= ~KEYC_CTRL
;
262 return (key
|modifiers
);
265 /* Convert a key code into string format, with prefix if necessary. */
267 key_string_lookup_key(key_code key
, int with_flags
)
269 key_code saved
= key
;
279 /* Literal keys are themselves. */
280 if (key
& KEYC_LITERAL
) {
281 snprintf(out
, sizeof out
, "%c", (int)(key
& 0xff));
285 /* Display C-@ as C-Space. */
286 if ((key
& (KEYC_MASK_KEY
|KEYC_MASK_MODIFIERS
)) == 0)
289 /* Fill in the modifiers. */
291 strlcat(out
, "C-", sizeof out
);
293 strlcat(out
, "M-", sizeof out
);
294 if (key
& KEYC_SHIFT
)
295 strlcat(out
, "S-", sizeof out
);
296 key
&= KEYC_MASK_KEY
;
299 if (key
== KEYC_NONE
) {
304 /* Handle special keys. */
305 if (key
== KEYC_UNKNOWN
) {
309 if (key
== KEYC_ANY
) {
313 if (key
== KEYC_FOCUS_IN
) {
317 if (key
== KEYC_FOCUS_OUT
) {
321 if (key
== KEYC_PASTE_START
) {
325 if (key
== KEYC_PASTE_END
) {
329 if (key
== KEYC_MOUSE
) {
333 if (key
== KEYC_DRAGGING
) {
337 if (key
== KEYC_MOUSEMOVE_PANE
) {
341 if (key
== KEYC_MOUSEMOVE_STATUS
) {
342 s
= "MouseMoveStatus";
345 if (key
== KEYC_MOUSEMOVE_STATUS_LEFT
) {
346 s
= "MouseMoveStatusLeft";
349 if (key
== KEYC_MOUSEMOVE_STATUS_RIGHT
) {
350 s
= "MouseMoveStatusRight";
353 if (key
== KEYC_MOUSEMOVE_BORDER
) {
354 s
= "MouseMoveBorder";
357 if (key
>= KEYC_USER
&& key
< KEYC_USER
+ KEYC_NUSER
) {
358 snprintf(tmp
, sizeof tmp
, "User%u", (u_int
)(key
- KEYC_USER
));
359 strlcat(out
, tmp
, sizeof out
);
363 /* Try the key against the string table. */
364 for (i
= 0; i
< nitems(key_string_table
); i
++) {
365 if (key
== (key_string_table
[i
].key
& KEYC_MASK_KEY
))
368 if (i
!= nitems(key_string_table
)) {
369 strlcat(out
, key_string_table
[i
].string
, sizeof out
);
373 /* Is this a Unicode key? */
374 if (KEYC_IS_UNICODE(key
)) {
375 utf8_to_data(key
, &ud
);
377 memcpy(out
+ off
, ud
.data
, ud
.size
);
378 out
[off
+ ud
.size
] = '\0';
382 /* Invalid keys are errors. */
384 snprintf(out
, sizeof out
, "Invalid#%llx", saved
);
388 /* Check for standard or control key. */
390 if (key
== 0 || key
> 26)
391 xsnprintf(tmp
, sizeof tmp
, "C-%c", (int)(64 + key
));
393 xsnprintf(tmp
, sizeof tmp
, "C-%c", (int)(96 + key
));
394 } else if (key
>= 32 && key
<= 126) {
397 } else if (key
== 127)
398 xsnprintf(tmp
, sizeof tmp
, "C-?");
400 xsnprintf(tmp
, sizeof tmp
, "\\%llo", key
);
402 strlcat(out
, tmp
, sizeof out
);
406 strlcat(out
, s
, sizeof out
);
409 if (with_flags
&& (saved
& KEYC_MASK_FLAGS
) != 0) {
410 strlcat(out
, "[", sizeof out
);
411 if (saved
& KEYC_LITERAL
)
412 strlcat(out
, "L", sizeof out
);
413 if (saved
& KEYC_KEYPAD
)
414 strlcat(out
, "K", sizeof out
);
415 if (saved
& KEYC_CURSOR
)
416 strlcat(out
, "C", sizeof out
);
417 if (saved
& KEYC_IMPLIED_META
)
418 strlcat(out
, "I", sizeof out
);
419 if (saved
& KEYC_BUILD_MODIFIERS
)
420 strlcat(out
, "B", sizeof out
);
421 strlcat(out
, "]", sizeof out
);