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