Debian Codegraph @

player/clipboard/clipboard-wayland.c

raw symbols SCIP
1 /*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <wayland-client.h>
19
20 #include "ext-data-control-v1.h"
21
22 #include "clipboard.h"
23 #include "common/common.h"
24 #include "common/msg.h"
25 #include "misc/bstr.h"
26 #include "osdep/io.h"
27 #include "osdep/poll_wrapper.h"
28 #include "osdep/threads.h"
29
30 static const uint8_t MESSAGE_DEATH = 0;
31 static const uint8_t MESSAGE_CREATE_SOURCES = 1;
32
33 struct clipboard_wayland_data_offer {
34 struct ext_data_control_offer_v1 *offer;
35 char *mime_type;
36 int fd;
37 };
38
39 struct clipboard_wayland_priv {
40 mp_mutex lock;
41 // accessed by both threads
42 int message_pipe[2];
43 bstr selection_text;
44 bstr primary_selection_text;
45 bool data_changed;
46 // only accessed by the core thread
47 mp_thread thread;
48 // only accessed by the clipboard thread
49 struct mp_log *log;
50 int display_fd;
51 struct wl_display *display;
52 struct wl_registry *registry;
53 struct wl_list seat_list;
54 struct ext_data_control_manager_v1 *dcman;
55 struct clipboard_wayland_data_offer *selection_offer;
56 struct clipboard_wayland_data_offer *primary_selection_offer;
57 };
58
59 struct clipboard_wayland_seat {
60 struct clipboard_wayland_priv *wl;
61 struct wl_seat *seat;
62 struct wl_list link;
63 uint32_t id;
64 struct ext_data_control_device_v1 *dcdev;
65 struct ext_data_control_source_v1 *selection_source;
66 struct ext_data_control_source_v1 *primary_selection_source;
67 bool offered_plain_text;
68 };
69
70 static void remove_seat(struct clipboard_wayland_seat *seat)
71 {
72 if (!seat)
73 return;
74
75 MP_VERBOSE(seat->wl, "Deregistering seat 0x%x\n", seat->id);
76 wl_list_remove(&seat->link);
77 if (seat->dcdev)
78 ext_data_control_device_v1_destroy(seat->dcdev);
79 if (seat->selection_source)
80 ext_data_control_source_v1_destroy(seat->selection_source);
81 if (seat->primary_selection_source)
82 ext_data_control_source_v1_destroy(seat->primary_selection_source);
83 wl_seat_destroy(seat->seat);
84 talloc_free(seat);
85 }
86
87 static void destroy_offer(struct clipboard_wayland_data_offer *o)
88 {
89 TA_FREEP(&o->mime_type);
90 if (o->fd != -1)
91 close(o->fd);
92 if (o->offer)
93 ext_data_control_offer_v1_destroy(o->offer);
94 *o = (struct clipboard_wayland_data_offer){.fd = -1};
95 }
96
97
98 static void handle_selection(void *data,
99 struct ext_data_control_device_v1 *dcdev,
100 struct ext_data_control_offer_v1 *id,
101 struct clipboard_wayland_data_offer *o)
102 {
103 struct clipboard_wayland_seat *s = data;
104 struct clipboard_wayland_priv *wl = s->wl;
105 if (o->offer) {
106 destroy_offer(o);
107 MP_VERBOSE(wl, "Received a new selection offer. Releasing the previous offer.\n");
108 }
109 o->offer = id;
110 if (!id)
111 return;
112
113 int pipefd[2];
114
115 if (pipe2(pipefd, O_CLOEXEC) == -1) {
116 MP_ERR(wl, "Failed to create selection pipe!\n");
117 return;
118 }
119
120 // Only receive plain text for now, may expand later.
121 if (s->offered_plain_text) {
122 ext_data_control_offer_v1_receive(o->offer, "text/plain;charset=utf-8", pipefd[1]);
123 s->offered_plain_text = false;
124 }
125 close(pipefd[1]);
126 o->fd = pipefd[0];
127 }
128
129 static void data_offer_handle_offer(void *data,
130 struct ext_data_control_offer_v1 *ext_data_control_offer_v1,
131 const char *mime_type)
132 {
133 struct clipboard_wayland_seat *s = data;
134 if (!s->offered_plain_text)
135 s->offered_plain_text = !strcmp(mime_type, "text/plain;charset=utf-8");
136 }
137
138 static const struct ext_data_control_offer_v1_listener data_offer_listener = {
139 data_offer_handle_offer,
140 };
141
142 static void data_device_handle_data_offer(void *data,
143 struct ext_data_control_device_v1 *dcdev,
144 struct ext_data_control_offer_v1 *id)
145 {
146 struct clipboard_wayland_seat *s = data;
147 ext_data_control_offer_v1_add_listener(id, &data_offer_listener, s);
148 }
149
150 static void data_device_handle_selection(void *data,
151 struct ext_data_control_device_v1 *dcdev,
152 struct ext_data_control_offer_v1 *id)
153 {
154 struct clipboard_wayland_seat *s = data;
155 struct clipboard_wayland_priv *wl = s->wl;
156 handle_selection(data, dcdev, id, wl->selection_offer);
157 }
158
159 static void data_device_handle_finished(void *data,
160 struct ext_data_control_device_v1 *dcdev)
161 {
162 struct clipboard_wayland_seat *s = data;
163 ext_data_control_device_v1_destroy(dcdev);
164 s->dcdev = NULL;
165 }
166
167 static void data_device_handle_primary_selection(void *data,
168 struct ext_data_control_device_v1 *dcdev,
169 struct ext_data_control_offer_v1 *id)
170 {
171 struct clipboard_wayland_seat *s = data;
172 struct clipboard_wayland_priv *wl = s->wl;
173 handle_selection(data, dcdev, id, wl->primary_selection_offer);
174 }
175
176 static const struct ext_data_control_device_v1_listener data_device_listener = {
177 data_device_handle_data_offer,
178 data_device_handle_selection,
179 data_device_handle_finished,
180 data_device_handle_primary_selection,
181 };
182
183 static void data_source_handle_send(void *data,
184 struct ext_data_control_source_v1 *data_source,
185 const char *mime_type,
186 int32_t fd)
187 {
188 struct clipboard_wayland_seat *s = data;
189 struct clipboard_wayland_priv *wl = s->wl;
190 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0 ||
191 strcmp(mime_type, "text/plain") == 0 ||
192 strcmp(mime_type, "text") == 0)
193 {
194 struct pollfd fdp = { .fd = fd, .events = POLLOUT };
195 if (poll(&fdp, 1, 0) <= 0)
196 goto cleanup;
197 if (fdp.revents & (POLLERR | POLLHUP)) {
198 MP_VERBOSE(wl, "data source send aborted (write error)\n");
199 goto cleanup;
200 }
201
202 if (fdp.revents & POLLOUT) {
203 mp_mutex_lock(&wl->lock);
204 bstr text = data_source == s->selection_source ? wl->selection_text : wl->primary_selection_text;
205 ssize_t data_written = write(fd, text.start, text.len);
206 mp_mutex_unlock(&wl->lock);
207 if (data_written == -1) {
208 MP_VERBOSE(wl, "data source send aborted (write error: %s)\n", mp_strerror(errno));
209 } else {
210 MP_VERBOSE(wl, "%zu bytes written to the data source fd\n", data_written);
211 }
212 }
213 }
214
215 cleanup:
216 close(fd);
217 }
218
219 static void data_source_handle_cancelled(void *data,
220 struct ext_data_control_source_v1 *data_source)
221 {
222 // This can happen when another client sets selection, which invalidates the current source.
223 struct clipboard_wayland_seat *s = data;
224 ext_data_control_source_v1_destroy(data_source);
225 if (s->selection_source == data_source)
226 s->selection_source = NULL;
227 if (s->primary_selection_source == data_source)
228 s->primary_selection_source = NULL;
229 }
230
231 static const struct ext_data_control_source_v1_listener data_source_listener = {
232 data_source_handle_send,
233 data_source_handle_cancelled,
234 };
235
236 static void registry_handle_add(void *data, struct wl_registry *reg, uint32_t id,
237 const char *interface, uint32_t ver)
238 {
239 int found = 1;
240 struct clipboard_wayland_priv *wl = data;
241
242 if (!strcmp(interface, wl_seat_interface.name) && found++) {
243 ver = MPMIN(ver, 8);
244 struct clipboard_wayland_seat *seat = talloc_zero(wl, struct clipboard_wayland_seat);
245 seat->wl = wl;
246 seat->id = id;
247 seat->seat = wl_registry_bind(reg, id, &wl_seat_interface, ver);
248 wl_list_insert(&wl->seat_list, &seat->link);
249 }
250
251 if (!strcmp(interface, ext_data_control_manager_v1_interface.name) && found++) {
252 ver = MPMIN(ver, 2);
253 wl->dcman = wl_registry_bind(reg, id, &ext_data_control_manager_v1_interface, ver);
254 }
255
256 if (found > 1)
257 MP_VERBOSE(wl, "Registered interface %s at version %d\n", interface, ver);
258 }
259
260 static void registry_handle_remove(void *data, struct wl_registry *reg, uint32_t id)
261 {
262 struct clipboard_wayland_priv *wl = data;
263 struct clipboard_wayland_seat *seat, *seat_tmp;
264 wl_list_for_each_safe(seat, seat_tmp, &wl->seat_list, link) {
265 if (seat->id == id) {
266 remove_seat(seat);
267 return;
268 }
269 }
270 }
271
272 static const struct wl_registry_listener registry_listener = {
273 registry_handle_add,
274 registry_handle_remove,
275 };
276
277 static void seat_create_data_source(struct clipboard_wayland_seat *seat, bool is_primary)
278 {
279 struct ext_data_control_source_v1 *source = ext_data_control_manager_v1_create_data_source(seat->wl->dcman);
280 ext_data_control_source_v1_offer(source, "text/plain;charset=utf-8");
281 ext_data_control_source_v1_offer(source, "text/plain");
282 ext_data_control_source_v1_offer(source, "text");
283 ext_data_control_source_v1_add_listener(source, &data_source_listener, seat);
284 if (is_primary) {
285 seat->primary_selection_source = source;
286 ext_data_control_device_v1_set_primary_selection(seat->dcdev, seat->primary_selection_source);
287 } else {
288 seat->selection_source = source;
289 ext_data_control_device_v1_set_selection(seat->dcdev, seat->selection_source);
290 }
291 }
292
293 static void create_data_sources(struct clipboard_wayland_priv *wl)
294 {
295 struct clipboard_wayland_seat *seat;
296 wl_list_for_each(seat, &wl->seat_list, link) {
297 if (!seat->selection_source && wl->dcman) {
298 seat_create_data_source(seat, false);
299 }
300 if (!seat->primary_selection_source && wl->dcman) {
301 seat_create_data_source(seat, true);
302 }
303 }
304 }
305
306 static void clipboard_wayland_uninit(struct clipboard_wayland_priv *wl)
307 {
308 if (!wl)
309 return;
310
311 destroy_offer(wl->selection_offer);
312 destroy_offer(wl->primary_selection_offer);
313
314 struct clipboard_wayland_seat *seat, *seat_tmp;
315 wl_list_for_each_safe(seat, seat_tmp, &wl->seat_list, link)
316 remove_seat(seat);
317
318 if (wl->dcman)
319 ext_data_control_manager_v1_destroy(wl->dcman);
320 if (wl->registry)
321 wl_registry_destroy(wl->registry);
322 if (wl->display)
323 wl_display_disconnect(wl->display);
324 }
325
326 static bool clipboard_wayland_init(struct clipboard_wayland_priv *wl)
327 {
328 if (!getenv("WAYLAND_DISPLAY") && !getenv("WAYLAND_SOCKET"))
329 goto err;
330
331 wl->display = wl_display_connect(NULL);
332 if (!wl->display)
333 goto err;
334
335 wl->registry = wl_display_get_registry(wl->display);
336 wl_registry_add_listener(wl->registry, &registry_listener, wl);
337
338 /* Do a roundtrip to run the registry */
339 wl_display_roundtrip(wl->display);
340
341 if (wl->dcman) {
342 struct clipboard_wayland_seat *seat;
343 wl_list_for_each(seat, &wl->seat_list, link) {
344 seat->dcdev = ext_data_control_manager_v1_get_data_device(wl->dcman, seat->seat);
345 ext_data_control_device_v1_add_listener(seat->dcdev, &data_device_listener, seat);
346 }
347 } else {
348 MP_VERBOSE(wl, "Compositor doesn't support the %s protocol!\n",
349 ext_data_control_manager_v1_interface.name);
350 goto err;
351 }
352
353 wl->display_fd = wl_display_get_fd(wl->display);
354
355 /* Do another roundtrip to ensure all of the above is initialized
356 * before mpv does anything else. */
357 wl_display_roundtrip(wl->display);
358
359 return true;
360
361 err:
362 clipboard_wayland_uninit(wl);
363 return false;
364 }
365
366 static void get_selection_data(struct clipboard_wayland_priv *wl, struct clipboard_wayland_data_offer *o,
367 bool is_primary)
368 {
369 ssize_t data_read = 0;
370 const size_t chunk_size = 256;
371 bstr content = {
372 .start = talloc_zero_size(wl, chunk_size),
373 };
374
375 while (1) {
376 data_read = read(o->fd, content.start + content.len, chunk_size);
377 if (data_read == -1 && errno == EINTR)
378 continue;
379 else if (data_read <= 0)
380 break;
381 content.len += data_read;
382 content.start = talloc_realloc_size(wl, content.start, content.len + chunk_size);
383 memset(content.start + content.len, 0, chunk_size);
384 }
385
386 if (data_read == -1) {
387 MP_VERBOSE(wl, "data offer aborted (read error: %s)\n", mp_strerror(errno));
388 } else {
389 MP_VERBOSE(wl, "Read %zu bytes from the data offer fd\n", content.len);
390 // Update clipboard text content
391 mp_mutex_lock(&wl->lock);
392 if (is_primary) {
393 talloc_free(wl->primary_selection_text.start);
394 wl->primary_selection_text = content;
395 } else {
396 talloc_free(wl->selection_text.start);
397 wl->selection_text = content;
398 }
399 wl->data_changed = true;
400 mp_mutex_unlock(&wl->lock);
401 content = (bstr){0};
402 }
403
404 talloc_free(content.start);
405 destroy_offer(o);
406 }
407
408 static bool clipboard_wayland_dispatch_events(struct clipboard_wayland_priv *wl, int64_t timeout_ns)
409 {
410 if (wl->display_fd == -1)
411 return false;
412
413 struct pollfd fds[] = {
414 {.fd = wl->display_fd, .events = POLLIN },
415 {.fd = wl->message_pipe[0], .events = POLLIN },
416 {.fd = wl->selection_offer->fd, .events = POLLIN },
417 {.fd = wl->primary_selection_offer->fd, .events = POLLIN },
418 };
419
420 while (wl_display_prepare_read(wl->display) != 0)
421 wl_display_dispatch_pending(wl->display);
422 wl_display_flush(wl->display);
423
424 mp_poll(fds, MP_ARRAY_SIZE(fds), timeout_ns);
425
426 if (fds[0].revents & POLLIN) {
427 wl_display_read_events(wl->display);
428 } else {
429 wl_display_cancel_read(wl->display);
430 }
431
432 if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
433 MP_FATAL(wl, "Error occurred on the display fd\n");
434 wl->display_fd = -1;
435 return false;
436 }
437
438 if (fds[1].revents & POLLIN) {
439 uint8_t msg = 0;
440 if (read(wl->message_pipe[0], &msg, sizeof(msg)) == sizeof(msg) && msg == MESSAGE_CREATE_SOURCES) {
441 create_data_sources(wl);
442 } else {
443 return false;
444 }
445 }
446
447 if (fds[2].revents & POLLIN)
448 get_selection_data(wl, wl->selection_offer, false);
449
450 if (fds[3].revents & POLLIN)
451 get_selection_data(wl, wl->primary_selection_offer, true);
452
453 if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))
454 destroy_offer(wl->selection_offer);
455
456 if (fds[3].revents & (POLLERR | POLLHUP | POLLNVAL))
457 destroy_offer(wl->primary_selection_offer);
458
459 wl_display_dispatch_pending(wl->display);
460 return true;
461 }
462
463 static void clipboard_wayland_run(struct clipboard_wayland_priv *wl)
464 {
465 while (clipboard_wayland_dispatch_events(wl, MP_TIME_S_TO_NS(10)))
466 ;
467
468 clipboard_wayland_uninit(wl);
469 }
470
471 static MP_THREAD_VOID clipboard_thread(void *p)
472 {
473 struct clipboard_wayland_priv *priv = p;
474 clipboard_wayland_run(priv);
475 MP_THREAD_RETURN();
476 }
477
478 static int init(struct clipboard_ctx *cl, struct clipboard_init_params *params)
479 {
480 cl->priv = talloc_zero(cl, struct clipboard_wayland_priv);
481 struct clipboard_wayland_priv *priv = cl->priv;
482 priv->message_pipe[0] = priv->message_pipe[1] = priv->display_fd = -1;
483 priv->log = mp_log_new(priv, cl->log, "wayland");
484 priv->selection_offer = talloc_zero(priv, struct clipboard_wayland_data_offer),
485 priv->primary_selection_offer = talloc_zero(priv, struct clipboard_wayland_data_offer),
486 priv->selection_offer->fd = priv->primary_selection_offer->fd = -1;
487 wl_list_init(&priv->seat_list);
488 mp_mutex_init(&priv->lock);
489
490 if (mp_make_wakeup_pipe(priv->message_pipe) < 0)
491 goto pipe_err;
492 if (!clipboard_wayland_init(priv))
493 goto init_err;
494 if (mp_thread_create(&priv->thread, clipboard_thread, cl->priv))
495 goto thread_err;
496 return CLIPBOARD_SUCCESS;
497
498 thread_err:
499 clipboard_wayland_uninit(priv);
500 init_err:
501 close(priv->message_pipe[0]);
502 close(priv->message_pipe[1]);
503 pipe_err:
504 mp_mutex_destroy(&priv->lock);
505 TA_FREEP(&cl->priv);
506 return CLIPBOARD_FAILED;
507 }
508
509 static void uninit(struct clipboard_ctx *cl)
510 {
511 struct clipboard_wayland_priv *priv = cl->priv;
512 if (!priv)
513 return;
514 (void)write(priv->message_pipe[1], &MESSAGE_DEATH, sizeof(MESSAGE_DEATH));
515 mp_thread_join(priv->thread);
516 close(priv->message_pipe[0]);
517 close(priv->message_pipe[1]);
518 mp_mutex_destroy(&priv->lock);
519 TA_FREEP(&cl->priv);
520 }
521
522 static bool data_changed(struct clipboard_ctx *cl)
523 {
524 struct clipboard_wayland_priv *priv = cl->priv;
525 bool res;
526 mp_mutex_lock(&priv->lock);
527 res = priv->data_changed;
528 if (res)
529 priv->data_changed = false;
530 mp_mutex_unlock(&priv->lock);
531 return res;
532 }
533
534 static int get_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
535 struct clipboard_data *out, void *talloc_ctx)
536 {
537 if (params->type != CLIPBOARD_DATA_TEXT)
538 return CLIPBOARD_FAILED;
539
540 int ret = CLIPBOARD_FAILED;
541 struct clipboard_wayland_priv *priv = cl->priv;
542 mp_mutex_lock(&priv->lock);
543 switch (params->target) {
544 case CLIPBOARD_TARGET_CLIPBOARD:
545 out->type = CLIPBOARD_DATA_TEXT;
546 out->u.text = bstrto0(talloc_ctx, priv->selection_text);
547 ret = CLIPBOARD_SUCCESS;
548 break;
549 case CLIPBOARD_TARGET_PRIMARY_SELECTION:
550 out->type = CLIPBOARD_DATA_TEXT;
551 out->u.text = bstrto0(talloc_ctx, priv->primary_selection_text);
552 ret = CLIPBOARD_SUCCESS;
553 break;
554 }
555 mp_mutex_unlock(&priv->lock);
556 return ret;
557 }
558
559 static int set_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
560 struct clipboard_data *data)
561 {
562 if (params->type != CLIPBOARD_DATA_TEXT || data->type != CLIPBOARD_DATA_TEXT)
563 return CLIPBOARD_FAILED;
564
565 struct clipboard_wayland_priv *priv = cl->priv;
566 bstr text = bstrdup(priv, bstr0(data->u.text));
567 mp_mutex_lock(&priv->lock);
568 switch (params->target) {
569 case CLIPBOARD_TARGET_CLIPBOARD:
570 talloc_free(priv->selection_text.start);
571 priv->selection_text = text;
572 break;
573 case CLIPBOARD_TARGET_PRIMARY_SELECTION:
574 talloc_free(priv->primary_selection_text.start);
575 priv->primary_selection_text = text;
576 break;
577 }
578 mp_mutex_unlock(&priv->lock);
579 (void)write(priv->message_pipe[1], &MESSAGE_CREATE_SOURCES, sizeof(MESSAGE_CREATE_SOURCES));
580 return CLIPBOARD_SUCCESS;
581 }
582
583 const struct clipboard_backend clipboard_backend_wayland = {
584 .name = "wayland",
585 .desc = "Wayland clipboard (Data control protocol)",
586 .init = init,
587 .uninit = uninit,
588 .data_changed = data_changed,
589 .get_data = get_data,
590 .set_data = set_data,
591 };