blob: aca3e07b210116b14555fdf4b29867bc0630afe4 [file] [log] [blame]
Foxe Chenb90c2392025-06-27 21:10:35 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * wayland.c: Stuff related to wayland
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_WAYLAND
17
18#include <wayland-client.h>
19
20#ifdef FEAT_WAYLAND_CLIPBOARD
21# include "auto/wayland/wlr-data-control-unstable-v1.h"
22# include "auto/wayland/ext-data-control-v1.h"
23# include "auto/wayland/xdg-shell.h"
24# include "auto/wayland/primary-selection-unstable-v1.h"
25#endif
26
27// Struct that represents a seat. (Should be accessed via
28// vwl_get_seat()).
29typedef struct {
30 struct wl_seat *proxy;
31 char *label; // Name of seat as text (e.g. seat0,
32 // seat1...).
33 uint32_t capabilities; // Bitmask of the capabilites of the seat
34 // (pointer, keyboard, touch).
35} vwl_seat_T;
36
37// Global objects
38typedef struct {
39#ifdef FEAT_WAYLAND_CLIPBOARD
40 // Data control protocols
41 struct zwlr_data_control_manager_v1 *zwlr_data_control_manager_v1;
42 struct ext_data_control_manager_v1 *ext_data_control_manager_v1;
43 struct wl_data_device_manager *wl_data_device_manager;
44 struct wl_shm *wl_shm;
45 struct wl_compositor *wl_compositor;
46 struct xdg_wm_base *xdg_wm_base;
47 struct zwp_primary_selection_device_manager_v1
48 *zwp_primary_selection_device_manager_v1;
49#endif
50} vwl_global_objects_T;
51
52// Struct wrapper for wayland display and registry
53typedef struct {
54 struct wl_display *proxy;
55 int fd; // File descriptor for display
56
57 struct {
58 struct wl_registry *proxy;
59 } registry;
60} vwl_display_T;
61
62#ifdef FEAT_WAYLAND_CLIPBOARD
63
64typedef struct {
65 struct wl_shm_pool *pool;
66 int fd;
67
68 struct wl_buffer *buffer;
69 int available;
70
71 int width;
72 int height;
73 int stride;
74 int size;
75} vwl_buffer_store_T;
76
77typedef struct {
78 void *user_data;
79 void (*on_focus)(void *data, uint32_t serial);
80
81 struct wl_surface *surface;
82 struct wl_keyboard *keyboard;
83
84 struct {
85 struct xdg_surface *surface;
86 struct xdg_toplevel *toplevel;
87 } shell;
88
89 int got_focus;
90} vwl_fs_surface_T; // fs = focus steal
91
92// Wayland protocols for accessing the selection
93typedef enum {
94 VWL_DATA_PROTOCOL_NONE,
95 VWL_DATA_PROTOCOL_EXT,
96 VWL_DATA_PROTOCOL_WLR,
97 VWL_DATA_PROTOCOL_CORE,
98 VWL_DATA_PROTOCOL_PRIMARY
99} vwl_data_protocol_T;
100
101// DATA RELATED OBJECT WRAPPERS
102// These wrap around a proxy and act as a generic container.
103// The `data` member is used to pass other needed stuff around such as a
104// vwl_clipboard_selection_T pointer.
105
106typedef struct {
107 void *proxy;
108 void *data; // Is not set when a new offer is created on a
109 // data_offer event. Only set when listening to a
110 // data offer.
111 vwl_data_protocol_T protocol;
112} vwl_data_offer_T;
113
114typedef struct {
115 void *proxy;
116 void *data;
117 vwl_data_protocol_T protocol;
118} vwl_data_source_T;
119
120typedef struct {
121 void *proxy;
122 void *data;
123 vwl_data_protocol_T protocol;
124} vwl_data_device_T;
125
126typedef struct {
127 void *proxy;
128 vwl_data_protocol_T protocol;
129} vwl_data_device_manager_T;
130
131// LISTENER WRAPPERS
132
133typedef struct {
134 void (*data_offer)(vwl_data_device_T *device, vwl_data_offer_T *offer);
135
136 // If the protocol that the data device uses doesn't support a specific
137 // selection, then this callback will never be called with that selection.
138 void (*selection)(
139 vwl_data_device_T *device,
140 vwl_data_offer_T *offer,
141 wayland_selection_T selection);
142
143 // This event is only relevant for data control protocols
144 void (*finished)(vwl_data_device_T *device);
145} vwl_data_device_listener_T;
146
147typedef struct {
148 void (*send)(vwl_data_source_T *source, const char *mime_type, int fd);
149 void (*cancelled)(vwl_data_source_T *source);
150} vwl_data_source_listener_T;
151
152typedef struct {
153 void (*offer)(vwl_data_offer_T *offer, const char *mime_type);
154} vwl_data_offer_listener_T;
155
156typedef struct
157{
158 // What selection this refers to
159 wayland_selection_T selection;
160
161 // Do not destroy here
162 vwl_data_device_manager_T manager;
163
164 vwl_data_device_T device;
165 vwl_data_source_T source;
166 vwl_data_offer_T *offer; // Current offer for the selection
167
168 garray_T mime_types; // Mime types supported by the
169 // current offer
170
171 garray_T tmp_mime_types; // Temporary array for mime
172 // types when we are receiving
173 // them. When the selection
174 // event arrives and it is the
175 // one we want, then copy it
176 // over to mime_types
177
178 // To be populated by callbacks from outside this file
179 wayland_cb_send_data_func_T send_cb;
180 wayland_cb_selection_cancelled_func_T cancelled_cb;
181
182 int requires_focus; // If focus needs to be given to us to work
183} vwl_clipboard_selection_T;
184
185// Holds stuff related to the clipboard/selections
186typedef struct {
187 // Do not destroy here, will be destroyed when vwl_disconnect_display() is
188 // called.
189 vwl_seat_T *seat;
190
191 vwl_clipboard_selection_T regular;
192 vwl_clipboard_selection_T primary;
193
194 vwl_buffer_store_T *fs_buffer;
195} vwl_clipboard_T;
196
197#endif // FEAT_WAYLAND_CLIPBOARD
198
199static int vwl_display_flush(vwl_display_T *display);
200static void vwl_callback_done(void *data, struct wl_callback *callback,
201 uint32_t cb_data);
202static int vwl_display_roundtrip(vwl_display_T *display);
203static int vwl_display_dispatch(vwl_display_T *display);
204static int vwl_display_dispatch_any(vwl_display_T *display);
205
206static void vwl_log_handler(const char *fmt, va_list args);
207static int vwl_connect_display(const char *display);
208static void vwl_disconnect_display(void);
209
210static void vwl_xdg_wm_base_listener_ping(void *data,
211 struct xdg_wm_base *base, uint32_t serial);
212static int vwl_listen_to_registry(void);
213
214static void vwl_registry_listener_global(void *data,
215 struct wl_registry *registry, uint32_t name,
216 const char *interface, uint32_t version);
217static void vwl_registry_listener_global_remove(void *data,
218 struct wl_registry *registry, uint32_t name);
219
220static void vwl_add_seat(struct wl_seat *seat);
221static void vwl_seat_listener_name(void *data, struct wl_seat *seat,
222 const char *name);
223static void vwl_seat_listener_capabilities(void *data, struct wl_seat *seat,
224 uint32_t capabilities);
225static void vwl_destroy_seat(vwl_seat_T *seat);
226
227static vwl_seat_T *vwl_get_seat(const char *label);
228static struct wl_keyboard *vwl_seat_get_keyboard(vwl_seat_T *seat);
229
230#ifdef FEAT_WAYLAND_CLIPBOARD
231
232static int vwl_focus_stealing_available(void);
233static void vwl_xdg_surface_listener_configure(void *data,
234 struct xdg_surface *surface, uint32_t serial);
235
236static void vwl_bs_buffer_listener_release(void *data,
237 struct wl_buffer *buffer);
238static void vwl_destroy_buffer_store(vwl_buffer_store_T *store);
239static vwl_buffer_store_T *vwl_init_buffer_store(int width, int height);
240
241static void vwl_destroy_fs_surface(vwl_fs_surface_T *store);
242static int vwl_init_fs_surface(vwl_seat_T *seat,
243 vwl_buffer_store_T *buffer_store,
244 void (*on_focus)(void *, uint32_t), void *user_data);
245
246static void vwl_fs_keyboard_listener_enter(void *data,
247 struct wl_keyboard *keyboard, uint32_t serial,
248 struct wl_surface *surface, struct wl_array *keys);
249static void vwl_fs_keyboard_listener_keymap(void *data,
250 struct wl_keyboard *keyboard, uint32_t format,
251 int fd, uint32_t size);
252static void vwl_fs_keyboard_listener_leave(void *data,
253 struct wl_keyboard *keyboard, uint32_t serial,
254 struct wl_surface *surface);
255static void vwl_fs_keyboard_listener_key(void *data,
256 struct wl_keyboard *keyboard, uint32_t serial,
257 uint32_t time, uint32_t key, uint32_t state);
258static void vwl_fs_keyboard_listener_modifiers(void *data,
259 struct wl_keyboard *keyboard, uint32_t serial,
260 uint32_t mods_depressed, uint32_t mods_latched,
261 uint32_t mods_locked, uint32_t group);
262static void vwl_fs_keyboard_listener_repeat_info(void *data,
263 struct wl_keyboard *keyboard, int32_t rate, int32_t delay);
264
265static void vwl_gen_data_device_listener_data_offer(void *data,
266 void *offer_proxy);
267static void vwl_gen_data_device_listener_selection(void *data,
268 void *offer_proxy, wayland_selection_T selection,
269 vwl_data_protocol_T protocol);
270
271static void vwl_data_device_destroy(vwl_data_device_T *device, int alloced);
272static void vwl_data_offer_destroy(vwl_data_offer_T *offer, int alloced);
273static void vwl_data_source_destroy(vwl_data_source_T *source, int alloced);
274
275static void vwl_data_device_add_listener(vwl_data_device_T *device,
276 void *data);
277static void vwl_data_source_add_listener(vwl_data_source_T *source,
278 void *data);
279static void vwl_data_offer_add_listener(vwl_data_offer_T *offer,
280 void *data);
281
282static void vwl_data_device_set_selection(vwl_data_device_T *device,
283 vwl_data_source_T *source, uint32_t serial,
284 wayland_selection_T selection);
285static void vwl_data_offer_receive(vwl_data_offer_T *offer,
286 const char *mime_type, int fd);
287static int vwl_get_data_device_manager(vwl_data_device_manager_T *manager,
288 wayland_selection_T selection);
289static void vwl_get_data_device(vwl_data_device_manager_T *manager,
290 vwl_seat_T *seat, vwl_data_device_T *device);
291static void vwl_create_data_source(vwl_data_device_manager_T *manager,
292 vwl_data_source_T *source);
293static void vwl_data_source_offer(vwl_data_source_T *source,
294 const char *mime_type);
295
296static void vwl_clipboard_free_mime_types(
297 vwl_clipboard_selection_T *clip_sel);
298static int vwl_clipboard_selection_is_ready(
299 vwl_clipboard_selection_T *clip_sel);
300
301static void vwl_data_device_listener_data_offer(
302 vwl_data_device_T *device, vwl_data_offer_T *offer);
303static void vwl_data_offer_listener_offer(vwl_data_offer_T *offer,
304 const char *mime_type);
305static void vwl_data_device_listener_selection(vwl_data_device_T *device,
306 vwl_data_offer_T *offer, wayland_selection_T selection);
307static void vwl_data_device_listener_finished(vwl_data_device_T *device);
308
309static void vwl_data_source_listener_send(vwl_data_source_T *source,
310 const char *mime_type, int fd);
311static void vwl_data_source_listener_cancelled(vwl_data_source_T *source);
312
313static void vwl_on_focus_set_selection(void *data, uint32_t serial);
314
315static void wayland_set_display(const char *display);
316
317static vwl_data_device_listener_T vwl_data_device_listener = {
318 .data_offer = vwl_data_device_listener_data_offer,
319 .selection = vwl_data_device_listener_selection,
320 .finished = vwl_data_device_listener_finished
321};
322
323static vwl_data_source_listener_T vwl_data_source_listener = {
324 .send = vwl_data_source_listener_send,
325 .cancelled = vwl_data_source_listener_cancelled
326};
327
328static vwl_data_offer_listener_T vwl_data_offer_listener = {
329 .offer = vwl_data_offer_listener_offer
330};
331
332static struct xdg_wm_base_listener vwl_xdg_wm_base_listener = {
333 .ping = vwl_xdg_wm_base_listener_ping
334};
335
336static struct xdg_surface_listener vwl_xdg_surface_listener = {
337 .configure = vwl_xdg_surface_listener_configure
338};
339
340static struct wl_buffer_listener vwl_cb_buffer_listener = {
341 .release = vwl_bs_buffer_listener_release
342};
343
344static struct wl_keyboard_listener vwl_fs_keyboard_listener = {
345 .enter = vwl_fs_keyboard_listener_enter,
346 .key = vwl_fs_keyboard_listener_key,
347 .keymap = vwl_fs_keyboard_listener_keymap,
348 .leave = vwl_fs_keyboard_listener_leave,
349 .modifiers = vwl_fs_keyboard_listener_modifiers,
350 .repeat_info = vwl_fs_keyboard_listener_repeat_info
351};
352
353#endif // FEAT_WAYLAND_CLIPBOARD
354
355static struct wl_callback_listener vwl_callback_listener = {
356 .done = vwl_callback_done
357};
358
359static struct wl_registry_listener vwl_registry_listener = {
360 .global = vwl_registry_listener_global,
361 .global_remove = vwl_registry_listener_global_remove
362};
363
364static struct wl_seat_listener vwl_seat_listener = {
365 .name = vwl_seat_listener_name,
366 .capabilities = vwl_seat_listener_capabilities
367};
368
369static vwl_display_T vwl_display;
370static vwl_global_objects_T vwl_gobjects;
371static garray_T vwl_seats;
372
373#ifdef FEAT_WAYLAND_CLIPBOARD
374// Make sure to sync this with vwl_cb_uninit since it memsets this to zero
375static vwl_clipboard_T vwl_clipboard = {
376 .regular.selection = WAYLAND_SELECTION_REGULAR,
377 .primary.selection = WAYLAND_SELECTION_PRIMARY,
378};
379
380// Only really used for debugging/testing purposes in order to force focus
381// stealing even when a data control protocol is available.
382static int force_fs = FALSE;
383#endif
384
385/*
386 * Like wl_display_flush but always writes all the data in the buffer to the
387 * display fd. Returns FAIL on failure and OK on success.
388 */
389 static int
390vwl_display_flush(vwl_display_T *display)
391{
392 int ret;
393
394#ifndef HAVE_SELECT
395 struct pollfd fds;
396
397 fds.fd = display->fd;
398 fds.events = POLLOUT;
399#else
400 fd_set wfds;
401 struct timeval tv;
402
403 FD_ZERO(&wfds);
404 FD_SET(display->fd, &wfds);
405
406 tv.tv_sec = 0;
407 tv.tv_usec = p_wtm * 1000;
408#endif
409
410 if (display->proxy == NULL)
411 return FAIL;
412
413 // Send the requests we have made to the compositor, until we have written
414 // all the data. Poll in order to check if the display fd is writable, if
415 // not, then wait until it is and continue writing or until we timeout.
416 while (errno = 0, (ret = wl_display_flush(display->proxy)) == -1
417 && errno == EAGAIN)
418 {
419#ifndef HAVE_SELECT
420 if (poll(&fds, 1, p_wtm) <= 0)
421#else
422 if (select(display->fd + 1, NULL, &wfds, NULL, &tv) <= 0)
423#endif
424 return FAIL;
425 }
426 // Return FAIL on error or timeout
427 if ((errno != 0 && errno != EAGAIN) || ret == -1)
428 return FAIL;
429
430 return OK;
431}
432
433/*
434 * Called when compositor is done processing requests/events.
435 */
436 static void
437vwl_callback_done(void *data, struct wl_callback *callback,
438 uint32_t cb_data UNUSED)
439{
440 *((int*)data) = TRUE;
441 wl_callback_destroy(callback);
442}
443
444/*
445 * Like wl_display_roundtrip but polls the display fd with a timeout. Returns
446 * FAIL on failure and OK on success.
447 */
448 static int
449vwl_display_roundtrip(vwl_display_T *display)
450{
451 struct wl_callback *callback;
452 int ret, done = FALSE;
453 struct timeval start, now;
454
455 if (display->proxy == NULL)
456 return FAIL;
457
458 // Tell compositor to emit 'done' event after processing all requests we
459 // have sent and handling events.
460 callback = wl_display_sync(display->proxy);
461
462 if (callback == NULL)
463 return FAIL;
464
465 wl_callback_add_listener(callback, &vwl_callback_listener, &done);
466
467 gettimeofday(&start, NULL);
468
469 // Wait till we get the done event (which will set `done` to TRUE), unless
470 // we timeout
471 while (TRUE)
472 {
473 ret = vwl_display_dispatch(display);
474
475 if (done || ret == -1)
476 break;
477
478 gettimeofday(&now, NULL);
479
480 if ((now.tv_sec * 1000000 + now.tv_usec) -
481 (start.tv_sec * 1000000 + start.tv_usec) >= p_wtm * 1000)
482 {
483 ret = -1;
484 break;
485 }
486 }
487
488 if (ret == -1)
489 {
490 if (!done)
491 wl_callback_destroy(callback);
492 return FAIL;
493 }
494
495 return OK;
496}
497
498/*
499 * Like wl_display_roundtrip but polls the display fd with a timeout. Returns
500 * number of events dispatched on success else -1 on failure.
501 */
502 static int
503vwl_display_dispatch(vwl_display_T *display)
504{
505#ifndef HAVE_SELECT
506 struct pollfd fds;
507
508 fds.fd = display->fd;
509 fds.events = POLLIN;
510#else
511 fd_set rfds;
512 struct timeval tv;
513
514 FD_ZERO(&rfds);
515 FD_SET(display->fd, &rfds);
516
517 tv.tv_sec = 0;
518 tv.tv_usec = p_wtm * 1000;
519#endif
520
521 if (display->proxy == NULL)
522 return -1;
523
524 while (wl_display_prepare_read(display->proxy) == -1)
525 // Dispatch any queued events so that we can start reading
526 if (wl_display_dispatch_pending(display->proxy) == -1)
527 return -1;
528
529 // Send any requests before we starting blocking to read display fd
530 if (vwl_display_flush(display) == FAIL)
531 {
532 wl_display_cancel_read(display->proxy);
533 return -1;
534 }
535
536 // Poll until there is data to read from the display fd.
537#ifndef HAVE_SELECT
538 if (poll(&fds, 1, p_wtm) <= 0)
539#else
540 if (select(display->fd + 1, &rfds, NULL, NULL, &tv) <= 0)
541#endif
542 {
543 wl_display_cancel_read(display->proxy);
544 return -1;
545 }
546
547 // Read events into the queue
548 if (wl_display_read_events(display->proxy) == -1)
549 return -1;
550
551 // Dispatch those events (call the handlers associated for each event)
552 return wl_display_dispatch_pending(display->proxy);
553}
554
555/*
556 * Same as vwl_display_dispatch but poll/select is never called. This is useful
557 * is poll/select was already called before or if you just want to dispatch any
558 * events that happen to be waiting to be dispatched on the display fd.
559 */
560 static int
561vwl_display_dispatch_any(vwl_display_T *display)
562{
563 if (display->proxy == NULL)
564 return -1;
565
566 while (wl_display_prepare_read(display->proxy) == -1)
567 // Dispatch any queued events so that we can start reading
568 if (wl_display_dispatch_pending(display->proxy) == -1)
569 return -1;
570
571 // Send any requests before we starting blocking to read display fd
572 if (vwl_display_flush(display) == FAIL)
573 {
574 wl_display_cancel_read(display->proxy);
575 return -1;
576 }
577
578 // Read events into the queue
579 if (wl_display_read_events(display->proxy) == -1)
580 return -1;
581
582 // Dispatch those events (call the handlers associated for each event)
583 return wl_display_dispatch_pending(display->proxy);
584}
585
586/*
587 * Redirect libwayland logging to use ch_log + emsg instead.
588 */
589 static void
590vwl_log_handler(const char *fmt, va_list args)
591{
592 // 512 bytes should be big enough
593 char *buf = alloc(512);
594 char *prefix = _("wayland protocol error -> ");
595 size_t len = STRLEN(prefix);
596
597 if (buf == NULL)
598 return;
599
600 vim_strncpy((char_u*)buf, (char_u*)prefix, len);
601 vim_vsnprintf(buf + len, 4096 - len, fmt, args);
602
603 // Remove newline that libwayland puts
604 buf[STRLEN(buf) - 1] = NUL;
605
606 ch_log(NULL, "%s", buf);
607 emsg(buf);
608
609 vim_free(buf);
610}
611
612/*
613 * Connect to the display with name; passing NULL will use libwayland's way of
614 * getting the display. Additionally get the registry object but will not
615 * starting listening. Returns OK on sucess and FAIL on failure.
616 */
617 static int
618vwl_connect_display(const char *display)
619{
620 if (wayland_no_connect)
621 return FAIL;
622
623 // We will get an error if XDG_RUNTIME_DIR is not set.
624 if (mch_getenv("XDG_RUNTIME_DIR") == NULL)
625 return FAIL;
626
627 // Must set log handler before we connect display in order to work.
628 wl_log_set_handler_client(vwl_log_handler);
629
630 vwl_display.proxy = wl_display_connect(display);
631
632 if (vwl_display.proxy == NULL)
633 return FAIL;
634
635 wayland_set_display(display);
636 vwl_display.fd = wl_display_get_fd(vwl_display.proxy);
637
638 vwl_display.registry.proxy = wl_display_get_registry(vwl_display.proxy);
639
640 if (vwl_display.registry.proxy == NULL)
641 {
642 vwl_disconnect_display();
643 return FAIL;
644 }
645
646 return OK;
647}
648
649#define destroy_gobject(object) \
650 if (vwl_gobjects.object != NULL) \
651 { \
652 object##_destroy(vwl_gobjects.object); \
653 vwl_gobjects.object = NULL; \
654 }
655
656/*
657 * Disconnects the display and frees up all resources, including all global
658 * objects.
659 */
660 static void
661vwl_disconnect_display(void)
662{
663
664 destroy_gobject(ext_data_control_manager_v1)
665 destroy_gobject(zwlr_data_control_manager_v1)
666 destroy_gobject(wl_data_device_manager)
667 destroy_gobject(wl_shm)
668 destroy_gobject(wl_compositor)
669 destroy_gobject(xdg_wm_base)
670 destroy_gobject(zwp_primary_selection_device_manager_v1)
671
672 for (int i = 0; i < vwl_seats.ga_len; i++)
673 vwl_destroy_seat(&((vwl_seat_T *)vwl_seats.ga_data)[i]);
674 ga_clear(&vwl_seats);
675 vwl_seats.ga_len = 0;
676
677 if (vwl_display.registry.proxy != NULL)
678 {
679 wl_registry_destroy(vwl_display.registry.proxy);
680 vwl_display.registry.proxy = NULL;
681 }
682 if (vwl_display.proxy != NULL)
683 {
684 wl_display_disconnect(vwl_display.proxy);
685 vwl_display.proxy = NULL;
686 }
687}
688
689/*
690 * Tells the compositor we are still responsive.
691 */
692 static void
693vwl_xdg_wm_base_listener_ping(
694 void *data UNUSED,
695 struct xdg_wm_base *base,
696 uint32_t serial)
697{
698 xdg_wm_base_pong(base, serial);
699}
700
701/*
702 * Start listening to the registry and get initial set of global
703 * objects/interfaces.
704 */
705 static int
706vwl_listen_to_registry(void)
707{
708 // Only meant for debugging/testing purposes
709 char_u *env = mch_getenv("VIM_WAYLAND_FORCE_FS");
710
711 if (env != NULL && STRCMP(env, "1") == 0)
712 force_fs = TRUE;
713 else
714 force_fs = FALSE;
715
716 ga_init2(&vwl_seats, sizeof(vwl_seat_T), 1);
717
718 wl_registry_add_listener(
719 vwl_display.registry.proxy,
720 &vwl_registry_listener,
721 NULL);
722
723 if (vwl_display_roundtrip(&vwl_display) == FAIL)
724 return FAIL;
725
726#ifdef FEAT_WAYLAND_CLIPBOARD
727 // If we have a suitable data control protocol discard the rest. If we only
728 // have wlr data control protocol but its version is 1, then don't discard
729 // globals if we also have the primary selection protocol.
730 if (!force_fs &&
731 (vwl_gobjects.ext_data_control_manager_v1 != NULL ||
732 (vwl_gobjects.zwlr_data_control_manager_v1 != NULL &&
733 zwlr_data_control_manager_v1_get_version(
734 vwl_gobjects.zwlr_data_control_manager_v1) > 1)))
735 {
736 destroy_gobject(wl_data_device_manager)
737 destroy_gobject(wl_shm)
738 destroy_gobject(wl_compositor)
739 destroy_gobject(xdg_wm_base)
740 }
741 else
742 // Be ready for ping events
743 xdg_wm_base_add_listener(
744 vwl_gobjects.xdg_wm_base,
745 &vwl_xdg_wm_base_listener,
746 NULL);
747#endif
748 return OK;
749}
750
751#define SET_GOBJECT(object, min_ver) \
752 do { \
753 chosen_interface = &object##_interface; \
754 object_member = (void*)&vwl_gobjects.object; \
755 min_version = min_ver; \
756 } while (0)
757
758/*
759 * Callback for global event, for each global interface the compositor supports.
760 * Keep in sync with vwl_disconnect_display().
761 */
762 static void
763vwl_registry_listener_global(
764 void *data UNUSED,
765 struct wl_registry *registry UNUSED,
766 uint32_t name,
767 const char *interface,
768 uint32_t version)
769{
770
771 const struct wl_interface *chosen_interface = NULL;
772 void *proxy;
773 uint32_t min_version;
774 void **object_member;
775
776 if (STRCMP(interface, wl_seat_interface.name) == 0)
777 {
778 chosen_interface = &wl_seat_interface;
779 min_version = 2;
780 }
781#ifdef FEAT_WAYLAND_CLIPBOARD
782 else if (STRCMP(interface, zwlr_data_control_manager_v1_interface.name) == 0)
783 SET_GOBJECT(zwlr_data_control_manager_v1, 1);
784
785 else if (STRCMP(interface, ext_data_control_manager_v1_interface.name) == 0)
786 SET_GOBJECT(ext_data_control_manager_v1, 1);
787
788 else if (STRCMP(interface, wl_data_device_manager_interface.name) == 0)
789 SET_GOBJECT(wl_data_device_manager, 1);
790
791 else if (STRCMP(interface, wl_shm_interface.name) == 0)
792 SET_GOBJECT(wl_shm, 1);
793
794 else if (STRCMP(interface, wl_compositor_interface.name) == 0)
795 SET_GOBJECT(wl_compositor, 2);
796
797 else if (STRCMP(interface, xdg_wm_base_interface.name) == 0)
798 SET_GOBJECT(xdg_wm_base, 1);
799
800 else if (STRCMP(interface,
801 zwp_primary_selection_device_manager_v1_interface.name) == 0)
802 SET_GOBJECT(zwp_primary_selection_device_manager_v1, 1);
803#endif
804
805 if (chosen_interface == NULL || version < min_version)
806 return;
807
808 proxy = wl_registry_bind(vwl_display.registry.proxy, name, chosen_interface,
809 version);
810
811 if (chosen_interface == &wl_seat_interface)
812 // Add seat to vwl_seats array, as we can have multiple seats.
813 vwl_add_seat(proxy);
814 else
815 // Hold proxy & name in the vwl_gobject struct
816 *object_member = proxy;
817}
818
819/*
820 * Called when a global object is removed, if so, then do nothing. This is to
821 * avoid a global being removed while it is in the process of being used. Let
822 * the user call :wlrestore in order to reset everything. Requests to that
823 * global will just be ignored on the compositor side.
824 */
825 static void
826vwl_registry_listener_global_remove(
827 void *data UNUSED,
828 struct wl_registry *registry UNUSED,
829 uint32_t name UNUSED)
830{
831}
832
833/*
834 * Add a new seat given its proxy to the global grow array
835 */
836 static void
837vwl_add_seat(struct wl_seat *seat_proxy)
838{
839 vwl_seat_T *seat;
840
841 if (ga_grow(&vwl_seats, 1) == FAIL)
842 return;
843
844 seat = &((vwl_seat_T *)vwl_seats.ga_data)[vwl_seats.ga_len];
845
846 seat->proxy = seat_proxy;
847
848 // Get label and capabilities
849 wl_seat_add_listener(seat_proxy, &vwl_seat_listener, seat);
850
851 if (vwl_display_roundtrip(&vwl_display) == FAIL)
852 return;
853
854 // Check if label has been allocated
855 if (seat->label == NULL)
856 return;
857
858 vwl_seats.ga_len++;
859}
860
861/*
862 * Callback for seat text label/name
863 */
864 static void
865vwl_seat_listener_name(
866 void *data,
867 struct wl_seat *seat_proxy UNUSED,
868 const char *name)
869{
870 vwl_seat_T *seat = data;
871
872 seat->label = (char *)vim_strsave((char_u *)name);
873}
874
875/*
876 * Callback for seat capabilities
877 */
878 static void
879vwl_seat_listener_capabilities(
880 void *data,
881 struct wl_seat *seat_proxy UNUSED,
882 uint32_t capabilities)
883{
884 vwl_seat_T *seat = data;
885
886 seat->capabilities = capabilities;
887}
888
889/*
890 * Destroy/free seat.
891 */
892 static void
893vwl_destroy_seat(vwl_seat_T *seat)
894{
895 if (seat->proxy != NULL)
896 {
897 if (wl_seat_get_version(seat->proxy) >= 5)
898 // Helpful for the compositor
899 wl_seat_release(seat->proxy);
900 else
901 wl_seat_destroy(seat->proxy);
902 seat->proxy = NULL;
903 }
904 vim_free(seat->label);
905 seat->label = NULL;
906}
907
908/*
909 * Return a seat with the give name/label. If none exists then NULL is returned.
910 * If NULL or an empty string is passed as the label then $XDG_SEAT is used
911 * else the first available seat found is used.
912 */
913 static vwl_seat_T *
914vwl_get_seat(const char *label)
915{
916 if ((STRCMP(label, "") == 0 || label == NULL) && vwl_seats.ga_len > 0)
917 {
918 const char *xdg_seat = (char*)mch_getenv("XDG_SEAT");
919
920 if (xdg_seat == NULL)
921 return &((vwl_seat_T *)vwl_seats.ga_data)[0];
922 else
923 label = xdg_seat;
924 }
925
926 for (int i = 0; i < vwl_seats.ga_len; i++)
927 {
928 vwl_seat_T *seat = &((vwl_seat_T *)vwl_seats.ga_data)[i];
929 if (STRCMP(seat->label, label) == 0)
930 return seat;
931 }
932 return NULL;
933}
934
935/*
936 * Get keyboard object from seat and return it. NULL is returned on
937 * failure such as when a keyboard is not available for seat.
938 */
939 static struct wl_keyboard *
940vwl_seat_get_keyboard(vwl_seat_T *seat)
941{
942 if (!(seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD))
943 return NULL;
944
945 return wl_seat_get_keyboard(seat->proxy);
946}
947
948/*
949 * Connects to the wayland display with given name and binds to global objects
950 * as needed. If display is NULL then the $WAYLAND_DISPLAY environment variable
951 * will be used (handled by libwayland). Returns FAIL on failure and OK on
952 * success
953 */
954 int
955wayland_init_client(const char *display)
956{
957 wayland_set_display(display);
958
959 if (vwl_connect_display(display) == FAIL ||
960 vwl_listen_to_registry() == FAIL)
961 goto fail;
962
963 wayland_display_fd = vwl_display.fd;
964
965 return OK;
966fail:
967 // Set v:wayland_display to empty string (but not wayland_display_name)
968 wayland_set_display("");
969 return FAIL;
970}
971
972/*
973 * Disconnect wayland client and free up all resources used.
974 */
975 void
976wayland_uninit_client(void)
977{
978#ifdef FEAT_WAYLAND_CLIPBOARD
979 wayland_cb_uninit();
980#endif
981 vwl_disconnect_display();
982
983 wayland_set_display("");
984}
985
986/*
987 * Return TRUE if wayland display connection is valid and ready.
988 */
989 int
990wayland_client_is_connected(int quiet)
991{
992 if (vwl_display.proxy == NULL)
993 goto error;
994
995 // Display errors are always fatal
996 if (wl_display_get_error(vwl_display.proxy) != 0
997 || vwl_display_flush(&vwl_display) == FAIL)
998 goto error;
999
1000 return TRUE;
1001error:
1002 if (!quiet)
1003 emsg(e_wayland_connection_unavailable);
1004 return FALSE;
1005}
1006
1007/*
1008 * Flush requests and process new Wayland events, does not poll the display file
1009 * descriptor.
1010 */
1011 int
1012wayland_client_update(void)
1013{
1014 return vwl_display_dispatch_any(&vwl_display) == -1 ? FAIL : OK;
1015}
1016
1017#ifdef FEAT_WAYLAND_CLIPBOARD
1018
1019/*
1020 * If globals required for focus stealing method is available.
1021 */
1022 static int
1023vwl_focus_stealing_available(void)
1024{
1025 return (p_wst || force_fs) &&
1026 vwl_gobjects.wl_compositor != NULL &&
1027 vwl_gobjects.wl_shm != NULL &&
1028 vwl_gobjects.xdg_wm_base != NULL;
1029}
1030
1031/*
1032 * Configure xdg_surface
1033 */
1034 static void
1035vwl_xdg_surface_listener_configure(
1036 void *data UNUSED,
1037 struct xdg_surface *surface,
1038 uint32_t serial)
1039{
1040 xdg_surface_ack_configure(surface, serial);
1041}
1042
1043/*
1044 * Called when compositor isn't using the buffer anymore, we can reuse it again.
1045 */
1046 static void
1047vwl_bs_buffer_listener_release(
1048 void *data,
1049 struct wl_buffer *buffer UNUSED)
1050{
1051 vwl_buffer_store_T *store = data;
1052
1053 store->available = TRUE;
1054}
1055
1056/*
1057 * Destroy a buffer store structure.
1058 */
1059 static void
1060vwl_destroy_buffer_store(vwl_buffer_store_T *store)
1061{
1062 if (store->buffer != NULL)
1063 wl_buffer_destroy(store->buffer);
1064 if (store->pool != NULL)
1065 wl_shm_pool_destroy(store->pool);
1066
1067 close(store->fd);
1068
1069 vim_free(store);
1070}
1071
1072/*
1073 * Initialize a buffer and its backing memory pool.
1074 */
1075 static vwl_buffer_store_T *
1076vwl_init_buffer_store(int width, int height)
1077{
1078 int fd, r;
1079 vwl_buffer_store_T *store;
1080
1081 if (vwl_gobjects.wl_shm == NULL)
1082 return NULL;
1083
1084 store = alloc(sizeof(*store));
1085
1086 if (store == NULL)
1087 return NULL;
1088
1089 store->available = FALSE;
1090
1091 store->width = width;
1092 store->height = height;
1093 store->stride = store->width * 4;
1094 store->size = store->stride * store->height;
1095
1096 fd = mch_create_anon_file();
1097 r = ftruncate(fd, store->size);
1098
1099 if (r == -1)
1100 {
1101 if (fd >= 0)
1102 close(fd);
1103 return NULL;
1104 }
1105
1106 store->pool = wl_shm_create_pool(vwl_gobjects.wl_shm, fd, store->size);
1107 store->buffer = wl_shm_pool_create_buffer(
1108 store->pool,
1109 0,
1110 store->width,
1111 store->height,
1112 store->stride,
1113 WL_SHM_FORMAT_ARGB8888);
1114
1115 store->fd = fd;
1116
1117 wl_buffer_add_listener(store->buffer, &vwl_cb_buffer_listener, store);
1118
1119 if (vwl_display_roundtrip(&vwl_display) == -1)
1120 {
1121 vwl_destroy_buffer_store(store);
1122 return NULL;
1123 }
1124
1125 store->available = TRUE;
1126
1127 return store;
1128}
1129
1130/*
1131 * Destroy a focus stealing store structure.
1132 */
1133 static void
1134vwl_destroy_fs_surface(vwl_fs_surface_T *store)
1135{
1136 if (store->shell.toplevel != NULL)
1137 xdg_toplevel_destroy(store->shell.toplevel);
1138 if (store->shell.surface != NULL)
1139 xdg_surface_destroy(store->shell.surface);
1140 if (store->surface != NULL)
1141 wl_surface_destroy(store->surface);
1142 if (store->keyboard != NULL)
1143 {
1144 if (wl_keyboard_get_version(store->keyboard) >= 3)
1145 wl_keyboard_release(store->keyboard);
1146 else
1147 wl_keyboard_destroy(store->keyboard);
1148 }
1149 vim_free(store);
1150}
1151
1152/*
1153 * Create an invisible surface in order to gain focus and call on_focus() with
1154 * serial that was given.
1155 */
1156 static int
1157vwl_init_fs_surface(
1158 vwl_seat_T *seat,
1159 vwl_buffer_store_T *buffer_store,
1160 void (*on_focus)(void *, uint32_t),
1161 void *user_data)
1162{
1163 vwl_fs_surface_T *store;
1164
1165 if (vwl_gobjects.wl_compositor == NULL || vwl_gobjects.xdg_wm_base == NULL)
1166 return FAIL;
1167 if (buffer_store == NULL || seat == NULL)
1168 return FAIL;
1169
1170 store = alloc_clear(sizeof(*store));
1171
1172 if (store == NULL)
1173 return FAIL;
1174
1175 // Get keyboard
1176 store->keyboard = vwl_seat_get_keyboard(seat);
1177
1178 if (store->keyboard == NULL)
1179 goto fail;
1180
1181 wl_keyboard_add_listener(store->keyboard, &vwl_fs_keyboard_listener, store);
1182
1183 if (vwl_display_dispatch(&vwl_display) == -1)
1184 goto fail;
1185
1186 store->surface = wl_compositor_create_surface(vwl_gobjects.wl_compositor);
1187 store->shell.surface = xdg_wm_base_get_xdg_surface(
1188 vwl_gobjects.xdg_wm_base, store->surface);
1189 store->shell.toplevel = xdg_surface_get_toplevel(store->shell.surface);
1190
1191 xdg_toplevel_set_title(store->shell.toplevel, "Vim clipboard");
1192
1193 xdg_surface_add_listener(store->shell.surface,
1194 &vwl_xdg_surface_listener, NULL);
1195
1196 wl_surface_commit(store->surface);
1197
1198 store->on_focus = on_focus;
1199 store->user_data = user_data;
1200 store->got_focus = FALSE;
1201
1202 if (vwl_display_roundtrip(&vwl_display) == FAIL)
1203 goto fail;
1204
1205 // We may get the enter event early, if we do then we will set `got_focus`
1206 // to TRUE.
1207 if (store->got_focus)
1208 goto early_exit;
1209
1210 // Buffer hasn't been released yet, abort. This shouldn't happen but still
1211 // check for it.
1212 if (!buffer_store->available)
1213 goto fail;
1214
1215 buffer_store->available = FALSE;
1216
1217 wl_surface_attach(store->surface, buffer_store->buffer, 0, 0);
1218 wl_surface_damage(store->surface, 0, 0,
1219 buffer_store->width, buffer_store->height);
1220 wl_surface_commit(store->surface);
1221
1222 {
1223 // Dispatch events until we receive the enter event. Add a max delay of
1224 // 'p_wtm' when waiting for it (may be longer depending on how long we
1225 // poll when dispatching events)
1226 struct timeval start, now;
1227
1228 gettimeofday(&start, NULL);
1229
1230 while (vwl_display_dispatch(&vwl_display) != -1)
1231 {
1232 if (store->got_focus)
1233 break;
1234
1235 gettimeofday(&now, NULL);
1236
1237 if ((now.tv_sec * 1000000 + now.tv_usec) -
1238 (start.tv_sec * 1000000 + start.tv_usec)
1239 >= p_wtm * 1000)
1240 goto fail;
1241 }
1242 }
1243early_exit:
1244 vwl_destroy_fs_surface(store);
1245 vwl_display_flush(&vwl_display);
1246
1247 return OK;
1248fail:
1249 vwl_destroy_fs_surface(store);
1250 vwl_display_flush(&vwl_display);
1251
1252 return FAIL;
1253}
1254
1255/*
1256 * Called when the keyboard focus is on our surface
1257 */
1258 static void
1259vwl_fs_keyboard_listener_enter(
1260 void *data,
1261 struct wl_keyboard *keyboard UNUSED,
1262 uint32_t serial,
1263 struct wl_surface *surface UNUSED,
1264 struct wl_array *keys UNUSED)
1265{
1266 vwl_fs_surface_T *store = data;
1267
1268 store->got_focus = TRUE;
1269
1270 if (store->on_focus != NULL)
1271 store->on_focus(store->user_data, serial);
1272}
1273
1274// Dummy functions to handle keyboard events we don't care about.
1275
1276 static void
1277vwl_fs_keyboard_listener_keymap(
1278 void *data UNUSED,
1279 struct wl_keyboard *keyboard UNUSED,
1280 uint32_t format UNUSED,
1281 int fd,
1282 uint32_t size UNUSED)
1283{
1284 close(fd);
1285}
1286
1287 static void
1288vwl_fs_keyboard_listener_leave(
1289 void *data UNUSED,
1290 struct wl_keyboard *keyboard UNUSED,
1291 uint32_t serial UNUSED,
1292 struct wl_surface *surface UNUSED)
1293{
1294}
1295
1296 static void
1297vwl_fs_keyboard_listener_key(
1298 void *data UNUSED,
1299 struct wl_keyboard *keyboard UNUSED,
1300 uint32_t serial UNUSED,
1301 uint32_t time UNUSED,
1302 uint32_t key UNUSED,
1303 uint32_t state UNUSED)
1304{
1305}
1306
1307 static void
1308vwl_fs_keyboard_listener_modifiers(
1309 void *data UNUSED,
1310 struct wl_keyboard *keyboard UNUSED,
1311 uint32_t serial UNUSED,
1312 uint32_t mods_depressed UNUSED,
1313 uint32_t mods_latched UNUSED,
1314 uint32_t mods_locked UNUSED,
1315 uint32_t group UNUSED)
1316{
1317}
1318
1319 static void
1320vwl_fs_keyboard_listener_repeat_info(
1321 void *data UNUSED,
1322 struct wl_keyboard *keyboard UNUSED,
1323 int32_t rate UNUSED,
1324 int32_t delay UNUSED)
1325{
1326}
1327
1328#define VWL_CODE_DATA_OBJECT_DESTROY(type) \
1329do { \
1330 if (type == NULL || type->proxy == NULL) \
1331 return; \
1332 switch (type->protocol) \
1333 { \
1334 case VWL_DATA_PROTOCOL_WLR: \
1335 zwlr_data_control_##type##_v1_destroy(type->proxy); \
1336 break; \
1337 case VWL_DATA_PROTOCOL_EXT: \
1338 ext_data_control_##type##_v1_destroy(type->proxy); \
1339 break; \
1340 case VWL_DATA_PROTOCOL_CORE: \
1341 wl_data_##type##_destroy(type->proxy); \
1342 break; \
1343 case VWL_DATA_PROTOCOL_PRIMARY: \
1344 zwp_primary_selection_##type##_v1_destroy(type->proxy); \
1345 break; \
1346 default: \
1347 break; \
1348 } \
1349 if (alloced) \
1350 vim_free(type); \
1351 else \
1352 type->proxy = NULL; \
1353} while (0)
1354
1355 static void
1356vwl_data_device_destroy(vwl_data_device_T *device, int alloced)
1357{
1358 VWL_CODE_DATA_OBJECT_DESTROY(device);
1359}
1360
1361 static void
1362vwl_data_offer_destroy(vwl_data_offer_T *offer, int alloced)
1363{
1364 VWL_CODE_DATA_OBJECT_DESTROY(offer);
1365}
1366
1367 static void
1368vwl_data_source_destroy(vwl_data_source_T *source, int alloced)
1369{
1370 VWL_CODE_DATA_OBJECT_DESTROY(source);
1371}
1372
1373
1374// Used to pass a vwl_data_offer_T struct from the data_offer event to the offer
1375// event and to the selection event.
1376static vwl_data_offer_T *tmp_vwl_offer;
1377
1378// These functions handle the more complicated data_offer and selection events.
1379
1380 static void
1381vwl_gen_data_device_listener_data_offer(void *data, void *offer_proxy)
1382{
1383 vwl_data_device_T *device = data;
1384
1385 tmp_vwl_offer = alloc(sizeof(*tmp_vwl_offer));
1386
1387 if (tmp_vwl_offer != NULL)
1388 {
1389 tmp_vwl_offer->proxy = offer_proxy;
1390 tmp_vwl_offer->protocol = device->protocol;
1391
1392 vwl_data_device_listener.data_offer(device, tmp_vwl_offer);
1393 }
1394}
1395
1396 static void
1397vwl_gen_data_device_listener_selection(
1398 void *data,
1399 void *offer_proxy,
1400 wayland_selection_T selection,
1401 vwl_data_protocol_T protocol)
1402{
1403 if (tmp_vwl_offer == NULL)
1404 {
1405 // Memory allocation failed or selection cleared (data_offer is never
1406 // sent when selection is cleared/empty).
1407 vwl_data_offer_T tmp = {
1408 .proxy = offer_proxy,
1409 .protocol = protocol
1410 };
1411
1412 vwl_data_offer_destroy(&tmp, FALSE);
1413
1414 // If offer proxy is NULL then we know the selection has been cleared.
1415 if (offer_proxy == NULL)
1416 vwl_data_device_listener.selection(data, NULL, selection);
1417 }
1418 else
1419 {
1420 vwl_data_device_listener.selection(data, tmp_vwl_offer, selection);
1421 tmp_vwl_offer = NULL;
1422 }
1423}
1424
1425// Boilerplate macros. Each just calls its respective generic callback.
1426//
1427#define VWL_FUNC_DATA_DEVICE_DATA_OFFER(device_name, offer_name) \
1428 static void device_name##_listener_data_offer( \
1429 void *data, struct device_name *device_proxy UNUSED, \
1430 struct offer_name *offer_proxy) \
1431{ \
1432 vwl_gen_data_device_listener_data_offer(data, offer_proxy); \
1433}
1434#define VWL_FUNC_DATA_DEVICE_SELECTION( \
1435 device_name, offer_name, type, selection_type, protocol) \
1436 static void device_name##_listener_##type( \
1437 void *data, struct device_name *device_proxy UNUSED, \
1438 struct offer_name *offer_proxy UNUSED) \
1439{ \
1440 vwl_gen_data_device_listener_selection( \
1441 data, offer_proxy, selection_type, protocol); \
1442}
1443#define VWL_FUNC_DATA_DEVICE_FINISHED(device_name) \
1444 static void device_name##_listener_finished( \
1445 void *data, struct device_name *device_proxy UNUSED) \
1446{ \
1447 vwl_data_device_listener.finished(data); \
1448}
1449#define VWL_FUNC_DATA_SOURCE_SEND(source_name) \
1450 static void source_name##_listener_send(void *data, \
1451 struct source_name *source_proxy UNUSED, \
1452 const char *mime_type, int fd) \
1453{ \
1454 vwl_data_source_listener.send(data, mime_type, fd); \
1455}
1456#define VWL_FUNC_DATA_SOURCE_CANCELLED(source_name) \
1457 static void source_name##_listener_cancelled(void *data, \
1458 struct source_name *source_proxy UNUSED) \
1459{ \
1460 vwl_data_source_listener.cancelled(data); \
1461}
1462#define VWL_FUNC_DATA_OFFER_OFFER(offer_name) \
1463 static void offer_name##_listener_offer(void *data, \
1464 struct offer_name *offer_proxy UNUSED, \
1465 const char *mime_type) \
1466{ \
1467 vwl_data_offer_listener.offer(data, mime_type); \
1468}
1469
1470VWL_FUNC_DATA_DEVICE_DATA_OFFER(
1471 ext_data_control_device_v1, ext_data_control_offer_v1)
1472VWL_FUNC_DATA_DEVICE_DATA_OFFER(
1473 zwlr_data_control_device_v1, zwlr_data_control_offer_v1)
1474VWL_FUNC_DATA_DEVICE_DATA_OFFER(wl_data_device, wl_data_offer)
1475VWL_FUNC_DATA_DEVICE_DATA_OFFER(
1476 zwp_primary_selection_device_v1, zwp_primary_selection_offer_v1)
1477
1478VWL_FUNC_DATA_DEVICE_SELECTION(
1479 ext_data_control_device_v1, ext_data_control_offer_v1,
1480 selection, WAYLAND_SELECTION_REGULAR, VWL_DATA_PROTOCOL_EXT)
1481VWL_FUNC_DATA_DEVICE_SELECTION(
1482 zwlr_data_control_device_v1, zwlr_data_control_offer_v1,
1483 selection, WAYLAND_SELECTION_REGULAR, VWL_DATA_PROTOCOL_WLR)
1484VWL_FUNC_DATA_DEVICE_SELECTION(
1485 wl_data_device, wl_data_offer, selection,
1486 WAYLAND_SELECTION_REGULAR, VWL_DATA_PROTOCOL_CORE)
1487
1488VWL_FUNC_DATA_DEVICE_SELECTION(
1489 ext_data_control_device_v1, ext_data_control_offer_v1,
1490 primary_selection, WAYLAND_SELECTION_PRIMARY, VWL_DATA_PROTOCOL_EXT)
1491VWL_FUNC_DATA_DEVICE_SELECTION(
1492 zwlr_data_control_device_v1, zwlr_data_control_offer_v1,
1493 primary_selection, WAYLAND_SELECTION_PRIMARY, VWL_DATA_PROTOCOL_WLR)
1494VWL_FUNC_DATA_DEVICE_SELECTION(
1495 zwp_primary_selection_device_v1, zwp_primary_selection_offer_v1,
1496 primary_selection, WAYLAND_SELECTION_PRIMARY, VWL_DATA_PROTOCOL_PRIMARY)
1497
1498VWL_FUNC_DATA_DEVICE_FINISHED(ext_data_control_device_v1)
1499VWL_FUNC_DATA_DEVICE_FINISHED(zwlr_data_control_device_v1)
1500
1501VWL_FUNC_DATA_SOURCE_SEND(ext_data_control_source_v1)
1502VWL_FUNC_DATA_SOURCE_SEND(zwlr_data_control_source_v1)
1503VWL_FUNC_DATA_SOURCE_SEND(wl_data_source)
1504VWL_FUNC_DATA_SOURCE_SEND(zwp_primary_selection_source_v1)
1505
1506VWL_FUNC_DATA_SOURCE_CANCELLED(ext_data_control_source_v1)
1507VWL_FUNC_DATA_SOURCE_CANCELLED(zwlr_data_control_source_v1)
1508VWL_FUNC_DATA_SOURCE_CANCELLED(wl_data_source)
1509VWL_FUNC_DATA_SOURCE_CANCELLED(zwp_primary_selection_source_v1)
1510
1511VWL_FUNC_DATA_OFFER_OFFER(ext_data_control_offer_v1)
1512VWL_FUNC_DATA_OFFER_OFFER(zwlr_data_control_offer_v1)
1513VWL_FUNC_DATA_OFFER_OFFER(wl_data_offer)
1514VWL_FUNC_DATA_OFFER_OFFER(zwp_primary_selection_offer_v1)
1515
1516// Listener handlers
1517
1518// DATA DEVICES
1519struct zwlr_data_control_device_v1_listener
1520zwlr_data_control_device_v1_listener = {
1521 .data_offer = zwlr_data_control_device_v1_listener_data_offer,
1522 .selection = zwlr_data_control_device_v1_listener_selection,
1523 .primary_selection = zwlr_data_control_device_v1_listener_primary_selection,
1524 .finished = zwlr_data_control_device_v1_listener_finished
1525};
1526
1527struct ext_data_control_device_v1_listener
1528ext_data_control_device_v1_listener = {
1529 .data_offer = ext_data_control_device_v1_listener_data_offer,
1530 .selection = ext_data_control_device_v1_listener_selection,
1531 .primary_selection = ext_data_control_device_v1_listener_primary_selection,
1532 .finished = ext_data_control_device_v1_listener_finished
1533};
1534
1535struct wl_data_device_listener wl_data_device_listener = {
1536 .data_offer = wl_data_device_listener_data_offer,
1537 .selection = wl_data_device_listener_selection,
1538};
1539
1540struct zwp_primary_selection_device_v1_listener
1541zwp_primary_selection_device_v1_listener = {
1542 .selection = zwp_primary_selection_device_v1_listener_primary_selection,
1543 .data_offer = zwp_primary_selection_device_v1_listener_data_offer
1544};
1545
1546// DATA SOURCES
1547struct zwlr_data_control_source_v1_listener
1548zwlr_data_control_source_v1_listener = {
1549 .send = zwlr_data_control_source_v1_listener_send,
1550 .cancelled = zwlr_data_control_source_v1_listener_cancelled
1551};
1552
1553struct ext_data_control_source_v1_listener
1554ext_data_control_source_v1_listener = {
1555 .send = ext_data_control_source_v1_listener_send,
1556 .cancelled = ext_data_control_source_v1_listener_cancelled
1557};
1558
1559struct wl_data_source_listener wl_data_source_listener = {
1560 .send = wl_data_source_listener_send,
1561 .cancelled = wl_data_source_listener_cancelled
1562};
1563
1564struct zwp_primary_selection_source_v1_listener
1565zwp_primary_selection_source_v1_listener = {
1566 .send = zwp_primary_selection_source_v1_listener_send,
1567 .cancelled = zwp_primary_selection_source_v1_listener_cancelled,
1568};
1569
1570// OFFERS
1571struct zwlr_data_control_offer_v1_listener
1572zwlr_data_control_offer_v1_listener = {
1573 .offer = zwlr_data_control_offer_v1_listener_offer
1574};
1575
1576struct ext_data_control_offer_v1_listener
1577ext_data_control_offer_v1_listener = {
1578 .offer = ext_data_control_offer_v1_listener_offer
1579};
1580
1581struct wl_data_offer_listener wl_data_offer_listener = {
1582 .offer = wl_data_offer_listener_offer
1583};
1584
1585struct zwp_primary_selection_offer_v1_listener
1586zwp_primary_selection_offer_v1_listener = {
1587 .offer = zwp_primary_selection_offer_v1_listener_offer
1588};
1589
1590// `type` is also used as the user data
1591#define VWL_CODE_DATA_OBJECT_ADD_LISTENER(type) \
1592do { \
1593 if (type->proxy == NULL) \
1594 return; \
1595 type->data = data; \
1596 switch (type->protocol) \
1597 { \
1598 case VWL_DATA_PROTOCOL_WLR: \
1599 zwlr_data_control_##type##_v1_add_listener( type->proxy, \
1600 &zwlr_data_control_##type##_v1_listener, type); \
1601 break; \
1602 case VWL_DATA_PROTOCOL_EXT: \
1603 ext_data_control_##type##_v1_add_listener(type->proxy, \
1604 &ext_data_control_##type##_v1_listener, type); \
1605 break; \
1606 case VWL_DATA_PROTOCOL_CORE: \
1607 wl_data_##type##_add_listener(type->proxy, \
1608 &wl_data_##type##_listener, type); \
1609 break; \
1610 case VWL_DATA_PROTOCOL_PRIMARY: \
1611 zwp_primary_selection_##type##_v1_add_listener(type->proxy, \
1612 &zwp_primary_selection_##type##_v1_listener, type); \
1613 break; \
1614 default: \
1615 break; \
1616 } \
1617} while (0)
1618
1619 static void
1620vwl_data_device_add_listener(vwl_data_device_T *device, void *data)
1621{
1622 VWL_CODE_DATA_OBJECT_ADD_LISTENER(device);
1623}
1624
1625 static void
1626vwl_data_source_add_listener(vwl_data_source_T *source, void *data)
1627{
1628 VWL_CODE_DATA_OBJECT_ADD_LISTENER(source);
1629}
1630
1631 static void
1632vwl_data_offer_add_listener(vwl_data_offer_T *offer, void *data)
1633{
1634 VWL_CODE_DATA_OBJECT_ADD_LISTENER(offer);
1635}
1636
1637/*
1638 * Sets the selection using the given data device with the given selection. If
1639 * the device does not support the selection then nothing happens. For data
1640 * control protocols the serial argument is ignored.
1641 */
1642 static void
1643vwl_data_device_set_selection(
1644 vwl_data_device_T *device,
1645 vwl_data_source_T *source,
1646 uint32_t serial,
1647 wayland_selection_T selection)
1648{
1649 if (selection == WAYLAND_SELECTION_REGULAR)
1650 {
1651 switch (device->protocol)
1652 {
1653 case VWL_DATA_PROTOCOL_WLR:
1654 zwlr_data_control_device_v1_set_selection(
1655 device->proxy, source->proxy);
1656 break;
1657 case VWL_DATA_PROTOCOL_EXT:
1658 ext_data_control_device_v1_set_selection(
1659 device->proxy, source->proxy);
1660 break;
1661 case VWL_DATA_PROTOCOL_CORE:
1662 wl_data_device_set_selection(
1663 device->proxy, source->proxy, serial);
1664 break;
1665 default:
1666 break;
1667 }
1668 }
1669 else if (selection == WAYLAND_SELECTION_PRIMARY)
1670 {
1671 switch (device->protocol)
1672 {
1673 case VWL_DATA_PROTOCOL_WLR:
1674 zwlr_data_control_device_v1_set_primary_selection(
1675 device->proxy, source->proxy);
1676 break;
1677 case VWL_DATA_PROTOCOL_EXT:
1678 ext_data_control_device_v1_set_primary_selection(
1679 device->proxy, source->proxy);
1680 break;
1681 case VWL_DATA_PROTOCOL_PRIMARY:
1682 zwp_primary_selection_device_v1_set_selection(
1683 device->proxy, source->proxy, serial);
1684 break;
1685 default:
1686 break;
1687 }
1688 }
1689}
1690
1691/*
1692 * Start receiving data from offer object, which sends the given fd to the
1693 * source client to write into.
1694 */
1695 static void
1696vwl_data_offer_receive(vwl_data_offer_T *offer, const char *mime_type, int fd)
1697{
1698 switch (offer->protocol)
1699 {
1700 case VWL_DATA_PROTOCOL_WLR:
1701 zwlr_data_control_offer_v1_receive(offer->proxy, mime_type, fd);
1702 break;
1703 case VWL_DATA_PROTOCOL_EXT:
1704 ext_data_control_offer_v1_receive(offer->proxy, mime_type, fd);
1705 break;
1706 case VWL_DATA_PROTOCOL_CORE:
1707 wl_data_offer_receive(offer->proxy, mime_type, fd);
1708 break;
1709 case VWL_DATA_PROTOCOL_PRIMARY:
1710 zwp_primary_selection_offer_v1_receive(offer->proxy, mime_type, fd);
1711 break;
1712 default:
1713 break;
1714 }
1715}
1716
1717#define SET_MANAGER(manager_name, protocol_enum, focus) \
1718 do { \
1719 manager->proxy = vwl_gobjects.manager_name; \
1720 manager->protocol = protocol_enum; \
1721 return focus; \
1722 } while (0)
1723
1724/*
1725 * Get a data device manager that supports the given selection. If none if found
1726 * then the manager protocol is set to VWL_DATA_PROTOCOL_NONE. TRUE is returned
1727 * if the given data device manager requires focus to work else FALSE.
1728 */
1729 static int
1730vwl_get_data_device_manager(
1731 vwl_data_device_manager_T *manager,
1732 wayland_selection_T selection)
1733{
1734 // Prioritize data control protocols first then try using the focus steal
1735 // method with the core protocol data objects.
1736 if (force_fs)
1737 goto focus_steal;
1738
1739 // Ext data control protocol supports both selections, try it first
1740 if (vwl_gobjects.ext_data_control_manager_v1 != NULL)
1741 SET_MANAGER(ext_data_control_manager_v1, VWL_DATA_PROTOCOL_EXT, FALSE);
1742 if (vwl_gobjects.zwlr_data_control_manager_v1 != NULL)
1743 {
1744 int ver = zwlr_data_control_manager_v1_get_version(
1745 vwl_gobjects.zwlr_data_control_manager_v1);
1746
1747 // version 2 or greater supports the primary selection
1748 if ((selection == WAYLAND_SELECTION_PRIMARY && ver >= 2)
1749 || selection == WAYLAND_SELECTION_REGULAR)
1750 SET_MANAGER(zwlr_data_control_manager_v1,
1751 VWL_DATA_PROTOCOL_WLR, FALSE);
1752 }
1753
1754focus_steal:
1755 if (vwl_focus_stealing_available())
1756 {
1757 if (vwl_gobjects.wl_data_device_manager != NULL
1758 && selection == WAYLAND_SELECTION_REGULAR)
1759 SET_MANAGER(wl_data_device_manager, VWL_DATA_PROTOCOL_CORE, TRUE);
1760
1761 else if (vwl_gobjects.zwp_primary_selection_device_manager_v1 != NULL
1762 && selection == WAYLAND_SELECTION_PRIMARY)
1763 SET_MANAGER(zwp_primary_selection_device_manager_v1,
1764 VWL_DATA_PROTOCOL_PRIMARY, TRUE);
1765 }
1766
1767 manager->protocol = VWL_DATA_PROTOCOL_NONE;
1768
1769 return FALSE;
1770}
1771
1772/*
1773 * Get a data device that manages the given seat's selection.
1774 */
1775 static void
1776vwl_get_data_device(
1777 vwl_data_device_manager_T *manager,
1778 vwl_seat_T *seat,
1779 vwl_data_device_T *device)
1780{
1781 switch (manager->protocol)
1782 {
1783 case VWL_DATA_PROTOCOL_WLR:
1784 device->proxy =
1785 zwlr_data_control_manager_v1_get_data_device(
1786 manager->proxy, seat->proxy);
1787 break;
1788 case VWL_DATA_PROTOCOL_EXT:
1789 device->proxy =
1790 ext_data_control_manager_v1_get_data_device(
1791 manager->proxy, seat->proxy);
1792 break;
1793 case VWL_DATA_PROTOCOL_CORE:
1794 device->proxy = wl_data_device_manager_get_data_device(
1795 manager->proxy, seat->proxy);
1796 break;
1797 case VWL_DATA_PROTOCOL_PRIMARY:
1798 device->proxy = zwp_primary_selection_device_manager_v1_get_device(
1799 manager->proxy, seat->proxy);
1800 break;
1801 default:
1802 device->protocol = VWL_DATA_PROTOCOL_NONE;
1803 return;
1804 }
1805 device->protocol = manager->protocol;
1806}
1807
1808/*
1809 * Create a data source
1810 */
1811 static void
1812vwl_create_data_source(
1813 vwl_data_device_manager_T *manager,
1814 vwl_data_source_T *source)
1815{
1816 switch (manager->protocol)
1817 {
1818 case VWL_DATA_PROTOCOL_WLR:
1819 source->proxy =
1820 zwlr_data_control_manager_v1_create_data_source(manager->proxy);
1821 break;
1822 case VWL_DATA_PROTOCOL_EXT:
1823 source->proxy =
1824 ext_data_control_manager_v1_create_data_source(manager->proxy);
1825 break;
1826 case VWL_DATA_PROTOCOL_CORE:
1827 source->proxy =
1828 wl_data_device_manager_create_data_source(manager->proxy);
1829 break;
1830 case VWL_DATA_PROTOCOL_PRIMARY:
1831 source->proxy =
1832 zwp_primary_selection_device_manager_v1_create_source(
1833 manager->proxy);
1834 break;
1835 default:
1836 source->protocol = VWL_DATA_PROTOCOL_NONE;
1837 return;
1838 }
1839 source->protocol = manager->protocol;
1840}
1841
1842/*
1843 * Offer a new mime type to be advertised by us to other clients.
1844 */
1845 static void
1846vwl_data_source_offer(vwl_data_source_T *source, const char *mime_type)
1847{
1848 switch (source->protocol)
1849 {
1850 case VWL_DATA_PROTOCOL_WLR:
1851 zwlr_data_control_source_v1_offer(source->proxy, mime_type);
1852 break;
1853 case VWL_DATA_PROTOCOL_EXT:
1854 ext_data_control_source_v1_offer(source->proxy, mime_type);
1855 break;
1856 case VWL_DATA_PROTOCOL_CORE:
1857 wl_data_source_offer(source->proxy, mime_type);
1858 break;
1859 case VWL_DATA_PROTOCOL_PRIMARY:
1860 zwp_primary_selection_source_v1_offer(source->proxy, mime_type);
1861 break;
1862 default:
1863 break;
1864 }
1865}
1866
1867/*
1868 * Free the mime types grow arrays in the given clip_sel struct.
1869 */
1870 static void
1871vwl_clipboard_free_mime_types(vwl_clipboard_selection_T *clip_sel)
1872{
1873 // Don't want to be double freeing
1874 if (clip_sel->mime_types.ga_data == clip_sel->tmp_mime_types.ga_data)
1875 {
1876 ga_clear_strings(&clip_sel->mime_types);
1877 ga_init(&vwl_clipboard.primary.tmp_mime_types);
1878 }
1879 else
1880 {
1881 ga_clear_strings(&clip_sel->mime_types);
1882 ga_clear_strings(&clip_sel->tmp_mime_types);
1883 }
1884}
1885
1886/*
1887 * Setup required objects to interact with wayland selections/clipboard on given
1888 * seat. Returns OK on success and FAIL on failure.
1889 */
1890 int
1891wayland_cb_init(const char *seat)
1892{
1893 vwl_clipboard.seat = vwl_get_seat(seat);
1894
1895 if (vwl_clipboard.seat == NULL)
1896 return FAIL;
1897
1898 // Get data device managers for each selection. If there wasn't any manager
1899 // that could be found that supports the given selection, then it will be
1900 // unavailable.
1901 vwl_clipboard.regular.requires_focus = vwl_get_data_device_manager(
1902 &vwl_clipboard.regular.manager,
1903 WAYLAND_SELECTION_REGULAR);
1904 vwl_clipboard.primary.requires_focus = vwl_get_data_device_manager(
1905 &vwl_clipboard.primary.manager,
1906 WAYLAND_SELECTION_PRIMARY);
1907
1908 // Initialize shm pool and buffer if core data protocol is available
1909 if (vwl_focus_stealing_available() &&
1910 (vwl_clipboard.regular.requires_focus ||
1911 vwl_clipboard.primary.requires_focus))
1912 vwl_clipboard.fs_buffer = vwl_init_buffer_store(1, 1);
1913
1914 // Get data devices for each selection. If one of the above function calls
1915 // results in an unavailable manager, then the device coming from it will
1916 // have its protocol set to VWL_DATA_PROTOCOL_NONE.
1917 vwl_get_data_device(
1918 &vwl_clipboard.regular.manager,
1919 vwl_clipboard.seat,
1920 &vwl_clipboard.regular.device);
1921 vwl_get_data_device(
1922 &vwl_clipboard.primary.manager,
1923 vwl_clipboard.seat,
1924 &vwl_clipboard.primary.device);
1925
1926 // Initialize grow arrays for the offer mime types.
1927 // I find most applications to have below 10 mime types that they offer.
1928 ga_init2(&vwl_clipboard.regular.tmp_mime_types, sizeof(char*), 10);
1929 ga_init2(&vwl_clipboard.primary.tmp_mime_types, sizeof(char*), 10);
1930
1931 // We dont need to use ga_init2 because tmp_mime_types will be copied over
1932 // to mime_types anyways.
1933 ga_init(&vwl_clipboard.regular.mime_types);
1934 ga_init(&vwl_clipboard.primary.mime_types);
1935
1936 // Start listening for data offers/new selections. Don't do anything when we
1937 // get a new data offer other than saving the mime types and saving the data
1938 // offer. Then when we want the data we use the saved data offer to receive
1939 // data from it along with the saved mime_types. For each new selection just
1940 // destroy the previous offer/free mime_types, if any.
1941 vwl_data_device_add_listener(
1942 &vwl_clipboard.regular.device,
1943 &vwl_clipboard.regular);
1944 vwl_data_device_add_listener(
1945 &vwl_clipboard.primary.device,
1946 &vwl_clipboard.primary);
1947
1948 if (vwl_display_roundtrip(&vwl_display) == FAIL)
1949 {
1950 wayland_cb_uninit();
1951 return FAIL;
1952 }
1953 clip_init(TRUE);
1954
1955 return OK;
1956}
1957
1958/*
1959 * Free up resources used for Wayland selections. Does not destroy global
1960 * objects such as data device managers.
1961 */
1962 void
1963wayland_cb_uninit(void)
1964{
1965 if (vwl_clipboard.fs_buffer != NULL)
1966 {
1967 vwl_destroy_buffer_store(vwl_clipboard.fs_buffer);
1968 vwl_clipboard.fs_buffer = NULL;
1969 }
1970
1971 // Destroy the current offer if it exists
1972 vwl_data_offer_destroy(vwl_clipboard.regular.offer, TRUE);
1973 vwl_data_offer_destroy(vwl_clipboard.primary.offer, TRUE);
1974
1975 // Destroy any devices or sources
1976 vwl_data_device_destroy(&vwl_clipboard.regular.device, FALSE);
1977 vwl_data_device_destroy(&vwl_clipboard.primary.device, FALSE);
1978 vwl_data_source_destroy(&vwl_clipboard.regular.source, FALSE);
1979 vwl_data_source_destroy(&vwl_clipboard.primary.source, FALSE);
1980
1981 // Free mime types
1982 vwl_clipboard_free_mime_types(&vwl_clipboard.regular);
1983 vwl_clipboard_free_mime_types(&vwl_clipboard.primary);
1984
1985 vwl_display_flush(&vwl_display);
1986
1987 vim_memset(&vwl_clipboard, 0, sizeof(vwl_clipboard));
1988 vwl_clipboard.regular.selection = WAYLAND_SELECTION_REGULAR;
1989 vwl_clipboard.primary.selection = WAYLAND_SELECTION_PRIMARY;
1990}
1991
1992/*
1993 * If the given selection can be used.
1994 */
1995 static int
1996vwl_clipboard_selection_is_ready(vwl_clipboard_selection_T *clip_sel)
1997{
1998 return clip_sel->manager.protocol != VWL_DATA_PROTOCOL_NONE &&
1999 clip_sel->device.protocol != VWL_DATA_PROTOCOL_NONE;
2000}
2001
2002/*
2003 * Callback for data offer event. Start listening to the given offer immediately
2004 * in order to get mime types.
2005 */
2006 static void
2007vwl_data_device_listener_data_offer(
2008 vwl_data_device_T *device,
2009 vwl_data_offer_T *offer)
2010{
2011 vwl_clipboard_selection_T *clip_sel = device->data;
2012
2013 // Get mime types and save them so we can use them when we want to paste the
2014 // selection.
2015 if (clip_sel->source.proxy != NULL)
2016 // We own the selection, no point in getting mime types
2017 return;
2018
2019 vwl_data_offer_add_listener(offer, device->data);
2020}
2021
2022/*
2023 * Callback for offer event. Save each mime type given to be used later.
2024 */
2025 static void
2026vwl_data_offer_listener_offer(vwl_data_offer_T *offer, const char *mime_type)
2027{
2028 vwl_clipboard_selection_T *clip_sel = offer->data;
2029
2030 // Save string into temporary grow array, which will be finalized into the
2031 // actual grow array if the selection matches with the selection that the
2032 // device manages.
2033 ga_copy_string(&clip_sel->tmp_mime_types, (char_u*)mime_type);
2034}
2035
2036/*
2037 * Callback for selection event, for either the regular or primary selection.
2038 * Don't try receiving data from the offer, instead destroy the previous offer
2039 * if any and set the current offer to the given offer, along with the
2040 * respective mime types.
2041 */
2042 static void
2043vwl_data_device_listener_selection(
2044 vwl_data_device_T *device UNUSED,
2045 vwl_data_offer_T *offer,
2046 wayland_selection_T selection)
2047{
2048 vwl_clipboard_selection_T *clip_sel = device->data;
2049 vwl_data_offer_T *prev_offer = clip_sel->offer;
2050
2051 // Save offer if it selection and clip_sel match, else discard it
2052 if (clip_sel->selection == selection)
2053 clip_sel->offer = offer;
2054 else
2055 {
2056 // Example: selection event is for the primary selection but this device
2057 // is only for the regular selection, if so then just discard the offer
2058 // and tmp_mime_types.
2059 vwl_data_offer_destroy(offer, TRUE);
2060 tmp_vwl_offer = NULL;
2061 ga_clear_strings(&clip_sel->tmp_mime_types);
2062 return;
2063 }
2064
2065 // There are two cases when clip_sel->offer is NULL
2066 // 1. No one owns the selection
2067 // 2. We own the selection (we'll just access the register directly)
2068 if (offer == NULL)
2069 {
2070 // Selection cleared/empty
2071 ga_clear_strings(&clip_sel->tmp_mime_types);
2072 clip_sel->offer = NULL;
2073 goto exit;
2074 }
2075 else if (clip_sel->source.proxy != NULL)
2076 {
2077 // We own the selection, ignore it
2078 vwl_data_offer_destroy(offer, TRUE);
2079 ga_clear_strings(&clip_sel->tmp_mime_types);
2080 clip_sel->offer = NULL;
2081 goto exit;
2082 }
2083
2084exit:
2085 // Destroy previous offer if any
2086 vwl_data_offer_destroy(prev_offer, TRUE);
2087 ga_clear_strings(&clip_sel->mime_types);
2088
2089 // Copy the grow array over
2090 clip_sel->mime_types = clip_sel->tmp_mime_types;
2091
2092 // Clear tmp_mime_types so next data_offer doesn't try to resize/grow it
2093 // (Don't free it though using ga_clear() because mime_types->ga_data is the
2094 // same pointer)r
2095 if (clip_sel->offer != NULL)
2096 ga_init(&clip_sel->tmp_mime_types);
2097}
2098
2099/*
2100 * Callback for finished event. Destroy device and all related objects/resources
2101 * such as offers and mime types.
2102 */
2103 static void
2104vwl_data_device_listener_finished(vwl_data_device_T *device)
2105{
2106 vwl_clipboard_selection_T *clip_sel = device->data;
2107
2108 vwl_data_device_destroy(device, FALSE);
2109 vwl_data_offer_destroy(clip_sel->offer, TRUE);
2110 vwl_data_source_destroy(&clip_sel->source, FALSE);
2111 vwl_clipboard_free_mime_types(clip_sel);
2112}
2113
2114/*
2115 * Return a pointer to a grow array of mime types that the current offer
2116 * supports sending. If the returned garray has NULL for ga_data or a ga_len of
2117 * 0, then the selection is cleared. If focus stealing is required, a surface
2118 * will be created to steal focus first.
2119 */
2120 garray_T *
2121wayland_cb_get_mime_types(wayland_selection_T selection)
2122{
2123 vwl_clipboard_selection_T *clip_sel;
2124
2125 if (selection == WAYLAND_SELECTION_REGULAR)
2126 clip_sel = &vwl_clipboard.regular;
2127 else if (selection == WAYLAND_SELECTION_PRIMARY)
2128 clip_sel = &vwl_clipboard.primary;
2129 else
2130 return NULL;
2131
2132 if (clip_sel->requires_focus)
2133 {
2134 // We don't care about the on_focus callback since once we gain focus
2135 // the data offer events will come immediately.
2136 if (vwl_init_fs_surface(vwl_clipboard.seat,
2137 vwl_clipboard.fs_buffer, NULL, NULL) == FAIL)
2138 return NULL;
2139 }
2140 else if (vwl_display_roundtrip(&vwl_display) == FAIL)
2141 return NULL;
2142
2143 return &clip_sel->mime_types;
2144}
2145
2146/*
2147 * Receive data from the given selection, and return the fd to read data from.
2148 * On failure -1 is returned.
2149 */
2150 int
2151wayland_cb_receive_data(const char *mime_type, wayland_selection_T selection)
2152{
2153 vwl_clipboard_selection_T *clip_sel;
2154
2155 // Create pipe that source client will write to
2156 int fds[2];
2157
2158 if (selection == WAYLAND_SELECTION_REGULAR)
2159 clip_sel = &vwl_clipboard.regular;
2160 else if (selection == WAYLAND_SELECTION_PRIMARY)
2161 clip_sel = &vwl_clipboard.primary;
2162 else
2163 return -1;
2164
2165 if (!wayland_client_is_connected(FALSE) ||
2166 !vwl_clipboard_selection_is_ready(clip_sel))
2167 return -1;
2168
2169 if (clip_sel->offer == NULL || clip_sel->offer->proxy == NULL)
2170 return -1;
2171
2172 if (pipe(fds) == -1)
2173 return -1;
2174
2175 vwl_data_offer_receive(clip_sel->offer, mime_type, fds[1]);
2176
2177 close(fds[1]); // Close before we read data so that when the source client
2178 // closes their end we receive an EOF.
2179
2180 if (vwl_display_flush(&vwl_display) == OK)
2181 return fds[0];
2182
2183 close(fds[0]);
2184
2185 return -1;
2186}
2187
2188/*
2189 * Callback for send event. Just call the user callback which will handle it
2190 * and do the writing stuff.
2191 */
2192 static void
2193vwl_data_source_listener_send(
2194 vwl_data_source_T *source,
2195 const char *mime_type,
2196 int32_t fd)
2197{
2198 vwl_clipboard_selection_T *clip_sel = source->data;
2199
2200 if (clip_sel->send_cb != NULL)
2201 clip_sel->send_cb(mime_type, fd, clip_sel->selection);
2202 close(fd);
2203}
2204
2205/*
2206 * Callback for cancelled event, just call the user callback.
2207 */
2208 static void
2209vwl_data_source_listener_cancelled(vwl_data_source_T *source)
2210{
2211 vwl_clipboard_selection_T *clip_sel = source->data;
2212
2213 if (clip_sel->send_cb != NULL)
2214 clip_sel->cancelled_cb(clip_sel->selection);
2215 vwl_data_source_destroy(source, FALSE);
2216}
2217
2218/*
2219 * Set the selection when we gain focus
2220 */
2221 static void
2222vwl_on_focus_set_selection(void *data, uint32_t serial)
2223{
2224 vwl_clipboard_selection_T *clip_sel = data;
2225
2226 vwl_data_device_set_selection(
2227 &clip_sel->device,
2228 &clip_sel->source,
2229 serial,
2230 clip_sel->selection);
2231 vwl_display_roundtrip(&vwl_display);
2232}
2233
2234/*
2235 * Become the given selection's owner, and advertise to other clients the mime
2236 * types found in mime_types array. Returns FAIL on failure and OK on success.
2237 */
2238 int
2239wayland_cb_own_selection(
2240 wayland_cb_send_data_func_T send_cb,
2241 wayland_cb_selection_cancelled_func_T cancelled_cb,
2242 const char **mime_types,
2243 int len,
2244 wayland_selection_T selection)
2245{
2246 vwl_clipboard_selection_T *clip_sel;
2247
2248 if (selection == WAYLAND_SELECTION_REGULAR)
2249 clip_sel = &vwl_clipboard.regular;
2250 else if (selection == WAYLAND_SELECTION_PRIMARY)
2251 clip_sel = &vwl_clipboard.primary;
2252 else
2253 return FAIL;
2254
2255 if (clip_sel->source.proxy != NULL)
2256 // We already own the selection
2257 return OK;
2258
2259 if (!wayland_client_is_connected(FALSE) ||
2260 !vwl_clipboard_selection_is_ready(clip_sel))
2261 return FAIL;
2262
2263 clip_sel->send_cb = send_cb;
2264 clip_sel->cancelled_cb = cancelled_cb;
2265
2266 vwl_create_data_source(&clip_sel->manager, &clip_sel->source);
2267
2268 vwl_data_source_add_listener(&clip_sel->source, clip_sel);
2269
2270 // Advertise mime types
2271 for (int i = 0; i < len; i++)
2272 vwl_data_source_offer(&clip_sel->source, mime_types[i]);
2273
2274 if (clip_sel->requires_focus)
2275 {
2276 // Call set_selection later when we gain focus
2277 if (vwl_init_fs_surface(vwl_clipboard.seat, vwl_clipboard.fs_buffer,
2278 vwl_on_focus_set_selection, clip_sel) == FAIL)
2279 goto fail;
2280 }
2281 else
2282 {
2283 vwl_data_device_set_selection(&clip_sel->device,
2284 &clip_sel->source, 0, selection);
2285 if (vwl_display_roundtrip(&vwl_display) == FAIL)
2286 goto fail;
2287 }
2288
2289 return OK;
2290fail:
2291 vwl_data_source_destroy(&clip_sel->source, FALSE);
2292 return FAIL;
2293}
2294
2295/*
2296 * Disown the given selection, so that we are not the source client that other
2297 * clients receive data from.
2298 */
2299 void
2300wayland_cb_lose_selection(wayland_selection_T selection)
2301{
2302 if (selection == WAYLAND_SELECTION_REGULAR)
2303 vwl_data_source_destroy(&vwl_clipboard.regular.source, FALSE);
2304 else if (selection == WAYLAND_SELECTION_PRIMARY)
2305 vwl_data_source_destroy(&vwl_clipboard.primary.source, FALSE);
2306 vwl_display_flush(&vwl_display);
2307}
2308
2309/*
2310 * Return TRUE if the selection is owned by either us or another client.
2311 */
2312 int
2313wayland_cb_selection_is_owned(wayland_selection_T selection)
2314{
2315 vwl_display_roundtrip(&vwl_display);
2316
2317 if (selection == WAYLAND_SELECTION_REGULAR)
2318 return vwl_clipboard.regular.source.proxy != NULL
2319 || vwl_clipboard.regular.offer != NULL;
2320 else if (selection == WAYLAND_SELECTION_PRIMARY)
2321 return vwl_clipboard.primary.source.proxy != NULL
2322 || vwl_clipboard.primary.offer != NULL;
2323 else
2324 return FALSE;
2325}
2326
2327/*
2328 * Return TRUE if the wayland clipboard/selections are ready to use.
2329 */
2330 int
2331wayland_cb_is_ready(void)
2332{
2333 vwl_display_roundtrip(&vwl_display);
2334
2335 // Clipboard is ready if we have at least one selection available
2336 return wayland_client_is_connected(TRUE) &&
2337 (vwl_clipboard_selection_is_ready(&vwl_clipboard.regular) ||
2338 vwl_clipboard_selection_is_ready(&vwl_clipboard.primary));
2339}
2340
2341/*
2342 * Reload wayland clipboard, useful if changing seat.
2343 */
2344 int
2345wayland_cb_reload(void)
2346{
2347 // Lose any selections we own
2348 if (clipmethod == CLIPMETHOD_WAYLAND)
2349 {
2350 if (clip_star.owned)
2351 clip_lose_selection(&clip_star);
2352 if (clip_plus.owned)
2353 clip_lose_selection(&clip_plus);
2354 }
2355
2356 wayland_cb_uninit();
2357
2358 if (wayland_cb_init((char*)p_wse) == FAIL)
2359 return FAIL;
2360
2361 choose_clipmethod();
2362 return OK;
2363}
2364
2365#endif // FEAT_WAYLAND_CLIPBOARD
2366
2367static int wayland_ct_restore_count = 0;
2368
2369/*
2370 * Attempts to restore the Wayland display connection. Returns OK if display
2371 * connection was/is now valid, else FAIL if the display connection is invalid.
2372 */
2373 int
2374wayland_may_restore_connection(void)
2375{
2376 // No point if we still are already connected properly
2377 if (wayland_client_is_connected(TRUE))
2378 return OK;
2379
2380 // No point in restoring the connection if we are exiting or dying.
2381 if (exiting || v_dying || wayland_ct_restore_count <= 0)
2382 {
2383 wayland_set_display("");
2384 return FAIL;
2385 }
2386
2387 --wayland_ct_restore_count;
2388 wayland_uninit_client();
2389
2390 return wayland_init_client(wayland_display_name);
2391}
2392
2393/*
2394 * Disconnect then reconnect wayland connection, and update clipmethod.
2395 */
2396 void
2397ex_wlrestore(exarg_T *eap)
2398{
2399 char *display;
2400
2401 if (eap->arg == NULL || STRLEN(eap->arg) == 0)
2402 // Use current display name if none given
2403 display = wayland_display_name;
2404 else
2405 display = (char*)eap->arg;
2406
2407 // Return early if shebang is not passed, we are still connected, and if not
2408 // changing to a new wayland display.
2409 if (!eap->forceit && wayland_client_is_connected(TRUE) &&
2410 (display == wayland_display_name ||
2411 (wayland_display_name != NULL &&
2412 STRCMP(wayland_display_name, display) == 0)))
2413 return;
2414
2415#ifdef FEAT_WAYLAND_CLIPBOARD
2416 if (clipmethod == CLIPMETHOD_WAYLAND)
2417 {
2418 // Lose any selections we own
2419 if (clip_star.owned)
2420 clip_lose_selection(&clip_star);
2421 if (clip_plus.owned)
2422 clip_lose_selection(&clip_plus);
2423 }
2424#endif
2425
2426
2427 if (display != NULL)
2428 display = (char*)vim_strsave((char_u*)display);
2429
2430 wayland_uninit_client();
2431
2432 // Reset amount of available tries to reconnect the display to 5
2433 wayland_ct_restore_count = 5;
2434
2435 if (wayland_init_client(display) == OK)
2436 {
2437 smsg(_("restoring wayland display %s"), wayland_display_name);
2438
2439#ifdef FEAT_WAYLAND_CLIPBOARD
2440 wayland_cb_init((char*)p_wse);
2441#endif
2442 }
2443 else
2444 msg(_("failed restoring, lost connection to wayland display"));
2445
2446 vim_free(display);
2447
2448 choose_clipmethod();
2449}
2450
2451/*
2452 * Set wayland_display_name to display. Note that this allocate a copy of the
2453 * string, unless NULL is passed. If NULL is passed then v:wayland_display is
2454 * set to $WAYLAND_DISPLAY, but wayland_display_name is set to NULL.
2455 */
2456 static void
2457wayland_set_display(const char *display)
2458{
2459 if (display == NULL)
2460 display = (char*)mch_getenv((char_u*)"WAYLAND_DISPLAY");
2461 else if (display == wayland_display_name)
2462 // Don't want to be freeing vwl_display_strname then trying to copy it
2463 // after.
2464 goto exit;
2465
2466 if (display == NULL)
2467 // $WAYLAND_DISPLAY is not set
2468 display = "";
2469
2470 // Leave unchanged if display is empty (but not NULL)
2471 if (STRCMP(display, "") != 0)
2472 {
2473 vim_free(wayland_display_name);
2474 wayland_display_name = (char*)vim_strsave((char_u*)display);
2475 }
2476
2477exit:
2478#ifdef FEAT_EVAL
2479 set_vim_var_string(VV_WAYLAND_DISPLAY, (char_u*)display, -1);
2480#endif
2481}
2482
2483#endif // FEAT_WAYLAND