Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: suspend & power off handling #1039

Merged
merged 5 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/systray/Systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct SystrayMenu

#ifdef _WIN32
HWND SystrayGetWindow();
void SystrayAssignQueueHandler(std::function<void(WPARAM wparam)> _queueHandler);
#endif


1 change: 1 addition & 0 deletions sources/base/AccessManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <base/AccessManager.h>
#include <db/AuthTable.h>
#include <db/MetaTable.h>
#include <QUuid>

using namespace hyperhdr;

Expand Down
1 change: 1 addition & 0 deletions sources/db/AuthTable.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QCryptographicHash>
#include <QUuid>

#include <db/AuthTable.h>
using namespace hyperhdr;
Expand Down
1 change: 1 addition & 0 deletions sources/db/MetaTable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QCryptographicHash>
#include <QNetworkInterface>
#include <QUuid>

#include <db/MetaTable.h>

Expand Down
1 change: 1 addition & 0 deletions sources/led-drivers/net/DriverNetUdpE131.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#include <QHostInfo>
#include <QUuid>

// hyperhdr local includes
#include <led-drivers/net/DriverNetUdpE131.h>
Expand Down
32 changes: 32 additions & 0 deletions sources/suspend-handler/SuspendHandlerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,49 @@
#include <windows.h>
#include <wtsapi32.h>
#include <systray/Systray.h>
#include <QCoreApplication>

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

namespace
{
HWND handle = nullptr;
SuspendHandler* instance = nullptr;
}

static void SuspendHandlerQueueHandler(WPARAM wparam)
{
if (wparam == 0)
{
auto instance = QCoreApplication::instance();
QUEUE_CALL_0(instance, quit);
}
else if (instance != nullptr)
{
MSG message{};
QByteArray eventType;

message.message = WM_POWERBROADCAST;
message.wParam = wparam;

#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
long result = 0;
BLOCK_CALL_3(instance, nativeEventFilter, QByteArray, eventType, void*, &message, long*, &result);
#else
qintptr result = 0;
BLOCK_CALL_3(instance, nativeEventFilter, QByteArray, eventType, void*, &message, qintptr*, &result);
#endif
}
}

SuspendHandler::SuspendHandler(bool sessionLocker):
_notifyHandle(NULL),
_notifyMonitorHandle(NULL),
_sessionLocker(sessionLocker)
{
instance = this;
handle = SystrayGetWindow();
SystrayAssignQueueHandler(SuspendHandlerQueueHandler);
_notifyHandle = RegisterSuspendResumeNotification(handle, DEVICE_NOTIFY_WINDOW_HANDLE);

if (_notifyHandle == NULL)
Expand All @@ -78,6 +107,9 @@ SuspendHandler::SuspendHandler(bool sessionLocker):

SuspendHandler::~SuspendHandler()
{
SystrayAssignQueueHandler(nullptr);
instance = nullptr;

if (_notifyHandle != NULL)
{
UnregisterSuspendResumeNotification(_notifyHandle);
Expand Down
22 changes: 22 additions & 0 deletions sources/systray/SystrayWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,38 @@ namespace
std::list<HICON> icons;
std::list<HBITMAP> bitmaps;
ULONG_PTR gdiToken = 0;
std::function<void(WPARAM suspend)> queueHandler = nullptr;
}

HWND SystrayGetWindow()
{
return window;
}

void SystrayAssignQueueHandler(std::function<void(WPARAM wparam)> _queueHandler)
{
queueHandler = _queueHandler;
}

static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
LPARAM lparam)
{
switch (msg)
{
case WM_POWERBROADCAST:
if (queueHandler != nullptr && (wparam == PBT_APMSUSPEND || wparam == PBT_APMRESUMESUSPEND || wparam == PBT_POWERSETTINGCHANGE))
{
queueHandler(wparam);
}
return true;

case WM_QUERYENDSESSION:
if (queueHandler != nullptr && lparam == 0)
{
queueHandler(0);
}
return false;

case WM_CLOSE:
DestroyWindow(hwnd);
hwnd = nullptr;
Expand Down Expand Up @@ -290,6 +310,8 @@ void SystrayUpdate(SystrayMenu *tray)

void SystrayClose()
{
queueHandler = nullptr;

if (systrayIcon.hIcon != nullptr)
{
Shell_NotifyIcon(NIM_DELETE, &systrayIcon);
Expand Down