screen-relay 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/LICENSE +201 -0
  2. package/Makefile +165 -0
  3. package/README.md +112 -1
  4. package/THIRD-PARTY-NOTICES.md +75 -0
  5. package/dist/cli/client.d.ts +28 -0
  6. package/dist/cli/commands.d.ts +52 -0
  7. package/dist/cli/config.d.ts +32 -0
  8. package/dist/cli/format.d.ts +15 -0
  9. package/dist/cli/help.d.ts +5 -0
  10. package/dist/cli/host/common.d.ts +23 -0
  11. package/dist/cli/host/doctor.d.ts +2 -0
  12. package/dist/cli/host/packages.d.ts +13 -0
  13. package/dist/cli/host/session_commands.d.ts +8 -0
  14. package/dist/cli/host/setup.d.ts +22 -0
  15. package/dist/cli/host/state.d.ts +23 -0
  16. package/dist/cli/host/supervisor.d.ts +94 -0
  17. package/dist/cli/host/watcher.d.ts +4 -0
  18. package/dist/cli/host/windows.d.ts +82 -0
  19. package/dist/cli/main.d.ts +3 -0
  20. package/dist/cli/mode.d.ts +14 -0
  21. package/dist/cli/output.d.ts +24 -0
  22. package/dist/cli/parser.d.ts +6 -0
  23. package/dist/cli/relay.mjs +12631 -0
  24. package/dist/cli/skills.d.ts +21 -0
  25. package/dist/cli/theme.d.ts +14 -0
  26. package/dist/protocol/errors.d.ts +155 -0
  27. package/dist/protocol/index.d.ts +119 -0
  28. package/dist/protocol/index.js +418 -0
  29. package/dist/sdk/index.d.ts +78 -0
  30. package/dist/sdk/index.js +834 -0
  31. package/dist/sdk/transport.d.ts +20 -0
  32. package/dist/viewer/index.html +96 -0
  33. package/dist/viewer/viewer.js +1079 -0
  34. package/package.json +47 -7
  35. package/packages.tsv +38 -0
  36. package/skills/core/SKILL.md +171 -0
  37. package/skills/core/references/cli.md +119 -0
  38. package/skills/core/references/config.md +48 -0
  39. package/skills/core/references/macos.md +50 -0
  40. package/skills/core/references/sdk.md +69 -0
  41. package/skills/core/references/troubleshooting.md +33 -0
  42. package/skills/core/references/wayland.md +47 -0
  43. package/skills/screen-relay/SKILL.md +24 -0
  44. package/skills/vercel-sandbox/SKILL.md +68 -0
  45. package/src/engine/backends/linux/keycodes.hpp +35 -0
  46. package/src/engine/backends/linux/wayland/capture.cpp +298 -0
  47. package/src/engine/backends/linux/wayland/input.cpp +559 -0
  48. package/src/engine/backends/linux/wayland/keymap.cpp +118 -0
  49. package/src/engine/backends/linux/wayland/keymap.hpp +54 -0
  50. package/src/engine/backends/linux/wayland/protocols/virtual-keyboard-unstable-v1.xml +112 -0
  51. package/src/engine/backends/linux/wayland/protocols/wlr-screencopy-unstable-v1.xml +234 -0
  52. package/src/engine/backends/linux/wayland/protocols/wlr-virtual-pointer-unstable-v1.xml +152 -0
  53. package/src/engine/backends/linux/wayland/wayland.cpp +166 -0
  54. package/src/engine/backends/linux/wayland/wayland.hpp +60 -0
  55. package/src/engine/backends/linux/x11/capture.cpp +206 -0
  56. package/src/engine/backends/linux/x11/input.cpp +266 -0
  57. package/src/engine/backends/macos/capture.mm +367 -0
  58. package/src/engine/backends/macos/display.hpp +20 -0
  59. package/src/engine/backends/macos/display.mm +84 -0
  60. package/src/engine/backends/macos/input.mm +287 -0
  61. package/src/engine/backends/macos/keys.hpp +34 -0
  62. package/src/engine/backends/windows/capture.cpp +393 -0
  63. package/src/engine/backends/windows/input.cpp +274 -0
  64. package/src/engine/backends/windows/output.cpp +65 -0
  65. package/src/engine/backends/windows/output.hpp +26 -0
  66. package/src/engine/core/capture-bucket.hpp +40 -0
  67. package/src/engine/core/capture-source.hpp +59 -0
  68. package/src/engine/core/capture.cpp +565 -0
  69. package/src/engine/core/capture.hpp +58 -0
  70. package/src/engine/core/color.cpp +176 -0
  71. package/src/engine/core/color.hpp +16 -0
  72. package/src/engine/core/cpu-budget.hpp +196 -0
  73. package/src/engine/core/flow.hpp +317 -0
  74. package/src/engine/core/input.cpp +98 -0
  75. package/src/engine/core/input.hpp +115 -0
  76. package/src/engine/core/wait.cpp +125 -0
  77. package/src/engine/core/wait.hpp +41 -0
  78. package/src/engine/server/backend.cpp +65 -0
  79. package/src/engine/server/backend.hpp +18 -0
  80. package/src/engine/server/config.cpp +257 -0
  81. package/src/engine/server/config.hpp +24 -0
  82. package/src/engine/server/main.cpp +82 -0
  83. package/src/engine/server/metrics.cpp +47 -0
  84. package/src/engine/server/metrics.hpp +10 -0
  85. package/src/engine/server/screenshot.cpp +108 -0
  86. package/src/engine/server/screenshot.hpp +31 -0
  87. package/src/engine/server/server.cpp +535 -0
  88. package/src/engine/server/server.hpp +89 -0
  89. package/src/engine/server/wire.cpp +70 -0
  90. package/src/engine/server/wire.hpp +29 -0
  91. package/index.js +0 -2
