Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
kwin_session.cpp
Go to the documentation of this file.
1
5#include "kwin_session.h"
6
7#include "fake-input.h"
8#include "wayland_peer.h"
9#include "zkde-screencast-unstable-v1.h"
10
11#include <algorithm>
12#include <cerrno>
13#include <cmath>
14#include <cstring>
15#include <filesystem>
16#include <limits>
17#include <mutex>
18#include <poll.h>
19#include <sys/socket.h>
20#include <sys/un.h>
21#include <unistd.h>
22#include <vector>
23#include <wayland-client.h>
24
25namespace platf::kwin {
26 namespace {
27 using clock_t = std::chrono::steady_clock;
28 constexpr auto setup_timeout = std::chrono::seconds(5);
29
30 int remaining_ms(clock_t::time_point deadline) {
31 const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - clock_t::now()).count();
32 return static_cast<int>(std::clamp<std::int64_t>(left, 0, std::numeric_limits<int>::max()));
33 }
34
35 bool fixed_valid(double value) {
36 return std::isfinite(value) && value >= -8388608.0 && value < 8388608.0;
37 }
38 } // namespace
39
41 struct monitor_t {
42 wl_output *proxy {};
43 std::uint32_t global {};
44 output_t info;
45 bool removed {};
46 };
47
48 mutable std::mutex mutex;
49 wl_display *display {};
50 wl_registry *registry {};
51 org_kde_kwin_fake_input *input {};
52 zkde_screencast_unstable_v1 *capture {};
53 zkde_screencast_stream_unstable_v1 *stream {};
54 std::vector<std::unique_ptr<monitor_t>> monitors;
55 monitor_t *selected {};
56 output_t geometry;
57 std::uint32_t capture_global {};
58 std::uint32_t input_global {};
59 std::uint64_t serial {};
60 bool node_ready {};
61 bool dead {};
62 std::string failure;
63
64 ~impl_t() {
65 if (stream) {
66 zkde_screencast_stream_unstable_v1_close(stream);
67 }
68 if (input) {
69 org_kde_kwin_fake_input_destroy(input);
70 }
71 if (capture) {
72 zkde_screencast_unstable_v1_destroy(capture);
73 }
74 for (const auto &monitor : monitors) {
75 wl_output_destroy(monitor->proxy);
76 }
77 if (registry) {
78 wl_registry_destroy(registry);
79 }
80 if (display) {
81 wl_display_disconnect(display);
82 }
83 }
84
85 void fail(std::string message) {
86 if (!dead) {
87 failure = std::move(message);
88 }
89 dead = true;
90 }
91
92 bool flush() {
93 if (dead) {
94 return false;
95 }
96 if (wl_display_flush(display) < 0 && errno != EAGAIN) {
97 fail("Private Wayland connection failed: " + std::string(std::strerror(errno)));
98 }
99 return !dead;
100 }
101
102 bool pump(std::chrono::milliseconds timeout) {
103 if (dead) {
104 return false;
105 }
106 const auto deadline = clock_t::now() + timeout;
107 while (wl_display_prepare_read(display) != 0) {
108 if (wl_display_dispatch_pending(display) < 0) {
109 fail("Private Wayland event dispatch failed");
110 return false;
111 }
112 if (dead) {
113 return false;
114 }
115 }
116 short events = POLLIN;
117 if (wl_display_flush(display) < 0) {
118 if (errno == EAGAIN) {
119 events |= POLLOUT;
120 } else {
121 wl_display_cancel_read(display);
122 fail("Private Wayland flush failed");
123 return false;
124 }
125 }
126 pollfd event {wl_display_get_fd(display), events, 0};
127 int result;
128 do {
129 result = ::poll(&event, 1, remaining_ms(deadline));
130 } while (result < 0 && errno == EINTR && clock_t::now() < deadline);
131 if (result < 0 || (event.revents & (POLLERR | POLLHUP | POLLNVAL))) {
132 wl_display_cancel_read(display);
133 fail("Private compositor connection closed");
134 return false;
135 }
136 if (event.revents & POLLIN) {
137 if (wl_display_read_events(display) < 0) {
138 fail("Private Wayland read failed");
139 return false;
140 }
141 } else {
142 wl_display_cancel_read(display);
143 }
144 if ((event.revents & POLLOUT) && !flush()) {
145 return false;
146 }
147 if (wl_display_dispatch_pending(display) < 0) {
148 fail("Private Wayland protocol error");
149 }
150 if (selected && (selected->info.width != geometry.width || selected->info.height != geometry.height ||
151 selected->info.x != geometry.x || selected->info.y != geometry.y || selected->info.scale != geometry.scale)) {
152 fail("Private output geometry changed; a new capture session is required");
153 }
154 return !dead;
155 }
156
157 bool sync() {
158 bool complete = false;
159 auto callback = wl_display_sync(display);
160 if (!callback) {
161 fail("Could not create private Wayland synchronization request");
162 return false;
163 }
164 static const wl_callback_listener listener {
165 [](void *data, wl_callback *, std::uint32_t) {
166 *static_cast<bool *>(data) = true;
167 },
168 };
169 wl_callback_add_listener(callback, &listener, &complete);
170 const auto deadline = clock_t::now() + setup_timeout;
171 while (!complete && !dead && clock_t::now() < deadline) {
172 pump(std::chrono::milliseconds(remaining_ms(deadline)));
173 }
174 wl_callback_destroy(callback);
175 if (!complete && !dead) {
176 fail("Private compositor synchronization timed out");
177 }
178 return complete && !dead;
179 }
180
181 static void announced(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
182 auto &self = *static_cast<impl_t *>(data);
183 if (std::strcmp(interface, "wl_output") == 0 && version >= 2) {
184 auto monitor = std::make_unique<monitor_t>();
185 monitor->global = name;
186 monitor->proxy = static_cast<wl_output *>(wl_registry_bind(registry, name, &wl_output_interface, std::min(version, 4u)));
187 static const wl_output_listener listener {
188 [](void *data, wl_output *, int x, int y, int, int, int, const char *, const char *, int) {
189 auto &info = static_cast<monitor_t *>(data)->info;
190 info.x = x;
191 info.y = y;
192 },
193 [](void *data, wl_output *, std::uint32_t flags, int width, int height, int) {
194 if (flags & WL_OUTPUT_MODE_CURRENT) {
195 auto &info = static_cast<monitor_t *>(data)->info;
196 info.width = width;
197 info.height = height;
198 }
199 },
200 [](void *, wl_output *) {
201 },
202 [](void *data, wl_output *, int scale) {
203 static_cast<monitor_t *>(data)->info.scale = scale;
204 },
205 [](void *data, wl_output *, const char *name) {
206 static_cast<monitor_t *>(data)->info.name = name;
207 },
208 [](void *, wl_output *, const char *) {
209 },
210 };
211 wl_output_add_listener(monitor->proxy, &listener, monitor.get());
212 self.monitors.emplace_back(std::move(monitor));
213 } else if (std::strcmp(interface, "org_kde_kwin_fake_input") == 0 && version >= 5 && !self.input) {
214 self.input_global = name;
215 self.input = static_cast<org_kde_kwin_fake_input *>(wl_registry_bind(registry, name, &org_kde_kwin_fake_input_interface, std::min(version, 6u)));
216 } else if (std::strcmp(interface, "zkde_screencast_unstable_v1") == 0 && version >= 6 && !self.capture) {
217 self.capture_global = name;
218 // Object serials avoid reconnecting to a recycled PipeWire node ID.
219 self.capture = static_cast<zkde_screencast_unstable_v1 *>(wl_registry_bind(registry, name, &zkde_screencast_unstable_v1_interface, 6));
220 }
221 }
222
223 static void removed(void *data, wl_registry *, std::uint32_t name) {
224 auto &self = *static_cast<impl_t *>(data);
225 for (auto &monitor : self.monitors) {
226 if (monitor->global == name) {
227 monitor->removed = true;
228 if (self.selected == monitor.get()) {
229 self.fail("Private output was removed");
230 }
231 }
232 }
233 if (name == self.capture_global || name == self.input_global) {
234 self.fail("Private compositor removed a required protocol");
235 }
236 }
237 };
238
239 connection_t::connection_t(std::unique_ptr<impl_t> state):
240 impl(std::move(state)) {}
241
242 connection_t::~connection_t() = default;
243
244 std::unique_ptr<connection_t> connection_t::open(const endpoint_t &endpoint, bool capture, bool input, std::string &error) {
245 error.clear();
246 auto state = std::make_unique<impl_t>();
247 // Keep the helper alive until KWin has resolved the peer executable and
248 // authorized the registry. The accepted socket retains its peer identity
249 // when handed to this capability-bearing process through SCM_RIGHTS.
250 wayland_peer_t peer;
251 int fd = peer.open(endpoint.wayland_socket, error);
252 if (fd < 0) {
253 return nullptr;
254 }
255 // libwayland takes ownership, including on connection-construction failure.
256 state->display = wl_display_connect_to_fd(fd);
257 if (!state->display) {
258 error = "Could not initialize private Wayland connection";
259 return nullptr;
260 }
261 state->registry = wl_display_get_registry(state->display);
262 if (!state->registry) {
263 error = "Could not create private Wayland registry";
264 return nullptr;
265 }
266 static const wl_registry_listener listener {impl_t::announced, impl_t::removed};
267 wl_registry_add_listener(state->registry, &listener, state.get());
268 if (!state->sync() || !state->sync()) {
269 error = state->failure;
270 return nullptr;
271 }
272 if ((capture && !state->capture) || (input && !state->input)) {
273 error = "Private KWin capture/input protocol unavailable or executable not authorized";
274 return nullptr;
275 }
276 std::vector<impl_t::monitor_t *> matches;
277 for (auto &monitor : state->monitors) {
278 if (!monitor->removed && (endpoint.output_name.empty() || monitor->info.name == endpoint.output_name)) {
279 matches.push_back(monitor.get());
280 }
281 }
282 if (matches.size() != 1 || matches.front()->info.width <= 0 || matches.front()->info.height <= 0 || matches.front()->info.scale <= 0) {
283 error = "Private output selection is missing, ambiguous, or has no valid mode";
284 return nullptr;
285 }
286 state->selected = matches.front();
287 state->geometry = state->selected->info;
288 if (input) {
289 org_kde_kwin_fake_input_authenticate(state->input, "Hermes", "Remote input for this detached desktop");
290 if (!state->sync()) {
291 error = state->failure;
292 return nullptr;
293 }
294 }
295 return std::unique_ptr<connection_t>(new connection_t(std::move(state)));
296 }
297
298 const output_t &connection_t::output() const {
299 return impl->geometry;
300 }
301
302 bool connection_t::start_capture(bool cursor, std::uint64_t &serial, std::string &error) {
303 std::lock_guard lock(impl->mutex);
304 if (impl->dead || !impl->capture || impl->stream) {
305 error = impl->dead ? impl->failure : "Private capture is unavailable or already started";
306 return false;
307 }
308 impl->stream = zkde_screencast_unstable_v1_stream_output(impl->capture, impl->selected->proxy, cursor ? ZKDE_SCREENCAST_UNSTABLE_V1_POINTER_EMBEDDED : ZKDE_SCREENCAST_UNSTABLE_V1_POINTER_HIDDEN);
309 if (!impl->stream) {
310 impl->fail("Could not create private capture request");
311 error = impl->failure;
312 return false;
313 }
314 static const zkde_screencast_stream_unstable_v1_listener listener {
315 [](void *data, zkde_screencast_stream_unstable_v1 *) {
316 static_cast<impl_t *>(data)->fail("Private capture stream closed");
317 },
318 [](void *, zkde_screencast_stream_unstable_v1 *, std::uint32_t) {
319 },
320 [](void *data, zkde_screencast_stream_unstable_v1 *, const char *message) {
321 static_cast<impl_t *>(data)->fail("Private capture failed: " + std::string(message));
322 },
323 [](void *data, zkde_screencast_stream_unstable_v1 *, std::uint32_t high, std::uint32_t low) {
324 auto &self = *static_cast<impl_t *>(data);
325 self.serial = (static_cast<std::uint64_t>(high) << 32) | low;
326 self.node_ready = true;
327 },
328 };
329 zkde_screencast_stream_unstable_v1_add_listener(impl->stream, &listener, impl.get());
330 const auto deadline = clock_t::now() + setup_timeout;
331 while (!impl->node_ready && !impl->dead && clock_t::now() < deadline) {
332 impl->pump(std::chrono::milliseconds(remaining_ms(deadline)));
333 }
334 if (!impl->node_ready && !impl->dead) {
335 impl->fail("Private capture stream creation timed out");
336 }
337 if (impl->dead) {
338 error = impl->failure;
339 return false;
340 }
341 serial = impl->serial;
342 return true;
343 }
344
345 bool connection_t::dispatch(std::chrono::milliseconds timeout) {
346 std::lock_guard lock(impl->mutex);
347 return impl->pump(std::min(timeout, std::chrono::milliseconds(50)));
348 }
349
350 bool connection_t::pointer_motion(double dx, double dy) {
351 std::lock_guard lock(impl->mutex);
352 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || !fixed_valid(dx) || !fixed_valid(dy)) {
353 return false;
354 }
355 org_kde_kwin_fake_input_pointer_motion(impl->input, wl_fixed_from_double(dx), wl_fixed_from_double(dy));
356 return impl->flush();
357 }
358
359 bool connection_t::pointer_absolute(double x, double y) {
360 std::lock_guard lock(impl->mutex);
361 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || !fixed_valid(x) || !fixed_valid(y)) {
362 return false;
363 }
364 org_kde_kwin_fake_input_pointer_motion_absolute(impl->input, wl_fixed_from_double(x), wl_fixed_from_double(y));
365 return impl->flush();
366 }
367
368 bool connection_t::button(std::uint32_t button, bool pressed) {
369 std::lock_guard lock(impl->mutex);
370 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input) {
371 return false;
372 }
373 org_kde_kwin_fake_input_button(impl->input, button, pressed ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED);
374 return impl->flush();
375 }
376
377 bool connection_t::axis(bool horizontal, double value) {
378 std::lock_guard lock(impl->mutex);
379 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || !fixed_valid(value)) {
380 return false;
381 }
382 org_kde_kwin_fake_input_axis(impl->input, horizontal ? WL_POINTER_AXIS_HORIZONTAL_SCROLL : WL_POINTER_AXIS_VERTICAL_SCROLL, wl_fixed_from_double(value));
383 return impl->flush();
384 }
385
386 bool connection_t::key(std::uint32_t key, bool pressed) {
387 std::lock_guard lock(impl->mutex);
388 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input) {
389 return false;
390 }
391 org_kde_kwin_fake_input_keyboard_key(impl->input, key, pressed ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED);
392 return impl->flush();
393 }
394
395 bool connection_t::touch_down(std::uint32_t id, double x, double y) {
396 std::lock_guard lock(impl->mutex);
397 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || !fixed_valid(x) || !fixed_valid(y)) {
398 return false;
399 }
400 org_kde_kwin_fake_input_touch_down(impl->input, id, wl_fixed_from_double(x), wl_fixed_from_double(y));
401 org_kde_kwin_fake_input_touch_frame(impl->input);
402 return impl->flush();
403 }
404
405 bool connection_t::touch_motion(std::uint32_t id, double x, double y) {
406 std::lock_guard lock(impl->mutex);
407 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || !fixed_valid(x) || !fixed_valid(y)) {
408 return false;
409 }
410 org_kde_kwin_fake_input_touch_motion(impl->input, id, wl_fixed_from_double(x), wl_fixed_from_double(y));
411 org_kde_kwin_fake_input_touch_frame(impl->input);
412 return impl->flush();
413 }
414
415 bool connection_t::touch_up(std::uint32_t id) {
416 std::lock_guard lock(impl->mutex);
417 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input) {
418 return false;
419 }
420 org_kde_kwin_fake_input_touch_up(impl->input, id);
421 org_kde_kwin_fake_input_touch_frame(impl->input);
422 return impl->flush();
423 }
424
425 bool connection_t::keysym(std::uint32_t symbol, bool pressed) {
426 std::lock_guard lock(impl->mutex);
427 if (!impl->pump(std::chrono::milliseconds(0)) || !impl->input || org_kde_kwin_fake_input_get_version(impl->input) < 6) {
428 return false;
429 }
430 org_kde_kwin_fake_input_keyboard_keysym(impl->input, symbol, pressed ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED);
431 return impl->flush();
432 }
433
434 std::string connection_t::error() const {
435 std::lock_guard lock(impl->mutex);
436 return impl->failure;
437 }
438} // namespace platf::kwin
@ low
Low priority.
Definition common.h:694
@ high
High priority.
Definition common.h:696
input_t input(const std::string &session_tag={})
@ timeout
Timeout.
Definition common.h:438
Explicit, generation-bound connections to a private KWin session.
Give private KWin connections an executable identity without changing Hermes's capabilities.