-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPatchWindowManagement.cpp
More file actions
208 lines (168 loc) · 5.93 KB
/
PatchWindowManagement.cpp
File metadata and controls
208 lines (168 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <windows.h>
#include "GameExeImports.hpp"
#include <stdio.h>
#include <detours.h>
#include "Log.hpp"
#include <thread>
static volatile bool focus = true;
static bool emperorLauncherDoFullscreen = 1;
static volatile HWND backgroundWindowHandle = nullptr;
static volatile DWORD backgroundWindowThreadId = 0;
PFN_wndProcDuneIII wndProcDuneIIIOrig = wndProcDuneIII;
int __stdcall wndProcDuneIIIPatched(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (emperorLauncherDoFullscreen)
{
// The original wndProc has code that sets up + tears down the renderer on focus gain / loss
// This is not necessary on modern windows. Also, this was causing problems with nesting the
// game window inside the black background window, as the game window itself didn't get those
// messages, so the renderer was never enabled.
// This function force runs the setup code once, when it receives its first event. Theoretically,
// we could also block forwarding WM_ACTIVATEAPP messages. That seems to almost work, but there
// are some weird issues with input after alt-tabbing in and out. So, we just force the event once.
// We also forward in WM_ACTIVATEAPP events from the background / parent window
static bool doOnce = false;
if (!doOnce)
{
wndProcDuneIIIOrig(hWnd, WM_ACTIVATEAPP, TRUE, 0);
doOnce = true;
}
}
// Something in WOLAPI likes to hang on exit, so let's just *really* make sure we exit
if (Msg == WM_DESTROY)
TerminateProcess(GetCurrentProcess(), 0);
return wndProcDuneIIIOrig(hWnd, Msg, wParam, lParam);
}
PFN_setWindowStyleAndDrainMessages setWindowStyleAndDrainMessagesOrig = setWindowStyleAndDrainMessages;
BOOL __cdecl setWindowStyleAndDrainMessagesPatched(HWND hWnd, int width, int height, char windowedMode)
{
if (emperorLauncherDoFullscreen)
{
SetWindowLongA(hWnd, GWL_STYLE, WS_POPUP);
SetParent(hWnd, backgroundWindowHandle);
int left = GetSystemMetrics(SM_CXSCREEN) / 2 - width / 2;
SetWindowPos(hWnd, nullptr, left, 0, width, height, SWP_SHOWWINDOW);
BOOL result;
MSG msg;
for (result = PeekMessageA(&msg, 0, 0, 0, 0); result; result = PeekMessageA(&msg, 0, 0, 0, 0))
{
GetMessageA(&msg, 0, 0, 0);
if (msg.message == WM_QUIT && (msg.hwnd == mainWindowHandle || (HWND)msg.wParam == mainWindowHandle))
gDoQuit = 1;
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
SetForegroundWindow(hWnd);
return result;
}
else
{
BOOL retval = setWindowStyleAndDrainMessagesOrig(hWnd, width, height, windowedMode);
int left = GetSystemMetrics(SM_CXSCREEN) / 2 - width / 2;
int top = GetSystemMetrics(SM_CYSCREEN) / 2 - height / 2;
SetWindowPos(hWnd, nullptr, left, top, width, height, SWP_SHOWWINDOW);
return retval;
}
}
LRESULT __stdcall backgroundWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_ACTIVATEAPP)
{
Log("GOT WM_ACTIVATEAPP %d\n", wParam);
}
// we need to forward these events to the game window or input gets screwy, see comment in wndProcDuneIIIPatched()
if (mainWindowHandle && Msg == WM_ACTIVATEAPP)
{
SetFocus(mainWindowHandle);
Log("FORWARD WM_ACTIVATEAPP %d\n", wParam);
SendMessageA(mainWindowHandle, Msg, wParam, lParam);
}
return DefWindowProcA(hWnd, Msg, wParam, lParam);
}
void createBackgroundWindow()
{
backgroundWindowThreadId = GetCurrentThreadId();
const char CLASS_NAME[] = "DuneIII background";
WNDCLASSA wc = { };
wc.lpfnWndProc = backgroundWndProc;
wc.hInstance = GetModuleHandleA(nullptr);
wc.lpszClassName = CLASS_NAME;
RegisterClassA(&wc);
HWND temp = CreateWindowExA(
0,//WS_EX_TOOLWINDOW, // Optional window styles.
CLASS_NAME, // Window class
nullptr, // Window text
WS_CLIPCHILDREN | WS_POPUP, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
wc.hInstance, // Instance handle
NULL // Additional application data
);
ShowWindow(temp, SW_MAXIMIZE);
HBRUSH black = CreateSolidBrush(RGB(0, 0, 0));
SetClassLongPtrA(temp, GCLP_HBRBACKGROUND, (LONG_PTR)black);
InvalidateRect(temp, nullptr, 1);
backgroundWindowHandle = temp;
}
void backgroundWindowLoop()
{
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, backgroundWindowHandle, 0, 0)) != 0)
{
if (bRet == -1)
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
void captureCursorLoop()
{
while (true)
{
Sleep(1000 * 1);
if (!mainWindowHandle)
continue;
if (mainWindowHandle != GetForegroundWindow())
continue;
RECT rect = {};
if (!GetClientRect(mainWindowHandle, &rect))
continue;
POINT topLeft = { 0, 0 };
if (!ClientToScreen(mainWindowHandle, &topLeft))
continue;
rect.left += topLeft.x;
rect.right += topLeft.x;
rect.top += topLeft.y;
rect.bottom += topLeft.y;
rect.left += 10;
rect.top += 10;
rect.right -= 10;
rect.bottom -= 10;
if (!IsDebuggerPresent())
ClipCursor(&rect);
}
}
void patchWindowManagement(bool doFullscreen, bool doCursorCapture)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)wndProcDuneIIIOrig, wndProcDuneIIIPatched);
DetourAttach(&(PVOID&)setWindowStyleAndDrainMessagesOrig, setWindowStyleAndDrainMessagesPatched);
DetourTransactionCommit();
emperorLauncherDoFullscreen = doFullscreen;
if (emperorLauncherDoFullscreen)
{
createBackgroundWindow();
std::thread([]() { backgroundWindowLoop(); }).detach();
}
if (doCursorCapture)
std::thread([]() { captureCursorLoop(); }).detach();
}