-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrangeInstances.cpp
More file actions
223 lines (195 loc) · 7.94 KB
/
ArrangeInstances.cpp
File metadata and controls
223 lines (195 loc) · 7.94 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Build command:
// cl /EHsc /std:c++17 ArrangeInstances.cpp /link User32.lib
#include <Windows.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <optional>
#include <regex>
#include <tuple>
#include <vector>
void DisableWindowStyle(const HWND& hWnd) {
// The styles to disable.
const LONG_PTR style = WS_DLGFRAME | // Dialog frame
WS_SIZEBOX | // Sizing border
WS_BORDER | // Thin-line border
WS_CAPTION | // Title bar
WS_TILEDWINDOW | 0xC40000; // "Full state"
LONG_PTR currentStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
SetWindowLongPtr(hWnd, GWL_STYLE, currentStyle & ~style);
}
FILETIME GetWindowCreationTime(HWND hWnd) {
DWORD processId;
GetWindowThreadProcessId(hWnd, &processId);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, processId);
if (hProcess == NULL) return FILETIME{};
FILETIME creationTime, exitTime, kernelTime, userTime;
if (!GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime,
&userTime)) {
CloseHandle(hProcess);
return FILETIME{};
}
CloseHandle(hProcess);
return creationTime;
}
bool CompareHwndByCreationTime(HWND hWnd1, HWND hWnd2) {
FILETIME ft1 = GetWindowCreationTime(hWnd1);
FILETIME ft2 = GetWindowCreationTime(hWnd2);
return CompareFileTime(&ft1, &ft2) <
0; // Returns true if ft1 is earlier than ft2
}
void SortByCreation(std::vector<HWND>& hWndVector) {
std::sort(hWndVector.begin(), hWndVector.end(), CompareHwndByCreationTime);
}
struct EnumWindowsParams {
EnumWindowsParams(const std::regex& regex) : regex(regex) {}
std::vector<HWND> matchingWindows;
const std::regex regex;
};
std::string WindowTitle(const HWND& hWnd) {
int length = GetWindowTextLength(hWnd);
char* buffer = new char[length + 1];
if (!buffer) return "Error";
GetWindowText(hWnd, buffer, length + 1);
std::string windowTitle(buffer);
delete[] buffer;
return windowTitle;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
EnumWindowsParams* params = reinterpret_cast<EnumWindowsParams*>(lParam);
if (!params) return FALSE;
std::string windowTitle = WindowTitle(hWnd);
// Check if the window title matches the regular expression.
if (std::regex_match(windowTitle, params->regex)) {
std::cout << "Window with title \"" << windowTitle << "\" matched!"
<< std::endl;
params->matchingWindows.push_back(hWnd);
}
return TRUE;
}
std::vector<HWND> GetProcesses(const std::regex& regex) {
EnumWindowsParams params(regex);
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(¶ms));
std::cout << "Found " << params.matchingWindows.size()
<< " matching windows:" << std::endl;
for (const auto window : params.matchingWindows) {
std::cout << window << "\t" << WindowTitle(window) << std::endl;
}
SortByCreation(params.matchingWindows);
return params.matchingWindows;
}
// Explanation of how the optimal tiling is found in constant time:
// Constraint: To display all instances, we want to have N instances tiled in a
// rows * columns grid, so: • rows * columns = N Constraint: To maximize screen
// usage, we want the number of columns and rows to reshape the effective
// instanceRatio to match the screen's ratio, so: • (columns / rows) *
// instanceRatio = screenRatio N, instanceRatio, and screenRatio are known. To
// find the optimal tiling, solve the system of equations. • rows * columns = N
// • columns = N/rows
// Substitution:
// • (columns / rows) * instanceRatio = screenRatio
// • (N / rows^2) * instanceRatio = screenRatio
// • rows^2 = instanceRatio/(screenRatio*N)
// • rows = sqrt(instanceRatio/(screenRatio*N))
// Then, since we know the number of rows:
// • columns = N/rows
// Which solves for columns.
// If we could have fractional rows and columns, as the system of equations
// solves for, we would perfectly fill the screen every time. Since this is not
// possible, we use the fractional part of the rows and columns count to
// determine which needs an extra row/column to accommodate all instances.
// Whichever remainder is higher should work better.
// Sometimes it's still not enough and we must add both an extra row and an
// extra column.
std::tuple<int, int> GetOptimalTiling(const double& screenRatio,
const double& instanceRatio,
const int& instanceCount) {
double targetRatio = screenRatio / instanceRatio;
double h = sqrt(instanceCount / targetRatio);
double w = instanceCount / h;
std::cout << "h: " << h << "\t"
<< "w: " << w << std::endl;
int numTall = static_cast<int>(h);
int numWide = static_cast<int>(w);
// Detemine if rounding must occur.
double remTall = h - numTall;
double remWide = w - numWide;
std::cout << "numTall: " << numTall << "\t"
<< "numWide: " << numWide << std::endl;
std::cout << "remTall: " << remTall << "\t"
<< "remWide: " << remWide << std::endl;
if (remTall > 0 || remWide > 0) {
// TODO: Could floating point precision errors make us think we need to do
// this, even when we don't?
if (remTall > remWide) {
numTall++;
if (numTall * numWide < instanceCount) {
numWide++;
}
} else {
numWide++;
if (numTall * numWide < instanceCount) {
numTall++;
}
}
}
std::cout << "numTall: " << numTall << "\t"
<< "numWide: " << numWide << std::endl;
return std::make_tuple(numWide, numTall);
}
std::tuple<int, int> GetDesktopResolution() {
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
return std::make_tuple(desktop.right, desktop.bottom);
}
void PlaceWindows(
const std::vector<HWND>& instances, const double& instanceRatio,
const std::optional<std::tuple<int, int>>& tiling = std::nullopt) {
const auto [screenW, screenH] = GetDesktopResolution();
int numWide, numTall;
if (tiling == std::nullopt) {
double screenRatio = (double)screenW / (double)screenH;
std::tie(numWide, numTall) =
GetOptimalTiling(screenRatio, instanceRatio, instances.size());
} else {
std::tie(numWide, numTall) = tiling.value();
}
// Dimensions of each instance.
const int width = screenW / numWide;
const int height = screenH / numTall;
// Reposition each instance.
for (std::size_t i = 0; i < instances.size(); i++) {
const int xPos = (i % numWide) * width;
const int yPos = (i / numWide) * height;
SetWindowPos(instances[i], NULL, xPos, yPos, width, height, SWP_SHOWWINDOW);
}
}
int main(int argc, char* argv[]) {
if (argc <= 2) {
std::cout << "Usage: " << argv[0]
<< " <instanceRatio (width/height)> <ProcessRegEx>\n"
<< "\tExample: " << argv[0]
<< " 1.33333333333 \"Dolphin.* \\|.*\"" << std::endl
<< "\tExample: " << argv[0] << " 0.666666667 \".*melonDS .*\""
<< std::endl
<< "\tExample: " << argv[0]
<< " 1.77777777777 \"yuzu Mainline.*\"" << std::endl;
return -1;
}
const double instanceRatio = std::stod(argv[1]);
const std::string procRegexStr = argv[2];
std::regex procRegex(procRegexStr, std::regex_constants::grep);
// Get matching proceses.
std::vector<HWND> instances = GetProcesses(procRegex);
for (const auto instance : instances) {
DisableWindowStyle(instance);
}
PlaceWindows(instances, instanceRatio);
return 0;
}