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