diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b3d190..f85f8be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,15 +7,27 @@ option(QHOTKEY_INSTALL "Enable install rule" ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -find_package(Qt5 COMPONENTS Core Widgets REQUIRED) +set(QT_MAJOR 5 CACHE STRING "Qt major version to use") -qt5_wrap_cpp(MOC_HEADERS - QHotkey/qhotkey.h - QHotkey/qhotkey_p.h) +if(QT_MAJOR EQUAL 6) + find_package(Qt${QT_MAJOR} 6.2.0 COMPONENTS Core Widgets REQUIRED) +else() + find_package(Qt${QT_MAJOR} COMPONENTS Core Widgets REQUIRED) +endif() + +if(QT_MAJOR GREATER_EQUAL 6) + qt_wrap_cpp(MOC_HEADERS + QHotkey/qhotkey.h + QHotkey/qhotkey_p.h) +else() + qt5_wrap_cpp(MOC_HEADERS + QHotkey/qhotkey.h + QHotkey/qhotkey_p.h) +endif() set(LIBS - Qt5::Core - Qt5::Widgets) + Qt${QT_MAJOR}::Core + Qt${QT_MAJOR}::Widgets) set(SRC_FILES QHotkey/qhotkey.cpp) @@ -30,10 +42,14 @@ elseif(WIN32) set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_win.cpp) else() find_package(X11 REQUIRED) - find_package(Qt5X11Extras REQUIRED) + if(QT_MAJOR GREATER_EQUAL 6) + set(LIBS ${LIBS} ${X11_LIBRARIES}) + else() + find_package(Qt${QT_MAJOR}X11Extras REQUIRED) + set(LIBS ${LIBS} ${X11_LIBRARIES} Qt${QT_MAJOR}::X11Extras) + endif() include_directories(${X11_INCLUDE_DIR}) - set(LIBS ${LIBS} ${X11_LIBRARIES} Qt5::X11Extras) set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_x11.cpp) endif() diff --git a/HotkeyTest/CMakeLists.txt b/HotkeyTest/CMakeLists.txt index 67dfc8e..6915fb3 100644 --- a/HotkeyTest/CMakeLists.txt +++ b/HotkeyTest/CMakeLists.txt @@ -1,12 +1,17 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) -qt5_wrap_ui(test_UI_HEADERS hottestwidget.ui) -qt5_wrap_cpp(test_MOC_HEADERS hottestwidget.h) +if(QT_MAJOR GREATER_EQUAL 6) + qt_wrap_ui(test_UI_HEADERS hottestwidget.ui) + qt_wrap_cpp(test_MOC_HEADERS hottestwidget.h) +else() + qt5_wrap_ui(test_UI_HEADERS hottestwidget.ui) + qt5_wrap_cpp(test_MOC_HEADERS hottestwidget.h) +endif() add_executable(HotkeyTest main.cpp hottestwidget.cpp ${test_UI_HEADERS} ${test_MOC_HEADERS}) -target_link_libraries(HotkeyTest Qt5::Widgets qhotkey) +target_link_libraries(HotkeyTest Qt${QT_MAJOR}::Widgets qhotkey) target_include_directories(HotkeyTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index ae6da22..e1ce190 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -10,9 +10,15 @@ Q_LOGGING_CATEGORY(logQHotkey, "QHotkey") void QHotkey::addGlobalMapping(const QKeySequence &shortcut, QHotkey::NativeShortcut nativeShortcut) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const int key = shortcut[0].toCombined(); +#else + const int key = shortcut[0]; +#endif + QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection, - Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)), - Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask)), + Q_ARG(Qt::Key, Qt::Key(key & ~Qt::KeyboardModifierMask)), + Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask)), Q_ARG(QHotkey::NativeShortcut, nativeShortcut)); } @@ -56,7 +62,12 @@ QKeySequence QHotkey::shortcut() const { if(_keyCode == Qt::Key_unknown) return QKeySequence(); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + return QKeySequence((_keyCode | _modifiers).toCombined()); +#else return QKeySequence(static_cast(_keyCode | _modifiers)); +#endif } Qt::Key QHotkey::keyCode() const @@ -88,8 +99,14 @@ bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister) "Only the first shortcut will be used!"); } - return setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask), - Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask), +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const int key = shortcut[0].toCombined(); +#else + const int key = shortcut[0]; +#endif + + return setShortcut(Qt::Key(key & ~Qt::KeyboardModifierMask), + Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask), autoRegister); } @@ -354,7 +371,7 @@ uint qHash(QHotkey::NativeShortcut key) return qHash(key.key) ^ qHash(key.modifier); } -uint qHash(QHotkey::NativeShortcut key, uint seed) +QHOTKEY_HASH_SEED qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed) { return qHash(key.key, seed) ^ qHash(key.modifier, seed); } diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index f669640..f21a2c8 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -16,6 +16,12 @@ #define QHOTKEY_SHARED_EXPORT #endif +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + #define QHOTKEY_HASH_SEED size_t +#else + #define QHOTKEY_HASH_SEED uint +#endif + //! A class to define global, systemwide Hotkeys class QHOTKEY_SHARED_EXPORT QHotkey : public QObject { @@ -115,7 +121,7 @@ private: }; uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key); -uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, uint seed); +QHOTKEY_HASH_SEED QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed); QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey) diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp index e3f8519..fc06c7a 100644 --- a/QHotkey/qhotkey_x11.cpp +++ b/QHotkey/qhotkey_x11.cpp @@ -1,7 +1,17 @@ #include "qhotkey.h" #include "qhotkey_p.h" -#include -#include + +#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) + #include + + #define _NATIVE_EVENT_RESULT qintptr +#else + #include + #include + + #define _NATIVE_EVENT_RESULT long +#endif + #include #include #include @@ -16,7 +26,7 @@ class QHotkeyPrivateX11 : public QHotkeyPrivate { public: // QAbstractNativeEventFilter interface - bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; + bool nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) override; protected: // QHotkeyPrivate interface @@ -52,13 +62,17 @@ NATIVE_INSTANCE(QHotkeyPrivateX11) bool QHotkeyPrivate::isPlatformSupported() { +#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) + return qGuiApp->nativeInterface(); +#else return QX11Info::isPlatformX11(); +#endif } 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) +bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) { Q_UNUSED(eventType) Q_UNUSED(result) @@ -120,8 +134,16 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok) return 0; } - if(QX11Info::isPlatformX11()) { - auto res = XKeysymToKeycode(QX11Info::display(), keysym); +#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) + const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface(); + Display *display = x11Interface->display(); +#else + const bool x11Interface = QX11Info::isPlatformX11(); + Display *display = QX11Info::display(); +#endif + + if(x11Interface) { + auto res = XKeysymToKeycode(display, keysym); if(res != 0) ok = true; return res; @@ -146,8 +168,15 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) + const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface(); + Display *display = x11Interface->display(); +#else + const bool x11Interface = QX11Info::isPlatformX11(); Display *display = QX11Info::display(); - if(!display || !QX11Info::isPlatformX11()) +#endif + + if(!display || !x11Interface) return false; HotkeyErrorHandler errorHandler; @@ -172,7 +201,12 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) + Display *display = qGuiApp->nativeInterface()->display(); +#else Display *display = QX11Info::display(); +#endif + if(!display) return false; diff --git a/qhotkey.prc b/qhotkey.prc index 30e2b04..d8e8ac0 100644 --- a/qhotkey.prc +++ b/qhotkey.prc @@ -1,6 +1,15 @@ mac: LIBS += -framework Carbon else:win32: LIBS += -luser32 else:unix { - QT += x11extras + equals(QT_MAJOR_VERSION, 6) { + lessThan(QT_VERSION, 6.2.0) { + error("Qt 6.2.0 or greater is required when using Qt6") + } + } + else { + lessThan(QT_MAJOR_VERSION, 6) { + QT += x11extras + } + } LIBS += -lX11 }