Enhance 'info vnc' monitor output ("Daniel P. Berrange")
[qemu-kvm/fedora.git] / keymaps.c
blob216e37831b6c16cb08fea191ba8150474752b1bd
1 /*
2 * QEMU keysym to keycode conversion using rdesktop keymaps
4 * Copyright (c) 2004 Johannes Schindelin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 static int get_keysym(const char *name)
27 const name2keysym_t *p;
28 for(p = name2keysym; p->name != NULL; p++) {
29 if (!strcmp(p->name, name))
30 return p->keysym;
32 return 0;
35 struct key_range {
36 int start;
37 int end;
38 struct key_range *next;
41 #define MAX_NORMAL_KEYCODE 512
42 #define MAX_EXTRA_COUNT 256
43 typedef struct {
44 uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
45 struct {
46 int keysym;
47 uint16_t keycode;
48 } keysym2keycode_extra[MAX_EXTRA_COUNT];
49 int extra_count;
50 struct key_range *keypad_range;
51 struct key_range *numlock_range;
52 } kbd_layout_t;
54 static void add_to_key_range(struct key_range **krp, int code) {
55 struct key_range *kr;
56 for (kr = *krp; kr; kr = kr->next) {
57 if (code >= kr->start && code <= kr->end)
58 break;
59 if (code == kr->start - 1) {
60 kr->start--;
61 break;
63 if (code == kr->end + 1) {
64 kr->end++;
65 break;
68 if (kr == NULL) {
69 kr = qemu_mallocz(sizeof(*kr));
70 kr->start = kr->end = code;
71 kr->next = *krp;
72 *krp = kr;
76 static kbd_layout_t *parse_keyboard_layout(const char *language,
77 kbd_layout_t * k)
79 FILE *f;
80 char file_name[1024];
81 char line[1024];
82 int len;
84 snprintf(file_name, sizeof(file_name),
85 "%s/keymaps/%s", bios_dir, language);
87 if (!k)
88 k = qemu_mallocz(sizeof(kbd_layout_t));
89 if (!(f = fopen(file_name, "r"))) {
90 fprintf(stderr,
91 "Could not read keymap file: '%s'\n", file_name);
92 return 0;
94 for(;;) {
95 if (fgets(line, 1024, f) == NULL)
96 break;
97 len = strlen(line);
98 if (len > 0 && line[len - 1] == '\n')
99 line[len - 1] = '\0';
100 if (line[0] == '#')
101 continue;
102 if (!strncmp(line, "map ", 4))
103 continue;
104 if (!strncmp(line, "include ", 8)) {
105 parse_keyboard_layout(line + 8, k);
106 } else {
107 char *end_of_keysym = line;
108 while (*end_of_keysym != 0 && *end_of_keysym != ' ')
109 end_of_keysym++;
110 if (*end_of_keysym) {
111 int keysym;
112 *end_of_keysym = 0;
113 keysym = get_keysym(line);
114 if (keysym == 0) {
115 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
116 } else {
117 const char *rest = end_of_keysym + 1;
118 char *rest2;
119 int keycode = strtol(rest, &rest2, 0);
121 if (rest && strstr(rest, "numlock")) {
122 add_to_key_range(&k->keypad_range, keycode);
123 add_to_key_range(&k->numlock_range, keysym);
124 //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
127 /* if(keycode&0x80)
128 keycode=(keycode<<8)^0x80e0; */
129 if (keysym < MAX_NORMAL_KEYCODE) {
130 //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
131 k->keysym2keycode[keysym] = keycode;
132 } else {
133 if (k->extra_count >= MAX_EXTRA_COUNT) {
134 fprintf(stderr,
135 "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
136 line, keysym);
137 } else {
138 #if 0
139 fprintf(stderr, "Setting %d: %d,%d\n",
140 k->extra_count, keysym, keycode);
141 #endif
142 k->keysym2keycode_extra[k->extra_count].
143 keysym = keysym;
144 k->keysym2keycode_extra[k->extra_count].
145 keycode = keycode;
146 k->extra_count++;
153 fclose(f);
154 return k;
157 static void *init_keyboard_layout(const char *language)
159 return parse_keyboard_layout(language, 0);
162 static int keysym2scancode(void *kbd_layout, int keysym)
164 kbd_layout_t *k = kbd_layout;
165 if (keysym < MAX_NORMAL_KEYCODE) {
166 if (k->keysym2keycode[keysym] == 0)
167 fprintf(stderr, "Warning: no scancode found for keysym %d\n",
168 keysym);
169 return k->keysym2keycode[keysym];
170 } else {
171 int i;
172 #ifdef XK_ISO_Left_Tab
173 if (keysym == XK_ISO_Left_Tab)
174 keysym = XK_Tab;
175 #endif
176 for (i = 0; i < k->extra_count; i++)
177 if (k->keysym2keycode_extra[i].keysym == keysym)
178 return k->keysym2keycode_extra[i].keycode;
180 return 0;
183 static inline int keycode_is_keypad(void *kbd_layout, int keycode)
185 kbd_layout_t *k = kbd_layout;
186 struct key_range *kr;
188 for (kr = k->keypad_range; kr; kr = kr->next)
189 if (keycode >= kr->start && keycode <= kr->end)
190 return 1;
191 return 0;
194 static inline int keysym_is_numlock(void *kbd_layout, int keysym)
196 kbd_layout_t *k = kbd_layout;
197 struct key_range *kr;
199 for (kr = k->numlock_range; kr; kr = kr->next)
200 if (keysym >= kr->start && keysym <= kr->end)
201 return 1;
202 return 0;