]> Git Repo - qemu.git/blame - ui/keymaps.c
migration: assert colo instead of check
[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
c227f099 31static int get_keysym(const name2keysym_t *table,
43948386 32 const char *name)
3d11d0eb 33{
c227f099 34 const name2keysym_t *p;
0483755a 35 for(p = table; p->name != NULL; p++) {
43948386 36 if (!strcmp(p->name, name)) {
3d11d0eb 37 return p->keysym;
43948386 38 }
3d11d0eb 39 }
82807159
JK
40 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
41 char *end;
42 int ret = (int)strtoul(name + 1, &end, 16);
43948386
GA
43 if (*end == '\0' && ret > 0) {
44 return ret;
45 }
82807159 46 }
3d11d0eb
FB
47 return 0;
48}
49
3d11d0eb 50
a528b80c
AZ
51static void add_to_key_range(struct key_range **krp, int code) {
52 struct key_range *kr;
53 for (kr = *krp; kr; kr = kr->next) {
43948386
GA
54 if (code >= kr->start && code <= kr->end) {
55 break;
56 }
57 if (code == kr->start - 1) {
58 kr->start--;
59 break;
60 }
61 if (code == kr->end + 1) {
62 kr->end++;
63 break;
64 }
a528b80c
AZ
65 }
66 if (kr == NULL) {
43948386 67 kr = g_malloc0(sizeof(*kr));
1eec614b
AL
68 kr->start = kr->end = code;
69 kr->next = *krp;
70 *krp = kr;
a528b80c
AZ
71 }
72}
73
44bb61c8
ST
74static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
75 if (keysym < MAX_NORMAL_KEYCODE) {
d3b787fa 76 trace_keymap_add("normal", keysym, keycode, line);
43948386 77 k->keysym2keycode[keysym] = keycode;
44bb61c8 78 } else {
43948386 79 if (k->extra_count >= MAX_EXTRA_COUNT) {
8297be80
AF
80 warn_report("Could not assign keysym %s (0x%x)"
81 " because of memory constraints.", line, keysym);
43948386 82 } else {
d3b787fa 83 trace_keymap_add("extra", keysym, keycode, line);
43948386
GA
84 k->keysym2keycode_extra[k->extra_count].
85 keysym = keysym;
86 k->keysym2keycode_extra[k->extra_count].
87 keycode = keycode;
88 k->extra_count++;
89 }
44bb61c8
ST
90 }
91}
92
c227f099 93static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
43948386
GA
94 const char *language,
95 kbd_layout_t *k)
3d11d0eb
FB
96{
97 FILE *f;
5cea8590 98 char * filename;
3d11d0eb 99 char line[1024];
d3b787fa 100 char keyname[64];
3d11d0eb
FB
101 int len;
102
5cea8590 103 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
d3b787fa 104 trace_keymap_parse(filename);
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 {
d3b787fa
GH
133 int offset = 0;
134 while (line[offset] != 0 &&
135 line[offset] != ' ' &&
136 offset < sizeof(keyname) - 1) {
137 keyname[offset] = line[offset];
138 offset++;
43948386 139 }
d3b787fa
GH
140 keyname[offset] = 0;
141 if (strlen(keyname)) {
43948386 142 int keysym;
d3b787fa 143 keysym = get_keysym(table, keyname);
43948386 144 if (keysym == 0) {
2ab4b135 145 /* warn_report("unknown keysym %s", line);*/
43948386 146 } else {
d3b787fa 147 const char *rest = line + offset + 1;
955d7b26 148 int keycode = strtol(rest, NULL, 0);
a528b80c 149
955d7b26 150 if (strstr(rest, "numlock")) {
43948386
GA
151 add_to_key_range(&k->keypad_range, keycode);
152 add_to_key_range(&k->numlock_range, keysym);
153 /* fprintf(stderr, "keypad keysym %04x keycode %d\n",
154 keysym, keycode); */
155 }
a528b80c 156
955d7b26 157 if (strstr(rest, "shift")) {
43948386 158 keycode |= SCANCODE_SHIFT;
955d7b26
MA
159 }
160 if (strstr(rest, "altgr")) {
43948386 161 keycode |= SCANCODE_ALTGR;
955d7b26
MA
162 }
163 if (strstr(rest, "ctrl")) {
43948386 164 keycode |= SCANCODE_CTRL;
955d7b26 165 }
44bb61c8 166
43948386 167 add_keysym(line, keysym, keycode, k);
44bb61c8 168
955d7b26 169 if (strstr(rest, "addupper")) {
43948386 170 char *c;
d3b787fa 171 for (c = keyname; *c; c++) {
43948386
GA
172 *c = qemu_toupper(*c);
173 }
d3b787fa 174 keysym = get_keysym(table, keyname);
43948386
GA
175 if (keysym) {
176 add_keysym(line, keysym,
177 keycode | SCANCODE_SHIFT, k);
178 }
179 }
180 }
181 }
182 }
3d11d0eb
FB
183 }
184 fclose(f);
185 return k;
186}
187
0483755a 188
c227f099 189void *init_keyboard_layout(const name2keysym_t *table, const char *language)
3d11d0eb 190{
660f11be 191 return parse_keyboard_layout(table, language, NULL);
3d11d0eb
FB
192}
193
0483755a
AL
194
195int keysym2scancode(void *kbd_layout, int keysym)
3d11d0eb 196{
c227f099 197 kbd_layout_t *k = kbd_layout;
3d11d0eb 198 if (keysym < MAX_NORMAL_KEYCODE) {
43948386 199 if (k->keysym2keycode[keysym] == 0) {
d3b787fa 200 trace_keymap_unmapped(keysym);
b62e39b4 201 warn_report("no scancode found for keysym %d", keysym);
43948386
GA
202 }
203 return k->keysym2keycode[keysym];
3d11d0eb 204 } else {
43948386 205 int i;
3d11d0eb 206#ifdef XK_ISO_Left_Tab
43948386
GA
207 if (keysym == XK_ISO_Left_Tab) {
208 keysym = XK_Tab;
209 }
3d11d0eb 210#endif
43948386
GA
211 for (i = 0; i < k->extra_count; i++) {
212 if (k->keysym2keycode_extra[i].keysym == keysym) {
213 return k->keysym2keycode_extra[i].keycode;
214 }
215 }
3d11d0eb
FB
216 }
217 return 0;
218}
a528b80c 219
0483755a 220int keycode_is_keypad(void *kbd_layout, int keycode)
a528b80c 221{
c227f099 222 kbd_layout_t *k = kbd_layout;
a528b80c
AZ
223 struct key_range *kr;
224
43948386
GA
225 for (kr = k->keypad_range; kr; kr = kr->next) {
226 if (keycode >= kr->start && keycode <= kr->end) {
a528b80c 227 return 1;
43948386
GA
228 }
229 }
a528b80c
AZ
230 return 0;
231}
232
0483755a 233int keysym_is_numlock(void *kbd_layout, int keysym)
a528b80c 234{
c227f099 235 kbd_layout_t *k = kbd_layout;
a528b80c
AZ
236 struct key_range *kr;
237
43948386
GA
238 for (kr = k->numlock_range; kr; kr = kr->next) {
239 if (keysym >= kr->start && keysym <= kr->end) {
a528b80c 240 return 1;
43948386
GA
241 }
242 }
a528b80c
AZ
243 return 0;
244}
This page took 0.890979 seconds and 4 git commands to generate.