blob: 76063df01ad3cf404b7166d70cab16d60c0cbd8c [file] [log] [blame]
Kristian Høgsberg102bf032012-05-21 15:52:02 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg102bf032012-05-21 15:52:02 -040024
25#include <stdlib.h>
Kristian Høgsberg102bf032012-05-21 15:52:02 -040026#include <string.h>
27#include <unistd.h>
28#include <fcntl.h>
29
30#include "xwayland.h"
31
32static int
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070033writable_callback(int fd, uint32_t mask, void *data)
Kristian Høgsberg102bf032012-05-21 15:52:02 -040034{
35 struct weston_wm *wm = data;
36 unsigned char *property;
37 int len, remainder;
38
39 property = xcb_get_property_value(wm->property_reply);
40 remainder = xcb_get_property_value_length(wm->property_reply) -
41 wm->property_start;
42
43 len = write(fd, property + wm->property_start, remainder);
44 if (len == -1) {
45 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070046 wm->property_reply = NULL;
47 if (wm->property_source)
48 wl_event_source_remove(wm->property_source);
Kristian Høgsberg102bf032012-05-21 15:52:02 -040049 close(fd);
Martin Minarik6d118362012-06-07 18:01:59 +020050 weston_log("write error to target fd: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040051 return 1;
52 }
53
Martin Minarik6d118362012-06-07 18:01:59 +020054 weston_log("wrote %d (chunk size %d) of %d bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -040055 wm->property_start + len,
56 len, xcb_get_property_value_length(wm->property_reply));
57
58 wm->property_start += len;
59 if (len == remainder) {
60 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070061 wm->property_reply = NULL;
62 if (wm->property_source)
63 wl_event_source_remove(wm->property_source);
Kristian Høgsberg102bf032012-05-21 15:52:02 -040064
65 if (wm->incr) {
66 xcb_delete_property(wm->conn,
67 wm->selection_window,
68 wm->atom.wl_selection);
69 } else {
Martin Minarik6d118362012-06-07 18:01:59 +020070 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040071 close(fd);
72 }
73 }
74
75 return 1;
76}
77
78static void
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070079weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply)
80{
81 wm->property_start = 0;
82 wm->property_reply = reply;
83 writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm);
84
85 if (wm->property_reply)
86 wm->property_source =
87 wl_event_loop_add_fd(wm->server->loop,
88 wm->data_source_fd,
89 WL_EVENT_WRITABLE,
90 writable_callback, wm);
91}
92
93static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -040094weston_wm_get_incr_chunk(struct weston_wm *wm)
95{
96 xcb_get_property_cookie_t cookie;
97 xcb_get_property_reply_t *reply;
98
99 cookie = xcb_get_property(wm->conn,
100 0, /* delete */
101 wm->selection_window,
102 wm->atom.wl_selection,
103 XCB_GET_PROPERTY_TYPE_ANY,
104 0, /* offset */
105 0x1fffffff /* length */);
106
107 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
108
109 dump_property(wm, wm->atom.wl_selection, reply);
110
111 if (xcb_get_property_value_length(reply) > 0) {
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700112 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400113 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200114 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400115 close(wm->data_source_fd);
116 free(reply);
117 }
118}
119
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400120struct x11_data_source {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700121 struct weston_data_source base;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400122 struct weston_wm *wm;
123};
124
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400125static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700126data_source_accept(struct weston_data_source *source,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400127 uint32_t time, const char *mime_type)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400128{
129}
130
131static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700132data_source_send(struct weston_data_source *base,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400133 const char *mime_type, int32_t fd)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400134{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400135 struct x11_data_source *source = (struct x11_data_source *) base;
136 struct weston_wm *wm = source->wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400137
138 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
139 /* Get data for the utf8_string target */
140 xcb_convert_selection(wm->conn,
141 wm->selection_window,
142 wm->atom.clipboard,
143 wm->atom.utf8_string,
144 wm->atom.wl_selection,
145 XCB_TIME_CURRENT_TIME);
146
147 xcb_flush(wm->conn);
148
149 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
Kristian Høgsberg668fc0d2013-09-04 20:48:46 -0700150 wm->data_source_fd = fd;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400151 }
152}
153
154static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700155data_source_cancel(struct weston_data_source *source)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400156{
157}
158
159static void
160weston_wm_get_selection_targets(struct weston_wm *wm)
161{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400162 struct x11_data_source *source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400163 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400164 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400165 xcb_get_property_cookie_t cookie;
166 xcb_get_property_reply_t *reply;
167 xcb_atom_t *value;
168 char **p;
169 uint32_t i;
170
171 cookie = xcb_get_property(wm->conn,
172 1, /* delete */
173 wm->selection_window,
174 wm->atom.wl_selection,
175 XCB_GET_PROPERTY_TYPE_ANY,
176 0, /* offset */
177 4096 /* length */);
178
179 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
180
181 dump_property(wm, wm->atom.wl_selection, reply);
182
183 if (reply->type != XCB_ATOM_ATOM) {
184 free(reply);
185 return;
186 }
187
188 source = malloc(sizeof *source);
189 if (source == NULL)
190 return;
191
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500192 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400193 source->base.accept = data_source_accept;
194 source->base.send = data_source_send;
195 source->base.cancel = data_source_cancel;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400196 source->wm = wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400197
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400198 wl_array_init(&source->base.mime_types);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400199 value = xcb_get_property_value(reply);
200 for (i = 0; i < reply->value_len; i++) {
201 if (value[i] == wm->atom.utf8_string) {
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400202 p = wl_array_add(&source->base.mime_types, sizeof *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400203 if (p)
204 *p = strdup("text/plain;charset=utf-8");
205 }
206 }
207
208 compositor = wm->server->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400209 weston_seat_set_selection(seat, &source->base,
210 wl_display_next_serial(compositor->wl_display));
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400211
212 free(reply);
213}
214
215static void
216weston_wm_get_selection_data(struct weston_wm *wm)
217{
218 xcb_get_property_cookie_t cookie;
219 xcb_get_property_reply_t *reply;
220
221 cookie = xcb_get_property(wm->conn,
222 1, /* delete */
223 wm->selection_window,
224 wm->atom.wl_selection,
225 XCB_GET_PROPERTY_TYPE_ANY,
226 0, /* offset */
227 0x1fffffff /* length */);
228
229 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
230
231 if (reply->type == wm->atom.incr) {
232 dump_property(wm, wm->atom.wl_selection, reply);
233 wm->incr = 1;
234 free(reply);
235 } else {
236 dump_property(wm, wm->atom.wl_selection, reply);
237 wm->incr = 0;
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700238 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400239 }
240}
241
242static void
243weston_wm_handle_selection_notify(struct weston_wm *wm,
244 xcb_generic_event_t *event)
245{
246 xcb_selection_notify_event_t *selection_notify =
247 (xcb_selection_notify_event_t *) event;
248
249 if (selection_notify->property == XCB_ATOM_NONE) {
250 /* convert selection failed */
251 } else if (selection_notify->target == wm->atom.targets) {
252 weston_wm_get_selection_targets(wm);
253 } else {
254 weston_wm_get_selection_data(wm);
255 }
256}
257
258static const size_t incr_chunk_size = 64 * 1024;
259
260static void
261weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property)
262{
263 xcb_selection_notify_event_t selection_notify;
264
265 memset(&selection_notify, 0, sizeof selection_notify);
266 selection_notify.response_type = XCB_SELECTION_NOTIFY;
267 selection_notify.sequence = 0;
268 selection_notify.time = wm->selection_request.time;
269 selection_notify.requestor = wm->selection_request.requestor;
270 selection_notify.selection = wm->selection_request.selection;
271 selection_notify.target = wm->selection_request.target;
272 selection_notify.property = property;
273
274 xcb_send_event(wm->conn, 0, /* propagate */
275 wm->selection_request.requestor,
276 XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify);
277}
278
279static void
280weston_wm_send_targets(struct weston_wm *wm)
281{
282 xcb_atom_t targets[] = {
283 wm->atom.timestamp,
284 wm->atom.targets,
285 wm->atom.utf8_string,
286 /* wm->atom.compound_text, */
287 wm->atom.text,
288 /* wm->atom.string */
289 };
290
291 xcb_change_property(wm->conn,
292 XCB_PROP_MODE_REPLACE,
293 wm->selection_request.requestor,
294 wm->selection_request.property,
295 XCB_ATOM_ATOM,
296 32, /* format */
297 ARRAY_LENGTH(targets), targets);
298
299 weston_wm_send_selection_notify(wm, wm->selection_request.property);
300}
301
302static void
303weston_wm_send_timestamp(struct weston_wm *wm)
304{
305 xcb_change_property(wm->conn,
306 XCB_PROP_MODE_REPLACE,
307 wm->selection_request.requestor,
308 wm->selection_request.property,
309 XCB_ATOM_INTEGER,
310 32, /* format */
311 1, &wm->selection_timestamp);
312
313 weston_wm_send_selection_notify(wm, wm->selection_request.property);
314}
315
316static int
317weston_wm_flush_source_data(struct weston_wm *wm)
318{
319 int length;
320
321 xcb_change_property(wm->conn,
322 XCB_PROP_MODE_REPLACE,
323 wm->selection_request.requestor,
324 wm->selection_request.property,
325 wm->selection_target,
326 8, /* format */
327 wm->source_data.size,
328 wm->source_data.data);
329 wm->selection_property_set = 1;
330 length = wm->source_data.size;
331 wm->source_data.size = 0;
332
333 return length;
334}
335
336static int
337weston_wm_read_data_source(int fd, uint32_t mask, void *data)
338{
339 struct weston_wm *wm = data;
340 int len, current, available;
341 void *p;
342
343 current = wm->source_data.size;
344 if (wm->source_data.size < incr_chunk_size)
345 p = wl_array_add(&wm->source_data, incr_chunk_size);
346 else
347 p = (char *) wm->source_data.data + wm->source_data.size;
348 available = wm->source_data.alloc - current;
349
350 len = read(fd, p, available);
351 if (len == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200352 weston_log("read error from data source: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400353 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
354 wl_event_source_remove(wm->property_source);
355 close(fd);
356 wl_array_release(&wm->source_data);
357 }
358
Martin Minarik6d118362012-06-07 18:01:59 +0200359 weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400360 len, available, mask, len, (char *) p);
361
362 wm->source_data.size = current + len;
363 if (wm->source_data.size >= incr_chunk_size) {
364 if (!wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200365 weston_log("got %zu bytes, starting incr\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400366 wm->source_data.size);
367 wm->incr = 1;
368 xcb_change_property(wm->conn,
369 XCB_PROP_MODE_REPLACE,
370 wm->selection_request.requestor,
371 wm->selection_request.property,
372 wm->atom.incr,
373 32, /* format */
374 1, &incr_chunk_size);
375 wm->selection_property_set = 1;
376 wm->flush_property_on_delete = 1;
377 wl_event_source_remove(wm->property_source);
378 weston_wm_send_selection_notify(wm, wm->selection_request.property);
379 } else if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200380 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400381 "property delete\n", wm->source_data.size);
382
383 wm->flush_property_on_delete = 1;
384 wl_event_source_remove(wm->property_source);
385 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200386 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400387 "property deleted, seting new property\n",
388 wm->source_data.size);
389 weston_wm_flush_source_data(wm);
390 }
391 } else if (len == 0 && !wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200392 weston_log("non-incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400393 /* Non-incr transfer all done. */
394 weston_wm_flush_source_data(wm);
395 weston_wm_send_selection_notify(wm, wm->selection_request.property);
396 xcb_flush(wm->conn);
397 wl_event_source_remove(wm->property_source);
398 close(fd);
399 wl_array_release(&wm->source_data);
400 wm->selection_request.requestor = XCB_NONE;
401 } else if (len == 0 && wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200402 weston_log("incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400403
404 wm->flush_property_on_delete = 1;
405 if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200406 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400407 "property delete\n", wm->source_data.size);
408 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200409 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400410 "property deleted, seting new property\n",
411 wm->source_data.size);
412 weston_wm_flush_source_data(wm);
413 }
414 xcb_flush(wm->conn);
415 wl_event_source_remove(wm->property_source);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400416 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400417 wm->data_source_fd = -1;
418 close(fd);
419 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200420 weston_log("nothing happened, buffered the bytes\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400421 }
422
423 return 1;
424}
425
426static void
427weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type)
428{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700429 struct weston_data_source *source;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400430 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400431 int p[2];
432
433 if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200434 weston_log("pipe2 failed: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400435 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
436 return;
437 }
438
439 wl_array_init(&wm->source_data);
440 wm->selection_target = target;
441 wm->data_source_fd = p[0];
442 wm->property_source = wl_event_loop_add_fd(wm->server->loop,
443 wm->data_source_fd,
444 WL_EVENT_READABLE,
445 weston_wm_read_data_source,
446 wm);
447
Kristian Høgsberge3148752013-05-06 23:19:49 -0400448 source = seat->selection_data_source;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400449 source->send(source, mime_type, p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400450}
451
452static void
453weston_wm_send_incr_chunk(struct weston_wm *wm)
454{
455 int length;
456
Martin Minarik6d118362012-06-07 18:01:59 +0200457 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400458
459 wm->selection_property_set = 0;
460 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200461 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400462 wm->source_data.size);
463 wm->flush_property_on_delete = 0;
464 length = weston_wm_flush_source_data(wm);
465
466 if (wm->data_source_fd >= 0) {
467 wm->property_source =
468 wl_event_loop_add_fd(wm->server->loop,
469 wm->data_source_fd,
470 WL_EVENT_READABLE,
471 weston_wm_read_data_source,
472 wm);
473 } else if (length > 0) {
474 /* Transfer is all done, but queue a flush for
475 * the delete of the last chunk so we can set
476 * the 0 sized propert to signal the end of
477 * the transfer. */
478 wm->flush_property_on_delete = 1;
479 wl_array_release(&wm->source_data);
480 } else {
481 wm->selection_request.requestor = XCB_NONE;
482 }
483 }
484}
485
486static int
487weston_wm_handle_selection_property_notify(struct weston_wm *wm,
488 xcb_generic_event_t *event)
489{
490 xcb_property_notify_event_t *property_notify =
491 (xcb_property_notify_event_t *) event;
492
493 if (property_notify->window == wm->selection_window) {
494 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
495 property_notify->atom == wm->atom.wl_selection &&
496 wm->incr)
497 weston_wm_get_incr_chunk(wm);
498 return 1;
499 } else if (property_notify->window == wm->selection_request.requestor) {
500 if (property_notify->state == XCB_PROPERTY_DELETE &&
501 property_notify->atom == wm->selection_request.property &&
502 wm->incr)
503 weston_wm_send_incr_chunk(wm);
504 return 1;
505 }
506
507 return 0;
508}
509
510static void
511weston_wm_handle_selection_request(struct weston_wm *wm,
512 xcb_generic_event_t *event)
513{
514 xcb_selection_request_event_t *selection_request =
515 (xcb_selection_request_event_t *) event;
516
Martin Minarik6d118362012-06-07 18:01:59 +0200517 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400518 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200519 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400520 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200521 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400522 get_atom_name(wm->conn, selection_request->property));
523
524 wm->selection_request = *selection_request;
525 wm->incr = 0;
526 wm->flush_property_on_delete = 0;
527
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400528 if (selection_request->selection == wm->atom.clipboard_manager) {
529 /* The weston clipboard should already have grabbed
530 * the first target, so just send selection notify
531 * now. This isn't synchronized with the clipboard
532 * finishing getting the data, so there's a race here. */
533 weston_wm_send_selection_notify(wm, wm->selection_request.property);
534 return;
535 }
536
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400537 if (selection_request->target == wm->atom.targets) {
538 weston_wm_send_targets(wm);
539 } else if (selection_request->target == wm->atom.timestamp) {
540 weston_wm_send_timestamp(wm);
541 } else if (selection_request->target == wm->atom.utf8_string ||
542 selection_request->target == wm->atom.text) {
543 weston_wm_send_data(wm, wm->atom.utf8_string,
544 "text/plain;charset=utf-8");
545 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200546 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400547 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
548 }
549}
550
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700551static int
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400552weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
553 xcb_generic_event_t *event)
554{
555 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
556 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400557 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400558 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400559 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400560
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700561 if (xfixes_selection_notify->selection != wm->atom.clipboard)
562 return 0;
563
Martin Minarik6d118362012-06-07 18:01:59 +0200564 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400565 xfixes_selection_notify->owner);
566
Kristian Høgsberg80566732012-06-01 00:08:12 -0400567 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
568 if (wm->selection_owner != wm->selection_window) {
569 /* A real X client selection went away, not our
570 * proxy selection. Clear the wayland selection. */
571 compositor = wm->server->compositor;
572 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400573 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400574 }
575
576 wm->selection_owner = XCB_WINDOW_NONE;
577
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700578 return 1;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400579 }
580
581 wm->selection_owner = xfixes_selection_notify->owner;
582
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400583 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
584 * selection, so grab the actual timestamp here so we can
585 * answer TIMESTAMP conversion requests correctly. */
586 if (xfixes_selection_notify->owner == wm->selection_window) {
587 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200588 weston_log("our window, skipping\n");
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700589 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400590 }
591
592 wm->incr = 0;
593 xcb_convert_selection(wm->conn, wm->selection_window,
594 wm->atom.clipboard,
595 wm->atom.targets,
596 wm->atom.wl_selection,
597 xfixes_selection_notify->timestamp);
598
599 xcb_flush(wm->conn);
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700600
601 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400602}
603
604int
605weston_wm_handle_selection_event(struct weston_wm *wm,
606 xcb_generic_event_t *event)
607{
608 switch (event->response_type & ~0x80) {
609 case XCB_SELECTION_NOTIFY:
610 weston_wm_handle_selection_notify(wm, event);
611 return 1;
612 case XCB_PROPERTY_NOTIFY:
613 return weston_wm_handle_selection_property_notify(wm, event);
614 case XCB_SELECTION_REQUEST:
615 weston_wm_handle_selection_request(wm, event);
616 return 1;
617 }
618
619 switch (event->response_type - wm->xfixes->first_event) {
620 case XCB_XFIXES_SELECTION_NOTIFY:
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700621 return weston_wm_handle_xfixes_selection_notify(wm, event);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400622 }
623
624 return 0;
625}
626
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200627static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400628weston_wm_set_selection(struct wl_listener *listener, void *data)
629{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400630 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400631 struct weston_wm *wm =
632 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700633 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400634 const char **p, **end;
635 int has_text_plain = 0;
636
Kristian Høgsberg80566732012-06-01 00:08:12 -0400637 if (source == NULL) {
638 if (wm->selection_owner == wm->selection_window)
639 xcb_set_selection_owner(wm->conn,
640 XCB_ATOM_NONE,
641 wm->atom.clipboard,
642 wm->selection_timestamp);
643 return;
644 }
645
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400646 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400647 return;
648
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400649 p = source->mime_types.data;
650 end = (const char **)
651 ((char *) source->mime_types.data + source->mime_types.size);
652 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200653 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400654 if (strcmp(*p, "text/plain") == 0 ||
655 strcmp(*p, "text/plain;charset=utf-8") == 0)
656 has_text_plain = 1;
657 p++;
658 }
659
660 if (has_text_plain) {
661 xcb_set_selection_owner(wm->conn,
662 wm->selection_window,
663 wm->atom.clipboard,
664 XCB_TIME_CURRENT_TIME);
665 } else {
666 xcb_set_selection_owner(wm->conn,
667 XCB_ATOM_NONE,
668 wm->atom.clipboard,
669 XCB_TIME_CURRENT_TIME);
670 }
671}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400672
673void
674weston_wm_selection_init(struct weston_wm *wm)
675{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400676 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400677 uint32_t values[1], mask;
678
679 wm->selection_request.requestor = XCB_NONE;
680
681 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
682 wm->selection_window = xcb_generate_id(wm->conn);
683 xcb_create_window(wm->conn,
684 XCB_COPY_FROM_PARENT,
685 wm->selection_window,
686 wm->screen->root,
687 0, 0,
688 10, 10,
689 0,
690 XCB_WINDOW_CLASS_INPUT_OUTPUT,
691 wm->screen->root_visual,
692 XCB_CW_EVENT_MASK, values);
693
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400694 xcb_set_selection_owner(wm->conn,
695 wm->selection_window,
696 wm->atom.clipboard_manager,
697 XCB_TIME_CURRENT_TIME);
698
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400699 mask =
700 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
701 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
702 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
703 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
704 wm->atom.clipboard, mask);
705
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400706 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400707 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400708 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400709
710 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400711}