x_keymap.c 3.3 KB

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