blob: 668556298ac1c88c36d6b98dd7f45cf486b36e24 [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"
26#include "sysemu.h"
27
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 }
36 return 0;
37}
38
bellard3d11d0e2004-12-12 16:56:30 +000039
balroga528b802007-10-30 22:38:53 +000040static 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) {
55 kr = qemu_mallocz(sizeof(*kr));
aliguori1eec6142009-02-05 22:06:18 +000056 kr->start = kr->end = code;
57 kr->next = *krp;
58 *krp = kr;
balroga528b802007-10-30 22:38:53 +000059 }
60}
61
Anthony Liguoric227f092009-10-01 16:12:16 -050062static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
aliguori04837552009-03-06 20:27:10 +000063 const char *language,
Anthony Liguoric227f092009-10-01 16:12:16 -050064 kbd_layout_t * k)
bellard3d11d0e2004-12-12 16:56:30 +000065{
66 FILE *f;
Paul Brook5cea8592009-05-30 00:52:44 +010067 char * filename;
bellard3d11d0e2004-12-12 16:56:30 +000068 char line[1024];
69 int len;
70
Paul Brook5cea8592009-05-30 00:52:44 +010071 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
bellard3d11d0e2004-12-12 16:56:30 +000072
73 if (!k)
Anthony Liguoric227f092009-10-01 16:12:16 -050074 k = qemu_mallocz(sizeof(kbd_layout_t));
Paul Brook5cea8592009-05-30 00:52:44 +010075 if (!(filename && (f = fopen(filename, "r")))) {
bellard3d11d0e2004-12-12 16:56:30 +000076 fprintf(stderr,
Paul Brook5cea8592009-05-30 00:52:44 +010077 "Could not read keymap file: '%s'\n", language);
Blue Swirl660f11b2009-07-31 21:16:51 +000078 return NULL;
bellard3d11d0e2004-12-12 16:56:30 +000079 }
Paul Brook5cea8592009-05-30 00:52:44 +010080 qemu_free(filename);
bellard3d11d0e2004-12-12 16:56:30 +000081 for(;;) {
82 if (fgets(line, 1024, f) == NULL)
83 break;
84 len = strlen(line);
85 if (len > 0 && line[len - 1] == '\n')
86 line[len - 1] = '\0';
87 if (line[0] == '#')
88 continue;
89 if (!strncmp(line, "map ", 4))
90 continue;
91 if (!strncmp(line, "include ", 8)) {
aliguori04837552009-03-06 20:27:10 +000092 parse_keyboard_layout(table, line + 8, k);
bellard3d11d0e2004-12-12 16:56:30 +000093 } else {
94 char *end_of_keysym = line;
95 while (*end_of_keysym != 0 && *end_of_keysym != ' ')
96 end_of_keysym++;
97 if (*end_of_keysym) {
98 int keysym;
99 *end_of_keysym = 0;
aliguori04837552009-03-06 20:27:10 +0000100 keysym = get_keysym(table, line);
bellard3d11d0e2004-12-12 16:56:30 +0000101 if (keysym == 0) {
102 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
103 } else {
104 const char *rest = end_of_keysym + 1;
balroga528b802007-10-30 22:38:53 +0000105 char *rest2;
106 int keycode = strtol(rest, &rest2, 0);
107
108 if (rest && strstr(rest, "numlock")) {
109 add_to_key_range(&k->keypad_range, keycode);
110 add_to_key_range(&k->numlock_range, keysym);
111 //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
112 }
113
bellard3d11d0e2004-12-12 16:56:30 +0000114 /* if(keycode&0x80)
115 keycode=(keycode<<8)^0x80e0; */
116 if (keysym < MAX_NORMAL_KEYCODE) {
117 //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
118 k->keysym2keycode[keysym] = keycode;
119 } else {
120 if (k->extra_count >= MAX_EXTRA_COUNT) {
121 fprintf(stderr,
122 "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
123 line, keysym);
124 } else {
bellard24236862006-04-30 21:28:36 +0000125#if 0
bellard3d11d0e2004-12-12 16:56:30 +0000126 fprintf(stderr, "Setting %d: %d,%d\n",
127 k->extra_count, keysym, keycode);
bellard24236862006-04-30 21:28:36 +0000128#endif
bellard3d11d0e2004-12-12 16:56:30 +0000129 k->keysym2keycode_extra[k->extra_count].
130 keysym = keysym;
131 k->keysym2keycode_extra[k->extra_count].
132 keycode = keycode;
133 k->extra_count++;
134 }
135 }
136 }
137 }
138 }
139 }
140 fclose(f);
141 return k;
142}
143
aliguori04837552009-03-06 20:27:10 +0000144
Anthony Liguoric227f092009-10-01 16:12:16 -0500145void *init_keyboard_layout(const name2keysym_t *table, const char *language)
bellard3d11d0e2004-12-12 16:56:30 +0000146{
Blue Swirl660f11b2009-07-31 21:16:51 +0000147 return parse_keyboard_layout(table, language, NULL);
bellard3d11d0e2004-12-12 16:56:30 +0000148}
149
aliguori04837552009-03-06 20:27:10 +0000150
151int keysym2scancode(void *kbd_layout, int keysym)
bellard3d11d0e2004-12-12 16:56:30 +0000152{
Anthony Liguoric227f092009-10-01 16:12:16 -0500153 kbd_layout_t *k = kbd_layout;
bellard3d11d0e2004-12-12 16:56:30 +0000154 if (keysym < MAX_NORMAL_KEYCODE) {
155 if (k->keysym2keycode[keysym] == 0)
156 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
157 keysym);
158 return k->keysym2keycode[keysym];
159 } else {
160 int i;
161#ifdef XK_ISO_Left_Tab
162 if (keysym == XK_ISO_Left_Tab)
163 keysym = XK_Tab;
164#endif
165 for (i = 0; i < k->extra_count; i++)
166 if (k->keysym2keycode_extra[i].keysym == keysym)
167 return k->keysym2keycode_extra[i].keycode;
168 }
169 return 0;
170}
balroga528b802007-10-30 22:38:53 +0000171
aliguori04837552009-03-06 20:27:10 +0000172int keycode_is_keypad(void *kbd_layout, int keycode)
balroga528b802007-10-30 22:38:53 +0000173{
Anthony Liguoric227f092009-10-01 16:12:16 -0500174 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000175 struct key_range *kr;
176
177 for (kr = k->keypad_range; kr; kr = kr->next)
178 if (keycode >= kr->start && keycode <= kr->end)
179 return 1;
180 return 0;
181}
182
aliguori04837552009-03-06 20:27:10 +0000183int keysym_is_numlock(void *kbd_layout, int keysym)
balroga528b802007-10-30 22:38:53 +0000184{
Anthony Liguoric227f092009-10-01 16:12:16 -0500185 kbd_layout_t *k = kbd_layout;
balroga528b802007-10-30 22:38:53 +0000186 struct key_range *kr;
187
188 for (kr = k->numlock_range; kr; kr = kr->next)
189 if (keysym >= kr->start && keysym <= kr->end)
190 return 1;
191 return 0;
192}