qemu/ui/keymaps.c
<<
>>
Prefs
   1/*
   2 * QEMU keysym to keycode conversion using rdesktop keymaps
   3 *
   4 * Copyright (c) 2004 Johannes Schindelin
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26#include "keymaps.h"
  27#include "sysemu/sysemu.h"
  28#include "trace.h"
  29
  30static int get_keysym(const name2keysym_t *table,
  31                      const char *name)
  32{
  33    const name2keysym_t *p;
  34    for(p = table; p->name != NULL; p++) {
  35        if (!strcmp(p->name, name)) {
  36            return p->keysym;
  37        }
  38    }
  39    if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
  40        char *end;
  41        int ret = (int)strtoul(name + 1, &end, 16);
  42        if (*end == '\0' && ret > 0) {
  43            return ret;
  44        }
  45    }
  46    return 0;
  47}
  48
  49
  50static void add_to_key_range(struct key_range **krp, int code) {
  51    struct key_range *kr;
  52    for (kr = *krp; kr; kr = kr->next) {
  53        if (code >= kr->start && code <= kr->end) {
  54            break;
  55        }
  56        if (code == kr->start - 1) {
  57            kr->start--;
  58            break;
  59        }
  60        if (code == kr->end + 1) {
  61            kr->end++;
  62            break;
  63        }
  64    }
  65    if (kr == NULL) {
  66        kr = g_malloc0(sizeof(*kr));
  67        kr->start = kr->end = code;
  68        kr->next = *krp;
  69        *krp = kr;
  70    }
  71}
  72
  73static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
  74    if (keysym < MAX_NORMAL_KEYCODE) {
  75        trace_keymap_add("normal", keysym, keycode, line);
  76        k->keysym2keycode[keysym] = keycode;
  77    } else {
  78        if (k->extra_count >= MAX_EXTRA_COUNT) {
  79            fprintf(stderr, "Warning: Could not assign keysym %s (0x%x)"
  80                    " because of memory constraints.\n", line, keysym);
  81        } else {
  82            trace_keymap_add("extra", keysym, keycode, line);
  83            k->keysym2keycode_extra[k->extra_count].
  84            keysym = keysym;
  85            k->keysym2keycode_extra[k->extra_count].
  86            keycode = keycode;
  87            k->extra_count++;
  88        }
  89    }
  90}
  91
  92static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
  93                                           const char *language,
  94                                           kbd_layout_t *k)
  95{
  96    FILE *f;
  97    char * filename;
  98    char line[1024];
  99    char keyname[64];
 100    int len;
 101
 102    filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
 103    trace_keymap_parse(filename);
 104    f = filename ? fopen(filename, "r") : NULL;
 105    g_free(filename);
 106    if (!f) {
 107        fprintf(stderr, "Could not read keymap file: '%s'\n", language);
 108        return NULL;
 109    }
 110
 111    if (!k) {
 112        k = g_new0(kbd_layout_t, 1);
 113    }
 114
 115    for(;;) {
 116        if (fgets(line, 1024, f) == NULL) {
 117            break;
 118        }
 119        len = strlen(line);
 120        if (len > 0 && line[len - 1] == '\n') {
 121            line[len - 1] = '\0';
 122        }
 123        if (line[0] == '#') {
 124            continue;
 125        }
 126        if (!strncmp(line, "map ", 4)) {
 127            continue;
 128        }
 129        if (!strncmp(line, "include ", 8)) {
 130            parse_keyboard_layout(table, line + 8, k);
 131        } else {
 132            int offset = 0;
 133            while (line[offset] != 0 &&
 134                   line[offset] != ' ' &&
 135                   offset < sizeof(keyname) - 1) {
 136                keyname[offset] = line[offset];
 137                offset++;
 138            }
 139            keyname[offset] = 0;
 140            if (strlen(keyname)) {
 141                int keysym;
 142                keysym = get_keysym(table, keyname);
 143                if (keysym == 0) {
 144                    /* fprintf(stderr, "Warning: unknown keysym %s\n", line);*/
 145                } else {
 146                    const char *rest = line + offset + 1;
 147                    int keycode = strtol(rest, NULL, 0);
 148
 149                    if (strstr(rest, "numlock")) {
 150                        add_to_key_range(&k->keypad_range, keycode);
 151                        add_to_key_range(&k->numlock_range, keysym);
 152                        /* fprintf(stderr, "keypad keysym %04x keycode %d\n",
 153                                   keysym, keycode); */
 154                    }
 155
 156                    if (strstr(rest, "shift")) {
 157                        keycode |= SCANCODE_SHIFT;
 158                    }
 159                    if (strstr(rest, "altgr")) {
 160                        keycode |= SCANCODE_ALTGR;
 161                    }
 162                    if (strstr(rest, "ctrl")) {
 163                        keycode |= SCANCODE_CTRL;
 164                    }
 165
 166                    add_keysym(line, keysym, keycode, k);
 167
 168                    if (strstr(rest, "addupper")) {
 169                        char *c;
 170                        for (c = keyname; *c; c++) {
 171                            *c = qemu_toupper(*c);
 172                        }
 173                        keysym = get_keysym(table, keyname);
 174                        if (keysym) {
 175                            add_keysym(line, keysym,
 176                                       keycode | SCANCODE_SHIFT, k);
 177                        }
 178                    }
 179                }
 180            }
 181        }
 182    }
 183    fclose(f);
 184    return k;
 185}
 186
 187
 188void *init_keyboard_layout(const name2keysym_t *table, const char *language)
 189{
 190    return parse_keyboard_layout(table, language, NULL);
 191}
 192
 193
 194int keysym2scancode(void *kbd_layout, int keysym)
 195{
 196    kbd_layout_t *k = kbd_layout;
 197    if (keysym < MAX_NORMAL_KEYCODE) {
 198        if (k->keysym2keycode[keysym] == 0) {
 199            trace_keymap_unmapped(keysym);
 200            fprintf(stderr, "Warning: no scancode found for keysym %d\n",
 201                    keysym);
 202        }
 203        return k->keysym2keycode[keysym];
 204    } else {
 205        int i;
 206#ifdef XK_ISO_Left_Tab
 207        if (keysym == XK_ISO_Left_Tab) {
 208            keysym = XK_Tab;
 209        }
 210#endif
 211        for (i = 0; i < k->extra_count; i++) {
 212            if (k->keysym2keycode_extra[i].keysym == keysym) {
 213                return k->keysym2keycode_extra[i].keycode;
 214            }
 215        }
 216    }
 217    return 0;
 218}
 219
 220int keycode_is_keypad(void *kbd_layout, int keycode)
 221{
 222    kbd_layout_t *k = kbd_layout;
 223    struct key_range *kr;
 224
 225    for (kr = k->keypad_range; kr; kr = kr->next) {
 226        if (keycode >= kr->start && keycode <= kr->end) {
 227            return 1;
 228        }
 229    }
 230    return 0;
 231}
 232
 233int keysym_is_numlock(void *kbd_layout, int keysym)
 234{
 235    kbd_layout_t *k = kbd_layout;
 236    struct key_range *kr;
 237
 238    for (kr = k->numlock_range; kr; kr = kr->next) {
 239        if (keysym >= kr->start && keysym <= kr->end) {
 240            return 1;
 241        }
 242    }
 243    return 0;
 244}
 245