Repository navigation
Expand file tree
/
Copy pathOutputWindow.cpp
More file actions
395 lines (356 loc) · 13.3 KB
/
Copy pathOutputWindow.cpp
File metadata and controls
395 lines (356 loc) · 13.3 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
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
#include "OutputWindow.h"
#include "Model.h"
namespace deskdup {
namespace {
void ShowWindowBorder(HWND windowHandle, bool shown) noexcept {
auto style = GetWindowLong(windowHandle, GWL_STYLE);
auto exstyle = GetWindowLong(windowHandle, GWL_EXSTYLE);
const auto flags = WS_THICKFRAME | WS_CAPTION;
if (shown) {
style |= flags;
exstyle |= WS_EX_OVERLAPPEDWINDOW;
}
else {
style &= ~flags;
exstyle &= ~WS_EX_OVERLAPPEDWINDOW;
}
SetWindowLong(windowHandle, GWL_STYLE, style);
SetWindowLong(windowHandle, GWL_EXSTYLE, exstyle);
}
auto createWindow(win32::WindowClass const &windowClass, Rect rect) -> win32::WindowWithMessages {
return windowClass.createWindow({
.style = win32::WindowStyle::overlappedWindow(),
.name = L"Duplicate Desktop Presenter (Double Click to toggle fullscreen)",
.rect = rect,
});
}
auto windowRegion(HWND windowHandle) {
auto windowInfo = WINDOWINFO{};
windowInfo.cbSize = sizeof(WINDOWINFO);
::GetWindowInfo(windowHandle, &windowInfo);
auto top = (windowInfo.rcClient.top - windowInfo.rcWindow.top);
auto left = (windowInfo.rcClient.left - windowInfo.rcWindow.left);
auto right = (windowInfo.rcWindow.right - windowInfo.rcClient.right);
auto bottom = (windowInfo.rcWindow.bottom - windowInfo.rcClient.bottom);
auto w = windowInfo.rcWindow.right - windowInfo.rcWindow.left;
auto h = windowInfo.rcWindow.bottom - windowInfo.rcWindow.top;
auto polys = std::array{
// outer
POINT{0, 0},
POINT{w, 0},
POINT{w, h},
POINT{0, h},
// inner
POINT{left, top},
POINT{left, h - bottom},
POINT{w - right, h - bottom},
POINT{w - right, top},
};
auto edgeCounts = std::array{4, 4};
auto rgn = CreatePolyPolygonRgn(polys.data(), edgeCounts.data(), static_cast<int>(edgeCounts.size()), WINDING);
auto redraw = BOOL{TRUE};
SetWindowRgn(windowHandle, rgn, redraw);
DeleteObject(rgn);
}
} // namespace
OutputWindow::OutputWindow(Args const &initArgs)
: m_controller{initArgs.mainController}
, m_window{createWindow(initArgs.windowClass, m_controller.config().outputRect())} {
m_window.setMessageHandler(this);
}
void OutputWindow::show(bool isMaximized) {
updateMaximized(isMaximized);
if (isMaximized) {
m_window.showMaximized();
}
else {
m_window.showNormal();
}
}
void OutputWindow::updateRect(Rect rect) {
m_window.moveClient(rect);
if (m_controller.config().operationMode == OperationMode::CaptureArea &&
m_controller.state().duplicationStatus == DuplicationStatus::Pause) {
windowRegion(m_window.handle());
}
}
void OutputWindow::updateMaximized(bool isMaximized) {
ShowWindowBorder(m_window.handle(), !isMaximized);
m_taskbarButtons.updateMaximized(isMaximized);
}
void OutputWindow::updateOperationMode(OperationMode operationMode) {
if (operationMode == OperationMode::CaptureArea) {
if (m_controller.state().duplicationStatus == DuplicationStatus::Live) {
::SetForegroundWindow(::GetNextWindow(m_window.handle(), GW_HWNDNEXT));
m_window.toBottom();
}
else {
windowRegion(m_window.handle());
}
}
if (operationMode == OperationMode::PresentMirror) {
if (m_controller.state().duplicationStatus == DuplicationStatus::Live) {
m_window.toTop();
}
else {
auto redraw = BOOL{TRUE};
SetWindowRgn(m_window.handle(), nullptr, redraw);
}
}
}
void OutputWindow::updateDuplicationStatus(DuplicationStatus status, PauseRendering pauseRendering) {
if (m_controller.config().operationMode == OperationMode::CaptureArea) {
if (status == DuplicationStatus::Live) {
auto redraw = BOOL{TRUE};
SetWindowRgn(m_window.handle(), nullptr, redraw);
::SetForegroundWindow(::GetNextWindow(m_window.handle(), GW_HWNDNEXT));
m_window.toBottom();
}
else {
m_window.toTop();
windowRegion(m_window.handle());
}
}
m_taskbarButtons.updateDuplicationStatus(status, pauseRendering);
}
void OutputWindow::size(const Dimension &dimension, uint32_t flags) {
if (flags == SIZE_MINIMIZED) return;
m_controller.resizeOutputWindow(dimension, (flags == SIZE_MAXIMIZED));
if (m_controller.config().operationMode == OperationMode::CaptureArea &&
m_controller.state().duplicationStatus == DuplicationStatus::Pause) {
windowRegion(m_window.handle());
}
}
void OutputWindow::move(const Point &topLeft) { m_controller.moveOutputWindowTo(topLeft); }
bool OutputWindow::position(const win32::WindowPosition &windowPosition) {
if (windowPosition.hasZOrder()) {
auto topWindow = ::GetTopWindow(nullptr);
if ((windowPosition.insertAfter == topWindow || windowPosition.insertAfter == HWND_NOTOPMOST) &&
m_controller.config().operationMode == OperationMode::CaptureArea) {
m_controller.changeDuplicationStatus(DuplicationStatus::Pause);
}
}
return false;
}
void OutputWindow::close() { m_controller.quit(); }
void OutputWindow::mouseMove(const Point &mousePosition, DWORD keyState) {
if (keyState == (MK_LBUTTON | MK_SHIFT)) {
if (m_draggingLastPosition) {
auto dx = mousePosition.x - m_draggingLastPosition->x;
auto dy = mousePosition.y - m_draggingLastPosition->y;
m_controller.moveCaptureOffsetByScreen(dx, dy);
m_draggingLastPosition = mousePosition;
}
}
}
void OutputWindow::mouseLeftButtonDown(const Point &mousePosition, DWORD keyState) {
if (keyState == (MK_LBUTTON | MK_SHIFT)) {
m_draggingLastPosition = mousePosition;
}
}
void OutputWindow::mouseLeftButtonUp(const Point & /*mousePosition*/, DWORD /*keyState*/) {
m_draggingLastPosition = {};
}
bool OutputWindow::mouseRightButtonUp(const Point & /*mousePosition*/, DWORD keyState) {
if (keyState == MK_SHIFT) {
m_controller.toggleVisualizeCaptureArea();
return true;
}
else if (keyState == MK_CONTROL) {
m_pickWindow ^= true;
return true;
}
return false;
}
enum MenuCommands : int {
Menu_TogglePause = 1,
Menu_Resolutions = 100,
Menu_ResolutionMax = 199,
Menu_Screens = 200,
Menu_ScreenMax = 299,
Menu_ModePresentMirror = 300,
Menu_ModeCaptureRegion = 301,
};
struct Resolution {
win32::Dimension dim;
win32::Name label;
};
auto resolutions = std::array{
Resolution{{3840, 2800}, L"3840x2800"},
Resolution{{3840, 2560}, L"3840x2560"},
Resolution{{2560, 1600}, L"2560x1600"},
Resolution{{2560, 1440}, L"2560x1440"},
Resolution{{1920, 1200}, L"1920x1200"},
Resolution{{1920, 1080}, L"1920x1080"},
Resolution{{1280, 960}, L"1280x960"},
Resolution{{1280, 720}, L"1280x720"},
Resolution{{1024, 768}, L"1024x768"},
};
void OutputWindow::contextMenu(const Point &position) {
auto hPopupMenu = CreatePopupMenu();
AppendMenu(
hPopupMenu,
MF_STRING,
Menu_TogglePause,
m_controller.state().duplicationStatus == DuplicationStatus::Pause ? L"Play" : L"Pause");
AppendMenu(hPopupMenu, MF_SEPARATOR, 0, nullptr);
auto hResolutionMenu = CreatePopupMenu();
auto &cfg = m_controller.config();
for (auto i = 0u; auto &res : resolutions) {
auto isChecked = (res.dim == cfg.outputDimension);
AppendMenu(hResolutionMenu, isChecked ? MF_CHECKED : MF_STRING, Menu_Resolutions + i, res.label.data());
++i;
}
AppendMenu(hPopupMenu, MF_POPUP, std::bit_cast<UINT_PTR>(hResolutionMenu), L"Resolutions");
auto hScreenMenu = CreatePopupMenu();
auto currentMonitor = m_controller.operationModeLens().captureMonitor();
for (auto scr = 0; scr < 2; ++scr) {
auto label = std::wstring{L"0"};
label[0] = static_cast<wchar_t>(label[0] + scr);
auto flags = UINT{MF_STRING};
if (currentMonitor == scr) {
flags |= MF_CHECKED;
}
if (cfg.operationMode != OperationMode::PresentMirror) {
flags |= MF_DISABLED;
}
AppendMenu(hScreenMenu, flags, Menu_Screens + scr, label.data());
}
AppendMenu(hPopupMenu, MF_POPUP, std::bit_cast<UINT_PTR>(hScreenMenu), L"Capture Screen");
AppendMenu(hPopupMenu, MF_SEPARATOR, 0, nullptr);
{
auto flags =
m_controller.config().operationMode == OperationMode::PresentMirror ? UINT{MF_CHECKED} : UINT{MF_STRING};
AppendMenu(hPopupMenu, flags, Menu_ModePresentMirror, L"Present Mirror");
}
{
auto flags =
m_controller.config().operationMode == OperationMode::CaptureArea ? UINT{MF_CHECKED} : UINT{MF_STRING};
AppendMenu(hPopupMenu, flags, Menu_ModeCaptureRegion, L"Capture Region");
}
auto const menuPos = [&]() {
if (position.x < 0 || position.y < 0) {
auto tmp = POINT{};
ClientToScreen(m_window.handle(), &tmp);
return Point::fromPOINT(tmp);
}
return position;
}();
TrackPopupMenuEx(hPopupMenu, 0, menuPos.x, menuPos.y, m_window.handle(), nullptr);
DestroyMenu(hResolutionMenu);
DestroyMenu(hScreenMenu);
DestroyMenu(hPopupMenu);
}
auto OutputWindow::menuCommand(uint16_t command) -> win32::OptLRESULT {
if (command == Menu_TogglePause) m_controller.togglePause();
if (command >= Menu_Resolutions && command <= Menu_ResolutionMax) {
auto &res = resolutions.at(command - Menu_Resolutions);
m_controller.resizeOutputWindow(res.dim, false);
return LRESULT{};
}
if (command >= Menu_Screens && command <= Menu_ScreenMax) {
auto scr = command - Menu_Screens;
m_controller.captureScreen(scr);
return LRESULT{};
}
if (command == Menu_ModePresentMirror) {
m_controller.changeOperationMode(OperationMode::PresentMirror);
}
if (command == Menu_ModeCaptureRegion) {
m_controller.changeOperationMode(OperationMode::CaptureArea);
}
return {};
}
void OutputWindow::mouseRightDoubleClick(const Point & /*mousePosition*/, DWORD /*keyState*/) { m_pickWindow ^= true; }
void OutputWindow::setFocus() {
if (m_controller.config().operationMode == OperationMode::CaptureArea) {
m_controller.changeDuplicationStatus(DuplicationStatus::Pause);
}
}
void OutputWindow::killFocus() {
if (m_pickWindow) {
m_pickWindow = false;
auto window = win32::Window::fromForeground();
auto rect = m_controller.operationModeLens().captureAreaRect();
auto mk = modifierKeys();
if (mk == MK_SHIFT)
window.moveBorder(rect);
else
window.moveClient(rect);
}
}
bool OutputWindow::inputKeyDown(uint32_t keyCode) {
const auto mk = modifierKeys();
switch (keyCode) {
case VK_UP:
if (mk == MK_SHIFT) m_controller.moveCaptureOffsetByScreen(0, -1);
return true;
case VK_DOWN:
if (mk == MK_SHIFT) m_controller.moveCaptureOffsetByScreen(0, +1);
return true;
case VK_LEFT:
if (mk == MK_SHIFT) m_controller.moveCaptureOffsetByScreen(-1, 0);
return true;
case VK_RIGHT:
if (mk == MK_SHIFT) m_controller.moveCaptureOffsetByScreen(+1, 0);
return true;
case VK_RETURN:
if (mk == MK_CONTROL) m_controller.toggleOutputMaximize();
return true;
default: {
const auto ch = ::MapVirtualKey(keyCode, MAPVK_VK_TO_CHAR);
switch (ch) {
case '0':
if (mk & MK_CONTROL) m_controller.resetOutputZoom();
return true;
case '+':
if (mk == MK_CONTROL) {
m_controller.zoomOutputBy(0.01f);
}
else if (mk & MK_CONTROL) {
m_controller.zoomOutputBy(0.001f);
}
return true;
case '-':
if (mk == MK_CONTROL) {
m_controller.zoomOutputBy(-0.01f);
}
else if (mk & MK_CONTROL) {
m_controller.zoomOutputBy(-0.001f);
}
return true;
case 'F': // Freeze
case 'P': // Pause / Play
if (mk == MK_CONTROL) {
m_controller.togglePause();
}
return true;
}
}
}
return false;
}
void OutputWindow::mouseLeftButtonDoubleClick(const win32::Point & /*mousePosition*/, DWORD /*keyState*/) {
m_controller.toggleOutputMaximize();
}
void OutputWindow::mouseWheel(int wheelDelta, const Point &mousePosition, DWORD keyState) {
if (0 != (keyState & MK_CONTROL)) {
auto change =
static_cast<float>(wheelDelta) / ((0 != (keyState & MK_SHIFT)) ? 300 * WHEEL_DELTA : 30 * WHEEL_DELTA);
m_controller.zoomOutputAt(mousePosition, change);
}
}
void OutputWindow::displayChange(uint32_t /*bitsPerPixel*/, Dimension) { m_controller.refreshMonitors(); }
void OutputWindow::dpiChanged(const win32::Rect &rect) { m_window.move(rect); }
auto OutputWindow::userMessage(const win32::WindowMessage &message) -> OptLRESULT {
if (message.type == m_taskbarButtons.userMessage()) {
m_taskbarButtons.createButtons(m_window, m_controller.state());
return 0;
}
return {};
}
auto OutputWindow::controlCommand(const win32::ControlCommand &command) -> OptLRESULT {
if (m_taskbarButtons.handleCommand(command.identifier, m_controller)) return 0;
return {};
}
} // namespace deskdup