-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
341 lines (292 loc) · 9.76 KB
/
main.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <thread>
#include <windows.h>
#include <winbase.h>
#define CONFIG_FILE_PATH "config.ini"
#define RUNNING_MUTEX "Global\\ValorantTrueStretchRunning"
bool find_best(const std::string& name, int aspect_ratio_x, int aspect_ratio_y, DEVMODE& dev_mode);
bool get_screen_name(int index, std::string& name);
inline void error(const char* message);
int show_message_box_with_timeout(int timeout, const std::string& message, const std::string& title);
bool find_best(const std::string& name, const int aspect_ratio_x, const int aspect_ratio_y, DEVMODE& dev_mode)
{
DEVMODE current;
bool has_found = false;
int i = 0;
while (EnumDisplaySettings(name.c_str(), i++, ¤t))
{
if (current.dmPelsHeight * aspect_ratio_x != current.dmPelsWidth * aspect_ratio_y)
{
continue;
}
if (has_found && current.dmDisplayFrequency < dev_mode.dmDisplayFrequency)
{
continue;
}
if (has_found && current.dmDisplayFrequency == dev_mode.dmDisplayFrequency && current.dmPelsWidth * current.dmPelsHeight < dev_mode.dmPelsWidth * dev_mode.dmPelsHeight)
{
continue;
}
dev_mode = current;
has_found = true;
}
return has_found;
}
bool get_screen_name(const int index, std::string& name)
{
DISPLAY_DEVICE display_device;
display_device.cb = sizeof(DISPLAY_DEVICE);
if (EnumDisplayDevices(nullptr, index, &display_device, 0))
{
name = display_device.DeviceName;
return true;
}
return false;
}
inline void error(const char* message)
{
MessageBoxA(nullptr, message, "Error", MB_OK | MB_ICONWARNING);
}
int show_message_box_with_timeout(const int timeout, const std::string& message, const std::string& title)
{
const HMODULE user32 = LoadLibrary("user32.dll");
if (!user32)
{
return IDNO;
}
typedef int (WINAPI*MessageBoxTimeout_t)(HWND, LPCSTR, LPCSTR, UINT, WORD, DWORD);
const auto MessageBoxTimeout = reinterpret_cast<MessageBoxTimeout_t>(GetProcAddress(user32, "MessageBoxTimeoutA"));
if (!MessageBoxTimeout)
{
FreeLibrary(user32);
return IDNO;
}
const int result = MessageBoxTimeout(
nullptr,
message.c_str(),
title.c_str(),
MB_YESNO | MB_ICONQUESTION,
0,
timeout
);
FreeLibrary(user32);
return result;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
const auto mutex = CreateMutexA(nullptr, TRUE, RUNNING_MUTEX);
try
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Another instance of this application is already running.");
return 1;
}
std::ifstream config_file(CONFIG_FILE_PATH);
if (!config_file.is_open())
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Could not open config file.");
return 1;
}
std::string line;
if (!std::getline(config_file, line))
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Config header not present.");
return 1;
}
if (strcmp(line.c_str(), "[ValorantTrueStretch]") != 0)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Wrong config header.");
return 1;
}
std::map<std::string, std::string> config;
while (std::getline(config_file, line))
{
if (line.empty())
{
continue;
}
if (line.at(0) == '#')
{
continue;
}
const unsigned long off = line.find('=');
if (off == -1)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Invalid config entry.");
return 1;
}
config.emplace(line.substr(0, off), line.substr(off + 1, line.length() - off - 1));
}
int screen_index;
int aspect_ratio_x;
int aspect_ratio_y;
{ // parse config
try
{
screen_index = std::stoi(config["screen_index"]);
}
catch (std::exception& _)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Configuration value screen_index is not a valid number.");
return 1;
}
try
{
const std::string aspect_ratio = config["aspect_ratio"];
const unsigned long off = aspect_ratio.find(':');
if (off == -1)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Invalid aspect ratio string.");
return 1;
}
aspect_ratio_x = std::stoi(aspect_ratio.substr(0, off));
aspect_ratio_y = std::stoi(aspect_ratio.substr(off + 1, aspect_ratio.length() - off - 1));
}
catch (std::exception& _)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Configuration value aspect_ratio needs formatting x:y where x and y is a number");
return 1;
}
}
auto window = FindWindowA("VALORANTUnrealWindow", nullptr);
if (window)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Please close VALORANT.");
return 1;
}
do
{
window = FindWindowA("VALORANTUnrealWindow", nullptr);
}
while (!window);
DWORD process_id;
GetWindowThreadProcessId(window, &process_id);
const auto process = OpenProcess(SYNCHRONIZE, FALSE, process_id);
if (!process)
{
CloseHandle(window);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Could not open Valorant process.");
return 1;
}
std::string screen_name;
if (!get_screen_name(screen_index, screen_name))
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to find screen.");
return 1;
}
DEVMODE original_screen_settings;
if (!EnumDisplaySettings(screen_name.c_str(), ENUM_CURRENT_SETTINGS, &original_screen_settings))
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to retrieve display settings.");
return 1;
}
DEVMODE dev_mode;
if (!find_best(screen_name, aspect_ratio_x, aspect_ratio_y, dev_mode))
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to find a fitting resolutio for that aspect ratio.");
return 1;
}
DEVMODE fixed_dev_mode;
ZeroMemory(&fixed_dev_mode, sizeof(fixed_dev_mode));
fixed_dev_mode.dmSize = sizeof(fixed_dev_mode);
fixed_dev_mode.dmDisplayFrequency = dev_mode.dmDisplayFrequency;
fixed_dev_mode.dmPelsHeight = dev_mode.dmPelsHeight;
fixed_dev_mode.dmPelsWidth = dev_mode.dmPelsWidth;
fixed_dev_mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
if (ChangeDisplaySettingsExA(screen_name.c_str(), &fixed_dev_mode, nullptr, CDS_RESET, nullptr) != DISP_CHANGE_SUCCESSFUL)
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to set screen resolution.");
return 1;
}
if (show_message_box_with_timeout(10000, "Please confirm your new display settings within 10 seconds, otherwise they will be reset.", "Confirmation") != IDYES)
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
if (ChangeDisplaySettingsExA(screen_name.c_str(), &original_screen_settings, nullptr, 0, nullptr) != DISP_CHANGE_SUCCESSFUL)
{
error("Failed to reset display settings.");
}
else
{
error("Did not confirm - display settings reset.");
}
return 1;
}
if (!SetWindowLongPtr(window, GWL_STYLE, GetWindowLongPtr(window, GWL_STYLE) & ~(WS_DLGFRAME | WS_BORDER)))
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to set window style.");
return 1;
}
if (!ShowWindow(window, SW_MAXIMIZE))
{
CloseHandle(window);
CloseHandle(process);
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to maximize window.");
return 1;
}
CloseHandle(window);
WaitForSingleObject(process, INFINITE);
CloseHandle(process);
if (ChangeDisplaySettingsExA(screen_name.c_str(), &original_screen_settings, nullptr, 0, nullptr) != DISP_CHANGE_SUCCESSFUL)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
error("Failed to reset screen resolution.");
return 1;
}
}
catch (std::exception& _)
{
}
ReleaseMutex(mutex);
CloseHandle(mutex);
return 0;
}