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