- 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
This commit is contained in:
Christoph Oelckers 2022-05-18 20:24:43 +02:00
parent 6670bc09b9
commit 3b3a3a31e8
5 changed files with 31 additions and 195 deletions

View file

@ -50,18 +50,6 @@
#pragma warning(disable:4244) #pragma warning(disable:4244)
#endif #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 <pbt.h>
#endif
#ifndef GET_RAWINPUT_CODE_WPARAM #ifndef GET_RAWINPUT_CODE_WPARAM
#define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff) #define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff)
#endif #endif
@ -105,8 +93,6 @@ FJoystickCollection *JoyDevices[NUM_JOYDEVICES];
extern HINSTANCE g_hInst; extern HINSTANCE g_hInst;
extern DWORD SessionID;
static HMODULE DInputDLL; static HMODULE DInputDLL;
bool GUICapture; bool GUICapture;
@ -134,7 +120,6 @@ LPDIRECTINPUT g_pdi3;
extern bool AppActive; extern bool AppActive;
int SessionState = 0;
int BlockMouseMove; int BlockMouseMove;
static bool EventHandlerResultForNativeMouse; static bool EventHandlerResultForNativeMouse;
@ -510,65 +495,6 @@ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
S_SetSoundPaused (wParam); S_SetSoundPaused (wParam);
break; 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: case WM_ERASEBKGND:
return true; return true;

View file

@ -48,9 +48,6 @@
#pragma warning(disable:4244) #pragma warning(disable:4244)
#endif #endif
//#include <wtsapi32.h>
#define NOTIFY_FOR_THIS_SESSION 0
#ifdef _MSC_VER #ifdef _MSC_VER
#include <eh.h> #include <eh.h>
#include <new.h> #include <new.h>
@ -116,57 +113,13 @@ extern UINT TimerPeriod;
FArgs *Args; FArgs *Args;
HINSTANCE g_hInst; HINSTANCE g_hInst;
DWORD SessionID;
HANDLE MainThread; HANDLE MainThread;
DWORD MainThreadID; DWORD MainThreadID;
HANDLE StdOut; HANDLE StdOut;
bool FancyStdOut, AttachedStdOut; bool FancyStdOut, AttachedStdOut;
FModule Kernel32Module{"Kernel32"};
FModule Shell32Module{"Shell32"};
FModule User32Module{"User32"};
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static HMODULE hwtsapi32; // handle to wtsapi32.dll
// CODE -------------------------------------------------------------------- // 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(); mainwindow.UpdateLayout();
} }
static void UnTbp()
{
timeEndPeriod(TimerPeriod);
}
//========================================================================== //==========================================================================
// //
// DoMain // DoMain
@ -209,16 +157,6 @@ int DoMain (HINSTANCE hInstance)
Args->AppendArg(FString(wargv[i])); 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")) if (Args->CheckParm("-stdout"))
{ {
// As a GUI application, we don't normally get a console when we start. // 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); StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
} }
// These two functions do not exist in Windows XP. // 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.
BOOL (WINAPI* p_GetCurrentConsoleFontEx)(HANDLE hConsoleOutput, BOOL bMaximumWindow, PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); CONSOLE_FONT_INFOEX cfi;
BOOL (WINAPI* p_SetCurrentConsoleFontEx)(HANDLE hConsoleOutput, BOOL bMaximumWindow, PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx); cfi.cbSize = sizeof(cfi);
if (GetCurrentConsoleFontEx(StdOut, false, &cfi))
p_SetCurrentConsoleFontEx = (decltype(p_SetCurrentConsoleFontEx))GetProcAddress(kernel, "SetCurrentConsoleFontEx");
p_GetCurrentConsoleFontEx = (decltype(p_GetCurrentConsoleFontEx))GetProcAddress(kernel, "GetCurrentConsoleFontEx");
if (p_SetCurrentConsoleFontEx && p_GetCurrentConsoleFontEx)
{ {
CONSOLE_FONT_INFOEX cfi; if (*cfi.FaceName == 0) // If the face name is empty, the default (useless) raster font is actoive.
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. //cfi.dwFontSize = { 8, 14 };
{ wcscpy(cfi.FaceName, L"Lucida Console");
//cfi.dwFontSize = { 8, 14 }; cfi.FontFamily = FF_DONTCARE;
wcscpy(cfi.FaceName, L"Lucida Console"); SetCurrentConsoleFontEx(StdOut, false, &cfi);
cfi.FontFamily = FF_DONTCARE;
p_SetCurrentConsoleFontEx(StdOut, false, &cfi);
}
} }
} }
FancyStdOut = true; FancyStdOut = true;
@ -287,7 +216,7 @@ int DoMain (HINSTANCE hInstance)
TimerPeriod = tc.wPeriodMin; TimerPeriod = tc.wPeriodMin;
timeBeginPeriod (TimerPeriod); timeBeginPeriod (TimerPeriod);
atexit(UnTbp); atexit([](){ timeEndPeriod(TimerPeriod); });
// Figure out what directory the program resides in. // Figure out what directory the program resides in.
WCHAR progbuff[1024]; WCHAR progbuff[1024];
@ -329,40 +258,13 @@ int DoMain (HINSTANCE hInstance)
FStringf caption("" GAMENAME " %s " X64 " (%s)", GetVersionString(), GetGitTime()); FStringf caption("" GAMENAME " %s " X64 " (%s)", GetVersionString(), GetGitTime());
mainwindow.Create(caption, x, y, width, height); 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); GetClientRect (mainwindow.GetHandle(), &cRect);
WinWidth = cRect.right; WinWidth = cRect.right;
WinHeight = cRect.bottom; WinHeight = cRect.bottom;
CoInitialize (NULL); CoInitialize (NULL);
atexit (UnCOM); atexit ([](){ CoUninitialize(); }); // beware of calling convention.
int ret = GameMain (); int ret = GameMain ();
mainwindow.CheckForRestart(); 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. // each platform has its own specific version of this function.
void I_SetWindowTitle(const char* caption) void I_SetWindowTitle(const char* caption)
{ {
std::wstring widecaption; mainwindow.SetWindowTitle(caption);
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());
} }

