-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManageWindow.cpp
262 lines (253 loc) · 7.21 KB
/
ManageWindow.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
#include "pch.h"
#define LIBWSACONTROLLER_USEATLIMAGE
#include "libWSAController.h"
#include "ManageWindow.h"
#include "CallbackLogger.h"
#include "VirtualTouch.h"
namespace SuperWindow
{
struct WndInfo
{
HWND wsa, redir;
RECT init_pos;
tstring pkgName, title;
bool blocked;
clock_t last_time;
};
std::jthread m_mainLoop;
static std::map<WindowID, WndInfo> m_allWnds;
static std::map<HWND, WindowID> m_red2init;
std::map<WORD, UINT64> m_pid2tid;
static size_t windowIDPool = 0;
static HINSTANCE hInstance = GetModuleHandle(NULL);
inline WindowID MakeWindowID() { return windowIDPool++; }
static int x_start = SHRT_MIN, moved_count = 0;
libWSAController::IWSAController* ictrl;
// ¼ÙÉèWSA´°¿Ú×Ü´óС½ÏС
/*
static class PlanningModule
{
private:
struct {
int top, right, bottom, left;
} m_thickness = {};
struct {
int top, right, bottom, left;
} m_space = { 65536, 65536, 65536, 65536 };
struct {
int top, right, bottom, left;
} m_start = {};
struct {
std::vector<std::pair<bool, int>> top, right, bottom, left;
} m_used;
public:
bool Emplace(WindowID id)
{
const RECT& pos = m_allWnds[id].init_pos;
int height = pos.bottom - pos.top, width = pos.right - pos.left;
if (height <= m_thickness.top)
{
if (width <= m_space.top)
{
SetWindowPos(m_allWnds[id].wsa, NULL, m_start.top, 0, width, height, SWP_NOZORDER);
m_space.top -= width, m_start.top += width;
m_used.top.push_back(std::make_pair(true, width));
return true;
}
for (auto& i : m_used.top)
{
}
}
}
} m_planning;
*/
LRESULT CALLBACK RedirectedWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WndInfo* pInfo = (WndInfo*)GetWindowLongPtr(hWnd, -21);
if (message == WM_DESTROY)
{
MoveBack(m_red2init[hWnd]);
PostQuitMessage(0);
}
if (pInfo == nullptr)
return DefWindowProc(hWnd, message, wParam, lParam);
if (message >= 0x200 && message < 0x2a3 && pInfo->blocked) return 0;
POINTS pts = MAKEPOINTS(lParam);
switch (message)
{
case WM_MOUSEMOVE:
{
if (GetKeyState(VK_LBUTTON) & 0x8000)
{
ictrl->MouseMove(pts.x, pts.y, pInfo->pkgName);
callbackfd("MouseMove");
}
}
break;
case WM_LBUTTONDOWN:
{
ictrl->MouseLeftDown(pts.x, pts.y, pInfo->pkgName);
callbackfd("MouseLeftDown");
}
break;
case WM_LBUTTONUP:
{
ictrl->MouseLeftUp(pts.x, pts.y, pInfo->pkgName);
callbackfd("MouseLeftUp");
}
break;
case WM_POINTERDOWN:
{
UINT64 tid = ictrl->TouchDown(pts.x, pts.y, pInfo->pkgName);
m_pid2tid[GET_POINTERID_WPARAM(wParam)] = tid;
callbackfd("PointerDown");
}
break;
case WM_POINTERUPDATE:
{
ictrl->TouchMove(pts.x, pts.y, m_pid2tid[GET_POINTERID_WPARAM(wParam)], pInfo->pkgName);
callbackfd("PointerUpdate");
}
break;
case WM_POINTERUP:
{
ictrl->TouchUp(pts.x, pts.y, m_pid2tid[GET_POINTERID_WPARAM(wParam)], pInfo->pkgName);
m_pid2tid.erase(GET_POINTERID_WPARAM(wParam));
callbackfd("PointerUp");
}
break;
case WM_MOUSELEAVE:
case WM_POINTERLEAVE:
{
ictrl->InputInterrupt(pInfo->pkgName);
callbackfd("Interrupted input!!!!!!!");
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
static struct {
bool need = false;
WindowID dst = 0;
} m_pipe;
bool InitWith(libWSAController::IWSAController* c) {
ictrl = c;
auto loopFunc = [](std::stop_token token) {
std::set<WindowID> allReds;
MSG msg{};
while (!token.stop_requested()) {
if (m_pipe.need)
{
WndInfo& info = m_allWnds[m_pipe.dst];
WNDCLASS cls = {
.style = CS_HREDRAW | CS_VREDRAW,
.lpfnWndProc = RedirectedWndProc,
.cbClsExtra = 0,
.cbWndExtra = sizeof(LPVOID),
.hInstance = hInstance,
.hIcon = NULL,
#pragma warning(disable:4302)
.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)),
#pragma warning(default:4302)
.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1),
.lpszMenuName = NULL,
.lpszClassName = info.pkgName.c_str()
};
RegisterClass(&cls);
HWND hRed = CreateWindow(info.pkgName.c_str(), info.title.c_str(),
GetWindowLong(info.wsa, GWL_STYLE), 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
SetWindowLongPtr(hRed, -21, (UINT64)&info);
auto x = info.init_pos.left, y = info.init_pos.top;
auto width = info.init_pos.right - x, height = info.init_pos.bottom - y;
x = x < 0 ? 0 : x; x = x > GetSystemMetrics(SM_CXSCREEN) - 10 ? 0 : x;
y = y < 0 ? 0 : y; y = y > GetSystemMetrics(SM_CYSCREEN) - 10 ? 0 : y;
SetWindowPos(hRed, NULL, x, y, width, height, SWP_NOZORDER);
ShowWindow(hRed, SW_SHOW);
UpdateWindow(hRed);
info.redir = hRed;
m_red2init[hRed] = m_pipe.dst;
allReds.insert(m_pipe.dst);
m_pipe.need = false;
continue;
}
// callbackfd(std::format("Try to invalid windows, total: {}", allReds.size()))
for (auto& i : allReds)
{
BOOL got_msg = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (got_msg) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
auto& info = m_allWnds[i];
cv::Mat img = ictrl->PeekOnce(false, info.pkgName);
if (img.empty()) continue;
HBITMAP bmp = CreateBitmap(img.cols, img.rows, 1, 32, img.data);
auto hdc = GetDC(info.redir);
auto hdcBits = CreateCompatibleDC(hdc);
SelectObject(hdcBits, bmp);
BitBlt(hdc, 0, 0, img.cols, img.rows, hdcBits, 0, 0, SRCCOPY);
DeleteDC(hdcBits);
ReleaseDC(info.redir, hdc);
DeleteObject(bmp);
}
std::this_thread::yield();
}
for (auto& i : m_allWnds)
if (!DestroyWindow(i.second.redir))
callbackfe(std::format("DestroyWindow Failed!! GetLastError() = {}", GetLastError()));
};
m_mainLoop = std::jthread(loopFunc);
return m_mainLoop.joinable();
}
std::optional<WindowID> Monitor(HWND hwnd, tstring pkgName, tstring title, bool block)
{
auto result = MakeWindowID();
RECT wnd_rect; GetWindowRect(hwnd, &wnd_rect);
m_allWnds[result] = WndInfo{ hwnd, NULL, wnd_rect, pkgName, title, block, 0 };
return result;
}
bool MoveAway(WindowID id)
{
if (m_allWnds.find(id) == m_allWnds.cend()) {
callbackfe("Not found window id");
return false;
}
if (x_start >= SHRT_MAX - 1) {
callbackfe("Not enough space to move window!");
return false;
}
WndInfo& info = m_allWnds[id];
auto width = info.init_pos.right - info.init_pos.left,
height = info.init_pos.bottom - info.init_pos.top;
SetWindowPos(info.wsa, NULL, x_start, 0, width, height, SWP_NOZORDER);
x_start += width, moved_count++;
return true;
}
bool MoveBack(WindowID id)
{
if (m_allWnds.find(id) == m_allWnds.cend()) {
callbackfe("Not found window id");
return false;
}
WndInfo& info = m_allWnds[id];
auto width = info.init_pos.right - info.init_pos.left,
height = info.init_pos.bottom - info.init_pos.top;
SetWindowPos(info.wsa, NULL, info.init_pos.left, info.init_pos.top, width, height, SWP_NOZORDER);
if (id == windowIDPool - 1) x_start -= width;
if (--moved_count == 0) x_start = 0;
return true;
}
void Redirect(WindowID id)
{
if (m_allWnds.find(id) == m_allWnds.cend()) return;
while (m_pipe.need);
m_pipe.dst = id, m_pipe.need = true;
}
void ReleaseAll()
{
m_mainLoop.request_stop();
if (m_mainLoop.joinable()) m_mainLoop.join();
}
}