Code cleanup in qxtglobalshortcut

This commit is contained in:
Jonas Kvinge
2018-11-27 19:14:35 +01:00
parent 77e5d0288f
commit 3b93963688
9 changed files with 690 additions and 818 deletions

View File

@@ -1,6 +1,13 @@
cmake_minimum_required(VERSION 2.8.11) cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
if (UNIX AND NOT APPLE)
find_package(X11)
if (X11_FOUND)
include_directories(${X11_INCLUDE_DIR})
endif(X11_FOUND)
endif(NOT APPLE)
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS}) include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
if (NOT WIN32 AND NOT APPLE) if (NOT WIN32 AND NOT APPLE)
find_path(HAVE_QPA_QPLATFORMNATIVEINTERFACE_H qpa/qplatformnativeinterface.h PATHS ${Qt5Gui_PRIVATE_INCLUDE_DIRS}) find_path(HAVE_QPA_QPLATFORMNATIVEINTERFACE_H qpa/qplatformnativeinterface.h PATHS ${Qt5Gui_PRIVATE_INCLUDE_DIRS})
@@ -9,25 +16,16 @@ if (NOT WIN32 AND NOT APPLE)
endif(NOT HAVE_QPA_QPLATFORMNATIVEINTERFACE_H) endif(NOT HAVE_QPA_QPLATFORMNATIVEINTERFACE_H)
endif(NOT WIN32 AND NOT APPLE) endif(NOT WIN32 AND NOT APPLE)
set(QXT-SOURCES set(QXT-SOURCES qxtglobal.cpp qxtglobalshortcut.cpp)
qxtglobal.cpp set(QXT-MOC-HEADERS qxtglobalshortcut.h )
qxtglobalshortcut.cpp
)
set(QXT-MOC-HEADERS if(X11_FOUND)
qxtglobalshortcut.h set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_x11.cpp)
)
find_package(X11)
include_directories(${X11_INCLUDE_DIR})
if(WIN32)
set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_win.cpp)
elseif(APPLE) elseif(APPLE)
set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_mac.cpp) set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_mac.cpp)
else(WIN32) elseif(WIN32)
set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_x11.cpp) set(QXT-SOURCES ${QXT-SOURCES} qxtglobalshortcut_win.cpp)
endif(WIN32) endif()
QT5_WRAP_CPP(QXT-SOURCES-MOC ${QXT-MOC-HEADERS}) QT5_WRAP_CPP(QXT-SOURCES-MOC ${QXT-MOC-HEADERS})
@@ -36,8 +34,8 @@ ADD_LIBRARY(qxt STATIC
${QXT-SOURCES-MOC} ${QXT-SOURCES-MOC}
) )
if(WIN32) target_link_libraries(qxt Qt5::Core Qt5::Widgets)
target_link_libraries(qxt Qt5::Core Qt5::Widgets)
else(WIN32) if(X11_FOUND)
target_link_libraries(qxt Qt5::Core Qt5::Widgets Qt5::X11Extras) target_link_libraries(qxt Qt5::X11Extras)
endif(WIN32) endif(X11_FOUND)

View File

@@ -1,4 +1,3 @@
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.

View File

@@ -1,4 +1,3 @@
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.

View File

