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 | { |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 168 | struct weston_seat *seat = es->configure_private; |
| 169 | struct wl_list *list; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 170 | float fx, fy; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 171 | |
| 172 | if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) { |
| 173 | if (seat->sprite && weston_surface_is_mapped(seat->sprite)) |
| 174 | list = &seat->sprite->layer_link; |
| 175 | else |
| 176 | list = &seat->compositor->cursor_layer.surface_list; |
| 177 | |
| 178 | wl_list_insert(list, &es->layer_link); |
| 179 | weston_surface_update_transform(es); |
| 180 | empty_region(&es->pending.input); |
| 181 | } |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 182 | |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 183 | seat->drag_dx += sx; |
| 184 | seat->drag_dy += sy; |
| 185 | |
| 186 | fx = wl_fixed_to_double(seat->pointer->x) + seat->drag_dx; |
| 187 | fy = wl_fixed_to_double(seat->pointer->y) + seat->drag_dy; |
| 188 | weston_surface_configure(es, fx, fy, width, height); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int |
| 192 | device_setup_new_drag_surface(struct weston_seat *seat, |
| 193 | struct weston_surface *surface) |
| 194 | { |
| 195 | if (surface->configure) { |
| 196 | wl_resource_post_error(&surface->surface.resource, |
| 197 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 198 | "surface->configure already set"); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | seat->drag_surface = surface; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 203 | seat->drag_dx = 0; |
| 204 | seat->drag_dy = 0; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 205 | |
| 206 | surface->configure = drag_surface_configure; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 207 | surface->configure_private = seat; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 208 | |
| 209 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 210 | &seat->drag_surface_destroy_listener); |
| 211 | |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | device_release_drag_surface(struct weston_seat *seat) |
| 217 | { |
| 218 | if (weston_surface_is_mapped(seat->drag_surface)) |
| 219 | weston_surface_unmap(seat->drag_surface); |
| 220 | |
| 221 | seat->drag_surface->configure = NULL; |
| 222 | empty_region(&seat->drag_surface->pending.input); |
| 223 | wl_list_remove(&seat->drag_surface_destroy_listener.link); |
| 224 | seat->drag_surface = NULL; |
| 225 | } |
| 226 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 227 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 228 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 229 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 230 | struct weston_seat *seat = |
| 231 | container_of(listener, struct weston_seat, drag_focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 232 | |
| 233 | seat->drag_focus_resource = NULL; |
| 234 | } |
| 235 | |
| 236 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 237 | drag_grab_focus(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 238 | struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) |
| 239 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 240 | struct weston_seat *seat = |
| 241 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 242 | struct wl_resource *resource, *offer = NULL; |
| 243 | struct wl_display *display; |
| 244 | uint32_t serial; |
| 245 | |
| 246 | if (seat->drag_focus_resource) { |
| 247 | wl_data_device_send_leave(seat->drag_focus_resource); |
| 248 | wl_list_remove(&seat->drag_focus_listener.link); |
| 249 | seat->drag_focus_resource = NULL; |
| 250 | seat->drag_focus = NULL; |
| 251 | } |
| 252 | |
| 253 | if (!surface) |
| 254 | return; |
| 255 | |
| 256 | if (!seat->drag_data_source && |
| 257 | surface->resource.client != seat->drag_client) |
| 258 | return; |
| 259 | |
| 260 | resource = find_resource(&seat->drag_resource_list, |
| 261 | surface->resource.client); |
| 262 | if (!resource) |
| 263 | return; |
| 264 | |
| 265 | display = wl_client_get_display(resource->client); |
| 266 | serial = wl_display_next_serial(display); |
| 267 | |
| 268 | if (seat->drag_data_source) |
| 269 | offer = wl_data_source_send_offer(seat->drag_data_source, |
| 270 | resource); |
| 271 | |
| 272 | wl_data_device_send_enter(resource, serial, &surface->resource, |
| 273 | x, y, offer); |
| 274 | |
| 275 | seat->drag_focus = surface; |
| 276 | seat->drag_focus_listener.notify = destroy_drag_focus; |
| 277 | wl_signal_add(&resource->destroy_signal, |
| 278 | &seat->drag_focus_listener); |
| 279 | seat->drag_focus_resource = resource; |
| 280 | grab->focus = surface; |
| 281 | } |
| 282 | |
| 283 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 284 | drag_grab_motion(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 285 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 286 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 287 | struct weston_seat *seat = |
| 288 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 289 | float fx, fy; |
| 290 | |
| 291 | if (seat->drag_surface) { |
| 292 | fx = wl_fixed_to_double(seat->pointer->x) + seat->drag_dx; |
| 293 | fy = wl_fixed_to_double(seat->pointer->y) + seat->drag_dy; |
| 294 | weston_surface_set_position(seat->drag_surface, fx, fy); |
| 295 | weston_surface_schedule_repaint(seat->drag_surface); |
| 296 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 297 | |
| 298 | if (seat->drag_focus_resource) |
| 299 | wl_data_device_send_motion(seat->drag_focus_resource, |
| 300 | time, x, y); |
| 301 | } |
| 302 | |
| 303 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 304 | data_device_end_drag_grab(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 305 | { |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 306 | if (seat->drag_surface) |
Kristian Høgsberg | 5f95557 | 2013-05-07 21:06:38 -0400 | [diff] [blame] | 307 | device_release_drag_surface(seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 308 | |
| 309 | drag_grab_focus(&seat->drag_grab, NULL, |
| 310 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
| 311 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 312 | weston_pointer_end_grab(seat->pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 313 | |
| 314 | seat->drag_data_source = NULL; |
| 315 | seat->drag_client = NULL; |
| 316 | } |
| 317 | |
| 318 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 319 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 320 | uint32_t time, uint32_t button, uint32_t state_w) |
| 321 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 322 | struct weston_seat *seat = |
| 323 | container_of(grab, struct weston_seat, drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 324 | enum wl_pointer_button_state state = state_w; |
| 325 | |
| 326 | if (seat->drag_focus_resource && |
| 327 | seat->pointer->grab_button == button && |
| 328 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 329 | wl_data_device_send_drop(seat->drag_focus_resource); |
| 330 | |
| 331 | if (seat->pointer->button_count == 0 && |
| 332 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
| 333 | if (seat->drag_data_source) |
| 334 | wl_list_remove(&seat->drag_data_source_listener.link); |
| 335 | data_device_end_drag_grab(seat); |
| 336 | } |
| 337 | } |
| 338 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 339 | static const struct weston_pointer_grab_interface drag_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 340 | drag_grab_focus, |
| 341 | drag_grab_motion, |
| 342 | drag_grab_button, |
| 343 | }; |
| 344 | |
| 345 | static void |
| 346 | destroy_data_device_source(struct wl_listener *listener, void *data) |
| 347 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 348 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 349 | drag_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 350 | |
| 351 | data_device_end_drag_grab(seat); |
| 352 | } |
| 353 | |
| 354 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 355 | handle_drag_surface_destroy(struct wl_listener *listener, void *data) |
| 356 | { |
| 357 | struct weston_seat *seat; |
| 358 | |
| 359 | seat = container_of(listener, struct weston_seat, |
| 360 | drag_surface_destroy_listener); |
| 361 | |
| 362 | seat->drag_surface = NULL; |
| 363 | } |
| 364 | |
| 365 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 366 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 367 | struct wl_resource *source_resource, |
| 368 | struct wl_resource *origin_resource, |
| 369 | struct wl_resource *icon_resource, uint32_t serial) |
| 370 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 371 | struct weston_seat *seat = resource->data; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 372 | |
| 373 | /* FIXME: Check that client has implicit grab on the origin |
| 374 | * surface that matches the given time. */ |
| 375 | |
| 376 | /* FIXME: Check that the data source type array isn't empty. */ |
| 377 | |
| 378 | seat->drag_grab.interface = &drag_grab_interface; |
| 379 | |
| 380 | seat->drag_client = client; |
| 381 | seat->drag_data_source = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 382 | seat->drag_surface_destroy_listener.notify = |
| 383 | handle_drag_surface_destroy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 384 | |
| 385 | if (source_resource) { |
| 386 | seat->drag_data_source = source_resource->data; |
| 387 | seat->drag_data_source_listener.notify = |
| 388 | destroy_data_device_source; |
| 389 | wl_signal_add(&source_resource->destroy_signal, |
| 390 | &seat->drag_data_source_listener); |
| 391 | } |
| 392 | |
| 393 | if (icon_resource) { |
Kristian Høgsberg | 5f95557 | 2013-05-07 21:06:38 -0400 | [diff] [blame] | 394 | if (!device_setup_new_drag_surface(seat, icon_resource->data)) |
| 395 | return; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 398 | weston_pointer_set_focus(seat->pointer, NULL, |
| 399 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
| 400 | weston_pointer_start_grab(seat->pointer, &seat->drag_grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
| 404 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 405 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 406 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 407 | selection_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 408 | struct wl_resource *data_device; |
| 409 | struct wl_resource *focus = NULL; |
| 410 | |
| 411 | seat->selection_data_source = NULL; |
| 412 | |
| 413 | if (seat->keyboard) |
| 414 | focus = seat->keyboard->focus_resource; |
| 415 | if (focus) { |
| 416 | data_device = find_resource(&seat->drag_resource_list, |
| 417 | focus->client); |
| 418 | if (data_device) |
| 419 | wl_data_device_send_selection(data_device, NULL); |
| 420 | } |
| 421 | |
| 422 | wl_signal_emit(&seat->selection_signal, seat); |
| 423 | } |
| 424 | |
| 425 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 426 | weston_seat_set_selection(struct weston_seat *seat, |
| 427 | struct wl_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 428 | { |
| 429 | struct wl_resource *data_device, *offer; |
| 430 | struct wl_resource *focus = NULL; |
| 431 | |
| 432 | if (seat->selection_data_source && |
| 433 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 434 | return; |
| 435 | |
| 436 | if (seat->selection_data_source) { |
| 437 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 438 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 439 | seat->selection_data_source = NULL; |
| 440 | } |
| 441 | |
| 442 | seat->selection_data_source = source; |
| 443 | seat->selection_serial = serial; |
| 444 | |
| 445 | if (seat->keyboard) |
| 446 | focus = seat->keyboard->focus_resource; |
| 447 | if (focus) { |
| 448 | data_device = find_resource(&seat->drag_resource_list, |
| 449 | focus->client); |
| 450 | if (data_device && source) { |
| 451 | offer = wl_data_source_send_offer(seat->selection_data_source, |
| 452 | data_device); |
| 453 | wl_data_device_send_selection(data_device, offer); |
| 454 | } else if (data_device) { |
| 455 | wl_data_device_send_selection(data_device, NULL); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | wl_signal_emit(&seat->selection_signal, seat); |
| 460 | |
| 461 | if (source) { |
| 462 | seat->selection_data_source_listener.notify = |
| 463 | destroy_selection_data_source; |
| 464 | wl_signal_add(&source->resource.destroy_signal, |
| 465 | &seat->selection_data_source_listener); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static void |
| 470 | data_device_set_selection(struct wl_client *client, |
| 471 | struct wl_resource *resource, |
| 472 | struct wl_resource *source_resource, uint32_t serial) |
| 473 | { |
| 474 | if (!source_resource) |
| 475 | return; |
| 476 | |
| 477 | /* FIXME: Store serial and check against incoming serial here. */ |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 478 | weston_seat_set_selection(resource->data, source_resource->data, |
| 479 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static const struct wl_data_device_interface data_device_interface = { |
| 483 | data_device_start_drag, |
| 484 | data_device_set_selection, |
| 485 | }; |
| 486 | |
| 487 | static void |
| 488 | destroy_data_source(struct wl_resource *resource) |
| 489 | { |
| 490 | struct wl_data_source *source = |
| 491 | container_of(resource, struct wl_data_source, resource); |
| 492 | char **p; |
| 493 | |
| 494 | wl_array_for_each(p, &source->mime_types) |
| 495 | free(*p); |
| 496 | |
| 497 | wl_array_release(&source->mime_types); |
| 498 | |
| 499 | source->resource.object.id = 0; |
| 500 | } |
| 501 | |
| 502 | static void |
| 503 | client_source_accept(struct wl_data_source *source, |
| 504 | uint32_t time, const char *mime_type) |
| 505 | { |
| 506 | wl_data_source_send_target(&source->resource, mime_type); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | client_source_send(struct wl_data_source *source, |
| 511 | const char *mime_type, int32_t fd) |
| 512 | { |
| 513 | wl_data_source_send_send(&source->resource, mime_type, fd); |
| 514 | close(fd); |
| 515 | } |
| 516 | |
| 517 | static void |
| 518 | client_source_cancel(struct wl_data_source *source) |
| 519 | { |
| 520 | wl_data_source_send_cancelled(&source->resource); |
| 521 | } |
| 522 | |
| 523 | static void |
| 524 | create_data_source(struct wl_client *client, |
| 525 | struct wl_resource *resource, uint32_t id) |
| 526 | { |
| 527 | struct wl_data_source *source; |
| 528 | |
| 529 | source = malloc(sizeof *source); |
| 530 | if (source == NULL) { |
| 531 | wl_resource_post_no_memory(resource); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | wl_resource_init(&source->resource, &wl_data_source_interface, |
| 536 | &data_source_interface, id, source); |
| 537 | source->resource.destroy = destroy_data_source; |
| 538 | |
| 539 | source->accept = client_source_accept; |
| 540 | source->send = client_source_send; |
| 541 | source->cancel = client_source_cancel; |
| 542 | |
| 543 | wl_array_init(&source->mime_types); |
| 544 | wl_client_add_resource(client, &source->resource); |
| 545 | } |
| 546 | |
| 547 | static void unbind_data_device(struct wl_resource *resource) |
| 548 | { |
| 549 | wl_list_remove(&resource->link); |
| 550 | free(resource); |
| 551 | } |
| 552 | |
| 553 | static void |
| 554 | get_data_device(struct wl_client *client, |
| 555 | struct wl_resource *manager_resource, |
| 556 | uint32_t id, struct wl_resource *seat_resource) |
| 557 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 558 | struct weston_seat *seat = seat_resource->data; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 559 | struct wl_resource *resource; |
| 560 | |
| 561 | resource = wl_client_add_object(client, &wl_data_device_interface, |
| 562 | &data_device_interface, id, |
| 563 | seat); |
| 564 | |
| 565 | wl_list_insert(&seat->drag_resource_list, &resource->link); |
| 566 | resource->destroy = unbind_data_device; |
| 567 | } |
| 568 | |
| 569 | static const struct wl_data_device_manager_interface manager_interface = { |
| 570 | create_data_source, |
| 571 | get_data_device |
| 572 | }; |
| 573 | |
| 574 | static void |
| 575 | bind_manager(struct wl_client *client, |
| 576 | void *data, uint32_t version, uint32_t id) |
| 577 | { |
| 578 | wl_client_add_object(client, &wl_data_device_manager_interface, |
| 579 | &manager_interface, id, NULL); |
| 580 | } |
| 581 | |
| 582 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 583 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 584 | { |
| 585 | struct wl_resource *data_device, *focus, *offer; |
| 586 | struct wl_data_source *source; |
| 587 | |
| 588 | if (!seat->keyboard) |
| 589 | return; |
| 590 | |
| 591 | focus = seat->keyboard->focus_resource; |
| 592 | if (!focus) |
| 593 | return; |
| 594 | |
| 595 | data_device = find_resource(&seat->drag_resource_list, |
| 596 | focus->client); |
| 597 | if (!data_device) |
| 598 | return; |
| 599 | |
| 600 | source = seat->selection_data_source; |
| 601 | if (source) { |
| 602 | offer = wl_data_source_send_offer(source, data_device); |
| 603 | wl_data_device_send_selection(data_device, offer); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | WL_EXPORT int |
| 608 | wl_data_device_manager_init(struct wl_display *display) |
| 609 | { |
| 610 | if (wl_display_add_global(display, |
| 611 | &wl_data_device_manager_interface, |
| 612 | NULL, bind_manager) == NULL) |
| 613 | return -1; |
| 614 | |
| 615 | return 0; |
| 616 | } |