neuswc

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

seat.c (19761B)


      1 /* swc: libswc/seat.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 "seat.h"
     25 #include "backend.h"
     26 #include "compositor.h"
     27 #include "data_device.h"
     28 #include "event.h"
     29 #include "internal.h"
     30 #include "keyboard.h"
     31 #include "launch.h"
     32 #include "pointer.h"
     33 #include "screen.h"
     34 #include "surface.h"
     35 #include "touch.h"
     36 #include "util.h"
     37 
     38 #include <dirent.h>
     39 #include <errno.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 
     45 #include <libinput.h>
     46 #if defined(__linux__)
     47 #include <linux/input.h>
     48 #elif defined(__FreeBSD__)
     49 #include <dev/evdev/input.h> /* mirrors linux/input.h functionality */
     50 #endif
     51 #ifdef ENABLE_LIBUDEV
     52 #include <libudev.h>
     53 #endif
     54 
     55 /* The netlink groups libinput listens on for devices that appear: a bit
     56  * for each group, and group 1 is the kernel's own uevents, which are there
     57  * with no device manager running. Another group hears nothing unless
     58  * something forwards to it, and a device that appears after the compositor
     59  * started (a touchpad whose driver binds late, a USB keyboard) is then not
     60  * seen until the seat is resumed. */
     61 #ifndef NETLINK_MASK
     62 #define NETLINK_MASK 1
     63 #endif
     64 
     65 struct seat {
     66 	struct swc_seat base;
     67 
     68 	char *name;
     69 	uint32_t capabilities;
     70 
     71 	struct libinput *libinput;
     72 	struct wl_event_source *libinput_source;
     73 
     74 #ifdef ENABLE_LIBUDEV
     75 	struct udev *udev;
     76 #endif
     77 
     78 	struct wl_listener swc_listener;
     79 
     80 	struct wl_listener keyboard_focus_listener;
     81 	struct pointer pointer;
     82 	struct wl_listener data_device_listener;
     83 
     84 	struct wl_global *global;
     85 	struct wl_list resources;
     86 };
     87 
     88 static void
     89 handle_keyboard_focus_event(struct wl_listener *listener, void *data)
     90 {
     91 	struct seat *seat =
     92 	    wl_container_of(listener, seat, keyboard_focus_listener);
     93 	struct event *ev = data;
     94 	struct input_focus_event_data *event_data = ev->data;
     95 
     96 	if (ev->type != INPUT_FOCUS_EVENT_CHANGED) {
     97 		return;
     98 	}
     99 
    100 	if (event_data->new) {
    101 		struct wl_client *client =
    102 		    wl_resource_get_client(event_data->new->surface->resource);
    103 
    104 		/* Offer the selection to the new focus. */
    105 		data_device_offer_selection(seat->base.data_device, client);
    106 	}
    107 }
    108 
    109 static void
    110 handle_data_device_event(struct wl_listener *listener, void *data)
    111 {
    112 	struct seat *seat = wl_container_of(listener, seat, data_device_listener);
    113 	struct event *ev = data;
    114 
    115 	if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED) {
    116 		return;
    117 	}
    118 
    119 	if (seat->base.keyboard->focus.client) {
    120 		data_device_offer_selection(seat->base.data_device,
    121 		                            seat->base.keyboard->focus.client);
    122 	}
    123 }
    124 
    125 static void
    126 handle_swc_event(struct wl_listener *listener, void *data)
    127 {
    128 	struct seat *seat = wl_container_of(listener, seat, swc_listener);
    129 	struct event *ev = data;
    130 
    131 	switch (ev->type) {
    132 	case SWC_EVENT_DEACTIVATED:
    133 		if (seat->libinput) {
    134 			libinput_suspend(seat->libinput);
    135 		}
    136 		keyboard_reset(seat->base.keyboard);
    137 		break;
    138 	case SWC_EVENT_ACTIVATED:
    139 		if (seat->libinput && libinput_resume(seat->libinput) != 0) {
    140 			WARNING("Failed to resume libinput context\n");
    141 		}
    142 		break;
    143 	}
    144 }
    145 
    146 /* Wayland Seat Interface */
    147 static void
    148 get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id)
    149 {
    150 	struct seat *seat = wl_resource_get_user_data(resource);
    151 
    152 	if (!pointer_bind(&seat->pointer, client, wl_resource_get_version(resource),
    153 	                  id)) {
    154 		wl_resource_post_no_memory(resource);
    155 	}
    156 }
    157 
    158 static void
    159 get_keyboard(struct wl_client *client, struct wl_resource *resource,
    160              uint32_t id)
    161 {
    162 	struct seat *seat = wl_resource_get_user_data(resource);
    163 
    164 	if (!keyboard_bind(seat->base.keyboard, client,
    165 	                   wl_resource_get_version(resource), id)) {
    166 		wl_resource_post_no_memory(resource);
    167 	}
    168 }
    169 
    170 static void
    171 get_touch(struct wl_client *client, struct wl_resource *resource, uint32_t id)
    172 {
    173 	struct seat *seat = wl_resource_get_user_data(resource);
    174 
    175 	if (!touch_bind(seat->base.touch, client, wl_resource_get_version(resource),
    176 	                id)) {
    177 		wl_resource_post_no_memory(resource);
    178 	}
    179 }
    180 
    181 static const struct wl_seat_interface seat_impl = {
    182 	.get_pointer = get_pointer,
    183 	.get_keyboard = get_keyboard,
    184 	.get_touch = get_touch,
    185 	.release = destroy_resource,
    186 };
    187 
    188 static void
    189 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
    190 {
    191 	struct seat *seat = data;
    192 	struct wl_resource *resource;
    193 
    194 	resource = wl_resource_create(client, &wl_seat_interface, version, id);
    195 	if (!resource) {
    196 		wl_client_post_no_memory(client);
    197 		return;
    198 	}
    199 	wl_resource_set_implementation(resource, &seat_impl, seat,
    200 	                               &remove_resource);
    201 	wl_list_insert(&seat->resources, wl_resource_get_link(resource));
    202 
    203 	if (version >= 2) {
    204 		wl_seat_send_name(resource, seat->name);
    205 	}
    206 
    207 	wl_seat_send_capabilities(resource, seat->capabilities);
    208 }
    209 
    210 static void
    211 update_capabilities(struct seat *seat, uint32_t capabilities)
    212 {
    213 	struct wl_resource *resource;
    214 
    215 	if (!(~seat->capabilities & capabilities)) {
    216 		return;
    217 	}
    218 
    219 	seat->capabilities |= capabilities;
    220 	wl_list_for_each(resource, &seat->resources, link)
    221 	    wl_seat_send_capabilities(resource, seat->capabilities);
    222 }
    223 
    224 static int
    225 open_restricted(const char *path, int flags, void *user_data)
    226 {
    227 	return launch_open_device(path, flags);
    228 }
    229 
    230 static void
    231 close_restricted(int fd, void *user_data)
    232 {
    233 	close(fd);
    234 }
    235 
    236 const struct libinput_interface libinput_interface = {
    237     .open_restricted = open_restricted,
    238     .close_restricted = close_restricted,
    239 };
    240 
    241 static uint32_t
    242 device_capabilities(struct libinput_device *device)
    243 {
    244 	uint32_t capabilities = 0;
    245 
    246 	if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
    247 		capabilities |= WL_SEAT_CAPABILITY_KEYBOARD;
    248 	}
    249 	/* a tablet's pen drives the pointer (see tablet_tool below) */
    250 	if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER) ||
    251 	    libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
    252 		capabilities |= WL_SEAT_CAPABILITY_POINTER;
    253 	}
    254 	/* a finger drives the pointer where no client takes it (touch.c), so a
    255 	 * touchscreen is a pointer too */
    256 	if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) {
    257 		capabilities |= WL_SEAT_CAPABILITY_TOUCH | WL_SEAT_CAPABILITY_POINTER;
    258 	}
    259 
    260 	return capabilities;
    261 }
    262 
    263 static int
    264 handle_libinput_data(int fd, uint32_t mask, void *data)
    265 {
    266 	struct seat *seat = data;
    267 	struct screen *screen;
    268 	struct swc_rectangle *rect;
    269 	struct libinput_event *generic_event;
    270 	struct libinput_device *device;
    271 	union {
    272 		struct libinput_event_keyboard *k;
    273 		struct libinput_event_pointer *p;
    274 		struct libinput_event_tablet_tool *t;
    275 		struct libinput_event_touch *touch;
    276 	} event;
    277 	wl_fixed_t x, y, value;
    278 	uint32_t time, key, state;
    279 	enum wl_pointer_axis_source source;
    280 	int value120;
    281 
    282 	if (libinput_dispatch(seat->libinput) != 0) {
    283 		WARNING("libinput_dispatch failed: %s\n", strerror(errno));
    284 		return 0;
    285 	}
    286 
    287 	while ((generic_event = libinput_get_event(seat->libinput))) {
    288 		switch (libinput_event_get_type(generic_event)) {
    289 		case LIBINPUT_EVENT_DEVICE_ADDED:
    290 			device = libinput_event_get_device(generic_event);
    291 			update_capabilities(seat, device_capabilities(device));
    292 			if (swc.manager->new_device) {
    293 				swc.manager->new_device(device);
    294 			}
    295 			break;
    296 		case LIBINPUT_EVENT_TOUCH_DOWN:
    297 		case LIBINPUT_EVENT_TOUCH_MOTION:
    298 			/* the panel's own coordinates are the first screen's */
    299 			screen = wl_container_of(swc.screens.next, screen, link);
    300 			rect = &screen->base.geometry;
    301 			event.touch = libinput_event_get_touch_event(generic_event);
    302 			time = libinput_event_touch_get_time(event.touch);
    303 			x = wl_fixed_from_double(rect->x +
    304 			    libinput_event_touch_get_x_transformed(event.touch, rect->width));
    305 			y = wl_fixed_from_double(rect->y +
    306 			    libinput_event_touch_get_y_transformed(event.touch, rect->height));
    307 			if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) {
    308 				touch_handle_down(seat->base.touch, time,
    309 				                  libinput_event_touch_get_seat_slot(event.touch), x, y);
    310 			} else {
    311 				touch_handle_motion(seat->base.touch, time,
    312 				                    libinput_event_touch_get_seat_slot(event.touch), x, y);
    313 			}
    314 			break;
    315 		case LIBINPUT_EVENT_TOUCH_UP:
    316 			event.touch = libinput_event_get_touch_event(generic_event);
    317 			touch_handle_up(seat->base.touch, libinput_event_touch_get_time(event.touch),
    318 			                libinput_event_touch_get_seat_slot(event.touch));
    319 			break;
    320 		case LIBINPUT_EVENT_TOUCH_FRAME:
    321 			touch_handle_frame(seat->base.touch);
    322 			break;
    323 		case LIBINPUT_EVENT_TOUCH_CANCEL:
    324 			touch_handle_cancel(seat->base.touch);
    325 			break;
    326 		case LIBINPUT_EVENT_KEYBOARD_KEY:
    327 			event.k = libinput_event_get_keyboard_event(generic_event);
    328 			time = libinput_event_keyboard_get_time(event.k);
    329 			key = libinput_event_keyboard_get_key(event.k);
    330 			state = libinput_event_keyboard_get_key_state(event.k);
    331 			keyboard_handle_key(seat->base.keyboard, time, key, state);
    332 			break;
    333 		case LIBINPUT_EVENT_POINTER_MOTION:
    334 			event.p = libinput_event_get_pointer_event(generic_event);
    335 			time = libinput_event_pointer_get_time(event.p);
    336 			x = wl_fixed_from_double(libinput_event_pointer_get_dx(event.p));
    337 			y = wl_fixed_from_double(libinput_event_pointer_get_dy(event.p));
    338 			pointer_handle_relative_motion(&seat->pointer, time, x, y);
    339 			pointer_handle_frame(&seat->pointer);
    340 			break;
    341 		case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
    342 			screen = wl_container_of(swc.screens.next, screen, link);
    343 			rect = &screen->base.geometry;
    344 			event.p = libinput_event_get_pointer_event(generic_event);
    345 			time = libinput_event_pointer_get_time(event.p);
    346 			x = wl_fixed_from_double(
    347 			    libinput_event_pointer_get_absolute_x_transformed(event.p,
    348 			                                                      rect->width));
    349 			y = wl_fixed_from_double(
    350 			    libinput_event_pointer_get_absolute_y_transformed(
    351 			        event.p, rect->height));
    352 			pointer_handle_absolute_motion(&seat->pointer, time, x, y);
    353 			pointer_handle_frame(&seat->pointer);
    354 			break;
    355 		/* A tablet's pen as the pointer until clients are told of tablets:
    356 		 * where it points, its tip the left button and its barrel's two
    357 		 * buttons the middle and right ones, as a mouse would have them. */
    358 		case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
    359 		case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
    360 		case LIBINPUT_EVENT_TABLET_TOOL_TIP:
    361 			screen = wl_container_of(swc.screens.next, screen, link);
    362 			rect = &screen->base.geometry;
    363 			event.t = libinput_event_get_tablet_tool_event(generic_event);
    364 			time = libinput_event_tablet_tool_get_time(event.t);
    365 			x = wl_fixed_from_double(
    366 			    libinput_event_tablet_tool_get_x_transformed(event.t, rect->width));
    367 			y = wl_fixed_from_double(
    368 			    libinput_event_tablet_tool_get_y_transformed(event.t, rect->height));
    369 			pointer_handle_absolute_motion(&seat->pointer, time, x, y);
    370 			if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TABLET_TOOL_TIP) {
    371 				pointer_handle_button(&seat->pointer, time, BTN_LEFT,
    372 				                      libinput_event_tablet_tool_get_tip_state(event.t) ==
    373 				                              LIBINPUT_TABLET_TOOL_TIP_DOWN
    374 				                          ? WL_POINTER_BUTTON_STATE_PRESSED
    375 				                          : WL_POINTER_BUTTON_STATE_RELEASED);
    376 			}
    377 			pointer_handle_frame(&seat->pointer);
    378 			break;
    379 		case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
    380 			event.t = libinput_event_get_tablet_tool_event(generic_event);
    381 			time = libinput_event_tablet_tool_get_time(event.t);
    382 			key = libinput_event_tablet_tool_get_button(event.t);
    383 			key = key == BTN_STYLUS ? BTN_MIDDLE : key == BTN_STYLUS2 ? BTN_RIGHT : key;
    384 			pointer_handle_button(&seat->pointer, time, key,
    385 			                      libinput_event_tablet_tool_get_button_state(event.t) ==
    386 			                              LIBINPUT_BUTTON_STATE_PRESSED
    387 			                          ? WL_POINTER_BUTTON_STATE_PRESSED
    388 			                          : WL_POINTER_BUTTON_STATE_RELEASED);
    389 			pointer_handle_frame(&seat->pointer);
    390 			break;
    391 		case LIBINPUT_EVENT_POINTER_BUTTON:
    392 			event.p = libinput_event_get_pointer_event(generic_event);
    393 			time = libinput_event_pointer_get_time(event.p);
    394 			key = libinput_event_pointer_get_button(event.p);
    395 			state = libinput_event_pointer_get_button_state(event.p);
    396 			pointer_handle_button(&seat->pointer, time, key, state);
    397 			if (state == LIBINPUT_BUTTON_STATE_PRESSED) {
    398 				/* qemu generates GEAR_UP/GEAR_DOWN events on scroll, so pass
    399 				 * those through as axis events. */
    400 				source = WL_POINTER_AXIS_SOURCE_WHEEL;
    401 				switch (key) {
    402 				case BTN_GEAR_DOWN:
    403 					pointer_handle_axis(&seat->pointer, time,
    404 					                    WL_POINTER_AXIS_VERTICAL_SCROLL, source,
    405 					                    wl_fixed_from_int(10), 120);
    406 					break;
    407 				case BTN_GEAR_UP:
    408 					pointer_handle_axis(&seat->pointer, time,
    409 					                    WL_POINTER_AXIS_VERTICAL_SCROLL, source,
    410 					                    wl_fixed_from_int(-10), -120);
    411 					break;
    412 				}
    413 			}
    414 			pointer_handle_frame(&seat->pointer);
    415 			break;
    416 		case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
    417 			source = WL_POINTER_AXIS_SOURCE_WHEEL;
    418 			goto scroll;
    419 		case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
    420 			source = WL_POINTER_AXIS_SOURCE_FINGER;
    421 			goto scroll;
    422 		case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
    423 			source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
    424 			goto scroll;
    425 		scroll:
    426 			event.p = libinput_event_get_pointer_event(generic_event);
    427 			time = libinput_event_pointer_get_time(event.p);
    428 			value120 = 0;
    429 			if (libinput_event_pointer_has_axis(
    430 			        event.p, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
    431 				value = wl_fixed_from_double(
    432 				    libinput_event_pointer_get_scroll_value(
    433 				        event.p, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL));
    434 				if (source == WL_POINTER_AXIS_SOURCE_WHEEL) {
    435 					value120 = libinput_event_pointer_get_scroll_value_v120(
    436 					    event.p, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
    437 				}
    438 				pointer_handle_axis(&seat->pointer, time,
    439 				                    WL_POINTER_AXIS_VERTICAL_SCROLL, source,
    440 				                    value, value120);
    441 			}
    442 			if (libinput_event_pointer_has_axis(
    443 			        event.p, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
    444 				value = wl_fixed_from_double(
    445 				    libinput_event_pointer_get_scroll_value(
    446 				        event.p, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL));
    447 				if (source == WL_POINTER_AXIS_SOURCE_WHEEL) {
    448 					value120 = libinput_event_pointer_get_scroll_value_v120(
    449 					    event.p, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
    450 				}
    451 				pointer_handle_axis(&seat->pointer, time,
    452 				                    WL_POINTER_AXIS_HORIZONTAL_SCROLL, source,
    453 				                    value, value120);
    454 			}
    455 			pointer_handle_frame(&seat->pointer);
    456 			break;
    457 		default:
    458 			break;
    459 		}
    460 
    461 		libinput_event_destroy(generic_event);
    462 	}
    463 
    464 	return 0;
    465 }
    466 
    467 bool
    468 initialize_libinput(struct seat *seat)
    469 {
    470 #ifdef ENABLE_LIBUDEV
    471 	if (!(seat->udev = udev_new())) {
    472 		ERROR("Could not create udev context\n");
    473 		goto error0;
    474 	}
    475 
    476 	seat->libinput =
    477 	    libinput_udev_create_context(&libinput_interface, NULL, seat->udev);
    478 #else
    479 	seat->libinput = libinput_netlink_create_context(&libinput_interface, NULL,
    480 	                                                 NETLINK_MASK);
    481 #endif
    482 
    483 	if (!seat->libinput) {
    484 		ERROR("Could not create libinput context\n");
    485 		goto error1;
    486 	}
    487 
    488 #ifdef ENABLE_LIBUDEV
    489 	if (libinput_udev_assign_seat(seat->libinput, seat->name) != 0) {
    490 		ERROR("Failed to assign seat to libinput context\n");
    491 		goto error2;
    492 	}
    493 #else
    494 	if (libinput_netlink_assign_seat(seat->libinput, seat->name) != 0) {
    495 		ERROR("Failed to assign seat to libinput context\n");
    496 		goto error2;
    497 	}
    498 #endif
    499 
    500 	seat->libinput_source =
    501 	    wl_event_loop_add_fd(swc.event_loop, libinput_get_fd(seat->libinput),
    502 	                         WL_EVENT_READABLE, &handle_libinput_data, seat);
    503 	if (!seat->libinput_source) {
    504 		ERROR("Could not create event source for libinput\n");
    505 		goto error2;
    506 	}
    507 
    508 	if (!swc.active) {
    509 		libinput_suspend(seat->libinput);
    510 	}
    511 
    512 	return true;
    513 
    514 error2:
    515 	libinput_unref(seat->libinput);
    516 error1:
    517 #ifdef ENABLE_LIBUDEV
    518 	udev_unref(seat->udev);
    519 error0:
    520 #endif
    521 	return false;
    522 }
    523 
    524 struct swc_seat *
    525 seat_create(struct wl_display *display, const char *seat_name)
    526 {
    527 	struct seat *seat;
    528 
    529 	seat = malloc(sizeof(*seat));
    530 	if (!seat) {
    531 		goto error0;
    532 	}
    533 	seat->name = strdup(seat_name);
    534 	if (!seat->name) {
    535 		ERROR("Could not allocate seat name string\n");
    536 		goto error1;
    537 	}
    538 	seat->global =
    539 	    wl_global_create(display, &wl_seat_interface, 8, seat, &bind_seat);
    540 	if (!seat->global) {
    541 		goto error2;
    542 	}
    543 	seat->capabilities = 0;
    544 	wl_list_init(&seat->resources);
    545 
    546 	seat->swc_listener.notify = &handle_swc_event;
    547 	wl_signal_add(&swc.event_signal, &seat->swc_listener);
    548 
    549 	seat->base.data_device = data_device_create();
    550 	if (!seat->base.data_device) {
    551 		ERROR("Could not initialize data device\n");
    552 		goto error3;
    553 	}
    554 	seat->data_device_listener.notify = &handle_data_device_event;
    555 	wl_signal_add(&seat->base.data_device->event_signal,
    556 	              &seat->data_device_listener);
    557 
    558 	seat->base.keyboard = keyboard_create(NULL);
    559 	if (!seat->base.keyboard) {
    560 		ERROR("Could not initialize keyboard\n");
    561 		goto error4;
    562 	}
    563 	seat->keyboard_focus_listener.notify = handle_keyboard_focus_event;
    564 	wl_signal_add(&seat->base.keyboard->focus.event_signal,
    565 	              &seat->keyboard_focus_listener);
    566 
    567 	if (!pointer_initialize(&seat->pointer)) {
    568 		ERROR("Could not initialize pointer\n");
    569 		goto error5;
    570 	}
    571 	seat->base.pointer = &seat->pointer;
    572 
    573 	if (!(seat->base.touch = touch_create(&seat->pointer))) {
    574 		ERROR("Could not initialize touch\n");
    575 		goto error6;
    576 	}
    577 
    578 	seat->libinput = NULL;
    579 	seat->libinput_source = NULL;
    580 	if (swc.backend->virtual_input) {
    581 		update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD |
    582 		                              WL_SEAT_CAPABILITY_POINTER |
    583 		                              (swc.backend->virtual_touch
    584 		                                   ? WL_SEAT_CAPABILITY_TOUCH
    585 		                                   : 0));
    586 	} else if (!initialize_libinput(seat)) {
    587 		goto error7;
    588 	}
    589 
    590 	return &seat->base;
    591 
    592 error7:
    593 	touch_destroy(seat->base.touch);
    594 error6:
    595 	pointer_finalize(&seat->pointer);
    596 error5:
    597 	keyboard_destroy(seat->base.keyboard);
    598 error4:
    599 	data_device_destroy(seat->base.data_device);
    600 error3:
    601 	wl_global_destroy(seat->global);
    602 error2:
    603 	free(seat->name);
    604 error1:
    605 	free(seat);
    606 error0:
    607 	return NULL;
    608 }
    609 
    610 void
    611 seat_destroy(struct swc_seat *seat_base)
    612 {
    613 	struct seat *seat = wl_container_of(seat_base, seat, base);
    614 
    615 	if (seat->libinput) {
    616 		wl_event_source_remove(seat->libinput_source);
    617 		libinput_unref(seat->libinput);
    618 #ifdef ENABLE_LIBUDEV
    619 		udev_unref(seat->udev);
    620 #endif
    621 	}
    622 
    623 	touch_destroy(seat->base.touch);
    624 	pointer_finalize(&seat->pointer);
    625 	keyboard_destroy(seat->base.keyboard);
    626 	data_device_destroy(seat->base.data_device);
    627 
    628 	wl_global_destroy(seat->global);
    629 	free(seat->name);
    630 	free(seat);
    631 }