Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the OS event filter implementation #2422

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions src/core/OSEventFilter.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions src/core/OSEventFilter.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/core/ScreenLockListenerDBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 10 additions & 45 deletions src/gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "MainWindow.h"
#include "core/Config.h"

#include <QAbstractNativeEventFilter>
#include <QFileInfo>
#include <QFileOpenEvent>
#include <QLockFile>
Expand All @@ -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>
Expand All @@ -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)
Expand All @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion src/gui/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -70,6 +75,9 @@ private slots:
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