2004-12-13 00:56:30 +08:00
|
|
|
/*
|
|
|
|
* QEMU keysym to keycode conversion using rdesktop keymaps
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-12-13 00:56:30 +08:00
|
|
|
* Copyright (c) 2004 Johannes Schindelin
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-12-13 00:56:30 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-30 01:49:51 +08:00
|
|
|
#include "qemu/osdep.h"
|
2009-03-07 04:27:10 +08:00
|
|
|
#include "keymaps.h"
|
2012-12-18 01:20:04 +08:00
|
|
|
#include "sysemu/sysemu.h"
|
2017-06-06 21:47:36 +08:00
|
|
|
#include "trace.h"
|
2017-09-12 03:52:53 +08:00
|
|
|
#include "qemu/error-report.h"
|
2009-03-07 04:27:10 +08:00
|
|
|
|
2018-02-22 15:05:10 +08:00
|
|
|
struct keysym2code {
|
2018-02-22 15:05:12 +08:00
|
|
|
uint32_t count;
|
|
|
|
uint16_t keycodes[4];
|
2018-02-22 15:05:10 +08:00
|
|
|
};
|
|
|
|
|
2018-02-22 15:05:09 +08:00
|
|
|
struct kbd_layout_t {
|
2018-02-22 15:05:10 +08:00
|
|
|
GHashTable *hash;
|
2018-02-22 15:05:09 +08:00
|
|
|
};
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static int get_keysym(const name2keysym_t *table,
|
2014-12-08 19:39:05 +08:00
|
|
|
const char *name)
|
2004-12-13 00:56:30 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
const name2keysym_t *p;
|
2009-03-07 04:27:10 +08:00
|
|
|
for(p = table; p->name != NULL; p++) {
|
2014-12-08 19:39:05 +08:00
|
|
|
if (!strcmp(p->name, name)) {
|
2004-12-13 00:56:30 +08:00
|
|
|
return p->keysym;
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
2013-10-16 20:40:05 +08:00
|
|
|
if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
|
|
|
|
char *end;
|
|
|
|
int ret = (int)strtoul(name + 1, &end, 16);
|
2014-12-08 19:39:05 +08:00
|
|
|
if (*end == '\0' && ret > 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-16 20:40:05 +08:00
|
|
|
}
|
2004-12-13 00:56:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-22 15:05:10 +08:00
|
|
|
static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k)
|
|
|
|
{
|
|
|
|
struct keysym2code *keysym2code;
|
|
|
|
|
|
|
|
keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym));
|
|
|
|
if (keysym2code) {
|
2018-02-22 15:05:12 +08:00
|
|
|
if (keysym2code->count < ARRAY_SIZE(keysym2code->keycodes)) {
|
|
|
|
keysym2code->keycodes[keysym2code->count++] = keycode;
|
|
|
|
} else {
|
|
|
|
warn_report("more than %zd keycodes for keysym %d",
|
|
|
|
ARRAY_SIZE(keysym2code->keycodes), keysym);
|
|
|
|
}
|
2018-02-22 15:05:10 +08:00
|
|
|
return;
|
2010-03-01 04:03:00 +08:00
|
|
|
}
|
2018-02-22 15:05:10 +08:00
|
|
|
|
|
|
|
keysym2code = g_new0(struct keysym2code, 1);
|
2018-02-22 15:05:12 +08:00
|
|
|
keysym2code->keycodes[0] = keycode;
|
|
|
|
keysym2code->count = 1;
|
2018-02-22 15:05:10 +08:00
|
|
|
g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code);
|
|
|
|
trace_keymap_add(keysym, keycode, line);
|
2010-03-01 04:03:00 +08:00
|
|
|
}
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
|
2014-12-08 19:39:05 +08:00
|
|
|
const char *language,
|
|
|
|
kbd_layout_t *k)
|
2004-12-13 00:56:30 +08:00
|
|
|
{
|
|
|
|
FILE *f;
|
2009-05-30 07:52:44 +08:00
|
|
|
char * filename;
|
2004-12-13 00:56:30 +08:00
|
|
|
char line[1024];
|
2017-06-06 21:47:36 +08:00
|
|
|
char keyname[64];
|
2004-12-13 00:56:30 +08:00
|
|
|
int len;
|
|
|
|
|
2009-05-30 07:52:44 +08:00
|
|
|
filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
|
2017-06-06 21:47:36 +08:00
|
|
|
trace_keymap_parse(filename);
|
2011-11-11 17:40:06 +08:00
|
|
|
f = filename ? fopen(filename, "r") : NULL;
|
|
|
|
g_free(filename);
|
|
|
|
if (!f) {
|
2014-12-08 19:39:05 +08:00
|
|
|
fprintf(stderr, "Could not read keymap file: '%s'\n", language);
|
|
|
|
return NULL;
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
2011-11-11 17:40:06 +08:00
|
|
|
|
2014-12-08 19:39:05 +08:00
|
|
|
if (!k) {
|
2015-11-04 00:12:03 +08:00
|
|
|
k = g_new0(kbd_layout_t, 1);
|
2018-02-22 15:05:10 +08:00
|
|
|
k->hash = g_hash_table_new(NULL, NULL);
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2011-11-11 17:40:06 +08:00
|
|
|
|
2004-12-13 00:56:30 +08:00
|
|
|
for(;;) {
|
2014-12-08 19:39:05 +08:00
|
|
|
if (fgets(line, 1024, f) == NULL) {
|
2004-12-13 00:56:30 +08:00
|
|
|
break;
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2004-12-13 00:56:30 +08:00
|
|
|
len = strlen(line);
|
2014-12-08 19:39:05 +08:00
|
|
|
if (len > 0 && line[len - 1] == '\n') {
|
2004-12-13 00:56:30 +08:00
|
|
|
line[len - 1] = '\0';
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
|
|
|
if (line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(line, "map ", 4)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(line, "include ", 8)) {
|
|
|
|
parse_keyboard_layout(table, line + 8, k);
|
2004-12-13 00:56:30 +08:00
|
|
|
} else {
|
2017-06-06 21:47:36 +08:00
|
|
|
int offset = 0;
|
|
|
|
while (line[offset] != 0 &&
|
|
|
|
line[offset] != ' ' &&
|
|
|
|
offset < sizeof(keyname) - 1) {
|
|
|
|
keyname[offset] = line[offset];
|
|
|
|
offset++;
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2017-06-06 21:47:36 +08:00
|
|
|
keyname[offset] = 0;
|
|
|
|
if (strlen(keyname)) {
|
2014-12-08 19:39:05 +08:00
|
|
|
int keysym;
|
2017-06-06 21:47:36 +08:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 19:39:05 +08:00
|
|
|
if (keysym == 0) {
|
2017-09-12 03:52:50 +08:00
|
|
|
/* warn_report("unknown keysym %s", line);*/
|
2014-12-08 19:39:05 +08:00
|
|
|
} else {
|
2017-06-06 21:47:36 +08:00
|
|
|
const char *rest = line + offset + 1;
|
2013-01-17 01:20:57 +08:00
|
|
|
int keycode = strtol(rest, NULL, 0);
|
2007-10-31 06:38:53 +08:00
|
|
|
|
2013-01-17 01:20:57 +08:00
|
|
|
if (strstr(rest, "shift")) {
|
2014-12-08 19:39:05 +08:00
|
|
|
keycode |= SCANCODE_SHIFT;
|
2013-01-17 01:20:57 +08:00
|
|
|
}
|
|
|
|
if (strstr(rest, "altgr")) {
|
2014-12-08 19:39:05 +08:00
|
|
|
keycode |= SCANCODE_ALTGR;
|
2013-01-17 01:20:57 +08:00
|
|
|
}
|
|
|
|
if (strstr(rest, "ctrl")) {
|
2014-12-08 19:39:05 +08:00
|
|
|
keycode |= SCANCODE_CTRL;
|
2013-01-17 01:20:57 +08:00
|
|
|
}
|
2010-03-01 04:03:00 +08:00
|
|
|
|
2014-12-08 19:39:05 +08:00
|
|
|
add_keysym(line, keysym, keycode, k);
|
2010-03-01 04:03:00 +08:00
|
|
|
|
2013-01-17 01:20:57 +08:00
|
|
|
if (strstr(rest, "addupper")) {
|
2014-12-08 19:39:05 +08:00
|
|
|
char *c;
|
2017-06-06 21:47:36 +08:00
|
|
|
for (c = keyname; *c; c++) {
|
2014-12-08 19:39:05 +08:00
|
|
|
*c = qemu_toupper(*c);
|
|
|
|
}
|
2017-06-06 21:47:36 +08:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 19:39:05 +08:00
|
|
|
if (keysym) {
|
|
|
|
add_keysym(line, keysym,
|
|
|
|
keycode | SCANCODE_SHIFT, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2009-03-07 04:27:10 +08:00
|
|
|
|
2018-02-22 15:05:09 +08:00
|
|
|
kbd_layout_t *init_keyboard_layout(const name2keysym_t *table,
|
|
|
|
const char *language)
|
2004-12-13 00:56:30 +08:00
|
|
|
{
|
2009-08-01 05:16:51 +08:00
|
|
|
return parse_keyboard_layout(table, language, NULL);
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
|
|
|
|
2009-03-07 04:27:10 +08:00
|
|
|
|
2018-02-22 15:05:13 +08:00
|
|
|
int keysym2scancode(kbd_layout_t *k, int keysym,
|
|
|
|
bool shift, bool altgr, bool ctrl)
|
2004-12-13 00:56:30 +08:00
|
|
|
{
|
2018-02-22 15:05:13 +08:00
|
|
|
static const uint32_t mask =
|
|
|
|
SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL;
|
|
|
|
uint32_t mods, i;
|
2018-02-22 15:05:10 +08:00
|
|
|
struct keysym2code *keysym2code;
|
|
|
|
|
2004-12-13 00:56:30 +08:00
|
|
|
#ifdef XK_ISO_Left_Tab
|
2018-02-22 15:05:10 +08:00
|
|
|
if (keysym == XK_ISO_Left_Tab) {
|
|
|
|
keysym = XK_Tab;
|
|
|
|
}
|
2004-12-13 00:56:30 +08:00
|
|
|
#endif
|
2018-02-22 15:05:10 +08:00
|
|
|
|
|
|
|
keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym));
|
|
|
|
if (!keysym2code) {
|
|
|
|
trace_keymap_unmapped(keysym);
|
|
|
|
warn_report("no scancode found for keysym %d", keysym);
|
|
|
|
return 0;
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
2018-02-22 15:05:10 +08:00
|
|
|
|
2018-02-22 15:05:13 +08:00
|
|
|
if (keysym2code->count == 1) {
|
|
|
|
return keysym2code->keycodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have multiple keysym -> keycode mappings.
|
|
|
|
*
|
|
|
|
* Check whenever we find one mapping where the modifier state of
|
|
|
|
* the mapping matches the current user interface modifier state.
|
|
|
|
* If so, prefer that one.
|
|
|
|
*/
|
|
|
|
mods = 0;
|
|
|
|
if (shift) {
|
|
|
|
mods |= SCANCODE_SHIFT;
|
|
|
|
}
|
|
|
|
if (altgr) {
|
|
|
|
mods |= SCANCODE_ALTGR;
|
|
|
|
}
|
|
|
|
if (ctrl) {
|
|
|
|
mods |= SCANCODE_CTRL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < keysym2code->count; i++) {
|
|
|
|
if ((keysym2code->keycodes[i] & mask) == mods) {
|
|
|
|
return keysym2code->keycodes[i];
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 15:05:12 +08:00
|
|
|
return keysym2code->keycodes[0];
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
2007-10-31 06:38:53 +08:00
|
|
|
|
2018-02-22 15:05:09 +08:00
|
|
|
int keycode_is_keypad(kbd_layout_t *k, int keycode)
|
2007-10-31 06:38:53 +08:00
|
|
|
{
|
2018-02-22 15:05:11 +08:00
|
|
|
if (keycode >= 0x47 && keycode <= 0x53) {
|
|
|
|
return true;
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2018-02-22 15:05:11 +08:00
|
|
|
return false;
|
2007-10-31 06:38:53 +08:00
|
|
|
}
|
|
|
|
|
2018-02-22 15:05:09 +08:00
|
|
|
int keysym_is_numlock(kbd_layout_t *k, int keysym)
|
2007-10-31 06:38:53 +08:00
|
|
|
{
|
2018-02-22 15:05:11 +08:00
|
|
|
switch (keysym) {
|
|
|
|
case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */
|
|
|
|
case 0xffac: /* KP_Separator */
|
|
|
|
case 0xffae: /* KP_Decimal */
|
|
|
|
return true;
|
2014-12-08 19:39:05 +08:00
|
|
|
}
|
2018-02-22 15:05:11 +08:00
|
|
|
return false;
|
2007-10-31 06:38:53 +08:00
|
|
|
}
|