Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouib <code@louib.net>2020-10-03 19:16:35 +0300
committerJonathan White <support@dmapps.us>2020-10-05 06:44:45 +0300
commit9bffe05020f5820773ad8b922de1b32613e6b743 (patch)
treec2af703dd47716bb63cddbf63ed3e3242c552790 /src/gui/osutils
parent8f846758745c2255f4498dcba6f612a36d2ae43b (diff)
Moving all OS utils to gui/osutils
The classes used for screen lock detection use QWidget and are only ever used by the GUI, so moving them there so we can eventually build core/ without Qt5::Widgets.
Diffstat (limited to 'src/gui/osutils')
-rw-r--r--src/gui/osutils/OSEventFilter.cpp47
-rw-r--r--src/gui/osutils/OSEventFilter.h35
-rw-r--r--src/gui/osutils/ScreenLockListener.cpp30
-rw-r--r--src/gui/osutils/ScreenLockListener.h39
-rw-r--r--src/gui/osutils/ScreenLockListenerPrivate.cpp42
-rw-r--r--src/gui/osutils/ScreenLockListenerPrivate.h36
-rw-r--r--src/gui/osutils/macutils/ScreenLockListenerMac.cpp65
-rw-r--r--src/gui/osutils/macutils/ScreenLockListenerMac.h44
-rw-r--r--src/gui/osutils/nixutils/ScreenLockListenerDBus.cpp132
-rw-r--r--src/gui/osutils/nixutils/ScreenLockListenerDBus.h39
-rw-r--r--src/gui/osutils/winutils/ScreenLockListenerWin.cpp88
-rw-r--r--src/gui/osutils/winutils/ScreenLockListenerWin.h38
12 files changed, 635 insertions, 0 deletions
diff --git a/src/gui/osutils/OSEventFilter.cpp b/src/gui/osutils/OSEventFilter.cpp
new file mode 100644
index 000000000..f1f4d97a9
--- /dev/null
+++ b/src/gui/osutils/OSEventFilter.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
+ * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "OSEventFilter.h"
+
+#include <QByteArray>
+
+#include "autotype/AutoType.h"
+#include "gui/MainWindow.h"
+#ifdef Q_OS_WIN
+#include <windows.h>
+#endif
+
+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
+ return autoType()->callEventFilter(message) == 1;
+ }
+
+ return false;
+}
diff --git a/src/gui/osutils/OSEventFilter.h b/src/gui/osutils/OSEventFilter.h
new file mode 100644
index 000000000..10434c0c2
--- /dev/null
+++ b/src/gui/osutils/OSEventFilter.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
+ * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#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
diff --git a/src/gui/osutils/ScreenLockListener.cpp b/src/gui/osutils/ScreenLockListener.cpp
new file mode 100644
index 000000000..2c1ba055a
--- /dev/null
+++ b/src/gui/osutils/ScreenLockListener.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ScreenLockListener.h"
+#include "ScreenLockListenerPrivate.h"
+
+ScreenLockListener::ScreenLockListener(QWidget* parent)
+ : QObject(parent)
+{
+ m_listener = ScreenLockListenerPrivate::instance(parent);
+ connect(m_listener, SIGNAL(screenLocked()), this, SIGNAL(screenLocked()));
+}
+
+ScreenLockListener::~ScreenLockListener()
+{
+}
diff --git a/src/gui/osutils/ScreenLockListener.h b/src/gui/osutils/ScreenLockListener.h
new file mode 100644
index 000000000..107d342a6
--- /dev/null
+++ b/src/gui/osutils/ScreenLockListener.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCREENLOCKLISTENER_H
+#define SCREENLOCKLISTENER_H
+#include <QWidget>
+
+class ScreenLockListenerPrivate;
+
+class ScreenLockListener : public QObject
+{
+ Q_OBJECT
+
+public:
+ ScreenLockListener(QWidget* parent = nullptr);
+ ~ScreenLockListener();
+
+signals:
+ void screenLocked();
+
+private:
+ ScreenLockListenerPrivate* m_listener;
+};
+
+#endif // SCREENLOCKLISTENER_H
diff --git a/src/gui/osutils/ScreenLockListenerPrivate.cpp b/src/gui/osutils/ScreenLockListenerPrivate.cpp
new file mode 100644
index 000000000..21cae07ec
--- /dev/null
+++ b/src/gui/osutils/ScreenLockListenerPrivate.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ScreenLockListenerPrivate.h"
+#if defined(Q_OS_MACOS)
+#include "macutils/ScreenLockListenerMac.h"
+#elif defined(Q_OS_UNIX)
+#include "nixutils/ScreenLockListenerDBus.h"
+#elif defined(Q_OS_WIN)
+#include "winutils/ScreenLockListenerWin.h"
+#endif
+
+ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent)
+ : QObject(parent)
+{
+}
+
+ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent)
+{
+#if defined(Q_OS_MACOS)
+ Q_UNUSED(parent);
+ return ScreenLockListenerMac::instance();
+#elif defined(Q_OS_UNIX)
+ return new ScreenLockListenerDBus(parent);
+#elif defined(Q_OS_WIN)
+ return new ScreenLockListenerWin(parent);
+#endif
+}
diff --git a/src/gui/osutils/ScreenLockListenerPrivate.h b/src/gui/osutils/ScreenLockListenerPrivate.h
new file mode 100644
index 000000000..a7c080687
--- /dev/null
+++ b/src/gui/osutils/ScreenLockListenerPrivate.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCREENLOCKLISTENERPRIVATE_H
+#define SCREENLOCKLISTENERPRIVATE_H
+#include <QObject>
+#include <QWidget>
+
+class ScreenLockListenerPrivate : public QObject
+{
+ Q_OBJECT
+public:
+ static ScreenLockListenerPrivate* instance(QWidget* parent = nullptr);
+
+protected:
+ ScreenLockListenerPrivate(QWidget* parent = nullptr);
+
+signals:
+ void screenLocked();
+};
+
+#endif // SCREENLOCKLISTENERPRIVATE_H
diff --git a/src/gui/osutils/macutils/ScreenLockListenerMac.cpp b/src/gui/osutils/macutils/ScreenLockListenerMac.cpp
new file mode 100644
index 000000000..08e4d1eee
--- /dev/null
+++ b/src/gui/osutils/macutils/ScreenLockListenerMac.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ScreenLockListenerMac.h"
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <QMutexLocker>
+
+ScreenLockListenerMac* ScreenLockListenerMac::instance()
+{
+ static QMutex mutex;
+ QMutexLocker lock(&mutex);
+
+ static ScreenLockListenerMac* m_ptr = nullptr;
+ if (m_ptr == nullptr) {
+ m_ptr = new ScreenLockListenerMac();
+ }
+ return m_ptr;
+}
+
+void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef,
+ void*,
+ CFStringRef,
+ const void*,
+ CFDictionaryRef)
+{
+ instance()->onSignalReception();
+}
+
+ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent)
+ : ScreenLockListenerPrivate(parent)
+{
+ CFNotificationCenterRef distCenter;
+ CFStringRef screenIsLockedSignal = CFSTR("com.apple.screenIsLocked");
+ distCenter = CFNotificationCenterGetDistributedCenter();
+ if (nullptr == distCenter) {
+ return;
+ }
+
+ CFNotificationCenterAddObserver(distCenter,
+ this,
+ &ScreenLockListenerMac::notificationCenterCallBack,
+ screenIsLockedSignal,
+ nullptr,
+ CFNotificationSuspensionBehaviorDeliverImmediately);
+}
+
+void ScreenLockListenerMac::onSignalReception()
+{
+ emit screenLocked();
+}
diff --git a/src/gui/osutils/macutils/ScreenLockListenerMac.h b/src/gui/osutils/macutils/ScreenLockListenerMac.h
new file mode 100644
index 000000000..b07529430
--- /dev/null
+++ b/src/gui/osutils/macutils/ScreenLockListenerMac.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCREENLOCKLISTENERMAC_H
+#define SCREENLOCKLISTENERMAC_H
+#include <QObject>
+#include <QWidget>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "gui/osutils/ScreenLockListenerPrivate.h"
+
+class ScreenLockListenerMac : public ScreenLockListenerPrivate
+{
+ Q_OBJECT
+
+public:
+ static ScreenLockListenerMac* instance();
+ static void notificationCenterCallBack(CFNotificationCenterRef center,
+ void* observer,
+ CFStringRef name,
+ const void* object,
+ CFDictionaryRef userInfo);
+
+private:
+ ScreenLockListenerMac(QWidget* parent = nullptr);
+ void onSignalReception();
+};
+
+#endif // SCREENLOCKLISTENERMAC_H
diff --git a/src/gui/osutils/nixutils/ScreenLockListenerDBus.cpp b/src/gui/osutils/nixutils/ScreenLockListenerDBus.cpp
new file mode 100644
index 000000000..2086e3302
--- /dev/null
+++ b/src/gui/osutils/nixutils/ScreenLockListenerDBus.cpp
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ScreenLockListenerDBus.h"
+
+#include <QDBusConnection>
+#include <QDBusInterface>
+#include <QDBusMessage>
+#include <QDBusReply>
+#include <QDebug>
+#include <QProcessEnvironment>
+
+ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget* parent)
+ : ScreenLockListenerPrivate(parent)
+{
+ QDBusConnection sessionBus = QDBusConnection::sessionBus();
+ QDBusConnection systemBus = QDBusConnection::systemBus();
+
+ sessionBus.connect("org.freedesktop.ScreenSaver", // service
+ "/org/freedesktop/ScreenSaver", // path
+ "org.freedesktop.ScreenSaver", // interface
+ "ActiveChanged", // signal name
+ this, // receiver
+ SLOT(freedesktopScreenSaver(bool)));
+
+ sessionBus.connect("org.gnome.ScreenSaver", // service
+ "/org/gnome/ScreenSaver", // path
+ "org.gnome.ScreenSaver", // interface
+ "ActiveChanged", // signal name
+ this, // receiver
+ SLOT(freedesktopScreenSaver(bool)));
+
+ sessionBus.connect("org.gnome.SessionManager", // service
+ "/org/gnome/SessionManager/Presence", // path
+ "org.gnome.SessionManager.Presence", // interface
+ "StatusChanged", // signal name
+ this, // receiver
+ SLOT(gnomeSessionStatusChanged(uint)));
+
+ sessionBus.connect("org.xfce.ScreenSaver", // service
+ "/org/xfce/ScreenSaver", // path
+ "org.xfce.ScreenSaver", // interface
+ "ActiveChanged", // signal name
+ this, // receiver
+ SLOT(freedesktopScreenSaver(bool)));
+
+ systemBus.connect("org.freedesktop.login1", // service
+ "/org/freedesktop/login1", // path
+ "org.freedesktop.login1.Manager", // interface
+ "PrepareForSleep", // signal name
+ this, // receiver
+ SLOT(logindPrepareForSleep(bool)));
+
+ QString sessionId = QProcessEnvironment::systemEnvironment().value("XDG_SESSION_ID");
+ QDBusInterface loginManager("org.freedesktop.login1", // service
+ "/org/freedesktop/login1", // path
+ "org.freedesktop.login1.Manager", // interface
+ systemBus);
+ if (loginManager.isValid()) {
+ QList<QVariant> args = {sessionId};
+ loginManager.callWithCallback("GetSession", args, this, SLOT(login1SessionObjectReceived(QDBusMessage)));
+ }
+
+ sessionBus.connect("com.canonical.Unity", // service
+ "/com/canonical/Unity/Session", // path
+ "com.canonical.Unity.Session", // interface
+ "Locked", // signal name
+ this, // receiver
+ SLOT(unityLocked()));
+}
+
+void ScreenLockListenerDBus::login1SessionObjectReceived(QDBusMessage response)
+{
+ if (response.arguments().isEmpty()) {
+ qDebug() << "org.freedesktop.login1.Manager.GetSession did not return results";
+ return;
+ }
+ QVariant arg0 = response.arguments().at(0);
+ if (!arg0.canConvert<QDBusObjectPath>()) {
+ qDebug() << "org.freedesktop.login1.Manager.GetSession did not return a QDBusObjectPath";
+ return;
+ }
+ QDBusObjectPath path = arg0.value<QDBusObjectPath>();
+ QDBusConnection systemBus = QDBusConnection::systemBus();
+
+ systemBus.connect("", // service
+ path.path(), // path
+ "org.freedesktop.login1.Session", // interface
+ "Lock", // signal name
+ this, // receiver
+ SLOT(unityLocked()));
+}
+
+void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status)
+{
+ if (status != 0) {
+ emit screenLocked();
+ }
+}
+
+void ScreenLockListenerDBus::logindPrepareForSleep(bool beforeSleep)
+{
+ if (beforeSleep) {
+ emit screenLocked();
+ }
+}
+
+void ScreenLockListenerDBus::unityLocked()
+{
+ emit screenLocked();
+}
+
+void ScreenLockListenerDBus::freedesktopScreenSaver(bool status)
+{
+ if (status) {
+ emit screenLocked();
+ }
+}
diff --git a/src/gui/osutils/nixutils/ScreenLockListenerDBus.h b/src/gui/osutils/nixutils/ScreenLockListenerDBus.h
new file mode 100644
index 000000000..0e473c0cc
--- /dev/null
+++ b/src/gui/osutils/nixutils/ScreenLockListenerDBus.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCREENLOCKLISTENERDBUS_H
+#define SCREENLOCKLISTENERDBUS_H
+#include "gui/osutils/ScreenLockListenerPrivate.h"
+#include <QDBusMessage>
+#include <QObject>
+#include <QWidget>
+
+class ScreenLockListenerDBus : public ScreenLockListenerPrivate
+{
+ Q_OBJECT
+public:
+ explicit ScreenLockListenerDBus(QWidget* parent = nullptr);
+
+private slots:
+ void gnomeSessionStatusChanged(uint status);
+ void logindPrepareForSleep(bool beforeSleep);
+ void unityLocked();
+ void freedesktopScreenSaver(bool status);
+ void login1SessionObjectReceived(QDBusMessage);
+};
+
+#endif // SCREENLOCKLISTENERDBUS_H
diff --git a/src/gui/osutils/winutils/ScreenLockListenerWin.cpp b/src/gui/osutils/winutils/ScreenLockListenerWin.cpp
new file mode 100644
index 000000000..05d01f4cc
--- /dev/null
+++ b/src/gui/osutils/winutils/ScreenLockListenerWin.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ScreenLockListenerWin.h"
+#include <QApplication>
+#include <windows.h>
+#include <wtsapi32.h>
+
+/*
+ * See https://msdn.microsoft.com/en-us/library/windows/desktop/aa373196(v=vs.85).aspx
+ * See https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx
+ * See https://blogs.msdn.microsoft.com/oldnewthing/20060104-50/?p=32783
+ */
+ScreenLockListenerWin::ScreenLockListenerWin(QWidget* parent)
+ : ScreenLockListenerPrivate(parent)
+ , QAbstractNativeEventFilter()
+{
+ Q_ASSERT(parent != nullptr);
+ // On windows, we need to register for platform specific messages and
+ // install a message handler for them
+ QCoreApplication::instance()->installNativeEventFilter(this);
+
+ // This call requests a notification from windows when a laptop is closed
+ HPOWERNOTIFY hPnotify = RegisterPowerSettingNotification(
+ reinterpret_cast<HWND>(parent->winId()), &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
+ m_powerNotificationHandle = reinterpret_cast<void*>(hPnotify);
+
+ // This call requests a notification for session changes
+ if (!WTSRegisterSessionNotification(reinterpret_cast<HWND>(parent->winId()), NOTIFY_FOR_THIS_SESSION)) {
+ }
+}
+
+ScreenLockListenerWin::~ScreenLockListenerWin()
+{
+ HWND h = reinterpret_cast<HWND>(static_cast<QWidget*>(parent())->winId());
+ WTSUnRegisterSessionNotification(h);
+
+ if (m_powerNotificationHandle) {
+ UnregisterPowerSettingNotification(reinterpret_cast<HPOWERNOTIFY>(m_powerNotificationHandle));
+ }
+}
+
+bool ScreenLockListenerWin::nativeEventFilter(const QByteArray& eventType, void* message, long*)
+{
+ if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") {
+ MSG* m = static_cast<MSG*>(message);
+ if (m->message == WM_POWERBROADCAST) {
+ if (m->wParam == PBT_POWERSETTINGCHANGE) {
+ const POWERBROADCAST_SETTING* setting = reinterpret_cast<const POWERBROADCAST_SETTING*>(m->lParam);
+ if (setting != nullptr && setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) {
+ const DWORD* state = reinterpret_cast<const DWORD*>(&setting->Data);
+ if (*state == 0) {
+ emit screenLocked();
+ return true;
+ }
+ }
+ } else if (m->wParam == PBT_APMSUSPEND) {
+ emit screenLocked();
+ return true;
+ }
+ }
+ if (m->message == WM_WTSSESSION_CHANGE) {
+ if (m->wParam == WTS_CONSOLE_DISCONNECT) {
+ emit screenLocked();
+ return true;
+ }
+ if (m->wParam == WTS_SESSION_LOCK) {
+ emit screenLocked();
+ return true;
+ }
+ }
+ }
+ return false;
+}
diff --git a/src/gui/osutils/winutils/ScreenLockListenerWin.h b/src/gui/osutils/winutils/ScreenLockListenerWin.h
new file mode 100644
index 000000000..edf6c2936
--- /dev/null
+++ b/src/gui/osutils/winutils/ScreenLockListenerWin.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 or (at your option)
+ * version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCREENLOCKLISTENERWIN_H
+#define SCREENLOCKLISTENERWIN_H
+#include <QAbstractNativeEventFilter>
+#include <QObject>
+#include <QWidget>
+
+#include "gui/osutils/ScreenLockListenerPrivate.h"
+
+class ScreenLockListenerWin : public ScreenLockListenerPrivate, public QAbstractNativeEventFilter
+{
+ Q_OBJECT
+public:
+ explicit ScreenLockListenerWin(QWidget* parent = nullptr);
+ ~ScreenLockListenerWin();
+ bool nativeEventFilter(const QByteArray& eventType, void* message, long*) override;
+
+private:
+ void* m_powerNotificationHandle;
+};
+
+#endif // SCREENLOCKLISTENERWIN_H