Skip to content

Commit

Permalink
Monitor on/off event listener (Win)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Feb 2, 2024
1 parent 611e5ae commit 4740755
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/utils/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ namespace hyperhdr
return COMP_INVALID;
}

enum SystemComponent { SUSPEND, LOCKER };
enum SystemComponent { SUSPEND, LOCKER, MONITOR };
}
3 changes: 2 additions & 1 deletion sources/base/HyperHdrManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ void HyperHdrManager::toggleGrabbersAllInstances(bool pause)

void HyperHdrManager::hibernate(bool wakeUp, hyperhdr::SystemComponent source)
{
if (source == hyperhdr::SystemComponent::LOCKER)
if (source == hyperhdr::SystemComponent::LOCKER || source == hyperhdr::SystemComponent::MONITOR)
{
Debug(_log, "OS event: %s", (source == hyperhdr::SystemComponent::LOCKER) ? ((wakeUp) ? "OS unlocked" : "OS locked") : ((wakeUp) ? "Monitor On" : "Monitor Off"));
bool _hasEffect = false;
for (const auto& instance : _runningInstances)
{
Expand Down
36 changes: 31 additions & 5 deletions sources/hyperhdr/SuspendHandlerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@

#pragma comment (lib, "WtsApi32.Lib")

SuspendHandler::SuspendHandler(bool sessionLocker)
SuspendHandler::SuspendHandler(bool sessionLocker):
_notifyHandle(NULL),
_notifyMonitorHandle(NULL),
_sessionLocker(sessionLocker)
{
auto handle = reinterpret_cast<HWND> (_widget.winId());
_notifyHandle = RegisterSuspendResumeNotification(handle, DEVICE_NOTIFY_WINDOW_HANDLE);
Expand All @@ -50,15 +53,19 @@ SuspendHandler::SuspendHandler(bool sessionLocker)
std::cout << "COULD NOT REGISTER SLEEP HANDLER!" << std::endl;
else
std::cout << "Sleep handler registered!" << std::endl;

_sessionLocker = sessionLocker;

if (_sessionLocker)
{
if (WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION))
_notifyMonitorHandle = RegisterPowerSettingNotification(handle, &GUID_SESSION_DISPLAY_STATUS, DEVICE_NOTIFY_WINDOW_HANDLE);

if (_notifyMonitorHandle != NULL && WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION))
std::cout << "Session handler registered!" << std::endl;
else
{
std::cout << "COULD NOT REGISTER SESSION HANDLER!" << std::endl;
std::cout << "COULD NOT REGISTER SESSION HANDLER!" << std::endl;
if (_notifyMonitorHandle != NULL)
UnregisterSuspendResumeNotification(_notifyMonitorHandle);
_notifyMonitorHandle = NULL;
_sessionLocker = false;
}
}
Expand All @@ -75,6 +82,13 @@ SuspendHandler::~SuspendHandler()

if (_sessionLocker)
{
if (_notifyMonitorHandle != NULL)
{
UnregisterSuspendResumeNotification(_notifyMonitorHandle);
std::cout << "Monitor state handler deregistered!" << std::endl;
}
_notifyMonitorHandle = NULL;

auto handle = reinterpret_cast<HWND> (_widget.winId());
WTSUnRegisterSessionNotification(handle);
}
Expand All @@ -101,6 +115,18 @@ bool SuspendHandler::nativeEventFilter(const QByteArray& eventType, void* messag
return true;
break;
}

if (_sessionLocker && msg->wParam == PBT_POWERSETTINGCHANGE && msg->lParam != 0)
{
POWERBROADCAST_SETTING* s = reinterpret_cast<POWERBROADCAST_SETTING*>(msg->lParam);
if (s != nullptr && s->PowerSetting == GUID_SESSION_DISPLAY_STATUS && s->DataLength > 0)
{
if (s->Data[0] == 1)
emit SignalHibernate(true, hyperhdr::SystemComponent::MONITOR);
else if (s->Data[0] == 0)
emit SignalHibernate(false, hyperhdr::SystemComponent::MONITOR);
}
}
}

if (_sessionLocker)
Expand Down
2 changes: 1 addition & 1 deletion sources/hyperhdr/SuspendHandlerWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SuspendHandler : public QObject, public QAbstractNativeEventFilter {
Q_OBJECT

QWidget _widget;
HPOWERNOTIFY _notifyHandle;
HPOWERNOTIFY _notifyHandle, _notifyMonitorHandle;
bool _sessionLocker;

signals:
Expand Down

0 comments on commit 4740755

Please sign in to comment.