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