Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 1 | /* |
| 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 Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <fcntl.h> |
| 29 | |
| 30 | #include "xwayland.h" |
| 31 | |
| 32 | static int |
| 33 | weston_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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 48 | weston_log("write error to target fd: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 49 | return 1; |
| 50 | } |
| 51 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 52 | weston_log("wrote %d (chunk size %d) of %d bytes\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 53 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 66 | weston_log("transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 67 | close(fd); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | weston_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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 102 | weston_log("transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 103 | close(wm->data_source_fd); |
| 104 | free(reply); |
| 105 | } |
| 106 | } |
| 107 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 108 | struct x11_data_source { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 109 | struct weston_data_source base; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 110 | struct weston_wm *wm; |
| 111 | }; |
| 112 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 113 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 114 | data_source_accept(struct weston_data_source *source, |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 115 | uint32_t time, const char *mime_type) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 116 | { |
| 117 | } |
| 118 | |
| 119 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 120 | data_source_send(struct weston_data_source *base, |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 121 | const char *mime_type, int32_t fd) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 122 | { |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 123 | struct x11_data_source *source = (struct x11_data_source *) base; |
| 124 | struct weston_wm *wm = source->wm; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 125 | |
| 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øgsberg | 668fc0d | 2013-09-04 20:48:46 -0700 | [diff] [blame] | 138 | wm->data_source_fd = fd; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 143 | data_source_cancel(struct weston_data_source *source) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 144 | { |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | weston_wm_get_selection_targets(struct weston_wm *wm) |
| 149 | { |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 150 | struct x11_data_source *source; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 151 | struct weston_compositor *compositor; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 152 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 153 | 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 Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 180 | wl_signal_init(&source->base.destroy_signal); |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 181 | source->base.accept = data_source_accept; |
| 182 | source->base.send = data_source_send; |
| 183 | source->base.cancel = data_source_cancel; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 184 | source->wm = wm; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 185 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 186 | wl_array_init(&source->base.mime_types); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 187 | 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øgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 190 | p = wl_array_add(&source->base.mime_types, sizeof *p); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 191 | if (p) |
| 192 | *p = strdup("text/plain;charset=utf-8"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | compositor = wm->server->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 197 | weston_seat_set_selection(seat, &source->base, |
| 198 | wl_display_next_serial(compositor->wl_display)); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 199 | |
| 200 | free(reply); |
| 201 | } |
| 202 | |
| 203 | static void |
| 204 | weston_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 | |
| 237 | static void |
| 238 | weston_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 | |
| 253 | static const size_t incr_chunk_size = 64 * 1024; |
| 254 | |
| 255 | static void |
| 256 | weston_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 | |
| 274 | static void |
| 275 | weston_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 | |
| 297 | static void |
| 298 | weston_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 | |
| 311 | static int |
| 312 | weston_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 | |
| 331 | static int |
| 332 | weston_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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 347 | weston_log("read error from data source: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 348 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 354 | weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 355 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 360 | weston_log("got %zu bytes, starting incr\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 361 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 375 | weston_log("got %zu bytes, waiting for " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 376 | "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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 381 | weston_log("got %zu bytes, " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 382 | "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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 387 | weston_log("non-incr transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 388 | /* 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 397 | weston_log("incr transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 398 | |
| 399 | wm->flush_property_on_delete = 1; |
| 400 | if (wm->selection_property_set) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 401 | weston_log("got %zu bytes, waiting for " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 402 | "property delete\n", wm->source_data.size); |
| 403 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 404 | weston_log("got %zu bytes, " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 405 | "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øgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 411 | close(wm->data_source_fd); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 412 | wm->data_source_fd = -1; |
| 413 | close(fd); |
| 414 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 415 | weston_log("nothing happened, buffered the bytes\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type) |
| 423 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 424 | struct weston_data_source *source; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 425 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 426 | int p[2]; |
| 427 | |
| 428 | if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 429 | weston_log("pipe2 failed: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 430 | 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øgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 443 | source = seat->selection_data_source; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 444 | source->send(source, mime_type, p[1]); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static void |
| 448 | weston_wm_send_incr_chunk(struct weston_wm *wm) |
| 449 | { |
| 450 | int length; |
| 451 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 452 | weston_log("property deleted\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 453 | |
| 454 | wm->selection_property_set = 0; |
| 455 | if (wm->flush_property_on_delete) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 456 | weston_log("setting new property, %zu bytes\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 457 | 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 | |
| 481 | static int |
| 482 | weston_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 | |
| 505 | static void |
| 506 | weston_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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 512 | weston_log("selection request, %s, ", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 513 | get_atom_name(wm->conn, selection_request->selection)); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 514 | weston_log_continue("target %s, ", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 515 | get_atom_name(wm->conn, selection_request->target)); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 516 | weston_log_continue("property %s\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 517 | 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øgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 523 | 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øgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 532 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 541 | weston_log("can only handle UTF8_STRING targets...\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 542 | weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); |
| 543 | } |
| 544 | } |
| 545 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 546 | static int |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 547 | weston_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øgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 552 | struct weston_compositor *compositor; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 553 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 554 | uint32_t serial; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 555 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 556 | if (xfixes_selection_notify->selection != wm->atom.clipboard) |
| 557 | return 0; |
| 558 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 559 | weston_log("xfixes selection notify event: owner %d\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 560 | xfixes_selection_notify->owner); |
| 561 | |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 562 | if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) { |
| 563 | if (wm->selection_owner != wm->selection_window) { |
| 564 | /* A real X client selection went away, not our |
| 565 | * proxy selection. Clear the wayland selection. */ |
| 566 | compositor = wm->server->compositor; |
| 567 | serial = wl_display_next_serial(compositor->wl_display); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 568 | weston_seat_set_selection(seat, NULL, serial); |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | wm->selection_owner = XCB_WINDOW_NONE; |
| 572 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 573 | return 1; |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | wm->selection_owner = xfixes_selection_notify->owner; |
| 577 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 578 | /* We have to use XCB_TIME_CURRENT_TIME when we claim the |
| 579 | * selection, so grab the actual timestamp here so we can |
| 580 | * answer TIMESTAMP conversion requests correctly. */ |
| 581 | if (xfixes_selection_notify->owner == wm->selection_window) { |
| 582 | wm->selection_timestamp = xfixes_selection_notify->timestamp; |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 583 | weston_log("our window, skipping\n"); |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 584 | return 1; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | wm->incr = 0; |
| 588 | xcb_convert_selection(wm->conn, wm->selection_window, |
| 589 | wm->atom.clipboard, |
| 590 | wm->atom.targets, |
| 591 | wm->atom.wl_selection, |
| 592 | xfixes_selection_notify->timestamp); |
| 593 | |
| 594 | xcb_flush(wm->conn); |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 595 | |
| 596 | return 1; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | int |
| 600 | weston_wm_handle_selection_event(struct weston_wm *wm, |
| 601 | xcb_generic_event_t *event) |
| 602 | { |
| 603 | switch (event->response_type & ~0x80) { |
| 604 | case XCB_SELECTION_NOTIFY: |
| 605 | weston_wm_handle_selection_notify(wm, event); |
| 606 | return 1; |
| 607 | case XCB_PROPERTY_NOTIFY: |
| 608 | return weston_wm_handle_selection_property_notify(wm, event); |
| 609 | case XCB_SELECTION_REQUEST: |
| 610 | weston_wm_handle_selection_request(wm, event); |
| 611 | return 1; |
| 612 | } |
| 613 | |
| 614 | switch (event->response_type - wm->xfixes->first_event) { |
| 615 | case XCB_XFIXES_SELECTION_NOTIFY: |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame^] | 616 | return weston_wm_handle_xfixes_selection_notify(wm, event); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
Tiago Vignatti | 2d129f1 | 2012-11-30 17:19:59 -0200 | [diff] [blame] | 622 | static void |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 623 | weston_wm_set_selection(struct wl_listener *listener, void *data) |
| 624 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 625 | struct weston_seat *seat = data; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 626 | struct weston_wm *wm = |
| 627 | container_of(listener, struct weston_wm, selection_listener); |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 628 | struct weston_data_source *source = seat->selection_data_source; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 629 | const char **p, **end; |
| 630 | int has_text_plain = 0; |
| 631 | |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 632 | if (source == NULL) { |
| 633 | if (wm->selection_owner == wm->selection_window) |
| 634 | xcb_set_selection_owner(wm->conn, |
| 635 | XCB_ATOM_NONE, |
| 636 | wm->atom.clipboard, |
| 637 | wm->selection_timestamp); |
| 638 | return; |
| 639 | } |
| 640 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 641 | if (source->send == data_source_send) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 642 | return; |
| 643 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 644 | p = source->mime_types.data; |
| 645 | end = (const char **) |
| 646 | ((char *) source->mime_types.data + source->mime_types.size); |
| 647 | while (p < end) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 648 | weston_log(" %s\n", *p); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 649 | if (strcmp(*p, "text/plain") == 0 || |
| 650 | strcmp(*p, "text/plain;charset=utf-8") == 0) |
| 651 | has_text_plain = 1; |
| 652 | p++; |
| 653 | } |
| 654 | |
| 655 | if (has_text_plain) { |
| 656 | xcb_set_selection_owner(wm->conn, |
| 657 | wm->selection_window, |
| 658 | wm->atom.clipboard, |
| 659 | XCB_TIME_CURRENT_TIME); |
| 660 | } else { |
| 661 | xcb_set_selection_owner(wm->conn, |
| 662 | XCB_ATOM_NONE, |
| 663 | wm->atom.clipboard, |
| 664 | XCB_TIME_CURRENT_TIME); |
| 665 | } |
| 666 | } |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 667 | |
| 668 | void |
| 669 | weston_wm_selection_init(struct weston_wm *wm) |
| 670 | { |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 671 | struct weston_seat *seat; |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 672 | uint32_t values[1], mask; |
| 673 | |
| 674 | wm->selection_request.requestor = XCB_NONE; |
| 675 | |
| 676 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 677 | wm->selection_window = xcb_generate_id(wm->conn); |
| 678 | xcb_create_window(wm->conn, |
| 679 | XCB_COPY_FROM_PARENT, |
| 680 | wm->selection_window, |
| 681 | wm->screen->root, |
| 682 | 0, 0, |
| 683 | 10, 10, |
| 684 | 0, |
| 685 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 686 | wm->screen->root_visual, |
| 687 | XCB_CW_EVENT_MASK, values); |
| 688 | |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 689 | xcb_set_selection_owner(wm->conn, |
| 690 | wm->selection_window, |
| 691 | wm->atom.clipboard_manager, |
| 692 | XCB_TIME_CURRENT_TIME); |
| 693 | |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 694 | mask = |
| 695 | XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | |
| 696 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | |
| 697 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; |
| 698 | xcb_xfixes_select_selection_input(wm->conn, wm->selection_window, |
| 699 | wm->atom.clipboard, mask); |
| 700 | |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 701 | seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 702 | wm->selection_listener.notify = weston_wm_set_selection; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 703 | wl_signal_add(&seat->selection_signal, &wm->selection_listener); |
Kristian Høgsberg | e220327 | 2012-06-03 10:32:48 -0400 | [diff] [blame] | 704 | |
| 705 | weston_wm_set_selection(&wm->selection_listener, seat); |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 706 | } |