Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "compositor.h" |
| 29 | |
| 30 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 31 | empty_region(pixman_region32_t *region) |
| 32 | { |
| 33 | pixman_region32_fini(region); |
| 34 | pixman_region32_init(region); |
| 35 | } |
| 36 | |
| 37 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 38 | data_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 39 | uint32_t serial, const char *mime_type) |
| 40 | { |
| 41 | struct wl_data_offer *offer = resource->data; |
| 42 | |
| 43 | /* FIXME: Check that client is currently focused by the input |
| 44 | * device that is currently dragging this data source. Should |
| 45 | * this be a wl_data_device request? */ |
| 46 | |
| 47 | if (offer->source) |
| 48 | offer->source->accept(offer->source, serial, mime_type); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | data_offer_receive(struct wl_client *client, struct wl_resource *resource, |
| 53 | const char *mime_type, int32_t fd) |
| 54 | { |
| 55 | struct wl_data_offer *offer = resource->data; |
| 56 | |
| 57 | if (offer->source) |
| 58 | offer->source->send(offer->source, mime_type, fd); |
| 59 | else |
| 60 | close(fd); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | data_offer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 65 | { |
| 66 | wl_resource_destroy(resource); |
| 67 | } |
| 68 | |
| 69 | static const struct wl_data_offer_interface data_offer_interface = { |
| 70 | data_offer_accept, |
| 71 | data_offer_receive, |
| 72 | data_offer_destroy, |
| 73 | }; |
| 74 | |
| 75 | static void |
| 76 | destroy_data_offer(struct wl_resource *resource) |
| 77 | { |
| 78 | struct wl_data_offer *offer = resource->data; |
| 79 | |
| 80 | if (offer->source) |
| 81 | wl_list_remove(&offer->source_destroy_listener.link); |
| 82 | free(offer); |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | destroy_offer_data_source(struct wl_listener *listener, void *data) |
| 87 | { |
| 88 | struct wl_data_offer *offer; |
| 89 | |
| 90 | offer = container_of(listener, struct wl_data_offer, |
| 91 | source_destroy_listener); |
| 92 | |
| 93 | offer->source = NULL; |
| 94 | } |
| 95 | |
| 96 | static struct wl_resource * |
| 97 | wl_data_source_send_offer(struct wl_data_source *source, |
| 98 | struct wl_resource *target) |
| 99 | { |
| 100 | struct wl_data_offer *offer; |
| 101 | char **p; |
| 102 | |
| 103 | offer = malloc(sizeof *offer); |
| 104 | if (offer == NULL) |
| 105 | return NULL; |
| 106 | |
| 107 | wl_resource_init(&offer->resource, &wl_data_offer_interface, |
| 108 | &data_offer_interface, 0, offer); |
| 109 | offer->resource.destroy = destroy_data_offer; |
| 110 | |
| 111 | offer->source = source; |
| 112 | offer->source_destroy_listener.notify = destroy_offer_data_source; |
| 113 | wl_signal_add(&source->resource.destroy_signal, |
| 114 | &offer->source_destroy_listener); |
| 115 | |
| 116 | wl_client_add_resource(target->client, &offer->resource); |
| 117 | |
| 118 | wl_data_device_send_data_offer(target, &offer->resource); |
| 119 | |
| 120 | wl_array_for_each(p, &source->mime_types) |
| 121 | wl_data_offer_send_offer(&offer->resource, *p); |
| 122 | |
| 123 | return &offer->resource; |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | data_source_offer(struct wl_client *client, |
| 128 | struct wl_resource *resource, |
| 129 | const char *type) |
| 130 | { |
| 131 | struct wl_data_source *source = resource->data; |
| 132 | char **p; |
| 133 | |
| 134 | p = wl_array_add(&source->mime_types, sizeof *p); |
| 135 | if (p) |
| 136 | *p = strdup(type); |
| 137 | if (!p || !*p) |
| 138 | wl_resource_post_no_memory(resource); |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | data_source_destroy(struct wl_client *client, struct wl_resource *resource) |
| 143 | { |
| 144 | wl_resource_destroy(resource); |
| 145 | } |
| 146 | |
| 147 | static struct wl_data_source_interface data_source_interface = { |
| 148 | data_source_offer, |
| 149 | data_source_destroy |
| 150 | }; |
| 151 | |
| 152 | static struct wl_resource * |
| 153 | find_resource(struct wl_list *list, struct wl_client *client) |
| 154 | { |
| 155 | struct wl_resource *r; |
| 156 | |
| 157 | wl_list_for_each(r, list, link) { |
| 158 | if (r->client == client) |
| 159 | return r; |
| 160 | } |
| 161 | |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 166 | drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height) |
| 167 | { |
| 168 | empty_region(&es->pending.input); |
| 169 | |
| 170 | weston_surface_configure(es, |
| 171 | es->geometry.x + sx, es->geometry.y + sy, |
| 172 | width, height); |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | device_setup_new_drag_surface(struct weston_seat *seat, |
| 177 | struct weston_surface *surface) |
| 178 | { |
| 179 | if (surface->configure) { |
| 180 | wl_resource_post_error(&surface->surface.resource, |
| 181 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 182 | "surface->configure already set"); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | seat->drag_surface = surface; |
| 187 | |
| 188 | weston_surface_set_position(seat->drag_surface, |
| 189 | wl_fixed_to_double(seat->pointer->x), |
| 190 | wl_fixed_to_double(seat->pointer->y)); |
| 191 | |
| 192 | surface->configure = drag_surface_configure; |
| 193 | |
| 194 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 195 | &seat->drag_surface_destroy_listener); |
| 196 | |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | device_release_drag_surface(struct weston_seat *seat) |
| 202 | { |
| 203 | if (weston_surface_is_mapped(seat->drag_surface)) |
| 204 | weston_surface_unmap(seat->drag_surface); |
| 205 | |
| 206 | seat->drag_surface->configure = NULL; |
| 207 | empty_region(&seat->drag_surface->pending.input); |
| 208 | wl_list_remove(&seat->drag_surface_destroy_listener.link); |
| 209 | seat->drag_surface = NULL; |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | device_map_drag_surface(struct weston_seat *seat) |
| 214 | { |
| 215 | struct wl_list *list; |
| 216 | |
| 217 | if (weston_surface_is_mapped(seat->drag_surface) || |
| 218 | !seat->drag_surface->buffer_ref.buffer) |
| 219 | return; |
| 220 | |
| 221 | if (seat->sprite && weston_surface_is_mapped(seat->sprite)) |
| 222 | list = &seat->sprite->layer_link; |
| 223 | else |
| 224 | list = &seat->compositor->cursor_layer.surface_list; |
| 225 | |
| 226 | wl_list_insert(list, &seat->drag_surface->layer_link); |
| 227 | weston_surface_update_transform(seat->drag_surface); |
| 228 | empty_region(&seat->drag_surface->input); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy) |
| 233 | { |
Kristian Høgsberg | 5f95557 | 2013-05-07 21:06:38 -0400 | [diff] [blame^] | 234 | if (!seat->drag_surface) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 235 | return; |
| 236 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 237 | /* the client may not have attached a buffer to the drag surface |
| 238 | * when we setup it up, so check if map is needed on every update */ |
| 239 | device_map_drag_surface(seat); |
| 240 | |
| 241 | if (!dx && !dy) |
| 242 | return; |
| 243 | |
| 244 | weston_surface_set_position(seat->drag_surface, |
| 245 | seat->drag_surface->geometry.x + wl_fixed_to_double(dx), |
| 246 | seat->drag_surface->geometry.y + wl_fixed_to_double(dy)); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | weston_compositor_update_drag_surfaces(struct weston_compositor *compositor) |
| 251 | { |
| 252 | struct weston_seat *seat; |
| 253 | |
| 254 | wl_list_for_each(seat, &compositor->seat_list, link) |
| 255 | weston_seat_update_drag_surface(seat, 0, 0); |
| 256 | } |
| 257 | |
| 258 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 259 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 260 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 261 | struct weston_seat *seat = |
| 262 | container_of(listener, struct weston_seat, drag_focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 263 | |
| 264 | seat->drag_focus_resource = NULL; |
| 265 | } |
| 266 | |
| 267 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 268 | drag_grab_focus(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 269 | struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) |
| 270 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 271 | struct weston_seat *seat = |
| 272 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 273 | struct wl_resource *resource, *offer = NULL; |
| 274 | struct wl_display *display; |
| 275 | uint32_t serial; |
| 276 | |
| 277 | if (seat->drag_focus_resource) { |
| 278 | wl_data_device_send_leave(seat->drag_focus_resource); |
| 279 | wl_list_remove(&seat->drag_focus_listener.link); |
| 280 | seat->drag_focus_resource = NULL; |
| 281 | seat->drag_focus = NULL; |
| 282 | } |
| 283 | |
| 284 | if (!surface) |
| 285 | return; |
| 286 | |
| 287 | if (!seat->drag_data_source && |
| 288 | surface->resource.client != seat->drag_client) |
| 289 | return; |
| 290 | |
| 291 | resource = find_resource(&seat->drag_resource_list, |
| 292 | surface->resource.client); |
| 293 | if (!resource) |
| 294 | return; |
| 295 | |
| 296 | display = wl_client_get_display(resource->client); |
| 297 | serial = wl_display_next_serial(display); |
| 298 | |
| 299 | if (seat->drag_data_source) |
| 300 | offer = wl_data_source_send_offer(seat->drag_data_source, |
| 301 | resource); |
| 302 | |
| 303 | wl_data_device_send_enter(resource, serial, &surface->resource, |
| 304 | x, y, offer); |
| 305 | |
| 306 | seat->drag_focus = surface; |
| 307 | seat->drag_focus_listener.notify = destroy_drag_focus; |
| 308 | wl_signal_add(&resource->destroy_signal, |
| 309 | &seat->drag_focus_listener); |
| 310 | seat->drag_focus_resource = resource; |
| 311 | grab->focus = surface; |
| 312 | } |
| 313 | |
| 314 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 315 | drag_grab_motion(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 316 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 317 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 318 | struct weston_seat *seat = |
| 319 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 320 | |
| 321 | if (seat->drag_focus_resource) |
| 322 | wl_data_device_send_motion(seat->drag_focus_resource, |
| 323 | time, x, y); |
| 324 | } |
| 325 | |
| 326 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 327 | data_device_end_drag_grab(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 328 | { |
| 329 | if (seat->drag_surface) { |
Kristian Høgsberg | 5f95557 | 2013-05-07 21:06:38 -0400 | [diff] [blame^] | 330 | device_release_drag_surface(seat); |
Kristian Høgsberg | 624d8f2 | 2013-05-07 15:30:49 -0400 | [diff] [blame] | 331 | weston_seat_update_drag_surface(seat, 0, 0); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | drag_grab_focus(&seat->drag_grab, NULL, |
| 335 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
| 336 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 337 | weston_pointer_end_grab(seat->pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 338 | |
| 339 | seat->drag_data_source = NULL; |
| 340 | seat->drag_client = NULL; |
| 341 | } |
| 342 | |
| 343 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 344 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 345 | uint32_t time, uint32_t button, uint32_t state_w) |
| 346 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 347 | struct weston_seat *seat = |
| 348 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 349 | enum wl_pointer_button_state state = state_w; |
| 350 | |
| 351 | if (seat->drag_focus_resource && |
| 352 | seat->pointer->grab_button == button && |
| 353 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 354 | wl_data_device_send_drop(seat->drag_focus_resource); |
| 355 | |
| 356 | if (seat->pointer->button_count == 0 && |
| 357 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
| 358 | if (seat->drag_data_source) |
| 359 | wl_list_remove(&seat->drag_data_source_listener.link); |
| 360 | data_device_end_drag_grab(seat); |
| 361 | } |
| 362 | } |
| 363 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 364 | static const struct weston_pointer_grab_interface drag_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 365 | drag_grab_focus, |
| 366 | drag_grab_motion, |
| 367 | drag_grab_button, |
| 368 | }; |
| 369 | |
| 370 | static void |
| 371 | destroy_data_device_source(struct wl_listener *listener, void *data) |
| 372 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 373 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 374 | drag_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 375 | |
| 376 | data_device_end_drag_grab(seat); |
| 377 | } |
| 378 | |
| 379 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 380 | handle_drag_surface_destroy(struct wl_listener *listener, void *data) |
| 381 | { |
| 382 | struct weston_seat *seat; |
| 383 | |
| 384 | seat = container_of(listener, struct weston_seat, |
| 385 | drag_surface_destroy_listener); |
| 386 | |
| 387 | seat->drag_surface = NULL; |
| 388 | } |
| 389 | |
| 390 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 391 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 392 | struct wl_resource *source_resource, |
| 393 | struct wl_resource *origin_resource, |
| 394 | struct wl_resource *icon_resource, uint32_t serial) |
| 395 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 396 | struct weston_seat *seat = resource->data; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 397 | |
| 398 | /* FIXME: Check that client has implicit grab on the origin |
| 399 | * surface that matches the given time. */ |
| 400 | |
| 401 | /* FIXME: Check that the data source type array isn't empty. */ |
| 402 | |
| 403 | seat->drag_grab.interface = &drag_grab_interface; |
| 404 | |
| 405 | seat->drag_client = client; |
| 406 | seat->drag_data_source = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 407 | seat->drag_surface_destroy_listener.notify = |
| 408 | handle_drag_surface_destroy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 409 | |
| 410 | if (source_resource) { |
| 411 | seat->drag_data_source = source_resource->data; |
| 412 | seat->drag_data_source_listener.notify = |
| 413 | destroy_data_device_source; |
| 414 | wl_signal_add(&source_resource->destroy_signal, |
| 415 | &seat->drag_data_source_listener); |
| 416 | } |
| 417 | |
| 418 | if (icon_resource) { |
Kristian Høgsberg | 5f95557 | 2013-05-07 21:06:38 -0400 | [diff] [blame^] | 419 | if (!device_setup_new_drag_surface(seat, icon_resource->data)) |
| 420 | return; |
Kristian Høgsberg | 624d8f2 | 2013-05-07 15:30:49 -0400 | [diff] [blame] | 421 | weston_seat_update_drag_surface(seat, 0, 0); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 424 | weston_pointer_set_focus(seat->pointer, NULL, |
| 425 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
| 426 | weston_pointer_start_grab(seat->pointer, &seat->drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static void |
| 430 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 431 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 432 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 433 | selection_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 434 | struct wl_resource *data_device; |
| 435 | struct wl_resource *focus = NULL; |
| 436 | |
| 437 | seat->selection_data_source = NULL; |
| 438 | |
| 439 | if (seat->keyboard) |
| 440 | focus = seat->keyboard->focus_resource; |
| 441 | if (focus) { |
| 442 | data_device = find_resource(&seat->drag_resource_list, |
| 443 | focus->client); |
| 444 | if (data_device) |
| 445 | wl_data_device_send_selection(data_device, NULL); |
| 446 | } |
| 447 | |
| 448 | wl_signal_emit(&seat->selection_signal, seat); |
| 449 | } |
| 450 | |
| 451 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 452 | weston_seat_set_selection(struct weston_seat *seat, |
| 453 | struct wl_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 454 | { |
| 455 | struct wl_resource *data_device, *offer; |
| 456 | struct wl_resource *focus = NULL; |
| 457 | |
| 458 | if (seat->selection_data_source && |
| 459 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 460 | return; |
| 461 | |
| 462 | if (seat->selection_data_source) { |
| 463 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 464 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 465 | seat->selection_data_source = NULL; |
| 466 | } |
| 467 | |
| 468 | seat->selection_data_source = source; |
| 469 | seat->selection_serial = serial; |
| 470 | |
| 471 | if (seat->keyboard) |
| 472 | focus = seat->keyboard->focus_resource; |
| 473 | if (focus) { |
| 474 | data_device = find_resource(&seat->drag_resource_list, |
| 475 | focus->client); |
| 476 | if (data_device && source) { |
| 477 | offer = wl_data_source_send_offer(seat->selection_data_source, |
| 478 | data_device); |
| 479 | wl_data_device_send_selection(data_device, offer); |
| 480 | } else if (data_device) { |
| 481 | wl_data_device_send_selection(data_device, NULL); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | wl_signal_emit(&seat->selection_signal, seat); |
| 486 | |
| 487 | if (source) { |
| 488 | seat->selection_data_source_listener.notify = |
| 489 | destroy_selection_data_source; |
| 490 | wl_signal_add(&source->resource.destroy_signal, |
| 491 | &seat->selection_data_source_listener); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | data_device_set_selection(struct wl_client *client, |
| 497 | struct wl_resource *resource, |
| 498 | struct wl_resource *source_resource, uint32_t serial) |
| 499 | { |
| 500 | if (!source_resource) |
| 501 | return; |
| 502 | |
| 503 | /* FIXME: Store serial and check against incoming serial here. */ |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 504 | weston_seat_set_selection(resource->data, source_resource->data, |
| 505 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static const struct wl_data_device_interface data_device_interface = { |
| 509 | data_device_start_drag, |
| 510 | data_device_set_selection, |
| 511 | }; |
| 512 | |
| 513 | static void |
| 514 | destroy_data_source(struct wl_resource *resource) |
| 515 | { |
| 516 | struct wl_data_source *source = |
| 517 | container_of(resource, struct wl_data_source, resource); |
| 518 | char **p; |
| 519 | |
| 520 | wl_array_for_each(p, &source->mime_types) |
| 521 | free(*p); |
| 522 | |
| 523 | wl_array_release(&source->mime_types); |
| 524 | |
| 525 | source->resource.object.id = 0; |
| 526 | } |
| 527 | |
| 528 | static void |
| 529 | client_source_accept(struct wl_data_source *source, |
| 530 | uint32_t time, const char *mime_type) |
| 531 | { |
| 532 | wl_data_source_send_target(&source->resource, mime_type); |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | client_source_send(struct wl_data_source *source, |
| 537 | const char *mime_type, int32_t fd) |
| 538 | { |
| 539 | wl_data_source_send_send(&source->resource, mime_type, fd); |
| 540 | close(fd); |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | client_source_cancel(struct wl_data_source *source) |
| 545 | { |
| 546 | wl_data_source_send_cancelled(&source->resource); |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | create_data_source(struct wl_client *client, |
| 551 | struct wl_resource *resource, uint32_t id) |
| 552 | { |
| 553 | struct wl_data_source *source; |
| 554 | |
| 555 | source = malloc(sizeof *source); |
| 556 | if (source == NULL) { |
| 557 | wl_resource_post_no_memory(resource); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | wl_resource_init(&source->resource, &wl_data_source_interface, |
| 562 | &data_source_interface, id, source); |
| 563 | source->resource.destroy = destroy_data_source; |
| 564 | |
| 565 | source->accept = client_source_accept; |
| 566 | source->send = client_source_send; |
| 567 | source->cancel = client_source_cancel; |
| 568 | |
| 569 | wl_array_init(&source->mime_types); |
| 570 | wl_client_add_resource(client, &source->resource); |
| 571 | } |
| 572 | |
| 573 | static void unbind_data_device(struct wl_resource *resource) |
| 574 | { |
| 575 | wl_list_remove(&resource->link); |
| 576 | free(resource); |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | get_data_device(struct wl_client *client, |
| 581 | struct wl_resource *manager_resource, |
| 582 | uint32_t id, struct wl_resource *seat_resource) |
| 583 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 584 | struct weston_seat *seat = seat_resource->data; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 585 | struct wl_resource *resource; |
| 586 | |
| 587 | resource = wl_client_add_object(client, &wl_data_device_interface, |
| 588 | &data_device_interface, id, |
| 589 | seat); |
| 590 | |
| 591 | wl_list_insert(&seat->drag_resource_list, &resource->link); |
| 592 | resource->destroy = unbind_data_device; |
| 593 | } |
| 594 | |
| 595 | static const struct wl_data_device_manager_interface manager_interface = { |
| 596 | create_data_source, |
| 597 | get_data_device |
| 598 | }; |
| 599 | |
| 600 | static void |
| 601 | bind_manager(struct wl_client *client, |
| 602 | void *data, uint32_t version, uint32_t id) |
| 603 | { |
| 604 | wl_client_add_object(client, &wl_data_device_manager_interface, |
| 605 | &manager_interface, id, NULL); |
| 606 | } |
| 607 | |
| 608 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 609 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 610 | { |
| 611 | struct wl_resource *data_device, *focus, *offer; |
| 612 | struct wl_data_source *source; |
| 613 | |
| 614 | if (!seat->keyboard) |
| 615 | return; |
| 616 | |
| 617 | focus = seat->keyboard->focus_resource; |
| 618 | if (!focus) |
| 619 | return; |
| 620 | |
| 621 | data_device = find_resource(&seat->drag_resource_list, |
| 622 | focus->client); |
| 623 | if (!data_device) |
| 624 | return; |
| 625 | |
| 626 | source = seat->selection_data_source; |
| 627 | if (source) { |
| 628 | offer = wl_data_source_send_offer(source, data_device); |
| 629 | wl_data_device_send_selection(data_device, offer); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | WL_EXPORT int |
| 634 | wl_data_device_manager_init(struct wl_display *display) |
| 635 | { |
| 636 | if (wl_display_add_global(display, |
| 637 | &wl_data_device_manager_interface, |
| 638 | NULL, bind_manager) == NULL) |
| 639 | return -1; |
| 640 | |
| 641 | return 0; |
| 642 | } |