blob: d220569e3c651554cc8811567770f13fe126734d [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
33weston_wm_write_property(int fd, uint32_t mask, void *data)
34{
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);
46 wl_event_source_remove(wm->property_source);
47 close(fd);
Martin Minarik6d118362012-06-07 18:01:59 +020048 weston_log("write error to target fd: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040049 return 1;
50 }
51
Martin Minarik6d118362012-06-07 18:01:59 +020052 weston_log("wrote %d (chunk size %d) of %d bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -040053 wm->property_start + len,
54 len, xcb_get_property_value_length(wm->property_reply));
55
56 wm->property_start += len;
57 if (len == remainder) {
58 free(wm->property_reply);
59 wl_event_source_remove(wm->property_source);
60
61 if (wm->incr) {
62 xcb_delete_property(wm->conn,
63 wm->selection_window,
64 wm->atom.wl_selection);
65 } else {
Martin Minarik6d118362012-06-07 18:01:59 +020066 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040067 close(fd);
68 }
69 }
70
71 return 1;
72}
73
74static void
75weston_wm_get_incr_chunk(struct weston_wm *wm)
76{
77 xcb_get_property_cookie_t cookie;
78 xcb_get_property_reply_t *reply;
79
80 cookie = xcb_get_property(wm->conn,
81 0, /* delete */
82 wm->selection_window,
83 wm->atom.wl_selection,
84 XCB_GET_PROPERTY_TYPE_ANY,
85 0, /* offset */
86 0x1fffffff /* length */);
87
88 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
89
90 dump_property(wm, wm->atom.wl_selection, reply);
91
92 if (xcb_get_property_value_length(reply) > 0) {
93 wm->property_start = 0;
94 wm->property_source =
95 wl_event_loop_add_fd(wm->server->loop,
96 wm->data_source_fd,
97 WL_EVENT_WRITABLE,
98 weston_wm_write_property,
99 wm);
100 wm->property_reply = reply;
101 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200102 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400103 close(wm->data_source_fd);
104 free(reply);
105 }
106}
107
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400108struct x11_data_source {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700109 struct weston_data_source base;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400110 struct weston_wm *wm;
111};
112
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400113static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700114data_source_accept(struct weston_data_source *source,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400115 uint32_t time, const char *mime_type)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400116{
117}
118
119static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700120data_source_send(struct weston_data_source *base,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400121 const char *mime_type, int32_t fd)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400122{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400123 struct x11_data_source *source = (struct x11_data_source *) base;
124 struct weston_wm *wm = source->wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400125
126 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
127 /* Get data for the utf8_string target */
128 xcb_convert_selection(wm->conn,
129 wm->selection_window,
130 wm->atom.clipboard,
131 wm->atom.utf8_string,
132 wm->atom.wl_selection,
133 XCB_TIME_CURRENT_TIME);
134
135 xcb_flush(wm->conn);
136
137 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
Kristian Høgsberg668fc0d2013-09-04 20:48:46 -0700138 wm->data_source_fd = fd;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400139 }
140}
141
142static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700143data_source_cancel(struct weston_data_source *source)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400144{
145}
146
147static void
148weston_wm_get_selection_targets(struct weston_wm *wm)
149{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400150 struct x11_data_source *source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400151 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400152 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400153 xcb_get_property_cookie_t cookie;
154 xcb_get_property_reply_t *reply;
155 xcb_atom_t *value;
156 char **p;
157 uint32_t i;
158
159 cookie = xcb_get_property(wm->conn,
160 1, /* delete */
161 wm->selection_window,
162 wm->atom.wl_selection,
163 XCB_GET_PROPERTY_TYPE_ANY,
164 0, /* offset */
165 4096 /* length */);
166
167 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
168
169 dump_property(wm, wm->atom.wl_selection, reply);
170
171 if (reply->type != XCB_ATOM_ATOM) {
172 free(reply);
173 return;
174 }
175
176 source = malloc(sizeof *source);
177 if (source == NULL)
178 return;
179
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500180 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400181 source->base.accept = data_source_accept;
182 source->base.send = data_source_send;
183 source->base.cancel = data_source_cancel;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400184 source->wm = wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400185
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400186 wl_array_init(&source->base.mime_types);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400187 value = xcb_get_property_value(reply);
188 for (i = 0; i < reply->value_len; i++) {
189 if (value[i] == wm->atom.utf8_string) {
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400190 p = wl_array_add(&source->base.mime_types, sizeof *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400191 if (p)
192 *p = strdup("text/plain;charset=utf-8");
193 }
194 }
195
196 compositor = wm->server->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400197 weston_seat_set_selection(seat, &source->base,
198 wl_display_next_serial(compositor->wl_display));
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400199
200 free(reply);
201}
202
203static void
204weston_wm_get_selection_data(struct weston_wm *wm)
205{
206 xcb_get_property_cookie_t cookie;
207 xcb_get_property_reply_t *reply;
208
209 cookie = xcb_get_property(wm->conn,
210 1, /* delete */
211 wm->selection_window,
212 wm->atom.wl_selection,
213 XCB_GET_PROPERTY_TYPE_ANY,
214 0, /* offset */
215 0x1fffffff /* length */);
216
217 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
218
219 if (reply->type == wm->atom.incr) {
220 dump_property(wm, wm->atom.wl_selection, reply);
221 wm->incr = 1;
222 free(reply);
223 } else {
224 dump_property(wm, wm->atom.wl_selection, reply);
225 wm->incr = 0;
226 wm->property_start = 0;
227 wm->property_source =
228 wl_event_loop_add_fd(wm->server->loop,
229 wm->data_source_fd,
230 WL_EVENT_WRITABLE,
231 weston_wm_write_property,
232 wm);
233 wm->property_reply = reply;
234 }
235}
236
237static void
238weston_wm_handle_selection_notify(struct weston_wm *wm,
239 xcb_generic_event_t *event)
240{
241 xcb_selection_notify_event_t *selection_notify =
242 (xcb_selection_notify_event_t *) event;
243
244 if (selection_notify->property == XCB_ATOM_NONE) {
245 /* convert selection failed */
246 } else if (selection_notify->target == wm->atom.targets) {
247 weston_wm_get_selection_targets(wm);
248 } else {
249 weston_wm_get_selection_data(wm);
250 }
251}
252
253static const size_t incr_chunk_size = 64 * 1024;
254
255static void
256weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property)
257{
258 xcb_selection_notify_event_t selection_notify;
259
260 memset(&selection_notify, 0, sizeof selection_notify);
261 selection_notify.response_type = XCB_SELECTION_NOTIFY;
262 selection_notify.sequence = 0;
263 selection_notify.time = wm->selection_request.time;
264 selection_notify.requestor = wm->selection_request.requestor;
265 selection_notify.selection = wm->selection_request.selection;
266 selection_notify.target = wm->selection_request.target;
267 selection_notify.property = property;
268
269 xcb_send_event(wm->conn, 0, /* propagate */
270 wm->selection_request.requestor,
271 XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify);
272}
273
274static void
275weston_wm_send_targets(struct weston_wm *wm)
276{
277 xcb_atom_t targets[] = {
278 wm->atom.timestamp,
279 wm->atom.targets,
280 wm->atom.utf8_string,
281 /* wm->atom.compound_text, */
282 wm->atom.text,
283 /* wm->atom.string */
284 };
285
286 xcb_change_property(wm->conn,
287 XCB_PROP_MODE_REPLACE,
288 wm->selection_request.requestor,
289 wm->selection_request.property,
290 XCB_ATOM_ATOM,
291 32, /* format */
292 ARRAY_LENGTH(targets), targets);
293
294 weston_wm_send_selection_notify(wm, wm->selection_request.property);
295}
296
297static void
298weston_wm_send_timestamp(struct weston_wm *wm)
299{
300 xcb_change_property(wm->conn,
301 XCB_PROP_MODE_REPLACE,
302 wm->selection_request.requestor,
303 wm->selection_request.property,
304 XCB_ATOM_INTEGER,
305 32, /* format */
306 1, &wm->selection_timestamp);
307
308 weston_wm_send_selection_notify(wm, wm->selection_request.property);
309}
310
311static int
312weston_wm_flush_source_data(struct weston_wm *wm)
313{
314 int length;
315
316 xcb_change_property(wm->conn,
317 XCB_PROP_MODE_REPLACE,
318 wm->selection_request.requestor,
319 wm->selection_request.property,
320 wm->selection_target,
321 8, /* format */
322 wm->source_data.size,
323 wm->source_data.data);
324 wm->selection_property_set = 1;
325 length = wm->source_data.size;
326 wm->source_data.size = 0;
327
328 return length;
329}
330
331static int
332weston_wm_read_data_source(int fd, uint32_t mask, void *data)
333{
334 struct weston_wm *wm = data;
335 int len, current, available;
336 void *p;
337
338 current = wm->source_data.size;
339 if (wm->source_data.size < incr_chunk_size)
340 p = wl_array_add(&wm->source_data, incr_chunk_size);
341 else
342 p = (char *) wm->source_data.data + wm->source_data.size;
343 available = wm->source_data.alloc - current;
344
345 len = read(fd, p, available);
346 if (len == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200347 weston_log("read error from data source: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400348 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
349 wl_event_source_remove(wm->property_source);
350 close(fd);
351 wl_array_release(&wm->source_data);
352 }
353
Martin Minarik6d118362012-06-07 18:01:59 +0200354 weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400355 len, available, mask, len, (char *) p);
356
357 wm->source_data.size = current + len;
358 if (wm->source_data.size >= incr_chunk_size) {
359 if (!wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200360 weston_log("got %zu bytes, starting incr\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400361 wm->source_data.size);
362 wm->incr = 1;
363 xcb_change_property(wm->conn,
364 XCB_PROP_MODE_REPLACE,
365 wm->selection_request.requestor,
366 wm->selection_request.property,
367 wm->atom.incr,
368 32, /* format */
369 1, &incr_chunk_size);
370 wm->selection_property_set = 1;
371 wm->flush_property_on_delete = 1;
372 wl_event_source_remove(wm->property_source);
373 weston_wm_send_selection_notify(wm, wm->selection_request.property);
374 } else if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200375 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400376 "property delete\n", wm->source_data.size);
377
378 wm->flush_property_on_delete = 1;
379 wl_event_source_remove(wm->property_source);
380 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200381 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400382 "property deleted, seting new property\n",
383 wm->source_data.size);
384 weston_wm_flush_source_data(wm);
385 }
386 } else if (len == 0 && !wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200387 weston_log("non-incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400388 /* Non-incr transfer all done. */
389 weston_wm_flush_source_data(wm);
390 weston_wm_send_selection_notify(wm, wm->selection_request.property);
391 xcb_flush(wm->conn);
392 wl_event_source_remove(wm->property_source);
393 close(fd);
394 wl_array_release(&wm->source_data);
395 wm->selection_request.requestor = XCB_NONE;
396 } else if (len == 0 && wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200397 weston_log("incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400398
399 wm->flush_property_on_delete = 1;
400 if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200401 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400402 "property delete\n", wm->source_data.size);
403 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200404 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400405 "property deleted, seting new property\n",
406 wm->source_data.size);
407 weston_wm_flush_source_data(wm);
408 }
409 xcb_flush(wm->conn);
410 wl_event_source_remove(wm->property_source);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400411 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400412 wm->data_source_fd = -1;
413 close(fd);
414 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("nothing happened, buffered the bytes\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400416 }
417
418 return 1;
419}
420
421static void
422weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type)
423{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700424 struct weston_data_source *source;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400425 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400426 int p[2];
427
428 if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200429 weston_log("pipe2 failed: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400430 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
431 return;
432 }
433
434 wl_array_init(&wm->source_data);
435 wm->selection_target = target;
436 wm->data_source_fd = p[0];
437 wm->property_source = wl_event_loop_add_fd(wm->server->loop,
438 wm->data_source_fd,
439 WL_EVENT_READABLE,
440 weston_wm_read_data_source,
441 wm);
442
Kristian Høgsberge3148752013-05-06 23:19:49 -0400443 source = seat->selection_data_source;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400444 source->send(source, mime_type, p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400445}
446
447static void
448weston_wm_send_incr_chunk(struct weston_wm *wm)
449{
450 int length;
451
Martin Minarik6d118362012-06-07 18:01:59 +0200452 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400453
454 wm->selection_property_set = 0;
455 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200456 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400457 wm->source_data.size);
458 wm->flush_property_on_delete = 0;
459 length = weston_wm_flush_source_data(wm);
460
461 if (wm->data_source_fd >= 0) {
462 wm->property_source =
463 wl_event_loop_add_fd(wm->server->loop,
464 wm->data_source_fd,
465 WL_EVENT_READABLE,
466 weston_wm_read_data_source,
467 wm);
468 } else if (length > 0) {
469 /* Transfer is all done, but queue a flush for
470 * the delete of the last chunk so we can set
471 * the 0 sized propert to signal the end of
472 * the transfer. */
473 wm->flush_property_on_delete = 1;
474 wl_array_release(&wm->source_data);
475 } else {
476 wm->selection_request.requestor = XCB_NONE;
477 }
478 }
479}
480
481static int
482weston_wm_handle_selection_property_notify(struct weston_wm *wm,
483 xcb_generic_event_t *event)
484{
485 xcb_property_notify_event_t *property_notify =
486 (xcb_property_notify_event_t *) event;
487
488 if (property_notify->window == wm->selection_window) {
489 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
490 property_notify->atom == wm->atom.wl_selection &&
491 wm->incr)
492 weston_wm_get_incr_chunk(wm);
493 return 1;
494 } else if (property_notify->window == wm->selection_request.requestor) {
495 if (property_notify->state == XCB_PROPERTY_DELETE &&
496 property_notify->atom == wm->selection_request.property &&
497 wm->incr)
498 weston_wm_send_incr_chunk(wm);
499 return 1;
500 }
501
502 return 0;
503}
504
505static void
506weston_wm_handle_selection_request(struct weston_wm *wm,
507 xcb_generic_event_t *event)
508{
509 xcb_selection_request_event_t *selection_request =
510 (xcb_selection_request_event_t *) event;
511
Martin Minarik6d118362012-06-07 18:01:59 +0200512 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400513 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200514 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400515 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200516 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400517 get_atom_name(wm->conn, selection_request->property));
518
519 wm->selection_request = *selection_request;
520 wm->incr = 0;
521 wm->flush_property_on_delete = 0;
522
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400523 if (selection_request->selection == wm->atom.clipboard_manager) {
524 /* The weston clipboard should already have grabbed
525 * the first target, so just send selection notify
526 * now. This isn't synchronized with the clipboard
527 * finishing getting the data, so there's a race here. */
528 weston_wm_send_selection_notify(wm, wm->selection_request.property);
529 return;
530 }
531
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400532 if (selection_request->target == wm->atom.targets) {
533 weston_wm_send_targets(wm);
534 } else if (selection_request->target == wm->atom.timestamp) {
535 weston_wm_send_timestamp(wm);
536 } else if (selection_request->target == wm->atom.utf8_string ||
537 selection_request->target == wm->atom.text) {
538 weston_wm_send_data(wm, wm->atom.utf8_string,
539 "text/plain;charset=utf-8");
540 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200541 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400542 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
543 }
544}
545
546static void
547weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
548 xcb_generic_event_t *event)
549{
550 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
551 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400552 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400553 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400554 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400555
Martin Minarik6d118362012-06-07 18:01:59 +0200556 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400557 xfixes_selection_notify->owner);
558
Kristian Høgsberg80566732012-06-01 00:08:12 -0400559 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
560 if (wm->selection_owner != wm->selection_window) {
561 /* A real X client selection went away, not our
562 * proxy selection. Clear the wayland selection. */
563 compositor = wm->server->compositor;
564 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400565 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400566 }
567
568 wm->selection_owner = XCB_WINDOW_NONE;
569
570 return;
571 }
572
573 wm->selection_owner = xfixes_selection_notify->owner;
574
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400575 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
576 * selection, so grab the actual timestamp here so we can
577 * answer TIMESTAMP conversion requests correctly. */
578 if (xfixes_selection_notify->owner == wm->selection_window) {
579 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200580 weston_log("our window, skipping\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400581 return;
582 }
583
584 wm->incr = 0;
585 xcb_convert_selection(wm->conn, wm->selection_window,
586 wm->atom.clipboard,
587 wm->atom.targets,
588 wm->atom.wl_selection,
589 xfixes_selection_notify->timestamp);
590
591 xcb_flush(wm->conn);
592}
593
594int
595weston_wm_handle_selection_event(struct weston_wm *wm,
596 xcb_generic_event_t *event)
597{
598 switch (event->response_type & ~0x80) {
599 case XCB_SELECTION_NOTIFY:
600 weston_wm_handle_selection_notify(wm, event);
601 return 1;
602 case XCB_PROPERTY_NOTIFY:
603 return weston_wm_handle_selection_property_notify(wm, event);
604 case XCB_SELECTION_REQUEST:
605 weston_wm_handle_selection_request(wm, event);
606 return 1;
607 }
608
609 switch (event->response_type - wm->xfixes->first_event) {
610 case XCB_XFIXES_SELECTION_NOTIFY:
611 weston_wm_handle_xfixes_selection_notify(wm, event);
612 return 1;
613 }
614
615 return 0;
616}
617
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200618static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400619weston_wm_set_selection(struct wl_listener *listener, void *data)
620{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400621 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400622 struct weston_wm *wm =
623 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700624 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400625 const char **p, **end;
626 int has_text_plain = 0;
627
Kristian Høgsberg80566732012-06-01 00:08:12 -0400628 if (source == NULL) {
629 if (wm->selection_owner == wm->selection_window)
630 xcb_set_selection_owner(wm->conn,
631 XCB_ATOM_NONE,
632 wm->atom.clipboard,
633 wm->selection_timestamp);
634 return;
635 }
636
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400637 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400638 return;
639
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400640 p = source->mime_types.data;
641 end = (const char **)
642 ((char *) source->mime_types.data + source->mime_types.size);
643 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200644 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400645 if (strcmp(*p, "text/plain") == 0 ||
646 strcmp(*p, "text/plain;charset=utf-8") == 0)
647 has_text_plain = 1;
648 p++;
649 }
650
651 if (has_text_plain) {
652 xcb_set_selection_owner(wm->conn,
653 wm->selection_window,
654 wm->atom.clipboard,
655 XCB_TIME_CURRENT_TIME);
656 } else {
657 xcb_set_selection_owner(wm->conn,
658 XCB_ATOM_NONE,
659 wm->atom.clipboard,
660 XCB_TIME_CURRENT_TIME);
661 }
662}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400663
664void
665weston_wm_selection_init(struct weston_wm *wm)
666{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400667 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400668 uint32_t values[1], mask;
669
670 wm->selection_request.requestor = XCB_NONE;
671
672 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
673 wm->selection_window = xcb_generate_id(wm->conn);
674 xcb_create_window(wm->conn,
675 XCB_COPY_FROM_PARENT,
676 wm->selection_window,
677 wm->screen->root,
678 0, 0,
679 10, 10,
680 0,
681 XCB_WINDOW_CLASS_INPUT_OUTPUT,
682 wm->screen->root_visual,
683 XCB_CW_EVENT_MASK, values);
684
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400685 xcb_set_selection_owner(wm->conn,
686 wm->selection_window,
687 wm->atom.clipboard_manager,
688 XCB_TIME_CURRENT_TIME);
689
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400690 mask =
691 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
692 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
693 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
694 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
695 wm->atom.clipboard, mask);
696
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400697 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400698 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400699 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400700
701 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400702}