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