@@ -0,0 +1,24 @@
1
+ ---
2
+ name: screen-relay
3
+ description: Stream a desktop and control it with the screen-relay CLI (screenshot, click, type, key). Use when an agent needs to see and operate a GUI on a Linux VM, sandbox or container, or on a Mac or Windows PC.
4
+ hidden: true
5
+ ---
6
+
7
+ # screen-relay
8
+
9
+ ## Start here
10
+
11
+ Load the current workflow from the installed CLI:
12
+
13
+ ```sh
14
+ screen-relay skills get core
15
+ screen-relay skills get core --full
16
+ ```
17
+
18
+ Load specialized guidance when needed:
19
+
20
+ ```sh
21
+ screen-relay skills get vercel-sandbox
22
+ ```
23
+
24
+ List available skills with `screen-relay skills list`.
@@ -0,0 +1,68 @@
1
+ ---
2
+ name: vercel-sandbox
3
+ description: Run and control screen-relay inside a Vercel Sandbox.
4
+ allowed-tools: Bash(screen-relay:*)
5
+ ---
6
+
7
+ # screen-relay in Vercel Sandbox
8
+
9
+ Run these steps in order. The port must be published before the first `up`, so that `--public` can match the sandbox URL.
10
+
11
+ 1. Create the sandbox with port 8080 published:
12
+
13
+ ```sh
14
+ sandbox create --name <name> --publish-port 8080
15
+ ```
16
+
17
+ To publish the port on a sandbox that already exists, run `sandbox config ports <name> -p 8080`. Either command prints an HTTPS URL such as `https://<id>.vercel.run`. Note it.
18
+
19
+ 2. In the sandbox, install the CLI:
20
+
21
+ ```sh
22
+ npm install -g screen-relay
23
+ ```
24
+
25
+ 3. Install the system packages and build the engine:
26
+
27
+ ```sh
28
+ screen-relay install --yes
29
+ ```
30
+
31
+ On a fresh sandbox this can take several minutes. Run it with a long command timeout or in the background, and wait for it to exit. It is safe to repeat.
32
+
33
+ 4. Start the session behind the public URL:
34
+
35
+ ```sh
36
+ screen-relay up -d --public https://<id>.vercel.run
37
+ ```
38
+
39
+ `--public` makes the session listen on `0.0.0.0` and creates a token. `/` must resolve to `https://<id>.vercel.run`, because the engine accepts only the `--public` host. Piped, `up -d` prints the viewer URL with its `#token=` fragment. Treat the whole URL as a secret, and share it only through the user's secret channel.
40
+
41
+ 5. Check the session:
42
+
43
+ ```sh
44
+ screen-relay status --json
45
+ ```
46
+
47
+ Run the commands in the sandbox itself. `sandbox ssh <name>` opens a shell in it.
48
+
49
+ Extend the sandbox lifetime when needed:
50
+
51
+ ```sh
52
+ sandbox extend <name> 1h
53
+ ```
54
+
55
+ ## Control it
56
+
57
+ The default desktop is X11 with XFCE. Start a terminal with `screen-relay run -- xfce4-terminal`, then run the control loop from `screen-relay skills get core`.
58
+
59
+ ## Errors
60
+
61
+ - `unreachable` with `Connection refused.`: the engine is not listening. Run `screen-relay up -d`.
62
+ - `unreachable` with `The host name did not resolve.` or `The TLS certificate was rejected.`: check the edge URL and certificate, then run `screen-relay status --url https://<id>.vercel.run --token-file <FILE>`.
63
+ - `unauthorized`: pass the session's token with `--token-file FILE` or `SCREEN_RELAY_TOKEN_FILE`.
64
+ - `forbidden`: `--public` does not match the URL you reached, or a proxy rewrote `Host` or `Origin`.
65
+ - `busy`: another controller holds input. See One controller in `screen-relay skills get core`.
66
+ - `component_failed` or `timeout` from `up`: read `data.component`, then run `screen-relay logs <component>` and `screen-relay doctor`.
67
+
68
+ Stop the desktop with `screen-relay down` before you stop the sandbox with `sandbox stop <name>`. The full workflow is in `screen-relay skills get core`.
@@ -0,0 +1,35 @@
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ namespace relay {
5
+ constexpr uint8_t letterKeycodes[26] = {30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50,
6
+ 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44};
7
+ constexpr int hidKeycode(uint16_t hid) {
8
+ if (hid >= 4 && hid <= 29) return letterKeycodes[hid - 4];
9
+ if (hid >= 30 && hid <= 39) return 2 + hid - 30;
10
+ if (hid >= 58 && hid <= 67) return 59 + hid - 58;
11
+ if (hid >= 68 && hid <= 69) return 87 + hid - 68;
12
+ if (hid >= 104 && hid <= 115) return 183 + hid - 104;
13
+ switch (hid) {
14
+ case 40: return 28; case 41: return 1; case 42: return 14; case 43: return 15;
15
+ case 44: return 57; case 45: return 12; case 46: return 13; case 47: return 26;
16
+ case 48: return 27; case 49: case 50: return 43; case 51: return 39;
17
+ case 52: return 40; case 53: return 41; case 54: return 51; case 55: return 52;
18
+ case 56: return 53; case 57: return 58; case 70: return 99; case 71: return 70;
19
+ case 72: return 119; case 73: return 110; case 74: return 102; case 75: return 104;
20
+ case 76: return 111; case 77: return 107; case 78: return 109; case 79: return 106;
21
+ case 80: return 105; case 81: return 108; case 82: return 103; case 83: return 69;
22
+ case 84: return 98; case 85: return 55; case 86: return 74; case 87: return 78;
23
+ case 88: return 96; case 89: return 79; case 90: return 80; case 91: return 81;
24
+ case 92: return 75; case 93: return 76; case 94: return 77; case 95: return 71;
25
+ case 96: return 72; case 97: return 73; case 98: return 82; case 99: return 83;
26
+ case 100: return 86; case 101: return 139; case 103: return 117;
27
+ case 224: return 29; case 225: return 42; case 226: return 56; case 227: return 125;
28
+ case 228: return 97; case 229: return 54; case 230: return 100; case 231: return 126;
29
+ default: return 0;
30
+ }
31
+ }
32
+ static_assert(hidKeycode(4) == 30 && hidKeycode(11) == 35 && hidKeycode(39) == 11 && hidKeycode(68) == 87 &&
33
+ hidKeycode(69) == 88 && hidKeycode(70) == 99 && hidKeycode(115) == 194 && hidKeycode(225) == 42 &&
34
+ hidKeycode(3) == 0);
35
+ }
@@ -0,0 +1,298 @@
1
+ #include "wayland.hpp"
2
+ #include "core/capture-source.hpp"
3
+ #include <sys/mman.h>
4
+ #include <poll.h>
5
+ #include <unistd.h>
6
+ #include <algorithm>
7
+ #include <array>
8
+ #include <cerrno>
9
+ #include <chrono>
10
+ #include <cstdint>
11
+ #include <exception>
12
+ #include <limits>
13
+ #include <memory>
14
+ #include <stdexcept>
15
+ #include <string>
16
+ #include <vector>
17
+
18
+ namespace relay::wayland {
19
+ namespace {
20
+ constexpr size_t max_damage_rects = 64;
21
+
22
+ void checkSize(int width, int height) {
23
+ try {
24
+ requireSurfaceSize(width, height);
25
+ } catch (const std::runtime_error& error) {
26
+ throw std::runtime_error(std::string(error.what()) +
27
+ "; create a headless output of a supported size (for example, swaymsg create_output or "
28
+ "hyprctl output create headless <name>) and select it with --output");
29
+ }
30
+ }
31
+
32
+ struct Buffer {
33
+ wl_buffer* proxy = nullptr;
34
+ uint8_t* data = nullptr;
35
+ size_t size = 0;
36
+ uint32_t format = 0;
37
+ int width = 0, height = 0, stride = 0;
38
+
39
+ ~Buffer() {
40
+ if (proxy) wl_buffer_destroy(proxy);
41
+ if (data) munmap(data, size);
42
+ }
43
+
44
+ void init(wl_shm* shm, uint32_t offeredFormat, int offeredWidth, int offeredHeight, int offeredStride) {
45
+ format = offeredFormat; width = offeredWidth; height = offeredHeight; stride = offeredStride;
46
+ size = size_t(stride) * size_t(height);
47
+ FileDescriptor fd = createMemfd("relay-wayland-capture");
48
+ require(fd.get() >= 0, "creating Wayland shared memory failed");
49
+ require(ftruncate(fd.get(), static_cast<off_t>(size)) == 0, "sizing Wayland shared memory failed");
50
+ void* mapping = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0);
51
+ require(mapping != MAP_FAILED, "mapping Wayland shared memory failed");
52
+ data = static_cast<uint8_t*>(mapping);
53
+ std::unique_ptr<wl_shm_pool, decltype(&wl_shm_pool_destroy)> pool(
54
+ wl_shm_create_pool(shm, fd.get(), static_cast<int32_t>(size)), &wl_shm_pool_destroy);
55
+ require(bool(pool), "creating Wayland shared memory pool failed");
56
+ proxy = wl_shm_pool_create_buffer(pool.get(), 0, width, height, stride, format);
57
+ require(proxy, "creating Wayland shared memory buffer failed");
58
+ }
59
+
60
+ void orient(bool inverted) {
61
+ if (inverted) {
62
+ for (int row = 0; row < height / 2; ++row)
63
+ std::swap_ranges(data + size_t(row) * stride, data + size_t(row + 1) * stride,
64
+ data + size_t(height - row - 1) * stride);
65
+ }
66
+ }
67
+
68
+ FrameView view() const { return {data, stride, width, height, PixelFormat::BGRA}; }
69
+ };
70
+
71
+ struct Frame {
72
+ zwlr_screencopy_frame_v1* proxy = nullptr;
73
+ int target = 0;
74
+ bool damageCopy = false, hasShm = false, hasDmabuf = false;
75
+ bool bufferDone = false, copied = false, ready = false, failed = false;
76
+ uint32_t format = 0, width = 0, height = 0, stride = 0, dmabufFormat = 0, flags = 0;
77
+ std::vector<DamageRect> damage;
78
+ std::exception_ptr error;
79
+
80
+ ~Frame() { if (proxy) zwlr_screencopy_frame_v1_destroy(proxy); }
81
+ };
82
+
83
+ void frameBuffer(void* data, zwlr_screencopy_frame_v1*, uint32_t format,
84
+ uint32_t width, uint32_t height, uint32_t stride) {
85
+ auto& frame = *static_cast<Frame*>(data);
86
+ frame.hasShm = true; frame.format = format; frame.width = width; frame.height = height; frame.stride = stride;
87
+ }
88
+ void frameFlags(void* data, zwlr_screencopy_frame_v1*, uint32_t flags) {
89
+ static_cast<Frame*>(data)->flags = flags;
90
+ }
91
+ void frameReady(void* data, zwlr_screencopy_frame_v1*, uint32_t, uint32_t, uint32_t) {
92
+ static_cast<Frame*>(data)->ready = true;
93
+ }
94
+ void frameFailed(void* data, zwlr_screencopy_frame_v1*) {
95
+ static_cast<Frame*>(data)->failed = true;
96
+ }
97
+ void frameDamage(void* data, zwlr_screencopy_frame_v1*, uint32_t x, uint32_t y,
98
+ uint32_t width, uint32_t height) {
99
+ auto& frame = *static_cast<Frame*>(data);
100
+ if (x >= frame.width || y >= frame.height) return;
101
+ width = std::min(width, frame.width - x);
102
+ height = std::min(height, frame.height - y);
103
+ if (!width || !height) return;
104
+ try {
105
+ frame.damage.push_back({static_cast<int>(x), static_cast<int>(y),
106
+ static_cast<int>(width), static_cast<int>(height)});
107
+ if (frame.damage.size() > max_damage_rects)
108
+ frame.damage = {{0, 0, static_cast<int>(frame.width), static_cast<int>(frame.height)}};
109
+ } catch (...) {
110
+ frame.error = std::current_exception();
111
+ }
112
+ }
113
+ void frameDmabuf(void* data, zwlr_screencopy_frame_v1*, uint32_t format, uint32_t, uint32_t) {
114
+ auto& frame = *static_cast<Frame*>(data);
115
+ frame.hasDmabuf = true; frame.dmabufFormat = format;
116
+ }
117
+ void frameBufferDone(void* data, zwlr_screencopy_frame_v1*) {
118
+ static_cast<Frame*>(data)->bufferDone = true;
119
+ }
120
+
121
+ const zwlr_screencopy_frame_v1_listener frameListener = {
122
+ frameBuffer, frameFlags, frameReady, frameFailed, frameDamage, frameDmabuf, frameBufferDone
123
+ };
124
+
125
+ class WaylandCaptureSource final : public CaptureSource {
126
+ Connection connection;
127
+ std::array<std::unique_ptr<Buffer>, 3> buffers;
128
+ std::unique_ptr<Frame> frame;
129
+ std::vector<DamageRect> damage;
130
+ const int width, height;
131
+ int current = -1;
132
+ unsigned failures = 0;
133
+
134
+ void request(int target, bool damageCopy) {
135
+ auto next = std::make_unique<Frame>();
136
+ next->target = target; next->damageCopy = damageCopy;
137
+ next->proxy = zwlr_screencopy_manager_v1_capture_output(connection.screencopyManager, 0, connection.output);
138
+ require(next->proxy, "requesting Wayland screencopy frame failed");
139
+ require(zwlr_screencopy_frame_v1_add_listener(next->proxy, &frameListener, next.get()) == 0,
140
+ "listening for Wayland screencopy frame failed");
141
+ frame = std::move(next);
142
+ }
143
+
144
+ void copy() {
145
+ auto& pending = *frame;
146
+ if (!pending.hasShm || (pending.format != WL_SHM_FORMAT_XRGB8888 && pending.format != WL_SHM_FORMAT_ARGB8888)) {
147
+ const std::string shm = pending.hasShm ? std::to_string(pending.format) : "none";
148
+ const std::string dmabuf = pending.hasDmabuf ? std::to_string(pending.dmabufFormat) : "none";
149
+ throw std::runtime_error("Unsupported Wayland screencopy formats (wl_shm: " + shm +
150
+ ", linux-dmabuf: " + dmabuf + ")");
151
+ }
152
+ if (pending.width > unsigned(std::numeric_limits<int>::max()) ||
153
+ pending.height > unsigned(std::numeric_limits<int>::max()))
154
+ throw std::runtime_error("Unsupported Wayland screencopy dimensions");
155
+ const int frameWidth = static_cast<int>(pending.width), frameHeight = static_cast<int>(pending.height);
156
+ checkSize(frameWidth, frameHeight);
157
+ require(pending.stride >= pending.width * 4 && pending.stride <= pending.width * 8,
158
+ "Unsupported Wayland screencopy stride");
159
+ auto& buffer = buffers[pending.target];
160
+ if (!buffer || buffer->format != pending.format || buffer->stride != static_cast<int>(pending.stride)) {
161
+ auto replacement = std::make_unique<Buffer>();
162
+ replacement->init(connection.shm, pending.format, frameWidth, frameHeight, static_cast<int>(pending.stride));
163
+ buffer = std::move(replacement);
164
+ }
165
+ if (pending.damageCopy) zwlr_screencopy_frame_v1_copy_with_damage(pending.proxy, buffer->proxy);
166
+ else zwlr_screencopy_frame_v1_copy(pending.proxy, buffer->proxy);
167
+ pending.copied = true;
168
+ }
169
+
170
+ bool process() {
171
+ if (!frame) return false;
172
+ if (frame->hasShm && (frame->width != static_cast<uint32_t>(width) ||
173
+ frame->height != static_cast<uint32_t>(height)))
174
+ throw std::runtime_error("Wayland output size changed from " + std::to_string(width) + "x" +
175
+ std::to_string(height) + " to " + std::to_string(frame->width) + "x" +
176
+ std::to_string(frame->height) +
177
+ "; restart relay and, in attach mode, keep the output mode fixed");
178
+ if (frame->error) std::rethrow_exception(frame->error);
179
+ if (frame->failed) {
180
+ const int target = frame->target;
181
+ const bool damageCopy = frame->damageCopy;
182
+ frame.reset();
183
+ if (!damageCopy) throw std::runtime_error("Wayland screencopy failed");
184
+ if (++failures >= 3) throw std::runtime_error("Wayland screencopy failed three consecutive times");
185
+ request(target, true);
186
+ return false;
187
+ }
188
+ if (frame->ready) {
189
+ const int target = frame->target;
190
+ const bool damageCopy = frame->damageCopy;
191
+ auto& buffer = *buffers[target];
192
+ buffer.orient(frame->flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT);
193
+ failures = 0;
194
+ if (damageCopy) {
195
+ damage.insert(damage.end(), frame->damage.begin(), frame->damage.end());
196
+ if (damage.size() > max_damage_rects) damage = {{0, 0, width, height}};
197
+ current = target;
198
+ }
199
+ frame.reset();
200
+ if (damageCopy) request(1 - target, true);
201
+ return damageCopy;
202
+ }
203
+ if (frame->bufferDone && !frame->copied) copy();
204
+ return false;
205
+ }
206
+
207
+ void flush() {
208
+ require(wl_display_flush(connection.display) >= 0, "flushing Wayland requests failed");
209
+ }
210
+
211
+ bool dispatch(int timeout) {
212
+ const bool hadFrame = bool(frame);
213
+ require(wl_display_dispatch_pending(connection.display) >= 0, "dispatching Wayland events failed");
214
+ bool dirty = process();
215
+ if (dirty || (hadFrame && !frame)) { flush(); return dirty; }
216
+ while (wl_display_prepare_read(connection.display) != 0) {
217
+ require(wl_display_dispatch_pending(connection.display) >= 0, "dispatching Wayland events failed");
218
+ dirty = process() || dirty;
219
+ if (dirty || (hadFrame && !frame)) { flush(); return dirty; }
220
+ }
221
+ if (wl_display_flush(connection.display) < 0) {
222
+ wl_display_cancel_read(connection.display);
223
+ throw std::runtime_error("flushing Wayland requests failed");
224
+ }
225
+ pollfd descriptor{fd(), POLLIN, 0};
226
+ const int result = poll(&descriptor, 1, timeout);
227
+ const int pollError = errno;
228
+ if (result <= 0 || !(descriptor.revents & POLLIN)) {
229
+ wl_display_cancel_read(connection.display);
230
+ if (result < 0 && pollError != EINTR) throw std::runtime_error("polling Wayland display failed");
231
+ if (descriptor.revents & (POLLERR | POLLHUP | POLLNVAL))
232
+ throw std::runtime_error("Wayland display connection closed");
233
+ return dirty;
234
+ }
235
+ require(wl_display_read_events(connection.display) >= 0, "reading Wayland events failed");
236
+ require(wl_display_dispatch_pending(connection.display) >= 0, "dispatching Wayland events failed");
237
+ dirty = process() || dirty;
238
+ flush();
239
+ return dirty;
240
+ }
241
+
242
+ void captureBlocking(int target) {
243
+ require(!frame, "Wayland screencopy frame already in flight");
244
+ request(target, false);
245
+ const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
246
+ while (frame) {
247
+ const auto now = std::chrono::steady_clock::now();
248
+ if (now >= deadline) {
249
+ frame.reset();
250
+ throw std::runtime_error("Wayland screencopy timed out");
251
+ }
252
+ const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now).count();
253
+ dispatch(static_cast<int>(remaining));
254
+ }
255
+ }
256
+
257
+ public:
258
+ WaylandCaptureSource() : width(connection.width), height(connection.height) {
259
+ require(connection.screencopyManager, "Wayland compositor does not offer zwlr_screencopy_manager_v1");
260
+ require(wl_proxy_get_version(reinterpret_cast<wl_proxy*>(connection.screencopyManager)) >= 3,
261
+ "Wayland compositor must offer zwlr_screencopy_manager_v1 version 3");
262
+ require(connection.shm, "Wayland compositor does not offer wl_shm");
263
+ checkSize(width, height);
264
+ }
265
+
266
+ int fd() const override { return wl_display_get_fd(connection.display); }
267
+ std::pair<int, int> dimensions() const override { return {width, height}; }
268
+ PixelFormat pixelFormat() const override { return PixelFormat::BGRA; }
269
+ bool cursorSupported() const override { return false; }
270
+ bool drainDamage() override { return dispatch(0); }
271
+
272
+ FrameView capture() override {
273
+ captureBlocking(2);
274
+ return buffers[2]->view();
275
+ }
276
+
277
+ FrameView captureChanged(bool) override {
278
+ if (current < 0) {
279
+ captureBlocking(0);
280
+ current = 0;
281
+ request(1, true);
282
+ flush();
283
+ }
284
+ return buffers[current]->view();
285
+ }
286
+
287
+ bool changedRects(std::vector<DamageRect>& out) override {
288
+ out = std::move(damage);
289
+ damage.clear();
290
+ return true;
291
+ }
292
+ };
293
+ }
294
+
295
+ std::unique_ptr<CaptureSource> makeCaptureSource() {
296
+ return std::make_unique<WaylandCaptureSource>();
297
+ }
298
+ }