Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Kristian Høgsberg |
| 3 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -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 | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -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 | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | 8e7a8bd | 2013-08-15 01:10:24 +0100 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 31 | #include <stdint.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 32 | #include <stdio.h> |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 33 | #include <assert.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 34 | |
| 35 | #include "compositor.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 36 | #include "shared/helpers.h" |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 37 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 38 | struct weston_drag { |
| 39 | struct wl_client *client; |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 40 | struct weston_data_source *data_source; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 41 | struct wl_listener data_source_listener; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 42 | struct weston_view *focus; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 43 | struct wl_resource *focus_resource; |
| 44 | struct wl_listener focus_listener; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 45 | struct weston_view *icon; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 46 | struct wl_listener icon_destroy_listener; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 47 | int32_t dx, dy; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 48 | struct weston_keyboard_grab keyboard_grab; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 51 | struct weston_pointer_drag { |
| 52 | struct weston_drag base; |
| 53 | struct weston_pointer_grab grab; |
| 54 | }; |
| 55 | |
| 56 | struct weston_touch_drag { |
| 57 | struct weston_drag base; |
| 58 | struct weston_touch_grab grab; |
| 59 | }; |
| 60 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 61 | #define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \ |
| 62 | WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \ |
| 63 | WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) |
| 64 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 65 | static void |
| 66 | data_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 67 | uint32_t serial, const char *mime_type) |
| 68 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 69 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 70 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 71 | /* Protect against untimely calls from older data offers */ |
| 72 | if (!offer->source || offer != offer->source->offer) |
| 73 | return; |
| 74 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 75 | /* FIXME: Check that client is currently focused by the input |
| 76 | * device that is currently dragging this data source. Should |
| 77 | * this be a wl_data_device request? */ |
| 78 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 79 | offer->source->accept(offer->source, serial, mime_type); |
| 80 | offer->source->accepted = mime_type != NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void |
| 84 | data_offer_receive(struct wl_client *client, struct wl_resource *resource, |
| 85 | const char *mime_type, int32_t fd) |
| 86 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 87 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 88 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 89 | if (offer->source && offer == offer->source->offer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 90 | offer->source->send(offer->source, mime_type, fd); |
| 91 | else |
| 92 | close(fd); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | data_offer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 97 | { |
| 98 | wl_resource_destroy(resource); |
| 99 | } |
| 100 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 101 | static void |
| 102 | data_source_notify_finish(struct weston_data_source *source) |
| 103 | { |
Carlos Garnacho | 4061e2b | 2016-02-01 20:28:16 +0100 | [diff] [blame] | 104 | if (!source->actions_set) |
| 105 | return; |
| 106 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 107 | if (source->offer->in_ask && |
| 108 | wl_resource_get_version(source->resource) >= |
| 109 | WL_DATA_SOURCE_ACTION_SINCE_VERSION) { |
| 110 | wl_data_source_send_action(source->resource, |
| 111 | source->current_dnd_action); |
| 112 | } |
| 113 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 114 | if (wl_resource_get_version(source->resource) >= |
| 115 | WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { |
| 116 | wl_data_source_send_dnd_finished(source->resource); |
| 117 | } |
| 118 | |
| 119 | source->offer = NULL; |
| 120 | } |
| 121 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 122 | static uint32_t |
| 123 | data_offer_choose_action(struct weston_data_offer *offer) |
| 124 | { |
| 125 | uint32_t available_actions, preferred_action = 0; |
| 126 | uint32_t source_actions, offer_actions; |
| 127 | |
| 128 | if (wl_resource_get_version(offer->resource) >= |
| 129 | WL_DATA_OFFER_ACTION_SINCE_VERSION) { |
| 130 | offer_actions = offer->dnd_actions; |
| 131 | preferred_action = offer->preferred_dnd_action; |
| 132 | } else { |
| 133 | offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; |
| 134 | } |
| 135 | |
| 136 | if (wl_resource_get_version(offer->source->resource) >= |
| 137 | WL_DATA_SOURCE_ACTION_SINCE_VERSION) |
| 138 | source_actions = offer->source->dnd_actions; |
| 139 | else |
| 140 | source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; |
| 141 | |
| 142 | available_actions = offer_actions & source_actions; |
| 143 | |
| 144 | if (!available_actions) |
| 145 | return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; |
| 146 | |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 147 | if (offer->source->seat && |
| 148 | offer->source->compositor_action & available_actions) |
| 149 | return offer->source->compositor_action; |
| 150 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 151 | /* If the dest side has a preferred DnD action, use it */ |
| 152 | if ((preferred_action & available_actions) != 0) |
| 153 | return preferred_action; |
| 154 | |
| 155 | /* Use the first found action, in bit order */ |
| 156 | return 1 << (ffs(available_actions) - 1); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | data_offer_update_action(struct weston_data_offer *offer) |
| 161 | { |
| 162 | uint32_t action; |
| 163 | |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 164 | if (!offer->source) |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 165 | return; |
| 166 | |
| 167 | action = data_offer_choose_action(offer); |
| 168 | |
| 169 | if (offer->source->current_dnd_action == action) |
| 170 | return; |
| 171 | |
| 172 | offer->source->current_dnd_action = action; |
| 173 | |
| 174 | if (offer->in_ask) |
| 175 | return; |
| 176 | |
| 177 | if (wl_resource_get_version(offer->source->resource) >= |
| 178 | WL_DATA_SOURCE_ACTION_SINCE_VERSION) |
| 179 | wl_data_source_send_action(offer->source->resource, action); |
| 180 | |
| 181 | if (wl_resource_get_version(offer->resource) >= |
| 182 | WL_DATA_OFFER_ACTION_SINCE_VERSION) |
| 183 | wl_data_offer_send_action(offer->resource, action); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | data_offer_set_actions(struct wl_client *client, |
| 188 | struct wl_resource *resource, |
| 189 | uint32_t dnd_actions, uint32_t preferred_action) |
| 190 | { |
| 191 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
| 192 | |
| 193 | if (dnd_actions & ~ALL_ACTIONS) { |
| 194 | wl_resource_post_error(offer->resource, |
| 195 | WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK, |
| 196 | "invalid action mask %x", dnd_actions); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (preferred_action && |
| 201 | (!(preferred_action & dnd_actions) || |
| 202 | __builtin_popcount(preferred_action) > 1)) { |
| 203 | wl_resource_post_error(offer->resource, |
| 204 | WL_DATA_OFFER_ERROR_INVALID_ACTION, |
| 205 | "invalid action %x", preferred_action); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | offer->dnd_actions = dnd_actions; |
| 210 | offer->preferred_dnd_action = preferred_action; |
| 211 | data_offer_update_action(offer); |
| 212 | } |
| 213 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 214 | static void |
| 215 | data_offer_finish(struct wl_client *client, struct wl_resource *resource) |
| 216 | { |
| 217 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
| 218 | |
| 219 | if (!offer->source || offer->source->offer != offer) |
| 220 | return; |
| 221 | |
| 222 | /* Disallow finish while we have a grab driving drag-and-drop, or |
| 223 | * if the negotiation is not at the right stage |
| 224 | */ |
| 225 | if (offer->source->seat || |
| 226 | !offer->source->accepted) { |
| 227 | wl_resource_post_error(offer->resource, |
| 228 | WL_DATA_OFFER_ERROR_INVALID_FINISH, |
| 229 | "premature finish request"); |
| 230 | return; |
| 231 | } |
| 232 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 233 | switch (offer->source->current_dnd_action) { |
| 234 | case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE: |
| 235 | case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK: |
| 236 | wl_resource_post_error(offer->resource, |
| 237 | WL_DATA_OFFER_ERROR_INVALID_OFFER, |
| 238 | "offer finished with an invalid action"); |
| 239 | return; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 244 | data_source_notify_finish(offer->source); |
| 245 | } |
| 246 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 247 | static const struct wl_data_offer_interface data_offer_interface = { |
| 248 | data_offer_accept, |
| 249 | data_offer_receive, |
| 250 | data_offer_destroy, |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 251 | data_offer_finish, |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 252 | data_offer_set_actions, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | static void |
| 256 | destroy_data_offer(struct wl_resource *resource) |
| 257 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 258 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 259 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 260 | if (!offer->source) |
| 261 | goto out; |
| 262 | |
| 263 | wl_list_remove(&offer->source_destroy_listener.link); |
| 264 | |
| 265 | if (offer->source->offer != offer) |
| 266 | goto out; |
| 267 | |
| 268 | /* If the drag destination has version < 3, wl_data_offer.finish |
| 269 | * won't be called, so do this here as a safety net, because |
| 270 | * we still want the version >=3 drag source to be happy. |
| 271 | */ |
| 272 | if (wl_resource_get_version(offer->resource) < |
| 273 | WL_DATA_OFFER_ACTION_SINCE_VERSION) { |
| 274 | data_source_notify_finish(offer->source); |
Carlos Garnacho | 4061e2b | 2016-02-01 20:28:16 +0100 | [diff] [blame] | 275 | } else if (offer->source->resource && |
| 276 | wl_resource_get_version(offer->source->resource) >= |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 277 | WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { |
| 278 | wl_data_source_send_cancelled(offer->source->resource); |
| 279 | } |
| 280 | |
| 281 | offer->source->offer = NULL; |
| 282 | out: |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 283 | free(offer); |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | destroy_offer_data_source(struct wl_listener *listener, void *data) |
| 288 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 289 | struct weston_data_offer *offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 290 | |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 291 | offer = container_of(listener, struct weston_data_offer, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 292 | source_destroy_listener); |
| 293 | |
| 294 | offer->source = NULL; |
| 295 | } |
| 296 | |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 297 | static struct weston_data_offer * |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 298 | weston_data_source_send_offer(struct weston_data_source *source, |
| 299 | struct wl_resource *target) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 300 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 301 | struct weston_data_offer *offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 302 | char **p; |
| 303 | |
| 304 | offer = malloc(sizeof *offer); |
| 305 | if (offer == NULL) |
| 306 | return NULL; |
| 307 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 308 | offer->resource = |
| 309 | wl_resource_create(wl_resource_get_client(target), |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 310 | &wl_data_offer_interface, |
| 311 | wl_resource_get_version(target), 0); |
Kristian Høgsberg | 3c30f0f | 2013-08-06 10:24:04 -0700 | [diff] [blame] | 312 | if (offer->resource == NULL) { |
| 313 | free(offer); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 317 | wl_resource_set_implementation(offer->resource, &data_offer_interface, |
| 318 | offer, destroy_data_offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 319 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 320 | offer->in_ask = false; |
| 321 | offer->dnd_actions = 0; |
| 322 | offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 323 | offer->source = source; |
| 324 | offer->source_destroy_listener.notify = destroy_offer_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 325 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 326 | &offer->source_destroy_listener); |
| 327 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 328 | wl_data_device_send_data_offer(target, offer->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 329 | |
| 330 | wl_array_for_each(p, &source->mime_types) |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 331 | wl_data_offer_send_offer(offer->resource, *p); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 332 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 333 | source->offer = offer; |
| 334 | source->accepted = false; |
| 335 | |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 336 | return offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void |
| 340 | data_source_offer(struct wl_client *client, |
| 341 | struct wl_resource *resource, |
| 342 | const char *type) |
| 343 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 344 | struct weston_data_source *source = |
| 345 | wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 346 | char **p; |
| 347 | |
| 348 | p = wl_array_add(&source->mime_types, sizeof *p); |
| 349 | if (p) |
| 350 | *p = strdup(type); |
| 351 | if (!p || !*p) |
| 352 | wl_resource_post_no_memory(resource); |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | data_source_destroy(struct wl_client *client, struct wl_resource *resource) |
| 357 | { |
| 358 | wl_resource_destroy(resource); |
| 359 | } |
| 360 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 361 | static void |
| 362 | data_source_set_actions(struct wl_client *client, |
| 363 | struct wl_resource *resource, |
| 364 | uint32_t dnd_actions) |
| 365 | { |
| 366 | struct weston_data_source *source = |
| 367 | wl_resource_get_user_data(resource); |
| 368 | |
| 369 | if (source->actions_set) { |
| 370 | wl_resource_post_error(source->resource, |
| 371 | WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, |
| 372 | "cannot set actions more than once"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | if (dnd_actions & ~ALL_ACTIONS) { |
| 377 | wl_resource_post_error(source->resource, |
| 378 | WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, |
| 379 | "invalid action mask %x", dnd_actions); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | if (source->seat) { |
| 384 | wl_resource_post_error(source->resource, |
| 385 | WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK, |
| 386 | "invalid action change after " |
| 387 | "wl_data_device.start_drag"); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | source->dnd_actions = dnd_actions; |
| 392 | source->actions_set = true; |
| 393 | } |
| 394 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 395 | static struct wl_data_source_interface data_source_interface = { |
| 396 | data_source_offer, |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 397 | data_source_destroy, |
| 398 | data_source_set_actions |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 399 | }; |
| 400 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 401 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 402 | drag_surface_configure(struct weston_drag *drag, |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 403 | struct weston_pointer *pointer, |
| 404 | struct weston_touch *touch, |
| 405 | struct weston_surface *es, |
| 406 | int32_t sx, int32_t sy) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 407 | { |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 408 | struct weston_layer_entry *list; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 409 | float fx, fy; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 410 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 411 | assert((pointer != NULL && touch == NULL) || |
| 412 | (pointer == NULL && touch != NULL)); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 413 | |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 414 | if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 415 | if (pointer && pointer->sprite && |
| 416 | weston_view_is_mapped(pointer->sprite)) |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 417 | list = &pointer->sprite->layer_link; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 418 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 419 | list = &es->compositor->cursor_layer.view_list; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 420 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 421 | weston_layer_entry_remove(&drag->icon->layer_link); |
| 422 | weston_layer_entry_insert(list, &drag->icon->layer_link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 423 | weston_view_update_transform(drag->icon); |
Jason Ekstrand | ef54008 | 2014-06-26 10:37:36 -0700 | [diff] [blame] | 424 | pixman_region32_clear(&es->pending.input); |
Armin Krezović | f8486c3 | 2016-06-30 06:04:28 +0200 | [diff] [blame] | 425 | es->is_mapped = true; |
| 426 | drag->icon->is_mapped = true; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 427 | } |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 428 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 429 | drag->dx += sx; |
| 430 | drag->dy += sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 431 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 432 | /* init to 0 for avoiding a compile warning */ |
| 433 | fx = fy = 0; |
| 434 | if (pointer) { |
| 435 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 436 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
| 437 | } else if (touch) { |
| 438 | fx = wl_fixed_to_double(touch->grab_x) + drag->dx; |
| 439 | fy = wl_fixed_to_double(touch->grab_y) + drag->dy; |
| 440 | } |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 441 | weston_view_set_position(drag->icon, fx, fy); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 444 | static int |
| 445 | pointer_drag_surface_get_label(struct weston_surface *surface, |
| 446 | char *buf, size_t len) |
| 447 | { |
| 448 | return snprintf(buf, len, "pointer drag icon"); |
| 449 | } |
| 450 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 451 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 452 | pointer_drag_surface_committed(struct weston_surface *es, |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 453 | int32_t sx, int32_t sy) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 454 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 455 | struct weston_pointer_drag *drag = es->committed_private; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 456 | struct weston_pointer *pointer = drag->grab.pointer; |
| 457 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 458 | assert(es->committed == pointer_drag_surface_committed); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 459 | |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 460 | drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 461 | } |
| 462 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 463 | static int |
| 464 | touch_drag_surface_get_label(struct weston_surface *surface, |
| 465 | char *buf, size_t len) |
| 466 | { |
| 467 | return snprintf(buf, len, "touch drag icon"); |
| 468 | } |
| 469 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 470 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 471 | touch_drag_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 472 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 473 | struct weston_touch_drag *drag = es->committed_private; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 474 | struct weston_touch *touch = drag->grab.touch; |
| 475 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 476 | assert(es->committed == touch_drag_surface_committed); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 477 | |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 478 | drag_surface_configure(&drag->base, NULL, touch, es, sx, sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 482 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 483 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 484 | struct weston_drag *drag = |
| 485 | container_of(listener, struct weston_drag, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 486 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 487 | drag->focus_resource = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 491 | weston_drag_set_focus(struct weston_drag *drag, |
| 492 | struct weston_seat *seat, |
| 493 | struct weston_view *view, |
| 494 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 495 | { |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 496 | struct wl_resource *resource, *offer_resource = NULL; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 497 | struct wl_display *display = seat->compositor->wl_display; |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 498 | struct weston_data_offer *offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 499 | uint32_t serial; |
| 500 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 501 | if (drag->focus && view && drag->focus->surface == view->surface) { |
| 502 | drag->focus = view; |
| 503 | return; |
| 504 | } |
| 505 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 506 | if (drag->focus_resource) { |
| 507 | wl_data_device_send_leave(drag->focus_resource); |
| 508 | wl_list_remove(&drag->focus_listener.link); |
| 509 | drag->focus_resource = NULL; |
| 510 | drag->focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 513 | if (!view || !view->surface->resource) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 514 | return; |
| 515 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 516 | if (!drag->data_source && |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 517 | wl_resource_get_client(view->surface->resource) != drag->client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 518 | return; |
| 519 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 520 | if (drag->data_source && |
| 521 | drag->data_source->offer) { |
| 522 | /* Unlink the offer from the source */ |
| 523 | offer = drag->data_source->offer; |
| 524 | offer->source = NULL; |
| 525 | drag->data_source->offer = NULL; |
| 526 | wl_list_remove(&offer->source_destroy_listener.link); |
| 527 | } |
| 528 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 529 | resource = wl_resource_find_for_client(&seat->drag_resource_list, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 530 | wl_resource_get_client(view->surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 531 | if (!resource) |
| 532 | return; |
| 533 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 534 | serial = wl_display_next_serial(display); |
| 535 | |
Kristian Høgsberg | a34e2f2 | 2013-08-20 15:58:25 -0700 | [diff] [blame] | 536 | if (drag->data_source) { |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 537 | drag->data_source->accepted = false; |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 538 | offer = weston_data_source_send_offer(drag->data_source, resource); |
| 539 | if (offer == NULL) |
Kristian Høgsberg | a34e2f2 | 2013-08-20 15:58:25 -0700 | [diff] [blame] | 540 | return; |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 541 | |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 542 | data_offer_update_action(offer); |
| 543 | |
| 544 | offer_resource = offer->resource; |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 545 | if (wl_resource_get_version (offer_resource) >= |
| 546 | WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { |
| 547 | wl_data_offer_send_source_actions (offer_resource, |
| 548 | drag->data_source->dnd_actions); |
| 549 | } |
Kristian Høgsberg | a34e2f2 | 2013-08-20 15:58:25 -0700 | [diff] [blame] | 550 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 551 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 552 | wl_data_device_send_enter(resource, serial, view->surface->resource, |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 553 | sx, sy, offer_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 554 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 555 | drag->focus = view; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 556 | drag->focus_listener.notify = destroy_drag_focus; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 557 | wl_resource_add_destroy_listener(resource, &drag->focus_listener); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 558 | drag->focus_resource = resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 562 | drag_grab_focus(struct weston_pointer_grab *grab) |
| 563 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 564 | struct weston_pointer_drag *drag = |
| 565 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 566 | struct weston_pointer *pointer = grab->pointer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 567 | struct weston_view *view; |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 568 | wl_fixed_t sx, sy; |
| 569 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 570 | view = weston_compositor_pick_view(pointer->seat->compositor, |
| 571 | pointer->x, pointer->y, |
| 572 | &sx, &sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 573 | if (drag->base.focus != view) |
| 574 | weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static void |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 578 | drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time, |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 579 | struct weston_pointer_motion_event *event) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 580 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 581 | struct weston_pointer_drag *drag = |
| 582 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 583 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 584 | float fx, fy; |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 585 | wl_fixed_t sx, sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 586 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 587 | weston_pointer_move(pointer, event); |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 588 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 589 | if (drag->base.icon) { |
| 590 | fx = wl_fixed_to_double(pointer->x) + drag->base.dx; |
| 591 | fy = wl_fixed_to_double(pointer->y) + drag->base.dy; |
| 592 | weston_view_set_position(drag->base.icon, fx, fy); |
| 593 | weston_view_schedule_repaint(drag->base.icon); |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 594 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 595 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 596 | if (drag->base.focus_resource) { |
| 597 | weston_view_from_global_fixed(drag->base.focus, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 598 | pointer->x, pointer->y, |
| 599 | &sx, &sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 600 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 601 | wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 602 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 606 | data_device_end_drag_grab(struct weston_drag *drag, |
| 607 | struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 608 | { |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 609 | if (drag->icon) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 610 | if (weston_view_is_mapped(drag->icon)) |
| 611 | weston_view_unmap(drag->icon); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 612 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 613 | drag->icon->surface->committed = NULL; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 614 | weston_surface_set_label_func(drag->icon->surface, NULL); |
Jason Ekstrand | ef54008 | 2014-06-26 10:37:36 -0700 | [diff] [blame] | 615 | pixman_region32_clear(&drag->icon->surface->pending.input); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 616 | wl_list_remove(&drag->icon_destroy_listener.link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 617 | weston_view_destroy(drag->icon); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 618 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 619 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 620 | weston_drag_set_focus(drag, seat, NULL, 0, 0); |
| 621 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 622 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 623 | static void |
| 624 | data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag) |
| 625 | { |
| 626 | struct weston_pointer *pointer = drag->grab.pointer; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 627 | struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 628 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 629 | data_device_end_drag_grab(&drag->base, pointer->seat); |
| 630 | weston_pointer_end_grab(pointer); |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 631 | weston_keyboard_end_grab(keyboard); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 632 | free(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 636 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 637 | uint32_t time, uint32_t button, uint32_t state_w) |
| 638 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 639 | struct weston_pointer_drag *drag = |
| 640 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 641 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 642 | enum wl_pointer_button_state state = state_w; |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 643 | struct weston_data_source *data_source = drag->base.data_source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 644 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 645 | if (data_source && |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 646 | pointer->grab_button == button && |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 647 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
| 648 | if (drag->base.focus_resource && |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 649 | data_source->accepted && |
| 650 | data_source->current_dnd_action) { |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 651 | wl_data_device_send_drop(drag->base.focus_resource); |
| 652 | |
| 653 | if (wl_resource_get_version(data_source->resource) >= |
| 654 | WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) |
| 655 | wl_data_source_send_dnd_drop_performed(data_source->resource); |
| 656 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 657 | data_source->offer->in_ask = |
| 658 | data_source->current_dnd_action == |
| 659 | WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; |
| 660 | |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 661 | data_source->seat = NULL; |
| 662 | } else if (wl_resource_get_version(data_source->resource) >= |
| 663 | WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { |
| 664 | wl_data_source_send_cancelled(data_source->resource); |
| 665 | } |
| 666 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 667 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 668 | if (pointer->button_count == 0 && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 669 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 670 | if (drag->base.data_source) |
| 671 | wl_list_remove(&drag->base.data_source_listener.link); |
| 672 | data_device_end_pointer_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 676 | static void |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 677 | drag_grab_axis(struct weston_pointer_grab *grab, |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 678 | uint32_t time, struct weston_pointer_axis_event *event) |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 679 | { |
| 680 | } |
| 681 | |
| 682 | static void |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 683 | drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source) |
| 684 | { |
| 685 | } |
| 686 | |
| 687 | static void |
| 688 | drag_grab_frame(struct weston_pointer_grab *grab) |
| 689 | { |
| 690 | } |
| 691 | |
| 692 | static void |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 693 | drag_grab_cancel(struct weston_pointer_grab *grab) |
| 694 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 695 | struct weston_pointer_drag *drag = |
| 696 | container_of(grab, struct weston_pointer_drag, grab); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 697 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 698 | if (drag->base.data_source) |
| 699 | wl_list_remove(&drag->base.data_source_listener.link); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 700 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 701 | data_device_end_pointer_drag_grab(drag); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 704 | static const struct weston_pointer_grab_interface pointer_drag_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 705 | drag_grab_focus, |
| 706 | drag_grab_motion, |
| 707 | drag_grab_button, |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 708 | drag_grab_axis, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 709 | drag_grab_axis_source, |
| 710 | drag_grab_frame, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 711 | drag_grab_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 712 | }; |
| 713 | |
| 714 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 715 | drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time, |
| 716 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 717 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 718 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 719 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 720 | static void |
| 721 | data_device_end_touch_drag_grab(struct weston_touch_drag *drag) |
| 722 | { |
| 723 | struct weston_touch *touch = drag->grab.touch; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 724 | struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 725 | |
| 726 | data_device_end_drag_grab(&drag->base, touch->seat); |
| 727 | weston_touch_end_grab(touch); |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 728 | weston_keyboard_end_grab(keyboard); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 729 | free(drag); |
| 730 | } |
| 731 | |
| 732 | static void |
| 733 | drag_grab_touch_up(struct weston_touch_grab *grab, |
| 734 | uint32_t time, int touch_id) |
| 735 | { |
| 736 | struct weston_touch_drag *touch_drag = |
| 737 | container_of(grab, struct weston_touch_drag, grab); |
| 738 | struct weston_touch *touch = grab->touch; |
| 739 | |
| 740 | if (touch_id != touch->grab_touch_id) |
| 741 | return; |
| 742 | |
| 743 | if (touch_drag->base.focus_resource) |
| 744 | wl_data_device_send_drop(touch_drag->base.focus_resource); |
| 745 | if (touch_drag->base.data_source) |
| 746 | wl_list_remove(&touch_drag->base.data_source_listener.link); |
| 747 | data_device_end_touch_drag_grab(touch_drag); |
| 748 | } |
| 749 | |
| 750 | static void |
| 751 | drag_grab_touch_focus(struct weston_touch_drag *drag) |
| 752 | { |
| 753 | struct weston_touch *touch = drag->grab.touch; |
| 754 | struct weston_view *view; |
| 755 | wl_fixed_t view_x, view_y; |
| 756 | |
| 757 | view = weston_compositor_pick_view(touch->seat->compositor, |
| 758 | touch->grab_x, touch->grab_y, |
| 759 | &view_x, &view_y); |
| 760 | if (drag->base.focus != view) |
| 761 | weston_drag_set_focus(&drag->base, touch->seat, |
| 762 | view, view_x, view_y); |
| 763 | } |
| 764 | |
| 765 | static void |
| 766 | drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time, |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 767 | int touch_id, wl_fixed_t x, wl_fixed_t y) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 768 | { |
| 769 | struct weston_touch_drag *touch_drag = |
| 770 | container_of(grab, struct weston_touch_drag, grab); |
| 771 | struct weston_touch *touch = grab->touch; |
| 772 | wl_fixed_t view_x, view_y; |
| 773 | float fx, fy; |
| 774 | |
| 775 | if (touch_id != touch->grab_touch_id) |
| 776 | return; |
| 777 | |
| 778 | drag_grab_touch_focus(touch_drag); |
| 779 | if (touch_drag->base.icon) { |
| 780 | fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx; |
| 781 | fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy; |
| 782 | weston_view_set_position(touch_drag->base.icon, fx, fy); |
| 783 | weston_view_schedule_repaint(touch_drag->base.icon); |
| 784 | } |
| 785 | |
| 786 | if (touch_drag->base.focus_resource) { |
| 787 | weston_view_from_global_fixed(touch_drag->base.focus, |
| 788 | touch->grab_x, touch->grab_y, |
| 789 | &view_x, &view_y); |
| 790 | wl_data_device_send_motion(touch_drag->base.focus_resource, time, |
| 791 | view_x, view_y); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static void |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 796 | drag_grab_touch_frame(struct weston_touch_grab *grab) |
| 797 | { |
| 798 | } |
| 799 | |
| 800 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 801 | drag_grab_touch_cancel(struct weston_touch_grab *grab) |
| 802 | { |
| 803 | struct weston_touch_drag *touch_drag = |
| 804 | container_of(grab, struct weston_touch_drag, grab); |
| 805 | |
| 806 | if (touch_drag->base.data_source) |
| 807 | wl_list_remove(&touch_drag->base.data_source_listener.link); |
| 808 | data_device_end_touch_drag_grab(touch_drag); |
| 809 | } |
| 810 | |
| 811 | static const struct weston_touch_grab_interface touch_drag_grab_interface = { |
| 812 | drag_grab_touch_down, |
| 813 | drag_grab_touch_up, |
| 814 | drag_grab_touch_motion, |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 815 | drag_grab_touch_frame, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 816 | drag_grab_touch_cancel |
| 817 | }; |
| 818 | |
| 819 | static void |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 820 | drag_grab_keyboard_key(struct weston_keyboard_grab *grab, |
| 821 | uint32_t time, uint32_t key, uint32_t state) |
| 822 | { |
| 823 | } |
| 824 | |
| 825 | static void |
| 826 | drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab, |
| 827 | uint32_t serial, uint32_t mods_depressed, |
| 828 | uint32_t mods_latched, |
| 829 | uint32_t mods_locked, uint32_t group) |
| 830 | { |
| 831 | struct weston_keyboard *keyboard = grab->keyboard; |
| 832 | struct weston_drag *drag = |
| 833 | container_of(grab, struct weston_drag, keyboard_grab); |
| 834 | uint32_t compositor_action; |
| 835 | |
| 836 | if (mods_depressed & (1 << keyboard->xkb_info->shift_mod)) |
| 837 | compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE; |
| 838 | else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod)) |
| 839 | compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; |
| 840 | else |
| 841 | compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; |
| 842 | |
| 843 | drag->data_source->compositor_action = compositor_action; |
| 844 | |
| 845 | if (drag->data_source->offer) |
| 846 | data_offer_update_action(drag->data_source->offer); |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab) |
| 851 | { |
| 852 | struct weston_drag *drag = |
| 853 | container_of(grab, struct weston_drag, keyboard_grab); |
| 854 | struct weston_pointer *pointer = grab->keyboard->seat->pointer_state; |
| 855 | struct weston_touch *touch = grab->keyboard->seat->touch_state; |
| 856 | |
| 857 | if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) { |
| 858 | struct weston_touch_drag *touch_drag = |
| 859 | (struct weston_touch_drag *) drag; |
| 860 | drag_grab_touch_cancel(&touch_drag->grab); |
| 861 | } else if (touch && touch->grab->interface == &touch_drag_grab_interface) { |
| 862 | struct weston_pointer_drag *pointer_drag = |
| 863 | (struct weston_pointer_drag *) drag; |
| 864 | drag_grab_cancel(&pointer_drag->grab); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = { |
| 869 | drag_grab_keyboard_key, |
| 870 | drag_grab_keyboard_modifiers, |
| 871 | drag_grab_keyboard_cancel |
| 872 | }; |
| 873 | |
| 874 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 875 | destroy_pointer_data_device_source(struct wl_listener *listener, void *data) |
| 876 | { |
| 877 | struct weston_pointer_drag *drag = container_of(listener, |
| 878 | struct weston_pointer_drag, base.data_source_listener); |
| 879 | |
| 880 | data_device_end_pointer_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static void |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 884 | handle_drag_icon_destroy(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 885 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 886 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 887 | icon_destroy_listener); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 888 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 889 | drag->icon = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 890 | } |
| 891 | |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 892 | WL_EXPORT int |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 893 | weston_pointer_start_drag(struct weston_pointer *pointer, |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 894 | struct weston_data_source *source, |
| 895 | struct weston_surface *icon, |
| 896 | struct wl_client *client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 897 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 898 | struct weston_pointer_drag *drag; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 899 | struct weston_keyboard *keyboard = |
| 900 | weston_seat_get_keyboard(pointer->seat); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 901 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 902 | drag = zalloc(sizeof *drag); |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 903 | if (drag == NULL) |
| 904 | return -1; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 905 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 906 | drag->grab.interface = &pointer_drag_grab_interface; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 907 | drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 908 | drag->base.client = client; |
| 909 | drag->base.data_source = source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 910 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 911 | if (icon) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 912 | drag->base.icon = weston_view_create(icon); |
| 913 | if (drag->base.icon == NULL) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 914 | free(drag); |
| 915 | return -1; |
| 916 | } |
| 917 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 918 | drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 919 | wl_signal_add(&icon->destroy_signal, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 920 | &drag->base.icon_destroy_listener); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 921 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 922 | icon->committed = pointer_drag_surface_committed; |
| 923 | icon->committed_private = drag; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 924 | weston_surface_set_label_func(icon, |
| 925 | pointer_drag_surface_get_label); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 926 | } else { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 927 | drag->base.icon = NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if (source) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 931 | drag->base.data_source_listener.notify = destroy_pointer_data_device_source; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 932 | wl_signal_add(&source->destroy_signal, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 933 | &drag->base.data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 936 | weston_pointer_clear_focus(pointer); |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 937 | weston_keyboard_set_focus(keyboard, NULL); |
| 938 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 939 | weston_pointer_start_grab(pointer, &drag->grab); |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 940 | weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static void |
| 946 | destroy_touch_data_device_source(struct wl_listener *listener, void *data) |
| 947 | { |
| 948 | struct weston_touch_drag *drag = container_of(listener, |
| 949 | struct weston_touch_drag, base.data_source_listener); |
| 950 | |
| 951 | data_device_end_touch_drag_grab(drag); |
| 952 | } |
| 953 | |
| 954 | WL_EXPORT int |
| 955 | weston_touch_start_drag(struct weston_touch *touch, |
| 956 | struct weston_data_source *source, |
| 957 | struct weston_surface *icon, |
| 958 | struct wl_client *client) |
| 959 | { |
| 960 | struct weston_touch_drag *drag; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 961 | struct weston_keyboard *keyboard = |
| 962 | weston_seat_get_keyboard(touch->seat); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 963 | |
| 964 | drag = zalloc(sizeof *drag); |
| 965 | if (drag == NULL) |
| 966 | return -1; |
| 967 | |
| 968 | drag->grab.interface = &touch_drag_grab_interface; |
| 969 | drag->base.client = client; |
| 970 | drag->base.data_source = source; |
| 971 | |
| 972 | if (icon) { |
| 973 | drag->base.icon = weston_view_create(icon); |
| 974 | if (drag->base.icon == NULL) { |
| 975 | free(drag); |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy; |
| 980 | wl_signal_add(&icon->destroy_signal, |
| 981 | &drag->base.icon_destroy_listener); |
| 982 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 983 | icon->committed = touch_drag_surface_committed; |
| 984 | icon->committed_private = drag; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 985 | weston_surface_set_label_func(icon, |
| 986 | touch_drag_surface_get_label); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 987 | } else { |
| 988 | drag->base.icon = NULL; |
| 989 | } |
| 990 | |
| 991 | if (source) { |
| 992 | drag->base.data_source_listener.notify = destroy_touch_data_device_source; |
| 993 | wl_signal_add(&source->destroy_signal, |
| 994 | &drag->base.data_source_listener); |
| 995 | } |
| 996 | |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 997 | weston_keyboard_set_focus(keyboard, NULL); |
| 998 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 999 | weston_touch_start_grab(touch, &drag->grab); |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 1000 | weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 1001 | |
| 1002 | drag_grab_touch_focus(drag); |
Kristian Høgsberg | 0abad07 | 2013-09-11 09:42:26 -0700 | [diff] [blame] | 1003 | |
| 1004 | return 0; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static void |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1008 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 1009 | struct wl_resource *source_resource, |
| 1010 | struct wl_resource *origin_resource, |
| 1011 | struct wl_resource *icon_resource, uint32_t serial) |
| 1012 | { |
| 1013 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1014 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 1015 | struct weston_touch *touch = weston_seat_get_touch(seat); |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1016 | struct weston_surface *origin = wl_resource_get_user_data(origin_resource); |
Kristian Høgsberg | 1702d4c | 2013-09-11 09:45:03 -0700 | [diff] [blame] | 1017 | struct weston_data_source *source = NULL; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1018 | struct weston_surface *icon = NULL; |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1019 | int is_pointer_grab, is_touch_grab; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 1020 | int32_t ret = 0; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1021 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1022 | is_pointer_grab = pointer && |
| 1023 | pointer->button_count == 1 && |
| 1024 | pointer->grab_serial == serial && |
| 1025 | pointer->focus && |
| 1026 | pointer->focus->surface == origin; |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1027 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1028 | is_touch_grab = touch && |
| 1029 | touch->num_tp == 1 && |
| 1030 | touch->grab_serial == serial && |
| 1031 | touch->focus && |
| 1032 | touch->focus->surface == origin; |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (!is_pointer_grab && !is_touch_grab) |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1035 | return; |
| 1036 | |
| 1037 | /* FIXME: Check that the data source type array isn't empty. */ |
| 1038 | |
| 1039 | if (source_resource) |
| 1040 | source = wl_resource_get_user_data(source_resource); |
| 1041 | if (icon_resource) |
| 1042 | icon = wl_resource_get_user_data(icon_resource); |
Pekka Paalanen | 50b6747 | 2014-10-01 15:02:41 +0300 | [diff] [blame] | 1043 | |
| 1044 | if (icon) { |
| 1045 | if (weston_surface_set_role(icon, "wl_data_device-icon", |
| 1046 | resource, |
| 1047 | WL_DATA_DEVICE_ERROR_ROLE) < 0) |
| 1048 | return; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1051 | if (is_pointer_grab) |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1052 | ret = weston_pointer_start_drag(pointer, source, icon, client); |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 1053 | else if (is_touch_grab) |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1054 | ret = weston_touch_start_drag(touch, source, icon, client); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 1055 | |
| 1056 | if (ret < 0) |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1057 | wl_resource_post_no_memory(resource); |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 1058 | else |
| 1059 | source->seat = seat; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1063 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 1064 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1065 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 1066 | selection_data_source_listener); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1067 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1068 | struct wl_resource *data_device; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1069 | struct weston_surface *focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1070 | |
| 1071 | seat->selection_data_source = NULL; |
| 1072 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1073 | if (keyboard) |
| 1074 | focus = keyboard->focus; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1075 | if (focus && focus->resource) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1076 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1077 | wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1078 | if (data_device) |
| 1079 | wl_data_device_send_selection(data_device, NULL); |
| 1080 | } |
| 1081 | |
| 1082 | wl_signal_emit(&seat->selection_signal, seat); |
| 1083 | } |
| 1084 | |
Giulio Camuffo | dddf9e6 | 2015-05-01 12:59:35 +0300 | [diff] [blame] | 1085 | /** \brief Send the selection to the specified client |
| 1086 | * |
| 1087 | * This function creates a new wl_data_offer if there is a wl_data_source |
| 1088 | * currently set as the selection and sends it to the specified client, |
| 1089 | * followed by the wl_data_device.selection() event. |
| 1090 | * If there is no current selection the wl_data_device.selection() event |
| 1091 | * will carry a NULL wl_data_offer. |
| 1092 | * |
| 1093 | * If the client does not have a wl_data_device for the specified seat |
| 1094 | * nothing will be done. |
| 1095 | * |
| 1096 | * \param seat The seat owning the wl_data_device used to send the events. |
| 1097 | * \param client The client to which to send the selection. |
| 1098 | */ |
| 1099 | WL_EXPORT void |
| 1100 | weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client) |
| 1101 | { |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 1102 | struct weston_data_offer *offer; |
| 1103 | struct wl_resource *data_device; |
Giulio Camuffo | dddf9e6 | 2015-05-01 12:59:35 +0300 | [diff] [blame] | 1104 | |
Giulio Camuffo | d46bb01 | 2015-05-01 12:59:36 +0300 | [diff] [blame] | 1105 | wl_resource_for_each(data_device, &seat->drag_resource_list) { |
| 1106 | if (wl_resource_get_client(data_device) != client) |
| 1107 | continue; |
| 1108 | |
| 1109 | if (seat->selection_data_source) { |
| 1110 | offer = weston_data_source_send_offer(seat->selection_data_source, |
Jonas Ådahl | 8b6c9fc | 2016-03-15 15:23:50 +0800 | [diff] [blame] | 1111 | data_device); |
| 1112 | wl_data_device_send_selection(data_device, offer->resource); |
Giulio Camuffo | d46bb01 | 2015-05-01 12:59:36 +0300 | [diff] [blame] | 1113 | } else { |
| 1114 | wl_data_device_send_selection(data_device, NULL); |
| 1115 | } |
Giulio Camuffo | dddf9e6 | 2015-05-01 12:59:35 +0300 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1119 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1120 | weston_seat_set_selection(struct weston_seat *seat, |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1121 | struct weston_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1122 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1123 | struct weston_surface *focus = NULL; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1124 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1125 | |
| 1126 | if (seat->selection_data_source && |
| 1127 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 1128 | return; |
| 1129 | |
| 1130 | if (seat->selection_data_source) { |
| 1131 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 1132 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 1133 | seat->selection_data_source = NULL; |
| 1134 | } |
| 1135 | |
| 1136 | seat->selection_data_source = source; |
| 1137 | seat->selection_serial = serial; |
| 1138 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1139 | if (keyboard) |
| 1140 | focus = keyboard->focus; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1141 | if (focus && focus->resource) { |
Giulio Camuffo | dddf9e6 | 2015-05-01 12:59:35 +0300 | [diff] [blame] | 1142 | weston_seat_send_selection(seat, wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | wl_signal_emit(&seat->selection_signal, seat); |
| 1146 | |
| 1147 | if (source) { |
| 1148 | seat->selection_data_source_listener.notify = |
| 1149 | destroy_selection_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1150 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1151 | &seat->selection_data_source_listener); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | static void |
| 1156 | data_device_set_selection(struct wl_client *client, |
| 1157 | struct wl_resource *resource, |
| 1158 | struct wl_resource *source_resource, uint32_t serial) |
| 1159 | { |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 1160 | struct weston_data_source *source; |
| 1161 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1162 | if (!source_resource) |
| 1163 | return; |
| 1164 | |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 1165 | source = wl_resource_get_user_data(source_resource); |
| 1166 | |
| 1167 | if (source->actions_set) { |
| 1168 | wl_resource_post_error(source_resource, |
| 1169 | WL_DATA_SOURCE_ERROR_INVALID_SOURCE, |
| 1170 | "cannot set drag-and-drop source as selection"); |
| 1171 | return; |
| 1172 | } |
| 1173 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1174 | /* FIXME: Store serial and check against incoming serial here. */ |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1175 | weston_seat_set_selection(wl_resource_get_user_data(resource), |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 1176 | source, serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1177 | } |
kabeer khan | 3a510d8 | 2014-10-20 11:47:15 +0530 | [diff] [blame] | 1178 | static void |
| 1179 | data_device_release(struct wl_client *client, struct wl_resource *resource) |
| 1180 | { |
| 1181 | wl_resource_destroy(resource); |
| 1182 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1183 | |
| 1184 | static const struct wl_data_device_interface data_device_interface = { |
| 1185 | data_device_start_drag, |
| 1186 | data_device_set_selection, |
kabeer khan | 3a510d8 | 2014-10-20 11:47:15 +0530 | [diff] [blame] | 1187 | data_device_release |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1188 | }; |
| 1189 | |
| 1190 | static void |
| 1191 | destroy_data_source(struct wl_resource *resource) |
| 1192 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1193 | struct weston_data_source *source = |
| 1194 | wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1195 | char **p; |
| 1196 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1197 | wl_signal_emit(&source->destroy_signal, source); |
| 1198 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1199 | wl_array_for_each(p, &source->mime_types) |
| 1200 | free(*p); |
| 1201 | |
| 1202 | wl_array_release(&source->mime_types); |
| 1203 | |
Kristian Høgsberg | 489b279 | 2013-06-25 11:26:31 -0400 | [diff] [blame] | 1204 | free(source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1208 | client_source_accept(struct weston_data_source *source, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1209 | uint32_t time, const char *mime_type) |
| 1210 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1211 | wl_data_source_send_target(source->resource, mime_type); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1215 | client_source_send(struct weston_data_source *source, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1216 | const char *mime_type, int32_t fd) |
| 1217 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1218 | wl_data_source_send_send(source->resource, mime_type, fd); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1219 | close(fd); |
| 1220 | } |
| 1221 | |
| 1222 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1223 | client_source_cancel(struct weston_data_source *source) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1224 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1225 | wl_data_source_send_cancelled(source->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void |
| 1229 | create_data_source(struct wl_client *client, |
| 1230 | struct wl_resource *resource, uint32_t id) |
| 1231 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 1232 | struct weston_data_source *source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1233 | |
| 1234 | source = malloc(sizeof *source); |
| 1235 | if (source == NULL) { |
| 1236 | wl_resource_post_no_memory(resource); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
cpaul@redhat.com | c9f8f8a | 2016-01-05 11:18:30 -0500 | [diff] [blame] | 1240 | source->resource = |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 1241 | wl_resource_create(client, &wl_data_source_interface, |
| 1242 | wl_resource_get_version(resource), id); |
cpaul@redhat.com | c9f8f8a | 2016-01-05 11:18:30 -0500 | [diff] [blame] | 1243 | if (source->resource == NULL) { |
| 1244 | free(source); |
| 1245 | wl_resource_post_no_memory(resource); |
| 1246 | return; |
| 1247 | } |
| 1248 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1249 | wl_signal_init(&source->destroy_signal); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1250 | source->accept = client_source_accept; |
| 1251 | source->send = client_source_send; |
| 1252 | source->cancel = client_source_cancel; |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 1253 | source->offer = NULL; |
| 1254 | source->accepted = false; |
| 1255 | source->seat = NULL; |
Carlos Garnacho | 9c93179 | 2016-01-18 23:52:12 +0100 | [diff] [blame] | 1256 | source->actions_set = false; |
| 1257 | source->dnd_actions = 0; |
| 1258 | source->current_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; |
Carlos Garnacho | b288988 | 2016-01-15 21:14:26 +0100 | [diff] [blame] | 1259 | source->compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1260 | |
| 1261 | wl_array_init(&source->mime_types); |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1262 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1263 | wl_resource_set_implementation(source->resource, &data_source_interface, |
| 1264 | source, destroy_data_source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | static void unbind_data_device(struct wl_resource *resource) |
| 1268 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 1269 | wl_list_remove(wl_resource_get_link(resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static void |
| 1273 | get_data_device(struct wl_client *client, |
| 1274 | struct wl_resource *manager_resource, |
| 1275 | uint32_t id, struct wl_resource *seat_resource) |
| 1276 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame] | 1277 | struct weston_seat *seat = wl_resource_get_user_data(seat_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1278 | struct wl_resource *resource; |
| 1279 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1280 | resource = wl_resource_create(client, |
kabeer khan | 3a510d8 | 2014-10-20 11:47:15 +0530 | [diff] [blame] | 1281 | &wl_data_device_interface, |
| 1282 | wl_resource_get_version(manager_resource), |
| 1283 | id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1284 | if (resource == NULL) { |
| 1285 | wl_resource_post_no_memory(manager_resource); |
| 1286 | return; |
| 1287 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1288 | |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1289 | wl_list_insert(&seat->drag_resource_list, |
| 1290 | wl_resource_get_link(resource)); |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1291 | wl_resource_set_implementation(resource, &data_device_interface, |
| 1292 | seat, unbind_data_device); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | static const struct wl_data_device_manager_interface manager_interface = { |
| 1296 | create_data_source, |
| 1297 | get_data_device |
| 1298 | }; |
| 1299 | |
| 1300 | static void |
| 1301 | bind_manager(struct wl_client *client, |
| 1302 | void *data, uint32_t version, uint32_t id) |
| 1303 | { |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1304 | struct wl_resource *resource; |
| 1305 | |
kabeer khan | 3a510d8 | 2014-10-20 11:47:15 +0530 | [diff] [blame] | 1306 | resource = wl_resource_create(client, |
| 1307 | &wl_data_device_manager_interface, |
| 1308 | version, id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1309 | if (resource == NULL) { |
| 1310 | wl_client_post_no_memory(client); |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | wl_resource_set_implementation(resource, &manager_interface, |
| 1315 | NULL, NULL); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1319 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1320 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1321 | struct weston_surface *focus; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1322 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1323 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1324 | if (!keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1325 | return; |
| 1326 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1327 | focus = keyboard->focus; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1328 | if (!focus || !focus->resource) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1329 | return; |
| 1330 | |
Giulio Camuffo | dddf9e6 | 2015-05-01 12:59:35 +0300 | [diff] [blame] | 1331 | weston_seat_send_selection(seat, wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | WL_EXPORT int |
| 1335 | wl_data_device_manager_init(struct wl_display *display) |
| 1336 | { |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 1337 | if (wl_global_create(display, |
Carlos Garnacho | 78d4bf9 | 2016-01-15 21:14:23 +0100 | [diff] [blame] | 1338 | &wl_data_device_manager_interface, 3, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 1339 | NULL, bind_manager) == NULL) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1340 | return -1; |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |