From 3b3a3a31e85818562350013e8406fea634ad0528 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 18 May 2022 20:24:43 +0200 Subject: [PATCH] - Win32 i_main.cpp cleanup Moved SetWndowTitle into the menu class Removed the dead Windows session code Removed the unused modules for Win32 core DLLs. removed dynamic function calls for post-XP features --- src/common/platform/win32/i_input.cpp | 74 ---------- src/common/platform/win32/i_main.cpp | 132 ++---------------- src/common/platform/win32/i_mainwindow.cpp | 16 +++ src/common/platform/win32/i_mainwindow.h | 2 + .../platform/win32/win32vulkanvideo.cpp | 2 +- 5 files changed, 31 insertions(+), 195 deletions(-) diff --git a/src/common/platform/win32/i_input.cpp b/src/common/platform/win32/i_input.cpp index 1b3b4aa83..a5ea5ad44 100644 --- a/src/common/platform/win32/i_input.cpp +++ b/src/common/platform/win32/i_input.cpp @@ -50,18 +50,6 @@ #pragma warning(disable:4244) #endif -// Compensate for w32api's lack -#ifndef WM_WTSSESSION_CHANGE -#define WM_WTSSESSION_CHANGE 0x02B1 -#define WTS_CONSOLE_CONNECT 1 -#define WTS_CONSOLE_DISCONNECT 2 -#define WTS_SESSION_LOCK 7 -#define WTS_SESSION_UNLOCK 8 -#endif -#ifndef PBT_APMSUSPEND -// w32api does not #define the PBT_ macros in winuser.h like the PSDK does -#include -#endif #ifndef GET_RAWINPUT_CODE_WPARAM #define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff) #endif @@ -105,8 +93,6 @@ FJoystickCollection *JoyDevices[NUM_JOYDEVICES]; extern HINSTANCE g_hInst; -extern DWORD SessionID; - static HMODULE DInputDLL; bool GUICapture; @@ -134,7 +120,6 @@ LPDIRECTINPUT g_pdi3; extern bool AppActive; -int SessionState = 0; int BlockMouseMove; static bool EventHandlerResultForNativeMouse; @@ -510,65 +495,6 @@ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) S_SetSoundPaused (wParam); break; - case WM_WTSSESSION_CHANGE: - case WM_POWERBROADCAST: - { - if (message == WM_WTSSESSION_CHANGE && lParam == (LPARAM)SessionID) - { -#ifdef _DEBUG - OutputDebugStringA ("SessionID matched\n"); -#endif - // When using fast user switching, XP will lock a session before - // disconnecting it, and the session will be unlocked before reconnecting it. - // For our purposes, video output will only happen when the session is - // both unlocked and connected (that is, SessionState is 0). - switch (wParam) - { - case WTS_SESSION_LOCK: - SessionState |= 1; - break; - case WTS_SESSION_UNLOCK: - SessionState &= ~1; - break; - case WTS_CONSOLE_DISCONNECT: - SessionState |= 2; - break; - case WTS_CONSOLE_CONNECT: - SessionState &= ~2; - break; - } - } - else if (message == WM_POWERBROADCAST) - { - switch (wParam) - { - case PBT_APMSUSPEND: - SessionState |= 4; - break; - case PBT_APMRESUMESUSPEND: - SessionState &= ~4; - break; - } - } - - if (GSnd != NULL) - { -#if 0 - // Do we actually need this here? - if (!oldstate && SessionState) - { - GSnd->SuspendSound (); - } -#endif - } -#ifdef _DEBUG - char foo[256]; - mysnprintf (foo, countof(foo), "Session Change: %ld %d\n", (long)lParam, (int)wParam); - OutputDebugStringA (foo); -#endif - } - break; - case WM_ERASEBKGND: return true; diff --git a/src/common/platform/win32/i_main.cpp b/src/common/platform/win32/i_main.cpp index 8b417f6f9..f414351a8 100644 --- a/src/common/platform/win32/i_main.cpp +++ b/src/common/platform/win32/i_main.cpp @@ -48,9 +48,6 @@ #pragma warning(disable:4244) #endif -//#include -#define NOTIFY_FOR_THIS_SESSION 0 - #ifdef _MSC_VER #include #include @@ -116,57 +113,13 @@ extern UINT TimerPeriod; FArgs *Args; HINSTANCE g_hInst; -DWORD SessionID; HANDLE MainThread; DWORD MainThreadID; HANDLE StdOut; bool FancyStdOut, AttachedStdOut; -FModule Kernel32Module{"Kernel32"}; -FModule Shell32Module{"Shell32"}; -FModule User32Module{"User32"}; - -// PRIVATE DATA DEFINITIONS ------------------------------------------------ - -static HMODULE hwtsapi32; // handle to wtsapi32.dll - // CODE -------------------------------------------------------------------- -//========================================================================== -// -// UnCOM -// -// Called by atexit if CoInitialize() succeeded. -// -//========================================================================== - -static void UnCOM (void) -{ - CoUninitialize (); -} - -//========================================================================== -// -// UnWTS -// -// Called by atexit if RegisterSessionNotification() succeeded. -// -//========================================================================== - -static void UnWTS (void) -{ - if (hwtsapi32 != 0) - { - typedef BOOL (WINAPI *ursn)(HWND); - ursn unreg = (ursn)GetProcAddress (hwtsapi32, "WTSUnRegisterSessionNotification"); - if (unreg != 0) - { - unreg (mainwindow.GetHandle()); - } - FreeLibrary (hwtsapi32); - hwtsapi32 = 0; - } -} //========================================================================== // @@ -180,11 +133,6 @@ void I_SetIWADInfo() mainwindow.UpdateLayout(); } -static void UnTbp() -{ - timeEndPeriod(TimerPeriod); -} - //========================================================================== // // DoMain @@ -209,16 +157,6 @@ int DoMain (HINSTANCE hInstance) Args->AppendArg(FString(wargv[i])); } - // Load Win32 modules - Kernel32Module.Load({"kernel32.dll"}); - Shell32Module.Load({"shell32.dll"}); - User32Module.Load({"user32.dll"}); - - // Under XP, get our session ID so we can know when the user changes/locks sessions. - // Since we need to remain binary compatible with older versions of Windows, we - // need to extract the ProcessIdToSessionId function from kernel32.dll manually. - HMODULE kernel = GetModuleHandleA ("kernel32.dll"); - if (Args->CheckParm("-stdout")) { // As a GUI application, we don't normally get a console when we start. @@ -254,26 +192,17 @@ int DoMain (HINSTANCE hInstance) StdOut = GetStdHandle(STD_OUTPUT_HANDLE); } - // These two functions do not exist in Windows XP. - BOOL (WINAPI* p_GetCurrentConsoleFontEx)(HANDLE hConsoleOutput, BOOL bMaximumWindow, PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); - BOOL (WINAPI* p_SetCurrentConsoleFontEx)(HANDLE hConsoleOutput, BOOL bMaximumWindow, PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); - - p_SetCurrentConsoleFontEx = (decltype(p_SetCurrentConsoleFontEx))GetProcAddress(kernel, "SetCurrentConsoleFontEx"); - p_GetCurrentConsoleFontEx = (decltype(p_GetCurrentConsoleFontEx))GetProcAddress(kernel, "GetCurrentConsoleFontEx"); - if (p_SetCurrentConsoleFontEx && p_GetCurrentConsoleFontEx) + // Deprecated stuff for legacy consoles. As of now this is still relevant, but this code can be removed once Windows 7 is no longer relevant. + CONSOLE_FONT_INFOEX cfi; + cfi.cbSize = sizeof(cfi); + if (GetCurrentConsoleFontEx(StdOut, false, &cfi)) { - CONSOLE_FONT_INFOEX cfi; - cfi.cbSize = sizeof(cfi); - - if (p_GetCurrentConsoleFontEx(StdOut, false, &cfi)) + if (*cfi.FaceName == 0) // If the face name is empty, the default (useless) raster font is actoive. { - if (*cfi.FaceName == 0) // If the face name is empty, the default (useless) raster font is actoive. - { - //cfi.dwFontSize = { 8, 14 }; - wcscpy(cfi.FaceName, L"Lucida Console"); - cfi.FontFamily = FF_DONTCARE; - p_SetCurrentConsoleFontEx(StdOut, false, &cfi); - } + //cfi.dwFontSize = { 8, 14 }; + wcscpy(cfi.FaceName, L"Lucida Console"); + cfi.FontFamily = FF_DONTCARE; + SetCurrentConsoleFontEx(StdOut, false, &cfi); } } FancyStdOut = true; @@ -287,7 +216,7 @@ int DoMain (HINSTANCE hInstance) TimerPeriod = tc.wPeriodMin; timeBeginPeriod (TimerPeriod); - atexit(UnTbp); + atexit([](){ timeEndPeriod(TimerPeriod); }); // Figure out what directory the program resides in. WCHAR progbuff[1024]; @@ -329,40 +258,13 @@ int DoMain (HINSTANCE hInstance) FStringf caption("" GAMENAME " %s " X64 " (%s)", GetVersionString(), GetGitTime()); mainwindow.Create(caption, x, y, width, height); - if (kernel != NULL) - { - typedef BOOL (WINAPI *pts)(DWORD, DWORD *); - pts pidsid = (pts)GetProcAddress (kernel, "ProcessIdToSessionId"); - if (pidsid != 0) - { - if (!pidsid (GetCurrentProcessId(), &SessionID)) - { - SessionID = 0; - } - hwtsapi32 = LoadLibraryA ("wtsapi32.dll"); - if (hwtsapi32 != 0) - { - FARPROC reg = GetProcAddress (hwtsapi32, "WTSRegisterSessionNotification"); - if (reg == 0 || !((BOOL(WINAPI *)(HWND, DWORD))reg) (mainwindow.GetHandle(), NOTIFY_FOR_THIS_SESSION)) - { - FreeLibrary (hwtsapi32); - hwtsapi32 = 0; - } - else - { - atexit (UnWTS); - } - } - } - } - GetClientRect (mainwindow.GetHandle(), &cRect); WinWidth = cRect.right; WinHeight = cRect.bottom; CoInitialize (NULL); - atexit (UnCOM); + atexit ([](){ CoUninitialize(); }); // beware of calling convention. int ret = GameMain (); mainwindow.CheckForRestart(); @@ -662,15 +564,5 @@ int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE nothing, LPWSTR cmdline, int // each platform has its own specific version of this function. void I_SetWindowTitle(const char* caption) { - std::wstring widecaption; - if (!caption) - { - FStringf default_caption("" GAMENAME " %s " X64 " (%s)", GetVersionString(), GetGitTime()); - widecaption = default_caption.WideString(); - } - else - { - widecaption = WideString(caption); - } - SetWindowText(mainwindow.GetHandle(), widecaption.c_str()); + mainwindow.SetWindowTitle(caption); } diff --git a/src/common/platform/win32/i_mainwindow.cpp b/src/common/platform/win32/i_mainwindow.cpp index deacd068e..0bf25076c 100644 --- a/src/common/platform/win32/i_mainwindow.cpp +++ b/src/common/platform/win32/i_mainwindow.cpp @@ -933,3 +933,19 @@ void MainWindow::GetLog(std::function