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