]>
Commit | Line | Data |
---|---|---|
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 GH |
31 | struct keysym2code { |
32 | uint16_t keycode; | |
33 | }; | |
34 | ||
fe5fca9a | 35 | struct kbd_layout_t { |
d713e3fd | 36 | GHashTable *hash; |
fe5fca9a GH |
37 | }; |
38 | ||
c227f099 | 39 | static int get_keysym(const name2keysym_t *table, |
43948386 | 40 | const char *name) |
3d11d0eb | 41 | { |
c227f099 | 42 | const name2keysym_t *p; |
0483755a | 43 | for(p = table; p->name != NULL; p++) { |
43948386 | 44 | if (!strcmp(p->name, name)) { |
3d11d0eb | 45 | return p->keysym; |
43948386 | 46 | } |
3d11d0eb | 47 | } |
82807159 JK |
48 | if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */ |
49 | char *end; | |
50 | int ret = (int)strtoul(name + 1, &end, 16); | |
43948386 GA |
51 | if (*end == '\0' && ret > 0) { |
52 | return ret; | |
53 | } | |
82807159 | 54 | } |
3d11d0eb FB |
55 | return 0; |
56 | } | |
57 | ||
3d11d0eb | 58 | |
d713e3fd GH |
59 | static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) |
60 | { | |
61 | struct keysym2code *keysym2code; | |
62 | ||
63 | keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); | |
64 | if (keysym2code) { | |
65 | return; | |
44bb61c8 | 66 | } |
d713e3fd GH |
67 | |
68 | keysym2code = g_new0(struct keysym2code, 1); | |
69 | keysym2code->keycode = keycode; | |
70 | g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code); | |
71 | trace_keymap_add(keysym, keycode, line); | |
44bb61c8 ST |
72 | } |
73 | ||
c227f099 | 74 | static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table, |
43948386 GA |
75 | const char *language, |
76 | kbd_layout_t *k) | |
3d11d0eb FB |
77 | { |
78 | FILE *f; | |
5cea8590 | 79 | char * filename; |
3d11d0eb | 80 | char line[1024]; |
d3b787fa | 81 | char keyname[64]; |
3d11d0eb FB |
82 | int len; |
83 | ||
5cea8590 | 84 | filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language); |
d3b787fa | 85 | trace_keymap_parse(filename); |
f2d3476e MA |
86 | f = filename ? fopen(filename, "r") : NULL; |
87 | g_free(filename); | |
88 | if (!f) { | |
43948386 GA |
89 | fprintf(stderr, "Could not read keymap file: '%s'\n", language); |
90 | return NULL; | |
3d11d0eb | 91 | } |
f2d3476e | 92 | |
43948386 | 93 | if (!k) { |
fedf0d35 | 94 | k = g_new0(kbd_layout_t, 1); |
d713e3fd | 95 | k->hash = g_hash_table_new(NULL, NULL); |
43948386 | 96 | } |
f2d3476e | 97 | |
3d11d0eb | 98 | for(;;) { |
43948386 | 99 | if (fgets(line, 1024, f) == NULL) { |
3d11d0eb | 100 | break; |
43948386 | 101 | } |
3d11d0eb | 102 | len = strlen(line); |
43948386 | 103 | if (len > 0 && line[len - 1] == '\n') { |
3d11d0eb | 104 | line[len - 1] = '\0'; |
43948386 GA |
105 | } |
106 | if (line[0] == '#') { | |
107 | continue; | |
108 | } | |
109 | if (!strncmp(line, "map ", 4)) { | |
110 | continue; | |
111 | } | |
112 | if (!strncmp(line, "include ", 8)) { | |
113 | parse_keyboard_layout(table, line + 8, k); | |
3d11d0eb | 114 | } else { |
d3b787fa GH |
115 | int offset = 0; |
116 | while (line[offset] != 0 && | |
117 | line[offset] != ' ' && | |
118 | offset < sizeof(keyname) - 1) { | |
119 | keyname[offset] = line[offset]; | |
120 | offset++; | |
43948386 | 121 | } |
d3b787fa GH |
122 | keyname[offset] = 0; |
123 | if (strlen(keyname)) { | |
43948386 | 124 | int keysym; |
d3b787fa | 125 | keysym = get_keysym(table, keyname); |
43948386 | 126 | if (keysym == 0) { |
2ab4b135 | 127 | /* warn_report("unknown keysym %s", line);*/ |
43948386 | 128 | } else { |
d3b787fa | 129 | const char *rest = line + offset + 1; |
955d7b26 | 130 | int keycode = strtol(rest, NULL, 0); |
a528b80c | 131 | |
955d7b26 | 132 | if (strstr(rest, "shift")) { |
43948386 | 133 | keycode |= SCANCODE_SHIFT; |
955d7b26 MA |
134 | } |
135 | if (strstr(rest, "altgr")) { | |
43948386 | 136 | keycode |= SCANCODE_ALTGR; |
955d7b26 MA |
137 | } |
138 | if (strstr(rest, "ctrl")) { | |
43948386 | 139 | keycode |= SCANCODE_CTRL; |
955d7b26 | 140 | } |
44bb61c8 | 141 | |
43948386 | 142 | add_keysym(line, keysym, keycode, k); |
44bb61c8 | 143 | |
955d7b26 | 144 | if (strstr(rest, "addupper")) { |
43948386 | 145 | char *c; |
d3b787fa | 146 | for (c = keyname; *c; c++) { |
43948386 GA |
147 | *c = qemu_toupper(*c); |
148 | } | |
d3b787fa | 149 | keysym = get_keysym(table, keyname); |
43948386 GA |
150 | if (keysym) { |
151 | add_keysym(line, keysym, | |
152 | keycode | SCANCODE_SHIFT, k); | |
153 | } | |
154 | } | |
155 | } | |
156 | } | |
157 | } | |
3d11d0eb FB |
158 | } |
159 | fclose(f); | |
160 | return k; | |
161 | } | |
162 | ||
0483755a | 163 | |
fe5fca9a GH |
164 | kbd_layout_t *init_keyboard_layout(const name2keysym_t *table, |
165 | const char *language) | |
3d11d0eb | 166 | { |
660f11be | 167 | return parse_keyboard_layout(table, language, NULL); |
3d11d0eb FB |
168 | } |
169 | ||
0483755a | 170 | |
fe5fca9a | 171 | int keysym2scancode(kbd_layout_t *k, int keysym) |
3d11d0eb | 172 | { |
d713e3fd GH |
173 | struct keysym2code *keysym2code; |
174 | ||
3d11d0eb | 175 | #ifdef XK_ISO_Left_Tab |
d713e3fd GH |
176 | if (keysym == XK_ISO_Left_Tab) { |
177 | keysym = XK_Tab; | |
178 | } | |
3d11d0eb | 179 | #endif |
d713e3fd GH |
180 | |
181 | keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); | |
182 | if (!keysym2code) { | |
183 | trace_keymap_unmapped(keysym); | |
184 | warn_report("no scancode found for keysym %d", keysym); | |
185 | return 0; | |
3d11d0eb | 186 | } |
d713e3fd GH |
187 | |
188 | return keysym2code->keycode; | |
3d11d0eb | 189 | } |
a528b80c | 190 | |
fe5fca9a | 191 | int keycode_is_keypad(kbd_layout_t *k, int keycode) |
a528b80c | 192 | { |
6b71ea11 GH |
193 | if (keycode >= 0x47 && keycode <= 0x53) { |
194 | return true; | |
43948386 | 195 | } |
6b71ea11 | 196 | return false; |
a528b80c AZ |
197 | } |
198 | ||
fe5fca9a | 199 | int keysym_is_numlock(kbd_layout_t *k, int keysym) |
a528b80c | 200 | { |
6b71ea11 GH |
201 | switch (keysym) { |
202 | case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */ | |
203 | case 0xffac: /* KP_Separator */ | |
204 | case 0xffae: /* KP_Decimal */ | |
205 | return true; | |
43948386 | 206 | } |
6b71ea11 | 207 | return false; |
a528b80c | 208 | } |