From bd4716c949ae23cb48b7b9dca57dccd9c8a77c05 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 9 May 2015 14:22:51 -0400 Subject: [PATCH] Started code for handling WM_QUERYENDSESSION and its console equivalent. --- windows/container.c | 13 +++++++++++++ windows/init.c | 18 ++++++++++++++++++ windows/uipriv_windows.h | 1 + 3 files changed, 32 insertions(+) diff --git a/windows/container.c b/windows/container.c index e1e98182..35bc59a3 100644 --- a/windows/container.c +++ b/windows/container.c @@ -243,6 +243,19 @@ static LRESULT CALLBACK containerWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP logLastError("error getting client rect for resize in containerWndProc()"); resize(cc, &r); return 0; + + // and this is run only on the initial parent + // this ensures that all end session handling is done in one place and only once + case WM_QUERYENDSESSION: + case msgConsoleEndSession: + if (cc != NULL) + break; + // TODO block handler + if (shouldQuit()) { + uiQuit(); + return TRUE; + } + return FALSE; } return DefWindowProcW(hwnd, uMsg, wParam, lParam); diff --git a/windows/init.c b/windows/init.c index 63e8ac7a..4c4bdf4c 100644 --- a/windows/init.c +++ b/windows/init.c @@ -52,6 +52,19 @@ static const char *loadLastError(const char *message) return str; } +static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType) +{ + switch (dwCtrlType) { + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + // the handler is run in a separate thread + SendMessageW(initialParent, msgConsoleEndSession, 0, 0); + // we handled it here + return TRUE; + } + return FALSE; +} + uiInitOptions options; const char *uiInit(uiInitOptions *o) @@ -98,6 +111,9 @@ const char *uiInit(uiInitOptions *o) if (ce != NULL) return loadLastError(ce); + if (SetConsoleCtrlHandler(consoleCtrlHandler, TRUE) == 0) + return loadLastError("setting up console end session handler"); + hollowBrush = (HBRUSH) GetStockObject(HOLLOW_BRUSH); if (hollowBrush == NULL) return loadLastError("getting hollow brush"); @@ -109,6 +125,8 @@ void uiUninit(void) { uninitMenus(); // TODO delete hollow brush + if (SetConsoleCtrlHandler(consoleCtrlHandler, FALSE) == 0) + logLastError("unregistering console end session handler"); uninitContainer(); if (DeleteObject(hMessageFont) == 0) logLastError("error deleting control font in uiUninit()"); diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index 5b79671c..c189f10d 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -36,6 +36,7 @@ enum { msgNOTIFY, msgUpdateChild, // fake because Windows seems to SWP_NOSIZE MoveWindow()s and SetWindowPos()s that don't change the window size (even if SWP_NOSIZE isn't specified) msgHasTabStops, + msgConsoleEndSession, }; // debug.c