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