2 * QEMU keysym to keycode conversion using rdesktop keymaps
4 * Copyright (c) 2004 Johannes Schindelin
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "sysemu/sysemu.h"
29 #include "qemu/error-report.h"
30 #include "qapi/error.h"
42 static int get_keysym(const name2keysym_t
*table
,
45 const name2keysym_t
*p
;
46 for(p
= table
; p
->name
!= NULL
; p
++) {
47 if (!strcmp(p
->name
, name
)) {
51 if (name
[0] == 'U' && strlen(name
) == 5) { /* try unicode Uxxxx */
53 int ret
= (int)strtoul(name
+ 1, &end
, 16);
54 if (*end
== '\0' && ret
> 0) {
62 static void add_keysym(char *line
, int keysym
, int keycode
, kbd_layout_t
*k
)
64 struct keysym2code
*keysym2code
;
66 keysym2code
= g_hash_table_lookup(k
->hash
, GINT_TO_POINTER(keysym
));
68 if (keysym2code
->count
< ARRAY_SIZE(keysym2code
->keycodes
)) {
69 keysym2code
->keycodes
[keysym2code
->count
++] = keycode
;
71 warn_report("more than %zd keycodes for keysym %d",
72 ARRAY_SIZE(keysym2code
->keycodes
), keysym
);
77 keysym2code
= g_new0(struct keysym2code
, 1);
78 keysym2code
->keycodes
[0] = keycode
;
79 keysym2code
->count
= 1;
80 g_hash_table_replace(k
->hash
, GINT_TO_POINTER(keysym
), keysym2code
);
81 trace_keymap_add(keysym
, keycode
, line
);
84 static int parse_keyboard_layout(kbd_layout_t
*k
,
85 const name2keysym_t
*table
,
86 const char *language
, Error
**errp
)
95 filename
= qemu_find_file(QEMU_FILE_TYPE_KEYMAP
, language
);
96 trace_keymap_parse(filename
);
97 f
= filename
? fopen(filename
, "r") : NULL
;
100 error_setg(errp
, "could not read keymap file: '%s'", language
);
105 if (fgets(line
, 1024, f
) == NULL
) {
109 if (len
> 0 && line
[len
- 1] == '\n') {
110 line
[len
- 1] = '\0';
112 if (line
[0] == '#') {
115 if (!strncmp(line
, "map ", 4)) {
118 if (!strncmp(line
, "include ", 8)) {
119 error_setg(errp
, "keymap include files are not supported any more");
124 while (line
[offset
] != 0 &&
125 line
[offset
] != ' ' &&
126 offset
< sizeof(keyname
) - 1) {
127 keyname
[offset
] = line
[offset
];
131 if (strlen(keyname
)) {
133 keysym
= get_keysym(table
, keyname
);
135 /* warn_report("unknown keysym %s", line);*/
137 const char *rest
= line
+ offset
+ 1;
138 int keycode
= strtol(rest
, NULL
, 0);
140 if (strstr(rest
, "shift")) {
141 keycode
|= SCANCODE_SHIFT
;
143 if (strstr(rest
, "altgr")) {
144 keycode
|= SCANCODE_ALTGR
;
146 if (strstr(rest
, "ctrl")) {
147 keycode
|= SCANCODE_CTRL
;
150 add_keysym(line
, keysym
, keycode
, k
);
152 if (strstr(rest
, "addupper")) {
154 for (c
= keyname
; *c
; c
++) {
155 *c
= qemu_toupper(*c
);
157 keysym
= get_keysym(table
, keyname
);
159 add_keysym(line
, keysym
,
160 keycode
| SCANCODE_SHIFT
, k
);
175 kbd_layout_t
*init_keyboard_layout(const name2keysym_t
*table
,
176 const char *language
, Error
**errp
)
180 k
= g_new0(kbd_layout_t
, 1);
181 k
->hash
= g_hash_table_new(NULL
, NULL
);
182 if (parse_keyboard_layout(k
, table
, language
, errp
) < 0) {
183 g_hash_table_unref(k
->hash
);
191 int keysym2scancode(kbd_layout_t
*k
, int keysym
,
192 QKbdState
*kbd
, bool down
)
194 static const uint32_t mask
=
195 SCANCODE_SHIFT
| SCANCODE_ALTGR
| SCANCODE_CTRL
;
197 struct keysym2code
*keysym2code
;
199 #ifdef XK_ISO_Left_Tab
200 if (keysym
== XK_ISO_Left_Tab
) {
205 keysym2code
= g_hash_table_lookup(k
->hash
, GINT_TO_POINTER(keysym
));
207 trace_keymap_unmapped(keysym
);
208 warn_report("no scancode found for keysym %d", keysym
);
212 if (keysym2code
->count
== 1) {
213 return keysym2code
->keycodes
[0];
216 /* We have multiple keysym -> keycode mappings. */
219 * On keydown: Check whenever we find one mapping where the
220 * modifier state of the mapping matches the current user
221 * interface modifier state. If so, prefer that one.
224 if (kbd
&& qkbd_state_modifier_get(kbd
, QKBD_MOD_SHIFT
)) {
225 mods
|= SCANCODE_SHIFT
;
227 if (kbd
&& qkbd_state_modifier_get(kbd
, QKBD_MOD_ALTGR
)) {
228 mods
|= SCANCODE_ALTGR
;
230 if (kbd
&& qkbd_state_modifier_get(kbd
, QKBD_MOD_CTRL
)) {
231 mods
|= SCANCODE_CTRL
;
234 for (i
= 0; i
< keysym2code
->count
; i
++) {
235 if ((keysym2code
->keycodes
[i
] & mask
) == mods
) {
236 return keysym2code
->keycodes
[i
];
241 * On keyup: Try find a key which is actually down.
243 for (i
= 0; i
< keysym2code
->count
; i
++) {
244 QKeyCode qcode
= qemu_input_key_number_to_qcode
245 (keysym2code
->keycodes
[i
]);
246 if (kbd
&& qkbd_state_key_get(kbd
, qcode
)) {
247 return keysym2code
->keycodes
[i
];
251 return keysym2code
->keycodes
[0];
254 int keycode_is_keypad(kbd_layout_t
*k
, int keycode
)
256 if (keycode
>= 0x47 && keycode
<= 0x53) {
262 int keysym_is_numlock(kbd_layout_t
*k
, int keysym
)
265 case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */
266 case 0xffac: /* KP_Separator */
267 case 0xffae: /* KP_Decimal */