2 * QEMU keysym to keycode conversion using rdesktop keymaps
4 * Copyright (c) 2004 Johannes Schindelin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include "sysemu/sysemu.h"
28 static int get_keysym(const name2keysym_t *table,
31 const name2keysym_t *p;
32 for(p = table; p->name != NULL; p++) {
33 if (!strcmp(p->name, name)) {
37 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
39 int ret = (int)strtoul(name + 1, &end, 16);
40 if (*end == '\0' && ret > 0) {
48 static void add_to_key_range(struct key_range **krp, int code) {
50 for (kr = *krp; kr; kr = kr->next) {
51 if (code >= kr->start && code <= kr->end) {
54 if (code == kr->start - 1) {
58 if (code == kr->end + 1) {
64 kr = g_malloc0(sizeof(*kr));
65 kr->start = kr->end = code;
71 static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
72 if (keysym < MAX_NORMAL_KEYCODE) {
73 /* fprintf(stderr,"Setting keysym %s (%d) to %d\n",
74 line, keysym, keycode); */
75 k->keysym2keycode[keysym] = keycode;
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);
82 fprintf(stderr, "Setting %d: %d,%d\n",
83 k->extra_count, keysym, keycode);
85 k->keysym2keycode_extra[k->extra_count].
87 k->keysym2keycode_extra[k->extra_count].
94 static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
103 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
104 f = filename ? fopen(filename, "r") : NULL;
107 fprintf(stderr, "Could not read keymap file: '%s'\n", language);
112 k = g_malloc0(sizeof(kbd_layout_t));
116 if (fgets(line, 1024, f) == NULL) {
120 if (len > 0 && line[len - 1] == '\n') {
121 line[len - 1] = '\0';
123 if (line[0] == '#') {
126 if (!strncmp(line, "map ", 4)) {
129 if (!strncmp(line, "include ", 8)) {
130 parse_keyboard_layout(table, line + 8, k);
132 char *end_of_keysym = line;
133 while (*end_of_keysym != 0 && *end_of_keysym != ' ') {
136 if (*end_of_keysym) {
139 keysym = get_keysym(table, line);
141 /* fprintf(stderr, "Warning: unknown keysym %s\n", line);*/
143 const char *rest = end_of_keysym + 1;
144 int keycode = strtol(rest, NULL, 0);
146 if (strstr(rest, "numlock")) {
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",
153 if (strstr(rest, "shift")) {
154 keycode |= SCANCODE_SHIFT;
156 if (strstr(rest, "altgr")) {
157 keycode |= SCANCODE_ALTGR;
159 if (strstr(rest, "ctrl")) {
160 keycode |= SCANCODE_CTRL;
163 add_keysym(line, keysym, keycode, k);
165 if (strstr(rest, "addupper")) {
167 for (c = line; *c; c++) {
168 *c = qemu_toupper(*c);
170 keysym = get_keysym(table, line);
172 add_keysym(line, keysym,
173 keycode | SCANCODE_SHIFT, k);
185 void *init_keyboard_layout(const name2keysym_t *table, const char *language)
187 return parse_keyboard_layout(table, language, NULL);
191 int keysym2scancode(void *kbd_layout, int keysym)
193 kbd_layout_t *k = kbd_layout;
194 if (keysym < MAX_NORMAL_KEYCODE) {
195 if (k->keysym2keycode[keysym] == 0) {
196 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
199 return k->keysym2keycode[keysym];
202 #ifdef XK_ISO_Left_Tab
203 if (keysym == XK_ISO_Left_Tab) {
207 for (i = 0; i < k->extra_count; i++) {
208 if (k->keysym2keycode_extra[i].keysym == keysym) {
209 return k->keysym2keycode_extra[i].keycode;
216 int keycode_is_keypad(void *kbd_layout, int keycode)
218 kbd_layout_t *k = kbd_layout;
219 struct key_range *kr;
221 for (kr = k->keypad_range; kr; kr = kr->next) {
222 if (keycode >= kr->start && keycode <= kr->end) {
229 int keysym_is_numlock(void *kbd_layout, int keysym)
231 kbd_layout_t *k = kbd_layout;
232 struct key_range *kr;
234 for (kr = k->numlock_range; kr; kr = kr->next) {
235 if (keysym >= kr->start && keysym <= kr->end) {