Extract the OS event filter implementation (#2422)

This commit is contained in:
Gianluca Recchia 2018-10-30 13:46:12 +01:00 committed by Jonathan White
parent fa687f246e
commit 4e1d3bfd73
6 changed files with 68 additions and 47 deletions

View File

@ -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)

View File

@ -0,0 +1,27 @@
#include "OSEventFilter.h"
#include <QByteArray>
#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;
}

16
src/core/OSEventFilter.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef OSEVENTFILTER_H
#define OSEVENTFILTER_H
#include <QAbstractNativeEventFilter>
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

View File

@ -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);

View File

@ -21,7 +21,6 @@
#include "MainWindow.h"
#include "core/Config.h"
#include <QAbstractNativeEventFilter>
#include <QFileInfo>
#include <QFileOpenEvent>
#include <QLockFile>
@ -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 <signal.h>
#include <sys/socket.h>
@ -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();

View File

@ -23,6 +23,11 @@
#include <QApplication>
#include <QtNetwork/QLocalServer>
#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
#include <QScopedPointer>
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<OSEventFilter> m_osEventFilter;
#endif
};
#endif // KEEPASSX_APPLICATION_H