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 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 30 | struct weston_drag { |
| 31 | struct wl_client *client; |
| 32 | struct wl_data_source *data_source; |
| 33 | struct wl_listener data_source_listener; |
| 34 | struct weston_surface *focus; |
| 35 | struct wl_resource *focus_resource; |
| 36 | struct wl_listener focus_listener; |
| 37 | struct weston_pointer_grab grab; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 38 | struct weston_surface *icon; |
| 39 | struct wl_listener icon_destroy_listener; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 40 | int32_t dx, dy; |
| 41 | }; |
| 42 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 43 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 44 | empty_region(pixman_region32_t *region) |
| 45 | { |
| 46 | pixman_region32_fini(region); |
| 47 | pixman_region32_init(region); |
| 48 | } |
| 49 | |
| 50 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 51 | data_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 52 | uint32_t serial, const char *mime_type) |
| 53 | { |
| 54 | struct wl_data_offer *offer = resource->data; |
| 55 | |
| 56 | /* FIXME: Check that client is currently focused by the input |
| 57 | * device that is currently dragging this data source. Should |
| 58 | * this be a wl_data_device request? */ |
| 59 | |
| 60 | if (offer->source) |
| 61 | offer->source->accept(offer->source, serial, mime_type); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | data_offer_receive(struct wl_client *client, struct wl_resource *resource, |
| 66 | const char *mime_type, int32_t fd) |
| 67 | { |
| 68 | struct wl_data_offer *offer = resource->data; |
| 69 | |
| 70 | if (offer->source) |
| 71 | offer->source->send(offer->source, mime_type, fd); |
| 72 | else |
| 73 | close(fd); |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | data_offer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 78 | { |
| 79 | wl_resource_destroy(resource); |
| 80 | } |
| 81 | |
| 82 | static const struct wl_data_offer_interface data_offer_interface = { |
| 83 | data_offer_accept, |
| 84 | data_offer_receive, |
| 85 | data_offer_destroy, |
| 86 | }; |
| 87 | |
| 88 | static void |
| 89 | destroy_data_offer(struct wl_resource *resource) |
| 90 | { |
| 91 | struct wl_data_offer *offer = resource->data; |
| 92 | |
| 93 | if (offer->source) |
| 94 | wl_list_remove(&offer->source_destroy_listener.link); |
| 95 | free(offer); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | destroy_offer_data_source(struct wl_listener *listener, void *data) |
| 100 | { |
| 101 | struct wl_data_offer *offer; |
| 102 | |
| 103 | offer = container_of(listener, struct wl_data_offer, |
| 104 | source_destroy_listener); |
| 105 | |
| 106 | offer->source = NULL; |
| 107 | } |
| 108 | |
| 109 | static struct wl_resource * |
| 110 | wl_data_source_send_offer(struct wl_data_source *source, |
| 111 | struct wl_resource *target) |
| 112 | { |
| 113 | struct wl_data_offer *offer; |
| 114 | char **p; |
| 115 | |
| 116 | offer = malloc(sizeof *offer); |
| 117 | if (offer == NULL) |
| 118 | return NULL; |
| 119 | |
| 120 | wl_resource_init(&offer->resource, &wl_data_offer_interface, |
| 121 | &data_offer_interface, 0, offer); |
| 122 | offer->resource.destroy = destroy_data_offer; |
| 123 | |
| 124 | offer->source = source; |
| 125 | offer->source_destroy_listener.notify = destroy_offer_data_source; |
| 126 | wl_signal_add(&source->resource.destroy_signal, |
| 127 | &offer->source_destroy_listener); |
| 128 | |
| 129 | wl_client_add_resource(target->client, &offer->resource); |
| 130 | |
| 131 | wl_data_device_send_data_offer(target, &offer->resource); |
| 132 | |
| 133 | wl_array_for_each(p, &source->mime_types) |
| 134 | wl_data_offer_send_offer(&offer->resource, *p); |
| 135 | |
| 136 | return &offer->resource; |
| 137 | } |
| 138 | |
| 139 | static void |
| 140 | data_source_offer(struct wl_client *client, |
| 141 | struct wl_resource *resource, |
| 142 | const char *type) |
| 143 | { |
| 144 | struct wl_data_source *source = resource->data; |
| 145 | char **p; |
| 146 | |
| 147 | p = wl_array_add(&source->mime_types, sizeof *p); |
| 148 | if (p) |
| 149 | *p = strdup(type); |
| 150 | if (!p || !*p) |
| 151 | wl_resource_post_no_memory(resource); |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | data_source_destroy(struct wl_client *client, struct wl_resource *resource) |
| 156 | { |
| 157 | wl_resource_destroy(resource); |
| 158 | } |
| 159 | |
| 160 | static struct wl_data_source_interface data_source_interface = { |
| 161 | data_source_offer, |
| 162 | data_source_destroy |
| 163 | }; |
| 164 | |
| 165 | static struct wl_resource * |
| 166 | find_resource(struct wl_list *list, struct wl_client *client) |
| 167 | { |
| 168 | struct wl_resource *r; |
| 169 | |
| 170 | wl_list_for_each(r, list, link) { |
| 171 | if (r->client == client) |
| 172 | return r; |
| 173 | } |
| 174 | |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 179 | drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height) |
| 180 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 181 | struct weston_drag *drag = es->configure_private; |
| 182 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 183 | struct wl_list *list; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 184 | float fx, fy; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 185 | |
| 186 | if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) { |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 187 | if (pointer->sprite && weston_surface_is_mapped(pointer->sprite)) |
| 188 | list = &pointer->sprite->layer_link; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 189 | else |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 190 | list = &es->compositor->cursor_layer.surface_list; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 191 | |
| 192 | wl_list_insert(list, &es->layer_link); |
| 193 | weston_surface_update_transform(es); |
| 194 | empty_region(&es->pending.input); |
| 195 | } |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 196 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 197 | drag->dx += sx; |
| 198 | drag->dy += sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 199 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 200 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 201 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 202 | weston_surface_configure(es, fx, fy, width, height); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 205 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 206 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 207 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 208 | struct weston_drag *drag = |
| 209 | container_of(listener, struct weston_drag, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 210 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 211 | drag->focus_resource = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 215 | weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface, |
| 216 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 217 | { |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 218 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 219 | struct wl_resource *resource, *offer = NULL; |
| 220 | struct wl_display *display; |
| 221 | uint32_t serial; |
| 222 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 223 | if (drag->focus_resource) { |
| 224 | wl_data_device_send_leave(drag->focus_resource); |
| 225 | wl_list_remove(&drag->focus_listener.link); |
| 226 | drag->focus_resource = NULL; |
| 227 | drag->focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (!surface) |
| 231 | return; |
| 232 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 233 | if (!drag->data_source && |
| 234 | wl_resource_get_client(surface->resource) != drag->client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 235 | return; |
| 236 | |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 237 | resource = find_resource(&pointer->seat->drag_resource_list, |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 238 | wl_resource_get_client(surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 239 | if (!resource) |
| 240 | return; |
| 241 | |
| 242 | display = wl_client_get_display(resource->client); |
| 243 | serial = wl_display_next_serial(display); |
| 244 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 245 | if (drag->data_source) |
| 246 | offer = wl_data_source_send_offer(drag->data_source, resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 247 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 248 | wl_data_device_send_enter(resource, serial, surface->resource, |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 249 | sx, sy, offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 250 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 251 | drag->focus = surface; |
| 252 | drag->focus_listener.notify = destroy_drag_focus; |
| 253 | wl_signal_add(&resource->destroy_signal, &drag->focus_listener); |
| 254 | drag->focus_resource = resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 258 | drag_grab_focus(struct weston_pointer_grab *grab) |
| 259 | { |
| 260 | struct weston_drag *drag = |
| 261 | container_of(grab, struct weston_drag, grab); |
| 262 | struct weston_pointer *pointer = grab->pointer; |
| 263 | struct weston_surface *surface; |
| 264 | wl_fixed_t sx, sy; |
| 265 | |
| 266 | surface = weston_compositor_pick_surface(pointer->seat->compositor, |
| 267 | pointer->x, pointer->y, |
| 268 | &sx, &sy); |
| 269 | if (drag->focus != surface) |
| 270 | weston_drag_set_focus(drag, surface, sx, sy); |
| 271 | } |
| 272 | |
| 273 | static void |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 274 | drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 275 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 276 | struct weston_drag *drag = |
| 277 | container_of(grab, struct weston_drag, grab); |
| 278 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 279 | float fx, fy; |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 280 | wl_fixed_t sx, sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 281 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 282 | if (drag->icon) { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 283 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 284 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 285 | weston_surface_set_position(drag->icon, fx, fy); |
| 286 | weston_surface_schedule_repaint(drag->icon); |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 287 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 288 | |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 289 | if (drag->focus_resource) { |
| 290 | weston_surface_from_global_fixed(drag->focus, |
| 291 | pointer->x, pointer->y, |
| 292 | &sx, &sy); |
| 293 | |
| 294 | wl_data_device_send_motion(drag->focus_resource, time, sx, sy); |
| 295 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 299 | data_device_end_drag_grab(struct weston_drag *drag) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 300 | { |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 301 | if (drag->icon) { |
| 302 | if (weston_surface_is_mapped(drag->icon)) |
| 303 | weston_surface_unmap(drag->icon); |
| 304 | |
| 305 | drag->icon->configure = NULL; |
| 306 | empty_region(&drag->icon->pending.input); |
| 307 | wl_list_remove(&drag->icon_destroy_listener.link); |
| 308 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 309 | |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 310 | weston_drag_set_focus(drag, NULL, 0, 0); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 311 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 312 | weston_pointer_end_grab(drag->grab.pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 313 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 314 | free(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 318 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 319 | uint32_t time, uint32_t button, uint32_t state_w) |
| 320 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 321 | struct weston_drag *drag = |
| 322 | container_of(grab, struct weston_drag, grab); |
| 323 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 324 | enum wl_pointer_button_state state = state_w; |
| 325 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 326 | if (drag->focus_resource && |
| 327 | pointer->grab_button == button && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 328 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 329 | wl_data_device_send_drop(drag->focus_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 330 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 331 | if (pointer->button_count == 0 && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 332 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 333 | if (drag->data_source) |
| 334 | wl_list_remove(&drag->data_source_listener.link); |
| 335 | data_device_end_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 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 | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 348 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
| 349 | data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 350 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 351 | data_device_end_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 355 | handle_drag_icon_destroy(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 356 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 357 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 358 | icon_destroy_listener); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 359 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 360 | drag->icon = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 364 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 365 | struct wl_resource *source_resource, |
| 366 | struct wl_resource *origin_resource, |
| 367 | struct wl_resource *icon_resource, uint32_t serial) |
| 368 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame^] | 369 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
| 370 | struct weston_drag *drag = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 371 | struct weston_surface *icon = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 372 | |
Kristian Høgsberg | dba2586 | 2013-05-08 15:53:42 -0400 | [diff] [blame] | 373 | if (seat->pointer->button_count == 0 || |
| 374 | seat->pointer->grab_serial != serial || |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 375 | seat->pointer->focus != wl_resource_get_user_data(origin_resource)) |
Kristian Høgsberg | dba2586 | 2013-05-08 15:53:42 -0400 | [diff] [blame] | 376 | return; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 377 | |
| 378 | /* FIXME: Check that the data source type array isn't empty. */ |
| 379 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 380 | if (icon_resource) |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 381 | icon = wl_resource_get_user_data(icon_resource); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 382 | if (icon && icon->configure) { |
| 383 | wl_resource_post_error(icon_resource, |
| 384 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 385 | "surface->configure already set"); |
| 386 | return; |
| 387 | } |
| 388 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 389 | drag = malloc(sizeof *drag); |
| 390 | if (drag == NULL) { |
| 391 | wl_resource_post_no_memory(resource); |
| 392 | return; |
| 393 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 394 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 395 | memset(drag, 0, sizeof *drag); |
| 396 | drag->grab.interface = &drag_grab_interface; |
| 397 | drag->client = client; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 398 | |
| 399 | if (source_resource) { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 400 | drag->data_source = source_resource->data; |
| 401 | drag->data_source_listener.notify = destroy_data_device_source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 402 | wl_signal_add(&source_resource->destroy_signal, |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 403 | &drag->data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 406 | if (icon) { |
| 407 | drag->icon = icon; |
| 408 | drag->icon_destroy_listener.notify = handle_drag_icon_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 409 | wl_signal_add(&icon->destroy_signal, |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 410 | &drag->icon_destroy_listener); |
| 411 | |
| 412 | icon->configure = drag_surface_configure; |
| 413 | icon->configure_private = drag; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 416 | weston_pointer_set_focus(seat->pointer, NULL, |
| 417 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 418 | weston_pointer_start_grab(seat->pointer, &drag->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static void |
| 422 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 423 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 424 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 425 | selection_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 426 | struct wl_resource *data_device; |
| 427 | struct wl_resource *focus = NULL; |
| 428 | |
| 429 | seat->selection_data_source = NULL; |
| 430 | |
| 431 | if (seat->keyboard) |
| 432 | focus = seat->keyboard->focus_resource; |
| 433 | if (focus) { |
| 434 | data_device = find_resource(&seat->drag_resource_list, |
| 435 | focus->client); |
| 436 | if (data_device) |
| 437 | wl_data_device_send_selection(data_device, NULL); |
| 438 | } |
| 439 | |
| 440 | wl_signal_emit(&seat->selection_signal, seat); |
| 441 | } |
| 442 | |
| 443 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 444 | weston_seat_set_selection(struct weston_seat *seat, |
| 445 | struct wl_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 446 | { |
| 447 | struct wl_resource *data_device, *offer; |
| 448 | struct wl_resource *focus = NULL; |
| 449 | |
| 450 | if (seat->selection_data_source && |
| 451 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 452 | return; |
| 453 | |
| 454 | if (seat->selection_data_source) { |
| 455 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 456 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 457 | seat->selection_data_source = NULL; |
| 458 | } |
| 459 | |
| 460 | seat->selection_data_source = source; |
| 461 | seat->selection_serial = serial; |
| 462 | |
| 463 | if (seat->keyboard) |
| 464 | focus = seat->keyboard->focus_resource; |
| 465 | if (focus) { |
| 466 | data_device = find_resource(&seat->drag_resource_list, |
| 467 | focus->client); |
| 468 | if (data_device && source) { |
| 469 | offer = wl_data_source_send_offer(seat->selection_data_source, |
| 470 | data_device); |
| 471 | wl_data_device_send_selection(data_device, offer); |
| 472 | } else if (data_device) { |
| 473 | wl_data_device_send_selection(data_device, NULL); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | wl_signal_emit(&seat->selection_signal, seat); |
| 478 | |
| 479 | if (source) { |
| 480 | seat->selection_data_source_listener.notify = |
| 481 | destroy_selection_data_source; |
| 482 | wl_signal_add(&source->resource.destroy_signal, |
| 483 | &seat->selection_data_source_listener); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static void |
| 488 | data_device_set_selection(struct wl_client *client, |
| 489 | struct wl_resource *resource, |
| 490 | struct wl_resource *source_resource, uint32_t serial) |
| 491 | { |
| 492 | if (!source_resource) |
| 493 | return; |
| 494 | |
| 495 | /* FIXME: Store serial and check against incoming serial here. */ |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 496 | weston_seat_set_selection(resource->data, wl_resource_get_user_data(source_resource), |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 497 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static const struct wl_data_device_interface data_device_interface = { |
| 501 | data_device_start_drag, |
| 502 | data_device_set_selection, |
| 503 | }; |
| 504 | |
| 505 | static void |
| 506 | destroy_data_source(struct wl_resource *resource) |
| 507 | { |
| 508 | struct wl_data_source *source = |
| 509 | container_of(resource, struct wl_data_source, resource); |
| 510 | char **p; |
| 511 | |
| 512 | wl_array_for_each(p, &source->mime_types) |
| 513 | free(*p); |
| 514 | |
| 515 | wl_array_release(&source->mime_types); |
| 516 | |
| 517 | source->resource.object.id = 0; |
| 518 | } |
| 519 | |
| 520 | static void |
| 521 | client_source_accept(struct wl_data_source *source, |
| 522 | uint32_t time, const char *mime_type) |
| 523 | { |
| 524 | wl_data_source_send_target(&source->resource, mime_type); |
| 525 | } |
| 526 | |
| 527 | static void |
| 528 | client_source_send(struct wl_data_source *source, |
| 529 | const char *mime_type, int32_t fd) |
| 530 | { |
| 531 | wl_data_source_send_send(&source->resource, mime_type, fd); |
| 532 | close(fd); |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | client_source_cancel(struct wl_data_source *source) |
| 537 | { |
| 538 | wl_data_source_send_cancelled(&source->resource); |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | create_data_source(struct wl_client *client, |
| 543 | struct wl_resource *resource, uint32_t id) |
| 544 | { |
| 545 | struct wl_data_source *source; |
| 546 | |
| 547 | source = malloc(sizeof *source); |
| 548 | if (source == NULL) { |
| 549 | wl_resource_post_no_memory(resource); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | wl_resource_init(&source->resource, &wl_data_source_interface, |
| 554 | &data_source_interface, id, source); |
| 555 | source->resource.destroy = destroy_data_source; |
| 556 | |
| 557 | source->accept = client_source_accept; |
| 558 | source->send = client_source_send; |
| 559 | source->cancel = client_source_cancel; |
| 560 | |
| 561 | wl_array_init(&source->mime_types); |
| 562 | wl_client_add_resource(client, &source->resource); |
| 563 | } |
| 564 | |
| 565 | static void unbind_data_device(struct wl_resource *resource) |
| 566 | { |
| 567 | wl_list_remove(&resource->link); |
| 568 | free(resource); |
| 569 | } |
| 570 | |
| 571 | static void |
| 572 | get_data_device(struct wl_client *client, |
| 573 | struct wl_resource *manager_resource, |
| 574 | uint32_t id, struct wl_resource *seat_resource) |
| 575 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame^] | 576 | struct weston_seat *seat = wl_resource_get_user_data(seat_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 577 | struct wl_resource *resource; |
| 578 | |
| 579 | resource = wl_client_add_object(client, &wl_data_device_interface, |
| 580 | &data_device_interface, id, |
| 581 | seat); |
| 582 | |
| 583 | wl_list_insert(&seat->drag_resource_list, &resource->link); |
| 584 | resource->destroy = unbind_data_device; |
| 585 | } |
| 586 | |
| 587 | static const struct wl_data_device_manager_interface manager_interface = { |
| 588 | create_data_source, |
| 589 | get_data_device |
| 590 | }; |
| 591 | |
| 592 | static void |
| 593 | bind_manager(struct wl_client *client, |
| 594 | void *data, uint32_t version, uint32_t id) |
| 595 | { |
| 596 | wl_client_add_object(client, &wl_data_device_manager_interface, |
| 597 | &manager_interface, id, NULL); |
| 598 | } |
| 599 | |
| 600 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 601 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 602 | { |
| 603 | struct wl_resource *data_device, *focus, *offer; |
| 604 | struct wl_data_source *source; |
| 605 | |
| 606 | if (!seat->keyboard) |
| 607 | return; |
| 608 | |
| 609 | focus = seat->keyboard->focus_resource; |
| 610 | if (!focus) |
| 611 | return; |
| 612 | |
| 613 | data_device = find_resource(&seat->drag_resource_list, |
| 614 | focus->client); |
| 615 | if (!data_device) |
| 616 | return; |
| 617 | |
| 618 | source = seat->selection_data_source; |
| 619 | if (source) { |
| 620 | offer = wl_data_source_send_offer(source, data_device); |
| 621 | wl_data_device_send_selection(data_device, offer); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | WL_EXPORT int |
| 626 | wl_data_device_manager_init(struct wl_display *display) |
| 627 | { |
| 628 | if (wl_display_add_global(display, |
| 629 | &wl_data_device_manager_interface, |
| 630 | NULL, bind_manager) == NULL) |
| 631 | return -1; |
| 632 | |
| 633 | return 0; |
| 634 | } |