Statistics
| Branch: | Revision:

root / keymaps.c @ 8c5e95d8

History | View | Annotate | Download (5.6 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 3d11d0eb bellard
static int get_keysym(const char *name)
26 3d11d0eb bellard
{
27 3d11d0eb bellard
    name2keysym_t *p;
28 3d11d0eb bellard
    for(p = name2keysym; p->name != NULL; p++) {
29 3d11d0eb bellard
        if (!strcmp(p->name, name))
30 3d11d0eb bellard
            return p->keysym;
31 3d11d0eb bellard
    }
32 3d11d0eb bellard
    return 0;
33 3d11d0eb bellard
}
34 3d11d0eb bellard
35 a528b80c balrog
struct key_range {
36 a528b80c balrog
    int start;
37 a528b80c balrog
    int end;
38 a528b80c balrog
    struct key_range *next;
39 a528b80c balrog
};
40 a528b80c balrog
41 3d11d0eb bellard
#define MAX_NORMAL_KEYCODE 512
42 3d11d0eb bellard
#define MAX_EXTRA_COUNT 256
43 3d11d0eb bellard
typedef struct {
44 3d11d0eb bellard
    uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
45 3d11d0eb bellard
    struct {
46 3d11d0eb bellard
        int keysym;
47 3d11d0eb bellard
        uint16_t keycode;
48 3d11d0eb bellard
    } keysym2keycode_extra[MAX_EXTRA_COUNT];
49 3d11d0eb bellard
    int extra_count;
50 a528b80c balrog
    struct key_range *keypad_range;
51 a528b80c balrog
    struct key_range *numlock_range;
52 3d11d0eb bellard
} kbd_layout_t;
53 3d11d0eb bellard
54 a528b80c balrog
static void add_to_key_range(struct key_range **krp, int code) {
55 a528b80c balrog
    struct key_range *kr;
56 a528b80c balrog
    for (kr = *krp; kr; kr = kr->next) {
57 a528b80c balrog
        if (code >= kr->start && code <= kr->end)
58 a528b80c balrog
            break;
59 a528b80c balrog
        if (code == kr->start - 1) {
60 a528b80c balrog
            kr->start--;
61 a528b80c balrog
            break;
62 a528b80c balrog
        }
63 a528b80c balrog
        if (code == kr->end + 1) {
64 a528b80c balrog
            kr->end++;
65 a528b80c balrog
            break;
66 a528b80c balrog
        }
67 a528b80c balrog
    }
68 a528b80c balrog
    if (kr == NULL) {
69 a528b80c balrog
        kr = qemu_mallocz(sizeof(*kr));
70 a528b80c balrog
        if (kr) {
71 a528b80c balrog
            kr->start = kr->end = code;
72 a528b80c balrog
            kr->next = *krp;
73 a528b80c balrog
            *krp = kr;
74 a528b80c balrog
        }
75 a528b80c balrog
    }
76 a528b80c balrog
}
77 a528b80c balrog
78 3d11d0eb bellard
static kbd_layout_t *parse_keyboard_layout(const char *language,
79 3d11d0eb bellard
                                           kbd_layout_t * k)
80 3d11d0eb bellard
{
81 3d11d0eb bellard
    FILE *f;
82 3d11d0eb bellard
    char file_name[1024];
83 3d11d0eb bellard
    char line[1024];
84 3d11d0eb bellard
    int len;
85 3d11d0eb bellard
86 3d11d0eb bellard
    snprintf(file_name, sizeof(file_name),
87 3d11d0eb bellard
             "%s/keymaps/%s", bios_dir, language);
88 3d11d0eb bellard
89 3d11d0eb bellard
    if (!k)
90 3d11d0eb bellard
        k = qemu_mallocz(sizeof(kbd_layout_t));
91 3d11d0eb bellard
    if (!k)
92 3d11d0eb bellard
        return 0;
93 3d11d0eb bellard
    if (!(f = fopen(file_name, "r"))) {
94 3d11d0eb bellard
        fprintf(stderr,
95 3d11d0eb bellard
                "Could not read keymap file: '%s'\n", file_name);
96 3d11d0eb bellard
        return 0;
97 3d11d0eb bellard
    }
98 3d11d0eb bellard
    for(;;) {
99 3d11d0eb bellard
        if (fgets(line, 1024, f) == NULL)
100 3d11d0eb bellard
            break;
101 3d11d0eb bellard
        len = strlen(line);
102 3d11d0eb bellard
        if (len > 0 && line[len - 1] == '\n')
103 3d11d0eb bellard
            line[len - 1] = '\0';
104 3d11d0eb bellard
        if (line[0] == '#')
105 3d11d0eb bellard
            continue;
106 3d11d0eb bellard
        if (!strncmp(line, "map ", 4))
107 3d11d0eb bellard
            continue;
108 3d11d0eb bellard
        if (!strncmp(line, "include ", 8)) {
109 3d11d0eb bellard
            parse_keyboard_layout(line + 8, k);
110 3d11d0eb bellard
        } else {
111 3d11d0eb bellard
            char *end_of_keysym = line;
112 3d11d0eb bellard
            while (*end_of_keysym != 0 && *end_of_keysym != ' ')
113 3d11d0eb bellard
                end_of_keysym++;
114 3d11d0eb bellard
            if (*end_of_keysym) {
115 3d11d0eb bellard
                int keysym;
116 3d11d0eb bellard
                *end_of_keysym = 0;
117 3d11d0eb bellard
                keysym = get_keysym(line);
118 3d11d0eb bellard
                if (keysym == 0) {
119 3d11d0eb bellard
                    //                    fprintf(stderr, "Warning: unknown keysym %s\n", line);
120 3d11d0eb bellard
                } else {
121 3d11d0eb bellard
                    const char *rest = end_of_keysym + 1;
122 a528b80c balrog
                    char *rest2;
123 a528b80c balrog
                    int keycode = strtol(rest, &rest2, 0);
124 a528b80c balrog
125 a528b80c balrog
                    if (rest && strstr(rest, "numlock")) {
126 a528b80c balrog
                        add_to_key_range(&k->keypad_range, keycode);
127 a528b80c balrog
                        add_to_key_range(&k->numlock_range, keysym);
128 a528b80c balrog
                        //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
129 a528b80c balrog
                    }
130 a528b80c balrog
131 3d11d0eb bellard
                    /* if(keycode&0x80)
132 3d11d0eb bellard
                       keycode=(keycode<<8)^0x80e0; */
133 3d11d0eb bellard
                    if (keysym < MAX_NORMAL_KEYCODE) {
134 3d11d0eb bellard
                        //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
135 3d11d0eb bellard
                        k->keysym2keycode[keysym] = keycode;
136 3d11d0eb bellard
                    } else {
137 3d11d0eb bellard
                        if (k->extra_count >= MAX_EXTRA_COUNT) {
138 3d11d0eb bellard
                            fprintf(stderr,
139 3d11d0eb bellard
                                    "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
140 3d11d0eb bellard
                                    line, keysym);
141 3d11d0eb bellard
                        } else {
142 24236869 bellard
#if 0
143 3d11d0eb bellard
                            fprintf(stderr, "Setting %d: %d,%d\n",
144 3d11d0eb bellard
                                    k->extra_count, keysym, keycode);
145 24236869 bellard
#endif
146 3d11d0eb bellard
                            k->keysym2keycode_extra[k->extra_count].
147 3d11d0eb bellard
                                keysym = keysym;
148 3d11d0eb bellard
                            k->keysym2keycode_extra[k->extra_count].
149 3d11d0eb bellard
                                keycode = keycode;
150 3d11d0eb bellard
                            k->extra_count++;
151 3d11d0eb bellard
                        }
152 3d11d0eb bellard
                    }
153 3d11d0eb bellard
                }
154 3d11d0eb bellard
            }
155 3d11d0eb bellard
        }
156 3d11d0eb bellard
    }
157 3d11d0eb bellard
    fclose(f);
158 3d11d0eb bellard
    return k;
159 3d11d0eb bellard
}
160 3d11d0eb bellard
161 3d11d0eb bellard
static void *init_keyboard_layout(const char *language)
162 3d11d0eb bellard
{
163 3d11d0eb bellard
    return parse_keyboard_layout(language, 0);
164 3d11d0eb bellard
}
165 3d11d0eb bellard
166 3d11d0eb bellard
static int keysym2scancode(void *kbd_layout, int keysym)
167 3d11d0eb bellard
{
168 3d11d0eb bellard
    kbd_layout_t *k = kbd_layout;
169 3d11d0eb bellard
    if (keysym < MAX_NORMAL_KEYCODE) {
170 3d11d0eb bellard
        if (k->keysym2keycode[keysym] == 0)
171 3d11d0eb bellard
            fprintf(stderr, "Warning: no scancode found for keysym %d\n",
172 3d11d0eb bellard
                    keysym);
173 3d11d0eb bellard
        return k->keysym2keycode[keysym];
174 3d11d0eb bellard
    } else {
175 3d11d0eb bellard
        int i;
176 3d11d0eb bellard
#ifdef XK_ISO_Left_Tab
177 3d11d0eb bellard
        if (keysym == XK_ISO_Left_Tab)
178 3d11d0eb bellard
            keysym = XK_Tab;
179 3d11d0eb bellard
#endif
180 3d11d0eb bellard
        for (i = 0; i < k->extra_count; i++)
181 3d11d0eb bellard
            if (k->keysym2keycode_extra[i].keysym == keysym)
182 3d11d0eb bellard
                return k->keysym2keycode_extra[i].keycode;
183 3d11d0eb bellard
    }
184 3d11d0eb bellard
    return 0;
185 3d11d0eb bellard
}
186 a528b80c balrog
187 a528b80c balrog
static inline int keycode_is_keypad(void *kbd_layout, int keycode)
188 a528b80c balrog
{
189 a528b80c balrog
    kbd_layout_t *k = kbd_layout;
190 a528b80c balrog
    struct key_range *kr;
191 a528b80c balrog
192 a528b80c balrog
    for (kr = k->keypad_range; kr; kr = kr->next)
193 a528b80c balrog
        if (keycode >= kr->start && keycode <= kr->end)
194 a528b80c balrog
            return 1;
195 a528b80c balrog
    return 0;
196 a528b80c balrog
}
197 a528b80c balrog
198 a528b80c balrog
static inline int keysym_is_numlock(void *kbd_layout, int keysym)
199 a528b80c balrog
{
200 a528b80c balrog
    kbd_layout_t *k = kbd_layout;
201 a528b80c balrog
    struct key_range *kr;
202 a528b80c balrog
203 a528b80c balrog
    for (kr = k->numlock_range; kr; kr = kr->next)
204 a528b80c balrog
        if (keysym >= kr->start && keysym <= kr->end)
205 a528b80c balrog
            return 1;
206 a528b80c balrog
    return 0;
207 a528b80c balrog
}