Refactor to fix #1671

This commit is contained in:
Lord-Grey
2023-12-23 15:38:13 +01:00
parent 2aa0d01dde
commit 1b4df36c49
2 changed files with 168 additions and 117 deletions

View File

@@ -10,6 +10,7 @@
#include <QAbstractEventDispatcher> #include <QAbstractEventDispatcher>
#include <QWidget> #include <QWidget>
#include <windows.h> #include <windows.h>
#include <powrprof.h>
#endif #endif
#include <utils/settings.h> #include <utils/settings.h>
@@ -21,8 +22,9 @@ class OsEventHandlerBase : public QObject
Q_OBJECT Q_OBJECT
public: public:
OsEventHandlerBase(); OsEventHandlerBase();
~OsEventHandlerBase() override; virtual ~OsEventHandlerBase();
public slots: public slots:
void suspend(bool sleep); void suspend(bool sleep);
@@ -46,6 +48,8 @@ protected:
bool _isSuspendRegistered; bool _isSuspendRegistered;
bool _isLockRegistered; bool _isLockRegistered;
bool _isService;
Logger* _log{}; Logger* _log{};
}; };
@@ -53,10 +57,11 @@ protected:
class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter
{ {
public: public:
OsEventHandlerWindows(); OsEventHandlerWindows();
~OsEventHandlerWindows() override; ~OsEventHandlerWindows();
void handleSuspendResumeEvent(bool sleep);
protected: protected:
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@@ -65,13 +70,17 @@ protected:
bool nativeEventFilter(const QByteArray& eventType, void* message, long int* result) override; bool nativeEventFilter(const QByteArray& eventType, void* message, long int* result) override;
#endif #endif
private:
bool registerOsEventHandler() override; bool registerOsEventHandler() override;
void unregisterOsEventHandler() override; void unregisterOsEventHandler() override;
bool registerLockHandler() override; bool registerLockHandler() override;
void unregisterLockHandler() override; void unregisterLockHandler() override;
QWidget _widget; private:
static OsEventHandlerWindows* getInstance();
static DEVICE_NOTIFY_CALLBACK_ROUTINE handlePowerNotifications;
QWidget* _widget;
HPOWERNOTIFY _notifyHandle; HPOWERNOTIFY _notifyHandle;
}; };

View File

