blob: 82771c4d4a1aab189673ab24b34550f71265cb4f [file] [log] [blame]
Kristian Høgsberg102bf032012-05-21 15:52:02 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg102bf032012-05-21 15:52:02 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg102bf032012-05-21 15:52:02 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg102bf032012-05-21 15:52:02 -040027
28#include <stdlib.h>
Kristian Høgsberg102bf032012-05-21 15:52:02 -040029#include <string.h>
30#include <unistd.h>
31#include <fcntl.h>
32
33#include "xwayland.h"
34
35static int
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070036writable_callback(int fd, uint32_t mask, void *data)
Kristian Høgsberg102bf032012-05-21 15:52:02 -040037{
38 struct weston_wm *wm = data;
39 unsigned char *property;
40 int len, remainder;
41
42 property = xcb_get_property_value(wm->property_reply);
43 remainder = xcb_get_property_value_length(wm->property_reply) -
44 wm->property_start;
45
46 len = write(fd, property + wm->property_start, remainder);
47 if (len == -1) {
48 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070049 wm->property_reply = NULL;
50 if (wm->property_source)
51 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020052 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040053 close(fd);
Martin Minarik6d118362012-06-07 18:01:59 +020054 weston_log("write error to target fd: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040055 return 1;
56 }
57
Martin Minarik6d118362012-06-07 18:01:59 +020058 weston_log("wrote %d (chunk size %d) of %d bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -040059 wm->property_start + len,
60 len, xcb_get_property_value_length(wm->property_reply));
61
62 wm->property_start += len;
63 if (len == remainder) {
64 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070065 wm->property_reply = NULL;
66 if (wm->property_source)
67 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020068 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040069
70 if (wm->incr) {
71 xcb_delete_property(wm->conn,
72 wm->selection_window,
73 wm->atom.wl_selection);
74 } else {
Martin Minarik6d118362012-06-07 18:01:59 +020075 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040076 close(fd);
77 }
78 }
79
80 return 1;
81}
82
83static void
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070084weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply)
85{
86 wm->property_start = 0;
87 wm->property_reply = reply;
88 writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm);
89
90 if (wm->property_reply)
91 wm->property_source =
92 wl_event_loop_add_fd(wm->server->loop,
93 wm->data_source_fd,
94 WL_EVENT_WRITABLE,
95 writable_callback, wm);
96}
97
98static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -040099weston_wm_get_incr_chunk(struct weston_wm *wm)
100{
101 xcb_get_property_cookie_t cookie;
102 xcb_get_property_reply_t *reply;
103
104 cookie = xcb_get_property(wm->conn,
105 0, /* delete */
106 wm->selection_window,
107 wm->atom.wl_selection,
108 XCB_GET_PROPERTY_TYPE_ANY,
109 0, /* offset */
110 0x1fffffff /* length */);
111
112 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
113
114 dump_property(wm, wm->atom.wl_selection, reply);
115
116 if (xcb_get_property_value_length(reply) > 0) {
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700117 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400118 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200119 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400120 close(wm->data_source_fd);
121 free(reply);
122 }
123}
124
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400125struct x11_data_source {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700126 struct weston_data_source base;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400127 struct weston_wm *wm;
128};
129
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400130static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700131data_source_accept(struct weston_data_source *source,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400132 uint32_t time, const char *mime_type)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400133{
134}
135
136static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700137data_source_send(struct weston_data_source *base,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400138 const char *mime_type, int32_t fd)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400139{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400140 struct x11_data_source *source = (struct x11_data_source *) base;
141 struct weston_wm *wm = source->wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400142
143 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
144 /* Get data for the utf8_string target */
145 xcb_convert_selection(wm->conn,
146 wm->selection_window,
147 wm->atom.clipboard,
148 wm->atom.utf8_string,
149 wm->atom.wl_selection,
150 XCB_TIME_CURRENT_TIME);
151
152 xcb_flush(wm->conn);
153
154 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
Kristian Høgsberg668fc0d2013-09-04 20:48:46 -0700155 wm->data_source_fd = fd;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400156 }
157}
158
159static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700160data_source_cancel(struct weston_data_source *source)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400161{
162}
163
164static void
165weston_wm_get_selection_targets(struct weston_wm *wm)
166{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400167 struct x11_data_source *source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400168 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400169 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400170 xcb_get_property_cookie_t cookie;
171 xcb_get_property_reply_t *reply;
172 xcb_atom_t *value;
173 char **p;
174 uint32_t i;
175
176 cookie = xcb_get_property(wm->conn,
177 1, /* delete */
178 wm->selection_window,
179 wm->atom.wl_selection,
180 XCB_GET_PROPERTY_TYPE_ANY,
181 0, /* offset */
182 4096 /* length */);
183
184 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
185
186 dump_property(wm, wm->atom.wl_selection, reply);
187
188 if (reply->type != XCB_ATOM_ATOM) {
189 free(reply);
190 return;
191 }
192
193 source = malloc(sizeof *source);
194 if (source == NULL)
195 return;
196
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500197 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400198 source->base.accept = data_source_accept;
199 source->base.send = data_source_send;
200 source->base.cancel = data_source_cancel;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400201 source->wm = wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400202
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400203 wl_array_init(&source->base.mime_types);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400204 value = xcb_get_property_value(reply);
205 for (i = 0; i < reply->value_len; i++) {
206 if (value[i] == wm->atom.utf8_string) {
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400207 p = wl_array_add(&source->base.mime_types, sizeof *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400208 if (p)
209 *p = strdup("text/plain;charset=utf-8");
210 }
211 }
212
213 compositor = wm->server->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400214 weston_seat_set_selection(seat, &source->base,
215 wl_display_next_serial(compositor->wl_display));
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400216
217 free(reply);
218}
219
220static void
221weston_wm_get_selection_data(struct weston_wm *wm)
222{
223 xcb_get_property_cookie_t cookie;
224 xcb_get_property_reply_t *reply;
225
226 cookie = xcb_get_property(wm->conn,
227 1, /* delete */
228 wm->selection_window,
229 wm->atom.wl_selection,
230 XCB_GET_PROPERTY_TYPE_ANY,
231 0, /* offset */
232 0x1fffffff /* length */);
233
234 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
235
236 if (reply->type == wm->atom.incr) {
237 dump_property(wm, wm->atom.wl_selection, reply);
238 wm->incr = 1;
239 free(reply);
240 } else {
241 dump_property(wm, wm->atom.wl_selection, reply);
242 wm->incr = 0;
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700243 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400244 }
245}
246
247static void
248weston_wm_handle_selection_notify(struct weston_wm *wm,
249 xcb_generic_event_t *event)
250{
251 xcb_selection_notify_event_t *selection_notify =
252 (xcb_selection_notify_event_t *) event;
253
254 if (selection_notify->property == XCB_ATOM_NONE) {
255 /* convert selection failed */
256 } else if (selection_notify->target == wm->atom.targets) {
257 weston_wm_get_selection_targets(wm);
258 } else {
259 weston_wm_get_selection_data(wm);
260 }
261}
262
263static const size_t incr_chunk_size = 64 * 1024;
264
265static void
266weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property)
267{
268 xcb_selection_notify_event_t selection_notify;
269
270 memset(&selection_notify, 0, sizeof selection_notify);
271 selection_notify.response_type = XCB_SELECTION_NOTIFY;
272 selection_notify.sequence = 0;
273 selection_notify.time = wm->selection_request.time;
274 selection_notify.requestor = wm->selection_request.requestor;
275 selection_notify.selection = wm->selection_request.selection;
276 selection_notify.target = wm->selection_request.target;
277 selection_notify.property = property;
278
279 xcb_send_event(wm->conn, 0, /* propagate */
280 wm->selection_request.requestor,
281 XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify);
282}
283
284static void
285weston_wm_send_targets(struct weston_wm *wm)
286{
287 xcb_atom_t targets[] = {
288 wm->atom.timestamp,
289 wm->atom.targets,
290 wm->atom.utf8_string,
291 /* wm->atom.compound_text, */
292 wm->atom.text,
293 /* wm->atom.string */
294 };
295
296 xcb_change_property(wm->conn,
297 XCB_PROP_MODE_REPLACE,
298 wm->selection_request.requestor,
299 wm->selection_request.property,
300 XCB_ATOM_ATOM,
301 32, /* format */
302 ARRAY_LENGTH(targets), targets);
303
304 weston_wm_send_selection_notify(wm, wm->selection_request.property);
305}
306
307static void
308weston_wm_send_timestamp(struct weston_wm *wm)
309{
310 xcb_change_property(wm->conn,
311 XCB_PROP_MODE_REPLACE,
312 wm->selection_request.requestor,
313 wm->selection_request.property,
314 XCB_ATOM_INTEGER,
315 32, /* format */
316 1, &wm->selection_timestamp);
317
318 weston_wm_send_selection_notify(wm, wm->selection_request.property);
319}
320
321static int
322weston_wm_flush_source_data(struct weston_wm *wm)
323{
324 int length;
325
326 xcb_change_property(wm->conn,
327 XCB_PROP_MODE_REPLACE,
328 wm->selection_request.requestor,
329 wm->selection_request.property,
330 wm->selection_target,
331 8, /* format */
332 wm->source_data.size,
333 wm->source_data.data);
334 wm->selection_property_set = 1;
335 length = wm->source_data.size;
336 wm->source_data.size = 0;
337
338 return length;
339}
340
341static int
342weston_wm_read_data_source(int fd, uint32_t mask, void *data)
343{
344 struct weston_wm *wm = data;
345 int len, current, available;
346 void *p;
347
348 current = wm->source_data.size;
349 if (wm->source_data.size < incr_chunk_size)
350 p = wl_array_add(&wm->source_data, incr_chunk_size);
351 else
352 p = (char *) wm->source_data.data + wm->source_data.size;
353 available = wm->source_data.alloc - current;
354
355 len = read(fd, p, available);
356 if (len == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200357 weston_log("read error from data source: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400358 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
359 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200360 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400361 close(fd);
362 wl_array_release(&wm->source_data);
363 }
364
Martin Minarik6d118362012-06-07 18:01:59 +0200365 weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400366 len, available, mask, len, (char *) p);
367
368 wm->source_data.size = current + len;
369 if (wm->source_data.size >= incr_chunk_size) {
370 if (!wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200371 weston_log("got %zu bytes, starting incr\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400372 wm->source_data.size);
373 wm->incr = 1;
374 xcb_change_property(wm->conn,
375 XCB_PROP_MODE_REPLACE,
376 wm->selection_request.requestor,
377 wm->selection_request.property,
378 wm->atom.incr,
379 32, /* format */
380 1, &incr_chunk_size);
381 wm->selection_property_set = 1;
382 wm->flush_property_on_delete = 1;
383 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200384 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400385 weston_wm_send_selection_notify(wm, wm->selection_request.property);
386 } else if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200387 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400388 "property delete\n", wm->source_data.size);
389
390 wm->flush_property_on_delete = 1;
391 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200392 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400393 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200394 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400395 "property deleted, seting new property\n",
396 wm->source_data.size);
397 weston_wm_flush_source_data(wm);
398 }
399 } else if (len == 0 && !wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200400 weston_log("non-incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400401 /* Non-incr transfer all done. */
402 weston_wm_flush_source_data(wm);
403 weston_wm_send_selection_notify(wm, wm->selection_request.property);
404 xcb_flush(wm->conn);
405 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200406 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400407 close(fd);
408 wl_array_release(&wm->source_data);
409 wm->selection_request.requestor = XCB_NONE;
410 } else if (len == 0 && wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200411 weston_log("incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400412
413 wm->flush_property_on_delete = 1;
414 if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400416 "property delete\n", wm->source_data.size);
417 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200418 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400419 "property deleted, seting new property\n",
420 wm->source_data.size);
421 weston_wm_flush_source_data(wm);
422 }
423 xcb_flush(wm->conn);
424 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200425 wm->property_source = NULL;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400426 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400427 wm->data_source_fd = -1;
428 close(fd);
429 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200430 weston_log("nothing happened, buffered the bytes\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400431 }
432
433 return 1;
434}
435
436static void
437weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type)
438{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700439 struct weston_data_source *source;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400440 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400441 int p[2];
442
443 if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200444 weston_log("pipe2 failed: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400445 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
446 return;
447 }
448
449 wl_array_init(&wm->source_data);
450 wm->selection_target = target;
451 wm->data_source_fd = p[0];
452 wm->property_source = wl_event_loop_add_fd(wm->server->loop,
453 wm->data_source_fd,
454 WL_EVENT_READABLE,
455 weston_wm_read_data_source,
456 wm);
457
Kristian Høgsberge3148752013-05-06 23:19:49 -0400458 source = seat->selection_data_source;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400459 source->send(source, mime_type, p[1]);
Kristian Høgsberg73bdc0c2013-09-04 22:32:50 -0700460 close(p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400461}
462
463static void
464weston_wm_send_incr_chunk(struct weston_wm *wm)
465{
466 int length;
467
Martin Minarik6d118362012-06-07 18:01:59 +0200468 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400469
470 wm->selection_property_set = 0;
471 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200472 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400473 wm->source_data.size);
474 wm->flush_property_on_delete = 0;
475 length = weston_wm_flush_source_data(wm);
476
477 if (wm->data_source_fd >= 0) {
478 wm->property_source =
479 wl_event_loop_add_fd(wm->server->loop,
480 wm->data_source_fd,
481 WL_EVENT_READABLE,
482 weston_wm_read_data_source,
483 wm);
484 } else if (length > 0) {
485 /* Transfer is all done, but queue a flush for
486 * the delete of the last chunk so we can set
487 * the 0 sized propert to signal the end of
488 * the transfer. */
489 wm->flush_property_on_delete = 1;
490 wl_array_release(&wm->source_data);
491 } else {
492 wm->selection_request.requestor = XCB_NONE;
493 }
494 }
495}
496
497static int
498weston_wm_handle_selection_property_notify(struct weston_wm *wm,
499 xcb_generic_event_t *event)
500{
501 xcb_property_notify_event_t *property_notify =
502 (xcb_property_notify_event_t *) event;
503
504 if (property_notify->window == wm->selection_window) {
505 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
506 property_notify->atom == wm->atom.wl_selection &&
507 wm->incr)
508 weston_wm_get_incr_chunk(wm);
509 return 1;
510 } else if (property_notify->window == wm->selection_request.requestor) {
511 if (property_notify->state == XCB_PROPERTY_DELETE &&
512 property_notify->atom == wm->selection_request.property &&
513 wm->incr)
514 weston_wm_send_incr_chunk(wm);
515 return 1;
516 }
517
518 return 0;
519}
520
521static void
522weston_wm_handle_selection_request(struct weston_wm *wm,
523 xcb_generic_event_t *event)
524{
525 xcb_selection_request_event_t *selection_request =
526 (xcb_selection_request_event_t *) event;
527
Martin Minarik6d118362012-06-07 18:01:59 +0200528 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400529 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200530 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400531 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200532 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400533 get_atom_name(wm->conn, selection_request->property));
534
535 wm->selection_request = *selection_request;
536 wm->incr = 0;
537 wm->flush_property_on_delete = 0;
538
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400539 if (selection_request->selection == wm->atom.clipboard_manager) {
540 /* The weston clipboard should already have grabbed
541 * the first target, so just send selection notify
542 * now. This isn't synchronized with the clipboard
543 * finishing getting the data, so there's a race here. */
544 weston_wm_send_selection_notify(wm, wm->selection_request.property);
545 return;
546 }
547
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400548 if (selection_request->target == wm->atom.targets) {
549 weston_wm_send_targets(wm);
550 } else if (selection_request->target == wm->atom.timestamp) {
551 weston_wm_send_timestamp(wm);
552 } else if (selection_request->target == wm->atom.utf8_string ||
553 selection_request->target == wm->atom.text) {
554 weston_wm_send_data(wm, wm->atom.utf8_string,
555 "text/plain;charset=utf-8");
556 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200557 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400558 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
559 }
560}
561
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700562static int
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400563weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
564 xcb_generic_event_t *event)
565{
566 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
567 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400568 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400569 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400570 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400571
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700572 if (xfixes_selection_notify->selection != wm->atom.clipboard)
573 return 0;
574
Martin Minarik6d118362012-06-07 18:01:59 +0200575 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400576 xfixes_selection_notify->owner);
577
Kristian Høgsberg80566732012-06-01 00:08:12 -0400578 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
579 if (wm->selection_owner != wm->selection_window) {
580 /* A real X client selection went away, not our
581 * proxy selection. Clear the wayland selection. */
582 compositor = wm->server->compositor;
583 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400584 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400585 }
586
587 wm->selection_owner = XCB_WINDOW_NONE;
588
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700589 return 1;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400590 }
591
592 wm->selection_owner = xfixes_selection_notify->owner;
593
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400594 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
595 * selection, so grab the actual timestamp here so we can
596 * answer TIMESTAMP conversion requests correctly. */
597 if (xfixes_selection_notify->owner == wm->selection_window) {
598 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200599 weston_log("our window, skipping\n");
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700600 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400601 }
602
603 wm->incr = 0;
604 xcb_convert_selection(wm->conn, wm->selection_window,
605 wm->atom.clipboard,
606 wm->atom.targets,
607 wm->atom.wl_selection,
608 xfixes_selection_notify->timestamp);
609
610 xcb_flush(wm->conn);
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700611
612 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400613}
614
615int
616weston_wm_handle_selection_event(struct weston_wm *wm,
617 xcb_generic_event_t *event)
618{
619 switch (event->response_type & ~0x80) {
620 case XCB_SELECTION_NOTIFY:
621 weston_wm_handle_selection_notify(wm, event);
622 return 1;
623 case XCB_PROPERTY_NOTIFY:
624 return weston_wm_handle_selection_property_notify(wm, event);
625 case XCB_SELECTION_REQUEST:
626 weston_wm_handle_selection_request(wm, event);
627 return 1;
628 }
629
630 switch (event->response_type - wm->xfixes->first_event) {
631 case XCB_XFIXES_SELECTION_NOTIFY:
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700632 return weston_wm_handle_xfixes_selection_notify(wm, event);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400633 }
634
635 return 0;
636}
637
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200638static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400639weston_wm_set_selection(struct wl_listener *listener, void *data)
640{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400641 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400642 struct weston_wm *wm =
643 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700644 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400645 const char **p, **end;
646 int has_text_plain = 0;
647
Kristian Høgsberg80566732012-06-01 00:08:12 -0400648 if (source == NULL) {
649 if (wm->selection_owner == wm->selection_window)
650 xcb_set_selection_owner(wm->conn,
651 XCB_ATOM_NONE,
652 wm->atom.clipboard,
653 wm->selection_timestamp);
654 return;
655 }
656
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400657 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400658 return;
659
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400660 p = source->mime_types.data;
661 end = (const char **)
662 ((char *) source->mime_types.data + source->mime_types.size);
663 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200664 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400665 if (strcmp(*p, "text/plain") == 0 ||
666 strcmp(*p, "text/plain;charset=utf-8") == 0)
667 has_text_plain = 1;
668 p++;
669 }
670
671 if (has_text_plain) {
672 xcb_set_selection_owner(wm->conn,
673 wm->selection_window,
674 wm->atom.clipboard,
675 XCB_TIME_CURRENT_TIME);
676 } else {
677 xcb_set_selection_owner(wm->conn,
678 XCB_ATOM_NONE,
679 wm->atom.clipboard,
680 XCB_TIME_CURRENT_TIME);
681 }
682}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400683
684void
685weston_wm_selection_init(struct weston_wm *wm)
686{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400687 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400688 uint32_t values[1], mask;
689
690 wm->selection_request.requestor = XCB_NONE;
691
692 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
693 wm->selection_window = xcb_generate_id(wm->conn);
694 xcb_create_window(wm->conn,
695 XCB_COPY_FROM_PARENT,
696 wm->selection_window,
697 wm->screen->root,
698 0, 0,
699 10, 10,
700 0,
701 XCB_WINDOW_CLASS_INPUT_OUTPUT,
702 wm->screen->root_visual,
703 XCB_CW_EVENT_MASK, values);
704
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400705 xcb_set_selection_owner(wm->conn,
706 wm->selection_window,
707 wm->atom.clipboard_manager,
708 XCB_TIME_CURRENT_TIME);
709
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400710 mask =
711 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
712 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
713 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
714 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
715 wm->atom.clipboard, mask);
716
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400717 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400718 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400719 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400720
721 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400722}