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