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