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
|
#include "DataDeviceWlr.hpp"
#include <algorithm>
#include <hyprutils/os/FileDescriptor.hpp>
#include "../managers/SeatManager.hpp"
#include "../managers/permissions/DynamicPermissionManager.hpp"
#include "core/Seat.hpp"
using namespace Hyprutils::OS;
CWLRDataOffer::CWLRDataOffer(SP<CZwlrDataControlOfferV1> resource_, SP<IDataSource> source_) : m_source(source_), m_resource(resource_) {
if UNLIKELY (!good())
return;
m_resource->setDestroy([this](CZwlrDataControlOfferV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlOfferV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setReceive([this](CZwlrDataControlOfferV1* r, const char* mime, int32_t fd) {
CFileDescriptor sendFd{fd};
if (!m_source) {
LOGM(Log::WARN, "Possible bug: Receive on an offer w/o a source");
return;
}
if (m_dead) {
LOGM(Log::WARN, "Possible bug: Receive on an offer that's dead");
return;
}
const auto CLIENT = r->client();
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(CLIENT, PERMISSION_TYPE_CLIPBOARD_READ);
if (PERM == PERMISSION_RULE_ALLOW_MODE_DENY) {
LOGM(Log::INFO, "Clipboard read denied by permission manager for client {:x}", (uintptr_t)CLIENT);
return;
} else if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING) {
LOGM(Log::INFO, "Clipboard read permission pending for client {:x}", (uintptr_t)CLIENT);
const auto PROMISE = g_pDynamicPermissionManager->promiseFor(CLIENT, PERMISSION_TYPE_CLIPBOARD_READ);
if (!PROMISE) {
LOGM(Log::ERR, "BUG THIS: No promise for client permission for clipboard read");
return;
}
auto sharedFd = std::make_shared<CFileDescriptor>(std::move(sendFd));
PROMISE->then([this, mime = std::string(mime ? mime : ""), sharedFd](SP<CPromiseResult<eDynamicPermissionAllowMode>> pr) {
if (pr->hasError()) {
LOGM(Log::ERR, "BUG THIS: No permission returned for clipboard read");
return;
}
if (pr->result() != PERMISSION_RULE_ALLOW_MODE_ALLOW) {
LOGM(Log::INFO, "Clipboard read denied by user");
return;
}
if (!m_source) {
LOGM(Log::WARN, "Source disappeared while waiting for permission");
return;
}
LOGM(Log::DEBUG, "Offer {:x} asks to send data from source {:x} (permission granted)", (uintptr_t)this, (uintptr_t)m_source.get());
m_source->send(mime, std::move(*sharedFd));
});
return;
}
LOGM(Log::DEBUG, "Offer {:x} asks to send data from source {:x}", (uintptr_t)this, (uintptr_t)m_source.get());
m_source->send(mime, std::move(sendFd));
});
}
bool CWLRDataOffer::good() {
return m_resource->resource();
}
void CWLRDataOffer::sendData() {
if UNLIKELY (!m_source)
return;
for (auto const& m : m_source->mimes()) {
m_resource->sendOffer(m.c_str());
}
}
CWLRDataSource::CWLRDataSource(SP<CZwlrDataControlSourceV1> resource_, SP<CWLRDataDevice> device_) : m_device(device_), m_resource(resource_) {
if UNLIKELY (!good())
return;
m_resource->setData(this);
m_resource->setDestroy([this](CZwlrDataControlSourceV1* r) {
m_events.destroy.emit();
PROTO::dataWlr->destroyResource(this);
});
m_resource->setOnDestroy([this](CZwlrDataControlSourceV1* r) {
m_events.destroy.emit();
PROTO::dataWlr->destroyResource(this);
});
m_resource->setOffer([this](CZwlrDataControlSourceV1* r, const char* mime) { m_mimeTypes.emplace_back(mime); });
}
CWLRDataSource::~CWLRDataSource() {
m_events.destroy.emit();
}
SP<CWLRDataSource> CWLRDataSource::fromResource(wl_resource* res) {
auto data = sc<CWLRDataSource*>(sc<CZwlrDataControlSourceV1*>(wl_resource_get_user_data(res))->data());
return data ? data->m_self.lock() : nullptr;
}
bool CWLRDataSource::good() {
return m_resource->resource();
}
std::vector<std::string> CWLRDataSource::mimes() {
return m_mimeTypes;
}
void CWLRDataSource::send(const std::string& mime, CFileDescriptor fd) {
if (std::ranges::find(m_mimeTypes, mime) == m_mimeTypes.end()) {
LOGM(Log::ERR, "Compositor/App bug: CWLRDataSource::sendAskSend with non-existent mime");
return;
}
m_resource->sendSend(mime.c_str(), fd.get());
}
void CWLRDataSource::accepted(const std::string& mime) {
if (std::ranges::find(m_mimeTypes, mime) == m_mimeTypes.end())
LOGM(Log::ERR, "Compositor/App bug: CWLRDataSource::sendAccepted with non-existent mime");
// wlr has no accepted
}
void CWLRDataSource::cancelled() {
m_resource->sendCancelled();
}
void CWLRDataSource::error(uint32_t code, const std::string& msg) {
m_resource->error(code, msg);
}
CWLRDataDevice::CWLRDataDevice(SP<CZwlrDataControlDeviceV1> resource_) : m_resource(resource_) {
if UNLIKELY (!good())
return;
m_client = m_resource->client();
m_resource->setDestroy([this](CZwlrDataControlDeviceV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlDeviceV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setSetSelection([](CZwlrDataControlDeviceV1* r, wl_resource* sourceR) {
auto source = sourceR ? CWLRDataSource::fromResource(sourceR) : CSharedPointer<CWLRDataSource>{};
if (!source) {
LOGM(Log::DEBUG, "wlr reset selection received");
g_pSeatManager->setCurrentSelection(nullptr);
return;
}
const auto CLIENT = r->client();
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(CLIENT, PERMISSION_TYPE_CLIPBOARD_WRITE);
if (PERM == PERMISSION_RULE_ALLOW_MODE_DENY) {
LOGM(Log::INFO, "Clipboard write denied by permission manager for client {:x}", (uintptr_t)CLIENT);
return;
} else if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING) {
LOGM(Log::INFO, "Clipboard write permission pending for client {:x}", (uintptr_t)CLIENT);
const auto PROMISE = g_pDynamicPermissionManager->promiseFor(CLIENT, PERMISSION_TYPE_CLIPBOARD_WRITE);
if (!PROMISE) {
LOGM(Log::ERR, "BUG THIS: No promise for client permission for clipboard write");
return;
}
PROMISE->then([source](SP<CPromiseResult<eDynamicPermissionAllowMode>> pr) mutable {
if (pr->hasError()) {
LOGM(Log::ERR, "BUG THIS: No permission returned for clipboard write");
return;
}
if (pr->result() != PERMISSION_RULE_ALLOW_MODE_ALLOW) {
LOGM(Log::INFO, "Clipboard write denied by user");
return;
}
if (!source) {
LOGM(Log::WARN, "Source disappeared while waiting for permission");
return;
}
if (source && source->used())
LOGM(Log::WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(Log::DEBUG, "wlr manager requests selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentSelection(source);
});
return;
}
if (source && source->used())
LOGM(Log::WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(Log::DEBUG, "wlr manager requests selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentSelection(source);
});
m_resource->setSetPrimarySelection([](CZwlrDataControlDeviceV1* r, wl_resource* sourceR) {
auto source = sourceR ? CWLRDataSource::fromResource(sourceR) : CSharedPointer<CWLRDataSource>{};
if (!source) {
LOGM(Log::DEBUG, "wlr reset primary selection received");
g_pSeatManager->setCurrentPrimarySelection(nullptr);
return;
}
const auto CLIENT = r->client();
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(CLIENT, PERMISSION_TYPE_CLIPBOARD_WRITE);
if (PERM == PERMISSION_RULE_ALLOW_MODE_DENY) {
LOGM(Log::INFO, "Clipboard write denied by permission manager for client {:x}", (uintptr_t)CLIENT);
return;
} else if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING) {
LOGM(Log::INFO, "Clipboard write permission pending for client {:x}", (uintptr_t)CLIENT);
const auto PROMISE = g_pDynamicPermissionManager->promiseFor(CLIENT, PERMISSION_TYPE_CLIPBOARD_WRITE);
if (!PROMISE) {
LOGM(Log::ERR, "BUG THIS: No promise for client permission for clipboard write");
return;
}
PROMISE->then([source](SP<CPromiseResult<eDynamicPermissionAllowMode>> pr) mutable {
if (pr->hasError()) {
LOGM(Log::ERR, "BUG THIS: No permission returned for clipboard write");
return;
}
if (pr->result() != PERMISSION_RULE_ALLOW_MODE_ALLOW) {
LOGM(Log::INFO, "Clipboard write denied by user");
return;
}
if (!source) {
LOGM(Log::WARN, "Source disappeared while waiting for permission");
return;
}
if (source && source->used())
LOGM(Log::WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(Log::DEBUG, "wlr manager requests primary selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentPrimarySelection(source);
});
return;
}
if (source && source->used())
LOGM(Log::WARN, "setSelection on a used resource. By protocol, this is a violation, but firefox et al insist on doing this.");
source->markUsed();
LOGM(Log::DEBUG, "wlr manager requests primary selection to {:x}", (uintptr_t)source.get());
g_pSeatManager->setCurrentPrimarySelection(source);
});
}
bool CWLRDataDevice::good() {
return m_resource->resource();
}
wl_client* CWLRDataDevice::client() {
return m_client;
}
void CWLRDataDevice::sendInitialSelections() {
PROTO::dataWlr->sendSelectionToDevice(self.lock(), g_pSeatManager->m_selection.currentSelection.lock(), false);
PROTO::dataWlr->sendSelectionToDevice(self.lock(), g_pSeatManager->m_selection.currentPrimarySelection.lock(), true);
}
void CWLRDataDevice::sendDataOffer(SP<CWLRDataOffer> offer) {
m_resource->sendDataOffer(offer->m_resource.get());
}
void CWLRDataDevice::sendSelection(SP<CWLRDataOffer> selection) {
m_resource->sendSelection(selection->m_resource.get());
}
void CWLRDataDevice::sendPrimarySelection(SP<CWLRDataOffer> selection) {
m_resource->sendPrimarySelection(selection->m_resource.get());
}
CWLRDataControlManagerResource::CWLRDataControlManagerResource(SP<CZwlrDataControlManagerV1> resource_) : m_resource(resource_) {
if UNLIKELY (!good())
return;
m_resource->setDestroy([this](CZwlrDataControlManagerV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setOnDestroy([this](CZwlrDataControlManagerV1* r) { PROTO::dataWlr->destroyResource(this); });
m_resource->setGetDataDevice([this](CZwlrDataControlManagerV1* r, uint32_t id, wl_resource* seat) {
const auto RESOURCE = PROTO::dataWlr->m_devices.emplace_back(makeShared<CWLRDataDevice>(makeShared<CZwlrDataControlDeviceV1>(r->client(), r->version(), id)));
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::dataWlr->m_devices.pop_back();
return;
}
RESOURCE->self = RESOURCE;
m_device = RESOURCE;
for (auto const& s : m_sources) {
if (!s)
continue;
s->m_device = RESOURCE;
}
RESOURCE->sendInitialSelections();
LOGM(Log::DEBUG, "New wlr data device bound at {:x}", (uintptr_t)RESOURCE.get());
});
m_resource->setCreateDataSource([this](CZwlrDataControlManagerV1* r, uint32_t id) {
std::erase_if(m_sources, [](const auto& e) { return e.expired(); });
const auto RESOURCE =
PROTO::dataWlr->m_sources.emplace_back(makeShared<CWLRDataSource>(makeShared<CZwlrDataControlSourceV1>(r->client(), r->version(), id), m_device.lock()));
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::dataWlr->m_sources.pop_back();
return;
}
if (!m_device)
LOGM(Log::WARN, "New data source before a device was created");
RESOURCE->m_self = RESOURCE;
m_sources.emplace_back(RESOURCE);
LOGM(Log::DEBUG, "New wlr data source bound at {:x}", (uintptr_t)RESOURCE.get());
});
}
bool CWLRDataControlManagerResource::good() {
return m_resource->resource();
}
CDataDeviceWLRProtocol::CDataDeviceWLRProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
;
}
void CDataDeviceWLRProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_managers.emplace_back(makeShared<CWLRDataControlManagerResource>(makeShared<CZwlrDataControlManagerV1>(client, ver, id)));
if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_managers.pop_back();
return;
}
LOGM(Log::DEBUG, "New wlr_data_control_manager at {:x}", (uintptr_t)RESOURCE.get());
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataControlManagerResource* resource) {
std::erase_if(m_managers, [&](const auto& other) { return other.get() == resource; });
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataSource* resource) {
std::erase_if(m_sources, [&](const auto& other) { return other.get() == resource; });
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataDevice* resource) {
std::erase_if(m_devices, [&](const auto& other) { return other.get() == resource; });
}
void CDataDeviceWLRProtocol::destroyResource(CWLRDataOffer* resource) {
std::erase_if(m_offers, [&](const auto& other) { return other.get() == resource; });
}
void CDataDeviceWLRProtocol::sendSelectionToDevice(SP<CWLRDataDevice> dev, SP<IDataSource> sel, bool primary) {
if (!sel) {
if (primary)
dev->m_resource->sendPrimarySelectionRaw(nullptr);
else
dev->m_resource->sendSelectionRaw(nullptr);
return;
}
const auto OFFER = m_offers.emplace_back(makeShared<CWLRDataOffer>(makeShared<CZwlrDataControlOfferV1>(dev->m_resource->client(), dev->m_resource->version(), 0), sel));
if (!OFFER->good()) {
dev->m_resource->noMemory();
m_offers.pop_back();
return;
}
OFFER->m_primary = primary;
LOGM(Log::DEBUG, "New {}offer {:x} for data source {:x}", primary ? "primary " : " ", (uintptr_t)OFFER.get(), (uintptr_t)sel.get());
dev->sendDataOffer(OFFER);
OFFER->sendData();
if (primary)
dev->sendPrimarySelection(OFFER);
else
dev->sendSelection(OFFER);
}
void CDataDeviceWLRProtocol::setSelection(SP<IDataSource> source, bool primary) {
for (auto const& o : m_offers) {
if (o->m_source && o->m_source->hasDnd())
continue;
if (o->m_primary != primary)
continue;
o->m_dead = true;
}
if (!source) {
LOGM(Log::DEBUG, "resetting {}selection", primary ? "primary " : " ");
for (auto const& d : m_devices) {
sendSelectionToDevice(d, nullptr, primary);
}
return;
}
LOGM(Log::DEBUG, "New {}selection for data source {:x}", primary ? "primary" : "", (uintptr_t)source.get());
for (auto const& d : m_devices) {
sendSelectionToDevice(d, source, primary);
}
}
SP<CWLRDataDevice> CDataDeviceWLRProtocol::dataDeviceForClient(wl_client* c) {
auto it = std::ranges::find_if(m_devices, [c](const auto& e) { return e->client() == c; });
if (it == m_devices.end())
return nullptr;
return *it;
}
|