blob: 49410ae9d15b53ac99c3ed8ccd8558f1afcf5c96 [file] [log] [blame]
bellard3d11d0e2004-12-12 16:56:30 +00001/*
2 * QEMU keysym to keycode conversion using rdesktop keymaps
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard3d11d0e2004-12-12 16:56:30 +00004 * Copyright (c) 2004 Johannes Schindelin
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard3d11d0e2004-12-12 16:56:30 +00006 * 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
aliguori04837552009-03-06 20:27:10 +000025#include "keymaps.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
aliguori04837552009-03-06 20:27:10 +000027
Anthony Liguoric227f092009-10-01 16:12:16 -050028static int get_keysym(const name2keysym_t *table,
Gonglei43948382014-12-08 19:39:05 +080029 const char *name)
bellard3d11d0e2004-12-12 16:56:30 +000030{
Anthony Liguoric227f092009-10-01 16:12:16 -050031 const name2keysym_t *p;
aliguori04837552009-03-06 20:27:10 +000032 for(p = table; p->name != NULL; p++) {
Gonglei43948382014-12-08 19:39:05 +080033 if (!strcmp(p->name, name)) {
bellard3d11d0e2004-12-12 16:56:30 +000034 return p->keysym;
Gonglei43948382014-12-08 19:39:05 +080035 }
bellard3d11d0e2004-12-12 16:56:30 +000036 }
Jan Krupa82807152013-10-16 14:40:05 +020037 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
38 char *end;
39 int ret = (int)strtoul(name + 1, &end, 16);
Gonglei43948382014-12-08 19:39:05 +080040 if (*end == '\0' && ret > 0) {
41 return ret;
42 }
Jan Krupa82807152013-10-16 14:40:05 +020043 }
bellard3d11d0e2004-12-12 16:56:30 +000044 return 0;
45}
46
bellard3d11d0e2004-12-12 16:56:30 +000047
balroga528b802007-10-30 22:38:53 +000048static void add_to_key_range(struct key_range **krp, int code) {
49 struct key_range *kr;
50 for (kr = *krp; kr; kr = kr->next) {
Gonglei43948382014-12-08 19:39:05 +080051 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 }
balroga528b802007-10-30 22:38:53 +000062 }
63 if (kr == NULL) {
Gonglei43948382014-12-08 19:39:05 +080064 kr = g_malloc0(sizeof(*kr));
aliguori1eec6142009-02-05 22:06:18 +000065 kr->start = kr->end = code;
66 kr->next = *krp;
67 *krp = kr;
balroga528b802007-10-30 22:38:53 +000068 }
69}
70
Samuel Thibault44bb61c2010-02-28 21:03:00 +010071static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
72 if (keysym < MAX_NORMAL_KEYCODE) {
Gonglei43948382014-12-08 19:39:05 +080073 /* fprintf(stderr,"Setting keysym %s (%d) to %d\n",
74 line, keysym, keycode); */
75 k->keysym2keycode[keysym] = keycode;
Samuel Thibault44bb61c2010-02-28 21:03:00 +010076 } else {
Gonglei43948382014-12-08 19:39:05 +080077 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 {
Samuel Thibault44bb61c2010-02-28 21:03:00 +010081#if 0
Gonglei43948382014-12-08 19:39:05 +080082 fprintf(stderr, "Setting %d: %d,%d\n",
83 k->extra_count, keysym, keycode);
Samuel Thibault44bb61c2010-02-28 21:03:00 +010084#endif
Gonglei43948382014-12-08 19:39:05 +080085 k->keysym2keycode_extra[k->extra_count].
86 keysym = keysym;
87 k->keysym2keycode_extra[k->extra_count].
88 keycode = keycode;
89 k->extra_count++;
90 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +010091 }
92}
93
Anthony Liguoric227f092009-10-01 16:12:16 -050094static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
Gonglei43948382014-12-08 19:39:05 +080095 const char *language,
96 kbd_layout_t *k)
bellard3d11d0e2004-12-12 16:56:30 +000097{
98 FILE *f;
Paul Brook5cea8592009-05-30 00:52:44 +010099 char * filename;
bellard3d11d0e2004-12-12 16:56:30 +0000100 char line[1024];
101 int len;
102
Paul Brook5cea8592009-05-30 00:52:44 +0100103 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100104 f = filename ? fopen(filename, "r") : NULL;
105 g_free(filename);
106 if (!f) {
Gonglei43948382014-12-08 19:39:05 +0800107 fprintf(stderr, "Could not read keymap file: '%s'\n", language);
108 return NULL;
bellard3d11d0e2004-12-12 16:56:30 +0000109 }
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100110
Gonglei43948382014-12-08 19:39:05 +0800111 if (!k) {
112 k = g_malloc0(sizeof(kbd_layout_t));
113 }
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100114
bellard3d11d0e2004-12-12 16:56:30 +0000115 for(;;) {
Gonglei43948382014-12-08 19:39:05 +0800116 if (fgets(line, 1024, f) == NULL) {
bellard3d11d0e2004-12-12 16:56:30 +0000117 break;
Gonglei43948382014-12-08 19:39:05 +0800118 }
bellard3d11d0e2004-12-12 16:56:30 +0000119 len = strlen(line);
Gonglei43948382014-12-08 19:39:05 +0800120 if (len > 0 && line[len - 1] == '\n') {
bellard3d11d0e2004-12-12 16:56:30 +0000121 line[len - 1] = '\0';
Gonglei43948382014-12-08 19:39:05 +0800122 }
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);
bellard3d11d0e2004-12-12 16:56:30 +0000131 } else {
Gonglei43948382014-12-08 19:39:05 +0800132 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;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100144 int keycode = strtol(rest, NULL, 0);
balroga528b802007-10-30 22:38:53 +0000145
Markus Armbruster955d7b22013-01-16 18:20:57 +0100146 if (strstr(rest, "numlock")) {
Gonglei43948382014-12-08 19:39:05 +0800147 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 }
balroga528b802007-10-30 22:38:53 +0000152
Markus Armbruster955d7b22013-01-16 18:20:57 +0100153 if (strstr(rest, "shift")) {
Gonglei43948382014-12-08 19:39:05 +0800154 keycode |= SCANCODE_SHIFT;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100155 }
156 if (strstr(rest, "altgr")) {
Gonglei43948382014-12-08 19:39:05 +0800157 keycode |= SCANCODE_ALTGR;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100158 }
159 if (strstr(rest, "ctrl")) {
Gonglei43948382014-12-08 19:39:05 +0800160 keycode |= SCANCODE_CTRL;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100161 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100162
Gonglei43948382014-12-08 19:39:05 +0800163 add_keysym(line, keysym, keycode, k);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100164
Markus Armbruster955d7b22013-01-16 18:20:57 +0100165 if (strstr(rest, "addupper")) {
Gonglei43948382014-12-08 19:39:05 +0800166 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 }
bellard3d11d0e2004-12-12 16:56:30 +0000179 }
180 fclose(f);
181 return k;
182}
183
aliguori04837552009-03-06 20:27:10 +0000184
Anthony Liguoric227f092009-10-01 16:12:16 -0500185void *init_keyboard_layout(const name2keysym_t *table, const char *language)
bellard3d11d0e2004-12-12 16:56:30 +0000186{
Blue Swirl660f11b2009-07-31 21:16:51 +0000187 return parse_keyboard_layout(table, language, NULL);
bellard3d11d0e2004-12-12 16:56:30 +0000188}
189
aliguori04837552009-03-06 20:27:10 +0000190
191int keysym2scancode(void *kbd_layout, int keysym)
bellard3d11d0e2004-12-12 16:56:30 +0000192{
Anthony Liguoric227f092009-10-01 16:12:16 -0500193 kbd_layout_t *k = kbd_layout;
bellard3d11d0e2004-12-12 16:56:30 +0000194 if (keysym < MAX_NORMAL_KEYCODE) {
Gonglei43948382014-12-08 19:39:05 +0800195 if (k->keysym2keycode[keysym] == 0) {
196 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
197 keysym);
198 }
199 return k->keysym2keycode[keysym];
bellard3d11d0e2004-12-12 16:56:30 +0000200 } else {
Gonglei43948382014-12-08 19:39:05 +0800201 int i;
bellard3d11d0e2004-12-12 16:56:30 +0000202#ifdef XK_ISO_Left_Tab
Gonglei43948382014-12-08 19:39:05 +0800203 if (keysym == XK_ISO_Left_Tab) {
204 keysym = XK_Tab;
205 }
bellard3d11d0e2004-12-12 16:56:30 +0000206#endif
Gonglei43948382014-12-08 19:39:05 +0800207 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 }
bellard3d11d0e2004-12-12 16:56:30 +0000212 }
213 return 0;
214}
balroga528b802007-10-30 22:38:53 +0000215
aliguori04837552009-03-06 20:27:10 +0000216int keycode_is_keypad(void *kbd_layout, int keycode)
balroga528b802007-10-30 22:38:53 +0000217{
Anthony Liguoric227f092009-10-01 16:12:16 -0500218 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000219 struct key_range *kr;
220
Gonglei43948382014-12-08 19:39:05 +0800221 for (kr = k->keypad_range; kr; kr = kr->next) {
222 if (keycode >= kr->start && keycode <= kr->end) {
balroga528b802007-10-30 22:38:53 +0000223 return 1;
Gonglei43948382014-12-08 19:39:05 +0800224 }
225 }
balroga528b802007-10-30 22:38:53 +0000226 return 0;
227}
228
aliguori04837552009-03-06 20:27:10 +0000229int keysym_is_numlock(void *kbd_layout, int keysym)
balroga528b802007-10-30 22:38:53 +0000230{
Anthony Liguoric227f092009-10-01 16:12:16 -0500231 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000232 struct key_range *kr;
233
Gonglei43948382014-12-08 19:39:05 +0800234 for (kr = k->numlock_range; kr; kr = kr->next) {
235 if (keysym >= kr->start && keysym <= kr->end) {
balroga528b802007-10-30 22:38:53 +0000236 return 1;
Gonglei43948382014-12-08 19:39:05 +0800237 }
238 }
balroga528b802007-10-30 22:38:53 +0000239 return 0;
240}