blob: 80d658d907d3d5671ccc7c5c3ceb21f587eea56d [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,
aliguori04837552009-03-06 20:27:10 +000029 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++) {
bellard3d11d0e2004-12-12 16:56:30 +000033 if (!strcmp(p->name, name))
34 return p->keysym;
35 }
Jan Krupa82807152013-10-16 14:40:05 +020036 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
37 char *end;
38 int ret = (int)strtoul(name + 1, &end, 16);
39 if (*end == '\0' && ret > 0)
40 return ret;
41 }
bellard3d11d0e2004-12-12 16:56:30 +000042 return 0;
43}
44
bellard3d11d0e2004-12-12 16:56:30 +000045
balroga528b802007-10-30 22:38:53 +000046static void add_to_key_range(struct key_range **krp, int code) {
47 struct key_range *kr;
48 for (kr = *krp; kr; kr = kr->next) {
49 if (code >= kr->start && code <= kr->end)
50 break;
51 if (code == kr->start - 1) {
52 kr->start--;
53 break;
54 }
55 if (code == kr->end + 1) {
56 kr->end++;
57 break;
58 }
59 }
60 if (kr == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -050061 kr = g_malloc0(sizeof(*kr));
aliguori1eec6142009-02-05 22:06:18 +000062 kr->start = kr->end = code;
63 kr->next = *krp;
64 *krp = kr;
balroga528b802007-10-30 22:38:53 +000065 }
66}
67
Samuel Thibault44bb61c2010-02-28 21:03:00 +010068static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
69 if (keysym < MAX_NORMAL_KEYCODE) {
70 //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
71 k->keysym2keycode[keysym] = keycode;
72 } else {
73 if (k->extra_count >= MAX_EXTRA_COUNT) {
74 fprintf(stderr,
75 "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
76 line, keysym);
77 } else {
78#if 0
79 fprintf(stderr, "Setting %d: %d,%d\n",
80 k->extra_count, keysym, keycode);
81#endif
82 k->keysym2keycode_extra[k->extra_count].
83 keysym = keysym;
84 k->keysym2keycode_extra[k->extra_count].
85 keycode = keycode;
86 k->extra_count++;
87 }
88 }
89}
90
Anthony Liguoric227f092009-10-01 16:12:16 -050091static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
aliguori04837552009-03-06 20:27:10 +000092 const char *language,
Anthony Liguoric227f092009-10-01 16:12:16 -050093 kbd_layout_t * k)
bellard3d11d0e2004-12-12 16:56:30 +000094{
95 FILE *f;
Paul Brook5cea8592009-05-30 00:52:44 +010096 char * filename;
bellard3d11d0e2004-12-12 16:56:30 +000097 char line[1024];
98 int len;
99
Paul Brook5cea8592009-05-30 00:52:44 +0100100 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100101 f = filename ? fopen(filename, "r") : NULL;
102 g_free(filename);
103 if (!f) {
bellard3d11d0e2004-12-12 16:56:30 +0000104 fprintf(stderr,
Paul Brook5cea8592009-05-30 00:52:44 +0100105 "Could not read keymap file: '%s'\n", language);
Blue Swirl660f11b2009-07-31 21:16:51 +0000106 return NULL;
bellard3d11d0e2004-12-12 16:56:30 +0000107 }
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100108
109 if (!k)
110 k = g_malloc0(sizeof(kbd_layout_t));
111
bellard3d11d0e2004-12-12 16:56:30 +0000112 for(;;) {
113 if (fgets(line, 1024, f) == NULL)
114 break;
115 len = strlen(line);
116 if (len > 0 && line[len - 1] == '\n')
117 line[len - 1] = '\0';
118 if (line[0] == '#')
119 continue;
120 if (!strncmp(line, "map ", 4))
121 continue;
122 if (!strncmp(line, "include ", 8)) {
aliguori04837552009-03-06 20:27:10 +0000123 parse_keyboard_layout(table, line + 8, k);
bellard3d11d0e2004-12-12 16:56:30 +0000124 } else {
125 char *end_of_keysym = line;
126 while (*end_of_keysym != 0 && *end_of_keysym != ' ')
127 end_of_keysym++;
128 if (*end_of_keysym) {
129 int keysym;
130 *end_of_keysym = 0;
aliguori04837552009-03-06 20:27:10 +0000131 keysym = get_keysym(table, line);
bellard3d11d0e2004-12-12 16:56:30 +0000132 if (keysym == 0) {
133 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
134 } else {
135 const char *rest = end_of_keysym + 1;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100136 int keycode = strtol(rest, NULL, 0);
balroga528b802007-10-30 22:38:53 +0000137
Markus Armbruster955d7b22013-01-16 18:20:57 +0100138 if (strstr(rest, "numlock")) {
balroga528b802007-10-30 22:38:53 +0000139 add_to_key_range(&k->keypad_range, keycode);
140 add_to_key_range(&k->numlock_range, keysym);
141 //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
142 }
143
Markus Armbruster955d7b22013-01-16 18:20:57 +0100144 if (strstr(rest, "shift")) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100145 keycode |= SCANCODE_SHIFT;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100146 }
147 if (strstr(rest, "altgr")) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100148 keycode |= SCANCODE_ALTGR;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100149 }
150 if (strstr(rest, "ctrl")) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100151 keycode |= SCANCODE_CTRL;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100152 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100153
154 add_keysym(line, keysym, keycode, k);
155
Markus Armbruster955d7b22013-01-16 18:20:57 +0100156 if (strstr(rest, "addupper")) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100157 char *c;
158 for (c = line; *c; c++)
Christoph Egger7b0a03a2011-09-21 11:10:52 +0100159 *c = qemu_toupper(*c);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100160 keysym = get_keysym(table, line);
161 if (keysym)
162 add_keysym(line, keysym, keycode | SCANCODE_SHIFT, k);
bellard3d11d0e2004-12-12 16:56:30 +0000163 }
164 }
165 }
166 }
167 }
168 fclose(f);
169 return k;
170}
171
aliguori04837552009-03-06 20:27:10 +0000172
Anthony Liguoric227f092009-10-01 16:12:16 -0500173void *init_keyboard_layout(const name2keysym_t *table, const char *language)
bellard3d11d0e2004-12-12 16:56:30 +0000174{
Blue Swirl660f11b2009-07-31 21:16:51 +0000175 return parse_keyboard_layout(table, language, NULL);
bellard3d11d0e2004-12-12 16:56:30 +0000176}
177
aliguori04837552009-03-06 20:27:10 +0000178
179int keysym2scancode(void *kbd_layout, int keysym)
bellard3d11d0e2004-12-12 16:56:30 +0000180{
Anthony Liguoric227f092009-10-01 16:12:16 -0500181 kbd_layout_t *k = kbd_layout;
bellard3d11d0e2004-12-12 16:56:30 +0000182 if (keysym < MAX_NORMAL_KEYCODE) {
183 if (k->keysym2keycode[keysym] == 0)
184 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
185 keysym);
186 return k->keysym2keycode[keysym];
187 } else {
188 int i;
189#ifdef XK_ISO_Left_Tab
190 if (keysym == XK_ISO_Left_Tab)
191 keysym = XK_Tab;
192#endif
193 for (i = 0; i < k->extra_count; i++)
194 if (k->keysym2keycode_extra[i].keysym == keysym)
195 return k->keysym2keycode_extra[i].keycode;
196 }
197 return 0;
198}
balroga528b802007-10-30 22:38:53 +0000199
aliguori04837552009-03-06 20:27:10 +0000200int keycode_is_keypad(void *kbd_layout, int keycode)
balroga528b802007-10-30 22:38:53 +0000201{
Anthony Liguoric227f092009-10-01 16:12:16 -0500202 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000203 struct key_range *kr;
204
205 for (kr = k->keypad_range; kr; kr = kr->next)
206 if (keycode >= kr->start && keycode <= kr->end)
207 return 1;
208 return 0;
209}
210
aliguori04837552009-03-06 20:27:10 +0000211int keysym_is_numlock(void *kbd_layout, int keysym)
balroga528b802007-10-30 22:38:53 +0000212{
Anthony Liguoric227f092009-10-01 16:12:16 -0500213 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000214 struct key_range *kr;
215
216 for (kr = k->numlock_range; kr; kr = kr->next)
217 if (keysym >= kr->start && keysym <= kr->end)
218 return 1;
219 return 0;
220}