]> git.proxmox.com Git - mirror_qemu.git/blame - ui/keymaps.c
ui/console: remove chardev frontend connected test
[mirror_qemu.git] / ui / keymaps.c
CommitLineData
3d11d0eb
FB
1/*
2 * QEMU keysym to keycode conversion using rdesktop keymaps
5fafdf24 3 *
3d11d0eb 4 * Copyright (c) 2004 Johannes Schindelin
5fafdf24 5 *
3d11d0eb
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
e16f4c87 25#include "qemu/osdep.h"
a8d25326 26#include "qemu-common.h"
2c65db5e 27#include "qemu/datadir.h"
0483755a 28#include "keymaps.h"
d3b787fa 29#include "trace.h"
856dfd8a 30#include "qemu/ctype.h"
8297be80 31#include "qemu/error-report.h"
ab4f931e 32#include "qapi/error.h"
19c1b9fd 33#include "ui/input.h"
0483755a 34
d713e3fd 35struct keysym2code {
23ad24e4
GH
36 uint32_t count;
37 uint16_t keycodes[4];
d713e3fd
GH
38};
39
fe5fca9a 40struct kbd_layout_t {
d713e3fd 41 GHashTable *hash;
fe5fca9a
GH
42};
43
c227f099 44static int get_keysym(const name2keysym_t *table,
43948386 45 const char *name)
3d11d0eb 46{
c227f099 47 const name2keysym_t *p;
0483755a 48 for(p = table; p->name != NULL; p++) {
43948386 49 if (!strcmp(p->name, name)) {
3d11d0eb 50 return p->keysym;
43948386 51 }
3d11d0eb 52 }
82807159
JK
53 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
54 char *end;
55 int ret = (int)strtoul(name + 1, &end, 16);
43948386
GA
56 if (*end == '\0' && ret > 0) {
57 return ret;
58 }
82807159 59 }
3d11d0eb
FB
60 return 0;
61}
62
3d11d0eb 63
d713e3fd
GH
64static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k)
65{
66 struct keysym2code *keysym2code;
67
68 keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym));
69 if (keysym2code) {
23ad24e4
GH
70 if (keysym2code->count < ARRAY_SIZE(keysym2code->keycodes)) {
71 keysym2code->keycodes[keysym2code->count++] = keycode;
72 } else {
73 warn_report("more than %zd keycodes for keysym %d",
74 ARRAY_SIZE(keysym2code->keycodes), keysym);
75 }
d713e3fd 76 return;
44bb61c8 77 }
d713e3fd
GH
78
79 keysym2code = g_new0(struct keysym2code, 1);
23ad24e4
GH
80 keysym2code->keycodes[0] = keycode;
81 keysym2code->count = 1;
d713e3fd
GH
82 g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code);
83 trace_keymap_add(keysym, keycode, line);
44bb61c8
ST
84}
85
f7b9e299
MA
86static int parse_keyboard_layout(kbd_layout_t *k,
87 const name2keysym_t *table,
ab4f931e 88 const char *language, Error **errp)
3d11d0eb 89{
f7b9e299 90 int ret;
3d11d0eb 91 FILE *f;
5cea8590 92 char * filename;
3d11d0eb 93 char line[1024];
d3b787fa 94 char keyname[64];
3d11d0eb
FB
95 int len;
96
5cea8590 97 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
d3b787fa 98 trace_keymap_parse(filename);
f2d3476e
MA
99 f = filename ? fopen(filename, "r") : NULL;
100 g_free(filename);
101 if (!f) {
ab4f931e 102 error_setg(errp, "could not read keymap file: '%s'", language);
f7b9e299 103 return -1;
43948386 104 }
f2d3476e 105
3d11d0eb 106 for(;;) {
43948386 107 if (fgets(line, 1024, f) == NULL) {
3d11d0eb 108 break;
43948386 109 }
3d11d0eb 110 len = strlen(line);
43948386 111 if (len > 0 && line[len - 1] == '\n') {
3d11d0eb 112 line[len - 1] = '\0';
43948386
GA
113 }
114 if (line[0] == '#') {
115 continue;
116 }
117 if (!strncmp(line, "map ", 4)) {
118 continue;
119 }
120 if (!strncmp(line, "include ", 8)) {
2a7bece6
GH
121 error_setg(errp, "keymap include files are not supported any more");
122 ret = -1;
123 goto out;
3d11d0eb 124 } else {
d3b787fa
GH
125 int offset = 0;
126 while (line[offset] != 0 &&
127 line[offset] != ' ' &&
128 offset < sizeof(keyname) - 1) {
129 keyname[offset] = line[offset];
130 offset++;
43948386 131 }
d3b787fa
GH
132 keyname[offset] = 0;
133 if (strlen(keyname)) {
43948386 134 int keysym;
d3b787fa 135 keysym = get_keysym(table, keyname);
43948386 136 if (keysym == 0) {
2ab4b135 137 /* warn_report("unknown keysym %s", line);*/
43948386 138 } else {
d3b787fa 139 const char *rest = line + offset + 1;
955d7b26 140 int keycode = strtol(rest, NULL, 0);
a528b80c 141
955d7b26 142 if (strstr(rest, "shift")) {
43948386 143 keycode |= SCANCODE_SHIFT;
955d7b26
MA
144 }
145 if (strstr(rest, "altgr")) {
43948386 146 keycode |= SCANCODE_ALTGR;
955d7b26
MA
147 }
148 if (strstr(rest, "ctrl")) {
43948386 149 keycode |= SCANCODE_CTRL;
955d7b26 150 }
44bb61c8 151
43948386 152 add_keysym(line, keysym, keycode, k);
44bb61c8 153
955d7b26 154 if (strstr(rest, "addupper")) {
43948386 155 char *c;
d3b787fa 156 for (c = keyname; *c; c++) {
43948386
GA
157 *c = qemu_toupper(*c);
158 }
d3b787fa 159 keysym = get_keysym(table, keyname);
43948386
GA
160 if (keysym) {
161 add_keysym(line, keysym,
162 keycode | SCANCODE_SHIFT, k);
163 }
164 }
165 }
166 }
167 }
3d11d0eb 168 }
f7b9e299
MA
169
170 ret = 0;
171out:
3d11d0eb 172 fclose(f);
f7b9e299 173 return ret;
3d11d0eb
FB
174}
175
0483755a 176
fe5fca9a 177kbd_layout_t *init_keyboard_layout(const name2keysym_t *table,
ab4f931e 178 const char *language, Error **errp)
3d11d0eb 179{
f7b9e299
MA
180 kbd_layout_t *k;
181
182 k = g_new0(kbd_layout_t, 1);
183 k->hash = g_hash_table_new(NULL, NULL);
ab4f931e 184 if (parse_keyboard_layout(k, table, language, errp) < 0) {
f7b9e299
MA
185 g_hash_table_unref(k->hash);
186 g_free(k);
187 return NULL;
188 }
189 return k;
3d11d0eb
FB
190}
191
0483755a 192
abb4f2c9 193int keysym2scancode(kbd_layout_t *k, int keysym,
19c1b9fd 194 QKbdState *kbd, bool down)
3d11d0eb 195{
abb4f2c9
GH
196 static const uint32_t mask =
197 SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL;
198 uint32_t mods, i;
d713e3fd
GH
199 struct keysym2code *keysym2code;
200
3d11d0eb 201#ifdef XK_ISO_Left_Tab
d713e3fd
GH
202 if (keysym == XK_ISO_Left_Tab) {
203 keysym = XK_Tab;
204 }
3d11d0eb 205#endif
d713e3fd
GH
206
207 keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym));
208 if (!keysym2code) {
209 trace_keymap_unmapped(keysym);
210 warn_report("no scancode found for keysym %d", keysym);
211 return 0;
3d11d0eb 212 }
d713e3fd 213
abb4f2c9
GH
214 if (keysym2code->count == 1) {
215 return keysym2code->keycodes[0];
216 }
217
19c1b9fd
GH
218 /* We have multiple keysym -> keycode mappings. */
219 if (down) {
220 /*
221 * On keydown: Check whenever we find one mapping where the
222 * modifier state of the mapping matches the current user
223 * interface modifier state. If so, prefer that one.
224 */
225 mods = 0;
226 if (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_SHIFT)) {
227 mods |= SCANCODE_SHIFT;
228 }
229 if (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_ALTGR)) {
230 mods |= SCANCODE_ALTGR;
231 }
232 if (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_CTRL)) {
233 mods |= SCANCODE_CTRL;
234 }
abb4f2c9 235
19c1b9fd
GH
236 for (i = 0; i < keysym2code->count; i++) {
237 if ((keysym2code->keycodes[i] & mask) == mods) {
238 return keysym2code->keycodes[i];
239 }
240 }
241 } else {
242 /*
243 * On keyup: Try find a key which is actually down.
244 */
245 for (i = 0; i < keysym2code->count; i++) {
246 QKeyCode qcode = qemu_input_key_number_to_qcode
247 (keysym2code->keycodes[i]);
248 if (kbd && qkbd_state_key_get(kbd, qcode)) {
249 return keysym2code->keycodes[i];
250 }
abb4f2c9
GH
251 }
252 }
23ad24e4 253 return keysym2code->keycodes[0];
3d11d0eb 254}
a528b80c 255
fe5fca9a 256int keycode_is_keypad(kbd_layout_t *k, int keycode)
a528b80c 257{
6b71ea11
GH
258 if (keycode >= 0x47 && keycode <= 0x53) {
259 return true;
43948386 260 }
6b71ea11 261 return false;
a528b80c
AZ
262}
263
fe5fca9a 264int keysym_is_numlock(kbd_layout_t *k, int keysym)
a528b80c 265{
6b71ea11
GH
266 switch (keysym) {
267 case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */
268 case 0xffac: /* KP_Separator */
269 case 0xffae: /* KP_Decimal */
270 return true;
43948386 271 }
6b71ea11 272 return false;
a528b80c 273}