2004-12-12 17:56:30 +01:00
|
|
|
/*
|
|
|
|
* QEMU keysym to keycode conversion using rdesktop keymaps
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-12-12 17:56:30 +01:00
|
|
|
* Copyright (c) 2004 Johannes Schindelin
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-12-12 17:56:30 +01: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 18:49:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2009-03-06 21:27:10 +01:00
|
|
|
#include "keymaps.h"
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/sysemu.h"
|
2017-06-06 15:47:36 +02:00
|
|
|
#include "trace.h"
|
2017-09-11 21:52:53 +02:00
|
|
|
#include "qemu/error-report.h"
|
2009-03-06 21:27:10 +01:00
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static int get_keysym(const name2keysym_t *table,
|
2014-12-08 12:39:05 +01:00
|
|
|
const char *name)
|
2004-12-12 17:56:30 +01:00
|
|
|
{
|
2009-10-01 23:12:16 +02:00
|
|
|
const name2keysym_t *p;
|
2009-03-06 21:27:10 +01:00
|
|
|
for(p = table; p->name != NULL; p++) {
|
2014-12-08 12:39:05 +01:00
|
|
|
if (!strcmp(p->name, name)) {
|
2004-12-12 17:56:30 +01:00
|
|
|
return p->keysym;
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
}
|
2013-10-16 14:40:05 +02:00
|
|
|
if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
|
|
|
|
char *end;
|
|
|
|
int ret = (int)strtoul(name + 1, &end, 16);
|
2014-12-08 12:39:05 +01:00
|
|
|
if (*end == '\0' && ret > 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-16 14:40:05 +02:00
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-30 23:38:53 +01:00
|
|
|
static void add_to_key_range(struct key_range **krp, int code) {
|
|
|
|
struct key_range *kr;
|
|
|
|
for (kr = *krp; kr; kr = kr->next) {
|
2014-12-08 12:39:05 +01:00
|
|
|
if (code >= kr->start && code <= kr->end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (code == kr->start - 1) {
|
|
|
|
kr->start--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (code == kr->end + 1) {
|
|
|
|
kr->end++;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-30 23:38:53 +01:00
|
|
|
}
|
|
|
|
if (kr == NULL) {
|
2014-12-08 12:39:05 +01:00
|
|
|
kr = g_malloc0(sizeof(*kr));
|
2009-02-05 23:06:18 +01:00
|
|
|
kr->start = kr->end = code;
|
|
|
|
kr->next = *krp;
|
|
|
|
*krp = kr;
|
2007-10-30 23:38:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 21:03:00 +01:00
|
|
|
static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
|
|
|
|
if (keysym < MAX_NORMAL_KEYCODE) {
|
2017-06-06 15:47:36 +02:00
|
|
|
trace_keymap_add("normal", keysym, keycode, line);
|
2014-12-08 12:39:05 +01:00
|
|
|
k->keysym2keycode[keysym] = keycode;
|
2010-02-28 21:03:00 +01:00
|
|
|
} else {
|
2014-12-08 12:39:05 +01:00
|
|
|
if (k->extra_count >= MAX_EXTRA_COUNT) {
|
2017-09-11 21:52:53 +02:00
|
|
|
warn_report("Could not assign keysym %s (0x%x)"
|
|
|
|
" because of memory constraints.", line, keysym);
|
2014-12-08 12:39:05 +01:00
|
|
|
} else {
|
2017-06-06 15:47:36 +02:00
|
|
|
trace_keymap_add("extra", keysym, keycode, line);
|
2014-12-08 12:39:05 +01:00
|
|
|
k->keysym2keycode_extra[k->extra_count].
|
|
|
|
keysym = keysym;
|
|
|
|
k->keysym2keycode_extra[k->extra_count].
|
|
|
|
keycode = keycode;
|
|
|
|
k->extra_count++;
|
|
|
|
}
|
2010-02-28 21:03:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
|
2014-12-08 12:39:05 +01:00
|
|
|
const char *language,
|
|
|
|
kbd_layout_t *k)
|
2004-12-12 17:56:30 +01:00
|
|
|
{
|
|
|
|
FILE *f;
|
2009-05-30 01:52:44 +02:00
|
|
|
char * filename;
|
2004-12-12 17:56:30 +01:00
|
|
|
char line[1024];
|
2017-06-06 15:47:36 +02:00
|
|
|
char keyname[64];
|
2004-12-12 17:56:30 +01:00
|
|
|
int len;
|
|
|
|
|
2009-05-30 01:52:44 +02:00
|
|
|
filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
|
2017-06-06 15:47:36 +02:00
|
|
|
trace_keymap_parse(filename);
|
2011-11-11 10:40:06 +01:00
|
|
|
f = filename ? fopen(filename, "r") : NULL;
|
|
|
|
g_free(filename);
|
|
|
|
if (!f) {
|
2014-12-08 12:39:05 +01:00
|
|
|
fprintf(stderr, "Could not read keymap file: '%s'\n", language);
|
|
|
|
return NULL;
|
2004-12-12 17:56:30 +01:00
|
|
|
}
|
2011-11-11 10:40:06 +01:00
|
|
|
|
2014-12-08 12:39:05 +01:00
|
|
|
if (!k) {
|
2015-11-03 17:12:03 +01:00
|
|
|
k = g_new0(kbd_layout_t, 1);
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
2011-11-11 10:40:06 +01:00
|
|
|
|
2004-12-12 17:56:30 +01:00
|
|
|
for(;;) {
|
2014-12-08 12:39:05 +01:00
|
|
|
if (fgets(line, 1024, f) == NULL) {
|
2004-12-12 17:56:30 +01:00
|
|
|
break;
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
len = strlen(line);
|
2014-12-08 12:39:05 +01:00
|
|
|
if (len > 0 && line[len - 1] == '\n') {
|
2004-12-12 17:56:30 +01:00
|
|
|
line[len - 1] = '\0';
|
2014-12-08 12:39:05 +01: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-12 17:56:30 +01:00
|
|
|
} else {
|
2017-06-06 15:47:36 +02:00
|
|
|
int offset = 0;
|
|
|
|
while (line[offset] != 0 &&
|
|
|
|
line[offset] != ' ' &&
|
|
|
|
offset < sizeof(keyname) - 1) {
|
|
|
|
keyname[offset] = line[offset];
|
|
|
|
offset++;
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
2017-06-06 15:47:36 +02:00
|
|
|
keyname[offset] = 0;
|
|
|
|
if (strlen(keyname)) {
|
2014-12-08 12:39:05 +01:00
|
|
|
int keysym;
|
2017-06-06 15:47:36 +02:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 12:39:05 +01:00
|
|
|
if (keysym == 0) {
|
2017-09-11 21:52:50 +02:00
|
|
|
/* warn_report("unknown keysym %s", line);*/
|
2014-12-08 12:39:05 +01:00
|
|
|
} else {
|
2017-06-06 15:47:36 +02:00
|
|
|
const char *rest = line + offset + 1;
|
2013-01-16 18:20:57 +01:00
|
|
|
int keycode = strtol(rest, NULL, 0);
|
2007-10-30 23:38:53 +01:00
|
|
|
|
2013-01-16 18:20:57 +01:00
|
|
|
if (strstr(rest, "numlock")) {
|
2014-12-08 12:39:05 +01:00
|
|
|
add_to_key_range(&k->keypad_range, keycode);
|
|
|
|
add_to_key_range(&k->numlock_range, keysym);
|
|
|
|
/* fprintf(stderr, "keypad keysym %04x keycode %d\n",
|
|
|
|
keysym, keycode); */
|
|
|
|
}
|
2007-10-30 23:38:53 +01:00
|
|
|
|
2013-01-16 18:20:57 +01:00
|
|
|
if (strstr(rest, "shift")) {
|
2014-12-08 12:39:05 +01:00
|
|
|
keycode |= SCANCODE_SHIFT;
|
2013-01-16 18:20:57 +01:00
|
|
|
}
|
|
|
|
if (strstr(rest, "altgr")) {
|
2014-12-08 12:39:05 +01:00
|
|
|
keycode |= SCANCODE_ALTGR;
|
2013-01-16 18:20:57 +01:00
|
|
|
}
|
|
|
|
if (strstr(rest, "ctrl")) {
|
2014-12-08 12:39:05 +01:00
|
|
|
keycode |= SCANCODE_CTRL;
|
2013-01-16 18:20:57 +01:00
|
|
|
}
|
2010-02-28 21:03:00 +01:00
|
|
|
|
2014-12-08 12:39:05 +01:00
|
|
|
add_keysym(line, keysym, keycode, k);
|
2010-02-28 21:03:00 +01:00
|
|
|
|
2013-01-16 18:20:57 +01:00
|
|
|
if (strstr(rest, "addupper")) {
|
2014-12-08 12:39:05 +01:00
|
|
|
char *c;
|
2017-06-06 15:47:36 +02:00
|
|
|
for (c = keyname; *c; c++) {
|
2014-12-08 12:39:05 +01:00
|
|
|
*c = qemu_toupper(*c);
|
|
|
|
}
|
2017-06-06 15:47:36 +02:00
|
|
|
keysym = get_keysym(table, keyname);
|
2014-12-08 12:39:05 +01:00
|
|
|
if (keysym) {
|
|
|
|
add_keysym(line, keysym,
|
|
|
|
keycode | SCANCODE_SHIFT, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2009-03-06 21:27:10 +01:00
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
void *init_keyboard_layout(const name2keysym_t *table, const char *language)
|
2004-12-12 17:56:30 +01:00
|
|
|
{
|
2009-07-31 23:16:51 +02:00
|
|
|
return parse_keyboard_layout(table, language, NULL);
|
2004-12-12 17:56:30 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 21:27:10 +01:00
|
|
|
|
|
|
|
int keysym2scancode(void *kbd_layout, int keysym)
|
2004-12-12 17:56:30 +01:00
|
|
|
{
|
2009-10-01 23:12:16 +02:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2004-12-12 17:56:30 +01:00
|
|
|
if (keysym < MAX_NORMAL_KEYCODE) {
|
2014-12-08 12:39:05 +01:00
|
|
|
if (k->keysym2keycode[keysym] == 0) {
|
2017-06-06 15:47:36 +02:00
|
|
|
trace_keymap_unmapped(keysym);
|
2017-09-11 21:52:56 +02:00
|
|
|
warn_report("no scancode found for keysym %d", keysym);
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
|
|
|
return k->keysym2keycode[keysym];
|
2004-12-12 17:56:30 +01:00
|
|
|
} else {
|
2014-12-08 12:39:05 +01:00
|
|
|
int i;
|
2004-12-12 17:56:30 +01:00
|
|
|
#ifdef XK_ISO_Left_Tab
|
2014-12-08 12:39:05 +01:00
|
|
|
if (keysym == XK_ISO_Left_Tab) {
|
|
|
|
keysym = XK_Tab;
|
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
#endif
|
2014-12-08 12:39:05 +01:00
|
|
|
for (i = 0; i < k->extra_count; i++) {
|
|
|
|
if (k->keysym2keycode_extra[i].keysym == keysym) {
|
|
|
|
return k->keysym2keycode_extra[i].keycode;
|
|
|
|
}
|
|
|
|
}
|
2004-12-12 17:56:30 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-30 23:38:53 +01:00
|
|
|
|
2009-03-06 21:27:10 +01:00
|
|
|
int keycode_is_keypad(void *kbd_layout, int keycode)
|
2007-10-30 23:38:53 +01:00
|
|
|
{
|
2009-10-01 23:12:16 +02:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2007-10-30 23:38:53 +01:00
|
|
|
struct key_range *kr;
|
|
|
|
|
2014-12-08 12:39:05 +01:00
|
|
|
for (kr = k->keypad_range; kr; kr = kr->next) {
|
|
|
|
if (keycode >= kr->start && keycode <= kr->end) {
|
2007-10-30 23:38:53 +01:00
|
|
|
return 1;
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2007-10-30 23:38:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-06 21:27:10 +01:00
|
|
|
int keysym_is_numlock(void *kbd_layout, int keysym)
|
2007-10-30 23:38:53 +01:00
|
|
|
{
|
2009-10-01 23:12:16 +02:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2007-10-30 23:38:53 +01:00
|
|
|
struct key_range *kr;
|
|
|
|
|
2014-12-08 12:39:05 +01:00
|
|
|
for (kr = k->numlock_range; kr; kr = kr->next) {
|
|
|
|
if (keysym >= kr->start && keysym <= kr->end) {
|
2007-10-30 23:38:53 +01:00
|
|
|
return 1;
|
2014-12-08 12:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2007-10-30 23:38:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|