blob: 451b9153a2b7cc02b0d0c7e841735249a8bd46d3 [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"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070034#include "shared/helpers.h"
Kristian Høgsberg102bf032012-05-21 15:52:02 -040035
36static int
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070037writable_callback(int fd, uint32_t mask, void *data)
Kristian Høgsberg102bf032012-05-21 15:52:02 -040038{
39 struct weston_wm *wm = data;
40 unsigned char *property;
41 int len, remainder;
42
43 property = xcb_get_property_value(wm->property_reply);
44 remainder = xcb_get_property_value_length(wm->property_reply) -
45 wm->property_start;
46
47 len = write(fd, property + wm->property_start, remainder);
48 if (len == -1) {
49 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070050 wm->property_reply = NULL;
51 if (wm->property_source)
52 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020053 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040054 close(fd);
Martin Minarik6d118362012-06-07 18:01:59 +020055 weston_log("write error to target fd: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040056 return 1;
57 }
58
Martin Minarik6d118362012-06-07 18:01:59 +020059 weston_log("wrote %d (chunk size %d) of %d bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -040060 wm->property_start + len,
61 len, xcb_get_property_value_length(wm->property_reply));
62
63 wm->property_start += len;
64 if (len == remainder) {
65 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070066 wm->property_reply = NULL;
67 if (wm->property_source)
68 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020069 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040070
71 if (wm->incr) {
72 xcb_delete_property(wm->conn,
73 wm->selection_window,
74 wm->atom.wl_selection);
75 } else {
Martin Minarik6d118362012-06-07 18:01:59 +020076 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040077 close(fd);
78 }
79 }
80
81 return 1;
82}
83
84static void
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070085weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply)
86{
87 wm->property_start = 0;
88 wm->property_reply = reply;
89 writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm);
90
91 if (wm->property_reply)
92 wm->property_source =
93 wl_event_loop_add_fd(wm->server->loop,
94 wm->data_source_fd,
95 WL_EVENT_WRITABLE,
96 writable_callback, wm);
97}
98
99static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400100weston_wm_get_incr_chunk(struct weston_wm *wm)
101{
102 xcb_get_property_cookie_t cookie;
103 xcb_get_property_reply_t *reply;
104
105 cookie = xcb_get_property(wm->conn,
106 0, /* delete */
107 wm->selection_window,
108 wm->atom.wl_selection,
109 XCB_GET_PROPERTY_TYPE_ANY,
110 0, /* offset */
111 0x1fffffff /* length */);
112
113 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
114
115 dump_property(wm, wm->atom.wl_selection, reply);
116
117 if (xcb_get_property_value_length(reply) > 0) {
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700118 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400119 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200120 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400121 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400122 }
Bryce Harringtond3553c72015-06-30 21:35:43 -0700123
124 free(reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400125}
126
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400127struct x11_data_source {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700128 struct weston_data_source base;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400129 struct weston_wm *wm;
130};
131
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400132static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700133data_source_accept(struct weston_data_source *source,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400134 uint32_t time, const char *mime_type)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400135{
136}
137
138static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700139data_source_send(struct weston_data_source *base,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400140 const char *mime_type, int32_t fd)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400141{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400142 struct x11_data_source *source = (struct x11_data_source *) base;
143 struct weston_wm *wm = source->wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400144
145 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
146 /* Get data for the utf8_string target */
147 xcb_convert_selection(wm->conn,
148 wm->selection_window,
149 wm->atom.clipboard,
150 wm->atom.utf8_string,
151 wm->atom.wl_selection,
152 XCB_TIME_CURRENT_TIME);
153
154 xcb_flush(wm->conn);
155
156 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
Kristian Høgsberg668fc0d2013-09-04 20:48:46 -0700157 wm->data_source_fd = fd;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400158 }
159}
160
161static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700162data_source_cancel(struct weston_data_source *source)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400163{
164}
165
166static void
167weston_wm_get_selection_targets(struct weston_wm *wm)
168{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400169 struct x11_data_source *source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400170 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400171 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400172 xcb_get_property_cookie_t cookie;
173 xcb_get_property_reply_t *reply;
174 xcb_atom_t *value;
175 char **p;
176 uint32_t i;
177
178 cookie = xcb_get_property(wm->conn,
179 1, /* delete */
180 wm->selection_window,
181 wm->atom.wl_selection,
182 XCB_GET_PROPERTY_TYPE_ANY,
183 0, /* offset */
184 4096 /* length */);
185
186 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
187
188 dump_property(wm, wm->atom.wl_selection, reply);
189
190 if (reply->type != XCB_ATOM_ATOM) {
191 free(reply);
192 return;
193 }
194
195 source = malloc(sizeof *source);
Bryce Harringtond3553c72015-06-30 21:35:43 -0700196 if (source == NULL) {
197 free(reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400198 return;
Bryce Harringtond3553c72015-06-30 21:35:43 -0700199 }
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400200
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500201 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400202 source->base.accept = data_source_accept;
203 source->base.send = data_source_send;
204 source->base.cancel = data_source_cancel;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400205 source->wm = wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400206
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400207 wl_array_init(&source->base.mime_types);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400208 value = xcb_get_property_value(reply);
209 for (i = 0; i < reply->value_len; i++) {
210 if (value[i] == wm->atom.utf8_string) {
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400211 p = wl_array_add(&source->base.mime_types, sizeof *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400212 if (p)
213 *p = strdup("text/plain;charset=utf-8");
214 }
215 }
216
217 compositor = wm->server->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400218 weston_seat_set_selection(seat, &source->base,
219 wl_display_next_serial(compositor->wl_display));
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400220
221 free(reply);
222}
223
224static void
225weston_wm_get_selection_data(struct weston_wm *wm)
226{
227 xcb_get_property_cookie_t cookie;
228 xcb_get_property_reply_t *reply;
229
230 cookie = xcb_get_property(wm->conn,
231 1, /* delete */
232 wm->selection_window,
233 wm->atom.wl_selection,
234 XCB_GET_PROPERTY_TYPE_ANY,
235 0, /* offset */
236 0x1fffffff /* length */);
237
238 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
239
240 if (reply->type == wm->atom.incr) {
241 dump_property(wm, wm->atom.wl_selection, reply);
242 wm->incr = 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400243 } else {
244 dump_property(wm, wm->atom.wl_selection, reply);
245 wm->incr = 0;
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700246 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400247 }
Bryce Harringtond3553c72015-06-30 21:35:43 -0700248
249 free(reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400250}
251
252static void
253weston_wm_handle_selection_notify(struct weston_wm *wm,
254 xcb_generic_event_t *event)
255{
256 xcb_selection_notify_event_t *selection_notify =
257 (xcb_selection_notify_event_t *) event;
258
259 if (selection_notify->property == XCB_ATOM_NONE) {
260 /* convert selection failed */
261 } else if (selection_notify->target == wm->atom.targets) {
262 weston_wm_get_selection_targets(wm);
263 } else {
264 weston_wm_get_selection_data(wm);
265 }
266}
267
268static const size_t incr_chunk_size = 64 * 1024;
269
270static void
271weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property)
272{
273 xcb_selection_notify_event_t selection_notify;
274
275 memset(&selection_notify, 0, sizeof selection_notify);
276 selection_notify.response_type = XCB_SELECTION_NOTIFY;
277 selection_notify.sequence = 0;
278 selection_notify.time = wm->selection_request.time;
279 selection_notify.requestor = wm->selection_request.requestor;
280 selection_notify.selection = wm->selection_request.selection;
281 selection_notify.target = wm->selection_request.target;
282 selection_notify.property = property;
283
284 xcb_send_event(wm->conn, 0, /* propagate */
285 wm->selection_request.requestor,
286 XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify);
287}
288
289static void
290weston_wm_send_targets(struct weston_wm *wm)
291{
292 xcb_atom_t targets[] = {
293 wm->atom.timestamp,
294 wm->atom.targets,
295 wm->atom.utf8_string,
296 /* wm->atom.compound_text, */
297 wm->atom.text,
298 /* wm->atom.string */
299 };
300
301 xcb_change_property(wm->conn,
302 XCB_PROP_MODE_REPLACE,
303 wm->selection_request.requestor,
304 wm->selection_request.property,
305 XCB_ATOM_ATOM,
306 32, /* format */
307 ARRAY_LENGTH(targets), targets);
308
309 weston_wm_send_selection_notify(wm, wm->selection_request.property);
310}
311
312static void
313weston_wm_send_timestamp(struct weston_wm *wm)
314{
315 xcb_change_property(wm->conn,
316 XCB_PROP_MODE_REPLACE,
317 wm->selection_request.requestor,
318 wm->selection_request.property,
319 XCB_ATOM_INTEGER,
320 32, /* format */
321 1, &wm->selection_timestamp);
322
323 weston_wm_send_selection_notify(wm, wm->selection_request.property);
324}
325
326static int
327weston_wm_flush_source_data(struct weston_wm *wm)
328{
329 int length;
330
331 xcb_change_property(wm->conn,
332 XCB_PROP_MODE_REPLACE,
333 wm->selection_request.requestor,
334 wm->selection_request.property,
335 wm->selection_target,
336 8, /* format */
337 wm->source_data.size,
338 wm->source_data.data);
339 wm->selection_property_set = 1;
340 length = wm->source_data.size;
341 wm->source_data.size = 0;
342
343 return length;
344}
345
346static int
347weston_wm_read_data_source(int fd, uint32_t mask, void *data)
348{
349 struct weston_wm *wm = data;
350 int len, current, available;
351 void *p;
352
353 current = wm->source_data.size;
354 if (wm->source_data.size < incr_chunk_size)
355 p = wl_array_add(&wm->source_data, incr_chunk_size);
356 else
357 p = (char *) wm->source_data.data + wm->source_data.size;
358 available = wm->source_data.alloc - current;
359
360 len = read(fd, p, available);
361 if (len == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200362 weston_log("read error from data source: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400363 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
364 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200365 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400366 close(fd);
367 wl_array_release(&wm->source_data);
368 }
369
Martin Minarik6d118362012-06-07 18:01:59 +0200370 weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400371 len, available, mask, len, (char *) p);
372
373 wm->source_data.size = current + len;
374 if (wm->source_data.size >= incr_chunk_size) {
375 if (!wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200376 weston_log("got %zu bytes, starting incr\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400377 wm->source_data.size);
378 wm->incr = 1;
379 xcb_change_property(wm->conn,
380 XCB_PROP_MODE_REPLACE,
381 wm->selection_request.requestor,
382 wm->selection_request.property,
383 wm->atom.incr,
384 32, /* format */
385 1, &incr_chunk_size);
386 wm->selection_property_set = 1;
387 wm->flush_property_on_delete = 1;
388 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200389 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400390 weston_wm_send_selection_notify(wm, wm->selection_request.property);
391 } else if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200392 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400393 "property delete\n", wm->source_data.size);
394
395 wm->flush_property_on_delete = 1;
396 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200397 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400398 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200399 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400400 "property deleted, seting new property\n",
401 wm->source_data.size);
402 weston_wm_flush_source_data(wm);
403 }
404 } else if (len == 0 && !wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200405 weston_log("non-incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400406 /* Non-incr transfer all done. */
407 weston_wm_flush_source_data(wm);
408 weston_wm_send_selection_notify(wm, wm->selection_request.property);
409 xcb_flush(wm->conn);
410 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200411 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400412 close(fd);
413 wl_array_release(&wm->source_data);
414 wm->selection_request.requestor = XCB_NONE;
415 } else if (len == 0 && wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200416 weston_log("incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400417
418 wm->flush_property_on_delete = 1;
419 if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200420 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400421 "property delete\n", wm->source_data.size);
422 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200423 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400424 "property deleted, seting new property\n",
425 wm->source_data.size);
426 weston_wm_flush_source_data(wm);
427 }
428 xcb_flush(wm->conn);
429 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200430 wm->property_source = NULL;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400431 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400432 wm->data_source_fd = -1;
433 close(fd);
434 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200435 weston_log("nothing happened, buffered the bytes\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400436 }
437
438 return 1;
439}
440
441static void
442weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type)
443{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700444 struct weston_data_source *source;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400445 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400446 int p[2];
447
448 if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200449 weston_log("pipe2 failed: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400450 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
451 return;
452 }
453
454 wl_array_init(&wm->source_data);
455 wm->selection_target = target;
456 wm->data_source_fd = p[0];
457 wm->property_source = wl_event_loop_add_fd(wm->server->loop,
458 wm->data_source_fd,
459 WL_EVENT_READABLE,
460 weston_wm_read_data_source,
461 wm);
462
Kristian Høgsberge3148752013-05-06 23:19:49 -0400463 source = seat->selection_data_source;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400464 source->send(source, mime_type, p[1]);
Kristian Høgsberg73bdc0c2013-09-04 22:32:50 -0700465 close(p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400466}
467
468static void
469weston_wm_send_incr_chunk(struct weston_wm *wm)
470{
471 int length;
472
Martin Minarik6d118362012-06-07 18:01:59 +0200473 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400474
475 wm->selection_property_set = 0;
476 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200477 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400478 wm->source_data.size);
479 wm->flush_property_on_delete = 0;
480 length = weston_wm_flush_source_data(wm);
481
482 if (wm->data_source_fd >= 0) {
483 wm->property_source =
484 wl_event_loop_add_fd(wm->server->loop,
485 wm->data_source_fd,
486 WL_EVENT_READABLE,
487 weston_wm_read_data_source,
488 wm);
489 } else if (length > 0) {
490 /* Transfer is all done, but queue a flush for
491 * the delete of the last chunk so we can set
492 * the 0 sized propert to signal the end of
493 * the transfer. */
494 wm->flush_property_on_delete = 1;
495 wl_array_release(&wm->source_data);
496 } else {
497 wm->selection_request.requestor = XCB_NONE;
498 }
499 }
500}
501
502static int
503weston_wm_handle_selection_property_notify(struct weston_wm *wm,
504 xcb_generic_event_t *event)
505{
506 xcb_property_notify_event_t *property_notify =
507 (xcb_property_notify_event_t *) event;
508
509 if (property_notify->window == wm->selection_window) {
510 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
511 property_notify->atom == wm->atom.wl_selection &&
512 wm->incr)
513 weston_wm_get_incr_chunk(wm);
514 return 1;
515 } else if (property_notify->window == wm->selection_request.requestor) {
516 if (property_notify->state == XCB_PROPERTY_DELETE &&
517 property_notify->atom == wm->selection_request.property &&
518 wm->incr)
519 weston_wm_send_incr_chunk(wm);
520 return 1;
521 }
522
523 return 0;
524}
525
526static void
527weston_wm_handle_selection_request(struct weston_wm *wm,
528 xcb_generic_event_t *event)
529{
530 xcb_selection_request_event_t *selection_request =
531 (xcb_selection_request_event_t *) event;
532
Martin Minarik6d118362012-06-07 18:01:59 +0200533 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400534 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200535 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400536 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200537 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400538 get_atom_name(wm->conn, selection_request->property));
539
540 wm->selection_request = *selection_request;
541 wm->incr = 0;
542 wm->flush_property_on_delete = 0;
543
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400544 if (selection_request->selection == wm->atom.clipboard_manager) {
545 /* The weston clipboard should already have grabbed
546 * the first target, so just send selection notify
547 * now. This isn't synchronized with the clipboard
548 * finishing getting the data, so there's a race here. */
549 weston_wm_send_selection_notify(wm, wm->selection_request.property);
550 return;
551 }
552
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400553 if (selection_request->target == wm->atom.targets) {
554 weston_wm_send_targets(wm);
555 } else if (selection_request->target == wm->atom.timestamp) {
556 weston_wm_send_timestamp(wm);
557 } else if (selection_request->target == wm->atom.utf8_string ||
558 selection_request->target == wm->atom.text) {
559 weston_wm_send_data(wm, wm->atom.utf8_string,
560 "text/plain;charset=utf-8");
561 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200562 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400563 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
564 }
565}
566
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700567static int
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400568weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
569 xcb_generic_event_t *event)
570{
571 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
572 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400573 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400574 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400575 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400576
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700577 if (xfixes_selection_notify->selection != wm->atom.clipboard)
578 return 0;
579
Martin Minarik6d118362012-06-07 18:01:59 +0200580 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400581 xfixes_selection_notify->owner);
582
Kristian Høgsberg80566732012-06-01 00:08:12 -0400583 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
584 if (wm->selection_owner != wm->selection_window) {
585 /* A real X client selection went away, not our
586 * proxy selection. Clear the wayland selection. */
587 compositor = wm->server->compositor;
588 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400589 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400590 }
591
592 wm->selection_owner = XCB_WINDOW_NONE;
593
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700594 return 1;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400595 }
596
597 wm->selection_owner = xfixes_selection_notify->owner;
598
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400599 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
600 * selection, so grab the actual timestamp here so we can
601 * answer TIMESTAMP conversion requests correctly. */
602 if (xfixes_selection_notify->owner == wm->selection_window) {
603 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200604 weston_log("our window, skipping\n");
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700605 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400606 }
607
608 wm->incr = 0;
609 xcb_convert_selection(wm->conn, wm->selection_window,
610 wm->atom.clipboard,
611 wm->atom.targets,
612 wm->atom.wl_selection,
613 xfixes_selection_notify->timestamp);
614
615 xcb_flush(wm->conn);
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700616
617 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400618}
619
620int
621weston_wm_handle_selection_event(struct weston_wm *wm,
622 xcb_generic_event_t *event)
623{
624 switch (event->response_type & ~0x80) {
625 case XCB_SELECTION_NOTIFY:
626 weston_wm_handle_selection_notify(wm, event);
627 return 1;
628 case XCB_PROPERTY_NOTIFY:
629 return weston_wm_handle_selection_property_notify(wm, event);
630 case XCB_SELECTION_REQUEST:
631 weston_wm_handle_selection_request(wm, event);
632 return 1;
633 }
634
635 switch (event->response_type - wm->xfixes->first_event) {
636 case XCB_XFIXES_SELECTION_NOTIFY:
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700637 return weston_wm_handle_xfixes_selection_notify(wm, event);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400638 }
639
640 return 0;
641}
642
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200643static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400644weston_wm_set_selection(struct wl_listener *listener, void *data)
645{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400646 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400647 struct weston_wm *wm =
648 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700649 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400650 const char **p, **end;
651 int has_text_plain = 0;
652
Kristian Høgsberg80566732012-06-01 00:08:12 -0400653 if (source == NULL) {
654 if (wm->selection_owner == wm->selection_window)
655 xcb_set_selection_owner(wm->conn,
656 XCB_ATOM_NONE,
657 wm->atom.clipboard,
658 wm->selection_timestamp);
659 return;
660 }
661
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400662 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400663 return;
664
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400665 p = source->mime_types.data;
666 end = (const char **)
667 ((char *) source->mime_types.data + source->mime_types.size);
668 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200669 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400670 if (strcmp(*p, "text/plain") == 0 ||
671 strcmp(*p, "text/plain;charset=utf-8") == 0)
672 has_text_plain = 1;
673 p++;
674 }
675
676 if (has_text_plain) {
677 xcb_set_selection_owner(wm->conn,
678 wm->selection_window,
679 wm->atom.clipboard,
680 XCB_TIME_CURRENT_TIME);
681 } else {
682 xcb_set_selection_owner(wm->conn,
683 XCB_ATOM_NONE,
684 wm->atom.clipboard,
685 XCB_TIME_CURRENT_TIME);
686 }
687}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400688
689void
690weston_wm_selection_init(struct weston_wm *wm)
691{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400692 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400693 uint32_t values[1], mask;
694
695 wm->selection_request.requestor = XCB_NONE;
696
697 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
698 wm->selection_window = xcb_generate_id(wm->conn);
699 xcb_create_window(wm->conn,
700 XCB_COPY_FROM_PARENT,
701 wm->selection_window,
702 wm->screen->root,
703 0, 0,
704 10, 10,
705 0,
706 XCB_WINDOW_CLASS_INPUT_OUTPUT,
707 wm->screen->root_visual,
708 XCB_CW_EVENT_MASK, values);
709
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400710 xcb_set_selection_owner(wm->conn,
711 wm->selection_window,
712 wm->atom.clipboard_manager,
713 XCB_TIME_CURRENT_TIME);
714
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400715 mask =
716 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
717 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
718 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
719 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
720 wm->atom.clipboard, mask);
721
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400722 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400723 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400724 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400725
726 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400727}