@@ -38,40 +38,27 @@
#include "qxtglobalshortcut_p.h" #include "qxtglobalshortcut_p.h"
bool QxtGlobalShortcutPrivate::error = false; bool QxtGlobalShortcutPrivate::error = false;
#ifndef Q_OS_MAC #ifndef Q_OS_MACOS
int QxtGlobalShortcutPrivate::ref = 0; int QxtGlobalShortcutPrivate::ref = 0;
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #endif // Q_OS_MACOS
QAbstractEventDispatcher::EventFilter QxtGlobalShortcutPrivate::prevEventFilter = 0;
#endif
#endif // Q_OS_MAC
QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> QxtGlobalShortcutPrivate::shortcuts; QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> QxtGlobalShortcutPrivate::shortcuts;
QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() : enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier) QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() : enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier) {
{ #ifndef Q_OS_MACOS
#ifndef Q_OS_MAC
if (!ref++) if (!ref++)
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
prevEventFilter = QAbstractEventDispatcher::instance()->setEventFilter(eventFilter);
#else
QAbstractEventDispatcher::instance()->installNativeEventFilter(this); QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
#endif #endif // Q_OS_MACOS
#endif // Q_OS_MAC
} }
QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate() QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate() {
{ #ifndef Q_OS_MACOS
#ifndef Q_OS_MAC
if (!--ref) if (!--ref)
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
QAbstractEventDispatcher::instance()->setEventFilter(prevEventFilter);
#else
QAbstractEventDispatcher::instance()->removeNativeEventFilter(this); QAbstractEventDispatcher::instance()->removeNativeEventFilter(this);
#endif #endif // Q_OS_MACOS
#endif // Q_OS_MAC
} }
bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut) bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut) {
{
Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier; Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;
key = shortcut.isEmpty() ? Qt::Key(0) : Qt::Key((shortcut[0] ^ allMods) & shortcut[0]); key = shortcut.isEmpty() ? Qt::Key(0) : Qt::Key((shortcut[0] ^ allMods) & shortcut[0]);
mods = shortcut.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(shortcut[0] & allMods); mods = shortcut.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(shortcut[0] & allMods);
@@ -83,10 +70,10 @@ bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut)
else else
qWarning() << "QxtGlobalShortcut failed to register:" << QKeySequence(key + mods).toString(); qWarning() << "QxtGlobalShortcut failed to register:" << QKeySequence(key + mods).toString();
return res; return res;
} }
bool QxtGlobalShortcutPrivate::unsetShortcut() bool QxtGlobalShortcutPrivate::unsetShortcut() {
{
bool res = false; bool res = false;
const quint32 nativeKey = nativeKeycode(key); const quint32 nativeKey = nativeKeycode(key);
const quint32 nativeMods = nativeModifiers(mods); const quint32 nativeMods = nativeModifiers(mods);
@@ -101,8 +88,7 @@ bool QxtGlobalShortcutPrivate::unsetShortcut()
return res; return res;
} }
void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods) void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods) {
{
QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods)); QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods));
if (shortcut && shortcut->isEnabled()) if (shortcut && shortcut->isEnabled())
emit shortcut->activated(); emit shortcut->activated();
@@ -139,18 +125,14 @@ void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativ
/*! /*!
Constructs a new QxtGlobalShortcut with \a parent. Constructs a new QxtGlobalShortcut with \a parent.
*/ */
QxtGlobalShortcut::QxtGlobalShortcut(QObject* parent) QxtGlobalShortcut::QxtGlobalShortcut(QObject* parent) : QObject(parent) {
: QObject(parent)
{
QXT_INIT_PRIVATE(QxtGlobalShortcut); QXT_INIT_PRIVATE(QxtGlobalShortcut);
} }
/*! /*!
Constructs a new QxtGlobalShortcut with \a shortcut and \a parent. Constructs a new QxtGlobalShortcut with \a shortcut and \a parent.
*/ */
QxtGlobalShortcut::QxtGlobalShortcut(const QKeySequence& shortcut, QObject* parent) QxtGlobalShortcut::QxtGlobalShortcut(const QKeySequence& shortcut, QObject* parent) : QObject(parent) {
: QObject(parent)
{
QXT_INIT_PRIVATE(QxtGlobalShortcut); QXT_INIT_PRIVATE(QxtGlobalShortcut);
setShortcut(shortcut); setShortcut(shortcut);
} }
@@ -158,10 +140,8 @@ QxtGlobalShortcut::QxtGlobalShortcut(const QKeySequence& shortcut, QObject* pare
/*! /*!
Destructs the QxtGlobalShortcut. Destructs the QxtGlobalShortcut.
*/ */
QxtGlobalShortcut::~QxtGlobalShortcut() QxtGlobalShortcut::~QxtGlobalShortcut() {
{ if (qxt_d().key != 0) qxt_d().unsetShortcut();
if (qxt_d().key != 0)
qxt_d().unsetShortcut();
} }
/*! /*!
@@ -178,13 +158,11 @@ QxtGlobalShortcut::~QxtGlobalShortcut()
Q_ASSERT(qxtShortcut->shortcut() == QKeySequence("Ctrl+Alt+A")); Q_ASSERT(qxtShortcut->shortcut() == QKeySequence("Ctrl+Alt+A"));
\endcode \endcode
*/ */
QKeySequence QxtGlobalShortcut::shortcut() const QKeySequence QxtGlobalShortcut::shortcut() const {
{
return QKeySequence(qxt_d().key | qxt_d().mods); return QKeySequence(qxt_d().key | qxt_d().mods);
} }
bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut) bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut) {
{
if (qxt_d().key != 0) if (qxt_d().key != 0)
qxt_d().unsetShortcut(); qxt_d().unsetShortcut();
return qxt_d().setShortcut(shortcut); return qxt_d().setShortcut(shortcut);
@@ -200,13 +178,11 @@ bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut)
\sa setDisabled() \sa setDisabled()
*/ */
bool QxtGlobalShortcut::isEnabled() const bool QxtGlobalShortcut::isEnabled() const {
{
return qxt_d().enabled; return qxt_d().enabled;
} }
void QxtGlobalShortcut::setEnabled(bool enabled) void QxtGlobalShortcut::setEnabled(bool enabled) {
{
qxt_d().enabled = enabled; qxt_d().enabled = enabled;
} }
@@ -215,8 +191,7 @@ void QxtGlobalShortcut::setEnabled(bool enabled)
\sa enabled \sa enabled
*/ */
void QxtGlobalShortcut::setDisabled(bool disabled) void QxtGlobalShortcut::setDisabled(bool disabled) {
{
qxt_d().enabled = !disabled; qxt_d().enabled = !disabled;
} }

View File

@@ -1,4 +1,3 @@
#ifndef QXTGLOBALSHORTCUT_H
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
@@ -29,6 +28,7 @@
** <http://libqxt.org> <foundation@libqxt.org> ** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/ *****************************************************************************/
#ifndef QXTGLOBALSHORTCUT_H
#define QXTGLOBALSHORTCUT_H #define QXTGLOBALSHORTCUT_H
#include <QObject> #include <QObject>
@@ -39,8 +39,7 @@
class QxtGlobalShortcutPrivate; class QxtGlobalShortcutPrivate;
class QXT_GUI_EXPORT QxtGlobalShortcut : public QObject class QXT_GUI_EXPORT QxtGlobalShortcut : public QObject {
{
Q_OBJECT Q_OBJECT
QXT_DECLARE_PRIVATE(QxtGlobalShortcut) QXT_DECLARE_PRIVATE(QxtGlobalShortcut)
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)

View File

@@ -1,4 +1,3 @@
#include <Carbon/Carbon.h>
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
@@ -29,9 +28,12 @@
** <http://libqxt.org> <foundation@libqxt.org> ** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/ *****************************************************************************/
#include <Carbon/Carbon.h>
#include "qxtglobalshortcut_p.h" #include "qxtglobalshortcut_p.h"
#include <QMap> #include <QMap>
#include <QHash> #include <QHash>
#include <QPair>
#include <QtDebug> #include <QtDebug>
#include <QApplication> #include <QApplication>
@@ -41,126 +43,79 @@ static QHash<Identifier, quint32> keyIDs;
static quint32 hotKeySerial = 0; static quint32 hotKeySerial = 0;
static bool qxt_mac_handler_installed = false; static bool qxt_mac_handler_installed = false;
OSStatus qxt_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void* data) OSStatus qxt_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void* data) {
{
Q_UNUSED(nextHandler); Q_UNUSED(nextHandler);
Q_UNUSED(data); Q_UNUSED(data);
if (GetEventClass(event) == kEventClassKeyboard && GetEventKind(event) == kEventHotKeyPressed) if (GetEventClass(event) == kEventClassKeyboard && GetEventKind(event) == kEventHotKeyPressed) {
{
EventHotKeyID keyID; EventHotKeyID keyID;
GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, nullptr, sizeof(keyID), nullptr, &keyID); GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, nullptr, sizeof(keyID), nullptr, &keyID);
Identifier id = keyIDs.key(keyID.id); Identifier id = keyIDs.key(keyID.id);
QxtGlobalShortcutPrivate::activateShortcut(id.second, id.first); QxtGlobalShortcutPrivate::activateShortcut(id.second, id.first);
} }
return noErr; return noErr;
} }
quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) {
{
quint32 native = 0; quint32 native = 0;
if (modifiers & Qt::ShiftModifier) if (modifiers & Qt::ShiftModifier) native |= shiftKey;
native |= shiftKey; if (modifiers & Qt::ControlModifier) native |= cmdKey;
if (modifiers & Qt::ControlModifier) if (modifiers & Qt::AltModifier) native |= optionKey;
native |= cmdKey; if (modifiers & Qt::MetaModifier) native |= controlKey;
if (modifiers & Qt::AltModifier) if (modifiers & Qt::KeypadModifier) native |= kEventKeyModifierNumLockMask;
native |= optionKey;
if (modifiers & Qt::MetaModifier)
native |= controlKey;
if (modifiers & Qt::KeypadModifier)
native |= kEventKeyModifierNumLockMask;
return native; return native;
} }
quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) {
{
UTF16Char ch; UTF16Char ch;
// Constants found in NSEvent.h from AppKit.framework // Constants found in NSEvent.h from AppKit.framework
switch (key) switch (key) {
{ case Qt::Key_Return: return kVK_Return;
case Qt::Key_Return: case Qt::Key_Enter: return kVK_ANSI_KeypadEnter;
return kVK_Return; case Qt::Key_Tab: return kVK_Tab;
case Qt::Key_Enter: case Qt::Key_Space: return kVK_Space;
return kVK_ANSI_KeypadEnter; case Qt::Key_Backspace: return kVK_Delete;
case Qt::Key_Tab: case Qt::Key_Control: return kVK_Command;
return kVK_Tab; case Qt::Key_Shift: return kVK_Shift;
case Qt::Key_Space: case Qt::Key_CapsLock: return kVK_CapsLock;
return kVK_Space; case Qt::Key_Option: return kVK_Option;
case Qt::Key_Backspace: case Qt::Key_Meta: return kVK_Control;
return kVK_Delete; case Qt::Key_F17: return kVK_F17;
case Qt::Key_Control: case Qt::Key_VolumeUp: return kVK_VolumeUp;
return kVK_Command; case Qt::Key_VolumeDown: return kVK_VolumeDown;
case Qt::Key_Shift: case Qt::Key_F18: return kVK_F18;
return kVK_Shift; case Qt::Key_F19: return kVK_F19;
case Qt::Key_CapsLock: case Qt::Key_F20: return kVK_F20;
return kVK_CapsLock; case Qt::Key_F5: return kVK_F5;
case Qt::Key_Option: case Qt::Key_F6: return kVK_F6;
return kVK_Option; case Qt::Key_F7: return kVK_F7;
case Qt::Key_Meta: case Qt::Key_F3: return kVK_F3;
return kVK_Control; case Qt::Key_F8: return kVK_F8;
case Qt::Key_F17: case Qt::Key_F9: return kVK_F9;
return kVK_F17; case Qt::Key_F11: return kVK_F11;
case Qt::Key_VolumeUp: case Qt::Key_F13: return kVK_F13;
return kVK_VolumeUp; case Qt::Key_F16: return kVK_F16;
case Qt::Key_VolumeDown: case Qt::Key_F14: return kVK_F14;
return kVK_VolumeDown; case Qt::Key_F10: return kVK_F10;
case Qt::Key_F18: case Qt::Key_F12: return kVK_F12;
return kVK_F18; case Qt::Key_F15: return kVK_F15;
case Qt::Key_F19: case Qt::Key_Help: return kVK_Help;
return kVK_F19; case Qt::Key_Home: return kVK_Home;
case Qt::Key_F20: case Qt::Key_PageUp: return kVK_PageUp;
return kVK_F20; case Qt::Key_Delete: return kVK_ForwardDelete;
case Qt::Key_F5: case Qt::Key_F4: return kVK_F4;
return kVK_F5; case Qt::Key_End: return kVK_End;
case Qt::Key_F6: case Qt::Key_F2: return kVK_F2;
return kVK_F6; case Qt::Key_PageDown: return kVK_PageDown;
case Qt::Key_F7: case Qt::Key_F1: return kVK_F1;
return kVK_F7; case Qt::Key_Left: return kVK_LeftArrow;
case Qt::Key_F3: case Qt::Key_Right: return kVK_RightArrow;
return kVK_F3; case Qt::Key_Down: return kVK_DownArrow;
case Qt::Key_F8: case Qt::Key_Up: return kVK_UpArrow;
return kVK_F8;
case Qt::Key_F9:
return kVK_F9;
case Qt::Key_F11:
return kVK_F11;
case Qt::Key_F13:
return kVK_F13;
case Qt::Key_F16:
return kVK_F16;
case Qt::Key_F14:
return kVK_F14;
case Qt::Key_F10:
return kVK_F10;
case Qt::Key_F12:
return kVK_F12;
case Qt::Key_F15:
return kVK_F15;
case Qt::Key_Help:
return kVK_Help;
case Qt::Key_Home:
return kVK_Home;
case Qt::Key_PageUp:
return kVK_PageUp;
case Qt::Key_Delete:
return kVK_ForwardDelete;
case Qt::Key_F4:
return kVK_F4;
case Qt::Key_End:
return kVK_End;
case Qt::Key_F2:
return kVK_F2;
case Qt::Key_PageDown:
return kVK_PageDown;
case Qt::Key_F1:
return kVK_F1;
case Qt::Key_Left:
return kVK_LeftArrow;
case Qt::Key_Right:
return kVK_RightArrow;
case Qt::Key_Down:
return kVK_DownArrow;
case Qt::Key_Up:
return kVK_UpArrow;
default: default:
; ;
} }
@@ -187,11 +142,9 @@ quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
uint8_t *data = (uint8_t*)header; uint8_t *data = (uint8_t*)header;
// God, would a little documentation for this shit kill you... // God, would a little documentation for this shit kill you...
for (quint32 i=0; i < header->keyboardTypeCount; i++) for (quint32 i=0; i < header->keyboardTypeCount; i++) {
{
UCKeyStateRecordsIndex* stateRec = 0; UCKeyStateRecordsIndex* stateRec = 0;
if (table[i].keyStateRecordsIndexOffset != 0) if (table[i].keyStateRecordsIndexOffset != 0) {
{
stateRec = reinterpret_cast<UCKeyStateRecordsIndex*>(data + table[i].keyStateRecordsIndexOffset); stateRec = reinterpret_cast<UCKeyStateRecordsIndex*>(data + table[i].keyStateRecordsIndexOffset);
if (stateRec->keyStateRecordsIndexFormat != kUCKeyStateRecordsIndexFormat) stateRec = 0; if (stateRec->keyStateRecordsIndexFormat != kUCKeyStateRecordsIndexFormat) stateRec = 0;
} }
@@ -199,34 +152,29 @@ quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
UCKeyToCharTableIndex* charTable = reinterpret_cast<UCKeyToCharTableIndex*>(data + table[i].keyToCharTableIndexOffset); UCKeyToCharTableIndex* charTable = reinterpret_cast<UCKeyToCharTableIndex*>(data + table[i].keyToCharTableIndexOffset);
if (charTable->keyToCharTableIndexFormat != kUCKeyToCharTableIndexFormat) continue; if (charTable->keyToCharTableIndexFormat != kUCKeyToCharTableIndexFormat) continue;
for (quint32 j=0; j < charTable->keyToCharTableCount; j++) for (quint32 j=0; j < charTable->keyToCharTableCount; j++) {
{
UCKeyOutput* keyToChar = reinterpret_cast<UCKeyOutput*>(data + charTable->keyToCharTableOffsets[j]); UCKeyOutput* keyToChar = reinterpret_cast<UCKeyOutput*>(data + charTable->keyToCharTableOffsets[j]);
for (quint32 k=0; k < charTable->keyToCharTableSize; k++) for (quint32 k=0; k < charTable->keyToCharTableSize; k++) {
{ if (keyToChar[k] & kUCKeyOutputTestForIndexMask) {
if (keyToChar[k] & kUCKeyOutputTestForIndexMask)
{
long idx = keyToChar[k] & kUCKeyOutputGetIndexMask; long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
if (stateRec && idx < stateRec->keyStateRecordCount) if (stateRec && idx < stateRec->keyStateRecordCount) {
{
UCKeyStateRecord* rec = reinterpret_cast<UCKeyStateRecord*>(data + stateRec->keyStateRecordOffsets[idx]); UCKeyStateRecord* rec = reinterpret_cast<UCKeyStateRecord*>(data + stateRec->keyStateRecordOffsets[idx]);
if (rec->stateZeroCharData == ch) return k; if (rec->stateZeroCharData == ch) return k;
} }
} }
else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE) else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE) {
{
if (keyToChar[k] == ch) return k; if (keyToChar[k] == ch) return k;
} }
} // for k } // for k
} // for j } // for j
} // for i } // for i
return 0; return 0;
} }
bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) {
{
if (!qxt_mac_handler_installed) if (!qxt_mac_handler_installed) {
{
EventTypeSpec t; EventTypeSpec t;
t.eventClass = kEventClassKeyboard; t.eventClass = kEventClassKeyboard;
t.eventKind = kEventHotKeyPressed; t.eventKind = kEventHotKeyPressed;
@@ -239,20 +187,21 @@ bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativ
EventHotKeyRef ref = 0; EventHotKeyRef ref = 0;
bool rv = !RegisterEventHotKey(nativeKey, nativeMods, keyID, GetApplicationEventTarget(), 0, &ref); bool rv = !RegisterEventHotKey(nativeKey, nativeMods, keyID, GetApplicationEventTarget(), 0, &ref);
if (rv) if (rv) {
{
keyIDs.insert(Identifier(nativeMods, nativeKey), keyID.id); keyIDs.insert(Identifier(nativeMods, nativeKey), keyID.id);
keyRefs.insert(keyID.id, ref); keyRefs.insert(keyID.id, ref);
} }
return rv; return rv;
} }
bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) {
{
Identifier id(nativeMods, nativeKey); Identifier id(nativeMods, nativeKey);
if (!keyIDs.contains(id)) return false; if (!keyIDs.contains(id)) return false;
EventHotKeyRef ref = keyRefs.take(keyIDs[id]); EventHotKeyRef ref = keyRefs.take(keyIDs[id]);
keyIDs.remove(id); keyIDs.remove(id);
return !UnregisterEventHotKey(ref); return !UnregisterEventHotKey(ref);
} }

View File

@@ -1,4 +1,3 @@
#ifndef QXTGLOBALSHORTCUT_P_H
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
@@ -29,23 +28,17 @@
** <http://libqxt.org> <foundation@libqxt.org> ** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/ *****************************************************************************/
#ifndef QXTGLOBALSHORTCUT_P_H
#define QXTGLOBALSHORTCUT_P_H #define QXTGLOBALSHORTCUT_P_H
#include "qxtglobalshortcut.h" #include "qxtglobalshortcut.h"
#include <QAbstractEventDispatcher>
#include <QKeySequence>
#include <QHash> #include <QHash>
#include <QAbstractEventDispatcher>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QAbstractNativeEventFilter> #include <QAbstractNativeEventFilter>
#endif #include <QKeySequence>
class QxtGlobalShortcutPrivate : public QxtPrivate<QxtGlobalShortcut>, public QAbstractNativeEventFilter {
class QxtGlobalShortcutPrivate : public QxtPrivate<QxtGlobalShortcut>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
,public QAbstractNativeEventFilter
#endif
{
public: public:
QXT_DECLARE_PUBLIC(QxtGlobalShortcut) QXT_DECLARE_PUBLIC(QxtGlobalShortcut)
QxtGlobalShortcutPrivate(); QxtGlobalShortcutPrivate();
@@ -59,14 +52,9 @@ public:
bool unsetShortcut(); bool unsetShortcut();
static bool error; static bool error;
#ifndef Q_OS_MAC #ifndef Q_OS_MACOS
static int ref; static int ref;
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
static QAbstractEventDispatcher::EventFilter prevEventFilter;
static bool eventFilter(void* message);
#else
virtual bool nativeEventFilter(const QByteArray & eventType, void * message, long * result); virtual bool nativeEventFilter(const QByteArray & eventType, void * message, long * result);
#endif // QT_VERSION < QT_VERSION_CHECK(5,0,0)
#endif // Q_OS_MAC #endif // Q_OS_MAC
static void activateShortcut(quint32 nativeKey, quint32 nativeMods); static void activateShortcut(quint32 nativeKey, quint32 nativeMods);

View File

@@ -31,30 +31,23 @@
#include <qt_windows.h> #include <qt_windows.h>
bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType, void * message, long * result) {
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
bool QxtGlobalShortcutPrivate::eventFilter(void* message)
{
#else
bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
void * message, long * result)
{
Q_UNUSED(eventType); Q_UNUSED(eventType);
Q_UNUSED(result); Q_UNUSED(result);
#endif
MSG* msg = static_cast<MSG*>(message); MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_HOTKEY) if (msg->message == WM_HOTKEY) {
{
const quint32 keycode = HIWORD(msg->lParam); const quint32 keycode = HIWORD(msg->lParam);
const quint32 modifiers = LOWORD(msg->lParam); const quint32 modifiers = LOWORD(msg->lParam);
activateShortcut(keycode, modifiers); activateShortcut(keycode, modifiers);
} }
return false; return false;
} }
quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) {
{
// MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WIN // MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WIN
quint32 native = 0; quint32 native = 0;
if (modifiers & Qt::ShiftModifier) if (modifiers & Qt::ShiftModifier)
@@ -69,12 +62,12 @@ quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifier
//if (modifiers & Qt::KeypadModifier) //if (modifiers & Qt::KeypadModifier)
//if (modifiers & Qt::GroupSwitchModifier) //if (modifiers & Qt::GroupSwitchModifier)
return native; return native;
} }
quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) {
{
switch (key) switch (key) {
{
case Qt::Key_Escape: case Qt::Key_Escape:
return VK_ESCAPE; return VK_ESCAPE;
case Qt::Key_Tab: case Qt::Key_Tab:
@@ -234,14 +227,13 @@ quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
default: default:
return 0; return 0;
} }
} }
bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) {
{
return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey); return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey);
} }
bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) {
{
return UnregisterHotKey(0, nativeMods ^ nativeKey); return UnregisterHotKey(0, nativeMods ^ nativeKey);
} }

