bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU keysym to keycode conversion using rdesktop keymaps |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004 Johannes Schindelin |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 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: |
| 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 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 26 | #include "keymaps.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 28 | #include "trace.h" |
Alistair Francis | 8297be8 | 2017-09-11 12:52:53 -0700 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 30 | |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 31 | struct keysym2code { |
Gerd Hoffmann | 23ad24e | 2018-02-22 08:05:12 +0100 | [diff] [blame] | 32 | uint32_t count; |
| 33 | uint16_t keycodes[4]; |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Gerd Hoffmann | fe5fca9 | 2018-02-22 08:05:09 +0100 | [diff] [blame] | 36 | struct kbd_layout_t { |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 37 | GHashTable *hash; |
Gerd Hoffmann | fe5fca9 | 2018-02-22 08:05:09 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 40 | static int get_keysym(const name2keysym_t *table, |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 41 | const char *name) |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 42 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 43 | const name2keysym_t *p; |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 44 | for(p = table; p->name != NULL; p++) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 45 | if (!strcmp(p->name, name)) { |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 46 | return p->keysym; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 47 | } |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 48 | } |
Jan Krupa | 8280715 | 2013-10-16 14:40:05 +0200 | [diff] [blame] | 49 | if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */ |
| 50 | char *end; |
| 51 | int ret = (int)strtoul(name + 1, &end, 16); |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 52 | if (*end == '\0' && ret > 0) { |
| 53 | return ret; |
| 54 | } |
Jan Krupa | 8280715 | 2013-10-16 14:40:05 +0200 | [diff] [blame] | 55 | } |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 59 | |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 60 | static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) |
| 61 | { |
| 62 | struct keysym2code *keysym2code; |
| 63 | |
| 64 | keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); |
| 65 | if (keysym2code) { |
Gerd Hoffmann | 23ad24e | 2018-02-22 08:05:12 +0100 | [diff] [blame] | 66 | if (keysym2code->count < ARRAY_SIZE(keysym2code->keycodes)) { |
| 67 | keysym2code->keycodes[keysym2code->count++] = keycode; |
| 68 | } else { |
| 69 | warn_report("more than %zd keycodes for keysym %d", |
| 70 | ARRAY_SIZE(keysym2code->keycodes), keysym); |
| 71 | } |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 72 | return; |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 73 | } |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 74 | |
| 75 | keysym2code = g_new0(struct keysym2code, 1); |
Gerd Hoffmann | 23ad24e | 2018-02-22 08:05:12 +0100 | [diff] [blame] | 76 | keysym2code->keycodes[0] = keycode; |
| 77 | keysym2code->count = 1; |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 78 | g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code); |
| 79 | trace_keymap_add(keysym, keycode, line); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 82 | static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table, |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 83 | const char *language, |
| 84 | kbd_layout_t *k) |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 85 | { |
| 86 | FILE *f; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 87 | char * filename; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 88 | char line[1024]; |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 89 | char keyname[64]; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 90 | int len; |
| 91 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 92 | filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language); |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 93 | trace_keymap_parse(filename); |
Markus Armbruster | f2d3476 | 2011-11-11 10:40:06 +0100 | [diff] [blame] | 94 | f = filename ? fopen(filename, "r") : NULL; |
| 95 | g_free(filename); |
| 96 | if (!f) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 97 | fprintf(stderr, "Could not read keymap file: '%s'\n", language); |
| 98 | return NULL; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 99 | } |
Markus Armbruster | f2d3476 | 2011-11-11 10:40:06 +0100 | [diff] [blame] | 100 | |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 101 | if (!k) { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 102 | k = g_new0(kbd_layout_t, 1); |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 103 | k->hash = g_hash_table_new(NULL, NULL); |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 104 | } |
Markus Armbruster | f2d3476 | 2011-11-11 10:40:06 +0100 | [diff] [blame] | 105 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 106 | for(;;) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 107 | if (fgets(line, 1024, f) == NULL) { |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 108 | break; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 109 | } |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 110 | len = strlen(line); |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 111 | if (len > 0 && line[len - 1] == '\n') { |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 112 | line[len - 1] = '\0'; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 113 | } |
| 114 | if (line[0] == '#') { |
| 115 | continue; |
| 116 | } |
| 117 | if (!strncmp(line, "map ", 4)) { |
| 118 | continue; |
| 119 | } |
| 120 | if (!strncmp(line, "include ", 8)) { |
| 121 | parse_keyboard_layout(table, line + 8, k); |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 122 | } else { |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 123 | int offset = 0; |
| 124 | while (line[offset] != 0 && |
| 125 | line[offset] != ' ' && |
| 126 | offset < sizeof(keyname) - 1) { |
| 127 | keyname[offset] = line[offset]; |
| 128 | offset++; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 129 | } |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 130 | keyname[offset] = 0; |
| 131 | if (strlen(keyname)) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 132 | int keysym; |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 133 | keysym = get_keysym(table, keyname); |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 134 | if (keysym == 0) { |
Alistair Francis | 2ab4b13 | 2017-09-11 12:52:50 -0700 | [diff] [blame] | 135 | /* warn_report("unknown keysym %s", line);*/ |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 136 | } else { |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 137 | const char *rest = line + offset + 1; |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 138 | int keycode = strtol(rest, NULL, 0); |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 139 | |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 140 | if (strstr(rest, "shift")) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 141 | keycode |= SCANCODE_SHIFT; |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 142 | } |
| 143 | if (strstr(rest, "altgr")) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 144 | keycode |= SCANCODE_ALTGR; |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 145 | } |
| 146 | if (strstr(rest, "ctrl")) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 147 | keycode |= SCANCODE_CTRL; |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 148 | } |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 149 | |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 150 | add_keysym(line, keysym, keycode, k); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 151 | |
Markus Armbruster | 955d7b2 | 2013-01-16 18:20:57 +0100 | [diff] [blame] | 152 | if (strstr(rest, "addupper")) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 153 | char *c; |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 154 | for (c = keyname; *c; c++) { |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 155 | *c = qemu_toupper(*c); |
| 156 | } |
Gerd Hoffmann | d3b787f | 2017-06-06 15:47:36 +0200 | [diff] [blame] | 157 | keysym = get_keysym(table, keyname); |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 158 | if (keysym) { |
| 159 | add_keysym(line, keysym, |
| 160 | keycode | SCANCODE_SHIFT, k); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 166 | } |
| 167 | fclose(f); |
| 168 | return k; |
| 169 | } |
| 170 | |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 171 | |
Gerd Hoffmann | fe5fca9 | 2018-02-22 08:05:09 +0100 | [diff] [blame] | 172 | kbd_layout_t *init_keyboard_layout(const name2keysym_t *table, |
| 173 | const char *language) |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 174 | { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 175 | return parse_keyboard_layout(table, language, NULL); |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 176 | } |
| 177 | |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 178 | |
Gerd Hoffmann | abb4f2c | 2018-02-22 08:05:13 +0100 | [diff] [blame] | 179 | int keysym2scancode(kbd_layout_t *k, int keysym, |
| 180 | bool shift, bool altgr, bool ctrl) |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 181 | { |
Gerd Hoffmann | abb4f2c | 2018-02-22 08:05:13 +0100 | [diff] [blame] | 182 | static const uint32_t mask = |
| 183 | SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL; |
| 184 | uint32_t mods, i; |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 185 | struct keysym2code *keysym2code; |
| 186 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 187 | #ifdef XK_ISO_Left_Tab |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 188 | if (keysym == XK_ISO_Left_Tab) { |
| 189 | keysym = XK_Tab; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 190 | } |
Gerd Hoffmann | d713e3f | 2018-02-22 08:05:10 +0100 | [diff] [blame] | 191 | #endif |
| 192 | |
| 193 | keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); |
| 194 | if (!keysym2code) { |
| 195 | trace_keymap_unmapped(keysym); |
| 196 | warn_report("no scancode found for keysym %d", keysym); |
| 197 | return 0; |
| 198 | } |
| 199 | |
Gerd Hoffmann | abb4f2c | 2018-02-22 08:05:13 +0100 | [diff] [blame] | 200 | if (keysym2code->count == 1) { |
| 201 | return keysym2code->keycodes[0]; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * We have multiple keysym -> keycode mappings. |
| 206 | * |
| 207 | * Check whenever we find one mapping where the modifier state of |
| 208 | * the mapping matches the current user interface modifier state. |
| 209 | * If so, prefer that one. |
| 210 | */ |
| 211 | mods = 0; |
| 212 | if (shift) { |
| 213 | mods |= SCANCODE_SHIFT; |
| 214 | } |
| 215 | if (altgr) { |
| 216 | mods |= SCANCODE_ALTGR; |
| 217 | } |
| 218 | if (ctrl) { |
| 219 | mods |= SCANCODE_CTRL; |
| 220 | } |
| 221 | |
| 222 | for (i = 0; i < keysym2code->count; i++) { |
| 223 | if ((keysym2code->keycodes[i] & mask) == mods) { |
| 224 | return keysym2code->keycodes[i]; |
| 225 | } |
| 226 | } |
Gerd Hoffmann | 23ad24e | 2018-02-22 08:05:12 +0100 | [diff] [blame] | 227 | return keysym2code->keycodes[0]; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 228 | } |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 229 | |
Gerd Hoffmann | fe5fca9 | 2018-02-22 08:05:09 +0100 | [diff] [blame] | 230 | int keycode_is_keypad(kbd_layout_t *k, int keycode) |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 231 | { |
Gerd Hoffmann | 6b71ea1 | 2018-02-22 08:05:11 +0100 | [diff] [blame] | 232 | if (keycode >= 0x47 && keycode <= 0x53) { |
| 233 | return true; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 234 | } |
Gerd Hoffmann | 6b71ea1 | 2018-02-22 08:05:11 +0100 | [diff] [blame] | 235 | return false; |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Gerd Hoffmann | fe5fca9 | 2018-02-22 08:05:09 +0100 | [diff] [blame] | 238 | int keysym_is_numlock(kbd_layout_t *k, int keysym) |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 239 | { |
Gerd Hoffmann | 6b71ea1 | 2018-02-22 08:05:11 +0100 | [diff] [blame] | 240 | switch (keysym) { |
| 241 | case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */ |
| 242 | case 0xffac: /* KP_Separator */ |
| 243 | case 0xffae: /* KP_Decimal */ |
| 244 | return true; |
Gonglei | 4394838 | 2014-12-08 19:39:05 +0800 | [diff] [blame] | 245 | } |
Gerd Hoffmann | 6b71ea1 | 2018-02-22 08:05:11 +0100 | [diff] [blame] | 246 | return false; |
balrog | a528b80 | 2007-10-30 22:38:53 +0000 | [diff] [blame] | 247 | } |