ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 1 | /* |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 2 | * QEMU X11 keymaps |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 3 | * |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com> |
| 5 | * Copyright (C) 2017 Red Hat, Inc |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 6 | * |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 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. |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 10 | */ |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 11 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 13 | |
aliguori | 5368a42 | 2009-03-03 17:37:21 +0000 | [diff] [blame] | 14 | #include "x_keymap.h" |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 15 | #include "trace.h" |
| 16 | #include "qemu/notify.h" |
| 17 | #include "ui/input.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 18 | |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 19 | #include <X11/XKBlib.h> |
Daniel P. Berrangé | 1e70de6 | 2018-03-13 10:42:35 +0000 | [diff] [blame] | 20 | #include <X11/Xutil.h> |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 21 | |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 22 | static gboolean check_for_xwin(Display *dpy) |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 23 | { |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 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; |
aliguori | 5368a42 | 2009-03-03 17:37:21 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 35 | static gboolean check_for_xquartz(Display *dpy) |
aliguori | 5368a42 | 2009-03-03 17:37:21 +0000 | [diff] [blame] | 36 | { |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 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 | { |
| 57 | XkbDescPtr desc; |
| 58 | const gchar *keycodes = NULL; |
| 59 | |
| 60 | /* There is no easy way to determine what X11 server |
| 61 | * and platform & keyboard driver is in use. Thus we |
| 62 | * do best guess heuristics. |
| 63 | * |
| 64 | * This will need more work for people with other |
| 65 | * X servers..... patches welcomed. |
| 66 | */ |
| 67 | |
| 68 | desc = XkbGetMap(dpy, |
| 69 | XkbGBN_AllComponentsMask, |
| 70 | XkbUseCoreKbd); |
| 71 | if (desc) { |
| 72 | if (XkbGetNames(dpy, XkbKeycodesNameMask, desc) == Success) { |
| 73 | keycodes = XGetAtomName (dpy, desc->names->keycodes); |
| 74 | if (!keycodes) { |
| 75 | g_warning("could not lookup keycode name"); |
| 76 | } else { |
| 77 | trace_xkeymap_keycodes(keycodes); |
| 78 | } |
| 79 | } |
| 80 | XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True); |
| 81 | } |
| 82 | |
| 83 | if (check_for_xwin(dpy)) { |
| 84 | trace_xkeymap_keymap("xwin"); |
| 85 | *maplen = qemu_input_map_xorgxwin_to_qcode_len; |
| 86 | return qemu_input_map_xorgxwin_to_qcode; |
| 87 | } else if (check_for_xquartz(dpy)) { |
| 88 | trace_xkeymap_keymap("xquartz"); |
| 89 | *maplen = qemu_input_map_xorgxquartz_to_qcode_len; |
| 90 | return qemu_input_map_xorgxquartz_to_qcode; |
Daniel P. Berrangé | 1e70de6 | 2018-03-13 10:42:35 +0000 | [diff] [blame] | 91 | } else if ((keycodes && g_str_has_prefix(keycodes, "evdev")) || |
| 92 | (XKeysymToKeycode(dpy, XK_Page_Up) == 0x70)) { |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 93 | trace_xkeymap_keymap("evdev"); |
| 94 | *maplen = qemu_input_map_xorgevdev_to_qcode_len; |
| 95 | return qemu_input_map_xorgevdev_to_qcode; |
Daniel P. Berrangé | 1e70de6 | 2018-03-13 10:42:35 +0000 | [diff] [blame] | 96 | } else if ((keycodes && g_str_has_prefix(keycodes, "xfree86")) || |
| 97 | (XKeysymToKeycode(dpy, XK_Page_Up) == 0x63)) { |
Daniel P. Berrange | 2ec7870 | 2018-01-17 16:47:15 +0000 | [diff] [blame] | 98 | trace_xkeymap_keymap("kbd"); |
| 99 | *maplen = qemu_input_map_xorgkbd_to_qcode_len; |
| 100 | return qemu_input_map_xorgkbd_to_qcode; |
| 101 | } else { |
| 102 | trace_xkeymap_keymap("NULL"); |
| 103 | g_warning("Unknown X11 keycode mapping '%s'.\n" |
| 104 | "Please report to qemu-devel@nongnu.org\n" |
| 105 | "including the following information:\n" |
| 106 | "\n" |
| 107 | " - Operating system\n" |
| 108 | " - X11 Server\n" |
| 109 | " - xprop -root\n" |
| 110 | " - xdpyinfo\n", |
| 111 | keycodes ? keycodes : "<null>"); |
| 112 | return NULL; |
| 113 | } |
ths | 6070dd0 | 2007-01-24 21:40:21 +0000 | [diff] [blame] | 114 | } |