blob: 43fe604724fe39832c71dd81e6927e85ed93cffd [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
Peter Maydelle16f4c82016-01-29 17:49:51 +000025#include "qemu/osdep.h"
aliguori04837552009-03-06 20:27:10 +000026#include "keymaps.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +020028#include "trace.h"
Alistair Francis8297be82017-09-11 12:52:53 -070029#include "qemu/error-report.h"
aliguori04837552009-03-06 20:27:10 +000030
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010031struct keysym2code {
Gerd Hoffmann23ad24e2018-02-22 08:05:12 +010032 uint32_t count;
33 uint16_t keycodes[4];
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010034};
35
Gerd Hoffmannfe5fca92018-02-22 08:05:09 +010036struct kbd_layout_t {
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010037 GHashTable *hash;
Gerd Hoffmannfe5fca92018-02-22 08:05:09 +010038};
39
Anthony Liguoric227f092009-10-01 16:12:16 -050040static int get_keysym(const name2keysym_t *table,
Gonglei43948382014-12-08 19:39:05 +080041 const char *name)
bellard3d11d0e2004-12-12 16:56:30 +000042{
Anthony Liguoric227f092009-10-01 16:12:16 -050043 const name2keysym_t *p;
aliguori04837552009-03-06 20:27:10 +000044 for(p = table; p->name != NULL; p++) {
Gonglei43948382014-12-08 19:39:05 +080045 if (!strcmp(p->name, name)) {
bellard3d11d0e2004-12-12 16:56:30 +000046 return p->keysym;
Gonglei43948382014-12-08 19:39:05 +080047 }
bellard3d11d0e2004-12-12 16:56:30 +000048 }
Jan Krupa82807152013-10-16 14:40:05 +020049 if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
50 char *end;
51 int ret = (int)strtoul(name + 1, &end, 16);
Gonglei43948382014-12-08 19:39:05 +080052 if (*end == '\0' && ret > 0) {
53 return ret;
54 }
Jan Krupa82807152013-10-16 14:40:05 +020055 }
bellard3d11d0e2004-12-12 16:56:30 +000056 return 0;
57}
58
bellard3d11d0e2004-12-12 16:56:30 +000059
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010060static 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 Hoffmann23ad24e2018-02-22 08:05:12 +010066 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 Hoffmannd713e3f2018-02-22 08:05:10 +010072 return;
Samuel Thibault44bb61c2010-02-28 21:03:00 +010073 }
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010074
75 keysym2code = g_new0(struct keysym2code, 1);
Gerd Hoffmann23ad24e2018-02-22 08:05:12 +010076 keysym2code->keycodes[0] = keycode;
77 keysym2code->count = 1;
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +010078 g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code);
79 trace_keymap_add(keysym, keycode, line);
Samuel Thibault44bb61c2010-02-28 21:03:00 +010080}
81
Anthony Liguoric227f092009-10-01 16:12:16 -050082static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
Gonglei43948382014-12-08 19:39:05 +080083 const char *language,
84 kbd_layout_t *k)
bellard3d11d0e2004-12-12 16:56:30 +000085{
86 FILE *f;
Paul Brook5cea8592009-05-30 00:52:44 +010087 char * filename;
bellard3d11d0e2004-12-12 16:56:30 +000088 char line[1024];
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +020089 char keyname[64];
bellard3d11d0e2004-12-12 16:56:30 +000090 int len;
91
Paul Brook5cea8592009-05-30 00:52:44 +010092 filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +020093 trace_keymap_parse(filename);
Markus Armbrusterf2d34762011-11-11 10:40:06 +010094 f = filename ? fopen(filename, "r") : NULL;
95 g_free(filename);
96 if (!f) {
Gonglei43948382014-12-08 19:39:05 +080097 fprintf(stderr, "Could not read keymap file: '%s'\n", language);
98 return NULL;
bellard3d11d0e2004-12-12 16:56:30 +000099 }
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100100
Gonglei43948382014-12-08 19:39:05 +0800101 if (!k) {
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100102 k = g_new0(kbd_layout_t, 1);
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +0100103 k->hash = g_hash_table_new(NULL, NULL);
Gonglei43948382014-12-08 19:39:05 +0800104 }
Markus Armbrusterf2d34762011-11-11 10:40:06 +0100105
bellard3d11d0e2004-12-12 16:56:30 +0000106 for(;;) {
Gonglei43948382014-12-08 19:39:05 +0800107 if (fgets(line, 1024, f) == NULL) {
bellard3d11d0e2004-12-12 16:56:30 +0000108 break;
Gonglei43948382014-12-08 19:39:05 +0800109 }
bellard3d11d0e2004-12-12 16:56:30 +0000110 len = strlen(line);
Gonglei43948382014-12-08 19:39:05 +0800111 if (len > 0 && line[len - 1] == '\n') {
bellard3d11d0e2004-12-12 16:56:30 +0000112 line[len - 1] = '\0';
Gonglei43948382014-12-08 19:39:05 +0800113 }
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);
bellard3d11d0e2004-12-12 16:56:30 +0000122 } else {
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200123 int offset = 0;
124 while (line[offset] != 0 &&
125 line[offset] != ' ' &&
126 offset < sizeof(keyname) - 1) {
127 keyname[offset] = line[offset];
128 offset++;
Gonglei43948382014-12-08 19:39:05 +0800129 }
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200130 keyname[offset] = 0;
131 if (strlen(keyname)) {
Gonglei43948382014-12-08 19:39:05 +0800132 int keysym;
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200133 keysym = get_keysym(table, keyname);
Gonglei43948382014-12-08 19:39:05 +0800134 if (keysym == 0) {
Alistair Francis2ab4b132017-09-11 12:52:50 -0700135 /* warn_report("unknown keysym %s", line);*/
Gonglei43948382014-12-08 19:39:05 +0800136 } else {
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200137 const char *rest = line + offset + 1;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100138 int keycode = strtol(rest, NULL, 0);
balroga528b802007-10-30 22:38:53 +0000139
Markus Armbruster955d7b22013-01-16 18:20:57 +0100140 if (strstr(rest, "shift")) {
Gonglei43948382014-12-08 19:39:05 +0800141 keycode |= SCANCODE_SHIFT;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100142 }
143 if (strstr(rest, "altgr")) {
Gonglei43948382014-12-08 19:39:05 +0800144 keycode |= SCANCODE_ALTGR;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100145 }
146 if (strstr(rest, "ctrl")) {
Gonglei43948382014-12-08 19:39:05 +0800147 keycode |= SCANCODE_CTRL;
Markus Armbruster955d7b22013-01-16 18:20:57 +0100148 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100149
Gonglei43948382014-12-08 19:39:05 +0800150 add_keysym(line, keysym, keycode, k);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100151
Markus Armbruster955d7b22013-01-16 18:20:57 +0100152 if (strstr(rest, "addupper")) {
Gonglei43948382014-12-08 19:39:05 +0800153 char *c;
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200154 for (c = keyname; *c; c++) {
Gonglei43948382014-12-08 19:39:05 +0800155 *c = qemu_toupper(*c);
156 }
Gerd Hoffmannd3b787f2017-06-06 15:47:36 +0200157 keysym = get_keysym(table, keyname);
Gonglei43948382014-12-08 19:39:05 +0800158 if (keysym) {
159 add_keysym(line, keysym,
160 keycode | SCANCODE_SHIFT, k);
161 }
162 }
163 }
164 }
165 }
bellard3d11d0e2004-12-12 16:56:30 +0000166 }
167 fclose(f);
168 return k;
169}
170
aliguori04837552009-03-06 20:27:10 +0000171
Gerd Hoffmannfe5fca92018-02-22 08:05:09 +0100172kbd_layout_t *init_keyboard_layout(const name2keysym_t *table,
173 const char *language)
bellard3d11d0e2004-12-12 16:56:30 +0000174{
Blue Swirl660f11b2009-07-31 21:16:51 +0000175 return parse_keyboard_layout(table, language, NULL);
bellard3d11d0e2004-12-12 16:56:30 +0000176}
177
aliguori04837552009-03-06 20:27:10 +0000178
Gerd Hoffmannabb4f2c2018-02-22 08:05:13 +0100179int keysym2scancode(kbd_layout_t *k, int keysym,
180 bool shift, bool altgr, bool ctrl)
bellard3d11d0e2004-12-12 16:56:30 +0000181{
Gerd Hoffmannabb4f2c2018-02-22 08:05:13 +0100182 static const uint32_t mask =
183 SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL;
184 uint32_t mods, i;
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +0100185 struct keysym2code *keysym2code;
186
bellard3d11d0e2004-12-12 16:56:30 +0000187#ifdef XK_ISO_Left_Tab
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +0100188 if (keysym == XK_ISO_Left_Tab) {
189 keysym = XK_Tab;
bellard3d11d0e2004-12-12 16:56:30 +0000190 }
Gerd Hoffmannd713e3f2018-02-22 08:05:10 +0100191#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 Hoffmannabb4f2c2018-02-22 08:05:13 +0100200 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 Hoffmann23ad24e2018-02-22 08:05:12 +0100227 return keysym2code->keycodes[0];
bellard3d11d0e2004-12-12 16:56:30 +0000228}
balroga528b802007-10-30 22:38:53 +0000229
Gerd Hoffmannfe5fca92018-02-22 08:05:09 +0100230int keycode_is_keypad(kbd_layout_t *k, int keycode)
balroga528b802007-10-30 22:38:53 +0000231{
Gerd Hoffmann6b71ea12018-02-22 08:05:11 +0100232 if (keycode >= 0x47 && keycode <= 0x53) {
233 return true;
Gonglei43948382014-12-08 19:39:05 +0800234 }
Gerd Hoffmann6b71ea12018-02-22 08:05:11 +0100235 return false;
balroga528b802007-10-30 22:38:53 +0000236}
237
Gerd Hoffmannfe5fca92018-02-22 08:05:09 +0100238int keysym_is_numlock(kbd_layout_t *k, int keysym)
balroga528b802007-10-30 22:38:53 +0000239{
Gerd Hoffmann6b71ea12018-02-22 08:05:11 +0100240 switch (keysym) {
241 case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */
242 case 0xffac: /* KP_Separator */
243 case 0xffae: /* KP_Decimal */
244 return true;
Gonglei43948382014-12-08 19:39:05 +0800245 }
Gerd Hoffmann6b71ea12018-02-22 08:05:11 +0100246 return false;
balroga528b802007-10-30 22:38:53 +0000247}