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
|
#include "ForeignToplevelWlr.hpp"
#include <algorithm>
#include "../Compositor.hpp"
#include "protocols/core/Output.hpp"
#include "render/Renderer.hpp"
#define LOGM PROTO::foreignToplevelWlr->protoLog
CForeignToplevelHandleWlr::CForeignToplevelHandleWlr(SP<CZwlrForeignToplevelHandleV1> resource_, PHLWINDOW pWindow_) : resource(resource_), pWindow(pWindow_) {
if (!resource_->resource())
return;
resource->setOnDestroy([this](CZwlrForeignToplevelHandleV1* h) { PROTO::foreignToplevelWlr->destroyHandle(this); });
resource->setDestroy([this](CZwlrForeignToplevelHandleV1* h) { PROTO::foreignToplevelWlr->destroyHandle(this); });
resource->setActivate([this](CZwlrForeignToplevelHandleV1* p, wl_resource* seat) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
// these requests bypass the config'd stuff cuz it's usually like
// window switchers and shit
PWINDOW->activate(true);
});
resource->setSetFullscreen([this](CZwlrForeignToplevelHandleV1* p, wl_resource* output) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
if (PWINDOW->m_eSuppressedEvents & SUPPRESS_FULLSCREEN)
return;
if (!PWINDOW->m_bIsMapped) {
PWINDOW->m_bWantsInitialFullscreen = true;
return;
}
if (output) {
const auto wpMonitor = CWLOutputResource::fromResource(output)->monitor;
if (!wpMonitor.expired()) {
const auto monitor = wpMonitor.lock();
if (PWINDOW->m_pWorkspace != monitor->activeWorkspace) {
g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, monitor->activeWorkspace);
g_pCompositor->setActiveMonitor(monitor.get());
}
}
}
g_pCompositor->setWindowFullscreen(PWINDOW, true, FULLSCREEN_FULL);
g_pHyprRenderer->damageWindow(PWINDOW);
});
resource->setUnsetFullscreen([this](CZwlrForeignToplevelHandleV1* p) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
if (PWINDOW->m_eSuppressedEvents & SUPPRESS_FULLSCREEN)
return;
g_pCompositor->setWindowFullscreen(PWINDOW, false);
});
resource->setSetMaximized([this](CZwlrForeignToplevelHandleV1* p) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
if (PWINDOW->m_eSuppressedEvents & SUPPRESS_MAXIMIZE)
return;
if (!PWINDOW->m_bIsMapped) {
PWINDOW->m_bWantsInitialFullscreen = true;
return;
}
g_pCompositor->setWindowFullscreen(PWINDOW, true, FULLSCREEN_MAXIMIZED);
});
resource->setUnsetMaximized([this](CZwlrForeignToplevelHandleV1* p) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
if (PWINDOW->m_eSuppressedEvents & SUPPRESS_MAXIMIZE)
return;
g_pCompositor->setWindowFullscreen(PWINDOW, false);
});
resource->setClose([this](CZwlrForeignToplevelHandleV1* p) {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW)
return;
g_pCompositor->closeWindow(PWINDOW);
});
}
bool CForeignToplevelHandleWlr::good() {
return resource->resource();
}
PHLWINDOW CForeignToplevelHandleWlr::window() {
return pWindow.lock();
}
wl_resource* CForeignToplevelHandleWlr::res() {
return resource->resource();
}
void CForeignToplevelHandleWlr::sendMonitor(CMonitor* pMonitor) {
if (lastMonitorID == (int64_t)pMonitor->ID)
return;
const auto CLIENT = resource->client();
if (const auto PLASTMONITOR = g_pCompositor->getMonitorFromID(lastMonitorID); PLASTMONITOR && PROTO::outputs.contains(PLASTMONITOR->szName)) {
const auto OLDRESOURCE = PROTO::outputs.at(PLASTMONITOR->szName)->outputResourceFrom(CLIENT);
if (OLDRESOURCE)
resource->sendOutputLeave(OLDRESOURCE->getResource()->resource());
}
if (PROTO::outputs.contains(pMonitor->szName)) {
const auto NEWRESOURCE = PROTO::outputs.at(pMonitor->szName)->outputResourceFrom(CLIENT);
if (NEWRESOURCE)
resource->sendOutputEnter(NEWRESOURCE->getResource()->resource());
}
lastMonitorID = pMonitor->ID;
}
void CForeignToplevelHandleWlr::sendState() {
const auto PWINDOW = pWindow.lock();
if (!PWINDOW || !PWINDOW->m_pWorkspace || !PWINDOW->m_bIsMapped)
return;
wl_array state;
wl_array_init(&state);
if (PWINDOW == g_pCompositor->m_pLastWindow) {
auto p = (uint32_t*)wl_array_add(&state, sizeof(uint32_t));
*p = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED;
}
if (PWINDOW->m_bIsFullscreen) {
auto p = (uint32_t*)wl_array_add(&state, sizeof(uint32_t));
if (PWINDOW->m_pWorkspace->m_efFullscreenMode == FULLSCREEN_FULL)
*p = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN;
else
*p = ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED;
}
resource->sendState(&state);
wl_array_release(&state);
}
CForeignToplevelWlrManager::CForeignToplevelWlrManager(SP<CZwlrForeignToplevelManagerV1> resource_) : resource(resource_) {
if (!resource_->resource())
return;
resource->setOnDestroy([this](CZwlrForeignToplevelManagerV1* h) { PROTO::foreignToplevelWlr->onManagerResourceDestroy(this); });
resource->setStop([this](CZwlrForeignToplevelManagerV1* h) {
resource->sendFinished();
finished = true;
LOGM(LOG, "CForeignToplevelWlrManager: finished");
PROTO::foreignToplevelWlr->onManagerResourceDestroy(this);
});
for (auto& w : g_pCompositor->m_vWindows) {
if (!w->m_bIsMapped || w->m_bFadingOut)
continue;
onMap(w);
}
lastFocus = g_pCompositor->m_pLastWindow;
}
void CForeignToplevelWlrManager::onMap(PHLWINDOW pWindow) {
if (finished)
return;
const auto NEWHANDLE = PROTO::foreignToplevelWlr->m_vHandles.emplace_back(
makeShared<CForeignToplevelHandleWlr>(makeShared<CZwlrForeignToplevelHandleV1>(resource->client(), resource->version(), 0), pWindow));
if (!NEWHANDLE->good()) {
LOGM(ERR, "Couldn't create a foreign handle");
resource->noMemory();
PROTO::foreignToplevelWlr->m_vHandles.pop_back();
return;
}
LOGM(LOG, "Newly mapped window {:016x}", (uintptr_t)pWindow.get());
resource->sendToplevel(NEWHANDLE->resource.get());
NEWHANDLE->resource->sendAppId(pWindow->m_szClass.c_str());
NEWHANDLE->resource->sendTitle(pWindow->m_szTitle.c_str());
if (const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID); PMONITOR)
NEWHANDLE->sendMonitor(PMONITOR);
NEWHANDLE->sendState();
NEWHANDLE->resource->sendDone();
handles.push_back(NEWHANDLE);
}
SP<CForeignToplevelHandleWlr> CForeignToplevelWlrManager::handleForWindow(PHLWINDOW pWindow) {
std::erase_if(handles, [](const auto& wp) { return wp.expired(); });
const auto IT = std::find_if(handles.begin(), handles.end(), [pWindow](const auto& h) { return h->window() == pWindow; });
return IT == handles.end() ? SP<CForeignToplevelHandleWlr>{} : IT->lock();
}
void CForeignToplevelWlrManager::onTitle(PHLWINDOW pWindow) {
if (finished)
return;
const auto H = handleForWindow(pWindow);
if (!H || H->closed)
return;
H->resource->sendTitle(pWindow->m_szTitle.c_str());
H->resource->sendDone();
}
void CForeignToplevelWlrManager::onClass(PHLWINDOW pWindow) {
if (finished)
return;
const auto H = handleForWindow(pWindow);
if (!H || H->closed)
return;
H->resource->sendAppId(pWindow->m_szClass.c_str());
H->resource->sendDone();
}
void CForeignToplevelWlrManager::onUnmap(PHLWINDOW pWindow) {
if (finished)
return;
const auto H = handleForWindow(pWindow);
if (!H)
return;
H->resource->sendClosed();
H->resource->sendDone();
H->closed = true;
}
void CForeignToplevelWlrManager::onMoveMonitor(PHLWINDOW pWindow) {
if (finished)
return;
const auto H = handleForWindow(pWindow);
if (!H || H->closed)
return;
const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);
if (!PMONITOR)
return;
H->sendMonitor(PMONITOR);
H->resource->sendDone();
}
void CForeignToplevelWlrManager::onFullscreen(PHLWINDOW pWindow) {
if (finished)
return;
const auto H = handleForWindow(pWindow);
if (!H || H->closed)
return;
H->sendState();
H->resource->sendDone();
}
void CForeignToplevelWlrManager::onNewFocus(PHLWINDOW pWindow) {
if (finished)
return;
if (const auto HOLD = handleForWindow(lastFocus.lock()); HOLD) {
HOLD->sendState();
HOLD->resource->sendDone();
}
lastFocus = pWindow;
const auto H = handleForWindow(pWindow);
if (!H || H->closed)
return;
H->sendState();
H->resource->sendDone();
}
bool CForeignToplevelWlrManager::good() {
return resource->resource();
}
CForeignToplevelWlrProtocol::CForeignToplevelWlrProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
static auto P = g_pHookSystem->hookDynamic("openWindow", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
for (auto& m : m_vManagers) {
m->onMap(PWINDOW);
}
});
static auto P1 = g_pHookSystem->hookDynamic("closeWindow", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
for (auto& m : m_vManagers) {
m->onUnmap(PWINDOW);
}
});
static auto P2 = g_pHookSystem->hookDynamic("windowTitle", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
for (auto& m : m_vManagers) {
m->onTitle(PWINDOW);
}
});
static auto P3 = g_pHookSystem->hookDynamic("activeWindow", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
for (auto& m : m_vManagers) {
m->onNewFocus(PWINDOW);
}
});
static auto P4 = g_pHookSystem->hookDynamic("moveWindow", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(std::any_cast<std::vector<std::any>>(data).at(0));
for (auto& m : m_vManagers) {
m->onMoveMonitor(PWINDOW);
}
});
static auto P5 = g_pHookSystem->hookDynamic("fullscreen", [this](void* self, SCallbackInfo& info, std::any data) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
for (auto& m : m_vManagers) {
m->onFullscreen(PWINDOW);
}
});
}
void CForeignToplevelWlrProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CForeignToplevelWlrManager>(makeShared<CZwlrForeignToplevelManagerV1>(client, ver, id))).get();
if (!RESOURCE->good()) {
LOGM(ERR, "Couldn't create a foreign list");
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
}
}
void CForeignToplevelWlrProtocol::onManagerResourceDestroy(CForeignToplevelWlrManager* mgr) {
std::erase_if(m_vManagers, [&](const auto& other) { return other.get() == mgr; });
}
void CForeignToplevelWlrProtocol::destroyHandle(CForeignToplevelHandleWlr* handle) {
std::erase_if(m_vHandles, [&](const auto& other) { return other.get() == handle; });
}
PHLWINDOW CForeignToplevelWlrProtocol::windowFromHandleResource(wl_resource* res) {
for (auto& h : m_vHandles) {
if (h->res() != res)
continue;
return h->window();
}
return nullptr;
}
|