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