qemu-e2k/ui/keymaps.c

245 lines
7.3 KiB
C
Raw Normal View History

/*
* QEMU keysym to keycode conversion using rdesktop keymaps
*
* Copyright (c) 2004 Johannes Schindelin
*
* 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.
*/
#include "qemu/osdep.h"
#include "keymaps.h"
#include "sysemu/sysemu.h"
#include "trace.h"
Convert multi-line fprintf() to warn_report() Convert all the multi-line uses of fprintf(stderr, "warning:"..."\n"... to use warn_report() instead. This helps standardise on a single method of printing warnings to the user. All of the warnings were changed using these commands: find ./* -type f -exec sed -i \ 'N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N;N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + Indentation fixed up manually afterwards. Some of the lines were manually edited to reduce the line length to below 80 charecters. Some of the lines with newlines in the middle of the string were also manually edit to avoid checkpatch errrors. The #include lines were manually updated to allow the code to compile. Several of the warning messages can be improved after this patch, to keep this patch mechanical this has been moved into a later patch. Signed-off-by: Alistair Francis <alistair.francis@xilinx.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Kevin Wolf <kwolf@redhat.com> Cc: Max Reitz <mreitz@redhat.com> Cc: "Michael S. Tsirkin" <mst@redhat.com> Cc: Igor Mammedov <imammedo@redhat.com> Cc: Peter Maydell <peter.maydell@linaro.org> Cc: Stefano Stabellini <sstabellini@kernel.org> Cc: Anthony Perard <anthony.perard@citrix.com> Cc: Richard Henderson <rth@twiddle.net> Cc: Eduardo Habkost <ehabkost@redhat.com> Cc: Aurelien Jarno <aurelien@aurel32.net> Cc: Yongbok Kim <yongbok.kim@imgtec.com> Cc: Cornelia Huck <cohuck@redhat.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Alexander Graf <agraf@suse.de> Cc: Jason Wang <jasowang@redhat.com> Cc: David Gibson <david@gibson.dropbear.id.au> Cc: Gerd Hoffmann <kraxel@redhat.com> Acked-by: Cornelia Huck <cohuck@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Message-Id: <5def63849ca8f551630c6f2b45bcb1c482f765a6.1505158760.git.alistair.francis@xilinx.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-09-11 21:52:53 +02:00
#include "qemu/error-report.h"
static int get_keysym(const name2keysym_t *table,
const char *name)
{
const name2keysym_t *p;
for(p = table; p->name != NULL; p++) {
if (!strcmp(p->name, name)) {
return p->keysym;
}
}
if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
char *end;
int ret = (int)strtoul(name + 1, &end, 16);
if (*end == '\0' && ret > 0) {
return ret;
}
}
return 0;
}
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 = g_malloc0(sizeof(*kr));
kr->start = kr->end = code;
kr->next = *krp;
*krp = kr;
}
}
static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
if (keysym < MAX_NORMAL_KEYCODE) {
trace_keymap_add("normal", keysym, keycode, line);
k->keysym2keycode[keysym] = keycode;
} else {
if (k->extra_count >= MAX_EXTRA_COUNT) {
Convert multi-line fprintf() to warn_report() Convert all the multi-line uses of fprintf(stderr, "warning:"..."\n"... to use warn_report() instead. This helps standardise on a single method of printing warnings to the user. All of the warnings were changed using these commands: find ./* -type f -exec sed -i \ 'N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N;N {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + find ./* -type f -exec sed -i \ 'N;N;N;N;N;N;N; {s|fprintf(.*".*warning[,:] \(.*\)\\n"\(.*\));|warn_report("\1"\2);|Ig}' \ {} + Indentation fixed up manually afterwards. Some of the lines were manually edited to reduce the line length to below 80 charecters. Some of the lines with newlines in the middle of the string were also manually edit to avoid checkpatch errrors. The #include lines were manually updated to allow the code to compile. Several of the warning messages can be improved after this patch, to keep this patch mechanical this has been moved into a later patch. Signed-off-by: Alistair Francis <alistair.francis@xilinx.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Kevin Wolf <kwolf@redhat.com> Cc: Max Reitz <mreitz@redhat.com> Cc: "Michael S. Tsirkin" <mst@redhat.com> Cc: Igor Mammedov <imammedo@redhat.com> Cc: Peter Maydell <peter.maydell@linaro.org> Cc: Stefano Stabellini <sstabellini@kernel.org> Cc: Anthony Perard <anthony.perard@citrix.com> Cc: Richard Henderson <rth@twiddle.net> Cc: Eduardo Habkost <ehabkost@redhat.com> Cc: Aurelien Jarno <aurelien@aurel32.net> Cc: Yongbok Kim <yongbok.kim@imgtec.com> Cc: Cornelia Huck <cohuck@redhat.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Alexander Graf <agraf@suse.de> Cc: Jason Wang <jasowang@redhat.com> Cc: David Gibson <david@gibson.dropbear.id.au> Cc: Gerd Hoffmann <kraxel@redhat.com> Acked-by: Cornelia Huck <cohuck@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Message-Id: <5def63849ca8f551630c6f2b45bcb1c482f765a6.1505158760.git.alistair.francis@xilinx.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-09-11 21:52:53 +02:00
warn_report("Could not assign keysym %s (0x%x)"
" because of memory constraints.", line, keysym);
} else {
trace_keymap_add("extra", keysym, keycode, line);
k->keysym2keycode_extra[k->extra_count].
keysym = keysym;
k->keysym2keycode_extra[k->extra_count].
keycode = keycode;
k->extra_count++;
}
}
}
static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
const char *language,
kbd_layout_t *k)
{
FILE *f;
char * filename;
char line[1024];
char keyname[64];
int len;
filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
trace_keymap_parse(filename);
f = filename ? fopen(filename, "r") : NULL;
g_free(filename);
if (!f) {
fprintf(stderr, "Could not read keymap file: '%s'\n", language);
return NULL;
}
if (!k) {
k = g_new0(kbd_layout_t, 1);
}
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)) {
parse_keyboard_layout(table, line + 8, k);
} else {
int offset = 0;
while (line[offset] != 0 &&
line[offset] != ' ' &&
offset < sizeof(keyname) - 1) {
keyname[offset] = line[offset];
offset++;
}
keyname[offset] = 0;
if (strlen(keyname)) {
int keysym;
keysym = get_keysym(table, keyname);
if (keysym == 0) {
/* warn_report("unknown keysym %s", line);*/
} else {
const char *rest = line + offset + 1;
int keycode = strtol(rest, NULL, 0);
if (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); */
}
if (strstr(rest, "shift")) {
keycode |= SCANCODE_SHIFT;
}
if (strstr(rest, "altgr")) {
keycode |= SCANCODE_ALTGR;
}
if (strstr(rest, "ctrl")) {
keycode |= SCANCODE_CTRL;
}
add_keysym(line, keysym, keycode, k);
if (strstr(rest, "addupper")) {
char *c;
for (c = keyname; *c; c++) {
*c = qemu_toupper(*c);
}
keysym = get_keysym(table, keyname);
if (keysym) {
add_keysym(line, keysym,
keycode | SCANCODE_SHIFT, k);
}
}
}
}
}
}
fclose(f);
return k;
}
void *init_keyboard_layout(const name2keysym_t *table, const char *language)
{
return parse_keyboard_layout(table, language, NULL);
}
int keysym2scancode(void *kbd_layout, int keysym)
{
kbd_layout_t *k = kbd_layout;
if (keysym < MAX_NORMAL_KEYCODE) {
if (k->keysym2keycode[keysym] == 0) {
trace_keymap_unmapped(keysym);
warn_report("no scancode found for keysym %d", 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;
}
int keycode_is_keypad(void *kbd_layout, int keycode)
{
kbd_layout_t *k = kbd_layout;
struct key_range *kr;
for (kr = k->keypad_range; kr; kr = kr->next) {
if (keycode >= kr->start && keycode <= kr->end) {
return 1;
}
}
return 0;
}
int keysym_is_numlock(void *kbd_layout, int keysym)
{
kbd_layout_t *k = kbd_layout;
struct key_range *kr;
for (kr = k->numlock_range; kr; kr = kr->next) {
if (keysym >= kr->start && keysym <= kr->end) {
return 1;
}
}
return 0;
}