-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathwayland.cpp
More file actions
566 lines (467 loc) · 16.3 KB
/
Copy pathwayland.cpp
File metadata and controls
566 lines (467 loc) · 16.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/**
* @file src/platform/linux/wayland.cpp
* @brief Definitions for Wayland capture.
*/
// standard includes
#include <cstdlib>
// platform includes
#include <drm_fourcc.h>
#include <fcntl.h>
#include <gbm.h>
#include <poll.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wayland-util.h>
#include <xf86drm.h>
// local includes
#include "graphics.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/round_robin.h"
#include "src/utility.h"
#include "wayland.h"
extern const wl_interface wl_output_interface;
using namespace std::literals;
// Disable warning for converting incompatible functions
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wpmf-conversions"
namespace wl {
// Helper to call C++ method from wayland C callback
template<class T, class Method, Method m, class... Params>
static auto classCall(void *data, Params... params) -> decltype(((*reinterpret_cast<T *>(data)).*m)(params...)) {
return ((*reinterpret_cast<T *>(data)).*m)(params...);
}
#define CLASS_CALL(c, m) classCall<c, decltype(&c::m), &c::m>
// Define buffer params listener
static const struct zwp_linux_buffer_params_v1_listener params_listener = {
.created = dmabuf_t::buffer_params_created,
.failed = dmabuf_t::buffer_params_failed
};
int display_t::init(const char *display_name) {
if (!display_name) {
if (std::string v; platf::get_env("WAYLAND_DISPLAY", v)) {
display_name = v.c_str();
}
}
if (!display_name) {
BOOST_LOG(error) << "[wayland] Environment variable WAYLAND_DISPLAY has not been defined"sv;
return -1;
}
display_internal.reset(wl_display_connect(display_name));
if (!display_internal) {
BOOST_LOG(error) << "[wayland] Couldn't connect to Wayland display: "sv << display_name;
return -1;
}
BOOST_LOG(info) << "[wayland] Found display ["sv << display_name << ']';
return 0;
}
void display_t::roundtrip() {
wl_display_roundtrip(display_internal.get());
}
/**
* @brief Waits up to the specified timeout to dispatch new events on the wl_display.
* @param timeout The timeout in milliseconds.
* @return `true` if new events were dispatched or `false` if the timeout expired.
*/
bool display_t::dispatch(std::chrono::milliseconds timeout) {
// Check if any events are queued already. If not, flush
// outgoing events, and prepare to wait for readability.
if (wl_display_prepare_read(display_internal.get()) == 0) {
wl_display_flush(display_internal.get());
// Wait for an event to come in
struct pollfd pfd = {};
pfd.fd = wl_display_get_fd(display_internal.get());
pfd.events = POLLIN;
if (poll(&pfd, 1, timeout.count()) == 1 && (pfd.revents & POLLIN)) {
// Read the new event(s)
wl_display_read_events(display_internal.get());
} else {
// We timed out, so unlock the queue now
wl_display_cancel_read(display_internal.get());
return false;
}
}
// Dispatch any existing or new pending events
wl_display_dispatch_pending(display_internal.get());
return true;
}
wl_registry *display_t::registry() {
return wl_display_get_registry(display_internal.get());
}
inline monitor_t::monitor_t(wl_output *output):
output {output},
wl_listener {
&CLASS_CALL(monitor_t, wl_geometry),
&CLASS_CALL(monitor_t, wl_mode),
&CLASS_CALL(monitor_t, wl_done),
&CLASS_CALL(monitor_t, wl_scale),
},
xdg_listener {
&CLASS_CALL(monitor_t, xdg_position),
&CLASS_CALL(monitor_t, xdg_size),
&CLASS_CALL(monitor_t, xdg_done),
&CLASS_CALL(monitor_t, xdg_name),
&CLASS_CALL(monitor_t, xdg_description)
} {
}
inline void monitor_t::xdg_name(zxdg_output_v1 *, const char *name) {
this->name = name;
BOOST_LOG(info) << "[wayland] Name: "sv << this->name;
}
void monitor_t::xdg_description(zxdg_output_v1 *, const char *description) {
this->description = description;
BOOST_LOG(info) << "[wayland] Found monitor: "sv << this->description;
}
void monitor_t::xdg_position(zxdg_output_v1 *, std::int32_t x, std::int32_t y) {
viewport.offset_x = x;
viewport.offset_y = y;
BOOST_LOG(info) << "[wayland] Offset: "sv << x << 'x' << y;
}
void monitor_t::xdg_size(zxdg_output_v1 *, std::int32_t width, std::int32_t height) {
viewport.logical_width = width;
viewport.logical_height = height;
BOOST_LOG(info) << "[wayland] Logical size: "sv << width << 'x' << height;
}
void monitor_t::wl_mode(
wl_output *wl_output,
std::uint32_t flags,
std::int32_t width,
std::int32_t height,
std::int32_t refresh
) {
viewport.width = width;
viewport.height = height;
BOOST_LOG(info) << "[wayland] Resolution: "sv << width << 'x' << height;
}
void monitor_t::listen(zxdg_output_manager_v1 *output_manager) {
auto xdg_output = zxdg_output_manager_v1_get_xdg_output(output_manager, output);
zxdg_output_v1_add_listener(xdg_output, &xdg_listener, this);
wl_output_add_listener(output, &wl_listener, this);
}
interface_t::interface_t() noexcept
:
screencopy_manager {nullptr},
dmabuf_interface {nullptr},
output_manager {nullptr},
listener {
&CLASS_CALL(interface_t, add_interface),
&CLASS_CALL(interface_t, del_interface)
} {
}
void interface_t::listen(wl_registry *registry) {
wl_registry_add_listener(registry, &listener, this);
}
void interface_t::add_interface(
wl_registry *registry,
std::uint32_t id,
const char *interface,
std::uint32_t version
) {
BOOST_LOG(debug) << "[wayland] Available interface: "sv << interface << '(' << id << ") version "sv << version;
if (!std::strcmp(interface, wl_output_interface.name)) {
BOOST_LOG(info) << "[wayland] Found interface: "sv << interface << '(' << id << ") version "sv << version;
monitors.emplace_back(
std::make_unique<monitor_t>(
(wl_output *) wl_registry_bind(registry, id, &wl_output_interface, 2)
)
);
} else if (!std::strcmp(interface, zxdg_output_manager_v1_interface.name)) {
BOOST_LOG(info) << "[wayland] Found interface: "sv << interface << '(' << id << ") version "sv << version;
output_manager = (zxdg_output_manager_v1 *) wl_registry_bind(registry, id, &zxdg_output_manager_v1_interface, version);
this->interface[XDG_OUTPUT] = true;
} else if (!std::strcmp(interface, zwlr_screencopy_manager_v1_interface.name)) {
BOOST_LOG(info) << "[wayland] Found interface: "sv << interface << '(' << id << ") version "sv << version;
screencopy_manager = (zwlr_screencopy_manager_v1 *) wl_registry_bind(registry, id, &zwlr_screencopy_manager_v1_interface, version);
this->interface[WLR_EXPORT_DMABUF] = true;
} else if (!std::strcmp(interface, zwp_linux_dmabuf_v1_interface.name)) {
BOOST_LOG(info) << "[wayland] Found interface: "sv << interface << '(' << id << ") version "sv << version;
dmabuf_interface = (zwp_linux_dmabuf_v1 *) wl_registry_bind(registry, id, &zwp_linux_dmabuf_v1_interface, version);
this->interface[LINUX_DMABUF] = true;
}
}
void interface_t::del_interface(wl_registry *registry, uint32_t id) {
BOOST_LOG(info) << "[wayland] Delete: "sv << id;
}
// Initialize GBM
bool dmabuf_t::init_gbm() {
if (gbm_device) {
return true;
}
auto render_path = platf::resolve_render_device();
int drm_fd = open(render_path.c_str(), O_RDWR);
if (drm_fd < 0) {
BOOST_LOG(error) << "[wayland] Failed to open DRM render node: "sv << render_path;
return false;
}
gbm_device = gbm_create_device(drm_fd);
if (!gbm_device) {
close(drm_fd);
BOOST_LOG(error) << "[wayland] Failed to create GBM device"sv;
return false;
}
return true;
}
// Cleanup GBM
void dmabuf_t::cleanup_gbm() {
if (current_bo) {
gbm_bo_destroy(current_bo);
current_bo = nullptr;
}
if (current_wl_buffer) {
wl_buffer_destroy(current_wl_buffer);
current_wl_buffer = nullptr;
}
}
dmabuf_t::dmabuf_t():
status {READY},
frames {},
current_frame {&frames[0]},
listener {
&CLASS_CALL(dmabuf_t, buffer),
&CLASS_CALL(dmabuf_t, flags),
&CLASS_CALL(dmabuf_t, ready),
&CLASS_CALL(dmabuf_t, failed),
&CLASS_CALL(dmabuf_t, damage),
&CLASS_CALL(dmabuf_t, linux_dmabuf),
&CLASS_CALL(dmabuf_t, buffer_done),
} {
}
// Start capture
void dmabuf_t::listen(
zwlr_screencopy_manager_v1 *screencopy_manager,
zwp_linux_dmabuf_v1 *dmabuf_interface,
wl_output *output,
bool blend_cursor
) {
this->dmabuf_interface = dmabuf_interface;
// Reset state
shm_info.supported = false;
dmabuf_info.supported = false;
// Create new frame
auto frame = zwlr_screencopy_manager_v1_capture_output(
screencopy_manager,
blend_cursor ? 1 : 0,
output
);
// Store frame data pointer for callbacks
zwlr_screencopy_frame_v1_set_user_data(frame, this);
// Add listener
zwlr_screencopy_frame_v1_add_listener(frame, &listener, this);
status = WAITING;
}
dmabuf_t::~dmabuf_t() {
cleanup_gbm();
for (auto &frame : frames) {
frame.destroy();
}
if (gbm_device) {
// We should close the DRM FD, but it's owned by GBM
gbm_device_destroy(gbm_device);
gbm_device = nullptr;
}
}
// Buffer format callback
void dmabuf_t::buffer(
zwlr_screencopy_frame_v1 *frame,
uint32_t format,
uint32_t width,
uint32_t height,
uint32_t stride
) {
shm_info.supported = true;
shm_info.format = format;
shm_info.width = width;
shm_info.height = height;
shm_info.stride = stride;
}
// DMA-BUF format callback
void dmabuf_t::linux_dmabuf(
zwlr_screencopy_frame_v1 *frame,
std::uint32_t format,
std::uint32_t width,
std::uint32_t height
) {
dmabuf_info.supported = true;
dmabuf_info.format = format;
dmabuf_info.width = width;
dmabuf_info.height = height;
}
// Flags callback
void dmabuf_t::flags(zwlr_screencopy_frame_v1 *frame, std::uint32_t flags) {
y_invert = flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT;
BOOST_LOG(verbose) << "Frame flags: "sv << flags << (y_invert ? " (y_invert)" : "");
}
// DMA-BUF creation helper
void dmabuf_t::create_and_copy_dmabuf(zwlr_screencopy_frame_v1 *frame) {
if (!init_gbm()) {
BOOST_LOG(error) << "Failed to initialize GBM"sv;
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
return;
}
// Create GBM buffer
current_bo = gbm_bo_create(gbm_device, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, GBM_BO_USE_RENDERING);
if (!current_bo) {
BOOST_LOG(error) << "Failed to create GBM buffer"sv;
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
return;
}
// Get buffer info
int fd = gbm_bo_get_fd(current_bo);
if (fd < 0) {
BOOST_LOG(error) << "Failed to get buffer FD"sv;
gbm_bo_destroy(current_bo);
current_bo = nullptr;
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
return;
}
uint32_t stride = gbm_bo_get_stride(current_bo);
uint64_t modifier = gbm_bo_get_modifier(current_bo);
// Store in surface descriptor for later use
auto next_frame = get_next_frame();
next_frame->sd.fds[0] = fd;
next_frame->sd.pitches[0] = stride;
next_frame->sd.offsets[0] = 0;
next_frame->sd.modifier = modifier;
// Create linux-dmabuf buffer
auto params = zwp_linux_dmabuf_v1_create_params(dmabuf_interface);
zwp_linux_buffer_params_v1_add(params, fd, 0, 0, stride, modifier >> 32, modifier & 0xffffffff);
// Add listener for buffer creation
zwp_linux_buffer_params_v1_add_listener(params, ¶ms_listener, frame);
// Create Wayland buffer (async - callback will handle copy)
zwp_linux_buffer_params_v1_create(params, dmabuf_info.width, dmabuf_info.height, dmabuf_info.format, 0);
}
// Buffer done callback - time to create buffer
void dmabuf_t::buffer_done(zwlr_screencopy_frame_v1 *frame) {
auto next_frame = get_next_frame();
// Prefer DMA-BUF if supported
if (dmabuf_info.supported && dmabuf_interface) {
// Store format info first
next_frame->sd.fourcc = dmabuf_info.format;
next_frame->sd.width = dmabuf_info.width;
next_frame->sd.height = dmabuf_info.height;
// Create and start copy
create_and_copy_dmabuf(frame);
} else if (shm_info.supported) {
// SHM fallback would go here
BOOST_LOG(warning) << "[wayland] SHM capture not implemented"sv;
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
} else {
BOOST_LOG(error) << "[wayland] No supported buffer types"sv;
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
}
}
// Buffer params created callback
void dmabuf_t::buffer_params_created(
void *data,
struct zwp_linux_buffer_params_v1 *params,
struct wl_buffer *buffer
) {
auto frame = static_cast<zwlr_screencopy_frame_v1 *>(data);
auto self = static_cast<dmabuf_t *>(zwlr_screencopy_frame_v1_get_user_data(frame));
// Store for cleanup
self->current_wl_buffer = buffer;
// Start the actual copy
zwp_linux_buffer_params_v1_destroy(params);
zwlr_screencopy_frame_v1_copy(frame, buffer);
}
// Buffer params failed callback
void dmabuf_t::buffer_params_failed(
void *data,
struct zwp_linux_buffer_params_v1 *params
) {
auto frame = static_cast<zwlr_screencopy_frame_v1 *>(data);
auto self = static_cast<dmabuf_t *>(zwlr_screencopy_frame_v1_get_user_data(frame));
BOOST_LOG(error) << "[wayland] Failed to create buffer from params"sv;
self->cleanup_gbm();
zwp_linux_buffer_params_v1_destroy(params);
zwlr_screencopy_frame_v1_destroy(frame);
self->status = REINIT;
}
// Ready callback
void dmabuf_t::ready(
zwlr_screencopy_frame_v1 *frame,
std::uint32_t tv_sec_hi,
std::uint32_t tv_sec_lo,
std::uint32_t tv_nsec
) {
// Frame is ready for use, GBM buffer now contains screen content
current_frame->destroy();
current_frame = get_next_frame();
std::uint64_t sec = (std::uint64_t(tv_sec_hi) << 32) | tv_sec_lo;
auto ready_ts = std::chrono::seconds(sec) + std::chrono::nanoseconds(tv_nsec);
current_frame->frame_timestamp = std::chrono::steady_clock::time_point {
std::chrono::duration_cast<std::chrono::steady_clock::duration>(ready_ts)
};
// Keep the GBM buffer alive but destroy the Wayland objects
if (current_wl_buffer) {
wl_buffer_destroy(current_wl_buffer);
current_wl_buffer = nullptr;
}
cleanup_gbm();
zwlr_screencopy_frame_v1_destroy(frame);
status = READY;
}
// Failed callback
void dmabuf_t::failed(zwlr_screencopy_frame_v1 *frame) {
BOOST_LOG(error) << "[wayland] Frame capture failed"sv;
// Clean up resources
cleanup_gbm();
auto next_frame = get_next_frame();
next_frame->destroy();
zwlr_screencopy_frame_v1_destroy(frame);
status = REINIT;
}
// Only called if using zwlr_screencopy_frame_v1_copy_with_damage()
void dmabuf_t::damage(
zwlr_screencopy_frame_v1 *frame,
std::uint32_t x,
std::uint32_t y,
std::uint32_t width,
std::uint32_t height
) {};
void frame_t::destroy() {
for (auto x = 0; x < 4; ++x) {
if (sd.fds[x] >= 0) {
close(sd.fds[x]);
sd.fds[x] = -1;
}
}
}
frame_t::frame_t() {
// File descriptors aren't open
std::fill_n(sd.fds, 4, -1);
};
std::vector<std::unique_ptr<monitor_t>> monitors(const char *display_name) {
display_t display;
if (display.init(display_name)) {
return {};
}
interface_t interface;
interface.listen(display.registry());
display.roundtrip();
if (!interface[interface_t::XDG_OUTPUT]) {
BOOST_LOG(error) << "[wayland] Missing Wayland wire XDG_OUTPUT"sv;
return {};
}
for (auto &monitor : interface.monitors) {
monitor->listen(interface.output_manager);
}
display.roundtrip();
return std::move(interface.monitors);
}
static bool validate() {
display_t display;
return display.init() == 0;
}
int init() {
static bool validated = validate();
return !validated;
}
} // namespace wl
#pragma GCC diagnostic pop