mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Refactor to fix #1671
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <QAbstractEventDispatcher>
|
||||
#include <QWidget>
|
||||
#include <windows.h>
|
||||
#include <powrprof.h>
|
||||
#endif
|
||||
|
||||
#include <utils/settings.h>
|
||||
@@ -21,8 +22,9 @@ class OsEventHandlerBase : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
OsEventHandlerBase();
|
||||
~OsEventHandlerBase() override;
|
||||
virtual ~OsEventHandlerBase();
|
||||
|
||||
public slots:
|
||||
void suspend(bool sleep);
|
||||
@@ -46,6 +48,8 @@ protected:
|
||||
bool _isSuspendRegistered;
|
||||
bool _isLockRegistered;
|
||||
|
||||
bool _isService;
|
||||
|
||||
Logger* _log{};
|
||||
};
|
||||
|
||||
@@ -53,10 +57,11 @@ protected:
|
||||
|
||||
class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter
|
||||
{
|
||||
|
||||
public:
|
||||
OsEventHandlerWindows();
|
||||
~OsEventHandlerWindows() override;
|
||||
~OsEventHandlerWindows();
|
||||
|
||||
void handleSuspendResumeEvent(bool sleep);
|
||||
|
||||
protected:
|
||||
#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;
|
||||
#endif
|
||||
|
||||
private:
|
||||
bool registerOsEventHandler() override;
|
||||
void unregisterOsEventHandler() override;
|
||||
bool registerLockHandler() override;
|
||||
void unregisterLockHandler() override;
|
||||
|
||||
QWidget _widget;
|
||||
private:
|
||||
static OsEventHandlerWindows* getInstance();
|
||||
|
||||
static DEVICE_NOTIFY_CALLBACK_ROUTINE handlePowerNotifications;
|
||||
|
||||
QWidget* _widget;
|
||||
HPOWERNOTIFY _notifyHandle;
|
||||
};
|
||||
|
||||
|
@@ -11,11 +11,15 @@
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <QCoreApplication>
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <windows.h>
|
||||
#include <wtsapi32.h>
|
||||
#include <powerbase.h>
|
||||
|
||||
#pragma comment( lib, "wtsapi32.lib" )
|
||||
#pragma comment(lib, "PowrProf.lib")
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
#include <AppKit/AppKit.h>
|
||||
#endif
|
||||
@@ -26,10 +30,16 @@ OsEventHandlerBase::OsEventHandlerBase()
|
||||
, _isSuspendOnLock(false)
|
||||
, _isSuspendRegistered(false)
|
||||
, _isLockRegistered(false)
|
||||
, _isService(false)
|
||||
{
|
||||
qRegisterMetaType<Event>("Event");
|
||||
_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);
|
||||
}
|
||||
|
||||
@@ -65,6 +75,8 @@ void OsEventHandlerBase::handleSettingsUpdate(settings::type type, const QJsonDo
|
||||
unregisterOsEventHandler();
|
||||
}
|
||||
|
||||
if (!_isService)
|
||||
{
|
||||
_isLockEnabled = obj["lockEnable"].toBool(true);
|
||||
if (_isLockEnabled || _isSuspendOnLock != prevIsSuspendOnLock)
|
||||
{
|
||||
@@ -77,6 +89,7 @@ void OsEventHandlerBase::handleSettingsUpdate(settings::type type, const QJsonDo
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OsEventHandlerBase::suspend(bool sleep)
|
||||
{
|
||||
@@ -118,8 +131,15 @@ void OsEventHandlerBase::lock(bool isLocked)
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
OsEventHandlerWindows* OsEventHandlerWindows::getInstance()
|
||||
{
|
||||
static OsEventHandlerWindows instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
OsEventHandlerWindows::OsEventHandlerWindows()
|
||||
: _notifyHandle(NULL)
|
||||
: _notifyHandle(NULL),
|
||||
_widget(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -127,6 +147,8 @@ OsEventHandlerWindows::~OsEventHandlerWindows()
|
||||
{
|
||||
unregisterLockHandler();
|
||||
unregisterOsEventHandler();
|
||||
|
||||
delete _widget;
|
||||
}
|
||||
|
||||
#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*/)
|
||||
#endif
|
||||
{
|
||||
MSG* msg = static_cast<MSG*>(message);
|
||||
|
||||
MSG* msg = static_cast<MSG*>(message);
|
||||
switch (msg->message)
|
||||
{
|
||||
case WM_WTSSESSION_CHANGE:
|
||||
@@ -152,34 +174,45 @@ bool OsEventHandlerWindows::nativeEventFilter(const QByteArray& eventType, void*
|
||||
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;
|
||||
}
|
||||
|
||||
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 isRegistered{ _isSuspendRegistered };
|
||||
if (!_isSuspendRegistered)
|
||||
{
|
||||
auto handle = reinterpret_cast<HWND> (_widget.winId());
|
||||
_notifyHandle = RegisterSuspendResumeNotification(handle, DEVICE_NOTIFY_WINDOW_HANDLE);
|
||||
if (_notifyHandle != NULL)
|
||||
DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS notificationsParameters = { handlePowerNotifications, NULL };
|
||||
if (PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, (HANDLE)¬ificationsParameters, &_notifyHandle) == ERROR_SUCCESS)
|
||||
{
|
||||
QCoreApplication::instance()->installNativeEventFilter(this);
|
||||
isRegistered = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -200,8 +233,7 @@ void OsEventHandlerWindows::unregisterOsEventHandler()
|
||||
{
|
||||
if (_notifyHandle != NULL)
|
||||
{
|
||||
QCoreApplication::instance()->removeNativeEventFilter(this);
|
||||
UnregisterSuspendResumeNotification(_notifyHandle);
|
||||
PowerUnregisterSuspendResumeNotification(_notifyHandle);
|
||||
}
|
||||
_notifyHandle = NULL;
|
||||
_isSuspendRegistered = false;
|
||||
@@ -213,17 +245,23 @@ bool OsEventHandlerWindows::registerLockHandler()
|
||||
bool isRegistered{ _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))
|
||||
{
|
||||
QCoreApplication::instance()->installNativeEventFilter(this);
|
||||
isRegistered = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error(_log, "Could not register for lock/unlock events!");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isRegistered)
|
||||
@@ -237,11 +275,15 @@ void OsEventHandlerWindows::unregisterLockHandler()
|
||||
{
|
||||
if (_isLockRegistered)
|
||||
{
|
||||
auto handle = reinterpret_cast<HWND> (_widget.winId());
|
||||
if (!_isService)
|
||||
{
|
||||
auto handle = reinterpret_cast<HWND> (_widget->winId());
|
||||
QCoreApplication::instance()->removeNativeEventFilter(this);
|
||||
WTSUnRegisterSessionNotification(handle);
|
||||
_isLockRegistered = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(__linux__)
|
||||
|
||||
|
Reference in New Issue
Block a user