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
28 static int get_keysym(const name2keysym_t
*table
,
31 const name2keysym_t
*p
;
32 for(p
= table
; p
->name
!= NULL
; p
++) {
33 if (!strcmp(p
->name
, name
))
40 static void add_to_key_range(struct key_range
**krp
, int code
) {
42 for (kr
= *krp
; kr
; kr
= kr
->next
) {
43 if (code
>= kr
->start
&& code
<= kr
->end
)
45 if (code
== kr
->start
- 1) {
49 if (code
== kr
->end
+ 1) {
55 kr
= qemu_mallocz(sizeof(*kr
));
56 kr
->start
= kr
->end
= code
;
62 static kbd_layout_t
*parse_keyboard_layout(const name2keysym_t
*table
,
71 snprintf(file_name
, sizeof(file_name
),
72 "%s/keymaps/%s", bios_dir
, language
);
75 k
= qemu_mallocz(sizeof(kbd_layout_t
));
76 if (!(f
= fopen(file_name
, "r"))) {
78 "Could not read keymap file: '%s'\n", file_name
);
82 if (fgets(line
, 1024, f
) == NULL
)
85 if (len
> 0 && line
[len
- 1] == '\n')
89 if (!strncmp(line
, "map ", 4))
91 if (!strncmp(line
, "include ", 8)) {
92 parse_keyboard_layout(table
, line
+ 8, k
);
94 char *end_of_keysym
= line
;
95 while (*end_of_keysym
!= 0 && *end_of_keysym
!= ' ')
100 keysym
= get_keysym(table
, line
);
102 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
104 const char *rest
= end_of_keysym
+ 1;
106 int keycode
= strtol(rest
, &rest2
, 0);
108 if (rest
&& strstr(rest
, "numlock")) {
109 add_to_key_range(&k
->keypad_range
, keycode
);
110 add_to_key_range(&k
->numlock_range
, keysym
);
111 //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
115 keycode=(keycode<<8)^0x80e0; */
116 if (keysym
< MAX_NORMAL_KEYCODE
) {
117 //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
118 k
->keysym2keycode
[keysym
] = keycode
;
120 if (k
->extra_count
>= MAX_EXTRA_COUNT
) {
122 "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
126 fprintf(stderr
, "Setting %d: %d,%d\n",
127 k
->extra_count
, keysym
, keycode
);
129 k
->keysym2keycode_extra
[k
->extra_count
].
131 k
->keysym2keycode_extra
[k
->extra_count
].
145 void *init_keyboard_layout(const name2keysym_t
*table
, const char *language
)
147 return parse_keyboard_layout(table
, language
, 0);
151 int keysym2scancode(void *kbd_layout
, int keysym
)
153 kbd_layout_t
*k
= kbd_layout
;
154 if (keysym
< MAX_NORMAL_KEYCODE
) {
155 if (k
->keysym2keycode
[keysym
] == 0)
156 fprintf(stderr
, "Warning: no scancode found for keysym %d\n",
158 return k
->keysym2keycode
[keysym
];
161 #ifdef XK_ISO_Left_Tab
162 if (keysym
== XK_ISO_Left_Tab
)
165 for (i
= 0; i
< k
->extra_count
; i
++)
166 if (k
->keysym2keycode_extra
[i
].keysym
== keysym
)
167 return k
->keysym2keycode_extra
[i
].keycode
;
172 int keycode_is_keypad(void *kbd_layout
, int keycode
)
174 kbd_layout_t
*k
= kbd_layout
;
175 struct key_range
*kr
;
177 for (kr
= k
->keypad_range
; kr
; kr
= kr
->next
)
178 if (keycode
>= kr
->start
&& keycode
<= kr
->end
)
183 int keysym_is_numlock(void *kbd_layout
, int keysym
)
185 kbd_layout_t
*k
= kbd_layout
;
186 struct key_range
*kr
;
188 for (kr
= k
->numlock_range
; kr
; kr
= kr
->next
)
189 if (keysym
>= kr
->start
&& keysym
<= kr
->end
)