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
25 static int get_keysym(const char *name)
28 for(p = name2keysym; p->name != NULL; p++) {
29 if (!strcmp(p->name, name))
35 #define MAX_NORMAL_KEYCODE 512
36 #define MAX_EXTRA_COUNT 256
38 uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
42 } keysym2keycode_extra[MAX_EXTRA_COUNT];
46 static kbd_layout_t *parse_keyboard_layout(const char *language,
54 snprintf(file_name, sizeof(file_name),
55 "%s/keymaps/%s", bios_dir, language);
58 k = qemu_mallocz(sizeof(kbd_layout_t));
61 if (!(f = fopen(file_name, "r"))) {
63 "Could not read keymap file: '%s'\n", file_name);
67 if (fgets(line, 1024, f) == NULL)
70 if (len > 0 && line[len - 1] == '\n')
74 if (!strncmp(line, "map ", 4))
76 if (!strncmp(line, "include ", 8)) {
77 parse_keyboard_layout(line + 8, k);
79 char *end_of_keysym = line;
80 while (*end_of_keysym != 0 && *end_of_keysym != ' ')
85 keysym = get_keysym(line);
87 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
89 const char *rest = end_of_keysym + 1;
90 int keycode = strtol(rest, NULL, 0);
92 keycode=(keycode<<8)^0x80e0; */
93 if (keysym < MAX_NORMAL_KEYCODE) {
94 //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
95 k->keysym2keycode[keysym] = keycode;
97 if (k->extra_count >= MAX_EXTRA_COUNT) {
99 "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
103 fprintf(stderr, "Setting %d: %d,%d\n",
104 k->extra_count, keysym, keycode);
106 k->keysym2keycode_extra[k->extra_count].
108 k->keysym2keycode_extra[k->extra_count].
121 static void *init_keyboard_layout(const char *language)
123 return parse_keyboard_layout(language, 0);
126 static int keysym2scancode(void *kbd_layout, int keysym)
128 kbd_layout_t *k = kbd_layout;
129 if (keysym < MAX_NORMAL_KEYCODE) {
130 if (k->keysym2keycode[keysym] == 0)
131 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
133 return k->keysym2keycode[keysym];
136 #ifdef XK_ISO_Left_Tab
137 if (keysym == XK_ISO_Left_Tab)
140 for (i = 0; i < k->extra_count; i++)
141 if (k->keysym2keycode_extra[i].keysym == keysym)
142 return k->keysym2keycode_extra[i].keycode;