xref: /openbmc/qemu/ui/x_keymap.c (revision 7eceff5b)
1 /*
2  * QEMU X11 keymaps
3  *
4  * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
5  * Copyright (C) 2017 Red Hat, Inc
6  *
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.
10  */
11 
12 #include "qemu/osdep.h"
13 
14 #include "x_keymap.h"
15 #include "trace.h"
16 #include "qemu/notify.h"
17 #include "ui/input.h"
18 
19 #include <X11/XKBlib.h>
20 
21 static gboolean check_for_xwin(Display *dpy)
22 {
23     const char *vendor = ServerVendor(dpy);
24 
25     trace_xkeymap_vendor(vendor);
26 
27     if (strstr(vendor, "Cygwin/X")) {
28         return TRUE;
29     }
30 
31     return FALSE;
32 }
33 
34 static gboolean check_for_xquartz(Display *dpy)
35 {
36     int nextensions;
37     int i;
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) {
44             match = TRUE;
45         }
46     }
47     if (extensions) {
48         XFreeExtensionList(extensions);
49     }
50 
51     return match;
52 }
53 
54 const guint16 *qemu_xkeymap_mapping_table(Display *dpy, size_t *maplen)
55 {
56     XkbDescPtr desc;
57     const gchar *keycodes = NULL;
58 
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.
62      *
63      * This will need more work for people with other
64      * X servers..... patches welcomed.
65      */
66 
67     desc = XkbGetMap(dpy,
68                      XkbGBN_AllComponentsMask,
69                      XkbUseCoreKbd);
70     if (desc) {
71         if (XkbGetNames(dpy, XkbKeycodesNameMask, desc) == Success) {
72             keycodes = XGetAtomName (dpy, desc->names->keycodes);
73             if (!keycodes) {
74                 g_warning("could not lookup keycode name");
75             } else {
76                 trace_xkeymap_keycodes(keycodes);
77             }
78         }
79         XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
80     }
81 
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;
98     } else {
99         trace_xkeymap_keymap("NULL");
100         g_warning("Unknown X11 keycode mapping '%s'.\n"
101                   "Please report to qemu-devel@nongnu.org\n"
102                   "including the following information:\n"
103                   "\n"
104                   "  - Operating system\n"
105                   "  - X11 Server\n"
106                   "  - xprop -root\n"
107                   "  - xdpyinfo\n",
108                   keycodes ? keycodes : "<null>");
109         return NULL;
110     }
111 }
112