diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c6f614568..1e7d64a1f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -204,6 +204,11 @@ if(MINGW) core/ScreenLockListenerWin.h core/ScreenLockListenerWin.cpp) endif() +if(MINGW OR (UNIX AND NOT APPLE)) + set(keepassx_SOURCES + ${keepassx_SOURCES} + core/OSEventFilter.cpp) +endif() set(keepassx_SOURCES_MAINEXE main.cpp) diff --git a/src/core/OSEventFilter.cpp b/src/core/OSEventFilter.cpp new file mode 100644 index 000000000..f6ad6c76a --- /dev/null +++ b/src/core/OSEventFilter.cpp @@ -0,0 +1,27 @@ +#include "OSEventFilter.h" + +#include + +#include "autotype/AutoType.h" + +OSEventFilter::OSEventFilter() +{ +} + +bool OSEventFilter::nativeEventFilter(const QByteArray& eventType, void* message, long* result) +{ + Q_UNUSED(result) + +#if defined(Q_OS_UNIX) + if (eventType == QByteArrayLiteral("xcb_generic_event_t")) { +#elif defined(Q_OS_WIN) + if (eventType == QByteArrayLiteral("windows_generic_MSG") + || eventType == QByteArrayLiteral("windows_dispatcher_MSG")) { +#endif + int retCode = autoType()->callEventFilter(message); + + return retCode == 1; + } + + return false; +} diff --git a/src/core/OSEventFilter.h b/src/core/OSEventFilter.h new file mode 100644 index 000000000..1dca5392e --- /dev/null +++ b/src/core/OSEventFilter.h @@ -0,0 +1,16 @@ +#ifndef OSEVENTFILTER_H +#define OSEVENTFILTER_H +#include + +class QByteArray; + +class OSEventFilter : public QAbstractNativeEventFilter +{ +public: + OSEventFilter(); + bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override; +private: + Q_DISABLE_COPY(OSEventFilter) +}; + +#endif // OSEVENTFILTER_H diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h index dd6f5ea86..ab73a8cf3 100644 --- a/src/core/ScreenLockListenerDBus.h +++ b/src/core/ScreenLockListenerDBus.h @@ -25,7 +25,7 @@ class ScreenLockListenerDBus : public ScreenLockListenerPrivate { Q_OBJECT public: - explicit ScreenLockListenerDBus(QWidget* parent = 0); + explicit ScreenLockListenerDBus(QWidget* parent = nullptr); private slots: void gnomeSessionStatusChanged(uint status); diff --git a/src/gui/Application.cpp b/src/gui/Application.cpp index cdd4df03f..b67f542c6 100644 --- a/src/gui/Application.cpp +++ b/src/gui/Application.cpp @@ -21,7 +21,6 @@ #include "MainWindow.h" #include "core/Config.h" -#include #include #include #include @@ -32,6 +31,10 @@ #include "autotype/AutoType.h" #include "core/Global.h" +#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) +#include "core/OSEventFilter.h" +#endif + #if defined(Q_OS_UNIX) #include #include @@ -42,46 +45,7 @@ namespace { constexpr int WaitTimeoutMSec = 150; const char BlockSizeProperty[] = "blockSize"; -} - -#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) -class XcbEventFilter : public QAbstractNativeEventFilter -{ -public: - bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override - { - Q_UNUSED(result) - - if (eventType == QByteArrayLiteral("xcb_generic_event_t")) { - int retCode = autoType()->callEventFilter(message); - if (retCode == 1) { - return true; - } - } - - return false; - } -}; -#elif defined(Q_OS_WIN) -class WinEventFilter : public QAbstractNativeEventFilter -{ -public: - bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override - { - Q_UNUSED(result); - - if (eventType == QByteArrayLiteral("windows_generic_MSG") - || eventType == QByteArrayLiteral("windows_dispatcher_MSG")) { - int retCode = autoType()->callEventFilter(message); - if (retCode == 1) { - return true; - } - } - - return false; - } -}; -#endif +} // namespace Application::Application(int& argc, char** argv) : QApplication(argc, argv) @@ -91,11 +55,12 @@ Application::Application(int& argc, char** argv) #endif , m_alreadyRunning(false) , m_lockFile(nullptr) +#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) + , m_osEventFilter(new OSEventFilter()) +{ + installNativeEventFilter(m_osEventFilter.data()); +#else { -#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) - installNativeEventFilter(new XcbEventFilter()); -#elif defined(Q_OS_WIN) - installNativeEventFilter(new WinEventFilter()); #endif #if defined(Q_OS_UNIX) registerUnixSignals(); diff --git a/src/gui/Application.h b/src/gui/Application.h index 3fdd8af90..7b1a77f60 100644 --- a/src/gui/Application.h +++ b/src/gui/Application.h @@ -23,6 +23,11 @@ #include #include +#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) +#include + +class OSEventFilter; +#endif class QLockFile; class QSocketNotifier; @@ -33,7 +38,7 @@ class Application : public QApplication public: Application(int& argc, char** argv); QWidget* mainWindow() const; - ~Application(); + ~Application() override; void setMainWindow(QWidget* mainWindow); bool event(QEvent* event) override; @@ -70,6 +75,9 @@ private: QLockFile* m_lockFile; QLocalServer m_lockServer; QString m_socketName; +#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) + QScopedPointer m_osEventFilter; +#endif }; #endif // KEEPASSX_APPLICATION_H