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.
|
|
|
|
*/
|
|
|
|
|
2009-03-07 04:27:10 +08:00
|
|
|
#include "keymaps.h"
|
|
|
|
#include "sysemu.h"
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static int get_keysym(const name2keysym_t *table,
|
2009-03-07 04:27:10 +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++) {
|
2004-12-13 00:56:30 +08:00
|
|
|
if (!strcmp(p->name, name))
|
|
|
|
return p->keysym;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-31 06:38:53 +08:00
|
|
|
static void add_to_key_range(struct key_range **krp, int code) {
|
|
|
|
struct key_range *kr;
|
|
|
|
for (kr = *krp; kr; kr = kr->next) {
|
|
|
|
if (code >= kr->start && code <= kr->end)
|
|
|
|
break;
|
|
|
|
if (code == kr->start - 1) {
|
|
|
|
kr->start--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (code == kr->end + 1) {
|
|
|
|
kr->end++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kr == NULL) {
|
|
|
|
kr = qemu_mallocz(sizeof(*kr));
|
2009-02-06 06:06:18 +08:00
|
|
|
kr->start = kr->end = code;
|
|
|
|
kr->next = *krp;
|
|
|
|
*krp = kr;
|
2007-10-31 06:38:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-01 04:03:00 +08:00
|
|
|
static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
|
|
|
|
if (keysym < MAX_NORMAL_KEYCODE) {
|
|
|
|
//fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
|
|
|
|
k->keysym2keycode[keysym] = keycode;
|
|
|
|
} else {
|
|
|
|
if (k->extra_count >= MAX_EXTRA_COUNT) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
|
|
|
|
line, keysym);
|
|
|
|
} else {
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "Setting %d: %d,%d\n",
|
|
|
|
k->extra_count, keysym, keycode);
|
|
|
|
#endif
|
|
|
|
k->keysym2keycode_extra[k->extra_count].
|
|
|
|
keysym = keysym;
|
|
|
|
k->keysym2keycode_extra[k->extra_count].
|
|
|
|
keycode = keycode;
|
|
|
|
k->extra_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
|
2009-03-07 04:27:10 +08:00
|
|
|
const char *language,
|
2009-10-02 05:12:16 +08:00
|
|
|
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];
|
|
|
|
int len;
|
|
|
|
|
2009-05-30 07:52:44 +08:00
|
|
|
filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
|
2004-12-13 00:56:30 +08:00
|
|
|
|
|
|
|
if (!k)
|
2009-10-02 05:12:16 +08:00
|
|
|
k = qemu_mallocz(sizeof(kbd_layout_t));
|
2009-05-30 07:52:44 +08:00
|
|
|
if (!(filename && (f = fopen(filename, "r")))) {
|
2004-12-13 00:56:30 +08:00
|
|
|
fprintf(stderr,
|
2009-05-30 07:52:44 +08:00
|
|
|
"Could not read keymap file: '%s'\n", language);
|
2009-08-01 05:16:51 +08:00
|
|
|
return NULL;
|
2004-12-13 00:56:30 +08:00
|
|
|
}
|
2009-05-30 07:52:44 +08:00
|
|
|
qemu_free(filename);
|
2004-12-13 00:56:30 +08:00
|
|
|
for(;;) {
|
|
|
|
if (fgets(line, 1024, f) == NULL)
|
|
|
|
break;
|
|
|
|
len = strlen(line);
|
|
|
|
if (len > 0 && line[len - 1] == '\n')
|
|
|
|
line[len - 1] = '\0';
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
if (!strncmp(line, "map ", 4))
|
|
|
|
continue;
|
|
|
|
if (!strncmp(line, "include ", 8)) {
|
2009-03-07 04:27:10 +08:00
|
|
|
parse_keyboard_layout(table, line + 8, k);
|
2004-12-13 00:56:30 +08:00
|
|
|
} else {
|
|
|
|
char *end_of_keysym = line;
|
|
|
|
while (*end_of_keysym != 0 && *end_of_keysym != ' ')
|
|
|
|
end_of_keysym++;
|
|
|
|
if (*end_of_keysym) {
|
|
|
|
int keysym;
|
|
|
|
*end_of_keysym = 0;
|
2009-03-07 04:27:10 +08:00
|
|
|
keysym = get_keysym(table, line);
|
2004-12-13 00:56:30 +08:00
|
|
|
if (keysym == 0) {
|
|
|
|
// fprintf(stderr, "Warning: unknown keysym %s\n", line);
|
|
|
|
} else {
|
|
|
|
const char *rest = end_of_keysym + 1;
|
2007-10-31 06:38:53 +08:00
|
|
|
char *rest2;
|
|
|
|
int keycode = strtol(rest, &rest2, 0);
|
|
|
|
|
|
|
|
if (rest && strstr(rest, "numlock")) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-03-01 04:03:00 +08:00
|
|
|
if (rest && strstr(rest, "shift"))
|
|
|
|
keycode |= SCANCODE_SHIFT;
|
|
|
|
if (rest && strstr(rest, "altgr"))
|
|
|
|
keycode |= SCANCODE_ALTGR;
|
|
|
|
if (rest && strstr(rest, "ctrl"))
|
|
|
|
keycode |= SCANCODE_CTRL;
|
|
|
|
|
|
|
|
add_keysym(line, keysym, keycode, k);
|
|
|
|
|
|
|
|
if (rest && strstr(rest, "addupper")) {
|
|
|
|
char *c;
|
|
|
|
for (c = line; *c; c++)
|
|
|
|
*c = toupper(*c);
|
|
|
|
keysym = get_keysym(table, line);
|
|
|
|
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
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
void *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
|
|
|
|
|
|
|
int keysym2scancode(void *kbd_layout, int keysym)
|
2004-12-13 00:56:30 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2004-12-13 00:56:30 +08:00
|
|
|
if (keysym < MAX_NORMAL_KEYCODE) {
|
|
|
|
if (k->keysym2keycode[keysym] == 0)
|
|
|
|
fprintf(stderr, "Warning: no scancode found for keysym %d\n",
|
|
|
|
keysym);
|
|
|
|
return k->keysym2keycode[keysym];
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
#ifdef XK_ISO_Left_Tab
|
|
|
|
if (keysym == XK_ISO_Left_Tab)
|
|
|
|
keysym = XK_Tab;
|
|
|
|
#endif
|
|
|
|
for (i = 0; i < k->extra_count; i++)
|
|
|
|
if (k->keysym2keycode_extra[i].keysym == keysym)
|
|
|
|
return k->keysym2keycode_extra[i].keycode;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-31 06:38:53 +08:00
|
|
|
|
2009-03-07 04:27:10 +08:00
|
|
|
int keycode_is_keypad(void *kbd_layout, int keycode)
|
2007-10-31 06:38:53 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2007-10-31 06:38:53 +08:00
|
|
|
struct key_range *kr;
|
|
|
|
|
|
|
|
for (kr = k->keypad_range; kr; kr = kr->next)
|
|
|
|
if (keycode >= kr->start && keycode <= kr->end)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-07 04:27:10 +08:00
|
|
|
int keysym_is_numlock(void *kbd_layout, int keysym)
|
2007-10-31 06:38:53 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
kbd_layout_t *k = kbd_layout;
|
2007-10-31 06:38:53 +08:00
|
|
|
struct key_range *kr;
|
|
|
|
|
|
|
|
for (kr = k->numlock_range; kr; kr = kr->next)
|
|
|
|
if (keysym >= kr->start && keysym <= kr->end)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|