neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

data_device.c (13741B)


      1 /* swc: data_device.c
      2  *
      3  * Copyright (c) 2013-2020 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #include "data_device.h"
     25 #include "compositor.h"
     26 #include "data.h"
     27 #include "event.h"
     28 #include "keyboard.h"
     29 #include "internal.h"
     30 #include "pointer.h"
     31 #include "seat.h"
     32 #include "surface.h"
     33 #include "util.h"
     34 #include "view.h"
     35 
     36 #include <string.h>
     37 
     38 /* drag and drop, one drag at a time, holding the pointer until the buttons are released */
     39 static struct drag {
     40 	bool active;
     41 	struct data_device *device;
     42 	struct wl_client *client;       /* starter */
     43 	struct wl_resource *source;     /* NULL for a drag inside one client */
     44 	struct compositor_view *icon;   /* the icon */
     45 	struct compositor_view *focus;  /* the view it is over */
     46 	struct wl_resource *target;     /* its client's data device */
     47 	struct pointer_handler handler;
     48 	struct wl_listener source_destroy, focus_destroy, icon_destroy;
     49 } drag;
     50 
     51 static bool drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state);
     52 
     53 static void
     54 drag_unfocus(void)
     55 {
     56 	if (drag.focus) {
     57 		wl_list_remove(&drag.focus_destroy.link);
     58 	}
     59 	drag.focus = NULL;
     60 	drag.target = NULL;
     61 }
     62 
     63 static void
     64 drag_leave(void)
     65 {
     66 	if (drag.target) {
     67 		wl_data_device_send_leave(drag.target);
     68 	}
     69 	drag_unfocus();
     70 }
     71 
     72 static wl_fixed_t
     73 local(int32_t at, int32_t origin, int32_t offset)
     74 {
     75 	return wl_fixed_from_int(at - origin + offset);
     76 }
     77 
     78 static void
     79 drag_enter(struct compositor_view *view, int32_t x, int32_t y)
     80 {
     81 	struct wl_client *client;
     82 	struct wl_resource *device, *offer = NULL;
     83 
     84 	if (view == drag.focus) {
     85 		return;
     86 	}
     87 	drag_leave();
     88 	if (!view || !view->surface) {
     89 		return;
     90 	}
     91 	client = wl_resource_get_client(view->surface->resource);
     92 	/* a drag with no source stays inside the client that made it */
     93 	if (!drag.source && client != drag.client) {
     94 		return;
     95 	}
     96 	device = wl_resource_find_for_client(&drag.device->resources, client);
     97 	if (!device) {
     98 		return;
     99 	}
    100 	if (drag.source && (offer = data_offer_new(client, drag.source, wl_resource_get_version(device)))) {
    101 		wl_data_device_send_data_offer(device, offer);
    102 		data_send_mime_types(drag.source, offer);
    103 		data_drag_enter(drag.source, offer);
    104 	}
    105 	drag.focus = view;
    106 	drag.target = device;
    107 	wl_signal_add(&view->destroy_signal, &drag.focus_destroy);
    108 	wl_data_device_send_enter(device, wl_display_next_serial(swc.display), view->surface->resource,
    109 	                          local(x, view->base.geometry.x, view->buffer_offset_x),
    110 	                          local(y, view->base.geometry.y, view->buffer_offset_y), offer);
    111 }
    112 
    113 static void
    114 drag_end(void)
    115 {
    116 	struct pointer *pointer = swc.seat->pointer;
    117 
    118 	drag_leave();
    119 	wl_list_remove(&drag.handler.link);
    120 	if (drag.source) {
    121 		wl_list_remove(&drag.source_destroy.link);
    122 	}
    123 	if (drag.icon) {
    124 		wl_list_remove(&drag.icon_destroy.link);
    125 		compositor_view_destroy(drag.icon);
    126 	}
    127 	memset(&drag, 0, sizeof(drag));
    128 	/* held buttons are swallowed as they go up */
    129 	drag.handler.button = drag_swallow;
    130 	/* the pointer goes back to what is under it */
    131 	pointer_set_focus(pointer, compositor_view_at(wl_fixed_to_int(pointer->x), wl_fixed_to_int(pointer->y)));
    132 }
    133 
    134 static void
    135 drag_drop(void)
    136 {
    137 	bool take = drag.target && (!drag.source || data_drag_droppable(drag.source));
    138 
    139 	if (!take) {
    140 		if (drag.source) {
    141 			wl_data_source_send_cancelled(drag.source);
    142 		}
    143 		drag_end();
    144 		return;
    145 	}
    146 	wl_data_device_send_drop(drag.target);
    147 	if (drag.source && wl_resource_get_version(drag.source) >= WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) {
    148 		wl_data_source_send_dnd_drop_performed(drag.source);
    149 	}
    150 	/* the target keeps its offer, no leave follows */
    151 	drag_unfocus();
    152 	drag_end();
    153 }
    154 
    155 static bool
    156 drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state)
    157 {
    158 	(void)handler;
    159 	(void)time;
    160 	(void)button;
    161 	(void)state;
    162 	return true;
    163 }
    164 
    165 /* modifiers that ask for each action, all held. none means never */
    166 static uint32_t action_modifiers[] = {
    167     [0] = SWC_MOD_CTRL,  /* copy */
    168     [1] = SWC_MOD_SHIFT, /* move */
    169     [2] = 0,             /* ask */
    170 };
    171 
    172 static const uint32_t actions[] = {
    173     WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,
    174     WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
    175     WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK,
    176 };
    177 
    178 /* action the held modifiers ask for, the one with most modifiers wins */
    179 static uint32_t
    180 drag_action_for(uint32_t mods)
    181 {
    182 	uint32_t best = 0;
    183 	int bits, most = 0;
    184 	size_t i;
    185 
    186 	for (i = 0; i < sizeof actions / sizeof actions[0]; i++) {
    187 		if (!action_modifiers[i] || (mods & action_modifiers[i]) != action_modifiers[i]) {
    188 			continue;
    189 		}
    190 		bits = __builtin_popcount(action_modifiers[i]);
    191 		if (bits > most) {
    192 			most = bits;
    193 			best = actions[i];
    194 		}
    195 	}
    196 	return best;
    197 }
    198 
    199 EXPORT void
    200 swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers)
    201 {
    202 	size_t i;
    203 
    204 	for (i = 0; i < sizeof actions / sizeof actions[0]; i++) {
    205 		if (actions[i] == (uint32_t)action) {
    206 			action_modifiers[i] = modifiers & (SWC_MOD_CTRL | SWC_MOD_ALT | SWC_MOD_LOGO | SWC_MOD_SHIFT);
    207 		}
    208 	}
    209 }
    210 
    211 static bool
    212 drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fixed_t fy)
    213 {
    214 	int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy);
    215 
    216 	(void)handler;
    217 	if (drag.icon) {
    218 		view_move(&drag.icon->base, x, y);
    219 	}
    220 	if (drag.source) {
    221 		uint32_t mods = swc.seat->keyboard ? swc.seat->keyboard->modifiers : 0;
    222 
    223 		data_drag_choose(drag.source, drag_action_for(mods));
    224 	}
    225 	drag_enter(compositor_view_at(x, y), x, y);
    226 	if (drag.target) {
    227 		wl_data_device_send_motion(drag.target, time,
    228 		                           local(x, drag.focus->base.geometry.x, drag.focus->buffer_offset_x),
    229 		                           local(y, drag.focus->base.geometry.y, drag.focus->buffer_offset_y));
    230 	}
    231 	return true;
    232 }
    233 
    234 static bool
    235 drag_button(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state)
    236 {
    237 	(void)handler;
    238 	(void)time;
    239 	(void)button;
    240 	/* the drop comes when the last button held is let go */
    241 	if (state == WL_POINTER_BUTTON_STATE_RELEASED && swc.seat->pointer->buttons.size <= sizeof(struct button)) {
    242 		drag_drop();
    243 	}
    244 	return true;
    245 }
    246 
    247 static void
    248 handle_drag_source_destroy(struct wl_listener *listener, void *data)
    249 {
    250 	(void)listener;
    251 	(void)data;
    252 	drag.source = NULL;
    253 	drag_end();
    254 }
    255 
    256 static void
    257 handle_drag_focus_destroy(struct wl_listener *listener, void *data)
    258 {
    259 	(void)data;
    260 	wl_list_remove(&listener->link);
    261 	drag.focus = NULL;
    262 	drag.target = NULL;
    263 }
    264 
    265 static void
    266 handle_drag_icon_destroy(struct wl_listener *listener, void *data)
    267 {
    268 	(void)listener;
    269 	(void)data;
    270 	compositor_view_destroy(drag.icon);
    271 	drag.icon = NULL;
    272 }
    273 
    274 static void
    275 start_drag(struct wl_client *client, struct wl_resource *resource,
    276            struct wl_resource *source_resource,
    277            struct wl_resource *origin_resource,
    278            struct wl_resource *icon_resource, uint32_t serial)
    279 {
    280 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    281 	int32_t x, y;
    282 	struct button *button;
    283 
    284 	(void)origin_resource;
    285 	(void)serial;
    286 	/* a drag needs a button held, and one at a time */
    287 	if (drag.active || !pointer || !pointer->buttons.size) {
    288 		if (source_resource) {
    289 			wl_data_source_send_cancelled(source_resource);
    290 		}
    291 		return;
    292 	}
    293 	x = wl_fixed_to_int(pointer->x);
    294 	y = wl_fixed_to_int(pointer->y);
    295 	drag.active = true;
    296 	drag.device = wl_resource_get_user_data(resource);
    297 	drag.client = client;
    298 	drag.source = source_resource;
    299 	drag.focus_destroy.notify = handle_drag_focus_destroy;
    300 	if (drag.source) {
    301 		drag.source_destroy.notify = handle_drag_source_destroy;
    302 		wl_resource_add_destroy_listener(drag.source, &drag.source_destroy);
    303 	}
    304 	if (icon_resource && (drag.icon = compositor_create_view(wl_resource_get_user_data(icon_resource)))) {
    305 		drag.icon->no_input = true;
    306 		drag.icon->always_top = true;
    307 		compositor_view_set_z(drag.icon, STACK_LAYER_OVERLAY, true);
    308 		view_move(&drag.icon->base, x, y);
    309 		compositor_view_show(drag.icon);
    310 		drag.icon_destroy.notify = handle_drag_icon_destroy;
    311 		wl_resource_add_destroy_listener(icon_resource, &drag.icon_destroy);
    312 	}
    313 	/* the pointer is the drag's: no client sees it until the drop */
    314 	pointer_set_focus(pointer, NULL);
    315 	drag.handler.motion = drag_motion;
    316 	drag.handler.button = drag_button;
    317 	wl_list_insert(&pointer->handlers, &drag.handler.link);
    318 	wl_array_for_each(button, &pointer->buttons) button->handler = &drag.handler;
    319 	drag_enter(compositor_view_at(x, y), x, y);
    320 }
    321 
    322 static void
    323 set_selection(struct wl_client *client, struct wl_resource *resource,
    324               struct wl_resource *data_source, uint32_t serial)
    325 {
    326 	struct data_device *data_device = wl_resource_get_user_data(resource);
    327 
    328 	/* Check if this data source is already the current selection. */
    329 	if (data_source == data_device->selection) {
    330 		return;
    331 	}
    332 
    333 	if (data_device->selection) {
    334 		wl_data_source_send_cancelled(data_device->selection);
    335 		wl_list_remove(&data_device->selection_destroy_listener.link);
    336 	}
    337 	if (data_device->host) {
    338 		data_cancel(data_device->host);
    339 		data_host_free(data_device->host);
    340 		data_device->host = NULL;
    341 	}
    342 
    343 	data_device->selection = data_source;
    344 
    345 	if (data_source) {
    346 		wl_resource_add_destroy_listener(
    347 		    data_source, &data_device->selection_destroy_listener);
    348 	}
    349 
    350 	send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED,
    351 	           NULL);
    352 }
    353 
    354 static const struct wl_data_device_interface data_device_impl = {
    355     .start_drag = start_drag,
    356     .set_selection = set_selection,
    357     .release = destroy_resource,
    358 };
    359 
    360 static void
    361 handle_selection_destroy(struct wl_listener *listener, void *data)
    362 {
    363 	struct data_device *data_device =
    364 	    wl_container_of(listener, data_device, selection_destroy_listener);
    365 
    366 	data_device->selection = NULL;
    367 	send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED,
    368 	           NULL);
    369 }
    370 
    371 struct data_device *
    372 data_device_create(void)
    373 {
    374 	struct data_device *data_device;
    375 
    376 	data_device = malloc(sizeof(*data_device));
    377 	if (!data_device) {
    378 		return NULL;
    379 	}
    380 	data_device->selection = NULL;
    381 	data_device->host = NULL;
    382 	data_device->selection_destroy_listener.notify = &handle_selection_destroy;
    383 	wl_signal_init(&data_device->event_signal);
    384 	wl_list_init(&data_device->resources);
    385 
    386 	return data_device;
    387 }
    388 
    389 void
    390 data_device_destroy(struct data_device *data_device)
    391 {
    392 	struct wl_resource *resource, *tmp;
    393 
    394 	wl_list_for_each_safe(resource, tmp, &data_device->resources, link)
    395 	    wl_resource_destroy(resource);
    396 	if (data_device->host) {
    397 		data_host_free(data_device->host);
    398 	}
    399 	free(data_device);
    400 }
    401 
    402 struct wl_resource *
    403 data_device_bind(struct data_device *data_device, struct wl_client *client,
    404                  uint32_t version, uint32_t id)
    405 {
    406 	struct wl_resource *resource;
    407 
    408 	resource =
    409 	    wl_resource_create(client, &wl_data_device_interface, version, id);
    410 	if (!resource) {
    411 		return NULL;
    412 	}
    413 	wl_resource_set_implementation(resource, &data_device_impl, data_device,
    414 	                               &remove_resource);
    415 	wl_list_insert(&data_device->resources, &resource->link);
    416 
    417 	return resource;
    418 }
    419 
    420 struct data *
    421 data_device_client_selection(struct data_device *data_device)
    422 {
    423 	return data_device->selection ? data_of(data_device->selection) : NULL;
    424 }
    425 
    426 void
    427 data_device_set_host(struct data_device *data_device, struct data *host)
    428 {
    429 	if (data_device->selection) {
    430 		wl_data_source_send_cancelled(data_device->selection);
    431 		wl_list_remove(&data_device->selection_destroy_listener.link);
    432 		data_device->selection = NULL;
    433 	}
    434 	if (data_device->host) {
    435 		data_cancel(data_device->host);
    436 		data_host_free(data_device->host);
    437 	}
    438 	data_device->host = host;
    439 	send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED,
    440 	           host);
    441 }
    442 
    443 static struct wl_resource *
    444 new_offer(struct wl_resource *resource, struct wl_client *client,
    445           struct data *source)
    446 {
    447 	struct wl_resource *offer;
    448 
    449 	offer = data_offer_for(client, source, wl_resource_get_version(resource));
    450 	if (!offer) {
    451 		return NULL;
    452 	}
    453 	wl_data_device_send_data_offer(resource, offer);
    454 	data_send_types(source, offer);
    455 
    456 	return offer;
    457 }
    458 
    459 void
    460 data_device_offer_selection(struct data_device *data_device,
    461                             struct wl_client *client)
    462 {
    463 	struct wl_resource *resource;
    464 	struct wl_resource *offer = NULL;
    465 
    466 	/* Look for the client's data_device resource. */
    467 	resource = wl_resource_find_for_client(&data_device->resources, client);
    468 
    469 	/* If the client does not have a data device, there is nothing to do. */
    470 	if (!resource) {
    471 		return;
    472 	}
    473 
    474 	/* If we have a selection, create a new offer for the client. */
    475 	if (data_device->host) {
    476 		offer = new_offer(resource, client, data_device->host);
    477 	} else if (data_device->selection) {
    478 		offer = new_offer(resource, client, data_of(data_device->selection));
    479 	}
    480 
    481 	wl_data_device_send_selection(resource, offer);
    482 }