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 #include "qemu/osdep.h"
27 #include "sysemu/sysemu.h"
29 #include "qemu/error-report.h"
31 static int get_keysym(const name2keysym_t *table,
34 const name2keysym_t *p;
35 for(p = table; p->name != NULL; p++) {
36 if (!strcmp(p->name, name)) {
40 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
42 int ret = (int)strtoul(name + 1, &end, 16);
43 if (*end == '\0' && ret > 0) {
51 static void add_to_key_range(struct key_range **krp, int code) {
53 for (kr = *krp; kr; kr = kr->next) {
54 if (code >= kr->start && code <= kr->end) {
57 if (code == kr->start - 1) {
61 if (code == kr->end + 1) {
67 kr = g_malloc0(sizeof(*kr));
68 kr->start = kr->end = code;
74 static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
75 if (keysym < MAX_NORMAL_KEYCODE) {
76 trace_keymap_add("normal", keysym, keycode, line);
77 k->keysym2keycode[keysym] = keycode;
79 if (k->extra_count >= MAX_EXTRA_COUNT) {
80 warn_report("Could not assign keysym %s (0x%x)"
81 " because of memory constraints.", line, keysym);
83 trace_keymap_add("extra", keysym, keycode, line);
84 k->keysym2keycode_extra[k->extra_count].
86 k->keysym2keycode_extra[k->extra_count].
93 static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
103 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
104 trace_keymap_parse(filename);
105 f = filename ? fopen(filename, "r") : NULL;
108 fprintf(stderr, "Could not read keymap file: '%s'\n", language);
113 k = g_new0(kbd_layout_t, 1);
117 if (fgets(line, 1024, f) == NULL) {
121 if (len > 0 && line[len - 1] == '\n') {
122 line[len - 1] = '\0';
124 if (line[0] == '#') {
127 if (!strncmp(line, "map ", 4)) {
130 if (!strncmp(line, "include ", 8)) {
131 parse_keyboard_layout(table, line + 8, k);
134 while (line[offset] != 0 &&
135 line[offset] != ' ' &&
136 offset < sizeof(keyname) - 1) {
137 keyname[offset] = line[offset];
141 if (strlen(keyname)) {
143 keysym = get_keysym(table, keyname);
145 /* warn_report("unknown keysym %s", line);*/
147 const char *rest = line + offset + 1;
148 int keycode = strtol(rest, NULL, 0);
150 if (strstr(rest, "numlock")) {
151 add_to_key_range(&k->keypad_range, keycode);
152 add_to_key_range(&k->numlock_range, keysym);
153 /* fprintf(stderr, "keypad keysym %04x keycode %d\n",
157 if (strstr(rest, "shift")) {
158 keycode |= SCANCODE_SHIFT;
160 if (strstr(rest, "altgr")) {
161 keycode |= SCANCODE_ALTGR;
163 if (strstr(rest, "ctrl")) {
164 keycode |= SCANCODE_CTRL;
167 add_keysym(line, keysym, keycode, k);
169 if (strstr(rest, "addupper")) {
171 for (c = keyname; *c; c++) {
172 *c = qemu_toupper(*c);
174 keysym = get_keysym(table, keyname);
176 add_keysym(line, keysym,
177 keycode | SCANCODE_SHIFT, k);
189 void *init_keyboard_layout(const name2keysym_t *table, const char *language)
191 return parse_keyboard_layout(table, language, NULL);
195 int keysym2scancode(void *kbd_layout, int keysym)
197 kbd_layout_t *k = kbd_layout;
198 if (keysym < MAX_NORMAL_KEYCODE) {
199 if (k->keysym2keycode[keysym] == 0) {
200 trace_keymap_unmapped(keysym);
201 warn_report("no scancode found for keysym %d", keysym);
203 return k->keysym2keycode[keysym];
206 #ifdef XK_ISO_Left_Tab
207 if (keysym == XK_ISO_Left_Tab) {
211 for (i = 0; i < k->extra_count; i++) {
212 if (k->keysym2keycode_extra[i].keysym == keysym) {
213 return k->keysym2keycode_extra[i].keycode;
220 int keycode_is_keypad(void *kbd_layout, int keycode)
222 kbd_layout_t *k = kbd_layout;
223 struct key_range *kr;
225 for (kr = k->keypad_range; kr; kr = kr->next) {
226 if (keycode >= kr->start && keycode <= kr->end) {
233 int keysym_is_numlock(void *kbd_layout, int keysym)
235 kbd_layout_t *k = kbd_layout;
236 struct key_range *kr;
238 for (kr = k->numlock_range; kr; kr = kr->next) {
239 if (keysym >= kr->start && keysym <= kr->end) {