| 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.1 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 | #include <X11/Xutil.h> |
| 21 | |
| 22 | static gboolean check_for_xwin(Display *dpy) |
| 23 | { |
| 24 | const char *vendor = ServerVendor(dpy); |
| 25 | |
| 26 | trace_xkeymap_vendor(vendor); |
| 27 | |
| 28 | if (strstr(vendor, "Cygwin/X")) { |
| 29 | return TRUE; |
| 30 | } |
| 31 | |
| 32 | return FALSE; |
| 33 | } |
| 34 | |
| 35 | static gboolean check_for_xquartz(Display *dpy) |
| 36 | { |
| 37 | int nextensions; |
| 38 | int i; |
| 39 | gboolean match = FALSE; |
| 40 | char **extensions = XListExtensions(dpy, &nextensions); |
| 41 | for (i = 0 ; extensions != NULL && i < nextensions ; i++) { |
| 42 | trace_xkeymap_extension(extensions[i]); |
| 43 | if (strcmp(extensions[i], "Apple-WM") == 0 || |
| 44 | strcmp(extensions[i], "Apple-DRI") == 0) { |
| 45 | match = TRUE; |
| 46 | } |
| 47 | } |
| 48 | if (extensions) { |
| 49 | XFreeExtensionList(extensions); |
| 50 | } |
| 51 | |
| 52 | return match; |
| 53 | } |
| 54 | |
| 55 | const guint16 *qemu_xkeymap_mapping_table(Display *dpy, size_t *maplen, |
| 56 | bool *evdev) |
| 57 | { |
| 58 | XkbDescPtr desc; |
| 59 | const gchar *keycodes = NULL; |
| 60 | const guint16 *map = NULL; |
| 61 | |
| 62 | /* There is no easy way to determine what X11 server |
| 63 | * and platform & keyboard driver is in use. Thus we |
| 64 | * do best guess heuristics. |
| 65 | * |
| 66 | * This will need more work for people with other |
| 67 | * X servers..... patches welcomed. |
| 68 | */ |
| 69 | |
| 70 | desc = XkbGetMap(dpy, |
| 71 | XkbGBN_AllComponentsMask, |
| 72 | XkbUseCoreKbd); |
| 73 | if (desc) { |
| 74 | if (XkbGetNames(dpy, XkbKeycodesNameMask, desc) == Success) { |
| 75 | keycodes = XGetAtomName (dpy, desc->names->keycodes); |
| 76 | if (!keycodes) { |
| 77 | g_warning("could not lookup keycode name"); |
| 78 | } else { |
| 79 | trace_xkeymap_keycodes(keycodes); |
| 80 | } |
| 81 | } |
| 82 | XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True); |
| 83 | } |
| 84 | |
| 85 | *maplen = 0; |
| 86 | *evdev = false; |
| 87 | |
| 88 | if (check_for_xwin(dpy)) { |
| 89 | trace_xkeymap_keymap("xwin"); |
| 90 | *maplen = qemu_input_map_xorgxwin_to_linux_len; |
| 91 | map = qemu_input_map_xorgxwin_to_linux; |
| 92 | } else if (check_for_xquartz(dpy)) { |
| 93 | trace_xkeymap_keymap("xquartz"); |
| 94 | *maplen = qemu_input_map_xorgxquartz_to_linux_len; |
| 95 | map = qemu_input_map_xorgxquartz_to_linux; |
| 96 | } else if ((keycodes && g_str_has_prefix(keycodes, "evdev")) || |
| 97 | (XKeysymToKeycode(dpy, XK_Page_Up) == 0x70)) { |
| 98 | trace_xkeymap_keymap("evdev"); |
| 99 | *evdev = true; |
| 100 | } else if ((keycodes && g_str_has_prefix(keycodes, "xfree86")) || |
| 101 | (XKeysymToKeycode(dpy, XK_Page_Up) == 0x63)) { |
| 102 | trace_xkeymap_keymap("kbd"); |
| 103 | *maplen = qemu_input_map_xorgkbd_to_linux_len; |
| 104 | map = qemu_input_map_xorgkbd_to_linux; |
| 105 | } else { |
| 106 | trace_xkeymap_keymap("NULL"); |
| 107 | g_warning("Unknown X11 keycode mapping '%s'.\n" |
| 108 | "Please report to qemu-devel@nongnu.org\n" |
| 109 | "including the following information:\n" |
| 110 | "\n" |
| 111 | " - Operating system\n" |
| 112 | " - X11 Server\n" |
| 113 | " - xprop -root\n" |
| 114 | " - xdpyinfo\n", |
| 115 | keycodes ? keycodes : "<null>"); |
| 116 | } |
| 117 | if (keycodes) { |
| 118 | XFree((void *)keycodes); |
| 119 | } |
| 120 | return map; |
| 121 | } |