ivshmem: Fix unplug of device "ivshmem-plain"
[qemu/armbru.git] / qemu-keymap.c
blob4d00468747f703b2cd10345bb9d958cf5c4f5ddf
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 "qemu/notify.h"
15 #include "ui/input.h"
17 #include <xkbcommon/xkbcommon.h>
19 struct xkb_rule_names names = {
20 .rules = NULL,
21 .model = "pc105",
22 .layout = "us",
23 .variant = NULL,
24 .options = NULL,
27 static xkb_mod_mask_t shift;
28 static xkb_mod_mask_t ctrl;
29 static xkb_mod_mask_t altgr;
30 static xkb_mod_mask_t numlock;
32 static FILE *outfile;
34 /* ------------------------------------------------------------------------ */
36 static uint32_t qcode_to_number(uint32_t qcode)
38 KeyValue keyvalue;
39 uint32_t number;
41 keyvalue.type = KEY_VALUE_KIND_QCODE;
42 keyvalue.u.qcode.data = qcode;
43 number = qemu_input_key_value_to_number(&keyvalue);
44 assert(number != 0);
45 return number;
48 static void print_sym(xkb_keysym_t sym, uint32_t qcode, const char *mod)
50 char name[64];
52 if (sym == XKB_KEY_NoSymbol) {
53 return;
55 xkb_keysym_get_name(sym, name, sizeof(name));
57 /* TODO: make ui/keymap.c parser accept QKeyCode names */
58 fprintf(outfile, "%s 0x%02x%s\n", name, qcode_to_number(qcode), mod);
61 static void walk_map(struct xkb_keymap *map, xkb_keycode_t code, void *data)
63 struct xkb_state *state = data;
64 xkb_keysym_t kbase, knumlock, kshift, kaltgr, kaltgrshift;
65 uint32_t evdev, qcode;
66 char name[64];
68 fprintf(outfile, "\n");
71 * map xkb keycode -> QKeyCode
73 * xkb keycode is linux evdev shifted by 8
75 evdev = code - 8;
76 qcode = qemu_input_linux_to_qcode(evdev);
77 if (qcode == Q_KEY_CODE_UNMAPPED) {
78 xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
79 kbase = xkb_state_key_get_one_sym(state, code);
80 xkb_keysym_get_name(kbase, name, sizeof(name));
81 fprintf(outfile, "# evdev %d (0x%x): no evdev -> QKeyCode mapping"
82 " (xkb keysym %s)\n", evdev, evdev, name);
83 return;
85 fprintf(outfile, "# evdev %d (0x%x), QKeyCode \"%s\", number 0x%x\n",
86 evdev, evdev,
87 QKeyCode_str(qcode),
88 qcode_to_number(qcode));
91 * check which modifier states generate which keysyms
93 xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
94 kbase = xkb_state_key_get_one_sym(state, code);
95 print_sym(kbase, qcode, "");
97 xkb_state_update_mask(state, 0, 0, numlock, 0, 0, 0);
98 knumlock = xkb_state_key_get_one_sym(state, code);
99 if (kbase != knumlock) {
100 print_sym(knumlock, qcode, " numlock");
103 xkb_state_update_mask(state, shift, 0, 0, 0, 0, 0);
104 kshift = xkb_state_key_get_one_sym(state, code);
105 if (kbase != kshift && knumlock != kshift) {
106 print_sym(kshift, qcode, " shift");
109 xkb_state_update_mask(state, altgr, 0, 0, 0, 0, 0);
110 kaltgr = xkb_state_key_get_one_sym(state, code);
111 if (kbase != kaltgr) {
112 print_sym(kaltgr, qcode, " altgr");
115 xkb_state_update_mask(state, altgr | shift, 0, 0, 0, 0, 0);
116 kaltgrshift = xkb_state_key_get_one_sym(state, code);
117 if (kshift != kaltgrshift && kaltgr != kaltgrshift) {
118 print_sym(kaltgrshift, qcode, " shift altgr");
120 return;
123 static void usage(FILE *out)
125 fprintf(out,
126 "\n"
127 "This tool generates qemu reverse keymaps from xkb keymaps,\n"
128 "which can be used with the qemu \"-k\" command line switch.\n"
129 "\n"
130 "usage: qemu-keymap <options>\n"
131 "options:\n"
132 " -h print this text\n"
133 " -f <file> set output file (default: stdout)\n"
134 " -m <model> set kbd model (default: %s)\n"
135 " -l <layout> set kbd layout (default: %s)\n"
136 " -v <variant> set kbd variant (default: %s)\n"
137 " -o <options> set kbd options (default: %s)\n"
138 "\n",
139 names.model, names.layout,
140 names.variant ?: "-",
141 names.options ?: "-");
144 int main(int argc, char *argv[])
146 struct xkb_context *ctx;
147 struct xkb_keymap *map;
148 struct xkb_state *state;
149 xkb_mod_index_t mod, mods;
150 int rc;
152 for (;;) {
153 rc = getopt(argc, argv, "hm:l:v:o:f:");
154 if (rc == -1) {
155 break;
157 switch (rc) {
158 case 'm':
159 names.model = optarg;
160 break;
161 case 'l':
162 names.layout = optarg;
163 break;
164 case 'v':
165 names.variant = optarg;
166 break;
167 case 'o':
168 names.options = optarg;
169 break;
170 case 'f':
171 outfile = fopen(optarg, "w");
172 if (outfile == NULL) {
173 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
174 exit(1);
176 break;
177 case 'h':
178 usage(stdout);
179 exit(0);
180 default:
181 usage(stderr);
182 exit(1);
186 if (outfile == NULL) {
187 outfile = stdout;
190 fprintf(outfile,
191 "#\n"
192 "# generated by qemu-keymap\n"
193 "# model : %s\n"
194 "# layout : %s\n"
195 "# variant : %s\n"
196 "# options : %s\n"
197 "\n",
198 names.model, names.layout,
199 names.variant ?: "-",
200 names.options ?: "-");
202 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
203 map = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
204 if (!map) {
205 /* libxkbcommon prints error */
206 exit(1);
209 fprintf(outfile, "# name: \"%s\"\n\n",
210 xkb_keymap_layout_get_name(map, 0));
211 fprintf(outfile, "# modifiers\n");
212 mods = xkb_keymap_num_mods(map);
213 for (mod = 0; mod < mods; mod++) {
214 fprintf(outfile, "# %2d: %s\n",
215 mod, xkb_keymap_mod_get_name(map, mod));
218 mod = xkb_keymap_mod_get_index(map, "Shift");
219 shift = (1 << mod);
220 mod = xkb_keymap_mod_get_index(map, "Control");
221 ctrl = (1 << mod);
222 mod = xkb_keymap_mod_get_index(map, "AltGr");
223 altgr = (1 << mod);
224 mod = xkb_keymap_mod_get_index(map, "NumLock");
225 numlock = (1 << mod);
227 state = xkb_state_new(map);
228 xkb_keymap_key_for_each(map, walk_map, state);
230 /* add quirks */
231 fprintf(outfile,
232 "\n"
233 "#\n"
234 "# quirks section start\n"
235 "#\n"
236 "# Sometimes multiple keysyms map to the same keycodes.\n"
237 "# The keycode -> keysym lookup finds only one of the\n"
238 "# keysyms. So append them here.\n"
239 "#\n"
240 "\n");
241 print_sym(XKB_KEY_Print, Q_KEY_CODE_SYSRQ, "");
242 print_sym(XKB_KEY_Sys_Req, Q_KEY_CODE_SYSRQ, "");
243 print_sym(XKB_KEY_Execute, Q_KEY_CODE_SYSRQ, "");
245 print_sym(XKB_KEY_KP_Decimal, Q_KEY_CODE_KP_DECIMAL, " numlock");
246 print_sym(XKB_KEY_KP_Separator, Q_KEY_CODE_KP_DECIMAL, " numlock");
248 print_sym(XKB_KEY_Alt_R, Q_KEY_CODE_ALT_R, "");
249 print_sym(XKB_KEY_ISO_Level3_Shift, Q_KEY_CODE_ALT_R, "");
250 print_sym(XKB_KEY_Mode_switch, Q_KEY_CODE_ALT_R, "");
252 fprintf(outfile,
253 "\n"
254 "# quirks section end\n");
256 exit(0);