2
0

x_keymap.c 3.4 KB

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