View File

@@ -1,4 +1,3 @@
#include "qxtglobalshortcut_p.h"
/**************************************************************************** /****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project. ** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
@@ -29,30 +28,29 @@
** <http://libqxt.org> <foundation@libqxt.org> ** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/ *****************************************************************************/
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) #include "qxtglobalshortcut_p.h"
# include <QX11Info>
#else
# include <qpa/qplatformnativeinterface.h>
# include <xcb/xcb.h>
# include <QApplication>
#endif
#include <X11/X.h>
#include <X11/Xlib.h>
#include <xcb/xproto.h>
#include <QtGlobal> #include <QtGlobal>
#include <QByteArray> #include <QApplication>
#include <QGuiApplication> #include <QGuiApplication>
#include <QKeySequence> #include <QKeySequence>
#include <QByteArray>
#include <QString> #include <QString>
#include <QVector> #include <QVector>
#include <QX11Info>
#include <qpa/qplatformnativeinterface.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include "keymapper_x11.h" #include "keymapper_x11.h"
namespace { namespace {
const QVector<quint32> maskModifiers = QVector<quint32>() const QVector<quint32> maskModifiers = QVector<quint32>() << 0 << Mod2Mask << LockMask << (Mod2Mask | LockMask);
<< 0 << Mod2Mask << LockMask << (Mod2Mask | LockMask);
typedef int (*X11ErrorHandler)(Display *display, XErrorEvent *event); typedef int (*X11ErrorHandler)(Display *display, XErrorEvent *event);
@@ -60,11 +58,9 @@ class QxtX11ErrorHandler {
public: public:
static bool error; static bool error;
static int qxtX11ErrorHandler(Display *display, XErrorEvent *event) static int qxtX11ErrorHandler(Display *display, XErrorEvent *event) {
{
Q_UNUSED(display); Q_UNUSED(display);
switch (event->error_code) switch (event->error_code) {
{
case BadAccess: case BadAccess:
case BadValue: case BadValue:
case BadWindow: case BadWindow:
@@ -80,14 +76,12 @@ public:
return 0; return 0;
} }
QxtX11ErrorHandler() QxtX11ErrorHandler() {
{
error = false; error = false;
m_previousErrorHandler = XSetErrorHandler(qxtX11ErrorHandler); m_previousErrorHandler = XSetErrorHandler(qxtX11ErrorHandler);
} }
~QxtX11ErrorHandler() ~QxtX11ErrorHandler() {
{
XSetErrorHandler(m_previousErrorHandler); XSetErrorHandler(m_previousErrorHandler);
} }
@@ -99,41 +93,31 @@ bool QxtX11ErrorHandler::error = false;
class QxtX11Data { class QxtX11Data {
public: public:
QxtX11Data() QxtX11Data() {
{
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
m_display = QX11Info::display();
#else
QPlatformNativeInterface *native = qApp->platformNativeInterface(); QPlatformNativeInterface *native = qApp->platformNativeInterface();
void *display = native->nativeResourceForScreen(QByteArray("display"), //void *display = native->nativeResourceForScreen(QByteArray("display"), QGuiApplication::primaryScreen());
QGuiApplication::primaryScreen()); //m_display = reinterpret_cast<Display *>(display);
m_display = reinterpret_cast<Display *>(display); m_display = QX11Info::display();
#endif
} }
bool isValid() bool isValid() {
{
return m_display != nullptr; return m_display != nullptr;
} }
Display *display() Display *display() {
{
Q_ASSERT(isValid()); Q_ASSERT(isValid());
return m_display; return m_display;
} }
Window rootWindow() Window rootWindow() {
{
return DefaultRootWindow(display()); return DefaultRootWindow(display());
} }
bool grabKey(quint32 keycode, quint32 modifiers, Window window) bool grabKey(quint32 keycode, quint32 modifiers, Window window) {
{
QxtX11ErrorHandler errorHandler; QxtX11ErrorHandler errorHandler;
for (int i = 0; !errorHandler.error && i < maskModifiers.size(); ++i) { for (int i = 0; !errorHandler.error && i < maskModifiers.size(); ++i) {
XGrabKey(display(), keycode, modifiers | maskModifiers[i], window, True, XGrabKey(display(), keycode, modifiers | maskModifiers[i], window, True, GrabModeAsync, GrabModeAsync);
GrabModeAsync, GrabModeAsync);
} }
if (errorHandler.error) { if (errorHandler.error) {
@@ -144,8 +128,7 @@ public:
return true; return true;
} }
bool ungrabKey(quint32 keycode, quint32 modifiers, Window window) bool ungrabKey(quint32 keycode, quint32 modifiers, Window window) {
{
QxtX11ErrorHandler errorHandler; QxtX11ErrorHandler errorHandler;
foreach (quint32 maskMods, maskModifiers) { foreach (quint32 maskMods, maskModifiers) {
@@ -161,19 +144,8 @@ private:
} // namespace } // namespace
#if QT_VERSION < QT_VERSION_CHECK(5,0,0) bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType, void *message, long *result) {
bool QxtGlobalShortcutPrivate::eventFilter(void *message)
{
XEvent *event = static_cast<XEvent *>(message);
if (event->type == KeyPress)
{
XKeyEvent *key = reinterpret_cast<XKeyEvent *>(event);
unsigned int keycode = key->keycode;
unsigned int keystate = key->state;
#else
bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
void *message, long *result)
{
Q_UNUSED(result); Q_UNUSED(result);
xcb_key_press_event_t *kev = nullptr; xcb_key_press_event_t *kev = nullptr;
@@ -194,16 +166,17 @@ bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
keystate |= Mod4Mask; keystate |= Mod4Mask;
if(kev->state & XCB_MOD_MASK_SHIFT) if(kev->state & XCB_MOD_MASK_SHIFT)
keystate |= ShiftMask; keystate |= ShiftMask;
#endif
activateShortcut(keycode, activateShortcut(keycode,
// Mod1Mask == Alt, Mod4Mask == Meta // Mod1Mask == Alt, Mod4Mask == Meta
keystate & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask)); keystate & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask));
} }
return false; return false;
} }
quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) {
{
// ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask // ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask
quint32 native = 0; quint32 native = 0;
if (modifiers & Qt::ShiftModifier) if (modifiers & Qt::ShiftModifier)
@@ -220,10 +193,11 @@ quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifier
//if (modifiers & Qt::KeypadModifier) //if (modifiers & Qt::KeypadModifier)
//if (modifiers & Qt::GroupSwitchModifier) //if (modifiers & Qt::GroupSwitchModifier)
return native; return native;
} }
quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key) {
{
// (davidsansome) Try the table from QKeyMapper first - this seems to be // (davidsansome) Try the table from QKeyMapper first - this seems to be
// the only way to get Keysyms for the media keys. // the only way to get Keysyms for the media keys.
unsigned int keysym = 0; unsigned int keysym = 0;
@@ -248,16 +222,15 @@ quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
return 0; return 0;
return XKeysymToKeycode(x11.display(), keysym); return XKeysymToKeycode(x11.display(), keysym);
} }
bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) {
{
QxtX11Data x11; QxtX11Data x11;
return x11.isValid() && x11.grabKey(nativeKey, nativeMods, x11.rootWindow()); return x11.isValid() && x11.grabKey(nativeKey, nativeMods, x11.rootWindow());
} }
bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) {
{
QxtX11Data x11; QxtX11Data x11;
return x11.isValid() && x11.ungrabKey(nativeKey, nativeMods, x11.rootWindow()); return x11.isValid() && x11.ungrabKey(nativeKey, nativeMods, x11.rootWindow());
} }