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