tcg: Fix compilation without TCG
[qemu/kevin.git] / qemu-keymap.c
blob49e9167b866a4f1cabea1b95d45eaba1fa357cff
1 /*
2 * QEMU keymap utility
4 * Copyright Red Hat, Inc. 2017
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu-common.h"
14 #include "qapi-types.h"
15 #include "qemu/notify.h"
16 #include "ui/input.h"
18 #include <xkbcommon/xkbcommon.h>
20 struct xkb_rule_names names = {
21 .rules = NULL,
22 .model = "pc105",
23 .layout = "us",
24 .variant = NULL,
25 .options = NULL,
28 static xkb_mod_mask_t shift;
29 static xkb_mod_mask_t ctrl;
30 static xkb_mod_mask_t altgr;
31 static xkb_mod_mask_t numlock;
33 static FILE *outfile;
35 /* ------------------------------------------------------------------------ */
37 static uint32_t qcode_to_number(uint32_t qcode)
39 KeyValue keyvalue;
40 uint32_t number;
42 keyvalue.type = KEY_VALUE_KIND_QCODE;
43 keyvalue.u.qcode.data = qcode;
44 number = qemu_input_key_value_to_number(&keyvalue);
45 assert(number != 0);
46 return number;
49 static void print_sym(xkb_keysym_t sym, uint32_t qcode, const char *mod)
51 char name[64];
53 if (sym == XKB_KEY_NoSymbol) {
54 return;
56 xkb_keysym_get_name(sym, name, sizeof(name));
58 /* TODO: make ui/keymap.c parser accept QKeyCode names */
59 fprintf(outfile, "%s 0x%02x%s\n", name, qcode_to_number(qcode), mod);
62 static void walk_map(struct xkb_keymap *map, xkb_keycode_t code, void *data)
64 struct xkb_state *state = data;
65 xkb_keysym_t kbase, knumlock, kshift, kaltgr, kaltgrshift;
66 uint32_t evdev, qcode;
67 char name[64];
69 fprintf(outfile, "\n");
72 * map xkb keycode -> QKeyCode
74 * xkb keycode is linux evdev shifted by 8
76 evdev = code - 8;
77 qcode = qemu_input_linux_to_qcode(evdev);
78 if (qcode == Q_KEY_CODE_UNMAPPED) {
79 xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
80 kbase = xkb_state_key_get_one_sym(state, code);
81 xkb_keysym_get_name(kbase, name, sizeof(name));
82 fprintf(outfile, "# evdev %d (0x%x): no evdev -> QKeyCode mapping"
83 " (xkb keysym %s)\n", evdev, evdev, name);
84 return;
86 fprintf(outfile, "# evdev %d (0x%x), QKeyCode \"%s\", number 0x%x\n",
87 evdev, evdev,
88 QKeyCode_lookup.array[qcode],
89 qcode_to_number(qcode));
92 * check which modifier states generate which keysyms
94 xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
95 kbase = xkb_state_key_get_one_sym(state, code);
96 print_sym(kbase, qcode, "");
98 xkb_state_update_mask(state, 0, 0, numlock, 0, 0, 0);
99 knumlock = xkb_state_key_get_one_sym(state, code);
100 if (kbase != knumlock) {
101 print_sym(knumlock, qcode, " numlock");
104 xkb_state_update_mask(state, shift, 0, 0, 0, 0, 0);
105 kshift = xkb_state_key_get_one_sym(state, code);
106 if (kbase != kshift && knumlock != kshift) {
107 print_sym(kshift, qcode, " shift");
110 xkb_state_update_mask(state, altgr, 0, 0, 0, 0, 0);
111 kaltgr = xkb_state_key_get_one_sym(state, code);
112 if (kbase != kaltgr) {
113 print_sym(kaltgr, qcode, " altgr");
116 xkb_state_update_mask(state, altgr | shift, 0, 0, 0, 0, 0);
117 kaltgrshift = xkb_state_key_get_one_sym(state, code);
118 if (kshift != kaltgrshift && kaltgr != kaltgrshift) {
119 print_sym(kaltgrshift, qcode, " shift altgr");
121 return;
124 static void usage(FILE *out)
126 fprintf(out,
127 "\n"
128 "This tool generates qemu reverse keymaps from xkb keymaps,\n"
129 "which can be used with the qemu \"-k\" command line switch.\n"
130 "\n"
131 "usage: qemu-keymap <options>\n"
132 "options:\n"
133 " -h print this text\n"
134 " -f <file> set output file (default: stdout)\n"
135 " -m <model> set kbd model (default: %s)\n"
136 " -l <layout> set kbd layout (default: %s)\n"
137 " -v <variant> set kbd variant (default: %s)\n"
138 " -o <options> set kbd options (default: %s)\n"
139 "\n",
140 names.model, names.layout,
141 names.variant ?: "-",
142 names.options ?: "-");
145 int main(int argc, char *argv[])
147 struct xkb_context *ctx;
148 struct xkb_keymap *map;
149 struct xkb_state *state;
150 xkb_mod_index_t mod, mods;
151 int rc;
153 for (;;) {
154 rc = getopt(argc, argv, "hm:l:v:o:f:");
155 if (rc == -1) {
156 break;
158 switch (rc) {
159 case 'm':
160 names.model = optarg;
161 break;
162 case 'l':
163 names.layout = optarg;
164 break;
165 case 'v':
166 names.variant = optarg;
167 break;
168 case 'o':
169 names.options = optarg;
170 break;
171 case 'f':
172 outfile = fopen(optarg, "w");
173 if (outfile == NULL) {
174 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
175 exit(1);
177 break;
178 case 'h':
179 usage(stdout);
180 exit(0);
181 default:
182 usage(stderr);
183 exit(1);
187 if (outfile == NULL) {
188 outfile = stdout;
191 fprintf(outfile,
192 "#\n"
193 "# generated by qemu-keymap\n"
194 "# model : %s\n"
195 "# layout : %s\n"
196 "# variant : %s\n"
197 "# options : %s\n"
198 "\n",
199 names.model, names.layout,
200 names.variant ?: "-",
201 names.options ?: "-");
203 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
204 map = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
205 if (!map) {
206 /* libxkbcommon prints error */
207 exit(1);
210 fprintf(outfile, "# name: \"%s\"\n\n",
211 xkb_keymap_layout_get_name(map, 0));
212 fprintf(outfile, "# modifiers\n");
213 mods = xkb_keymap_num_mods(map);
214 for (mod = 0; mod < mods; mod++) {
215 fprintf(outfile, "# %2d: %s\n",
216 mod, xkb_keymap_mod_get_name(map, mod));
219 mod = xkb_keymap_mod_get_index(map, "Shift");
220 shift = (1 << mod);
221 mod = xkb_keymap_mod_get_index(map, "Control");
222 ctrl = (1 << mod);
223 mod = xkb_keymap_mod_get_index(map, "AltGr");
224 altgr = (1 << mod);
225 mod = xkb_keymap_mod_get_index(map, "NumLock");
226 numlock = (1 << mod);
228 state = xkb_state_new(map);
229 xkb_keymap_key_for_each(map, walk_map, state);
231 /* add quirks */
232 fprintf(outfile,
233 "\n"
234 "#\n"
235 "# quirks section start\n"
236 "#\n"
237 "# Sometimes multiple keysyms map to the same keycodes.\n"
238 "# The keycode -> keysym lookup finds only one of the\n"
239 "# keysyms. So append them here.\n"
240 "#\n"
241 "\n");
242 print_sym(XKB_KEY_Print, Q_KEY_CODE_SYSRQ, "");
243 print_sym(XKB_KEY_Sys_Req, Q_KEY_CODE_SYSRQ, "");
244 print_sym(XKB_KEY_Execute, Q_KEY_CODE_SYSRQ, "");
246 print_sym(XKB_KEY_KP_Decimal, Q_KEY_CODE_KP_DECIMAL, " numlock");
247 print_sym(XKB_KEY_KP_Separator, Q_KEY_CODE_KP_DECIMAL, " numlock");
249 print_sym(XKB_KEY_Alt_R, Q_KEY_CODE_ALT_R, "");
250 print_sym(XKB_KEY_ISO_Level3_Shift, Q_KEY_CODE_ALT_R, "");
251 print_sym(XKB_KEY_Mode_switch, Q_KEY_CODE_ALT_R, "");
253 fprintf(outfile,
254 "\n"
255 "# quirks section end\n");
257 exit(0);