2004-12-12 19:56:30 +03:00
|
|
|
/*
|
|
|
|
* QEMU keysym to keycode conversion using rdesktop keymaps
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-12 19:56:30 +03:00
|
|
|
* Copyright (c) 2004 Johannes Schindelin
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-12 19:56:30 +03: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-29 20:49:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2009-03-06 23:27:10 +03:00
|
|
|
#include "keymaps.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
2017-06-06 16:47:36 +03:00
|
|
|
#include "trace.h"
|
2017-09-11 22:52:53 +03:00
|
|
|
#include "qemu/error-report.h"
|
2018-10-17 11:26:50 +03:00
|
|
|
#include "qapi/error.h"
|
2019-01-22 12:28:14 +03:00
|
|
|
#include "ui/input.h"
|
2009-03-06 23:27:10 +03:00
|
|
|
|
2018-02-22 10:05:10 +03:00
|
|
|
struct keysym2code {
|
2018-02-22 10:05:12 +03:00
|
|
|
uint32_t count;
|
|
|
|
uint16_t keycodes[4];
|
2018-02-22 10:05:10 +03:00
|
|
|
};
|
|
|
|
|
2018-02-22 10:05:09 +03:00
|
|
|
struct kbd_layout_t {
|
2018-02-22 10:05:10 +03:00
|
|
|
GHashTable *hash;
|
2018-02-22 10:05:09 +03:00
|
|
|
};
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static int get_keysym(const name2keysym_t *table,
|
2014-12-08 14:39:05 +03:00
|
|
|
const char *name)
|
2004-12-12 19:56:30 +03:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
const name2keysym_t *p;
|
2009-03-06 23:27:10 +03:00
|
|
|
for(p = table; p->name != NULL; p++) {
|
2014-12-08 14:39:05 +03:00
|
|
|
if (!strcmp(p->name, name)) {
|
2004-12-12 19:56:30 +03:00
|
|
|
return p->keysym;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2004-12-12 19:56:30 +03:00
|
|
|
}
|
2013-10-16 16:40:05 +04:00
|
|
|
if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
|
|
|
|
char *end;
|
|
|
|
int ret = (int)strtoul(name + 1, &end, 16);
|
2014-12-08 14:39:05 +03:00
|
|
|
if (*end == '\0' && ret > 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-16 16:40:05 +04:00
|
|
|
}
|
2004-12-12 19:56:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-22 10:05:10 +03: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 10:05:12 +03: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 10:05:10 +03:00
|
|
|
return;
|
2010-02-28 23:03:00 +03:00
|
|
|
}
|
2018-02-22 10:05:10 +03:00
|
|
|
|
|
|
|
keysym2code = g_new0(struct keysym2code, 1);
|
2018-02-22 10:05:12 +03:00
|
|
|
keysym2code->keycodes[0] = keycode;
|
|
|
|
keysym2code->count = 1;
|
2018-02-22 10:05:10 +03:00
|
|
|
g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code);
|
|
|
|
trace_keymap_add(keysym, keycode, line);
|
2010-02-28 23:03:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 11:26:49 +03:00
|
|
|
static int parse_keyboard_layout(kbd_layout_t *k,
|
|
|
|
const name2keysym_t *table,
|
2018-10-17 11:26:50 +03:00
|
|
|
const char *language, Error **errp)
|
2004-12-12 19:56:30 +03:00
|
|
|
{
|
2018-10-17 11:26:49 +03:00
|
|
|
int ret;
|
2004-12-12 19:56:30 +03:00
|
|
|
FILE *f;
|
2009-05-30 03:52:44 +04:00
|
|
|
char * filename;
|
2004-12-12 19:56:30 +03:00
|
|
|
char line[1024];
|
2017-06-06 16:47:36 +03:00
|
|
|
char keyname[64];
|
2004-12-12 19:56:30 +03:00
|
|
|
int len;
|
|
|
|
|
2009-05-30 03:52:44 +04:00
|
|
|
filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
|
2017-06-06 16:47:36 +03:00
|
|
|
trace_keymap_parse(filename);
|
2011-11-11 13:40:06 +04:00
|
|
|
f = filename ? fopen(filename, "r") : NULL;
|
|
|
|
g_free(filename);
|
|
|
|
if (!f) {
|
2018-10-17 11:26:50 +03:00
|
|
|
error_setg(errp, "could not read keymap file: '%s'", language);
|
2018-10-17 11:26:49 +03:00
|
|
|
return -1;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2011-11-11 13:40:06 +04:00
|
|
|
|
2004-12-12 19:56:30 +03:00
|
|
|
for(;;) {
|
2014-12-08 14:39:05 +03:00
|
|
|
if (fgets(line, 1024, f) == NULL) {
|
2004-12-12 19:56:30 +03:00
|
|
|
break;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2004-12-12 19:56:30 +03:00
|
|
|
len = strlen(line);
|
2014-12-08 14:39:05 +03:00
|
|
|
if (len > 0 && line[len - 1] == '\n') {
|
2004-12-12 19:56:30 +03:00
|
|
|
line[len - 1] = '\0';
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
|
|
|
if (line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(line, "map ", 4)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(line, "include ", 8)) {
|
2018-11-16 13:43:19 +03:00
|
|
|
error_setg(errp, "keymap include files are not supported any more");
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2004-12-12 19:56:30 +03:00
|
|
|
} else {
|
2017-06-06 16:47:36 +03:00
|
|
|
int offset = 0;
|
|
|
|
while (line[offset] != 0 &&
|
|
|
|
line[offset] != ' ' &&
|
|
|
|
offset < sizeof(keyname) - 1) {
|
|
|
|
keyname[offset] = line[offset];
|
|
|
|
offset++;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2017-06-06 16:47:36 +03:00
|
|
|
keyname[offset] = 0;
|
|
|
|
if (strlen(keyname)) {
|
2014-12-08 14:39:05 +03:00
|
|
|
int keysym;
|
2017-06-06 16:47:36 +03:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 14:39:05 +03:00
|
|
|
if (keysym == 0) {
|
2017-09-11 22:52:50 +03:00
|
|
|
/* warn_report("unknown keysym %s", line);*/
|
2014-12-08 14:39:05 +03:00
|
|
|
} else {
|
2017-06-06 16:47:36 +03:00
|
|
|
const char *rest = line + offset + 1;
|
2013-01-16 21:20:57 +04:00
|
|
|
int keycode = strtol(rest, NULL, 0);
|
2007-10-31 01:38:53 +03:00
|
|
|
|
2013-01-16 21:20:57 +04:00
|
|
|
if (strstr(rest, "shift")) {
|
2014-12-08 14:39:05 +03:00
|
|
|
keycode |= SCANCODE_SHIFT;
|
2013-01-16 21:20:57 +04:00
|
|
|
}
|
|
|
|
if (strstr(rest, "altgr")) {
|
2014-12-08 14:39:05 +03:00
|
|
|
keycode |= SCANCODE_ALTGR;
|
2013-01-16 21:20:57 +04:00
|
|
|
}
|
|
|
|
if (strstr(rest, "ctrl")) {
|
2014-12-08 14:39:05 +03:00
|
|
|
keycode |= SCANCODE_CTRL;
|
2013-01-16 21:20:57 +04:00
|
|
|
}
|
2010-02-28 23:03:00 +03:00
|
|
|
|
2014-12-08 14:39:05 +03:00
|
|
|
add_keysym(line, keysym, keycode, k);
|
2010-02-28 23:03:00 +03:00
|
|
|
|
2013-01-16 21:20:57 +04:00
|
|
|
if (strstr(rest, "addupper")) {
|
2014-12-08 14:39:05 +03:00
|
|
|
char *c;
|
2017-06-06 16:47:36 +03:00
|
|
|
for (c = keyname; *c; c++) {
|
2014-12-08 14:39:05 +03:00
|
|
|
*c = qemu_toupper(*c);
|
|
|
|
}
|
2017-06-06 16:47:36 +03:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 14:39:05 +03:00
|
|
|
if (keysym) {
|
|
|
|
add_keysym(line, keysym,
|
|
|
|
keycode | SCANCODE_SHIFT, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-12 19:56:30 +03:00
|
|
|
}
|
2018-10-17 11:26:49 +03:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
out:
|
2004-12-12 19:56:30 +03:00
|
|
|
fclose(f);
|
2018-10-17 11:26:49 +03:00
|
|
|
return ret;
|
2004-12-12 19:56:30 +03:00
|
|
|
}
|
|
|
|
|
2009-03-06 23:27:10 +03:00
|
|
|
|
2018-02-22 10:05:09 +03:00
|
|
|
kbd_layout_t *init_keyboard_layout(const name2keysym_t *table,
|
2018-10-17 11:26:50 +03:00
|
|
|
const char *language, Error **errp)
|
2004-12-12 19:56:30 +03:00
|
|
|
{
|
2018-10-17 11:26:49 +03:00
|
|
|
kbd_layout_t *k;
|
|
|
|
|
|
|
|
k = g_new0(kbd_layout_t, 1);
|
|
|
|
k->hash = g_hash_table_new(NULL, NULL);
|
2018-10-17 11:26:50 +03:00
|
|
|
if (parse_keyboard_layout(k, table, language, errp) < 0) {
|
2018-10-17 11:26:49 +03:00
|
|
|
g_hash_table_unref(k->hash);
|
|
|
|
g_free(k);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return k;
|
2004-12-12 19:56:30 +03:00
|
|
|
}
|
|
|
|
|
2009-03-06 23:27:10 +03:00
|
|
|
|
2018-02-22 10:05:13 +03:00
|
|
|
int keysym2scancode(kbd_layout_t *k, int keysym,
|
2019-01-22 12:28:14 +03:00
|
|
|
QKbdState *kbd, bool down)
|
2004-12-12 19:56:30 +03:00
|
|
|
{
|
2018-02-22 10:05:13 +03:00
|
|
|
static const uint32_t mask =
|
|
|
|
SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL;
|
|
|
|
uint32_t mods, i;
|
2018-02-22 10:05:10 +03:00
|
|
|
struct keysym2code *keysym2code;
|
|
|
|
|
2004-12-12 19:56:30 +03:00
|
|
|
#ifdef XK_ISO_Left_Tab
|
2018-02-22 10:05:10 +03:00
|
|
|
if (keysym == XK_ISO_Left_Tab) {
|
|
|
|
keysym = XK_Tab;
|
|
|
|
}
|
2004-12-12 19:56:30 +03:00
|
|
|
#endif
|
2018-02-22 10:05:10 +03: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-12 19:56:30 +03:00
|
|
|
}
|
2018-02-22 10:05:10 +03:00
|
|
|
|
2018-02-22 10:05:13 +03:00
|
|
|
if (keysym2code->count == 1) {
|
|
|
|
return keysym2code->keycodes[0];
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:28:14 +03:00
|
|
|
/* We have multiple keysym -> keycode mappings. */
|
|
|
|
if (down) {
|
|
|
|
/*
|
|
|
|
* On keydown: 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 (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_SHIFT)) {
|
|
|
|
mods |= SCANCODE_SHIFT;
|
|
|
|
}
|
|
|
|
if (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_ALTGR)) {
|
|
|
|
mods |= SCANCODE_ALTGR;
|
|
|
|
}
|
|
|
|
if (kbd && qkbd_state_modifier_get(kbd, QKBD_MOD_CTRL)) {
|
|
|
|
mods |= SCANCODE_CTRL;
|
|
|
|
}
|
2018-02-22 10:05:13 +03:00
|
|
|
|
2019-01-22 12:28:14 +03:00
|
|
|
for (i = 0; i < keysym2code->count; i++) {
|
|
|
|
if ((keysym2code->keycodes[i] & mask) == mods) {
|
|
|
|
return keysym2code->keycodes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* On keyup: Try find a key which is actually down.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < keysym2code->count; i++) {
|
|
|
|
QKeyCode qcode = qemu_input_key_number_to_qcode
|
|
|
|
(keysym2code->keycodes[i]);
|
|
|
|
if (kbd && qkbd_state_key_get(kbd, qcode)) {
|
|
|
|
return keysym2code->keycodes[i];
|
|
|
|
}
|
2018-02-22 10:05:13 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 10:05:12 +03:00
|
|
|
return keysym2code->keycodes[0];
|
2004-12-12 19:56:30 +03:00
|
|
|
}
|
2007-10-31 01:38:53 +03:00
|
|
|
|
2018-02-22 10:05:09 +03:00
|
|
|
int keycode_is_keypad(kbd_layout_t *k, int keycode)
|
2007-10-31 01:38:53 +03:00
|
|
|
{
|
2018-02-22 10:05:11 +03:00
|
|
|
if (keycode >= 0x47 && keycode <= 0x53) {
|
|
|
|
return true;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2018-02-22 10:05:11 +03:00
|
|
|
return false;
|
2007-10-31 01:38:53 +03:00
|
|
|
}
|
|
|
|
|
2018-02-22 10:05:09 +03:00
|
|
|
int keysym_is_numlock(kbd_layout_t *k, int keysym)
|
2007-10-31 01:38:53 +03:00
|
|
|
{
|
2018-02-22 10:05:11 +03:00
|
|
|
switch (keysym) {
|
|
|
|
case 0xffb0 ... 0xffb9: /* KP_0 .. KP_9 */
|
|
|
|
case 0xffac: /* KP_Separator */
|
|
|
|
case 0xffae: /* KP_Decimal */
|
|
|
|
return true;
|
2014-12-08 14:39:05 +03:00
|
|
|
}
|
2018-02-22 10:05:11 +03:00
|
|
|
return false;
|
2007-10-31 01:38:53 +03:00
|
|
|
}
|