/
main.cpp
455 lines (360 loc) · 11.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//#define _HAS_EXCEPTIONS 0
#define STRICT
#define WIN32_LEAN_AND_MEAN
#pragma warning(push)
#pragma warning(disable:4389)//warning C4389: '==' : signed/unsigned mismatch
#pragma warning(disable:4018)//warning C4018: <operator> : signed/unsigned mismatch
#pragma warning(disable:4702)//warning C4702: unreachable code
#include <windows.h>
#include <TlHelp32.h>
#include <shellapi.h>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdarg.h>
#include "TRexpp.h"
#include <shlwapi.h>
#pragma warning(pop)
extern const bool g_false = false;
#ifdef _DEBUG
#define ENABLE_OUTPUTDEBUGSTRING
#endif
#ifdef ENABLE_OUTPUTDEBUGSTRING
#define ODS(X) OutputDebugString(X)
#else//ENABLE_OUTPUTDEBUGSTRING
#define ODS(X)
#endif//ENABLE_OUTPUTDEBUGSTRING
#define GTC (void(0))
#ifndef GTC
#define GTC\
do\
{\
tick=GetTickCount()-tick;\
ODS(strprintf("%s(%d): tick=%lu\n",__FILE__,__LINE__,tick));\
}\
while(g_false)
#endif
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
static const char *strprintf(const char *fmt, ...)
{
static char s_buf[1024];
va_list v;
va_start(v, fmt);
_vsnprintf(s_buf, sizeof s_buf - 1, fmt, v);
va_end(v);
return s_buf;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
struct MonitorInfo
{
HMONITOR hmonitor;
size_t original_index;
MONITORINFOEX info;
};
static BOOL CALLBACK GetAllMonitorsMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
(void)hdcMonitor, (void)lprcMonitor;
auto monitors = (std::vector<MonitorInfo> *)dwData;
MonitorInfo mi;
mi.hmonitor = hMonitor;
mi.original_index = monitors->size();
mi.info.cbSize = sizeof mi.info;
GetMonitorInfo(mi.hmonitor, &mi.info);
monitors->emplace_back(mi);
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
static void EnumProcesses(std::vector<PROCESSENTRY32> *processes)
{
processes->clear();
// Find file name of program that owns the window.
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return;
PROCESSENTRY32 pe;
pe.dwSize = sizeof pe;
if (Process32First(snapshot, &pe))
{
do
{
if (pe.dwSize == sizeof pe)
processes->push_back(pe);
pe.dwSize = sizeof pe;
} while (Process32Next(snapshot, &pe));
CloseHandle(snapshot);
}
}
static void FindExeFileByProcessId(char *exe_file, DWORD process_id, const std::vector<PROCESSENTRY32> &processes)
{
strcpy(exe_file, "");
for (unsigned i = 0; i < processes.size(); ++i)
{
const PROCESSENTRY32 *pe = &processes[i];
if (pe->th32ProcessID == process_id)
{
strcpy(exe_file, pe->szExeFile);
break;
}
}
}
struct WindowDetails
{
unsigned flags;
DWORD process_id;
char exe_file[MAX_PATH];
};
static void GetWindowDetails(const std::vector<PROCESSENTRY32> &processes, HWND hwnd, WindowDetails *details)
{
GetWindowThreadProcessId(hwnd, &details->process_id);
FindExeFileByProcessId(details->exe_file, details->process_id,processes);
char exe_name[MAX_PATH];
strcpy(exe_name, details->exe_file);
PathStripPath(exe_name);
int n = GetWindowTextLength(hwnd);
std::vector<char> title(n + 1);
GetWindowText(hwnd, &title[0], title.size());
details->flags = 0;
// Add flags based on window caps
if ((GetWindowLong(hwnd, GWL_STYLE)&WS_THICKFRAME) == 0)
details->flags |= F_DONT_RESIZE;
// Add flags from INI
for (const FlagsName *f = g_flags_names; f->name; ++f)
AddFlag(&details->flags, hwnd, &title[0], exe_name, f->name, f->flags);
}
static void Help(const wchar_t *bad_arg)
{
std::wstring msg;
msg = L"Bad flag \"";
msg += bad_arg;
msg += L"\" on command line.\n\nValid flags are:\n\n";
for (const FlagsName *f = g_flags_names; f->name; ++f)
{
msg += f->wname;
msg += L"\n";
}
MessageBoxW(0, msg.c_str(), L"dispswitch error", MB_OK | MB_ICONERROR);
}
static BOOL CALLBACK GetAllVisibleWindowsWindowEnumProc(HWND hwnd, LPARAM lParam)
{
std::vector<HWND> *hwnds = reinterpret_cast<std::vector<HWND> *>(lParam);
if (IsWindowVisible(hwnd))
hwnds->push_back(hwnd);
return TRUE;
}
static LONG GetRectWidth(const RECT &r)
{
return r.right - r.left;
}
static LONG GetRectHeight(const RECT &r)
{
return r.bottom - r.top;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
DWORD tick = GetTickCount();
(void)tick;
unsigned cmd_line_flags = 0;
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int i = 1; i < argc; ++i)
{
const FlagsName *f;
for (f = g_flags_names; f->name; ++f)
{
if (_wcsicmp(argv[i], f->wname) == 0)
{
cmd_line_flags |= f->flags;
break;
}
}
if (!f->name)
{
Help(argv[i]);
return 1;
}
}
GTC;
char exe[MAX_PATH];
GetModuleFileName(GetModuleHandle(0), exe, sizeof exe);
PathRemoveFileSpec(exe);
PathCombine(g_ini_path, exe, g_ini_file_name);
ODS(strprintf("dispswitch: %s: INI path is \"%s\".\n", __FUNCTION__, g_ini_path));
// STARTUPINFO si;
// GetStartupInfo(&si);
GTC;
std::vector<MonitorInfo> monitors;
EnumDisplayMonitors(0, 0, &GetAllMonitorsMonitorEnumProc, LPARAM(&monitors));
if (monitors.size() <= 1)
return 0;
std::sort(monitors.begin(), monitors.end(), [](const MonitorInfo &a, const MonitorInfo &b) { return a.info.rcWork.left < b.info.rcWork.left; });
for (unsigned i = 0; i < monitors.size(); ++i)
{
const MonitorInfo *mi = &monitors[i];
(void)mi;
ODS(strprintf("dispswitch: monitor %u/%u:\n", 1 + i, monitors.size()));
ODS(strprintf(" Original index: %u\n", (unsigned)mi->original_index));
ODS(strprintf(" Device=\"%s\"\n", mi->info.szDevice));
ODS(strprintf(" MonitorRect=(%ld,%ld)-(%ld,%ld)\n",
mi->info.rcMonitor.left, mi->info.rcMonitor.top, mi->info.rcMonitor.right, mi->info.rcMonitor.bottom));
ODS(strprintf(" WorkRect=(%ld,%ld)-(%ld,%ld)\n",
mi->info.rcWork.left, mi->info.rcWork.top, mi->info.rcWork.right, mi->info.rcWork.bottom));
}
//
std::vector<PROCESSENTRY32> processes;
EnumProcesses(&processes);
GTC;
//
HWND fg = GetForegroundWindow();
WindowDetails fg_details;
GetWindowDetails(processes, fg, &fg_details);
ODS(strprintf("exe=%s\n", fg_details.exe_file));
std::vector<HWND> hwnds;
if (fg_details.flags&F_TAKE_EXE)
{
EnumWindows(&GetAllVisibleWindowsWindowEnumProc, LPARAM(&hwnds));
ODS(strprintf("%u hwnds.\n", hwnds.size()));
std::vector<HWND>::iterator hwnd = hwnds.begin();
while (hwnd != hwnds.end())
{
char exe_file[MAX_PATH];
GetExeFileForWindow(processes, *hwnd, exe_file);
if (strcmp(exe_file, fg_details.exe_file) == 0)
++hwnd;
else
hwnd = hwnds.erase(hwnd);
}
}
else
{
HWND h = fg;
do
{
hwnds.push_back(h);
h = GetParent(h);
} while (h && (fg_details.flags&F_TAKE_PARENT));
}
GTC;
ODS(strprintf("%u hwnds.\n", hwnds.size()));
for (unsigned i = 0; i < hwnds.size(); ++i)
{
HWND fg_wnd = hwnds[i];
bool was_zoomed = false;
if (IsZoomed(fg_wnd))
{
was_zoomed = true;
ShowWindow(fg_wnd, SW_RESTORE);
}
RECT fg_rect;
GetWindowRect(fg_wnd, &fg_rect);
ODS(strprintf("dispswitch: old rect=(%ld,%ld)->(%ld,%ld).\n", fg_rect.left, fg_rect.top, fg_rect.right,
fg_rect.bottom));
HMONITOR fg_monitor = MonitorFromRect(&fg_rect, MONITOR_DEFAULTTOPRIMARY);
size_t monitor_idx;
for (monitor_idx = 0; monitor_idx < monitors.size(); ++monitor_idx)
{
if (monitors[monitor_idx].hmonitor == fg_monitor)
break;
}
if (monitor_idx >= monitors.size())
{
// Oh dear.
continue;
}
const MONITORINFOEX *old_info = &monitors[monitor_idx].info;
++monitor_idx;
monitor_idx %= monitors.size();
const MONITORINFOEX *new_info = &monitors[monitor_idx].info;
//
RECT dr;
dr.left = fg_rect.left - old_info->rcWork.left;
dr.top = fg_rect.top - old_info->rcWork.top;
dr.right = fg_rect.right - old_info->rcWork.right;
dr.bottom = fg_rect.bottom - old_info->rcWork.bottom;
if (fg_details.flags&F_DONT_RESIZE)
{
LONG cx = (fg_rect.left + fg_rect.right) / 2 - old_info->rcWork.left;
LONG cy = (fg_rect.top + fg_rect.bottom) / 2 - old_info->rcWork.top;
float tcx = cx / (float)GetRectWidth(old_info->rcWork);
float tcy = cy / (float)GetRectHeight(old_info->rcWork);
LONG fg_w = GetRectWidth(fg_rect);
LONG fg_h = GetRectHeight(fg_rect);
fg_rect.left = new_info->rcWork.left + LONG(tcx*GetRectWidth(new_info->rcWork) + .5f) - fg_w / 2;
fg_rect.top = new_info->rcWork.top + LONG(tcy*GetRectHeight(new_info->rcWork) + .5f) - fg_h / 2;
fg_rect.right = fg_rect.left + fg_w;
fg_rect.bottom = fg_rect.top + fg_h;
// don't let it go outside the work rect.
LONG dx = new_info->rcWork.left - fg_rect.left;
if (dx < 0)
dx = 0;
LONG dy = new_info->rcWork.top - fg_rect.top;
if (dy < 0)
dy = 0;
OffsetRect(&fg_rect, dx, dy);
}
else
{
float left_frac, top_frac, right_frac, bottom_frac;
{
RECT local_rect = fg_rect;
OffsetRect(&local_rect, -old_info->rcWork.left, -old_info->rcWork.top);
left_frac = local_rect.left / float(GetRectWidth(old_info->rcWork));
top_frac = local_rect.top / float(GetRectHeight(old_info->rcWork));
right_frac = local_rect.right / float(GetRectWidth(old_info->rcWork));
bottom_frac = local_rect.bottom / float(GetRectHeight(old_info->rcWork));
}
if (fg_details.flags&F_SIDE_RELATIVE)
{
fg_rect.left = new_info->rcWork.left + dr.left;
fg_rect.top = new_info->rcWork.top + dr.top;
fg_rect.right = new_info->rcWork.right + dr.right;
fg_rect.bottom = new_info->rcWork.bottom + dr.bottom;
}
else
{
// round to nearest instead of round to zero, prevents window
// floating towards the origin with repeated invocations (or at
// least it did with the test window).
fg_rect.left = new_info->rcWork.left + LONG(left_frac*GetRectWidth(new_info->rcWork) + .5f);
fg_rect.top = new_info->rcWork.top + LONG(top_frac*GetRectHeight(new_info->rcWork) + .5f);
fg_rect.right = new_info->rcWork.left + LONG(right_frac*GetRectWidth(new_info->rcWork) + .5f);
fg_rect.bottom = new_info->rcWork.top + LONG(bottom_frac*GetRectHeight(new_info->rcWork) + .5f);
}
// Work out a minimum size for this window.
LONG min_width, min_height;
{
// Include a small client area
RECT rect = { 0, 0, 100, 100 };
DWORD style = GetWindowLong(fg_wnd, GWL_STYLE);
DWORD ex_style = GetWindowLong(fg_wnd, GWL_EXSTYLE);
BOOL menu = !!GetMenu(fg_wnd);
AdjustWindowRectEx(&rect, style, menu, ex_style);
min_width = GetRectWidth(rect);
min_height = GetRectHeight(rect);
ODS(strprintf("dispswitch: min size=%ldx%ld\n", min_width, min_height));
}
// Clamp size, rather crudely.
if (GetRectWidth(fg_rect) < min_width)
fg_rect.right = fg_rect.left + min_width;
if (GetRectHeight(fg_rect) < min_height)
fg_rect.bottom = fg_rect.top + min_height;
}
ODS(strprintf("dispswitch: new rect=(%ld,%ld)->(%ld,%ld).\n", fg_rect.left, fg_rect.top, fg_rect.right,
fg_rect.bottom));
// BOOL ok=MoveWindow(fg_wnd,fg_rect.left,fg_rect.top,fg_rect.right-fg_rect.left,
// fg_rect.bottom-fg_rect.top,TRUE);
BOOL ok = SetWindowPos(fg_wnd, 0, fg_rect.left, fg_rect.top, fg_rect.right - fg_rect.left,
fg_rect.bottom - fg_rect.top, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOCOPYBITS);
if (!ok)
{
DWORD err = GetLastError();
(void)err;
ODS(strprintf("dispswitch: move failed. Error: 0x%08lX\n", err));
}
if (was_zoomed)
ShowWindow(fg_wnd, SW_MAXIMIZE);
}
return 0;
}