Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame^] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame^] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 27 | |
| 28 | #include <stdlib.h> |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include "xwayland.h" |
| 34 | |
| 35 | static int |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 36 | writable_callback(int fd, uint32_t mask, void *data) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 37 | { |
| 38 | struct weston_wm *wm = data; |
| 39 | unsigned char *property; |
| 40 | int len, remainder; |
| 41 | |
| 42 | property = xcb_get_property_value(wm->property_reply); |
| 43 | remainder = xcb_get_property_value_length(wm->property_reply) - |
| 44 | wm->property_start; |
| 45 | |
| 46 | len = write(fd, property + wm->property_start, remainder); |
| 47 | if (len == -1) { |
| 48 | free(wm->property_reply); |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 49 | wm->property_reply = NULL; |
| 50 | if (wm->property_source) |
| 51 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 52 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 53 | close(fd); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 54 | weston_log("write error to target fd: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 58 | weston_log("wrote %d (chunk size %d) of %d bytes\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 59 | wm->property_start + len, |
| 60 | len, xcb_get_property_value_length(wm->property_reply)); |
| 61 | |
| 62 | wm->property_start += len; |
| 63 | if (len == remainder) { |
| 64 | free(wm->property_reply); |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 65 | wm->property_reply = NULL; |
| 66 | if (wm->property_source) |
| 67 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 68 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 69 | |
| 70 | if (wm->incr) { |
| 71 | xcb_delete_property(wm->conn, |
| 72 | wm->selection_window, |
| 73 | wm->atom.wl_selection); |
| 74 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 75 | weston_log("transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 76 | close(fd); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | static void |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 84 | weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply) |
| 85 | { |
| 86 | wm->property_start = 0; |
| 87 | wm->property_reply = reply; |
| 88 | writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm); |
| 89 | |
| 90 | if (wm->property_reply) |
| 91 | wm->property_source = |
| 92 | wl_event_loop_add_fd(wm->server->loop, |
| 93 | wm->data_source_fd, |
| 94 | WL_EVENT_WRITABLE, |
| 95 | writable_callback, wm); |
| 96 | } |
| 97 | |
| 98 | static void |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 99 | weston_wm_get_incr_chunk(struct weston_wm *wm) |
| 100 | { |
| 101 | xcb_get_property_cookie_t cookie; |
| 102 | xcb_get_property_reply_t *reply; |
| 103 | |
| 104 | cookie = xcb_get_property(wm->conn, |
| 105 | 0, /* delete */ |
| 106 | wm->selection_window, |
| 107 | wm->atom.wl_selection, |
| 108 | XCB_GET_PROPERTY_TYPE_ANY, |
| 109 | 0, /* offset */ |
| 110 | 0x1fffffff /* length */); |
| 111 | |
| 112 | reply = xcb_get_property_reply(wm->conn, cookie, NULL); |
| 113 | |
| 114 | dump_property(wm, wm->atom.wl_selection, reply); |
| 115 | |
| 116 | if (xcb_get_property_value_length(reply) > 0) { |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 117 | weston_wm_write_property(wm, reply); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 118 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 119 | weston_log("transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 120 | close(wm->data_source_fd); |
| 121 | free(reply); |
| 122 | } |
| 123 | } |
| 124 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 125 | struct x11_data_source { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 126 | struct weston_data_source base; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 127 | struct weston_wm *wm; |
| 128 | }; |
| 129 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 130 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 131 | data_source_accept(struct weston_data_source *source, |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 132 | uint32_t time, const char *mime_type) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 133 | { |
| 134 | } |
| 135 | |
| 136 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 137 | data_source_send(struct weston_data_source *base, |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 138 | const char *mime_type, int32_t fd) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 139 | { |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 140 | struct x11_data_source *source = (struct x11_data_source *) base; |
| 141 | struct weston_wm *wm = source->wm; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 142 | |
| 143 | if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { |
| 144 | /* Get data for the utf8_string target */ |
| 145 | xcb_convert_selection(wm->conn, |
| 146 | wm->selection_window, |
| 147 | wm->atom.clipboard, |
| 148 | wm->atom.utf8_string, |
| 149 | wm->atom.wl_selection, |
| 150 | XCB_TIME_CURRENT_TIME); |
| 151 | |
| 152 | xcb_flush(wm->conn); |
| 153 | |
| 154 | fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); |
Kristian Høgsberg | 668fc0d | 2013-09-04 20:48:46 -0700 | [diff] [blame] | 155 | wm->data_source_fd = fd; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 160 | data_source_cancel(struct weston_data_source *source) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 161 | { |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | weston_wm_get_selection_targets(struct weston_wm *wm) |
| 166 | { |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 167 | struct x11_data_source *source; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 168 | struct weston_compositor *compositor; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 169 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 170 | xcb_get_property_cookie_t cookie; |
| 171 | xcb_get_property_reply_t *reply; |
| 172 | xcb_atom_t *value; |
| 173 | char **p; |
| 174 | uint32_t i; |
| 175 | |
| 176 | cookie = xcb_get_property(wm->conn, |
| 177 | 1, /* delete */ |
| 178 | wm->selection_window, |
| 179 | wm->atom.wl_selection, |
| 180 | XCB_GET_PROPERTY_TYPE_ANY, |
| 181 | 0, /* offset */ |
| 182 | 4096 /* length */); |
| 183 | |
| 184 | reply = xcb_get_property_reply(wm->conn, cookie, NULL); |
| 185 | |
| 186 | dump_property(wm, wm->atom.wl_selection, reply); |
| 187 | |
| 188 | if (reply->type != XCB_ATOM_ATOM) { |
| 189 | free(reply); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | source = malloc(sizeof *source); |
| 194 | if (source == NULL) |
| 195 | return; |
| 196 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 197 | wl_signal_init(&source->base.destroy_signal); |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 198 | source->base.accept = data_source_accept; |
| 199 | source->base.send = data_source_send; |
| 200 | source->base.cancel = data_source_cancel; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 201 | source->wm = wm; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 202 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 203 | wl_array_init(&source->base.mime_types); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 204 | value = xcb_get_property_value(reply); |
| 205 | for (i = 0; i < reply->value_len; i++) { |
| 206 | if (value[i] == wm->atom.utf8_string) { |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 207 | p = wl_array_add(&source->base.mime_types, sizeof *p); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 208 | if (p) |
| 209 | *p = strdup("text/plain;charset=utf-8"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | compositor = wm->server->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 214 | weston_seat_set_selection(seat, &source->base, |
| 215 | wl_display_next_serial(compositor->wl_display)); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 216 | |
| 217 | free(reply); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | weston_wm_get_selection_data(struct weston_wm *wm) |
| 222 | { |
| 223 | xcb_get_property_cookie_t cookie; |
| 224 | xcb_get_property_reply_t *reply; |
| 225 | |
| 226 | cookie = xcb_get_property(wm->conn, |
| 227 | 1, /* delete */ |
| 228 | wm->selection_window, |
| 229 | wm->atom.wl_selection, |
| 230 | XCB_GET_PROPERTY_TYPE_ANY, |
| 231 | 0, /* offset */ |
| 232 | 0x1fffffff /* length */); |
| 233 | |
| 234 | reply = xcb_get_property_reply(wm->conn, cookie, NULL); |
| 235 | |
| 236 | if (reply->type == wm->atom.incr) { |
| 237 | dump_property(wm, wm->atom.wl_selection, reply); |
| 238 | wm->incr = 1; |
| 239 | free(reply); |
| 240 | } else { |
| 241 | dump_property(wm, wm->atom.wl_selection, reply); |
| 242 | wm->incr = 0; |
Kristian Høgsberg | 3f7fcf8 | 2013-09-04 22:12:28 -0700 | [diff] [blame] | 243 | weston_wm_write_property(wm, reply); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | weston_wm_handle_selection_notify(struct weston_wm *wm, |
| 249 | xcb_generic_event_t *event) |
| 250 | { |
| 251 | xcb_selection_notify_event_t *selection_notify = |
| 252 | (xcb_selection_notify_event_t *) event; |
| 253 | |
| 254 | if (selection_notify->property == XCB_ATOM_NONE) { |
| 255 | /* convert selection failed */ |
| 256 | } else if (selection_notify->target == wm->atom.targets) { |
| 257 | weston_wm_get_selection_targets(wm); |
| 258 | } else { |
| 259 | weston_wm_get_selection_data(wm); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static const size_t incr_chunk_size = 64 * 1024; |
| 264 | |
| 265 | static void |
| 266 | weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property) |
| 267 | { |
| 268 | xcb_selection_notify_event_t selection_notify; |
| 269 | |
| 270 | memset(&selection_notify, 0, sizeof selection_notify); |
| 271 | selection_notify.response_type = XCB_SELECTION_NOTIFY; |
| 272 | selection_notify.sequence = 0; |
| 273 | selection_notify.time = wm->selection_request.time; |
| 274 | selection_notify.requestor = wm->selection_request.requestor; |
| 275 | selection_notify.selection = wm->selection_request.selection; |
| 276 | selection_notify.target = wm->selection_request.target; |
| 277 | selection_notify.property = property; |
| 278 | |
| 279 | xcb_send_event(wm->conn, 0, /* propagate */ |
| 280 | wm->selection_request.requestor, |
| 281 | XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | weston_wm_send_targets(struct weston_wm *wm) |
| 286 | { |
| 287 | xcb_atom_t targets[] = { |
| 288 | wm->atom.timestamp, |
| 289 | wm->atom.targets, |
| 290 | wm->atom.utf8_string, |
| 291 | /* wm->atom.compound_text, */ |
| 292 | wm->atom.text, |
| 293 | /* wm->atom.string */ |
| 294 | }; |
| 295 | |
| 296 | xcb_change_property(wm->conn, |
| 297 | XCB_PROP_MODE_REPLACE, |
| 298 | wm->selection_request.requestor, |
| 299 | wm->selection_request.property, |
| 300 | XCB_ATOM_ATOM, |
| 301 | 32, /* format */ |
| 302 | ARRAY_LENGTH(targets), targets); |
| 303 | |
| 304 | weston_wm_send_selection_notify(wm, wm->selection_request.property); |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | weston_wm_send_timestamp(struct weston_wm *wm) |
| 309 | { |
| 310 | xcb_change_property(wm->conn, |
| 311 | XCB_PROP_MODE_REPLACE, |
| 312 | wm->selection_request.requestor, |
| 313 | wm->selection_request.property, |
| 314 | XCB_ATOM_INTEGER, |
| 315 | 32, /* format */ |
| 316 | 1, &wm->selection_timestamp); |
| 317 | |
| 318 | weston_wm_send_selection_notify(wm, wm->selection_request.property); |
| 319 | } |
| 320 | |
| 321 | static int |
| 322 | weston_wm_flush_source_data(struct weston_wm *wm) |
| 323 | { |
| 324 | int length; |
| 325 | |
| 326 | xcb_change_property(wm->conn, |
| 327 | XCB_PROP_MODE_REPLACE, |
| 328 | wm->selection_request.requestor, |
| 329 | wm->selection_request.property, |
| 330 | wm->selection_target, |
| 331 | 8, /* format */ |
| 332 | wm->source_data.size, |
| 333 | wm->source_data.data); |
| 334 | wm->selection_property_set = 1; |
| 335 | length = wm->source_data.size; |
| 336 | wm->source_data.size = 0; |
| 337 | |
| 338 | return length; |
| 339 | } |
| 340 | |
| 341 | static int |
| 342 | weston_wm_read_data_source(int fd, uint32_t mask, void *data) |
| 343 | { |
| 344 | struct weston_wm *wm = data; |
| 345 | int len, current, available; |
| 346 | void *p; |
| 347 | |
| 348 | current = wm->source_data.size; |
| 349 | if (wm->source_data.size < incr_chunk_size) |
| 350 | p = wl_array_add(&wm->source_data, incr_chunk_size); |
| 351 | else |
| 352 | p = (char *) wm->source_data.data + wm->source_data.size; |
| 353 | available = wm->source_data.alloc - current; |
| 354 | |
| 355 | len = read(fd, p, available); |
| 356 | if (len == -1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 357 | weston_log("read error from data source: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 358 | weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); |
| 359 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 360 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 361 | close(fd); |
| 362 | wl_array_release(&wm->source_data); |
| 363 | } |
| 364 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 365 | 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] | 366 | len, available, mask, len, (char *) p); |
| 367 | |
| 368 | wm->source_data.size = current + len; |
| 369 | if (wm->source_data.size >= incr_chunk_size) { |
| 370 | if (!wm->incr) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 371 | weston_log("got %zu bytes, starting incr\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 372 | wm->source_data.size); |
| 373 | wm->incr = 1; |
| 374 | xcb_change_property(wm->conn, |
| 375 | XCB_PROP_MODE_REPLACE, |
| 376 | wm->selection_request.requestor, |
| 377 | wm->selection_request.property, |
| 378 | wm->atom.incr, |
| 379 | 32, /* format */ |
| 380 | 1, &incr_chunk_size); |
| 381 | wm->selection_property_set = 1; |
| 382 | wm->flush_property_on_delete = 1; |
| 383 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 384 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 385 | weston_wm_send_selection_notify(wm, wm->selection_request.property); |
| 386 | } else if (wm->selection_property_set) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 387 | weston_log("got %zu bytes, waiting for " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 388 | "property delete\n", wm->source_data.size); |
| 389 | |
| 390 | wm->flush_property_on_delete = 1; |
| 391 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 392 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 393 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 394 | weston_log("got %zu bytes, " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 395 | "property deleted, seting new property\n", |
| 396 | wm->source_data.size); |
| 397 | weston_wm_flush_source_data(wm); |
| 398 | } |
| 399 | } else if (len == 0 && !wm->incr) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 400 | weston_log("non-incr transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 401 | /* Non-incr transfer all done. */ |
| 402 | weston_wm_flush_source_data(wm); |
| 403 | weston_wm_send_selection_notify(wm, wm->selection_request.property); |
| 404 | xcb_flush(wm->conn); |
| 405 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 406 | wm->property_source = NULL; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 407 | close(fd); |
| 408 | wl_array_release(&wm->source_data); |
| 409 | wm->selection_request.requestor = XCB_NONE; |
| 410 | } else if (len == 0 && wm->incr) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 411 | weston_log("incr transfer complete\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 412 | |
| 413 | wm->flush_property_on_delete = 1; |
| 414 | if (wm->selection_property_set) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 415 | weston_log("got %zu bytes, waiting for " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 416 | "property delete\n", wm->source_data.size); |
| 417 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 418 | weston_log("got %zu bytes, " |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 419 | "property deleted, seting new property\n", |
| 420 | wm->source_data.size); |
| 421 | weston_wm_flush_source_data(wm); |
| 422 | } |
| 423 | xcb_flush(wm->conn); |
| 424 | wl_event_source_remove(wm->property_source); |
Giulio Camuffo | 9e1aeb8 | 2014-12-26 18:10:35 +0200 | [diff] [blame] | 425 | wm->property_source = NULL; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 426 | close(wm->data_source_fd); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 427 | wm->data_source_fd = -1; |
| 428 | close(fd); |
| 429 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 430 | weston_log("nothing happened, buffered the bytes\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | static void |
| 437 | weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type) |
| 438 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 439 | struct weston_data_source *source; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 440 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 441 | int p[2]; |
| 442 | |
| 443 | if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 444 | weston_log("pipe2 failed: %m\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 445 | weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | wl_array_init(&wm->source_data); |
| 450 | wm->selection_target = target; |
| 451 | wm->data_source_fd = p[0]; |
| 452 | wm->property_source = wl_event_loop_add_fd(wm->server->loop, |
| 453 | wm->data_source_fd, |
| 454 | WL_EVENT_READABLE, |
| 455 | weston_wm_read_data_source, |
| 456 | wm); |
| 457 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 458 | source = seat->selection_data_source; |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 459 | source->send(source, mime_type, p[1]); |
Kristian Høgsberg | 73bdc0c | 2013-09-04 22:32:50 -0700 | [diff] [blame] | 460 | close(p[1]); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static void |
| 464 | weston_wm_send_incr_chunk(struct weston_wm *wm) |
| 465 | { |
| 466 | int length; |
| 467 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 468 | weston_log("property deleted\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 469 | |
| 470 | wm->selection_property_set = 0; |
| 471 | if (wm->flush_property_on_delete) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 472 | weston_log("setting new property, %zu bytes\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 473 | wm->source_data.size); |
| 474 | wm->flush_property_on_delete = 0; |
| 475 | length = weston_wm_flush_source_data(wm); |
| 476 | |
| 477 | if (wm->data_source_fd >= 0) { |
| 478 | wm->property_source = |
| 479 | wl_event_loop_add_fd(wm->server->loop, |
| 480 | wm->data_source_fd, |
| 481 | WL_EVENT_READABLE, |
| 482 | weston_wm_read_data_source, |
| 483 | wm); |
| 484 | } else if (length > 0) { |
| 485 | /* Transfer is all done, but queue a flush for |
| 486 | * the delete of the last chunk so we can set |
| 487 | * the 0 sized propert to signal the end of |
| 488 | * the transfer. */ |
| 489 | wm->flush_property_on_delete = 1; |
| 490 | wl_array_release(&wm->source_data); |
| 491 | } else { |
| 492 | wm->selection_request.requestor = XCB_NONE; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static int |
| 498 | weston_wm_handle_selection_property_notify(struct weston_wm *wm, |
| 499 | xcb_generic_event_t *event) |
| 500 | { |
| 501 | xcb_property_notify_event_t *property_notify = |
| 502 | (xcb_property_notify_event_t *) event; |
| 503 | |
| 504 | if (property_notify->window == wm->selection_window) { |
| 505 | if (property_notify->state == XCB_PROPERTY_NEW_VALUE && |
| 506 | property_notify->atom == wm->atom.wl_selection && |
| 507 | wm->incr) |
| 508 | weston_wm_get_incr_chunk(wm); |
| 509 | return 1; |
| 510 | } else if (property_notify->window == wm->selection_request.requestor) { |
| 511 | if (property_notify->state == XCB_PROPERTY_DELETE && |
| 512 | property_notify->atom == wm->selection_request.property && |
| 513 | wm->incr) |
| 514 | weston_wm_send_incr_chunk(wm); |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static void |
| 522 | weston_wm_handle_selection_request(struct weston_wm *wm, |
| 523 | xcb_generic_event_t *event) |
| 524 | { |
| 525 | xcb_selection_request_event_t *selection_request = |
| 526 | (xcb_selection_request_event_t *) event; |
| 527 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 528 | weston_log("selection request, %s, ", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 529 | get_atom_name(wm->conn, selection_request->selection)); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 530 | weston_log_continue("target %s, ", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 531 | get_atom_name(wm->conn, selection_request->target)); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 532 | weston_log_continue("property %s\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 533 | get_atom_name(wm->conn, selection_request->property)); |
| 534 | |
| 535 | wm->selection_request = *selection_request; |
| 536 | wm->incr = 0; |
| 537 | wm->flush_property_on_delete = 0; |
| 538 | |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 539 | if (selection_request->selection == wm->atom.clipboard_manager) { |
| 540 | /* The weston clipboard should already have grabbed |
| 541 | * the first target, so just send selection notify |
| 542 | * now. This isn't synchronized with the clipboard |
| 543 | * finishing getting the data, so there's a race here. */ |
| 544 | weston_wm_send_selection_notify(wm, wm->selection_request.property); |
| 545 | return; |
| 546 | } |
| 547 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 548 | if (selection_request->target == wm->atom.targets) { |
| 549 | weston_wm_send_targets(wm); |
| 550 | } else if (selection_request->target == wm->atom.timestamp) { |
| 551 | weston_wm_send_timestamp(wm); |
| 552 | } else if (selection_request->target == wm->atom.utf8_string || |
| 553 | selection_request->target == wm->atom.text) { |
| 554 | weston_wm_send_data(wm, wm->atom.utf8_string, |
| 555 | "text/plain;charset=utf-8"); |
| 556 | } else { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 557 | weston_log("can only handle UTF8_STRING targets...\n"); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 558 | weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); |
| 559 | } |
| 560 | } |
| 561 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 562 | static int |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 563 | weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm, |
| 564 | xcb_generic_event_t *event) |
| 565 | { |
| 566 | xcb_xfixes_selection_notify_event_t *xfixes_selection_notify = |
| 567 | (xcb_xfixes_selection_notify_event_t *) event; |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 568 | struct weston_compositor *compositor; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 569 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 570 | uint32_t serial; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 571 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 572 | if (xfixes_selection_notify->selection != wm->atom.clipboard) |
| 573 | return 0; |
| 574 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 575 | weston_log("xfixes selection notify event: owner %d\n", |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 576 | xfixes_selection_notify->owner); |
| 577 | |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 578 | if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) { |
| 579 | if (wm->selection_owner != wm->selection_window) { |
| 580 | /* A real X client selection went away, not our |
| 581 | * proxy selection. Clear the wayland selection. */ |
| 582 | compositor = wm->server->compositor; |
| 583 | serial = wl_display_next_serial(compositor->wl_display); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 584 | weston_seat_set_selection(seat, NULL, serial); |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | wm->selection_owner = XCB_WINDOW_NONE; |
| 588 | |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 589 | return 1; |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | wm->selection_owner = xfixes_selection_notify->owner; |
| 593 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 594 | /* We have to use XCB_TIME_CURRENT_TIME when we claim the |
| 595 | * selection, so grab the actual timestamp here so we can |
| 596 | * answer TIMESTAMP conversion requests correctly. */ |
| 597 | if (xfixes_selection_notify->owner == wm->selection_window) { |
| 598 | wm->selection_timestamp = xfixes_selection_notify->timestamp; |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 599 | weston_log("our window, skipping\n"); |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 600 | return 1; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | wm->incr = 0; |
| 604 | xcb_convert_selection(wm->conn, wm->selection_window, |
| 605 | wm->atom.clipboard, |
| 606 | wm->atom.targets, |
| 607 | wm->atom.wl_selection, |
| 608 | xfixes_selection_notify->timestamp); |
| 609 | |
| 610 | xcb_flush(wm->conn); |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 611 | |
| 612 | return 1; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int |
| 616 | weston_wm_handle_selection_event(struct weston_wm *wm, |
| 617 | xcb_generic_event_t *event) |
| 618 | { |
| 619 | switch (event->response_type & ~0x80) { |
| 620 | case XCB_SELECTION_NOTIFY: |
| 621 | weston_wm_handle_selection_notify(wm, event); |
| 622 | return 1; |
| 623 | case XCB_PROPERTY_NOTIFY: |
| 624 | return weston_wm_handle_selection_property_notify(wm, event); |
| 625 | case XCB_SELECTION_REQUEST: |
| 626 | weston_wm_handle_selection_request(wm, event); |
| 627 | return 1; |
| 628 | } |
| 629 | |
| 630 | switch (event->response_type - wm->xfixes->first_event) { |
| 631 | case XCB_XFIXES_SELECTION_NOTIFY: |
Kristian Høgsberg | 9466e50 | 2013-09-04 21:09:24 -0700 | [diff] [blame] | 632 | return weston_wm_handle_xfixes_selection_notify(wm, event); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Tiago Vignatti | 2d129f1 | 2012-11-30 17:19:59 -0200 | [diff] [blame] | 638 | static void |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 639 | weston_wm_set_selection(struct wl_listener *listener, void *data) |
| 640 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 641 | struct weston_seat *seat = data; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 642 | struct weston_wm *wm = |
| 643 | container_of(listener, struct weston_wm, selection_listener); |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 644 | struct weston_data_source *source = seat->selection_data_source; |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 645 | const char **p, **end; |
| 646 | int has_text_plain = 0; |
| 647 | |
Kristian Høgsberg | 8056673 | 2012-06-01 00:08:12 -0400 | [diff] [blame] | 648 | if (source == NULL) { |
| 649 | if (wm->selection_owner == wm->selection_window) |
| 650 | xcb_set_selection_owner(wm->conn, |
| 651 | XCB_ATOM_NONE, |
| 652 | wm->atom.clipboard, |
| 653 | wm->selection_timestamp); |
| 654 | return; |
| 655 | } |
| 656 | |
Kristian Høgsberg | c65d56a | 2012-06-02 21:23:01 -0400 | [diff] [blame] | 657 | if (source->send == data_source_send) |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 658 | return; |
| 659 | |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 660 | p = source->mime_types.data; |
| 661 | end = (const char **) |
| 662 | ((char *) source->mime_types.data + source->mime_types.size); |
| 663 | while (p < end) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 664 | weston_log(" %s\n", *p); |
Kristian Høgsberg | 102bf03 | 2012-05-21 15:52:02 -0400 | [diff] [blame] | 665 | if (strcmp(*p, "text/plain") == 0 || |
| 666 | strcmp(*p, "text/plain;charset=utf-8") == 0) |
| 667 | has_text_plain = 1; |
| 668 | p++; |
| 669 | } |
| 670 | |
| 671 | if (has_text_plain) { |
| 672 | xcb_set_selection_owner(wm->conn, |
| 673 | wm->selection_window, |
| 674 | wm->atom.clipboard, |
| 675 | XCB_TIME_CURRENT_TIME); |
| 676 | } else { |
| 677 | xcb_set_selection_owner(wm->conn, |
| 678 | XCB_ATOM_NONE, |
| 679 | wm->atom.clipboard, |
| 680 | XCB_TIME_CURRENT_TIME); |
| 681 | } |
| 682 | } |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 683 | |
| 684 | void |
| 685 | weston_wm_selection_init(struct weston_wm *wm) |
| 686 | { |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 687 | struct weston_seat *seat; |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 688 | uint32_t values[1], mask; |
| 689 | |
| 690 | wm->selection_request.requestor = XCB_NONE; |
| 691 | |
| 692 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 693 | wm->selection_window = xcb_generate_id(wm->conn); |
| 694 | xcb_create_window(wm->conn, |
| 695 | XCB_COPY_FROM_PARENT, |
| 696 | wm->selection_window, |
| 697 | wm->screen->root, |
| 698 | 0, 0, |
| 699 | 10, 10, |
| 700 | 0, |
| 701 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 702 | wm->screen->root_visual, |
| 703 | XCB_CW_EVENT_MASK, values); |
| 704 | |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 705 | xcb_set_selection_owner(wm->conn, |
| 706 | wm->selection_window, |
| 707 | wm->atom.clipboard_manager, |
| 708 | XCB_TIME_CURRENT_TIME); |
| 709 | |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 710 | mask = |
| 711 | XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | |
| 712 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | |
| 713 | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; |
| 714 | xcb_xfixes_select_selection_input(wm->conn, wm->selection_window, |
| 715 | wm->atom.clipboard, mask); |
| 716 | |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 717 | seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 718 | wm->selection_listener.notify = weston_wm_set_selection; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 719 | wl_signal_add(&seat->selection_signal, &wm->selection_listener); |
Kristian Høgsberg | e220327 | 2012-06-03 10:32:48 -0400 | [diff] [blame] | 720 | |
| 721 | weston_wm_set_selection(&wm->selection_listener, seat); |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 722 | } |