added X11 implementation

This commit is contained in:
Skycoder42
2016-02-06 19:31:50 +01:00
parent fc66c9229f
commit 7a9d6b6467
6 changed files with 192 additions and 12 deletions
+4 -3
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>520</width>
<height>425</height>
<height>426</height>
</rect>
</property>
<property name="windowTitle">
@@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>2</number>
<number>0</number>
</property>
<widget class="QWidget" name="playTab">
<attribute name="title">
@@ -958,7 +958,8 @@
&lt;ul&gt;
&lt;li&gt;&lt;u&gt;Windows:&lt;/u&gt; 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.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;Mac:&lt;/u&gt; Works fine.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Linux:&lt;/u&gt; Works fine.&lt;/li&gt;
&lt;/ul&gt;&lt;br/&gt;
&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textFormat">
+2 -2
View File
@@ -3,10 +3,10 @@
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QMetaMethod>
#include <QDebug>
#define LOCKER QMutexLocker locker(&this->mutex)
#include <QDebug>
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();
+1 -1
View File
@@ -12,7 +12,7 @@ mac {
SOURCES += $$PWD/qhotkey_win.cpp
LIBS += -lUser32
} else:unix {
SOURCES +=
SOURCES += $$PWD/qhotkey_x11.cpp
QT += x11extras
LIBS += -lX11
}
+2 -2
View File
@@ -207,7 +207,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
0,
&eventRef);
if (status != noErr) {
qWarning() << "QHotkey: Failed to register hotkey with error:" << status;
qWarning() << "QHotkey: Failed to register hotkey. Error:" << status;
return false;
} else {
this->hotkeyRefs.insert(shortcut, eventRef);
@@ -220,7 +220,7 @@ 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;
qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << status;
return false;
} else {
this->hotkeyRefs.remove(shortcut);
+2 -2
View File
@@ -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;
}
}
+179
View File
@@ -0,0 +1,179 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
#include <QDebug>
#include <QX11Info>
#include <QThreadStorage>
#include <X11/Xlib.h>
#include <xcb/xcb.h>
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<quint32> 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<quint32> 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<xcb_generic_event_t *>(message);
if (genericEvent->response_type == XCB_KEY_PRESS) {
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(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;
}
}