Statistics
| Branch: | Revision:

root / keymaps.c @ 4556bd8b

History | View | Annotate | Download (5.7 kB)

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