View file

@ -933,3 +933,19 @@ void MainWindow::GetLog(std::function<bool(const void* data, uint32_t size, uint
EDITSTREAM streamer = { (DWORD_PTR)&writeData, 0, WriteLogFileStreamer }; EDITSTREAM streamer = { (DWORD_PTR)&writeData, 0, WriteLogFileStreamer };
SendMessage(ConWindow, EM_STREAMOUT, SF_RTF, (LPARAM)&streamer); SendMessage(ConWindow, EM_STREAMOUT, SF_RTF, (LPARAM)&streamer);
} }
// each platform has its own specific version of this function.
void MainWindow::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(Window, widecaption.c_str());
}

View file

@ -43,6 +43,8 @@ public:
void HideStartupScreen(); void HideStartupScreen();
void InvalidateStartupScreen(); void InvalidateStartupScreen();
void InvalidateStartupScreen(int left, int top, int right, int bottom); void InvalidateStartupScreen(int left, int top, int right, int bottom);
void SetWindowTitle(const char* caption);
HWND GetHandle() { return Window; } HWND GetHandle() { return Window; }

View file

@ -11,7 +11,7 @@
void I_GetVulkanDrawableSize(int *width, int *height) void I_GetVulkanDrawableSize(int *width, int *height)
{ {
assert(Window); assert(mainwindow.GetHandle());
RECT clientRect = { 0 }; RECT clientRect = { 0 };
GetClientRect(mainwindow.GetHandle(), &clientRect); GetClientRect(mainwindow.GetHandle(), &clientRect);