blob: b694477eba67f51b65cc1a83b22d4a747f4b9932 [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øgsberg73bdc0c2013-09-04 22:32:50 -0700450 close(p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400451}
452
453static void
454weston_wm_send_incr_chunk(struct weston_wm *wm)
455{
456 int length;
457
Martin Minarik6d118362012-06-07 18:01:59 +0200458 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400459
460 wm->selection_property_set = 0;
461 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200462 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400463 wm->source_data.size);
464 wm->flush_property_on_delete = 0;
465 length = weston_wm_flush_source_data(wm);
466
467 if (wm->data_source_fd >= 0) {
468 wm->property_source =
469 wl_event_loop_add_fd(wm->server->loop,
470 wm->data_source_fd,
471 WL_EVENT_READABLE,
472 weston_wm_read_data_source,
473 wm);
474 } else if (length > 0) {
475 /* Transfer is all done, but queue a flush for
476 * the delete of the last chunk so we can set
477 * the 0 sized propert to signal the end of
478 * the transfer. */
479 wm->flush_property_on_delete = 1;
480 wl_array_release(&wm->source_data);
481 } else {
482 wm->selection_request.requestor = XCB_NONE;
483 }
484 }
485}
486
487static int
488weston_wm_handle_selection_property_notify(struct weston_wm *wm,
489 xcb_generic_event_t *event)
490{
491 xcb_property_notify_event_t *property_notify =
492 (xcb_property_notify_event_t *) event;
493
494 if (property_notify->window == wm->selection_window) {
495 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
496 property_notify->atom == wm->atom.wl_selection &&
497 wm->incr)
498 weston_wm_get_incr_chunk(wm);
499 return 1;
500 } else if (property_notify->window == wm->selection_request.requestor) {
501 if (property_notify->state == XCB_PROPERTY_DELETE &&
502 property_notify->atom == wm->selection_request.property &&
503 wm->incr)
504 weston_wm_send_incr_chunk(wm);
505 return 1;
506 }
507
508 return 0;
509}
510
511static void
512weston_wm_handle_selection_request(struct weston_wm *wm,
513 xcb_generic_event_t *event)
514{
515 xcb_selection_request_event_t *selection_request =
516 (xcb_selection_request_event_t *) event;
517
Martin Minarik6d118362012-06-07 18:01:59 +0200518 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400519 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200520 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400521 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200522 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400523 get_atom_name(wm->conn, selection_request->property));
524
525 wm->selection_request = *selection_request;
526 wm->incr = 0;
527 wm->flush_property_on_delete = 0;
528
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400529 if (selection_request->selection == wm->atom.clipboard_manager) {
530 /* The weston clipboard should already have grabbed
531 * the first target, so just send selection notify
532 * now. This isn't synchronized with the clipboard
533 * finishing getting the data, so there's a race here. */
534 weston_wm_send_selection_notify(wm, wm->selection_request.property);
535 return;
536 }
537
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400538 if (selection_request->target == wm->atom.targets) {
539 weston_wm_send_targets(wm);
540 } else if (selection_request->target == wm->atom.timestamp) {
541 weston_wm_send_timestamp(wm);
542 } else if (selection_request->target == wm->atom.utf8_string ||
543 selection_request->target == wm->atom.text) {
544 weston_wm_send_data(wm, wm->atom.utf8_string,
545 "text/plain;charset=utf-8");
546 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200547 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400548 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
549 }
550}
551
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700552static int
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400553weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
554 xcb_generic_event_t *event)
555{
556 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
557 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400558 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400559 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400560 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400561
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700562 if (xfixes_selection_notify->selection != wm->atom.clipboard)
563 return 0;
564
Martin Minarik6d118362012-06-07 18:01:59 +0200565 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400566 xfixes_selection_notify->owner);
567
Kristian Høgsberg80566732012-06-01 00:08:12 -0400568 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
569 if (wm->selection_owner != wm->selection_window) {
570 /* A real X client selection went away, not our
571 * proxy selection. Clear the wayland selection. */
572 compositor = wm->server->compositor;
573 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400574 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400575 }
576
577 wm->selection_owner = XCB_WINDOW_NONE;
578
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700579 return 1;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400580 }
581
582 wm->selection_owner = xfixes_selection_notify->owner;
583
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400584 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
585 * selection, so grab the actual timestamp here so we can
586 * answer TIMESTAMP conversion requests correctly. */
587 if (xfixes_selection_notify->owner == wm->selection_window) {
588 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200589 weston_log("our window, skipping\n");
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700590 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400591 }
592
593 wm->incr = 0;
594 xcb_convert_selection(wm->conn, wm->selection_window,
595 wm->atom.clipboard,
596 wm->atom.targets,
597 wm->atom.wl_selection,
598 xfixes_selection_notify->timestamp);
599
600 xcb_flush(wm->conn);
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700601
602 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400603}
604
605int
606weston_wm_handle_selection_event(struct weston_wm *wm,
607 xcb_generic_event_t *event)
608{
609 switch (event->response_type & ~0x80) {
610 case XCB_SELECTION_NOTIFY:
611 weston_wm_handle_selection_notify(wm, event);
612 return 1;
613 case XCB_PROPERTY_NOTIFY:
614 return weston_wm_handle_selection_property_notify(wm, event);
615 case XCB_SELECTION_REQUEST:
616 weston_wm_handle_selection_request(wm, event);
617 return 1;
618 }
619
620 switch (event->response_type - wm->xfixes->first_event) {
621 case XCB_XFIXES_SELECTION_NOTIFY:
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700622 return weston_wm_handle_xfixes_selection_notify(wm, event);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400623 }
624
625 return 0;
626}
627
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200628static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400629weston_wm_set_selection(struct wl_listener *listener, void *data)
630{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400631 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400632 struct weston_wm *wm =
633 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700634 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400635 const char **p, **end;
636 int has_text_plain = 0;
637
Kristian Høgsberg80566732012-06-01 00:08:12 -0400638 if (source == NULL) {
639 if (wm->selection_owner == wm->selection_window)
640 xcb_set_selection_owner(wm->conn,
641 XCB_ATOM_NONE,
642 wm->atom.clipboard,
643 wm->selection_timestamp);
644 return;
645 }
646
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400647 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400648 return;
649
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400650 p = source->mime_types.data;
651 end = (const char **)
652 ((char *) source->mime_types.data + source->mime_types.size);
653 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200654 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400655 if (strcmp(*p, "text/plain") == 0 ||
656 strcmp(*p, "text/plain;charset=utf-8") == 0)
657 has_text_plain = 1;
658 p++;
659 }
660
661 if (has_text_plain) {
662 xcb_set_selection_owner(wm->conn,
663 wm->selection_window,
664 wm->atom.clipboard,
665 XCB_TIME_CURRENT_TIME);
666 } else {
667 xcb_set_selection_owner(wm->conn,
668 XCB_ATOM_NONE,
669 wm->atom.clipboard,
670 XCB_TIME_CURRENT_TIME);
671 }
672}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400673
674void
675weston_wm_selection_init(struct weston_wm *wm)
676{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400677 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400678 uint32_t values[1], mask;
679
680 wm->selection_request.requestor = XCB_NONE;
681
682 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
683 wm->selection_window = xcb_generate_id(wm->conn);
684 xcb_create_window(wm->conn,
685 XCB_COPY_FROM_PARENT,
686 wm->selection_window,
687 wm->screen->root,
688 0, 0,
689 10, 10,
690 0,
691 XCB_WINDOW_CLASS_INPUT_OUTPUT,
692 wm->screen->root_visual,
693 XCB_CW_EVENT_MASK, values);
694
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400695 xcb_set_selection_owner(wm->conn,
696 wm->selection_window,
697 wm->atom.clipboard_manager,
698 XCB_TIME_CURRENT_TIME);
699
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400700 mask =
701 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
702 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
703 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
704 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
705 wm->atom.clipboard, mask);
706
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400707 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400708 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400709 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400710
711 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400712}