Skip to content

Commit

Permalink
Windows: fix system suspend handling
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jan 4, 2025
1 parent e4a7f7d commit 993bf9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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


25 changes: 25 additions & 0 deletions sources/suspend-handler/SuspendHandlerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,37 @@
namespace
{
HWND handle = nullptr;
SuspendHandler* instance = nullptr;
}

static void SuspendHandlerQueueHandler(WPARAM wparam)
{
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 +101,8 @@ SuspendHandler::SuspendHandler(bool sessionLocker):

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

if (_notifyHandle != NULL)
{
UnregisterSuspendResumeNotification(_notifyHandle);
Expand Down
15 changes: 15 additions & 0 deletions sources/systray/SystrayWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,31 @@ 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_CLOSE:
DestroyWindow(hwnd);
hwnd = nullptr;
Expand Down Expand Up @@ -290,6 +303,8 @@ void SystrayUpdate(SystrayMenu *tray)

void SystrayClose()
{
queueHandler = nullptr;

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

0 comments on commit 993bf9d

Please sign in to comment.