Skip to content

Commit

Permalink
Disable on user lock screen (Windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jan 17, 2024
1 parent 83c1f62 commit 9a406ce
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 11 deletions.
9 changes: 9 additions & 0 deletions sources/base/schema/schema-general.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
"hidden":true
},
"propertyOrder" : 4
},
"disableOnLocked" :
{
"type" : "boolean",
"format": "checkbox",
"title" : "edt_conf_gen_disableOnLocked_title",
"default" : false,
"required" : true,
"propertyOrder" : 5
}

},
Expand Down
6 changes: 5 additions & 1 deletion sources/hyperhdr/HyperHdrDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ HyperHdrDaemon::HyperHdrDaemon(const QString& rootPath, QApplication* parent, bo

// power management
#if defined(HAVE_POWER_MANAGEMENT)
_suspendHandler = std::unique_ptr<SuspendHandler>(new SuspendHandler());
auto genSet = getSetting(settings::type::GENERAL);
const QJsonObject& genConfig = genSet.object();
bool lockedEnable = genConfig["disableOnLocked"].toBool(false);

_suspendHandler = std::unique_ptr<SuspendHandler>(new SuspendHandler(lockedEnable));
connect(_suspendHandler.get(), &SuspendHandler::SignalHibernate, _instanceManager.get(), &HyperHdrManager::hibernate);

#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion sources/hyperhdr/SuspendHandlerLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace {
const QString UPOWER_INTER = QStringLiteral("org.freedesktop.login1.Manager");
}

SuspendHandler::SuspendHandler()
SuspendHandler::SuspendHandler(bool sessionLocker)
{
QDBusConnection bus = QDBusConnection::systemBus();

Expand Down
2 changes: 1 addition & 1 deletion sources/hyperhdr/SuspendHandlerLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SuspendHandler : public QObject {
void SignalHibernate(bool wakeUp);

public:
SuspendHandler();
SuspendHandler(bool sessionLocker = false);
~SuspendHandler();

public slots:
Expand Down
2 changes: 1 addition & 1 deletion sources/hyperhdr/SuspendHandlerMacOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ class SuspendHandler : public QObject {
void SignalHibernate(bool wakeUp);

public:
SuspendHandler();
SuspendHandler(bool sessionLocker = false);
~SuspendHandler();
};
2 changes: 1 addition & 1 deletion sources/hyperhdr/SuspendHandlerMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ - (void)goingWake: (NSNotification*)note

@end

SuspendHandler::SuspendHandler()
SuspendHandler::SuspendHandler(bool sessionLocker)
{
_macSuspendHandlerInstance = [MacSuspendHandler new];
_suspendHandler = this;
Expand Down
55 changes: 52 additions & 3 deletions sources/hyperhdr/SuspendHandlerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,47 @@
#include <utils/Image.h>
#include <base/HyperHdrManager.h>
#include <windows.h>
#include <wtsapi32.h>

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

SuspendHandler::SuspendHandler(bool sessionLocker)
{
auto handle = reinterpret_cast<HWND> (_widget.winId());
_notifyHandle = RegisterSuspendResumeNotification(handle, DEVICE_NOTIFY_WINDOW_HANDLE);

if (_notifyHandle == NULL)
std::cout << "COULD NOT REGISTER SLEEP HANDLER!" << std::endl;
else
std::cout << "SLEEP HANDLER REGISTERED!" << std::endl;
std::cout << "Sleep handler registered!" << std::endl;

_sessionLocker = sessionLocker;
if (_sessionLocker)
{
if (WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION))
std::cout << "Session handler registered!" << std::endl;
else
{
std::cout << "COULD NOT REGISTER SESSION HANDLER!" << std::endl;
_sessionLocker = false;
}
}
}

SuspendHandler::~SuspendHandler()
{
if (_notifyHandle != NULL)
{
UnregisterSuspendResumeNotification(_notifyHandle);
std::cout << "SLEEP HANDLER DEREGISTERED!" << std::endl;
std::cout << "Sleep handler deregistered!" << std::endl;
}
_notifyHandle = NULL;

if (_sessionLocker)
{
auto handle = reinterpret_cast<HWND> (_widget.winId());
WTSUnRegisterSessionNotification(handle);
}
}

#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
Expand All @@ -81,6 +102,34 @@ bool SuspendHandler::nativeEventFilter(const QByteArray& eventType, void* messag
break;
}
}

if (_sessionLocker)
{
if (msg->message == WM_WTSSESSION_CHANGE)
{
switch (msg->wParam)
{
case WTS_SESSION_UNLOCK:
emit SignalHibernate(true);
return true;
break;

case WTS_SESSION_LOCK:

if (GetSystemMetrics(SM_REMOTESESSION) != 0)
{
std::cout << "Detected RDP session. Skipping disable on lock." << std::endl;
}
else
{
emit SignalHibernate(false);
return true;
}
break;
}
}
}

return false;
}

3 changes: 2 additions & 1 deletion sources/hyperhdr/SuspendHandlerWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ class SuspendHandler : public QObject, public QAbstractNativeEventFilter {

QWidget _widget;
HPOWERNOTIFY _notifyHandle;
bool _sessionLocker;

signals:
void SignalHibernate(bool wakeUp);

public:
SuspendHandler();
SuspendHandler(bool sessionLocker = false);
~SuspendHandler();
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) Q_DECL_OVERRIDE;
Expand Down
6 changes: 4 additions & 2 deletions www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1237,5 +1237,7 @@
"edt_conf_hardware_expl" : "Enable hardware acceleration, for example: pixel and vertex shaders on Windows",
"edt_conf_monitor_nits_title" : "HDR brightness correction",
"edt_conf_monitor_nits_expl" : "SDR target brightness used for HDR to SDR conversion. If 0, it disables hardware color conversion while maintaining accelerated scaling.",
"edt_append_nits" : "nits"
}
"edt_append_nits" : "nits",
"edt_conf_gen_disableOnLocked_title" : "Disable when locked",
"edt_conf_gen_disableOnLocked_expl" : "Turn off processing when the user has locked the system"
}
10 changes: 10 additions & 0 deletions www/js/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,15 @@ $(document).ready(function()
createHint("intro", $.i18n('conf_general_inst_desc'), "inst_desc_cont");
}

if (window.serverInfo.grabbers != null && window.serverInfo.grabbers != undefined &&
window.serverInfo.grabbers.active != null && window.serverInfo.grabbers.active != undefined)
{
var grabbers = window.serverInfo.grabbers.active;
if (grabbers.indexOf('Media Foundation') < 0)
{
conf_editor.getEditor('root.general.disableOnLocked').disable();
}
}

removeOverlay();
});

0 comments on commit 9a406ce

Please sign in to comment.