5 * Copyright (C) 2017 Red Hat, Inc
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include "qemu/osdep.h"
16 #include "qemu/notify.h"
19 #include <X11/XKBlib.h>
21 static gboolean check_for_xwin(Display *dpy)
23 const char *vendor = ServerVendor(dpy);
25 trace_xkeymap_vendor(vendor);
27 if (strstr(vendor, "Cygwin/X")) {
34 static gboolean check_for_xquartz(Display *dpy)
38 gboolean match = FALSE;
39 char **extensions = XListExtensions(dpy, &nextensions);
40 for (i = 0 ; extensions != NULL && i < nextensions ; i++) {
41 trace_xkeymap_extension(extensions[i]);
42 if (strcmp(extensions[i], "Apple-WM") == 0 ||
43 strcmp(extensions[i], "Apple-DRI") == 0) {
48 XFreeExtensionList(extensions);
54 const guint16 *qemu_xkeymap_mapping_table(Display *dpy, size_t *maplen)
57 const gchar *keycodes = NULL;
59 /* There is no easy way to determine what X11 server
60 * and platform & keyboard driver is in use. Thus we
61 * do best guess heuristics.
63 * This will need more work for people with other
64 * X servers..... patches welcomed.
68 XkbGBN_AllComponentsMask,
71 if (XkbGetNames(dpy, XkbKeycodesNameMask, desc) == Success) {
72 keycodes = XGetAtomName (dpy, desc->names->keycodes);
74 g_warning("could not lookup keycode name");
76 trace_xkeymap_keycodes(keycodes);
79 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
82 if (check_for_xwin(dpy)) {
83 trace_xkeymap_keymap("xwin");
84 *maplen = qemu_input_map_xorgxwin_to_qcode_len;
85 return qemu_input_map_xorgxwin_to_qcode;
86 } else if (check_for_xquartz(dpy)) {
87 trace_xkeymap_keymap("xquartz");
88 *maplen = qemu_input_map_xorgxquartz_to_qcode_len;
89 return qemu_input_map_xorgxquartz_to_qcode;
90 } else if (keycodes && g_str_has_prefix(keycodes, "evdev")) {
91 trace_xkeymap_keymap("evdev");
92 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
93 return qemu_input_map_xorgevdev_to_qcode;
94 } else if (keycodes && g_str_has_prefix(keycodes, "xfree86")) {
95 trace_xkeymap_keymap("kbd");
96 *maplen = qemu_input_map_xorgkbd_to_qcode_len;
97 return qemu_input_map_xorgkbd_to_qcode;
99 trace_xkeymap_keymap("NULL");
100 g_warning("Unknown X11 keycode mapping '%s'.\n"
102 "including the following information:\n"
104 " - Operating system\n"
108 keycodes ? keycodes : "<null>");