]> git.proxmox.com Git - mirror_qemu.git/blame - ui/keymaps.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-block-2017-03-28' 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"
0483755a 26#include "keymaps.h"
9c17d615 27#include "sysemu/sysemu.h"
0483755a 28
c227f099 29static int get_keysym(const name2keysym_t *table,
43948386 30 const char *name)
3d11d0eb 31{
c227f099 32 const name2keysym_t *p;
0483755a 33 for(p = table; p->name != NULL; p++) {
43948386 34 if (!strcmp(p->name, name)) {
3d11d0eb 35 return p->keysym;
43948386 36 }
3d11d0eb 37 }
82807159
JK
38 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
39 char *end;
40 int ret = (int)strtoul(name + 1, &end, 16);
43948386
GA
41 if (*end == '\0' && ret > 0) {
42 return ret;
43 }
82807159 44 }
3d11d0eb
FB
45 return 0;
46}
47
3d11d0eb 48
a528b80c
AZ
49static void add_to_key_range(struct key_range **krp, int code) {
50 struct key_range *kr;
51 for (kr = *krp; kr; kr = kr->next) {
43948386
GA
52 if (code >= kr->start && code <= kr->end) {
53 break;
54 }
55 if (code == kr->start - 1) {
56 kr->start--;
57 break;
58 }
59 if (code == kr->end + 1) {
60 kr->end++;
61 break;
62 }
a528b80c
AZ
63 }
64 if (kr == NULL) {
43948386 65 kr = g_malloc0(sizeof(*kr));
1eec614b
AL
66 kr->start = kr->end = code;
67 kr->next = *krp;
68 *krp = kr;
a528b80c
AZ
69 }
70}
71
44bb61c8
ST
72static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
73 if (keysym < MAX_NORMAL_KEYCODE) {
43948386
GA
74 /* fprintf(stderr,"Setting keysym %s (%d) to %d\n",
75 line, keysym, keycode); */
76 k->keysym2keycode[keysym] = keycode;
44bb61c8 77 } else {
43948386
GA
78 if (k->extra_count >= MAX_EXTRA_COUNT) {
79 fprintf(stderr, "Warning: Could not assign keysym %s (0x%x)"
80 " because of memory constraints.\n", line, keysym);
81 } else {
44bb61c8 82#if 0
43948386
GA
83 fprintf(stderr, "Setting %d: %d,%d\n",
84 k->extra_count, keysym, keycode);
44bb61c8 85#endif
43948386
GA
86 k->keysym2keycode_extra[k->extra_count].
87 keysym = keysym;
88 k->keysym2keycode_extra[k->extra_count].
89 keycode = keycode;
90 k->extra_count++;
91 }
44bb61c8
ST
92 }
93}
94
c227f099 95static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
43948386
GA
96 const char *language,
97 kbd_layout_t *k)
3d11d0eb
FB
98{
99 FILE *f;
5cea8590 100 char * filename;
3d11d0eb
FB
101 char line[1024];
102 int len;
103
5cea8590 104 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
f2d3476e
MA
105 f = filename ? fopen(filename, "r") : NULL;
106 g_free(filename);
107 if (!f) {
43948386
GA
108 fprintf(stderr, "Could not read keymap file: '%s'\n", language);
109 return NULL;
3d11d0eb 110 }
f2d3476e 111
43948386 112 if (!k) {
fedf0d35 113 k = g_new0(kbd_layout_t, 1);
43948386 114 }
f2d3476e 115
3d11d0eb 116 for(;;) {
43948386 117 if (fgets(line, 1024, f) == NULL) {
3d11d0eb 118 break;
43948386 119 }
3d11d0eb 120 len = strlen(line);
43948386 121 if (len > 0 && line[len - 1] == '\n') {
3d11d0eb 122 line[len - 1] = '\0';
43948386
GA
123 }
124 if (line[0] == '#') {
125 continue;
126 }
127 if (!strncmp(line, "map ", 4)) {
128 continue;
129 }
130 if (!strncmp(line, "include ", 8)) {
131 parse_keyboard_layout(table, line + 8, k);
3d11d0eb 132 } else {
43948386
GA
133 char *end_of_keysym = line;
134 while (*end_of_keysym != 0 && *end_of_keysym != ' ') {
135 end_of_keysym++;
136 }
137 if (*end_of_keysym) {
138 int keysym;
139 *end_of_keysym = 0;
140 keysym = get_keysym(table, line);
141 if (keysym == 0) {
142 /* fprintf(stderr, "Warning: unknown keysym %s\n", line);*/
143 } else {
144 const char *rest = end_of_keysym + 1;
955d7b26 145 int keycode = strtol(rest, NULL, 0);
a528b80c 146
955d7b26 147 if (strstr(rest, "numlock")) {
43948386
GA
148 add_to_key_range(&k->keypad_range, keycode);
149 add_to_key_range(&k->numlock_range, keysym);
150 /* fprintf(stderr, "keypad keysym %04x keycode %d\n",
151 keysym, keycode); */
152 }
a528b80c 153
955d7b26 154 if (strstr(rest, "shift")) {
43948386 155 keycode |= SCANCODE_SHIFT;
955d7b26
MA
156 }
157 if (strstr(rest, "altgr")) {
43948386 158 keycode |= SCANCODE_ALTGR;
955d7b26
MA
159 }
160 if (strstr(rest, "ctrl")) {
43948386 161 keycode |= SCANCODE_CTRL;
955d7b26 162 }
44bb61c8 163
43948386 164 add_keysym(line, keysym, keycode, k);
44bb61c8 165
955d7b26 166 if (strstr(rest, "addupper")) {
43948386
GA
167 char *c;
168 for (c = line; *c; c++) {
169 *c = qemu_toupper(*c);
170 }
171 keysym = get_keysym(table, line);
172 if (keysym) {
173 add_keysym(line, keysym,
174 keycode | SCANCODE_SHIFT, k);
175 }
176 }
177 }
178 }
179 }
3d11d0eb
FB
180 }
181 fclose(f);
182 return k;
183}
184
0483755a 185
c227f099 186void *init_keyboard_layout(const name2keysym_t *table, const char *language)
3d11d0eb 187{
660f11be 188 return parse_keyboard_layout(table, language, NULL);
3d11d0eb
FB
189}
190
0483755a
AL
191
192int keysym2scancode(void *kbd_layout, int keysym)
3d11d0eb 193{
c227f099 194 kbd_layout_t *k = kbd_layout;
3d11d0eb 195 if (keysym < MAX_NORMAL_KEYCODE) {
43948386
GA
196 if (k->keysym2keycode[keysym] == 0) {
197 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
198 keysym);
199 }
200 return k->keysym2keycode[keysym];
3d11d0eb 201 } else {
43948386 202 int i;
3d11d0eb 203#ifdef XK_ISO_Left_Tab
43948386
GA
204 if (keysym == XK_ISO_Left_Tab) {
205 keysym = XK_Tab;
206 }
3d11d0eb 207#endif
43948386
GA
208 for (i = 0; i < k->extra_count; i++) {
209 if (k->keysym2keycode_extra[i].keysym == keysym) {
210 return k->keysym2keycode_extra[i].keycode;
211 }
212 }
3d11d0eb
FB
213 }
214 return 0;
215}
a528b80c 216
0483755a 217int keycode_is_keypad(void *kbd_layout, int keycode)
a528b80c 218{
c227f099 219 kbd_layout_t *k = kbd_layout;
a528b80c
AZ
220 struct key_range *kr;
221
43948386
GA
222 for (kr = k->keypad_range; kr; kr = kr->next) {
223 if (keycode >= kr->start && keycode <= kr->end) {
a528b80c 224 return 1;
43948386
GA
225 }
226 }
a528b80c
AZ
227 return 0;
228}
229
0483755a 230int keysym_is_numlock(void *kbd_layout, int keysym)
a528b80c 231{
c227f099 232 kbd_layout_t *k = kbd_layout;
a528b80c
AZ
233 struct key_range *kr;
234
43948386
GA
235 for (kr = k->numlock_range; kr; kr = kr->next) {
236 if (keysym >= kr->start && keysym <= kr->end) {
a528b80c 237 return 1;
43948386
GA
238 }
239 }
a528b80c
AZ
240 return 0;
241}