root / keymaps.c @ ba5e5090
History | View | Annotate | Download (5.3 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 | c227f099 | Anthony Liguori | static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table, |
63 | 0483755a | aliguori | const char *language, |
64 | c227f099 | Anthony Liguori | kbd_layout_t * k) |
65 | 3d11d0eb | bellard | { |
66 | 3d11d0eb | bellard | FILE *f; |
67 | 5cea8590 | Paul Brook | char * filename;
|
68 | 3d11d0eb | bellard | char line[1024]; |
69 | 3d11d0eb | bellard | int len;
|
70 | 3d11d0eb | bellard | |
71 | 5cea8590 | Paul Brook | filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language); |
72 | 3d11d0eb | bellard | |
73 | 3d11d0eb | bellard | if (!k)
|
74 | c227f099 | Anthony Liguori | k = qemu_mallocz(sizeof(kbd_layout_t));
|
75 | 5cea8590 | Paul Brook | if (!(filename && (f = fopen(filename, "r")))) { |
76 | 3d11d0eb | bellard | fprintf(stderr, |
77 | 5cea8590 | Paul Brook | "Could not read keymap file: '%s'\n", language);
|
78 | 660f11be | Blue Swirl | return NULL; |
79 | 3d11d0eb | bellard | } |
80 | 5cea8590 | Paul Brook | qemu_free(filename); |
81 | 3d11d0eb | bellard | for(;;) {
|
82 | 3d11d0eb | bellard | if (fgets(line, 1024, f) == NULL) |
83 | 3d11d0eb | bellard | break;
|
84 | 3d11d0eb | bellard | len = strlen(line); |
85 | 3d11d0eb | bellard | if (len > 0 && line[len - 1] == '\n') |
86 | 3d11d0eb | bellard | line[len - 1] = '\0'; |
87 | 3d11d0eb | bellard | if (line[0] == '#') |
88 | 3d11d0eb | bellard | continue;
|
89 | 3d11d0eb | bellard | if (!strncmp(line, "map ", 4)) |
90 | 3d11d0eb | bellard | continue;
|
91 | 3d11d0eb | bellard | if (!strncmp(line, "include ", 8)) { |
92 | 0483755a | aliguori | parse_keyboard_layout(table, line + 8, k);
|
93 | 3d11d0eb | bellard | } else {
|
94 | 3d11d0eb | bellard | char *end_of_keysym = line;
|
95 | 3d11d0eb | bellard | while (*end_of_keysym != 0 && *end_of_keysym != ' ') |
96 | 3d11d0eb | bellard | end_of_keysym++; |
97 | 3d11d0eb | bellard | if (*end_of_keysym) {
|
98 | 3d11d0eb | bellard | int keysym;
|
99 | 3d11d0eb | bellard | *end_of_keysym = 0;
|
100 | 0483755a | aliguori | keysym = get_keysym(table, line); |
101 | 3d11d0eb | bellard | if (keysym == 0) { |
102 | 3d11d0eb | bellard | // fprintf(stderr, "Warning: unknown keysym %s\n", line);
|
103 | 3d11d0eb | bellard | } else {
|
104 | 3d11d0eb | bellard | const char *rest = end_of_keysym + 1; |
105 | a528b80c | balrog | char *rest2;
|
106 | a528b80c | balrog | int keycode = strtol(rest, &rest2, 0); |
107 | a528b80c | balrog | |
108 | a528b80c | balrog | if (rest && strstr(rest, "numlock")) { |
109 | a528b80c | balrog | add_to_key_range(&k->keypad_range, keycode); |
110 | a528b80c | balrog | add_to_key_range(&k->numlock_range, keysym); |
111 | a528b80c | balrog | //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
|
112 | a528b80c | balrog | } |
113 | a528b80c | balrog | |
114 | 3d11d0eb | bellard | /* if(keycode&0x80)
|
115 | 3d11d0eb | bellard | keycode=(keycode<<8)^0x80e0; */
|
116 | 3d11d0eb | bellard | if (keysym < MAX_NORMAL_KEYCODE) {
|
117 | 3d11d0eb | bellard | //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
|
118 | 3d11d0eb | bellard | k->keysym2keycode[keysym] = keycode; |
119 | 3d11d0eb | bellard | } else {
|
120 | 3d11d0eb | bellard | if (k->extra_count >= MAX_EXTRA_COUNT) {
|
121 | 3d11d0eb | bellard | fprintf(stderr, |
122 | 3d11d0eb | bellard | "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
|
123 | 3d11d0eb | bellard | line, keysym); |
124 | 3d11d0eb | bellard | } else {
|
125 | 24236869 | bellard | #if 0
|
126 | 3d11d0eb | bellard | fprintf(stderr, "Setting %d: %d,%d\n",
|
127 | 3d11d0eb | bellard | k->extra_count, keysym, keycode);
|
128 | 24236869 | bellard | #endif
|
129 | 3d11d0eb | bellard | k->keysym2keycode_extra[k->extra_count]. |
130 | 3d11d0eb | bellard | keysym = keysym; |
131 | 3d11d0eb | bellard | k->keysym2keycode_extra[k->extra_count]. |
132 | 3d11d0eb | bellard | keycode = keycode; |
133 | 3d11d0eb | bellard | k->extra_count++; |
134 | 3d11d0eb | bellard | } |
135 | 3d11d0eb | bellard | } |
136 | 3d11d0eb | bellard | } |
137 | 3d11d0eb | bellard | } |
138 | 3d11d0eb | bellard | } |
139 | 3d11d0eb | bellard | } |
140 | 3d11d0eb | bellard | fclose(f); |
141 | 3d11d0eb | bellard | return k;
|
142 | 3d11d0eb | bellard | } |
143 | 3d11d0eb | bellard | |
144 | 0483755a | aliguori | |
145 | c227f099 | Anthony Liguori | void *init_keyboard_layout(const name2keysym_t *table, const char *language) |
146 | 3d11d0eb | bellard | { |
147 | 660f11be | Blue Swirl | return parse_keyboard_layout(table, language, NULL); |
148 | 3d11d0eb | bellard | } |
149 | 3d11d0eb | bellard | |
150 | 0483755a | aliguori | |
151 | 0483755a | aliguori | int keysym2scancode(void *kbd_layout, int keysym) |
152 | 3d11d0eb | bellard | { |
153 | c227f099 | Anthony Liguori | kbd_layout_t *k = kbd_layout; |
154 | 3d11d0eb | bellard | if (keysym < MAX_NORMAL_KEYCODE) {
|
155 | 3d11d0eb | bellard | if (k->keysym2keycode[keysym] == 0) |
156 | 3d11d0eb | bellard | fprintf(stderr, "Warning: no scancode found for keysym %d\n",
|
157 | 3d11d0eb | bellard | keysym); |
158 | 3d11d0eb | bellard | return k->keysym2keycode[keysym];
|
159 | 3d11d0eb | bellard | } else {
|
160 | 3d11d0eb | bellard | int i;
|
161 | 3d11d0eb | bellard | #ifdef XK_ISO_Left_Tab
|
162 | 3d11d0eb | bellard | if (keysym == XK_ISO_Left_Tab)
|
163 | 3d11d0eb | bellard | keysym = XK_Tab; |
164 | 3d11d0eb | bellard | #endif
|
165 | 3d11d0eb | bellard | for (i = 0; i < k->extra_count; i++) |
166 | 3d11d0eb | bellard | if (k->keysym2keycode_extra[i].keysym == keysym)
|
167 | 3d11d0eb | bellard | return k->keysym2keycode_extra[i].keycode;
|
168 | 3d11d0eb | bellard | } |
169 | 3d11d0eb | bellard | return 0; |
170 | 3d11d0eb | bellard | } |
171 | a528b80c | balrog | |
172 | 0483755a | aliguori | int keycode_is_keypad(void *kbd_layout, int keycode) |
173 | a528b80c | balrog | { |
174 | c227f099 | Anthony Liguori | kbd_layout_t *k = kbd_layout; |
175 | a528b80c | balrog | struct key_range *kr;
|
176 | a528b80c | balrog | |
177 | a528b80c | balrog | for (kr = k->keypad_range; kr; kr = kr->next)
|
178 | a528b80c | balrog | if (keycode >= kr->start && keycode <= kr->end)
|
179 | a528b80c | balrog | return 1; |
180 | a528b80c | balrog | return 0; |
181 | a528b80c | balrog | } |
182 | a528b80c | balrog | |
183 | 0483755a | aliguori | int keysym_is_numlock(void *kbd_layout, int keysym) |
184 | a528b80c | balrog | { |
185 | c227f099 | Anthony Liguori | kbd_layout_t *k = kbd_layout; |
186 | a528b80c | balrog | struct key_range *kr;
|
187 | a528b80c | balrog | |
188 | a528b80c | balrog | for (kr = k->numlock_range; kr; kr = kr->next)
|
189 | a528b80c | balrog | if (keysym >= kr->start && keysym <= kr->end)
|
190 | a528b80c | balrog | return 1; |
191 | a528b80c | balrog | return 0; |
192 | a528b80c | balrog | } |