@@ -11,11 +11,15 @@
#if defined(_WIN32) #if defined(_WIN32)
#include <QCoreApplication> #include <QCoreApplication>
#include <QApplication>
#include <QWidget> #include <QWidget>
#include <windows.h> #include <windows.h>
#include <wtsapi32.h> #include <wtsapi32.h>
#include <powerbase.h>
#pragma comment( lib, "wtsapi32.lib" ) #pragma comment( lib, "wtsapi32.lib" )
#pragma comment(lib, "PowrProf.lib")
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#endif #endif
@@ -26,10 +30,16 @@ OsEventHandlerBase::OsEventHandlerBase()
, _isSuspendOnLock(false) , _isSuspendOnLock(false)
, _isSuspendRegistered(false) , _isSuspendRegistered(false)
, _isLockRegistered(false) , _isLockRegistered(false)
, _isService(false)
{ {
qRegisterMetaType<Event>("Event"); qRegisterMetaType<Event>("Event");
_log = Logger::getInstance("EVENTS-OS"); _log = Logger::getInstance("EVENTS-OS");
QCoreApplication* app = QCoreApplication::instance();
if (!qobject_cast<QApplication*>(app))
{
_isService = true;
}
QObject::connect(this, &OsEventHandlerBase::signalEvent, EventHandler::getInstance(), &EventHandler::handleEvent); QObject::connect(this, &OsEventHandlerBase::signalEvent, EventHandler::getInstance(), &EventHandler::handleEvent);
} }
@@ -65,6 +75,8 @@ void OsEventHandlerBase::handleSettingsUpdate(settings::type type, const QJsonDo
unregisterOsEventHandler(); unregisterOsEventHandler();
} }
if (!_isService)
{
_isLockEnabled = obj["lockEnable"].toBool(true); _isLockEnabled = obj["lockEnable"].toBool(true);
if (_isLockEnabled || _isSuspendOnLock != prevIsSuspendOnLock) if (_isLockEnabled || _isSuspendOnLock != prevIsSuspendOnLock)
{ {
@@ -77,6 +89,7 @@ void OsEventHandlerBase::handleSettingsUpdate(settings::type type, const QJsonDo
} }
} }
} }
}
void OsEventHandlerBase::suspend(bool sleep) void OsEventHandlerBase::suspend(bool sleep)
{ {
@@ -118,8 +131,15 @@ void OsEventHandlerBase::lock(bool isLocked)
#if defined(_WIN32) #if defined(_WIN32)
OsEventHandlerWindows* OsEventHandlerWindows::getInstance()
{
static OsEventHandlerWindows instance;
return &instance;
}
OsEventHandlerWindows::OsEventHandlerWindows() OsEventHandlerWindows::OsEventHandlerWindows()
: _notifyHandle(NULL) : _notifyHandle(NULL),
_widget(nullptr)
{ {
} }
@@ -127,6 +147,8 @@ OsEventHandlerWindows::~OsEventHandlerWindows()
{ {
unregisterLockHandler(); unregisterLockHandler();
unregisterOsEventHandler(); unregisterOsEventHandler();
delete _widget;
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@@ -135,8 +157,8 @@ bool OsEventHandlerWindows::nativeEventFilter(const QByteArray& eventType, void*
bool OsEventHandlerWindows::nativeEventFilter(const QByteArray& eventType, void* message, long int* /*result*/) bool OsEventHandlerWindows::nativeEventFilter(const QByteArray& eventType, void* message, long int* /*result*/)
#endif #endif
{ {
MSG* msg = static_cast<MSG*>(message);
MSG* msg = static_cast<MSG*>(message);
switch (msg->message) switch (msg->message)
{ {
case WM_WTSSESSION_CHANGE: case WM_WTSSESSION_CHANGE:
@@ -152,34 +174,45 @@ bool OsEventHandlerWindows::nativeEventFilter(const QByteArray& eventType, void*
break; break;
} }
break; break;
case WM_POWERBROADCAST:
switch (msg->wParam)
{
case PBT_APMRESUMESUSPEND:
emit suspend(false);
return true;
break;
case PBT_APMSUSPEND:
emit suspend(true);
return true;
break;
}
break;
} }
return false; return false;
} }
void OsEventHandlerWindows::handleSuspendResumeEvent(bool sleep)
{
if (sleep)
{
suspend(true);
}
else
{
suspend(false);
}
}
ULONG OsEventHandlerWindows::handlePowerNotifications(PVOID Context, ULONG Type, PVOID Setting)
{
switch (Type)
{
case PBT_APMRESUMESUSPEND:
getInstance()->handleSuspendResumeEvent(false);
break;
case PBT_APMSUSPEND:
getInstance()->handleSuspendResumeEvent(true);
break;
}
return S_OK;
}
bool OsEventHandlerWindows::registerOsEventHandler() bool OsEventHandlerWindows::registerOsEventHandler()
{ {
bool isRegistered{ _isSuspendRegistered }; bool isRegistered{ _isSuspendRegistered };
if (!_isSuspendRegistered) if (!_isSuspendRegistered)
{ {
auto handle = reinterpret_cast<HWND> (_widget.winId()); DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS notificationsParameters = { handlePowerNotifications, NULL };
_notifyHandle = RegisterSuspendResumeNotification(handle, DEVICE_NOTIFY_WINDOW_HANDLE); if (PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, (HANDLE)&notificationsParameters, &_notifyHandle) == ERROR_SUCCESS)
if (_notifyHandle != NULL)
{ {
QCoreApplication::instance()->installNativeEventFilter(this); isRegistered = true;
} }
else else
{ {
@@ -200,8 +233,7 @@ void OsEventHandlerWindows::unregisterOsEventHandler()
{ {
if (_notifyHandle != NULL) if (_notifyHandle != NULL)
{ {
QCoreApplication::instance()->removeNativeEventFilter(this); PowerUnregisterSuspendResumeNotification(_notifyHandle);
UnregisterSuspendResumeNotification(_notifyHandle);
} }
_notifyHandle = NULL; _notifyHandle = NULL;
_isSuspendRegistered = false; _isSuspendRegistered = false;
@@ -213,17 +245,23 @@ bool OsEventHandlerWindows::registerLockHandler()
bool isRegistered{ _isLockRegistered }; bool isRegistered{ _isLockRegistered };
if (!_isLockRegistered) if (!_isLockRegistered)
{ {
auto handle = reinterpret_cast<HWND> (_widget.winId()); if (!_isService)
{
_widget = new QWidget();
if (_widget)
{
auto handle = reinterpret_cast<HWND> (_widget->winId());
if (WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION)) if (WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION))
{ {
QCoreApplication::instance()->installNativeEventFilter(this);
isRegistered = true; isRegistered = true;
} }
else else
{ {
Error(_log, "Could not register for lock/unlock events!"); Error(_log, "Could not register for lock/unlock events!");
} }
}
}
} }
if (isRegistered) if (isRegistered)
@@ -237,11 +275,15 @@ void OsEventHandlerWindows::unregisterLockHandler()
{ {
if (_isLockRegistered) if (_isLockRegistered)
{ {
auto handle = reinterpret_cast<HWND> (_widget.winId()); if (!_isService)
{
auto handle = reinterpret_cast<HWND> (_widget->winId());
QCoreApplication::instance()->removeNativeEventFilter(this);
WTSUnRegisterSessionNotification(handle); WTSUnRegisterSessionNotification(handle);
_isLockRegistered = false; _isLockRegistered = false;
} }
} }
}
#elif defined(__linux__) #elif defined(__linux__)