blob: 24bd56df598f82fc609c8c7823a29bd1de86fd51 [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);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020049 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040050 close(fd);
Martin Minarik6d118362012-06-07 18:01:59 +020051 weston_log("write error to target fd: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040052 return 1;
53 }
54
Martin Minarik6d118362012-06-07 18:01:59 +020055 weston_log("wrote %d (chunk size %d) of %d bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -040056 wm->property_start + len,
57 len, xcb_get_property_value_length(wm->property_reply));
58
59 wm->property_start += len;
60 if (len == remainder) {
61 free(wm->property_reply);
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070062 wm->property_reply = NULL;
63 if (wm->property_source)
64 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +020065 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -040066
67 if (wm->incr) {
68 xcb_delete_property(wm->conn,
69 wm->selection_window,
70 wm->atom.wl_selection);
71 } else {
Martin Minarik6d118362012-06-07 18:01:59 +020072 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -040073 close(fd);
74 }
75 }
76
77 return 1;
78}
79
80static void
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -070081weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply)
82{
83 wm->property_start = 0;
84 wm->property_reply = reply;
85 writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm);
86
87 if (wm->property_reply)
88 wm->property_source =
89 wl_event_loop_add_fd(wm->server->loop,
90 wm->data_source_fd,
91 WL_EVENT_WRITABLE,
92 writable_callback, wm);
93}
94
95static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -040096weston_wm_get_incr_chunk(struct weston_wm *wm)
97{
98 xcb_get_property_cookie_t cookie;
99 xcb_get_property_reply_t *reply;
100
101 cookie = xcb_get_property(wm->conn,
102 0, /* delete */
103 wm->selection_window,
104 wm->atom.wl_selection,
105 XCB_GET_PROPERTY_TYPE_ANY,
106 0, /* offset */
107 0x1fffffff /* length */);
108
109 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
110
111 dump_property(wm, wm->atom.wl_selection, reply);
112
113 if (xcb_get_property_value_length(reply) > 0) {
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700114 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400115 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200116 weston_log("transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400117 close(wm->data_source_fd);
118 free(reply);
119 }
120}
121
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400122struct x11_data_source {
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700123 struct weston_data_source base;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400124 struct weston_wm *wm;
125};
126
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400127static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700128data_source_accept(struct weston_data_source *source,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400129 uint32_t time, const char *mime_type)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400130{
131}
132
133static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700134data_source_send(struct weston_data_source *base,
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400135 const char *mime_type, int32_t fd)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400136{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400137 struct x11_data_source *source = (struct x11_data_source *) base;
138 struct weston_wm *wm = source->wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400139
140 if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
141 /* Get data for the utf8_string target */
142 xcb_convert_selection(wm->conn,
143 wm->selection_window,
144 wm->atom.clipboard,
145 wm->atom.utf8_string,
146 wm->atom.wl_selection,
147 XCB_TIME_CURRENT_TIME);
148
149 xcb_flush(wm->conn);
150
151 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
Kristian Høgsberg668fc0d2013-09-04 20:48:46 -0700152 wm->data_source_fd = fd;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400153 }
154}
155
156static void
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700157data_source_cancel(struct weston_data_source *source)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400158{
159}
160
161static void
162weston_wm_get_selection_targets(struct weston_wm *wm)
163{
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400164 struct x11_data_source *source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400165 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400166 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400167 xcb_get_property_cookie_t cookie;
168 xcb_get_property_reply_t *reply;
169 xcb_atom_t *value;
170 char **p;
171 uint32_t i;
172
173 cookie = xcb_get_property(wm->conn,
174 1, /* delete */
175 wm->selection_window,
176 wm->atom.wl_selection,
177 XCB_GET_PROPERTY_TYPE_ANY,
178 0, /* offset */
179 4096 /* length */);
180
181 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
182
183 dump_property(wm, wm->atom.wl_selection, reply);
184
185 if (reply->type != XCB_ATOM_ATOM) {
186 free(reply);
187 return;
188 }
189
190 source = malloc(sizeof *source);
191 if (source == NULL)
192 return;
193
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500194 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400195 source->base.accept = data_source_accept;
196 source->base.send = data_source_send;
197 source->base.cancel = data_source_cancel;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400198 source->wm = wm;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400199
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400200 wl_array_init(&source->base.mime_types);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400201 value = xcb_get_property_value(reply);
202 for (i = 0; i < reply->value_len; i++) {
203 if (value[i] == wm->atom.utf8_string) {
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400204 p = wl_array_add(&source->base.mime_types, sizeof *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400205 if (p)
206 *p = strdup("text/plain;charset=utf-8");
207 }
208 }
209
210 compositor = wm->server->compositor;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400211 weston_seat_set_selection(seat, &source->base,
212 wl_display_next_serial(compositor->wl_display));
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400213
214 free(reply);
215}
216
217static void
218weston_wm_get_selection_data(struct weston_wm *wm)
219{
220 xcb_get_property_cookie_t cookie;
221 xcb_get_property_reply_t *reply;
222
223 cookie = xcb_get_property(wm->conn,
224 1, /* delete */
225 wm->selection_window,
226 wm->atom.wl_selection,
227 XCB_GET_PROPERTY_TYPE_ANY,
228 0, /* offset */
229 0x1fffffff /* length */);
230
231 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
232
233 if (reply->type == wm->atom.incr) {
234 dump_property(wm, wm->atom.wl_selection, reply);
235 wm->incr = 1;
236 free(reply);
237 } else {
238 dump_property(wm, wm->atom.wl_selection, reply);
239 wm->incr = 0;
Kristian Høgsberg3f7fcf82013-09-04 22:12:28 -0700240 weston_wm_write_property(wm, reply);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400241 }
242}
243
244static void
245weston_wm_handle_selection_notify(struct weston_wm *wm,
246 xcb_generic_event_t *event)
247{
248 xcb_selection_notify_event_t *selection_notify =
249 (xcb_selection_notify_event_t *) event;
250
251 if (selection_notify->property == XCB_ATOM_NONE) {
252 /* convert selection failed */
253 } else if (selection_notify->target == wm->atom.targets) {
254 weston_wm_get_selection_targets(wm);
255 } else {
256 weston_wm_get_selection_data(wm);
257 }
258}
259
260static const size_t incr_chunk_size = 64 * 1024;
261
262static void
263weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property)
264{
265 xcb_selection_notify_event_t selection_notify;
266
267 memset(&selection_notify, 0, sizeof selection_notify);
268 selection_notify.response_type = XCB_SELECTION_NOTIFY;
269 selection_notify.sequence = 0;
270 selection_notify.time = wm->selection_request.time;
271 selection_notify.requestor = wm->selection_request.requestor;
272 selection_notify.selection = wm->selection_request.selection;
273 selection_notify.target = wm->selection_request.target;
274 selection_notify.property = property;
275
276 xcb_send_event(wm->conn, 0, /* propagate */
277 wm->selection_request.requestor,
278 XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify);
279}
280
281static void
282weston_wm_send_targets(struct weston_wm *wm)
283{
284 xcb_atom_t targets[] = {
285 wm->atom.timestamp,
286 wm->atom.targets,
287 wm->atom.utf8_string,
288 /* wm->atom.compound_text, */
289 wm->atom.text,
290 /* wm->atom.string */
291 };
292
293 xcb_change_property(wm->conn,
294 XCB_PROP_MODE_REPLACE,
295 wm->selection_request.requestor,
296 wm->selection_request.property,
297 XCB_ATOM_ATOM,
298 32, /* format */
299 ARRAY_LENGTH(targets), targets);
300
301 weston_wm_send_selection_notify(wm, wm->selection_request.property);
302}
303
304static void
305weston_wm_send_timestamp(struct weston_wm *wm)
306{
307 xcb_change_property(wm->conn,
308 XCB_PROP_MODE_REPLACE,
309 wm->selection_request.requestor,
310 wm->selection_request.property,
311 XCB_ATOM_INTEGER,
312 32, /* format */
313 1, &wm->selection_timestamp);
314
315 weston_wm_send_selection_notify(wm, wm->selection_request.property);
316}
317
318static int
319weston_wm_flush_source_data(struct weston_wm *wm)
320{
321 int length;
322
323 xcb_change_property(wm->conn,
324 XCB_PROP_MODE_REPLACE,
325 wm->selection_request.requestor,
326 wm->selection_request.property,
327 wm->selection_target,
328 8, /* format */
329 wm->source_data.size,
330 wm->source_data.data);
331 wm->selection_property_set = 1;
332 length = wm->source_data.size;
333 wm->source_data.size = 0;
334
335 return length;
336}
337
338static int
339weston_wm_read_data_source(int fd, uint32_t mask, void *data)
340{
341 struct weston_wm *wm = data;
342 int len, current, available;
343 void *p;
344
345 current = wm->source_data.size;
346 if (wm->source_data.size < incr_chunk_size)
347 p = wl_array_add(&wm->source_data, incr_chunk_size);
348 else
349 p = (char *) wm->source_data.data + wm->source_data.size;
350 available = wm->source_data.alloc - current;
351
352 len = read(fd, p, available);
353 if (len == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200354 weston_log("read error from data source: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400355 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
356 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200357 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400358 close(fd);
359 wl_array_release(&wm->source_data);
360 }
361
Martin Minarik6d118362012-06-07 18:01:59 +0200362 weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400363 len, available, mask, len, (char *) p);
364
365 wm->source_data.size = current + len;
366 if (wm->source_data.size >= incr_chunk_size) {
367 if (!wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200368 weston_log("got %zu bytes, starting incr\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400369 wm->source_data.size);
370 wm->incr = 1;
371 xcb_change_property(wm->conn,
372 XCB_PROP_MODE_REPLACE,
373 wm->selection_request.requestor,
374 wm->selection_request.property,
375 wm->atom.incr,
376 32, /* format */
377 1, &incr_chunk_size);
378 wm->selection_property_set = 1;
379 wm->flush_property_on_delete = 1;
380 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200381 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400382 weston_wm_send_selection_notify(wm, wm->selection_request.property);
383 } else if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200384 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400385 "property delete\n", wm->source_data.size);
386
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 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200391 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400392 "property deleted, seting new property\n",
393 wm->source_data.size);
394 weston_wm_flush_source_data(wm);
395 }
396 } else if (len == 0 && !wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200397 weston_log("non-incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400398 /* Non-incr transfer all done. */
399 weston_wm_flush_source_data(wm);
400 weston_wm_send_selection_notify(wm, wm->selection_request.property);
401 xcb_flush(wm->conn);
402 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200403 wm->property_source = NULL;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400404 close(fd);
405 wl_array_release(&wm->source_data);
406 wm->selection_request.requestor = XCB_NONE;
407 } else if (len == 0 && wm->incr) {
Martin Minarik6d118362012-06-07 18:01:59 +0200408 weston_log("incr transfer complete\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400409
410 wm->flush_property_on_delete = 1;
411 if (wm->selection_property_set) {
Martin Minarik6d118362012-06-07 18:01:59 +0200412 weston_log("got %zu bytes, waiting for "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400413 "property delete\n", wm->source_data.size);
414 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("got %zu bytes, "
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400416 "property deleted, seting new property\n",
417 wm->source_data.size);
418 weston_wm_flush_source_data(wm);
419 }
420 xcb_flush(wm->conn);
421 wl_event_source_remove(wm->property_source);
Giulio Camuffo9e1aeb82014-12-26 18:10:35 +0200422 wm->property_source = NULL;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400423 close(wm->data_source_fd);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400424 wm->data_source_fd = -1;
425 close(fd);
426 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200427 weston_log("nothing happened, buffered the bytes\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400428 }
429
430 return 1;
431}
432
433static void
434weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type)
435{
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700436 struct weston_data_source *source;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400437 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400438 int p[2];
439
440 if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200441 weston_log("pipe2 failed: %m\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400442 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
443 return;
444 }
445
446 wl_array_init(&wm->source_data);
447 wm->selection_target = target;
448 wm->data_source_fd = p[0];
449 wm->property_source = wl_event_loop_add_fd(wm->server->loop,
450 wm->data_source_fd,
451 WL_EVENT_READABLE,
452 weston_wm_read_data_source,
453 wm);
454
Kristian Høgsberge3148752013-05-06 23:19:49 -0400455 source = seat->selection_data_source;
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400456 source->send(source, mime_type, p[1]);
Kristian Høgsberg73bdc0c2013-09-04 22:32:50 -0700457 close(p[1]);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400458}
459
460static void
461weston_wm_send_incr_chunk(struct weston_wm *wm)
462{
463 int length;
464
Martin Minarik6d118362012-06-07 18:01:59 +0200465 weston_log("property deleted\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400466
467 wm->selection_property_set = 0;
468 if (wm->flush_property_on_delete) {
Martin Minarik6d118362012-06-07 18:01:59 +0200469 weston_log("setting new property, %zu bytes\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400470 wm->source_data.size);
471 wm->flush_property_on_delete = 0;
472 length = weston_wm_flush_source_data(wm);
473
474 if (wm->data_source_fd >= 0) {
475 wm->property_source =
476 wl_event_loop_add_fd(wm->server->loop,
477 wm->data_source_fd,
478 WL_EVENT_READABLE,
479 weston_wm_read_data_source,
480 wm);
481 } else if (length > 0) {
482 /* Transfer is all done, but queue a flush for
483 * the delete of the last chunk so we can set
484 * the 0 sized propert to signal the end of
485 * the transfer. */
486 wm->flush_property_on_delete = 1;
487 wl_array_release(&wm->source_data);
488 } else {
489 wm->selection_request.requestor = XCB_NONE;
490 }
491 }
492}
493
494static int
495weston_wm_handle_selection_property_notify(struct weston_wm *wm,
496 xcb_generic_event_t *event)
497{
498 xcb_property_notify_event_t *property_notify =
499 (xcb_property_notify_event_t *) event;
500
501 if (property_notify->window == wm->selection_window) {
502 if (property_notify->state == XCB_PROPERTY_NEW_VALUE &&
503 property_notify->atom == wm->atom.wl_selection &&
504 wm->incr)
505 weston_wm_get_incr_chunk(wm);
506 return 1;
507 } else if (property_notify->window == wm->selection_request.requestor) {
508 if (property_notify->state == XCB_PROPERTY_DELETE &&
509 property_notify->atom == wm->selection_request.property &&
510 wm->incr)
511 weston_wm_send_incr_chunk(wm);
512 return 1;
513 }
514
515 return 0;
516}
517
518static void
519weston_wm_handle_selection_request(struct weston_wm *wm,
520 xcb_generic_event_t *event)
521{
522 xcb_selection_request_event_t *selection_request =
523 (xcb_selection_request_event_t *) event;
524
Martin Minarik6d118362012-06-07 18:01:59 +0200525 weston_log("selection request, %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400526 get_atom_name(wm->conn, selection_request->selection));
Martin Minarik6d118362012-06-07 18:01:59 +0200527 weston_log_continue("target %s, ",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400528 get_atom_name(wm->conn, selection_request->target));
Martin Minarik6d118362012-06-07 18:01:59 +0200529 weston_log_continue("property %s\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400530 get_atom_name(wm->conn, selection_request->property));
531
532 wm->selection_request = *selection_request;
533 wm->incr = 0;
534 wm->flush_property_on_delete = 0;
535
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400536 if (selection_request->selection == wm->atom.clipboard_manager) {
537 /* The weston clipboard should already have grabbed
538 * the first target, so just send selection notify
539 * now. This isn't synchronized with the clipboard
540 * finishing getting the data, so there's a race here. */
541 weston_wm_send_selection_notify(wm, wm->selection_request.property);
542 return;
543 }
544
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400545 if (selection_request->target == wm->atom.targets) {
546 weston_wm_send_targets(wm);
547 } else if (selection_request->target == wm->atom.timestamp) {
548 weston_wm_send_timestamp(wm);
549 } else if (selection_request->target == wm->atom.utf8_string ||
550 selection_request->target == wm->atom.text) {
551 weston_wm_send_data(wm, wm->atom.utf8_string,
552 "text/plain;charset=utf-8");
553 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200554 weston_log("can only handle UTF8_STRING targets...\n");
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400555 weston_wm_send_selection_notify(wm, XCB_ATOM_NONE);
556 }
557}
558
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700559static int
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400560weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm,
561 xcb_generic_event_t *event)
562{
563 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
564 (xcb_xfixes_selection_notify_event_t *) event;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400565 struct weston_compositor *compositor;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400566 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400567 uint32_t serial;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400568
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700569 if (xfixes_selection_notify->selection != wm->atom.clipboard)
570 return 0;
571
Martin Minarik6d118362012-06-07 18:01:59 +0200572 weston_log("xfixes selection notify event: owner %d\n",
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400573 xfixes_selection_notify->owner);
574
Kristian Høgsberg80566732012-06-01 00:08:12 -0400575 if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) {
576 if (wm->selection_owner != wm->selection_window) {
577 /* A real X client selection went away, not our
578 * proxy selection. Clear the wayland selection. */
579 compositor = wm->server->compositor;
580 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400581 weston_seat_set_selection(seat, NULL, serial);
Kristian Høgsberg80566732012-06-01 00:08:12 -0400582 }
583
584 wm->selection_owner = XCB_WINDOW_NONE;
585
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700586 return 1;
Kristian Høgsberg80566732012-06-01 00:08:12 -0400587 }
588
589 wm->selection_owner = xfixes_selection_notify->owner;
590
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400591 /* We have to use XCB_TIME_CURRENT_TIME when we claim the
592 * selection, so grab the actual timestamp here so we can
593 * answer TIMESTAMP conversion requests correctly. */
594 if (xfixes_selection_notify->owner == wm->selection_window) {
595 wm->selection_timestamp = xfixes_selection_notify->timestamp;
Martin Minarik6d118362012-06-07 18:01:59 +0200596 weston_log("our window, skipping\n");
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700597 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400598 }
599
600 wm->incr = 0;
601 xcb_convert_selection(wm->conn, wm->selection_window,
602 wm->atom.clipboard,
603 wm->atom.targets,
604 wm->atom.wl_selection,
605 xfixes_selection_notify->timestamp);
606
607 xcb_flush(wm->conn);
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700608
609 return 1;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400610}
611
612int
613weston_wm_handle_selection_event(struct weston_wm *wm,
614 xcb_generic_event_t *event)
615{
616 switch (event->response_type & ~0x80) {
617 case XCB_SELECTION_NOTIFY:
618 weston_wm_handle_selection_notify(wm, event);
619 return 1;
620 case XCB_PROPERTY_NOTIFY:
621 return weston_wm_handle_selection_property_notify(wm, event);
622 case XCB_SELECTION_REQUEST:
623 weston_wm_handle_selection_request(wm, event);
624 return 1;
625 }
626
627 switch (event->response_type - wm->xfixes->first_event) {
628 case XCB_XFIXES_SELECTION_NOTIFY:
Kristian Høgsberg9466e502013-09-04 21:09:24 -0700629 return weston_wm_handle_xfixes_selection_notify(wm, event);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400630 }
631
632 return 0;
633}
634
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200635static void
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400636weston_wm_set_selection(struct wl_listener *listener, void *data)
637{
Kristian Høgsberge3148752013-05-06 23:19:49 -0400638 struct weston_seat *seat = data;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400639 struct weston_wm *wm =
640 container_of(listener, struct weston_wm, selection_listener);
Kristian Høgsberg7ff3bdb2013-07-25 15:52:14 -0700641 struct weston_data_source *source = seat->selection_data_source;
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400642 const char **p, **end;
643 int has_text_plain = 0;
644
Kristian Høgsberg80566732012-06-01 00:08:12 -0400645 if (source == NULL) {
646 if (wm->selection_owner == wm->selection_window)
647 xcb_set_selection_owner(wm->conn,
648 XCB_ATOM_NONE,
649 wm->atom.clipboard,
650 wm->selection_timestamp);
651 return;
652 }
653
Kristian Høgsbergc65d56a2012-06-02 21:23:01 -0400654 if (source->send == data_source_send)
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400655 return;
656
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400657 p = source->mime_types.data;
658 end = (const char **)
659 ((char *) source->mime_types.data + source->mime_types.size);
660 while (p < end) {
Martin Minarik6d118362012-06-07 18:01:59 +0200661 weston_log(" %s\n", *p);
Kristian Høgsberg102bf032012-05-21 15:52:02 -0400662 if (strcmp(*p, "text/plain") == 0 ||
663 strcmp(*p, "text/plain;charset=utf-8") == 0)
664 has_text_plain = 1;
665 p++;
666 }
667
668 if (has_text_plain) {
669 xcb_set_selection_owner(wm->conn,
670 wm->selection_window,
671 wm->atom.clipboard,
672 XCB_TIME_CURRENT_TIME);
673 } else {
674 xcb_set_selection_owner(wm->conn,
675 XCB_ATOM_NONE,
676 wm->atom.clipboard,
677 XCB_TIME_CURRENT_TIME);
678 }
679}
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400680
681void
682weston_wm_selection_init(struct weston_wm *wm)
683{
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400684 struct weston_seat *seat;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400685 uint32_t values[1], mask;
686
687 wm->selection_request.requestor = XCB_NONE;
688
689 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
690 wm->selection_window = xcb_generate_id(wm->conn);
691 xcb_create_window(wm->conn,
692 XCB_COPY_FROM_PARENT,
693 wm->selection_window,
694 wm->screen->root,
695 0, 0,
696 10, 10,
697 0,
698 XCB_WINDOW_CLASS_INPUT_OUTPUT,
699 wm->screen->root_visual,
700 XCB_CW_EVENT_MASK, values);
701
Kristian Høgsbergcba022a2012-06-04 10:11:45 -0400702 xcb_set_selection_owner(wm->conn,
703 wm->selection_window,
704 wm->atom.clipboard_manager,
705 XCB_TIME_CURRENT_TIME);
706
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400707 mask =
708 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
709 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
710 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
711 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
712 wm->atom.clipboard, mask);
713
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400714 seat = weston_wm_pick_seat(wm);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400715 wm->selection_listener.notify = weston_wm_set_selection;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400716 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
Kristian Høgsberge2203272012-06-03 10:32:48 -0400717
718 weston_wm_set_selection(&wm->selection_listener, seat);
Kristian Høgsberg4dec0112012-06-03 09:18:06 -0400719}