neuswc

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

data.c (10145B)


      1 /* swc: data.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.h"
     25 #include "util.h"
     26 
     27 #include <stdlib.h>
     28 #include <string.h>
     29 #include <unistd.h>
     30 #include <wayland-server.h>
     31 
     32 struct data {
     33 	struct wl_array mime_types;
     34 	struct wl_resource *source; /* NULL for a source of the compositor's own */
     35 	struct data_host host;
     36 	struct wl_list offers;
     37 
     38 	/* drag state: source actions, target actions, agreed action */
     39 	uint32_t source_actions, action;
     40 	bool accepted;
     41 	/* current offer, target actions and preference, key-chosen action */
     42 	struct wl_resource *drag_offer;
     43 	uint32_t offer_actions, preferred, chosen;
     44 };
     45 
     46 /* action both allow: target preference, then copy, move, ask */
     47 static uint32_t
     48 agree(uint32_t both, uint32_t preferred)
     49 {
     50 	static const uint32_t order[] = {
     51 		WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,
     52 		WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
     53 		WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK,
     54 	};
     55 
     56 	if (preferred & both) {
     57 		return preferred;
     58 	}
     59 	for (size_t i = 0; i < sizeof(order) / sizeof(order[0]); i++) {
     60 		if (order[i] & both) {
     61 			return order[i];
     62 		}
     63 	}
     64 	return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
     65 }
     66 
     67 static void
     68 offer_accept(struct wl_client *client, struct wl_resource *offer,
     69              uint32_t serial, const char *mime_type)
     70 {
     71 	struct data *data = wl_resource_get_user_data(offer);
     72 
     73 	/* Protect against expired data_offers being used. */
     74 	if (!data) {
     75 		return;
     76 	}
     77 
     78 	if (!data->source) {
     79 		return;
     80 	}
     81 	data->accepted = mime_type != NULL;
     82 	wl_data_source_send_target(data->source, mime_type);
     83 }
     84 
     85 static void
     86 offer_receive(struct wl_client *client, struct wl_resource *offer,
     87               const char *mime_type, int fd)
     88 {
     89 	struct data *data = wl_resource_get_user_data(offer);
     90 
     91 	/* Protect against expired data_offers being used. */
     92 	if (!data) {
     93 		return;
     94 	}
     95 
     96 	data_request(data, mime_type, fd);
     97 	close(fd);
     98 }
     99 
    100 /* drop finished: the source may release its data, delete it after a move */
    101 static void
    102 offer_finish(struct wl_client *client, struct wl_resource *offer)
    103 {
    104 	struct data *data = wl_resource_get_user_data(offer);
    105 
    106 	(void)client;
    107 	if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
    108 		wl_data_source_send_dnd_finished(data->source);
    109 	}
    110 }
    111 
    112 /* pick the action, a held key first, and tell both sides */
    113 static void
    114 settle(struct data *data, struct wl_resource *offer)
    115 {
    116 	uint32_t both = data->source_actions & data->offer_actions;
    117 
    118 	data->action = data->chosen & both ? data->chosen : agree(both, data->preferred);
    119 	if (wl_resource_get_version(offer) >= WL_DATA_OFFER_ACTION_SINCE_VERSION) {
    120 		wl_data_offer_send_action(offer, data->action);
    121 	}
    122 	if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
    123 		wl_data_source_send_action(data->source, data->action);
    124 	}
    125 }
    126 
    127 static void
    128 offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t dnd_actions, uint32_t preferred_action)
    129 {
    130 	struct data *data = wl_resource_get_user_data(offer);
    131 
    132 	(void)client;
    133 	if (!data || !data->source) {
    134 		return;
    135 	}
    136 	data->offer_actions = dnd_actions;
    137 	data->preferred = preferred_action;
    138 	settle(data, offer);
    139 }
    140 
    141 static const struct wl_data_offer_interface data_offer_impl = {
    142 	.accept = offer_accept,
    143 	.receive = offer_receive,
    144 	.destroy = destroy_resource,
    145 	.finish = offer_finish,
    146 	.set_actions = offer_set_actions,
    147 };
    148 
    149 static void
    150 source_offer(struct wl_client *client, struct wl_resource *source,
    151              const char *mime_type)
    152 {
    153 	struct data *data = wl_resource_get_user_data(source);
    154 	char *s, **dst;
    155 
    156 	s = strdup(mime_type);
    157 	if (!s) {
    158 		goto error0;
    159 	}
    160 	dst = wl_array_add(&data->mime_types, sizeof(*dst));
    161 	if (!dst) {
    162 		goto error1;
    163 	}
    164 	*dst = s;
    165 	return;
    166 
    167 error1:
    168 	free(s);
    169 error0:
    170 	wl_resource_post_no_memory(source);
    171 }
    172 
    173 static void
    174 source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions)
    175 {
    176 	struct data *data = wl_resource_get_user_data(resource);
    177 
    178 	(void)client;
    179 	data->source_actions = dnd_actions;
    180 }
    181 
    182 static const struct wl_data_source_interface data_source_impl = {
    183 	.offer = source_offer,
    184 	.destroy = destroy_resource,
    185 	.set_actions = source_set_actions,
    186 };
    187 
    188 /* after the source is gone each data_offer made from it has a pointer to a
    189  * free'd struct. the resources cannot be destroyed, a client that correctly
    190  * calls data_offer.destroy would crash it, so the user data of each offer is
    191  * cleared, and a client that still calls accept or receive does nothing */
    192 static void
    193 release(struct data *data)
    194 {
    195 	struct wl_resource *offer;
    196 	char **mime_type;
    197 
    198 	wl_array_for_each(mime_type, &data->mime_types) free(*mime_type);
    199 	wl_array_release(&data->mime_types);
    200 	wl_resource_for_each(offer, &data->offers)
    201 	{
    202 		wl_resource_set_user_data(offer, NULL);
    203 		wl_resource_set_destructor(offer, NULL);
    204 	}
    205 	free(data);
    206 }
    207 
    208 static void
    209 data_destroy(struct wl_resource *source)
    210 {
    211 	release(wl_resource_get_user_data(source));
    212 }
    213 
    214 struct wl_resource *
    215 data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
    216 {
    217 	struct data *data;
    218 
    219 	data = malloc(sizeof(*data));
    220 	if (!data) {
    221 		goto error0;
    222 	}
    223 	wl_array_init(&data->mime_types);
    224 	wl_list_init(&data->offers);
    225 	data->source_actions = data->action = 0;
    226 	memset(&data->host, 0, sizeof(data->host));
    227 	data->accepted = false;
    228 	data->drag_offer = NULL;
    229 	data->offer_actions = data->preferred = data->chosen = 0;
    230 
    231 	data->source =
    232 	    wl_resource_create(client, &wl_data_source_interface, version, id);
    233 	if (!data->source) {
    234 		goto error1;
    235 	}
    236 	wl_resource_set_implementation(data->source, &data_source_impl, data,
    237 	                               &data_destroy);
    238 
    239 	return data->source;
    240 
    241 error1:
    242 	free(data);
    243 error0:
    244 	return NULL;
    245 }
    246 
    247 struct wl_resource *
    248 data_offer_for(struct wl_client *client, struct data *data, uint32_t version)
    249 {
    250 	struct wl_resource *offer;
    251 
    252 	offer = wl_resource_create(client, &wl_data_offer_interface, version, 0);
    253 	if (!offer) {
    254 		return NULL;
    255 	}
    256 	wl_resource_set_implementation(offer, &data_offer_impl, data,
    257 	                               &remove_resource);
    258 	wl_list_insert(&data->offers, wl_resource_get_link(offer));
    259 
    260 	return offer;
    261 }
    262 
    263 struct wl_resource *
    264 data_offer_new(struct wl_client *client, struct wl_resource *source,
    265                uint32_t version)
    266 {
    267 	return data_offer_for(client, wl_resource_get_user_data(source), version);
    268 }
    269 
    270 void
    271 data_send_types(struct data *data, struct wl_resource *offer)
    272 {
    273 	char **mime_type;
    274 
    275 	wl_array_for_each(mime_type, &data->mime_types)
    276 	    wl_data_offer_send_offer(offer, *mime_type);
    277 }
    278 
    279 void
    280 data_send_mime_types(struct wl_resource *source, struct wl_resource *offer)
    281 {
    282 	data_send_types(wl_resource_get_user_data(source), offer);
    283 }
    284 
    285 struct data *
    286 data_of(struct wl_resource *source)
    287 {
    288 	return wl_resource_get_user_data(source);
    289 }
    290 
    291 unsigned
    292 data_type_count(const struct data *data)
    293 {
    294 	return data->mime_types.size / sizeof(char *);
    295 }
    296 
    297 const char *
    298 data_type_at(const struct data *data, unsigned index)
    299 {
    300 	return ((char **)data->mime_types.data)[index];
    301 }
    302 
    303 void
    304 data_request(struct data *data, const char *mime_type, int fd)
    305 {
    306 	if (data->source) {
    307 		wl_data_source_send_send(data->source, mime_type, fd);
    308 	} else if (data->host.send) {
    309 		data->host.send(data->host.data, mime_type, fd);
    310 	}
    311 }
    312 
    313 void
    314 data_cancel(struct data *data)
    315 {
    316 	if (data->source) {
    317 		wl_data_source_send_cancelled(data->source);
    318 	} else if (data->host.cancelled) {
    319 		data->host.cancelled(data->host.data);
    320 	}
    321 }
    322 
    323 struct data *
    324 data_host_new(const char *const *types, unsigned count,
    325               const struct data_host *host, void *host_data)
    326 {
    327 	struct data *data = calloc(1, sizeof(*data));
    328 	char **dst;
    329 	unsigned i;
    330 
    331 	if (!data) {
    332 		return NULL;
    333 	}
    334 	wl_array_init(&data->mime_types);
    335 	wl_list_init(&data->offers);
    336 	data->host = *host;
    337 	data->host.data = host_data;
    338 	for (i = 0; i < count; i++) {
    339 		dst = wl_array_add(&data->mime_types, sizeof(*dst));
    340 		if (!dst) {
    341 			release(data);
    342 			return NULL;
    343 		}
    344 		if (!(*dst = strdup(types[i]))) {
    345 			data->mime_types.size -= sizeof(*dst);
    346 			release(data);
    347 			return NULL;
    348 		}
    349 	}
    350 	return data;
    351 }
    352 
    353 void
    354 data_host_free(struct data *data)
    355 {
    356 	release(data);
    357 }
    358 
    359 void
    360 data_drag_enter(struct wl_resource *source, struct wl_resource *offer)
    361 {
    362 	struct data *data = wl_resource_get_user_data(source);
    363 
    364 	data->accepted = false;
    365 	data->action = data->offer_actions = data->preferred = 0;
    366 	data->drag_offer = offer;
    367 	if (wl_resource_get_version(offer) >= WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
    368 		wl_data_offer_send_source_actions(offer, data->source_actions);
    369 	}
    370 }
    371 
    372 bool
    373 data_drag_droppable(struct wl_resource *source)
    374 {
    375 	struct data *data = wl_resource_get_user_data(source);
    376 
    377 	if (!data->accepted) {
    378 		return false;
    379 	}
    380 	/* no actions before version 3, a drop copies */
    381 	return wl_resource_get_version(source) < WL_DATA_SOURCE_ACTION_SINCE_VERSION || data->action;
    382 }
    383 
    384 void
    385 data_drag_choose(struct wl_resource *source, uint32_t action)
    386 {
    387 	struct data *data = wl_resource_get_user_data(source);
    388 
    389 	if (data->chosen == action) {
    390 		return;
    391 	}
    392 	data->chosen = action;
    393 	if (data->drag_offer && data->offer_actions) {
    394 		settle(data, data->drag_offer);
    395 	}
    396 }