qhotkey_x11.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "qhotkey.h"
  2. #include "qhotkey_p.h"
  3. #include <QDebug>
  4. #include <QThreadStorage>
  5. #include <QTimer>
  6. #include <QX11Info>
  7. #include <X11/Xlib.h>
  8. #include <xcb/xcb.h>
  9. // compability to pre Qt 5.8
  10. #ifndef Q_FALLTHROUGH
  11. #define Q_FALLTHROUGH() (void)0
  12. #endif
  13. class QHotkeyPrivateX11 : public QHotkeyPrivate
  14. {
  15. public:
  16. // QAbstractNativeEventFilter interface
  17. bool nativeEventFilter(const QByteArray& eventType,
  18. void* message,
  19. long* result) Q_DECL_OVERRIDE;
  20. protected:
  21. // QHotkeyPrivate interface
  22. quint32 nativeKeycode(Qt::Key keycode, bool& ok) Q_DECL_OVERRIDE;
  23. quint32 nativeModifiers(Qt::KeyboardModifiers modifiers,
  24. bool& ok) Q_DECL_OVERRIDE;
  25. static QString getX11String(Qt::Key keycode);
  26. bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
  27. bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
  28. private:
  29. static const QVector<quint32> specialModifiers;
  30. static const quint32 validModsMask;
  31. xcb_key_press_event_t prevHandledEvent;
  32. xcb_key_press_event_t prevEvent;
  33. static QString formatX11Error(Display* display, int errorCode);
  34. class HotkeyErrorHandler
  35. {
  36. public:
  37. HotkeyErrorHandler();
  38. ~HotkeyErrorHandler();
  39. static bool hasError;
  40. static QString errorString;
  41. private:
  42. XErrorHandler prevHandler;
  43. static int handleError(Display* display, XErrorEvent* error);
  44. };
  45. };
  46. NATIVE_INSTANCE(QHotkeyPrivateX11)
  47. bool QHotkeyPrivate::isPlatformSupported()
  48. {
  49. return QX11Info::isPlatformX11();
  50. }
  51. const QVector<quint32> QHotkeyPrivateX11::specialModifiers = { 0,
  52. Mod2Mask,
  53. LockMask,
  54. (Mod2Mask |
  55. LockMask) };
  56. const quint32 QHotkeyPrivateX11::validModsMask =
  57. ShiftMask | ControlMask | Mod1Mask | Mod4Mask;
  58. bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray& eventType,
  59. void* message,
  60. long* result)
  61. {
  62. Q_UNUSED(eventType)
  63. Q_UNUSED(result)
  64. auto* genericEvent = static_cast<xcb_generic_event_t*>(message);
  65. if (genericEvent->response_type == XCB_KEY_PRESS) {
  66. xcb_key_press_event_t keyEvent =
  67. *static_cast<xcb_key_press_event_t*>(message);
  68. this->prevEvent = keyEvent;
  69. if (this->prevHandledEvent.response_type == XCB_KEY_RELEASE) {
  70. if (this->prevHandledEvent.time == keyEvent.time)
  71. return false;
  72. }
  73. this->activateShortcut(
  74. { keyEvent.detail,
  75. keyEvent.state & QHotkeyPrivateX11::validModsMask });
  76. } else if (genericEvent->response_type == XCB_KEY_RELEASE) {
  77. xcb_key_release_event_t keyEvent =
  78. *static_cast<xcb_key_release_event_t*>(message);
  79. this->prevEvent = keyEvent;
  80. QTimer::singleShot(50, [this, keyEvent] {
  81. if (this->prevEvent.time == keyEvent.time &&
  82. this->prevEvent.response_type == keyEvent.response_type &&
  83. this->prevEvent.detail == keyEvent.detail) {
  84. this->releaseShortcut(
  85. { keyEvent.detail,
  86. keyEvent.state & QHotkeyPrivateX11::validModsMask });
  87. }
  88. });
  89. this->prevHandledEvent = keyEvent;
  90. }
  91. return false;
  92. }
  93. QString QHotkeyPrivateX11::getX11String(Qt::Key keycode)
  94. {
  95. switch (keycode) {
  96. case Qt::Key_MediaLast:
  97. case Qt::Key_MediaPrevious:
  98. return QStringLiteral("XF86AudioPrev");
  99. case Qt::Key_MediaNext:
  100. return QStringLiteral("XF86AudioNext");
  101. case Qt::Key_MediaPause:
  102. case Qt::Key_MediaPlay:
  103. case Qt::Key_MediaTogglePlayPause:
  104. return QStringLiteral("XF86AudioPlay");
  105. case Qt::Key_MediaRecord:
  106. return QStringLiteral("XF86AudioRecord");
  107. case Qt::Key_MediaStop:
  108. return QStringLiteral("XF86AudioStop");
  109. default:
  110. return QKeySequence(keycode).toString(QKeySequence::NativeText);
  111. }
  112. }
  113. quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool& ok)
  114. {
  115. QString keyString = getX11String(keycode);
  116. KeySym keysym = XStringToKeysym(keyString.toLatin1().constData());
  117. if (keysym == NoSymbol) {
  118. // not found -> just use the key
  119. if (keycode <= 0xFFFF)
  120. keysym = keycode;
  121. else
  122. return 0;
  123. }
  124. if (QX11Info::isPlatformX11()) {
  125. auto res = XKeysymToKeycode(QX11Info::display(), keysym);
  126. if (res != 0)
  127. ok = true;
  128. return res;
  129. }
  130. return 0;
  131. }
  132. quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers,
  133. bool& ok)
  134. {
  135. quint32 nMods = 0;
  136. if (modifiers & Qt::ShiftModifier)
  137. nMods |= ShiftMask;
  138. if (modifiers & Qt::ControlModifier)
  139. nMods |= ControlMask;
  140. if (modifiers & Qt::AltModifier)
  141. nMods |= Mod1Mask;
  142. if (modifiers & Qt::MetaModifier)
  143. nMods |= Mod4Mask;
  144. ok = true;
  145. return nMods;
  146. }
  147. bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
  148. {
  149. Display* display = QX11Info::display();
  150. if (!display || !QX11Info::isPlatformX11())
  151. return false;
  152. HotkeyErrorHandler errorHandler;
  153. for (quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
  154. XGrabKey(display,
  155. shortcut.key,
  156. shortcut.modifier | specialMod,
  157. DefaultRootWindow(display),
  158. True,
  159. GrabModeAsync,
  160. GrabModeAsync);
  161. }
  162. XSync(display, False);
  163. if (errorHandler.hasError) {
  164. error = errorHandler.errorString;
  165. this->unregisterShortcut(shortcut);
  166. return false;
  167. }
  168. return true;
  169. }
  170. bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
  171. {
  172. Display* display = QX11Info::display();
  173. if (!display)
  174. return false;
  175. HotkeyErrorHandler errorHandler;
  176. for (quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
  177. XUngrabKey(display,
  178. shortcut.key,
  179. shortcut.modifier | specialMod,
  180. XDefaultRootWindow(display));
  181. }
  182. XSync(display, False);
  183. if (HotkeyErrorHandler::hasError) {
  184. error = HotkeyErrorHandler::errorString;
  185. return false;
  186. }
  187. return true;
  188. }
  189. QString QHotkeyPrivateX11::formatX11Error(Display* display, int errorCode)
  190. {
  191. char errStr[256];
  192. XGetErrorText(display, errorCode, errStr, 256);
  193. return QString::fromLatin1(errStr);
  194. }
  195. // ---------- QHotkeyPrivateX11::HotkeyErrorHandler implementation ----------
  196. bool QHotkeyPrivateX11::HotkeyErrorHandler::hasError = false;
  197. QString QHotkeyPrivateX11::HotkeyErrorHandler::errorString;
  198. QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
  199. {
  200. prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
  201. }
  202. QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
  203. {
  204. XSetErrorHandler(prevHandler);
  205. hasError = false;
  206. errorString.clear();
  207. }
  208. int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display* display,
  209. XErrorEvent* error)
  210. {
  211. switch (error->error_code) {
  212. case BadAccess:
  213. case BadValue:
  214. case BadWindow:
  215. if (error->request_code == 33 || // grab key
  216. error->request_code == 34) { // ungrab key
  217. hasError = true;
  218. errorString =
  219. QHotkeyPrivateX11::formatX11Error(display, error->error_code);
  220. return 1;
  221. }
  222. Q_FALLTHROUGH();
  223. // fall through
  224. default:
  225. return 0;
  226. }
  227. }