-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.cpp
168 lines (143 loc) · 6.52 KB
/
src.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
// github.com/cafali/ResolutionTweak
#include <windows.h>
#include <fstream>
#include <string>
#define CFG_FILE "config.cfg"
// get highest available refresh rate for a given resolution
int GetHighestRefreshRate(int width, int height) {
DEVMODE devMode;
ZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
devMode.dmPelsWidth = width;
devMode.dmPelsHeight = height;
devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
int highestRefreshRate = 0;
for (int i = 0; EnumDisplaySettings(NULL, i, &devMode); i++) {
if (devMode.dmPelsWidth == width && devMode.dmPelsHeight == height) {
if (devMode.dmDisplayFrequency > highestRefreshRate) {
highestRefreshRate = devMode.dmDisplayFrequency;
}
}
}
return highestRefreshRate;
}
// change the display resolution and refresh rate
bool ChangeResolutionAndRefreshRate(int width, int height, int refreshRate) {
DEVMODE devMode;
ZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode)) {
MessageBox(NULL, "Failed to get current display settings.", "Error", MB_OK | MB_ICONERROR);
return false;
}
devMode.dmPelsWidth = width;
devMode.dmPelsHeight = height;
devMode.dmDisplayFrequency = refreshRate;
devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
LONG result = ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_UPDATEREGISTRY, NULL);
if (result != DISP_CHANGE_SUCCESSFUL) {
MessageBox(NULL, "Failed to change resolution and refresh rate.", "Error", MB_OK | MB_ICONERROR);
return false;
}
return true;
}
// get the current screen resolution and refresh rate
bool GetCurrentResolutionAndRefreshRate(int& width, int& height, int& refreshRate) {
DEVMODE devMode;
ZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode)) {
MessageBox(NULL, "Failed to get current display settings.", "Error", MB_OK | MB_ICONERROR);
return false;
}
width = devMode.dmPelsWidth;
height = devMode.dmPelsHeight;
refreshRate = devMode.dmDisplayFrequency;
return true;
}
// read resolution values from the configuration file
bool ReadConfig(int& customWidth, int& customHeight, int& nativeWidth, int& nativeHeight, int& nativeRefreshRate, int& customRefreshRate) {
std::ifstream cfgFile(CFG_FILE);
if (!cfgFile.is_open()) {
MessageBox(NULL, "Failed to open config file for reading.", "Error", MB_OK | MB_ICONERROR);
return false;
}
std::string line;
while (std::getline(cfgFile, line)) {
if (line.find("CX=") == 0) {
customWidth = std::stoi(line.substr(3));
} else if (line.find("CY=") == 0) {
customHeight = std::stoi(line.substr(3));
} else if (line.find("NX=") == 0) {
nativeWidth = std::stoi(line.substr(3));
} else if (line.find("NY=") == 0) {
nativeHeight = std::stoi(line.substr(3));
} else if (line.find("NR=") == 0) {
nativeRefreshRate = std::stoi(line.substr(3));
} else if (line.find("CR=") == 0) { // Check for custom refresh rate
customRefreshRate = std::stoi(line.substr(3));
}
}
cfgFile.close();
return true;
}
// write resolution values to the configuration file
bool WriteConfig(int customWidth, int customHeight, int nativeWidth, int nativeHeight, int nativeRefreshRate, int customRefreshRate) {
std::ofstream outFile(CFG_FILE);
if (!outFile.is_open()) {
MessageBox(NULL, "Failed to open config file for writing.", "Error", MB_OK | MB_ICONERROR);
return false;
}
outFile << "## CustomRes\n";
outFile << "CX=" << customWidth << "\n";
outFile << "CY=" << customHeight << "\n";
outFile << "CR=" << customRefreshRate << "\n"; // Add custom refresh rate to config
outFile << "\n## NativeRes\n";
outFile << "NX=" << nativeWidth << "\n";
outFile << "NY=" << nativeHeight << "\n";
outFile << "NR=" << nativeRefreshRate << "\n";
outFile.close();
return true;
}
// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
int customWidth = 0, customHeight = 0;
int nativeWidth = 0, nativeHeight = 0;
int nativeRefreshRate = 0;
int customRefreshRate = 0; // Added custom refresh rate
// read resolution from config.cfg
if (!ReadConfig(customWidth, customHeight, nativeWidth, nativeHeight, nativeRefreshRate, customRefreshRate)) {
MessageBox(NULL, "Failed to read configuration file", "Error", MB_OK | MB_ICONERROR);
return 1;
}
// get current screen resolution and refresh rate
int currentWidth, currentHeight, currentRefreshRate;
if (!GetCurrentResolutionAndRefreshRate(currentWidth, currentHeight, currentRefreshRate)) {
MessageBox(NULL, "Failed to get current screen resolution and refresh rate", "Error", MB_OK | MB_ICONERROR);
return 1;
}
// check current resolution matches the custom resolution
if (currentWidth == customWidth && currentHeight == customHeight) {
if (!ChangeResolutionAndRefreshRate(nativeWidth, nativeHeight, nativeRefreshRate)) {
MessageBox(NULL, "Failed to revert resolution and refresh rate to native settings", "Error", MB_OK | MB_ICONERROR);
return 1;
}
} else {
// detect refresh rate to use
int refreshRateToUse = (customRefreshRate > 0) ? customRefreshRate : GetHighestRefreshRate(customWidth, customHeight);
if (refreshRateToUse == 0) {
MessageBox(NULL, "Failed to find a suitable refresh rate for custom resolution", "Error", MB_OK | MB_ICONERROR);
return 1;
}
if (!ChangeResolutionAndRefreshRate(customWidth, customHeight, refreshRateToUse)) {
MessageBox(NULL, "Failed to change resolution and refresh rate to custom settings", "Error", MB_OK | MB_ICONERROR);
return 1;
}
// save the current resolution
if (!WriteConfig(customWidth, customHeight, nativeWidth, nativeHeight, nativeRefreshRate, customRefreshRate)) {
MessageBox(NULL, "Failed to save resolution to config file", "Error", MB_OK | MB_ICONERROR);
return 1;
}
}
return 0;
}