diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui
index 2857559..63a3a22 100644
--- a/HotkeyTest/hottestwidget.ui
+++ b/HotkeyTest/hottestwidget.ui
@@ -7,7 +7,7 @@
0
0
520
- 425
+ 426
@@ -17,7 +17,7 @@
-
- 2
+ 0
@@ -958,7 +958,8 @@
<ul>
<li><u>Windows:</u> Registering on multiple threads is possible, but: The thread that registered must unregister too and events will be send to the thread registered from, not the main thread.</li>
<li><u>Mac:</u> Works fine.</li>
-</ul>
+ <li><u>Linux:</u> Works fine.</li>
+</ul><br/>
</p></body></html>
diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp
index ca8aee0..89bc036 100644
--- a/QHotkey/qhotkey.cpp
+++ b/QHotkey/qhotkey.cpp
@@ -3,10 +3,10 @@
#include
#include
#include
+#include
#define LOCKER QMutexLocker locker(&this->mutex)
-#include
QHotkey::QHotkey(QObject *parent) :
QObject(parent),
key(Qt::Key_unknown),
@@ -96,7 +96,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
else
return true;
} else {
- qWarning("QHotkey: Unable to map shortcut to native keys.");
+ qWarning() << "QHotkey: Unable to map shortcut to native keys. Key:" << key << "Modifiers:" << modifiers;
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
diff --git a/QHotkey/qhotkey.pri b/QHotkey/qhotkey.pri
index cf46f3c..9b4fb20 100644
--- a/QHotkey/qhotkey.pri
+++ b/QHotkey/qhotkey.pri
@@ -12,7 +12,7 @@ mac {
SOURCES += $$PWD/qhotkey_win.cpp
LIBS += -lUser32
} else:unix {
- SOURCES +=
+ SOURCES += $$PWD/qhotkey_x11.cpp
QT += x11extras
LIBS += -lX11
}
diff --git a/QHotkey/qhotkey_mac.cpp b/QHotkey/qhotkey_mac.cpp
index f217c38..e3e7622 100644
--- a/QHotkey/qhotkey_mac.cpp
+++ b/QHotkey/qhotkey_mac.cpp
@@ -206,8 +206,8 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
GetApplicationEventTarget(),
0,
&eventRef);
- if (status != noErr) {
- qWarning() << "QHotkey: Failed to register hotkey with error:" << status;
+ if (status != noErr) {
+ qWarning() << "QHotkey: Failed to register hotkey. Error:" << status;
return false;
} else {
this->hotkeyRefs.insert(shortcut, eventRef);
@@ -219,8 +219,8 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef);
- if (status != noErr) {
- qWarning() << "QHotkey: Failed to unregister hotkey with error:" << status;
+ if (status != noErr) {
+ qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << status;
return false;
} else {
this->hotkeyRefs.remove(shortcut);
diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp
index 951ee40..ede787b 100644
--- a/QHotkey/qhotkey_win.cpp
+++ b/QHotkey/qhotkey_win.cpp
@@ -228,7 +228,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
- qWarning() << "Failed to register hotkey. Error:" << formatWinError(::GetLastError());
+ qWarning() << "QHotkey: Failed to register hotkey. Error:" << qPrintable(formatWinError(::GetLastError()));
return false;
}
}
@@ -239,7 +239,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
- qWarning() << "Failed to unregister hotkey. Error:" << formatWinError(::GetLastError());
+ qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << qPrintable(formatWinError(::GetLastError()));
return false;
}
}
diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp
new file mode 100644
index 0000000..dc2e4c6
--- /dev/null
+++ b/QHotkey/qhotkey_x11.cpp
@@ -0,0 +1,179 @@
+#include "qhotkey.h"
+#include "qhotkey_p.h"
+#include
+#include
+#include
+#include
+#include
+
+class QHotkeyPrivateX11 : public QHotkeyPrivate
+{
+public:
+ // QAbstractNativeEventFilter interface
+ bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
+
+protected:
+ // QHotkeyPrivate interface
+ quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
+ quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
+ bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
+ bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
+
+private:
+ static const QVector specialModifiers;
+ static const quint32 validModsMask;
+
+ static QString formatX11Error(Display *display, int errorCode);
+
+ class HotkeyErrorHandler {
+ public:
+ HotkeyErrorHandler();
+ ~HotkeyErrorHandler();
+
+ static bool hasError;//TODO threading
+ static QString errorString;//TODO threading
+
+ private:
+ XErrorHandler prevHandler;
+
+ static int handleError(Display *display, XErrorEvent *error);
+ };
+};
+NATIVE_INSTANCE(QHotkeyPrivateX11)
+
+const QVector QHotkeyPrivateX11::specialModifiers = {0, Mod2Mask, LockMask, (Mod2Mask | LockMask)};
+const quint32 QHotkeyPrivateX11::validModsMask = ShiftMask | ControlMask | Mod1Mask | Mod4Mask;
+
+bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
+{
+ Q_UNUSED(eventType);
+ Q_UNUSED(result);
+
+ xcb_generic_event_t *genericEvent = static_cast(message);
+ if (genericEvent->response_type == XCB_KEY_PRESS) {
+ xcb_key_press_event_t *keyEvent = static_cast(message);
+ this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask});
+ }
+
+ return false;
+}
+
+quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
+{
+ KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
+ if (keysym == NoSymbol) {
+ //not found -> just use the key
+ if(keycode <= 0xFFFF)
+ keysym = keycode;
+ else
+ return 0;
+ }
+
+ Display *display = QX11Info::display();
+ if(display)
+ return XKeysymToKeycode(QX11Info::display(), keysym);
+ else
+ return 0;
+}
+
+quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
+{
+ quint32 nMods = 0;
+ if (modifiers & Qt::ShiftModifier)
+ nMods |= ShiftMask;
+ if (modifiers & Qt::ControlModifier)
+ nMods |= ControlMask;
+ if (modifiers & Qt::AltModifier)
+ nMods |= Mod1Mask;
+ if (modifiers & Qt::MetaModifier)
+ nMods |= Mod4Mask;
+ return nMods;
+}
+
+bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
+{
+ Display *display = QX11Info::display();
+ if(!display)
+ return false;
+
+ HotkeyErrorHandler errorHandler;
+ for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
+ XGrabKey(display,
+ shortcut.first,
+ shortcut.second | specialMod,
+ DefaultRootWindow(display),
+ True,
+ GrabModeAsync,
+ GrabModeAsync);
+ }
+
+ if(errorHandler.hasError) {
+ qWarning() << "QHotkey: Failed to register hotkey. Error:" << qPrintable(errorHandler.errorString);
+ this->unregisterShortcut(shortcut);
+ return false;
+ } else
+ return true;
+}
+
+bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
+{
+ Display *display = QX11Info::display();
+ if(!display)
+ return false;
+
+ HotkeyErrorHandler errorHandler;
+ for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
+ XUngrabKey(display,
+ shortcut.first,
+ shortcut.second | specialMod,
+ DefaultRootWindow(display));
+ }
+
+ if(errorHandler.hasError) {
+ qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << qPrintable(errorHandler.errorString);
+ this->unregisterShortcut(shortcut);
+ return false;
+ } else
+ return true;
+}
+
+QString QHotkeyPrivateX11::formatX11Error(Display *display, int errorCode)
+{
+ char errStr[256];
+ XGetErrorText(display, errorCode, errStr, 256);
+ return QString::fromLatin1(errStr);
+}
+
+
+
+// ---------- QHotkeyPrivateX11::HotkeyErrorHandler implementation ----------
+
+bool QHotkeyPrivateX11::HotkeyErrorHandler::hasError = false;
+QString QHotkeyPrivateX11::HotkeyErrorHandler::errorString;
+
+QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
+{
+ this->prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
+}
+
+QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
+{
+ XSetErrorHandler(this->prevHandler);
+}
+
+int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
+{
+ switch (error->error_code) {
+ case BadAccess:
+ case BadValue:
+ case BadWindow:
+ if (error->request_code == 33 || //grab key
+ error->request_code == 34) {// ungrab key
+ HotkeyErrorHandler::hasError = true;
+ HotkeyErrorHandler::errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
+ return 1;
+ }
+ default:
+ return 0;
+ }
+}