neuswc

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

input.patch (55363B)


      1 Subject: input, cursors, drag and drop, a keymap from a file
      2 
      3 diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c
      4 new file mode 100644
      5 index 0000000..28e5a78
      6 --- /dev/null
      7 +++ b/libswc/cursor_shape.c
      8 @@ -0,0 +1,146 @@
      9 +/* cursor-shape-v1 */
     10 +
     11 +#include "cursor_shape.h"
     12 +#include "internal.h"
     13 +#include "pointer.h"
     14 +#include "seat.h"
     15 +#include "util.h"
     16 +
     17 +#include <wayland-server.h>
     18 +#include "cursor-shape-v1-server-protocol.h"
     19 +
     20 +/* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */
     21 +static const char *const names[] = {
     22 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default",
     23 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu",
     24 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help",
     25 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer",
     26 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress",
     27 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait",
     28 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell",
     29 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair",
     30 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text",
     31 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text",
     32 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias",
     33 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy",
     34 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move",
     35 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop",
     36 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed",
     37 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab",
     38 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing",
     39 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize",
     40 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize",
     41 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize",
     42 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize",
     43 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize",
     44 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize",
     45 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize",
     46 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize",
     47 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize",
     48 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize",
     49 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize",
     50 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize",
     51 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize",
     52 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize",
     53 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll",
     54 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in",
     55 +    [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out",
     56 +};
     57 +
     58 +#define NAMES (sizeof names / sizeof names[0])
     59 +
     60 +static void
     61 +pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource,
     62 +                          uint32_t serial, uint32_t shape)
     63 +{
     64 +	struct pointer *pointer = wl_resource_get_user_data(resource);
     65 +
     66 +	(void)serial;
     67 +	if (shape < 1 || shape >= NAMES || !names[shape]) {
     68 +		wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE,
     69 +		                       "no such shape: %u", shape);
     70 +		return;
     71 +	}
     72 +	/* only what has the pointer says what it looks like */
     73 +	if (!pointer || client != pointer->focus.client) {
     74 +		return;
     75 +	}
     76 +	pointer_set_shape(pointer, names[shape]);
     77 +}
     78 +
     79 +static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = {
     80 +    .destroy = destroy_resource,
     81 +    .set_shape = pointer_set_shape_request,
     82 +};
     83 +
     84 +/* a tablet tool has no pointer here: it is answered and does nothing */
     85 +static void
     86 +tool_set_shape_request(struct wl_client *client, struct wl_resource *resource,
     87 +                       uint32_t serial, uint32_t shape)
     88 +{
     89 +	(void)client;
     90 +	(void)serial;
     91 +	(void)shape;
     92 +	(void)resource;
     93 +}
     94 +
     95 +static const struct wp_cursor_shape_device_v1_interface tool_device_impl = {
     96 +    .destroy = destroy_resource,
     97 +    .set_shape = tool_set_shape_request,
     98 +};
     99 +
    100 +static void
    101 +get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id,
    102 +            struct wl_resource *pointer_resource)
    103 +{
    104 +	struct wl_resource *device = wl_resource_create(
    105 +	    client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id);
    106 +
    107 +	if (!device) {
    108 +		wl_resource_post_no_memory(resource);
    109 +		return;
    110 +	}
    111 +	wl_resource_set_implementation(device, &pointer_device_impl,
    112 +	                               wl_resource_get_user_data(pointer_resource), NULL);
    113 +}
    114 +
    115 +static void
    116 +get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id,
    117 +                   struct wl_resource *tool)
    118 +{
    119 +	struct wl_resource *device = wl_resource_create(
    120 +	    client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id);
    121 +
    122 +	(void)tool;
    123 +	if (!device) {
    124 +		wl_resource_post_no_memory(resource);
    125 +		return;
    126 +	}
    127 +	wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL);
    128 +}
    129 +
    130 +static const struct wp_cursor_shape_manager_v1_interface manager_impl = {
    131 +    .destroy = destroy_resource,
    132 +    .get_pointer = get_pointer,
    133 +    .get_tablet_tool_v2 = get_tablet_tool_v2,
    134 +};
    135 +
    136 +static void
    137 +bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id)
    138 +{
    139 +	struct wl_resource *resource;
    140 +
    141 +	(void)data;
    142 +	resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id);
    143 +	if (!resource) {
    144 +		wl_client_post_no_memory(client);
    145 +		return;
    146 +	}
    147 +	wl_resource_set_implementation(resource, &manager_impl, NULL, NULL);
    148 +}
    149 +
    150 +struct wl_global *
    151 +cursor_shape_manager_create(struct wl_display *display)
    152 +{
    153 +	return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager);
    154 +}
    155 diff --git a/libswc/cursor_shape.h b/libswc/cursor_shape.h
    156 new file mode 100644
    157 index 0000000..be8ebb4
    158 --- /dev/null
    159 +++ b/libswc/cursor_shape.h
    160 @@ -0,0 +1,11 @@
    161 +/* cursor-shape-v1: a client asks for the pointer's picture by name */
    162 +
    163 +#ifndef SWC_CURSOR_SHAPE_H
    164 +#define SWC_CURSOR_SHAPE_H
    165 +
    166 +struct wl_display;
    167 +
    168 +struct wl_global *
    169 +cursor_shape_manager_create(struct wl_display *display);
    170 +
    171 +#endif
    172 diff --git a/libswc/data.c b/libswc/data.c
    173 index 8c34b83..d497544 100644
    174 --- a/libswc/data.c
    175 +++ b/libswc/data.c
    176 @@ -31,10 +31,39 @@
    177  
    178  struct data {
    179  	struct wl_array mime_types;
    180 -	struct wl_resource *source;
    181 +	struct wl_resource *source; /* NULL for a source of the compositor's own */
    182 +	struct data_host host;
    183  	struct wl_list offers;
    184 +
    185 +	/* drag state: source actions, target actions, agreed action */
    186 +	uint32_t source_actions, action;
    187 +	bool accepted;
    188 +	/* current offer, target actions and preference, key-chosen action */
    189 +	struct wl_resource *drag_offer;
    190 +	uint32_t offer_actions, preferred, chosen;
    191  };
    192  
    193 +/* action both allow: target preference, then copy, move, ask */
    194 +static uint32_t
    195 +agree(uint32_t both, uint32_t preferred)
    196 +{
    197 +	static const uint32_t order[] = {
    198 +		WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,
    199 +		WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
    200 +		WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK,
    201 +	};
    202 +
    203 +	if (preferred & both) {
    204 +		return preferred;
    205 +	}
    206 +	for (size_t i = 0; i < sizeof(order) / sizeof(order[0]); i++) {
    207 +		if (order[i] & both) {
    208 +			return order[i];
    209 +		}
    210 +	}
    211 +	return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
    212 +}
    213 +
    214  static void
    215  offer_accept(struct wl_client *client, struct wl_resource *offer,
    216               uint32_t serial, const char *mime_type)
    217 @@ -46,6 +75,10 @@ offer_accept(struct wl_client *client, struct wl_resource *offer,
    218  		return;
    219  	}
    220  
    221 +	if (!data->source) {
    222 +		return;
    223 +	}
    224 +	data->accepted = mime_type != NULL;
    225  	wl_data_source_send_target(data->source, mime_type);
    226  }
    227  
    228 @@ -60,26 +93,49 @@ offer_receive(struct wl_client *client, struct wl_resource *offer,
    229  		return;
    230  	}
    231  
    232 -	wl_data_source_send_send(data->source, mime_type, fd);
    233 +	data_request(data, mime_type, fd);
    234  	close(fd);
    235  }
    236  
    237 +/* drop finished: the source may release its data, delete it after a move */
    238  static void
    239  offer_finish(struct wl_client *client, struct wl_resource *offer)
    240  {
    241 +	struct data *data = wl_resource_get_user_data(offer);
    242 +
    243  	(void)client;
    244 -	(void)offer;
    245 -	/* TODO: Implement */
    246 +	if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
    247 +		wl_data_source_send_dnd_finished(data->source);
    248 +	}
    249 +}
    250 +
    251 +/* pick the action, a held key first, and tell both sides */
    252 +static void
    253 +settle(struct data *data, struct wl_resource *offer)
    254 +{
    255 +	uint32_t both = data->source_actions & data->offer_actions;
    256 +
    257 +	data->action = data->chosen & both ? data->chosen : agree(both, data->preferred);
    258 +	if (wl_resource_get_version(offer) >= WL_DATA_OFFER_ACTION_SINCE_VERSION) {
    259 +		wl_data_offer_send_action(offer, data->action);
    260 +	}
    261 +	if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
    262 +		wl_data_source_send_action(data->source, data->action);
    263 +	}
    264  }
    265  
    266  static void
    267  offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t dnd_actions, uint32_t preferred_action)
    268  {
    269 +	struct data *data = wl_resource_get_user_data(offer);
    270 +
    271  	(void)client;
    272 -	(void)offer;
    273 -	(void)dnd_actions;
    274 -	(void)preferred_action;
    275 -	/* TODO: Implement */
    276 +	if (!data || !data->source) {
    277 +		return;
    278 +	}
    279 +	data->offer_actions = dnd_actions;
    280 +	data->preferred = preferred_action;
    281 +	settle(data, offer);
    282  }
    283  
    284  static const struct wl_data_offer_interface data_offer_impl = {
    285 @@ -117,10 +173,10 @@ error0:
    286  static void
    287  source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions)
    288  {
    289 +	struct data *data = wl_resource_get_user_data(resource);
    290 +
    291  	(void)client;
    292 -	(void)resource;
    293 -	(void)dnd_actions;
    294 -	/* TODO: Implement */
    295 +	data->source_actions = dnd_actions;
    296  }
    297  
    298  static const struct wl_data_source_interface data_source_impl = {
    299 @@ -129,33 +185,32 @@ static const struct wl_data_source_interface data_source_impl = {
    300  	.set_actions = source_set_actions,
    301  };
    302  
    303 +/* after the source is gone each data_offer made from it has a pointer to a
    304 + * free'd struct. the resources cannot be destroyed, a client that correctly
    305 + * calls data_offer.destroy would crash it, so the user data of each offer is
    306 + * cleared, and a client that still calls accept or receive does nothing */
    307  static void
    308 -data_destroy(struct wl_resource *source)
    309 +release(struct data *data)
    310  {
    311 -	struct data *data = wl_resource_get_user_data(source);
    312  	struct wl_resource *offer;
    313  	char **mime_type;
    314  
    315  	wl_array_for_each(mime_type, &data->mime_types) free(*mime_type);
    316  	wl_array_release(&data->mime_types);
    317 -
    318 -	/* After this data_source is destroyed, each of the data_offer objects
    319 -	 * associated with the data_source has a pointer to a free'd struct. We
    320 -	 * can't destroy the resources because this results in a segfault on the
    321 -	 * client when it correctly tries to call data_source.destroy. However, a
    322 -	 * misbehaving client could still attempt to call accept or receive on the
    323 -	 * data_offer, which would crash the server.
    324 -	 *
    325 -	 * So, we clear the user data on each of the offers to protect us. */
    326  	wl_resource_for_each(offer, &data->offers)
    327  	{
    328  		wl_resource_set_user_data(offer, NULL);
    329  		wl_resource_set_destructor(offer, NULL);
    330  	}
    331 -
    332  	free(data);
    333  }
    334  
    335 +static void
    336 +data_destroy(struct wl_resource *source)
    337 +{
    338 +	release(wl_resource_get_user_data(source));
    339 +}
    340 +
    341  struct wl_resource *
    342  data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
    343  {
    344 @@ -167,6 +222,11 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
    345  	}
    346  	wl_array_init(&data->mime_types);
    347  	wl_list_init(&data->offers);
    348 +	data->source_actions = data->action = 0;
    349 +	memset(&data->host, 0, sizeof(data->host));
    350 +	data->accepted = false;
    351 +	data->drag_offer = NULL;
    352 +	data->offer_actions = data->preferred = data->chosen = 0;
    353  
    354  	data->source =
    355  	    wl_resource_create(client, &wl_data_source_interface, version, id);
    356 @@ -185,10 +245,8 @@ error0:
    357  }
    358  
    359  struct wl_resource *
    360 -data_offer_new(struct wl_client *client, struct wl_resource *source,
    361 -               uint32_t version)
    362 +data_offer_for(struct wl_client *client, struct data *data, uint32_t version)
    363  {
    364 -	struct data *data = wl_resource_get_user_data(source);
    365  	struct wl_resource *offer;
    366  
    367  	offer = wl_resource_create(client, &wl_data_offer_interface, version, 0);
    368 @@ -202,12 +260,137 @@ data_offer_new(struct wl_client *client, struct wl_resource *source,
    369  	return offer;
    370  }
    371  
    372 +struct wl_resource *
    373 +data_offer_new(struct wl_client *client, struct wl_resource *source,
    374 +               uint32_t version)
    375 +{
    376 +	return data_offer_for(client, wl_resource_get_user_data(source), version);
    377 +}
    378 +
    379  void
    380 -data_send_mime_types(struct wl_resource *source, struct wl_resource *offer)
    381 +data_send_types(struct data *data, struct wl_resource *offer)
    382  {
    383 -	struct data *data = wl_resource_get_user_data(source);
    384  	char **mime_type;
    385  
    386  	wl_array_for_each(mime_type, &data->mime_types)
    387  	    wl_data_offer_send_offer(offer, *mime_type);
    388  }
    389 +
    390 +void
    391 +data_send_mime_types(struct wl_resource *source, struct wl_resource *offer)
    392 +{
    393 +	data_send_types(wl_resource_get_user_data(source), offer);
    394 +}
    395 +
    396 +struct data *
    397 +data_of(struct wl_resource *source)
    398 +{
    399 +	return wl_resource_get_user_data(source);
    400 +}
    401 +
    402 +unsigned
    403 +data_type_count(const struct data *data)
    404 +{
    405 +	return data->mime_types.size / sizeof(char *);
    406 +}
    407 +
    408 +const char *
    409 +data_type_at(const struct data *data, unsigned index)
    410 +{
    411 +	return ((char **)data->mime_types.data)[index];
    412 +}
    413 +
    414 +void
    415 +data_request(struct data *data, const char *mime_type, int fd)
    416 +{
    417 +	if (data->source) {
    418 +		wl_data_source_send_send(data->source, mime_type, fd);
    419 +	} else if (data->host.send) {
    420 +		data->host.send(data->host.data, mime_type, fd);
    421 +	}
    422 +}
    423 +
    424 +void
    425 +data_cancel(struct data *data)
    426 +{
    427 +	if (data->source) {
    428 +		wl_data_source_send_cancelled(data->source);
    429 +	} else if (data->host.cancelled) {
    430 +		data->host.cancelled(data->host.data);
    431 +	}
    432 +}
    433 +
    434 +struct data *
    435 +data_host_new(const char *const *types, unsigned count,
    436 +              const struct data_host *host, void *host_data)
    437 +{
    438 +	struct data *data = calloc(1, sizeof(*data));
    439 +	char **dst;
    440 +	unsigned i;
    441 +
    442 +	if (!data) {
    443 +		return NULL;
    444 +	}
    445 +	wl_array_init(&data->mime_types);
    446 +	wl_list_init(&data->offers);
    447 +	data->host = *host;
    448 +	data->host.data = host_data;
    449 +	for (i = 0; i < count; i++) {
    450 +		dst = wl_array_add(&data->mime_types, sizeof(*dst));
    451 +		if (!dst) {
    452 +			release(data);
    453 +			return NULL;
    454 +		}
    455 +		if (!(*dst = strdup(types[i]))) {
    456 +			data->mime_types.size -= sizeof(*dst);
    457 +			release(data);
    458 +			return NULL;
    459 +		}
    460 +	}
    461 +	return data;
    462 +}
    463 +
    464 +void
    465 +data_host_free(struct data *data)
    466 +{
    467 +	release(data);
    468 +}
    469 +
    470 +void
    471 +data_drag_enter(struct wl_resource *source, struct wl_resource *offer)
    472 +{
    473 +	struct data *data = wl_resource_get_user_data(source);
    474 +
    475 +	data->accepted = false;
    476 +	data->action = data->offer_actions = data->preferred = 0;
    477 +	data->drag_offer = offer;
    478 +	if (wl_resource_get_version(offer) >= WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
    479 +		wl_data_offer_send_source_actions(offer, data->source_actions);
    480 +	}
    481 +}
    482 +
    483 +bool
    484 +data_drag_droppable(struct wl_resource *source)
    485 +{
    486 +	struct data *data = wl_resource_get_user_data(source);
    487 +
    488 +	if (!data->accepted) {
    489 +		return false;
    490 +	}
    491 +	/* no actions before version 3, a drop copies */
    492 +	return wl_resource_get_version(source) < WL_DATA_SOURCE_ACTION_SINCE_VERSION || data->action;
    493 +}
    494 +
    495 +void
    496 +data_drag_choose(struct wl_resource *source, uint32_t action)
    497 +{
    498 +	struct data *data = wl_resource_get_user_data(source);
    499 +
    500 +	if (data->chosen == action) {
    501 +		return;
    502 +	}
    503 +	data->chosen = action;
    504 +	if (data->drag_offer && data->offer_actions) {
    505 +		settle(data, data->drag_offer);
    506 +	}
    507 +}
    508 diff --git a/libswc/data.h b/libswc/data.h
    509 index c99f274..d1c762f 100644
    510 --- a/libswc/data.h
    511 +++ b/libswc/data.h
    512 @@ -24,6 +24,7 @@
    513  #ifndef SWC_DATA_H
    514  #define SWC_DATA_H
    515  
    516 +#include <stdbool.h>
    517  #include <stdint.h>
    518  
    519  struct wl_client;
    520 @@ -36,4 +37,47 @@ data_offer_new(struct wl_client *client, struct wl_resource *source,
    521  void
    522  data_send_mime_types(struct wl_resource *source, struct wl_resource *offer);
    523  
    524 +struct data;
    525 +
    526 +/* a source of the compositor's own, for a selection it keeps itself: it
    527 + * offers types and answers each ask on its own */
    528 +struct data_host {
    529 +	void (*send)(void *data, const char *mime_type, int fd);
    530 +	void (*cancelled)(void *data);
    531 +	void *data;
    532 +};
    533 +
    534 +struct data *
    535 +data_host_new(const char *const *types, unsigned count,
    536 +              const struct data_host *host, void *data);
    537 +void
    538 +data_host_free(struct data *data);
    539 +struct data *
    540 +data_of(struct wl_resource *source);
    541 +/* offers of a selection, host or client */
    542 +struct wl_resource *
    543 +data_offer_for(struct wl_client *client, struct data *data, uint32_t version);
    544 +void
    545 +data_send_types(struct data *data, struct wl_resource *offer);
    546 +unsigned
    547 +data_type_count(const struct data *data);
    548 +const char *
    549 +data_type_at(const struct data *data, unsigned index);
    550 +/* asks the source for a type, to be written to fd (the caller closes its copy) */
    551 +void
    552 +data_request(struct data *data, const char *mime_type, int fd);
    553 +/* the selection went to someone else */
    554 +void
    555 +data_cancel(struct data *data);
    556 +
    557 +/* a drag offer for source entered a client, nothing accepted yet */
    558 +void
    559 +data_drag_enter(struct wl_resource *source, struct wl_resource *offer);
    560 +/* action a held key asks for, 0 for none, wins over the target preference */
    561 +void
    562 +data_drag_choose(struct wl_resource *source, uint32_t action);
    563 +/* target took a type and an action both allow */
    564 +bool
    565 +data_drag_droppable(struct wl_resource *source);
    566 +
    567  #endif
    568 diff --git a/libswc/data_device.c b/libswc/data_device.c
    569 index f286c37..b69f57c 100644
    570 --- a/libswc/data_device.c
    571 +++ b/libswc/data_device.c
    572 @@ -22,9 +22,254 @@
    573   */
    574  
    575  #include "data_device.h"
    576 +#include "compositor.h"
    577  #include "data.h"
    578  #include "event.h"
    579 +#include "keyboard.h"
    580 +#include "internal.h"
    581 +#include "pointer.h"
    582 +#include "seat.h"
    583 +#include "surface.h"
    584  #include "util.h"
    585 +#include "view.h"
    586 +
    587 +#include <string.h>
    588 +
    589 +/* drag and drop, one drag at a time, holding the pointer until the buttons are released */
    590 +static struct drag {
    591 +	bool active;
    592 +	struct data_device *device;
    593 +	struct wl_client *client;       /* starter */
    594 +	struct wl_resource *source;     /* NULL for a drag inside one client */
    595 +	struct compositor_view *icon;   /* the icon */
    596 +	struct compositor_view *focus;  /* the view it is over */
    597 +	struct wl_resource *target;     /* its client's data device */
    598 +	struct pointer_handler handler;
    599 +	struct wl_listener source_destroy, focus_destroy, icon_destroy;
    600 +} drag;
    601 +
    602 +static bool drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state);
    603 +
    604 +static void
    605 +drag_unfocus(void)
    606 +{
    607 +	if (drag.focus) {
    608 +		wl_list_remove(&drag.focus_destroy.link);
    609 +	}
    610 +	drag.focus = NULL;
    611 +	drag.target = NULL;
    612 +}
    613 +
    614 +static void
    615 +drag_leave(void)
    616 +{
    617 +	if (drag.target) {
    618 +		wl_data_device_send_leave(drag.target);
    619 +	}
    620 +	drag_unfocus();
    621 +}
    622 +
    623 +static wl_fixed_t
    624 +local(int32_t at, int32_t origin, int32_t offset)
    625 +{
    626 +	return wl_fixed_from_int(at - origin + offset);
    627 +}
    628 +
    629 +static void
    630 +drag_enter(struct compositor_view *view, int32_t x, int32_t y)
    631 +{
    632 +	struct wl_client *client;
    633 +	struct wl_resource *device, *offer = NULL;
    634 +
    635 +	if (view == drag.focus) {
    636 +		return;
    637 +	}
    638 +	drag_leave();
    639 +	if (!view || !view->surface) {
    640 +		return;
    641 +	}
    642 +	client = wl_resource_get_client(view->surface->resource);
    643 +	/* a drag with no source stays inside the client that made it */
    644 +	if (!drag.source && client != drag.client) {
    645 +		return;
    646 +	}
    647 +	device = wl_resource_find_for_client(&drag.device->resources, client);
    648 +	if (!device) {
    649 +		return;
    650 +	}
    651 +	if (drag.source && (offer = data_offer_new(client, drag.source, wl_resource_get_version(device)))) {
    652 +		wl_data_device_send_data_offer(device, offer);
    653 +		data_send_mime_types(drag.source, offer);
    654 +		data_drag_enter(drag.source, offer);
    655 +	}
    656 +	drag.focus = view;
    657 +	drag.target = device;
    658 +	wl_signal_add(&view->destroy_signal, &drag.focus_destroy);
    659 +	wl_data_device_send_enter(device, wl_display_next_serial(swc.display), view->surface->resource,
    660 +	                          local(x, view->base.geometry.x, view->buffer_offset_x),
    661 +	                          local(y, view->base.geometry.y, view->buffer_offset_y), offer);
    662 +}
    663 +
    664 +static void
    665 +drag_end(void)
    666 +{
    667 +	struct pointer *pointer = swc.seat->pointer;
    668 +
    669 +	drag_leave();
    670 +	wl_list_remove(&drag.handler.link);
    671 +	if (drag.source) {
    672 +		wl_list_remove(&drag.source_destroy.link);
    673 +	}
    674 +	if (drag.icon) {
    675 +		wl_list_remove(&drag.icon_destroy.link);
    676 +		compositor_view_destroy(drag.icon);
    677 +	}
    678 +	memset(&drag, 0, sizeof(drag));
    679 +	/* held buttons are swallowed as they go up */
    680 +	drag.handler.button = drag_swallow;
    681 +	/* the pointer goes back to what is under it */
    682 +	pointer_set_focus(pointer, compositor_view_at(wl_fixed_to_int(pointer->x), wl_fixed_to_int(pointer->y)));
    683 +}
    684 +
    685 +static void
    686 +drag_drop(void)
    687 +{
    688 +	bool take = drag.target && (!drag.source || data_drag_droppable(drag.source));
    689 +
    690 +	if (!take) {
    691 +		if (drag.source) {
    692 +			wl_data_source_send_cancelled(drag.source);
    693 +		}
    694 +		drag_end();
    695 +		return;
    696 +	}
    697 +	wl_data_device_send_drop(drag.target);
    698 +	if (drag.source && wl_resource_get_version(drag.source) >= WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) {
    699 +		wl_data_source_send_dnd_drop_performed(drag.source);
    700 +	}
    701 +	/* the target keeps its offer, no leave follows */
    702 +	drag_unfocus();
    703 +	drag_end();
    704 +}
    705 +
    706 +static bool
    707 +drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state)
    708 +{
    709 +	(void)handler;
    710 +	(void)time;
    711 +	(void)button;
    712 +	(void)state;
    713 +	return true;
    714 +}
    715 +
    716 +/* modifiers that ask for each action, all held. none means never */
    717 +static uint32_t action_modifiers[] = {
    718 +    [0] = SWC_MOD_CTRL,  /* copy */
    719 +    [1] = SWC_MOD_SHIFT, /* move */
    720 +    [2] = 0,             /* ask */
    721 +};
    722 +
    723 +static const uint32_t actions[] = {
    724 +    WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,
    725 +    WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
    726 +    WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK,
    727 +};
    728 +
    729 +/* action the held modifiers ask for, the one with most modifiers wins */
    730 +static uint32_t
    731 +drag_action_for(uint32_t mods)
    732 +{
    733 +	uint32_t best = 0;
    734 +	int bits, most = 0;
    735 +	size_t i;
    736 +
    737 +	for (i = 0; i < sizeof actions / sizeof actions[0]; i++) {
    738 +		if (!action_modifiers[i] || (mods & action_modifiers[i]) != action_modifiers[i]) {
    739 +			continue;
    740 +		}
    741 +		bits = __builtin_popcount(action_modifiers[i]);
    742 +		if (bits > most) {
    743 +			most = bits;
    744 +			best = actions[i];
    745 +		}
    746 +	}
    747 +	return best;
    748 +}
    749 +
    750 +EXPORT void
    751 +swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers)
    752 +{
    753 +	size_t i;
    754 +
    755 +	for (i = 0; i < sizeof actions / sizeof actions[0]; i++) {
    756 +		if (actions[i] == (uint32_t)action) {
    757 +			action_modifiers[i] = modifiers & (SWC_MOD_CTRL | SWC_MOD_ALT | SWC_MOD_LOGO | SWC_MOD_SHIFT);
    758 +		}
    759 +	}
    760 +}
    761 +
    762 +static bool
    763 +drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fixed_t fy)
    764 +{
    765 +	int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy);
    766 +
    767 +	(void)handler;
    768 +	if (drag.icon) {
    769 +		view_move(&drag.icon->base, x, y);
    770 +	}
    771 +	if (drag.source) {
    772 +		uint32_t mods = swc.seat->keyboard ? swc.seat->keyboard->modifiers : 0;
    773 +
    774 +		data_drag_choose(drag.source, drag_action_for(mods));
    775 +	}
    776 +	drag_enter(compositor_view_at(x, y), x, y);
    777 +	if (drag.target) {
    778 +		wl_data_device_send_motion(drag.target, time,
    779 +		                           local(x, drag.focus->base.geometry.x, drag.focus->buffer_offset_x),
    780 +		                           local(y, drag.focus->base.geometry.y, drag.focus->buffer_offset_y));
    781 +	}
    782 +	return true;
    783 +}
    784 +
    785 +static bool
    786 +drag_button(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state)
    787 +{
    788 +	(void)handler;
    789 +	(void)time;
    790 +	(void)button;
    791 +	/* the drop comes when the last button held is let go */
    792 +	if (state == WL_POINTER_BUTTON_STATE_RELEASED && swc.seat->pointer->buttons.size <= sizeof(struct button)) {
    793 +		drag_drop();
    794 +	}
    795 +	return true;
    796 +}
    797 +
    798 +static void
    799 +handle_drag_source_destroy(struct wl_listener *listener, void *data)
    800 +{
    801 +	(void)listener;
    802 +	(void)data;
    803 +	drag.source = NULL;
    804 +	drag_end();
    805 +}
    806 +
    807 +static void
    808 +handle_drag_focus_destroy(struct wl_listener *listener, void *data)
    809 +{
    810 +	(void)data;
    811 +	wl_list_remove(&listener->link);
    812 +	drag.focus = NULL;
    813 +	drag.target = NULL;
    814 +}
    815 +
    816 +static void
    817 +handle_drag_icon_destroy(struct wl_listener *listener, void *data)
    818 +{
    819 +	(void)listener;
    820 +	(void)data;
    821 +	compositor_view_destroy(drag.icon);
    822 +	drag.icon = NULL;
    823 +}
    824  
    825  static void
    826  start_drag(struct wl_client *client, struct wl_resource *resource,
    827 @@ -32,7 +277,46 @@ start_drag(struct wl_client *client, struct wl_resource *resource,
    828             struct wl_resource *origin_resource,
    829             struct wl_resource *icon_resource, uint32_t serial)
    830  {
    831 -	/* XXX: Implement */
    832 +	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    833 +	int32_t x, y;
    834 +	struct button *button;
    835 +
    836 +	(void)origin_resource;
    837 +	(void)serial;
    838 +	/* a drag needs a button held, and one at a time */
    839 +	if (drag.active || !pointer || !pointer->buttons.size) {
    840 +		if (source_resource) {
    841 +			wl_data_source_send_cancelled(source_resource);
    842 +		}
    843 +		return;
    844 +	}
    845 +	x = wl_fixed_to_int(pointer->x);
    846 +	y = wl_fixed_to_int(pointer->y);
    847 +	drag.active = true;
    848 +	drag.device = wl_resource_get_user_data(resource);
    849 +	drag.client = client;
    850 +	drag.source = source_resource;
    851 +	drag.focus_destroy.notify = handle_drag_focus_destroy;
    852 +	if (drag.source) {
    853 +		drag.source_destroy.notify = handle_drag_source_destroy;
    854 +		wl_resource_add_destroy_listener(drag.source, &drag.source_destroy);
    855 +	}
    856 +	if (icon_resource && (drag.icon = compositor_create_view(wl_resource_get_user_data(icon_resource)))) {
    857 +		drag.icon->no_input = true;
    858 +		drag.icon->always_top = true;
    859 +		compositor_view_set_z(drag.icon, STACK_LAYER_OVERLAY, true);
    860 +		view_move(&drag.icon->base, x, y);
    861 +		compositor_view_show(drag.icon);
    862 +		drag.icon_destroy.notify = handle_drag_icon_destroy;
    863 +		wl_resource_add_destroy_listener(icon_resource, &drag.icon_destroy);
    864 +	}
    865 +	/* the pointer is the drag's: no client sees it until the drop */
    866 +	pointer_set_focus(pointer, NULL);
    867 +	drag.handler.motion = drag_motion;
    868 +	drag.handler.button = drag_button;
    869 +	wl_list_insert(&pointer->handlers, &drag.handler.link);
    870 +	wl_array_for_each(button, &pointer->buttons) button->handler = &drag.handler;
    871 +	drag_enter(compositor_view_at(x, y), x, y);
    872  }
    873  
    874  static void
    875 @@ -50,6 +334,11 @@ set_selection(struct wl_client *client, struct wl_resource *resource,
    876  		wl_data_source_send_cancelled(data_device->selection);
    877  		wl_list_remove(&data_device->selection_destroy_listener.link);
    878  	}
    879 +	if (data_device->host) {
    880 +		data_cancel(data_device->host);
    881 +		data_host_free(data_device->host);
    882 +		data_device->host = NULL;
    883 +	}
    884  
    885  	data_device->selection = data_source;
    886  
    887 @@ -89,6 +378,7 @@ data_device_create(void)
    888  		return NULL;
    889  	}
    890  	data_device->selection = NULL;
    891 +	data_device->host = NULL;
    892  	data_device->selection_destroy_listener.notify = &handle_selection_destroy;
    893  	wl_signal_init(&data_device->event_signal);
    894  	wl_list_init(&data_device->resources);
    895 @@ -103,6 +393,9 @@ data_device_destroy(struct data_device *data_device)
    896  
    897  	wl_list_for_each_safe(resource, tmp, &data_device->resources, link)
    898  	    wl_resource_destroy(resource);
    899 +	if (data_device->host) {
    900 +		data_host_free(data_device->host);
    901 +	}
    902  	free(data_device);
    903  }
    904  
    905 @@ -124,18 +417,41 @@ data_device_bind(struct data_device *data_device, struct wl_client *client,
    906  	return resource;
    907  }
    908  
    909 +struct data *
    910 +data_device_client_selection(struct data_device *data_device)
    911 +{
    912 +	return data_device->selection ? data_of(data_device->selection) : NULL;
    913 +}
    914 +
    915 +void
    916 +data_device_set_host(struct data_device *data_device, struct data *host)
    917 +{
    918 +	if (data_device->selection) {
    919 +		wl_data_source_send_cancelled(data_device->selection);
    920 +		wl_list_remove(&data_device->selection_destroy_listener.link);
    921 +		data_device->selection = NULL;
    922 +	}
    923 +	if (data_device->host) {
    924 +		data_cancel(data_device->host);
    925 +		data_host_free(data_device->host);
    926 +	}
    927 +	data_device->host = host;
    928 +	send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED,
    929 +	           host);
    930 +}
    931 +
    932  static struct wl_resource *
    933  new_offer(struct wl_resource *resource, struct wl_client *client,
    934 -          struct wl_resource *source)
    935 +          struct data *source)
    936  {
    937  	struct wl_resource *offer;
    938  
    939 -	offer = data_offer_new(client, source, wl_resource_get_version(resource));
    940 +	offer = data_offer_for(client, source, wl_resource_get_version(resource));
    941  	if (!offer) {
    942  		return NULL;
    943  	}
    944  	wl_data_device_send_data_offer(resource, offer);
    945 -	data_send_mime_types(source, offer);
    946 +	data_send_types(source, offer);
    947  
    948  	return offer;
    949  }
    950 @@ -156,8 +472,10 @@ data_device_offer_selection(struct data_device *data_device,
    951  	}
    952  
    953  	/* If we have a selection, create a new offer for the client. */
    954 -	if (data_device->selection) {
    955 -		offer = new_offer(resource, client, data_device->selection);
    956 +	if (data_device->host) {
    957 +		offer = new_offer(resource, client, data_device->host);
    958 +	} else if (data_device->selection) {
    959 +		offer = new_offer(resource, client, data_of(data_device->selection));
    960  	}
    961  
    962  	wl_data_device_send_selection(resource, offer);
    963 diff --git a/libswc/data_device.h b/libswc/data_device.h
    964 index 6712190..f7242cc 100644
    965 --- a/libswc/data_device.h
    966 +++ b/libswc/data_device.h
    967 @@ -29,9 +29,13 @@
    968  
    969  enum { DATA_DEVICE_EVENT_SELECTION_CHANGED };
    970  
    971 +struct data;
    972 +
    973  struct data_device {
    974  	/* The data source corresponding to the current selection. */
    975  	struct wl_resource *selection;
    976 +	/* the selection when the compositor keeps it itself, never with selection */
    977 +	struct data *host;
    978  	struct wl_listener selection_destroy_listener;
    979  
    980  	struct wl_signal event_signal;
    981 @@ -45,6 +49,14 @@ data_device_destroy(struct data_device *data_device);
    982  struct wl_resource *
    983  data_device_bind(struct data_device *data_device, struct wl_client *client,
    984                   uint32_t version, uint32_t id);
    985 +/* the selection a client set, as data, NULL for none */
    986 +struct data *
    987 +data_device_client_selection(struct data_device *data_device);
    988 +/* takes the selection for the compositor, host owned by the device now. what
    989 + * held it is told it is cancelled. a event with data set says it came from
    990 + * the compositor and not from a client */
    991 +void
    992 +data_device_set_host(struct data_device *data_device, struct data *host);
    993  void
    994  data_device_offer_selection(struct data_device *data_device,
    995                              struct wl_client *client);
    996 diff --git a/libswc/keyboard.c b/libswc/keyboard.c
    997 index af9a286..ffa0629 100644
    998 --- a/libswc/keyboard.c
    999 +++ b/libswc/keyboard.c
   1000 @@ -29,6 +29,7 @@
   1001  #include "compositor.h"
   1002  #include "internal.h"
   1003  #include "surface.h"
   1004 +#include "xdg_shell.h"
   1005  #include "swc.h"
   1006  #include "util.h"
   1007  
   1008 @@ -36,6 +37,7 @@
   1009  #include <fcntl.h>
   1010  #include <limits.h>
   1011  #include <stdio.h>
   1012 +#include <stdlib.h>
   1013  #include <string.h>
   1014  #include <sys/mman.h>
   1015  #include <unistd.h>
   1016 @@ -228,6 +230,23 @@ error0:
   1017  	return false;
   1018  }
   1019  
   1020 +/* The keymap in the file SWC_KEYMAP_FILE names, NULL when there is none. */
   1021 +static struct xkb_keymap *
   1022 +keymap_from_file(struct xkb_context *context)
   1023 +{
   1024 +	const char *path = getenv("SWC_KEYMAP_FILE");
   1025 +	struct xkb_keymap *map;
   1026 +	FILE *file;
   1027 +
   1028 +	if (!path || !*path || !(file = fopen(path, "r")))
   1029 +		return NULL;
   1030 +	map = xkb_keymap_new_from_file(context, file, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
   1031 +	fclose(file);
   1032 +	if (!map)
   1033 +		WARNING("Could not compile the keymap in %s\n", path);
   1034 +	return map;
   1035 +}
   1036 +
   1037  struct keyboard *
   1038  keyboard_create(struct xkb_rule_names *names)
   1039  {
   1040 @@ -245,7 +264,10 @@ keyboard_create(struct xkb_rule_names *names)
   1041  		goto error1;
   1042  	}
   1043  
   1044 -	if (!(xkb->keymap.map =
   1045 +	/* a whole keymap from a file, when the session names one, needs no xkb
   1046 +	 * data of the system's; names need the rules and files they name */
   1047 +	if (!(xkb->keymap.map = keymap_from_file(xkb->context)) &&
   1048 +	    !(xkb->keymap.map =
   1049  	          xkb_keymap_new_from_names(xkb->context, names, 0))) {
   1050  		ERROR("Could not create XKB keymap\n");
   1051  		goto error2;
   1052 @@ -304,6 +326,68 @@ keyboard_destroy(struct keyboard *keyboard)
   1053  	free(keyboard);
   1054  }
   1055  
   1056 +static void update_modifiers(struct keyboard *keyboard);
   1057 +
   1058 +/* A keymap made from names, put in place of the one in use. Clients are
   1059 + * sent the new one on their keyboards and the keys held are let go, since
   1060 + * what they meant belongs to the old layout. False, with the old keymap
   1061 + * kept, when the names make none. */
   1062 +bool
   1063 +keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names)
   1064 +{
   1065 +	struct xkb next = keyboard->xkb;
   1066 +	struct wl_resource *resource;
   1067 +
   1068 +	if (!(next.keymap.map =
   1069 +	          xkb_keymap_new_from_names(next.context, names, 0))) {
   1070 +		WARNING("Could not create XKB keymap\n");
   1071 +		return false;
   1072 +	}
   1073 +	if (!(next.state = xkb_state_new(next.keymap.map))) {
   1074 +		xkb_keymap_unref(next.keymap.map);
   1075 +		return false;
   1076 +	}
   1077 +	if (!update_keymap(&next)) {
   1078 +		xkb_state_unref(next.state);
   1079 +		xkb_keymap_unref(next.keymap.map);
   1080 +		return false;
   1081 +	}
   1082 +
   1083 +	munmap(keyboard->xkb.keymap.area, keyboard->xkb.keymap.size);
   1084 +	close(keyboard->xkb.keymap.fd);
   1085 +	xkb_state_unref(keyboard->xkb.state);
   1086 +	xkb_keymap_unref(keyboard->xkb.keymap.map);
   1087 +	keyboard->xkb = next;
   1088 +
   1089 +	wl_resource_for_each(resource, &keyboard->focus.active)
   1090 +	    wl_keyboard_send_keymap(resource,
   1091 +	                            WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
   1092 +	                            keyboard->xkb.keymap.fd,
   1093 +	                            keyboard->xkb.keymap.size);
   1094 +	wl_resource_for_each(resource, &keyboard->focus.inactive)
   1095 +	    wl_keyboard_send_keymap(resource,
   1096 +	                            WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
   1097 +	                            keyboard->xkb.keymap.fd,
   1098 +	                            keyboard->xkb.keymap.size);
   1099 +	return keyboard_reset(keyboard);
   1100 +}
   1101 +
   1102 +/* The repeat rate and delay, told again to every client that can hear it. */
   1103 +void
   1104 +keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay)
   1105 +{
   1106 +	struct wl_resource *resource;
   1107 +
   1108 +	swc_repeat_rate = rate;
   1109 +	swc_repeat_delay = delay;
   1110 +	wl_resource_for_each(resource, &keyboard->focus.active)
   1111 +	    if (wl_resource_get_version(resource) >= 4)
   1112 +		    wl_keyboard_send_repeat_info(resource, rate, delay);
   1113 +	wl_resource_for_each(resource, &keyboard->focus.inactive)
   1114 +	    if (wl_resource_get_version(resource) >= 4)
   1115 +		    wl_keyboard_send_repeat_info(resource, rate, delay);
   1116 +}
   1117 +
   1118  bool
   1119  keyboard_reset(struct keyboard *keyboard)
   1120  {
   1121 @@ -328,8 +412,6 @@ keyboard_reset(struct keyboard *keyboard)
   1122  	 * handler. */
   1123  	assert(keyboard->client_keys.size == 0);
   1124  	keyboard->keys.size = 0;
   1125 -	keyboard->modifier_state = (struct keyboard_modifier_state){0};
   1126 -	keyboard->modifiers = 0;
   1127  
   1128  	if (!(state = xkb_state_new(keyboard->xkb.keymap.map))) {
   1129  		ERROR("Failed to allocate new XKB state\n");
   1130 @@ -338,6 +420,8 @@ keyboard_reset(struct keyboard *keyboard)
   1131  
   1132  	xkb_state_unref(keyboard->xkb.state);
   1133  	keyboard->xkb.state = state;
   1134 +	/* tell clients the modifiers are released */
   1135 +	update_modifiers(keyboard);
   1136  
   1137  	return true;
   1138  }
   1139 @@ -348,6 +432,8 @@ keyboard_reset(struct keyboard *keyboard)
   1140  void
   1141  keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view)
   1142  {
   1143 +	xdg_popups_focus_changed(
   1144 +	    view && view->surface ? wl_resource_get_client(view->surface->resource) : NULL);
   1145  	input_focus_set(&keyboard->focus, view);
   1146  }
   1147  
   1148 @@ -393,6 +479,56 @@ keyboard_bind(struct keyboard *keyboard,
   1149  	return client_resource;
   1150  }
   1151  
   1152 +/* update modifiers from xkb state, run handlers on change */
   1153 +static void
   1154 +update_modifiers(struct keyboard *keyboard)
   1155 +{
   1156 +	struct xkb *xkb = &keyboard->xkb;
   1157 +	struct keyboard_handler *handler;
   1158 +	struct keyboard_modifier_state modifier_state;
   1159 +
   1160 +	modifier_state.depressed =
   1161 +	    xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED);
   1162 +	modifier_state.latched =
   1163 +	    xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED);
   1164 +	modifier_state.locked =
   1165 +	    xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED);
   1166 +	modifier_state.group =
   1167 +	    xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE);
   1168 +
   1169 +	if (modifier_state.depressed != keyboard->modifier_state.depressed ||
   1170 +	    modifier_state.latched != keyboard->modifier_state.latched ||
   1171 +	    modifier_state.locked != keyboard->modifier_state.locked ||
   1172 +	    modifier_state.group != keyboard->modifier_state.group) {
   1173 +		uint32_t mods_active =
   1174 +		    modifier_state.depressed | modifier_state.latched;
   1175 +
   1176 +		/* Update keyboard modifier state. */
   1177 +		keyboard->modifier_state = modifier_state;
   1178 +		keyboard->modifiers = 0;
   1179 +		if (mods_active & (1 << keyboard->xkb.indices.ctrl)) {
   1180 +			keyboard->modifiers |= SWC_MOD_CTRL;
   1181 +		}
   1182 +		if (mods_active & (1 << keyboard->xkb.indices.alt)) {
   1183 +			keyboard->modifiers |= SWC_MOD_ALT;
   1184 +		}
   1185 +		if (mods_active & (1 << keyboard->xkb.indices.super)) {
   1186 +			keyboard->modifiers |= SWC_MOD_LOGO;
   1187 +		}
   1188 +		if (mods_active & (1 << keyboard->xkb.indices.shift)) {
   1189 +			keyboard->modifiers |= SWC_MOD_SHIFT;
   1190 +		}
   1191 +
   1192 +		/* Run any modifier handlers. */
   1193 +		wl_list_for_each(handler, &keyboard->handlers, link)
   1194 +		{
   1195 +			if (handler->modifiers) {
   1196 +				handler->modifiers(keyboard, &modifier_state);
   1197 +			}
   1198 +		}
   1199 +	}
   1200 +}
   1201 +
   1202  void
   1203  keyboard_handle_key(struct keyboard *keyboard,
   1204                      uint32_t time,
   1205 @@ -400,7 +536,6 @@ keyboard_handle_key(struct keyboard *keyboard,
   1206                      uint32_t state)
   1207  {
   1208  	struct key *key;
   1209 -	struct keyboard_modifier_state modifier_state;
   1210  	enum xkb_key_direction direction;
   1211  	struct xkb *xkb = &keyboard->xkb;
   1212  	struct keyboard_handler *handler;
   1213 @@ -455,44 +590,47 @@ update_xkb_state:
   1214  	    state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP;
   1215  	xkb_state_update_key(xkb->state, XKB_KEY(value), direction);
   1216  
   1217 -	modifier_state.depressed =
   1218 -	    xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED);
   1219 -	modifier_state.latched =
   1220 -	    xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED);
   1221 -	modifier_state.locked =
   1222 -	    xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED);
   1223 -	modifier_state.group =
   1224 -	    xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE);
   1225 +	update_modifiers(keyboard);
   1226 +}
   1227  
   1228 -	if (modifier_state.depressed != keyboard->modifier_state.depressed ||
   1229 -	    modifier_state.latched != keyboard->modifier_state.latched ||
   1230 -	    modifier_state.locked != keyboard->modifier_state.locked ||
   1231 -	    modifier_state.group != keyboard->modifier_state.group) {
   1232 -		uint32_t mods_active =
   1233 -		    modifier_state.depressed | modifier_state.latched;
   1234 +void
   1235 +keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed,
   1236 +                          uint32_t latched, uint32_t locked, uint32_t group)
   1237 +{
   1238 +	xkb_state_update_mask(keyboard->xkb.state, depressed, latched, locked, 0,
   1239 +	                      0, group);
   1240 +	update_modifiers(keyboard);
   1241 +}
   1242  
   1243 -		/* Update keyboard modifier state. */
   1244 -		keyboard->modifier_state = modifier_state;
   1245 -		keyboard->modifiers = 0;
   1246 -		if (mods_active & (1 << keyboard->xkb.indices.ctrl)) {
   1247 -			keyboard->modifiers |= SWC_MOD_CTRL;
   1248 -		}
   1249 -		if (mods_active & (1 << keyboard->xkb.indices.alt)) {
   1250 -			keyboard->modifiers |= SWC_MOD_ALT;
   1251 -		}
   1252 -		if (mods_active & (1 << keyboard->xkb.indices.super)) {
   1253 -			keyboard->modifiers |= SWC_MOD_LOGO;
   1254 -		}
   1255 -		if (mods_active & (1 << keyboard->xkb.indices.shift)) {
   1256 -			keyboard->modifiers |= SWC_MOD_SHIFT;
   1257 -		}
   1258 +bool
   1259 +keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map)
   1260 +{
   1261 +	struct xkb old = keyboard->xkb;
   1262 +	struct wl_resource *resource;
   1263  
   1264 -		/* Run any modifier handlers. */
   1265 -		wl_list_for_each(handler, &keyboard->handlers, link)
   1266 -		{
   1267 -			if (handler->modifiers) {
   1268 -				handler->modifiers(keyboard, &modifier_state);
   1269 -			}
   1270 -		}
   1271 +	keyboard->xkb.keymap.map = xkb_keymap_ref(map);
   1272 +	if (!update_keymap(&keyboard->xkb)) {
   1273 +		xkb_keymap_unref(map);
   1274 +		keyboard->xkb = old;
   1275 +		return false;
   1276 +	}
   1277 +
   1278 +	/* held keys belong to the old keymap */
   1279 +	if (!keyboard_reset(keyboard)) {
   1280 +		return false;
   1281  	}
   1282 +	munmap(old.keymap.area, old.keymap.size);
   1283 +	close(old.keymap.fd);
   1284 +	xkb_keymap_unref(old.keymap.map);
   1285 +
   1286 +	wl_resource_for_each(resource, &keyboard->focus.active)
   1287 +	    wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
   1288 +	                            keyboard->xkb.keymap.fd,
   1289 +	                            keyboard->xkb.keymap.size);
   1290 +	wl_resource_for_each(resource, &keyboard->focus.inactive)
   1291 +	    wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
   1292 +	                            keyboard->xkb.keymap.fd,
   1293 +	                            keyboard->xkb.keymap.size);
   1294 +
   1295 +	return true;
   1296  }
   1297 diff --git a/libswc/keyboard.h b/libswc/keyboard.h
   1298 index 28c5882..01ac208 100644
   1299 --- a/libswc/keyboard.h
   1300 +++ b/libswc/keyboard.h
   1301 @@ -92,6 +92,10 @@ void
   1302  keyboard_destroy(struct keyboard *keyboard);
   1303  bool
   1304  keyboard_reset(struct keyboard *keyboard);
   1305 +bool
   1306 +keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names);
   1307 +void
   1308 +keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay);
   1309  void
   1310  keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view);
   1311  struct wl_resource *
   1312 @@ -100,5 +104,12 @@ keyboard_bind(struct keyboard *keyboard, struct wl_client *client,
   1313  void
   1314  keyboard_handle_key(struct keyboard *keyboard, uint32_t time, uint32_t key,
   1315                      uint32_t state);
   1316 +/* for backends that get modifier state from outside */
   1317 +void
   1318 +keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed,
   1319 +                          uint32_t latched, uint32_t locked, uint32_t group);
   1320 +/* swap the keymap and resend it */
   1321 +bool
   1322 +keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map);
   1323  
   1324  #endif
   1325 diff --git a/libswc/pointer.c b/libswc/pointer.c
   1326 index ab23c3a..e785cbd 100644
   1327 --- a/libswc/pointer.c
   1328 +++ b/libswc/pointer.c
   1329 @@ -26,6 +26,7 @@
   1330  #include "compositor.h"
   1331  #include "cursor/cursor_data.h"
   1332  #include "event.h"
   1333 +#include "layer_shell.h"
   1334  #include "internal.h"
   1335  #ifdef ENABLE_DRM
   1336  #include "plane.h"
   1337 @@ -41,6 +42,19 @@
   1338  #include <string.h>
   1339  #include <wld/wld.h>
   1340  
   1341 +/* the pictures of cursors by name, and the one the window manager shows */
   1342 +#define SHAPES 64
   1343 +struct shape {
   1344 +	char name[32];
   1345 +	const uint32_t *data;
   1346 +	uint32_t width, height;
   1347 +	int32_t hotspot_x, hotspot_y;
   1348 +};
   1349 +static struct shape shapes[SHAPES];
   1350 +static char shape_override[32];
   1351 +static void (*motion_handler)(void *data, int32_t x, int32_t y);
   1352 +static void *motion_data;
   1353 +
   1354  static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT;
   1355  static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT;
   1356  
   1357 @@ -62,6 +76,9 @@ swc_pointer_send_button(uint32_t time, uint32_t button, uint32_t state)
   1358  		return;
   1359  	}
   1360  
   1361 +	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
   1362 +		layer_shell_pressed(pointer->focus.view);
   1363 +	}
   1364  	serial = wl_display_next_serial(swc.display);
   1365  	wl_resource_for_each(resource, &pointer->focus.active)
   1366  	    wl_pointer_send_button(resource, serial, time, button, state);
   1367 @@ -208,15 +225,24 @@ attach(struct view *view, struct wld_buffer *buffer)
   1368  		view_update_screens(view);
   1369  	}
   1370  
   1371 +	if (swc.backend->set_cursor) {
   1372 +		swc.backend->set_cursor(buffer ? pointer->cursor.buffer : NULL,
   1373 +		                        pointer->cursor.hotspot.x,
   1374 +		                        pointer->cursor.hotspot.y);
   1375 +		return 0;
   1376 +	}
   1377 +
   1378  #ifdef ENABLE_DRM
   1379 -	wl_list_for_each(screen, &swc.screens, link) {
   1380 -		view_attach(&screen->planes.cursor->view,
   1381 -		            buffer ? pointer->cursor.buffer : NULL);
   1382 -		view_update(&screen->planes.cursor->view);
   1383 +	if (swc.video == SWC_VIDEO_DRM) {
   1384 +		wl_list_for_each(screen, &swc.screens, link) {
   1385 +			view_attach(&screen->planes.cursor->view,
   1386 +			            buffer ? pointer->cursor.buffer : NULL);
   1387 +			view_update(&screen->planes.cursor->view);
   1388 +		}
   1389 +		return 0;
   1390  	}
   1391 -#else
   1392 -	compositor_damage_all();
   1393  #endif
   1394 +	compositor_damage_all();
   1395  
   1396  	return 0;
   1397  }
   1398 @@ -232,15 +258,22 @@ move(struct view *view, int32_t x, int32_t y)
   1399  		view_update_screens(view);
   1400  	}
   1401  
   1402 +	/* the backend shows the cursor itself and already knows where it is */
   1403 +	if (swc.backend->set_cursor) {
   1404 +		return true;
   1405 +	}
   1406 +
   1407  #ifdef ENABLE_DRM
   1408 -	wl_list_for_each(screen, &swc.screens, link) {
   1409 -		view_move(&screen->planes.cursor->view, view->geometry.x,
   1410 -		          view->geometry.y);
   1411 -		view_update(&screen->planes.cursor->view);
   1412 +	if (swc.video == SWC_VIDEO_DRM) {
   1413 +		wl_list_for_each(screen, &swc.screens, link) {
   1414 +			view_move(&screen->planes.cursor->view, view->geometry.x,
   1415 +			          view->geometry.y);
   1416 +			view_update(&screen->planes.cursor->view);
   1417 +		}
   1418 +		return true;
   1419  	}
   1420 -#else
   1421 -	compositor_damage_all();
   1422  #endif
   1423 +	compositor_damage_all();
   1424  
   1425  	return true;
   1426  }
   1427 @@ -347,6 +380,117 @@ swc_clear_cursor_image(enum swc_cursor_kind kind)
   1428  	apply_cursor_override(pointer);
   1429  }
   1430  
   1431 +static const struct shape *
   1432 +shape_named(const char *name)
   1433 +{
   1434 +	if (!name || !*name) {
   1435 +		return NULL;
   1436 +	}
   1437 +	for (int i = 0; i < SHAPES; i++) {
   1438 +		if (shapes[i].data && !strcmp(shapes[i].name, name)) {
   1439 +			return &shapes[i];
   1440 +		}
   1441 +	}
   1442 +	return NULL;
   1443 +}
   1444 +
   1445 +EXPORT void
   1446 +swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888,
   1447 +                           uint32_t width, uint32_t height, int32_t hotspot_x,
   1448 +                           int32_t hotspot_y)
   1449 +{
   1450 +	int free_slot = -1;
   1451 +
   1452 +	if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 ||
   1453 +	    !width || !height) {
   1454 +		return;
   1455 +	}
   1456 +	for (int i = 0; i < SHAPES; i++) {
   1457 +		if (shapes[i].data && !strcmp(shapes[i].name, name)) {
   1458 +			free_slot = i;
   1459 +			break;
   1460 +		}
   1461 +		if (!shapes[i].data && free_slot < 0) {
   1462 +			free_slot = i;
   1463 +		}
   1464 +	}
   1465 +	if (free_slot < 0) {
   1466 +		return;
   1467 +	}
   1468 +	strcpy(shapes[free_slot].name, name);
   1469 +	shapes[free_slot].data = argb8888;
   1470 +	shapes[free_slot].width = width;
   1471 +	shapes[free_slot].height = height;
   1472 +	shapes[free_slot].hotspot_x = hotspot_x;
   1473 +	shapes[free_slot].hotspot_y = hotspot_y;
   1474 +}
   1475 +
   1476 +EXPORT void
   1477 +swc_set_cursor_shape(const char *name)
   1478 +{
   1479 +	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
   1480 +
   1481 +	if (!name) {
   1482 +		name = "";
   1483 +	}
   1484 +	if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) {
   1485 +		return;
   1486 +	}
   1487 +	strcpy(shape_override, name);
   1488 +	apply_cursor_override(pointer);
   1489 +}
   1490 +
   1491 +EXPORT void
   1492 +swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y),
   1493 +                               void *data)
   1494 +{
   1495 +	motion_handler = handler;
   1496 +	motion_data = data;
   1497 +}
   1498 +
   1499 +void
   1500 +pointer_motion_notify(int32_t x, int32_t y)
   1501 +{
   1502 +	if (motion_handler) {
   1503 +		motion_handler(motion_data, x, y);
   1504 +	}
   1505 +}
   1506 +
   1507 +/* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's
   1508 + * own cursor surface would be */
   1509 +void
   1510 +pointer_set_shape(struct pointer *pointer, const char *name)
   1511 +{
   1512 +	const struct shape *shape = shape_named(name);
   1513 +	struct cursor shape_cursor;
   1514 +	union wld_object object;
   1515 +	struct wld_buffer *buffer;
   1516 +
   1517 +	if (!shape && !(shape = shape_named("default"))) {
   1518 +		return;
   1519 +	}
   1520 +	if (pointer->cursor.internal_buffer) {
   1521 +		wld_buffer_unreference(pointer->cursor.internal_buffer);
   1522 +		pointer->cursor.internal_buffer = NULL;
   1523 +	}
   1524 +	drop_client_cursor_surface(pointer);
   1525 +	shape_cursor.width = (int)shape->width;
   1526 +	shape_cursor.height = (int)shape->height;
   1527 +	object.ptr = (void *)shape->data;
   1528 +	buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object,
   1529 +	                           shape_cursor.width, shape_cursor.height,
   1530 +	                           WLD_FORMAT_ARGB8888, shape_cursor.width * 4);
   1531 +	if (!buffer) {
   1532 +		WARNING("Failed to create cursor buffer\n");
   1533 +		return;
   1534 +	}
   1535 +	pointer->cursor.internal_buffer = buffer;
   1536 +	pointer->cursor.hotspot.x = shape->hotspot_x;
   1537 +	pointer->cursor.hotspot.y = shape->hotspot_y;
   1538 +	update_cursor(pointer);
   1539 +	view_attach(&pointer->cursor.view, buffer);
   1540 +}
   1541 +
   1542  void
   1543  pointer_set_cursor(struct pointer *pointer, uint32_t id)
   1544  {
   1545 @@ -355,7 +499,20 @@ pointer_set_cursor(struct pointer *pointer, uint32_t id)
   1546  	union wld_object object = {.ptr = &cursor_data[cursor->offset]};
   1547  	struct wld_buffer *buffer;
   1548  
   1549 -	if (id == cursor_left_ptr) {
   1550 +	if (id == cursor_left_ptr && shape_named(shape_override)) {
   1551 +		/* the window manager's, where no client has the pointer */
   1552 +		static struct cursor shape_cursor;
   1553 +		const struct shape *shape = shape_named(shape_override);
   1554 +
   1555 +		shape_cursor.width = (int)shape->width;
   1556 +		shape_cursor.height = (int)shape->height;
   1557 +		shape_cursor.hotspot_x = shape->hotspot_x;
   1558 +		shape_cursor.hotspot_y = shape->hotspot_y;
   1559 +		shape_cursor.offset = 0;
   1560 +		cursor = &shape_cursor;
   1561 +		data = shape->data;
   1562 +		object.ptr = (void *)data;
   1563 +	} else if (id == cursor_left_ptr) {
   1564  		enum swc_cursor_kind kind = cursor_override;
   1565  		if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) {
   1566  			kind = SWC_CURSOR_DEFAULT;
   1567 @@ -431,6 +588,9 @@ client_handle_button(struct pointer_handler *handler, uint32_t time,
   1568  	if (wl_list_empty(&pointer->focus.active)) {
   1569  		return false;
   1570  	}
   1571 +	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
   1572 +		layer_shell_pressed(pointer->focus.view);
   1573 +	}
   1574  
   1575  	wl_resource_for_each(resource, &pointer->focus.active)
   1576  	    wl_pointer_send_button(resource, button->press.serial, time,
   1577 @@ -548,6 +708,17 @@ pointer_initialize(struct pointer *pointer)
   1578  void
   1579  pointer_finalize(struct pointer *pointer)
   1580  {
   1581 +	if (pointer->cursor.surface) {
   1582 +		surface_set_view(pointer->cursor.surface, NULL);
   1583 +		wl_list_remove(&pointer->cursor.destroy_listener.link);
   1584 +	}
   1585 +	if (swc.backend->set_cursor)
   1586 +		swc.backend->set_cursor(NULL, 0, 0);
   1587 +	view_finalize(&pointer->cursor.view);
   1588 +	if (pointer->cursor.internal_buffer)
   1589 +		wld_buffer_unreference(pointer->cursor.internal_buffer);
   1590 +	wld_buffer_unreference(pointer->cursor.buffer);
   1591 +	wl_array_release(&pointer->buttons);
   1592  	input_focus_finalize(&pointer->focus);
   1593  	pixman_region32_fini(&pointer->region);
   1594  }
   1595 @@ -768,8 +939,6 @@ pointer_handle_absolute_motion(struct pointer *pointer, uint32_t time,
   1596  			break;
   1597  		}
   1598  	}
   1599 -
   1600 -	update_cursor(pointer);
   1601  }
   1602  
   1603  void
   1604 diff --git a/libswc/pointer.h b/libswc/pointer.h
   1605 index 21a54cd..c6e2126 100644
   1606 --- a/libswc/pointer.h
   1607 +++ b/libswc/pointer.h
   1608 @@ -86,6 +86,10 @@ void
   1609  pointer_set_region(struct pointer *pointer, pixman_region32_t *region);
   1610  void
   1611  pointer_set_cursor(struct pointer *pointer, uint32_t id);
   1612 +void
   1613 +pointer_set_shape(struct pointer *pointer, const char *name);
   1614 +void
   1615 +pointer_motion_notify(int32_t x, int32_t y);
   1616  
   1617  struct button *
   1618  pointer_get_button(struct pointer *pointer, uint32_t serial);
   1619 diff --git a/libswc/seat.c b/libswc/seat.c
   1620 index 413a396..02d43a0 100644
   1621 --- a/libswc/seat.c
   1622 +++ b/libswc/seat.c
   1623 @@ -22,6 +22,7 @@
   1624   */
   1625  
   1626  #include "seat.h"
   1627 +#include "backend.h"
   1628  #include "compositor.h"
   1629  #include "data_device.h"
   1630  #include "event.h"
   1631 @@ -50,8 +51,14 @@
   1632  #include <libudev.h>
   1633  #endif
   1634  
   1635 +/* The netlink groups libinput listens on for devices that appear: a bit
   1636 + * for each group, and group 1 is the kernel's own uevents, which are there
   1637 + * with no device manager running. Another group hears nothing unless
   1638 + * something forwards to it, and a device that appears after the compositor
   1639 + * started (a touchpad whose driver binds late, a USB keyboard) is then not
   1640 + * seen until the seat is resumed. */
   1641  #ifndef NETLINK_MASK
   1642 -#define NETLINK_MASK 4
   1643 +#define NETLINK_MASK 1
   1644  #endif
   1645  
   1646  struct seat {
   1647 @@ -122,11 +129,13 @@ handle_swc_event(struct wl_listener *listener, void *data)
   1648  
   1649  	switch (ev->type) {
   1650  	case SWC_EVENT_DEACTIVATED:
   1651 -		libinput_suspend(seat->libinput);
   1652 +		if (seat->libinput) {
   1653 +			libinput_suspend(seat->libinput);
   1654 +		}
   1655  		keyboard_reset(seat->base.keyboard);
   1656  		break;
   1657  	case SWC_EVENT_ACTIVATED:
   1658 -		if (libinput_resume(seat->libinput) != 0) {
   1659 +		if (seat->libinput && libinput_resume(seat->libinput) != 0) {
   1660  			WARNING("Failed to resume libinput context\n");
   1661  		}
   1662  		break;
   1663 @@ -484,7 +493,12 @@ seat_create(struct wl_display *display, const char *seat_name)
   1664  	}
   1665  	seat->base.pointer = &seat->pointer;
   1666  
   1667 -	if (!initialize_libinput(seat)) {
   1668 +	seat->libinput = NULL;
   1669 +	seat->libinput_source = NULL;
   1670 +	if (swc.backend->virtual_input) {
   1671 +		update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD |
   1672 +		                              WL_SEAT_CAPABILITY_POINTER);
   1673 +	} else if (!initialize_libinput(seat)) {
   1674  		goto error6;
   1675  	}
   1676  
   1677 @@ -511,11 +525,13 @@ seat_destroy(struct swc_seat *seat_base)
   1678  {
   1679  	struct seat *seat = wl_container_of(seat_base, seat, base);
   1680  
   1681 -	wl_event_source_remove(seat->libinput_source);
   1682 -	libinput_unref(seat->libinput);
   1683 +	if (seat->libinput) {
   1684 +		wl_event_source_remove(seat->libinput_source);
   1685 +		libinput_unref(seat->libinput);
   1686  #ifdef ENABLE_LIBUDEV
   1687 -	udev_unref(seat->udev);
   1688 +		udev_unref(seat->udev);
   1689  #endif
   1690 +	}
   1691  
   1692  	pointer_finalize(&seat->pointer);
   1693  	keyboard_destroy(seat->base.keyboard);
   1694 diff --git a/libswc/selection.c b/libswc/selection.c
   1695 new file mode 100644
   1696 index 0000000..b6845e0
   1697 --- /dev/null
   1698 +++ b/libswc/selection.c
   1699 @@ -0,0 +1,100 @@
   1700 +/* swc: selection.c
   1701 + *
   1702 + * the clipboard for a compositor that keeps it itself, see swc.h
   1703 + *
   1704 + * Permission is hereby granted, free of charge, to any person obtaining a copy
   1705 + * of this software and associated documentation files (the "Software"), to deal
   1706 + * in the Software without restriction, including without limitation the rights
   1707 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   1708 + * copies of the Software, and to permit persons to whom the Software is
   1709 + * furnished to do so, subject to the following conditions:
   1710 + *
   1711 + * The above copyright notice and this permission notice shall be included in
   1712 + * all copies or substantial portions of the Software.
   1713 + */
   1714 +
   1715 +#include "swc.h"
   1716 +#include "data.h"
   1717 +#include "data_device.h"
   1718 +#include "event.h"
   1719 +#include "internal.h"
   1720 +#include "seat.h"
   1721 +#include "util.h"
   1722 +
   1723 +#include <string.h>
   1724 +
   1725 +#define MAX_TYPES 64
   1726 +
   1727 +static struct {
   1728 +	const struct swc_selection_handler *handler;
   1729 +	void *data;
   1730 +	struct wl_listener listener;
   1731 +	bool hooked;
   1732 +} selection;
   1733 +
   1734 +static void
   1735 +handle_event(struct wl_listener *listener, void *data)
   1736 +{
   1737 +	struct event *ev = data;
   1738 +	struct data *source;
   1739 +	const char *types[MAX_TYPES];
   1740 +	unsigned i, count = 0;
   1741 +
   1742 +	if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED || ev->data || !selection.handler) {
   1743 +		return;
   1744 +	}
   1745 +	if ((source = data_device_client_selection(swc.seat->data_device))) {
   1746 +		count = data_type_count(source);
   1747 +		if (count > MAX_TYPES) {
   1748 +			count = MAX_TYPES;
   1749 +		}
   1750 +		for (i = 0; i < count; i++) {
   1751 +			types[i] = data_type_at(source, i);
   1752 +		}
   1753 +	}
   1754 +	selection.handler->changed(selection.data, types, count);
   1755 +}
   1756 +
   1757 +EXPORT void
   1758 +swc_selection_set_handler(const struct swc_selection_handler *handler, void *data)
   1759 +{
   1760 +	selection.handler = handler;
   1761 +	selection.data = data;
   1762 +	if (!selection.hooked && swc.seat) {
   1763 +		selection.listener.notify = handle_event;
   1764 +		wl_signal_add(&swc.seat->data_device->event_signal, &selection.listener);
   1765 +		selection.hooked = true;
   1766 +	}
   1767 +}
   1768 +
   1769 +EXPORT bool
   1770 +swc_selection_request(const char *mime_type, int fd)
   1771 +{
   1772 +	struct data *source = data_device_client_selection(swc.seat->data_device);
   1773 +	unsigned i;
   1774 +
   1775 +	if (!source) {
   1776 +		return false;
   1777 +	}
   1778 +	for (i = 0; i < data_type_count(source); i++) {
   1779 +		if (strcmp(data_type_at(source, i), mime_type) == 0) {
   1780 +			data_request(source, mime_type, fd);
   1781 +			return true;
   1782 +		}
   1783 +	}
   1784 +	return false;
   1785 +}
   1786 +
   1787 +EXPORT bool
   1788 +swc_selection_offer(const char *const *mime_types, unsigned count,
   1789 +                    const struct swc_selection_source *source, void *data)
   1790 +{
   1791 +	struct data_host host = { .send = source->send, .cancelled = source->cancelled };
   1792 +	struct data *offered = data_host_new(mime_types, count, &host, data);
   1793 +
   1794 +	if (!offered) {
   1795 +		return false;
   1796 +	}
   1797 +	data_device_set_host(swc.seat->data_device, offered);
   1798 +	return true;
   1799 +}