xf86libinput.c 128 KB
Newer Older
1
/*
Peter Hutterer's avatar
Peter Hutterer committed
2
 * Copyright © 2013-2015 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Red Hat
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  Red
 * Hat makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <xorg-server.h>
Peter Hutterer's avatar
Peter Hutterer committed
33
#include <list.h>
34
#include <exevents.h>
35
#include <xkbsrv.h>
36
#include <xf86.h>
37
#include <xf86Xinput.h>
38
#include <xf86_OSproc.h>
39 40 41 42
#include <xserver-properties.h>
#include <libinput.h>
#include <linux/input.h>

43 44
#include <X11/Xatom.h>

Peter Hutterer's avatar
Peter Hutterer committed
45
#include "draglock.h"
46 47
#include "libinput-properties.h"

Peter Hutterer's avatar
Peter Hutterer committed
48 49 50 51
#ifndef XI86_SERVER_FD
#define XI86_SERVER_FD 0x20
#endif

52 53 54 55 56 57
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) * 1000 + GET_ABI_MINOR(ABI_XINPUT_VERSION) > 22000
#define HAVE_VMASK_UNACCEL 1
#else
#undef HAVE_VMASK_UNACCEL
#endif

58 59 60 61
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 23
#define HAVE_THREADED_INPUT	1
#endif

62
#define TOUCHPAD_NUM_AXES 4 /* x, y, hscroll, vscroll */
63
#define TABLET_NUM_BUTTONS 7 /* we need scroll buttons */
Peter Hutterer's avatar
Peter Hutterer committed
64
#define TOUCH_MAX_SLOTS 15
65 66
#define XORG_KEYCODE_OFFSET 8

Peter Hutterer's avatar
Peter Hutterer committed
67 68 69 70 71 72 73
/*
   libinput does not provide axis information for absolute devices, instead
   it scales into the screen dimensions provided. So we set up the axes with
   a fixed range, let libinput scale into that range and then the server
   do the scaling it usually does.
 */
#define TOUCH_AXIS_MAX 0xffff
74 75 76
#define TABLET_AXIS_MAX 0xffffff
#define TABLET_PRESSURE_AXIS_MAX 2047
#define TABLET_TILT_AXIS_MAX 64
Peter Hutterer's avatar
Peter Hutterer committed
77 78
#define TABLET_STRIP_AXIS_MAX 4096
#define TABLET_RING_AXIS_MAX 71
79

80 81 82
#define CAP_KEYBOARD	0x1
#define CAP_POINTER	0x2
#define CAP_TOUCH	0x4
83 84
#define CAP_TABLET	0x8
#define CAP_TABLET_TOOL	0x10
Peter Hutterer's avatar
Peter Hutterer committed
85
#define CAP_TABLET_PAD	0x20
86

Peter Hutterer's avatar
Peter Hutterer committed
87 88
struct xf86libinput_driver {
	struct libinput *libinput;
89
	int device_enabled_count;
90
	void *registered_InputInfoPtr;
Peter Hutterer's avatar
Peter Hutterer committed
91 92 93 94
};

static struct xf86libinput_driver driver_context;

95 96 97 98 99 100 101
struct xf86libinput_device {
	int refcount;
	int enabled_count;
	uint32_t id;
	struct libinput_device *device;
	struct xorg_list device_list;
	int server_fd;
102 103 104 105

	struct xorg_list unclaimed_tablet_tool_list;
};

106 107 108 109 110 111 112 113 114 115
struct xf86libinput_tablet_tool_queued_event {
	struct xorg_list node;
	struct libinput_event_tablet_tool *event;
};

struct xf86libinput_tablet_tool_event_queue {
	bool need_to_queue;
	struct xorg_list event_list;
};

116 117 118
struct xf86libinput_tablet_tool {
	struct xorg_list node;
	struct libinput_tablet_tool *tool;
119 120
};

121
struct xf86libinput {
122
	InputInfoPtr pInfo;
123
	char *path;
124
	uint32_t capabilities;
125

126 127 128 129
	struct {
		int vdist;
		int hdist;
	} scroll;
130 131 132 133 134 135 136

	struct {
		double x;
		double y;
		double x_remainder;
		double y_remainder;
	} scale;
137

138 139
	BOOL has_abs;

140
	ValuatorMask *valuators;
141
	ValuatorMask *valuators_unaccelerated;
142

143
	struct options {
144
		BOOL tapping;
145
		BOOL tap_drag;
146
		BOOL tap_drag_lock;
147
		enum libinput_config_tap_button_map tap_button_map;
148
		BOOL natural_scrolling;
149
		BOOL left_handed;
150
		BOOL middle_emulation;
151
		BOOL disable_while_typing;
152
		CARD32 sendevents;
153
		CARD32 scroll_button; /* xorg button number */
154 155
		float speed;
		float matrix[9];
156
		enum libinput_config_scroll_method scroll_method;
157
		enum libinput_config_click_method click_method;
158
		enum libinput_config_accel_profile accel_profile;
159 160

		unsigned char btnmap[MAX_BUTTONS + 1];
161 162

		BOOL horiz_scrolling_enabled;
163 164

		float rotation_angle;
165
	} options;
Peter Hutterer's avatar
Peter Hutterer committed
166 167

	struct draglock draglock;
168 169 170

	struct xf86libinput_device *shared_device;
	struct xorg_list shared_device_link;
171 172

	struct libinput_tablet_tool *tablet_tool;
173 174

	bool allow_mode_group_updates;
175 176
};

177 178 179
enum event_handling {
	EVENT_QUEUED,
	EVENT_HANDLED,
180 181
};

182
static void
183 184 185
xf86libinput_create_subdevice(InputInfoPtr pInfo,
			      uint32_t capabilities,
			      XF86OptionPtr extra_opts);
186 187 188
static inline void
update_mode_prop(InputInfoPtr pInfo,
		 struct libinput_event_tablet_pad *event);
189

190 191 192
static enum event_handling
xf86libinput_handle_event(struct libinput_event *event);

Peter Hutterer's avatar
Peter Hutterer committed
193 194 195 196 197
static inline int
use_server_fd(const InputInfoPtr pInfo) {
	return pInfo->fd > -1 && (pInfo->flags & XI86_SERVER_FD);
}

198 199 200 201 202 203 204 205 206 207
static inline unsigned int
btn_linux2xorg(unsigned int b)
{
	unsigned int button;

	switch(b) {
	case 0: button = 0; break;
	case BTN_LEFT: button = 1; break;
	case BTN_MIDDLE: button = 2; break;
	case BTN_RIGHT: button = 3; break;
208 209 210
	/* tablet button range */
	case BTN_STYLUS: button = 2; break;
	case BTN_STYLUS2: button = 3; break;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	default:
		button = 8 + b - BTN_SIDE;
		break;
	}

	return button;
}
static inline unsigned int
btn_xorg2linux(unsigned int b)
{
	unsigned int button;

	switch(b) {
	case 0: button = 0; break;
	case 1: button = BTN_LEFT; break;
	case 2: button = BTN_MIDDLE; break;
	case 3: button = BTN_RIGHT; break;
	default:
		button = b - 8 + BTN_SIDE;
		break;
	}

	return button;
}

236 237 238 239 240 241 242 243 244 245 246 247 248
static BOOL
xf86libinput_is_subdevice(InputInfoPtr pInfo)
{
	char *source;
	BOOL is_subdevice;

	source = xf86SetStrOption(pInfo->options, "_source", "");
	is_subdevice = strcmp(source, "_driver/libinput") == 0;
	free(source);

	return is_subdevice;
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262
static inline InputInfoPtr
xf86libinput_get_parent(InputInfoPtr pInfo)
{
	InputInfoPtr parent;
	int parent_id;

	parent_id = xf86CheckIntOption(pInfo->options, "_libinput/shared-device", -1);
	if (parent_id == -1)
		return NULL;

	nt_list_for_each_entry(parent, xf86FirstLocalDevice(), next) {
		int id = xf86CheckIntOption(parent->options,
					    "_libinput/shared-device",
					    -1);
263
		if (id == parent_id && !xf86libinput_is_subdevice(parent))
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			return parent;
	}

	return NULL;
}

static inline struct xf86libinput_device*
xf86libinput_shared_create(struct libinput_device *device)
{
	static uint32_t next_shared_device_id;
	struct xf86libinput_device *shared_device;

	shared_device = calloc(1, sizeof(*shared_device));
	if (!shared_device)
		return NULL;

	shared_device->device = device;
	shared_device->refcount = 1;
	shared_device->id = ++next_shared_device_id;
	xorg_list_init(&shared_device->device_list);
284
	xorg_list_init(&shared_device->unclaimed_tablet_tool_list);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

	return shared_device;
}

static inline struct xf86libinput_device*
xf86libinput_shared_ref(struct xf86libinput_device *shared_device)
{
	shared_device->refcount++;

	return shared_device;
}

static inline struct xf86libinput_device*
xf86libinput_shared_unref(struct xf86libinput_device *shared_device)
{
	shared_device->refcount--;

	if (shared_device->refcount > 0)
		return shared_device;

	free(shared_device);

	return NULL;
}

static inline struct libinput_device *
xf86libinput_shared_enable(InputInfoPtr pInfo,
			   struct xf86libinput_device *shared_device,
			   const char *path)
{
	struct libinput_device *device;
	struct libinput *libinput = driver_context.libinput;

	/* With systemd-logind the server requests the fd from logind, sets
	 * pInfo->fd and sets the "fd" option to the fd number.
	 *
	 * If we have a second device that uses the same path, the server
	 * checks all pInfo->major/minor for a match and returns the matched
	 * device's pInfo->fd. In this driver, this fd is the epollfd, not
	 * the actual device. This causes troubles when removing the
	 * device.
	 *
	 * What we need to do here is: after enabling the device the first
	 * time extract the real fd and store it in the shared device
	 * struct. The second device replaces the pInfo->options "fd" with
	 * the real fd we're using.
	 *
	 * When the device is unplugged, the server now correctly finds two
	 * devices on the real fd and releases them in order.
	 */
	shared_device->enabled_count++;
	if (shared_device->enabled_count > 1) {
		if (pInfo->flags & XI86_SERVER_FD) {
			pInfo->options = xf86ReplaceIntOption(pInfo->options,
							      "fd",
							      shared_device->server_fd);
		}

		return shared_device->device;
	}

	device = libinput_path_add_device(libinput, path);
	if (!device)
		return NULL;

	libinput_device_set_user_data(device, shared_device);
	shared_device->device = libinput_device_ref(device);

	if (pInfo->flags & XI86_SERVER_FD)
		shared_device->server_fd = xf86CheckIntOption(pInfo->options,
							      "fd",
							      -1);
	return device;
}

static inline void
xf86libinput_shared_disable(struct xf86libinput_device *shared_device)
{
	struct libinput_device *device = shared_device->device;

	shared_device->enabled_count--;

	if (shared_device->enabled_count > 0)
		return;

	if (!device)
		return;

	libinput_device_set_user_data(device, NULL);
	libinput_path_remove_device(device);
	device = libinput_device_unref(device);
	shared_device->device = NULL;
}

static inline bool
xf86libinput_shared_is_enabled(struct xf86libinput_device *shared_device)
{
	return shared_device->enabled_count > 0;
}

385 386 387 388 389 390
static int
LibinputSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
                 BOOL checkonly);
static void
LibinputInitProperty(DeviceIntPtr dev);

391 392 393 394 395
static inline void
LibinputApplyConfig(DeviceIntPtr dev)
{
	InputInfoPtr pInfo = dev->public.devicePrivate;
	struct xf86libinput *driver_data = pInfo->private;
396
	struct libinput_device *device = driver_data->shared_device->device;
397
	unsigned int scroll_button;
398 399 400 401 402

	if (libinput_device_config_send_events_get_modes(device) != LIBINPUT_CONFIG_SEND_EVENTS_ENABLED &&
	    libinput_device_config_send_events_set_mode(device,
							driver_data->options.sendevents) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
403
			    "Failed to set SendEventsMode %u\n",
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
			    driver_data->options.sendevents);

	if (libinput_device_config_scroll_has_natural_scroll(device) &&
	    libinput_device_config_scroll_set_natural_scroll_enabled(device,
								     driver_data->options.natural_scrolling) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set NaturalScrolling to %d\n",
			    driver_data->options.natural_scrolling);

	if (libinput_device_config_accel_is_available(device) &&
	    libinput_device_config_accel_set_speed(device,
						   driver_data->options.speed) != LIBINPUT_CONFIG_STATUS_SUCCESS)
			xf86IDrvMsg(pInfo, X_ERROR,
				    "Failed to set speed %.2f\n",
				    driver_data->options.speed);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

	if (libinput_device_config_accel_get_profiles(device) &&
	    driver_data->options.accel_profile != LIBINPUT_CONFIG_ACCEL_PROFILE_NONE  &&
	    libinput_device_config_accel_set_profile(device,
						     driver_data->options.accel_profile) !=
			    LIBINPUT_CONFIG_STATUS_SUCCESS) {
		const char *profile;

		switch (driver_data->options.accel_profile) {
		case LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE:
			profile = "adaptive";
			break;
		case LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT:
			profile = "flat";
			break;
		default:
			profile = "unknown";
			break;
		}
		xf86IDrvMsg(pInfo, X_ERROR, "Failed to set profile %s\n", profile);
	}

441 442 443 444 445 446 447
	if (libinput_device_config_tap_get_finger_count(device) > 0 &&
	    libinput_device_config_tap_set_enabled(device,
						   driver_data->options.tapping) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set Tapping to %d\n",
			    driver_data->options.tapping);

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	if (libinput_device_config_tap_get_finger_count(device) > 0 &&
	    libinput_device_config_tap_set_button_map(device,
						      driver_data->options.tap_button_map) != LIBINPUT_CONFIG_STATUS_SUCCESS) {
		const char *map;

		switch(driver_data->options.tap_button_map) {
		case LIBINPUT_CONFIG_TAP_MAP_LRM: map = "lrm"; break;
		case LIBINPUT_CONFIG_TAP_MAP_LMR: map = "lmr"; break;
		default: map = "unknown"; break;
		}
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set Tapping ButtonMap to %s\n",
			    map);
	}

463 464 465 466 467 468 469
	if (libinput_device_config_tap_get_finger_count(device) > 0 &&
	    libinput_device_config_tap_set_drag_lock_enabled(device,
							     driver_data->options.tap_drag_lock) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set Tapping DragLock to %d\n",
			    driver_data->options.tap_drag_lock);

470 471 472 473 474 475 476
	if (libinput_device_config_tap_get_finger_count(device) > 0 &&
	    libinput_device_config_tap_set_drag_enabled(device,
							driver_data->options.tap_drag) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set Tapping Drag to %d\n",
			    driver_data->options.tap_drag);

477 478 479 480 481 482 483 484 485 486 487
	if (libinput_device_config_calibration_has_matrix(device) &&
	    libinput_device_config_calibration_set_matrix(device,
							  driver_data->options.matrix) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to apply matrix: "
			    "%.2f %.2f %.2f %2.f %.2f %.2f %.2f %.2f %.2f\n",
			    driver_data->options.matrix[0], driver_data->options.matrix[1],
			    driver_data->options.matrix[2], driver_data->options.matrix[3],
			    driver_data->options.matrix[4], driver_data->options.matrix[5],
			    driver_data->options.matrix[6], driver_data->options.matrix[7],
			    driver_data->options.matrix[8]);
488

489 490
	if (libinput_device_config_left_handed_is_available(device) &&
	    libinput_device_config_left_handed_set(device,
491
						   driver_data->options.left_handed) != LIBINPUT_CONFIG_STATUS_SUCCESS)
492 493 494
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set LeftHanded to %d\n",
			    driver_data->options.left_handed);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

	if (libinput_device_config_scroll_set_method(device,
						     driver_data->options.scroll_method) != LIBINPUT_CONFIG_STATUS_SUCCESS) {
		const char *method;

		switch(driver_data->options.scroll_method) {
		case LIBINPUT_CONFIG_SCROLL_NO_SCROLL: method = "none"; break;
		case LIBINPUT_CONFIG_SCROLL_2FG: method = "twofinger"; break;
		case LIBINPUT_CONFIG_SCROLL_EDGE: method = "edge"; break;
		case LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN: method = "button"; break;
		default:
			method = "unknown"; break;
		}

		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set scroll to %s\n",
			    method);
	}
513

514 515 516 517
	if (libinput_device_config_scroll_get_methods(device) & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) {
		scroll_button = btn_xorg2linux(driver_data->options.scroll_button);
		if (libinput_device_config_scroll_set_button(device, scroll_button) != LIBINPUT_CONFIG_STATUS_SUCCESS)
			xf86IDrvMsg(pInfo, X_ERROR,
518
				    "Failed to set ScrollButton to %u\n",
519 520
				    driver_data->options.scroll_button);
	}
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

	if (libinput_device_config_click_set_method(device,
						    driver_data->options.click_method) != LIBINPUT_CONFIG_STATUS_SUCCESS) {
		const char *method;

		switch (driver_data->options.click_method) {
		case LIBINPUT_CONFIG_CLICK_METHOD_NONE: method = "none"; break;
		case LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS: method = "buttonareas"; break;
		case LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER: method = "clickfinger"; break;
		default:
			method = "unknown"; break;
		}

		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set click method to %s\n",
			    method);
	}
538 539 540 541 542 543 544

	if (libinput_device_config_middle_emulation_is_available(device) &&
	    libinput_device_config_middle_emulation_set_enabled(device,
								driver_data->options.middle_emulation) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set MiddleEmulation to %d\n",
			    driver_data->options.middle_emulation);
545 546 547 548 549 550 551

	if (libinput_device_config_dwt_is_available(device) &&
	    libinput_device_config_dwt_set_enabled(device,
						   driver_data->options.disable_while_typing) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set DisableWhileTyping to %d\n",
			    driver_data->options.disable_while_typing);
552 553 554 555 556 557 558

	if (libinput_device_config_rotation_is_available(device) &&
	    libinput_device_config_rotation_set_angle(device, driver_data->options.rotation_angle) != LIBINPUT_CONFIG_STATUS_SUCCESS)
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Failed to set RotationAngle to %.2f\n",
			    driver_data->options.rotation_angle);

559 560
}

561 562 563 564 565
static int
xf86libinput_on(DeviceIntPtr dev)
{
	InputInfoPtr pInfo = dev->public.devicePrivate;
	struct xf86libinput *driver_data = pInfo->private;
566
	struct xf86libinput_device *shared_device = driver_data->shared_device;
Peter Hutterer's avatar
Peter Hutterer committed
567
	struct libinput *libinput = driver_context.libinput;
568
	struct libinput_device *device;
569

570 571 572
	device = xf86libinput_shared_enable(pInfo,
					    shared_device,
					    driver_data->path);
Peter Hutterer's avatar
Peter Hutterer committed
573
	if (!device)
574
		return !Success;
Peter Hutterer's avatar
Peter Hutterer committed
575 576 577 578 579

	/* if we use server fds, overwrite the fd with the one from
	   libinput nonetheless, otherwise the server won't call ReadInput
	   for our device. This must be swapped back to the real fd in
	   DEVICE_OFF so systemd-logind closes the right fd */
580
	pInfo->fd = libinput_get_fd(libinput);
581 582

	if (driver_context.device_enabled_count == 0) {
583 584
#if HAVE_THREADED_INPUT
		xf86AddEnabledDevice(pInfo);
585
		driver_context.registered_InputInfoPtr = pInfo;
586
#else
587 588
		/* Can't use xf86AddEnabledDevice on an epollfd */
		AddEnabledDevice(pInfo->fd);
589
#endif
590 591 592
	}

	driver_context.device_enabled_count++;
593 594
	dev->public.on = TRUE;

595 596
	LibinputApplyConfig(dev);

597 598 599 600 601 602 603 604
	return Success;
}

static int
xf86libinput_off(DeviceIntPtr dev)
{
	InputInfoPtr pInfo = dev->public.devicePrivate;
	struct xf86libinput *driver_data = pInfo->private;
605
	struct xf86libinput_device *shared_device = driver_data->shared_device;
606

607
	if (--driver_context.device_enabled_count == 0) {
608 609 610
#if HAVE_THREADED_INPUT
		xf86RemoveEnabledDevice(pInfo);
#else
611
		RemoveEnabledDevice(pInfo->fd);
612
#endif
613 614
	}

Peter Hutterer's avatar
Peter Hutterer committed
615 616 617 618 619 620
	if (use_server_fd(pInfo)) {
		pInfo->fd = xf86SetIntOption(pInfo->options, "fd", -1);
	} else {
		pInfo->fd = -1;
	}

621
	dev->public.on = FALSE;
Peter Hutterer's avatar
Peter Hutterer committed
622

623
	xf86libinput_shared_disable(shared_device);
Peter Hutterer's avatar
Peter Hutterer committed
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638
	return Success;
}

static void
xf86libinput_ptr_ctl(DeviceIntPtr dev, PtrCtrl *ctl)
{
}

static void
init_button_map(unsigned char *btnmap, size_t size)
{
	int i;

	memset(btnmap, 0, size);
639
	for (i = 0; i < size; i++)
640 641 642 643 644 645
		btnmap[i] = i;
}

static void
init_button_labels(Atom *labels, size_t size)
{
646 647
	assert(size > 10);

648 649 650 651 652 653 654 655
	memset(labels, 0, size * sizeof(Atom));
	labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
	labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
	labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
	labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
	labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
	labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
	labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
656 657
	labels[7] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_SIDE);
	labels[8] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_EXTRA);
658 659
	labels[9] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_FORWARD);
	labels[10] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_BACK);
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
}

static void
init_axis_labels(Atom *labels, size_t size)
{
	memset(labels, 0, size * sizeof(Atom));
	labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
	labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
	labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HSCROLL);
	labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_VSCROLL);
}

static int
xf86libinput_init_pointer(InputInfoPtr pInfo)
{
	DeviceIntPtr dev= pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
677
	struct libinput_device *device = driver_data->shared_device->device;
678
	int min, max, res;
679
	int nbuttons = 7;
680
	int i;
681

682
	Atom btnlabels[MAX_BUTTONS];
683 684
	Atom axislabels[TOUCHPAD_NUM_AXES];

685
	for (i = BTN_JOYSTICK - 1; i >= BTN_SIDE; i--) {
686
		if (libinput_device_pointer_has_button(device, i)) {
687 688 689 690 691
			nbuttons += i - BTN_SIDE + 1;
			break;
		}
	}

692 693 694
	init_button_labels(btnlabels, ARRAY_SIZE(btnlabels));
	init_axis_labels(axislabels, ARRAY_SIZE(axislabels));

695 696
	InitPointerDeviceStruct((DevicePtr)dev,
				driver_data->options.btnmap,
697
				nbuttons,
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
				btnlabels,
				xf86libinput_ptr_ctl,
				GetMotionHistorySize(),
				TOUCHPAD_NUM_AXES,
				axislabels);
	min = -1;
	max = -1;
	res = 0;

	xf86InitValuatorAxisStruct(dev, 0,
			           XIGetKnownProperty(AXIS_LABEL_PROP_REL_X),
				   min, max, res * 1000, 0, res * 1000, Relative);
	xf86InitValuatorAxisStruct(dev, 1,
			           XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y),
				   min, max, res * 1000, 0, res * 1000, Relative);

714 715
	SetScrollValuator(dev, 2, SCROLL_TYPE_HORIZONTAL, driver_data->scroll.hdist, 0);
	SetScrollValuator(dev, 3, SCROLL_TYPE_VERTICAL, driver_data->scroll.vdist, 0);
716 717 718 719

	return Success;
}

720 721 722 723 724
static int
xf86libinput_init_pointer_absolute(InputInfoPtr pInfo)
{
	DeviceIntPtr dev= pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
725
	struct libinput_device *device = driver_data->shared_device->device;
726 727 728 729 730 731 732 733
	int min, max, res;
	int nbuttons = 7;
	int i;

	Atom btnlabels[MAX_BUTTONS];
	Atom axislabels[TOUCHPAD_NUM_AXES];

	for (i = BTN_BACK; i >= BTN_SIDE; i--) {
734
		if (libinput_device_pointer_has_button(device, i)) {
735 736 737 738 739 740 741 742
			nbuttons += i - BTN_SIDE + 1;
			break;
		}
	}

	init_button_labels(btnlabels, ARRAY_SIZE(btnlabels));
	init_axis_labels(axislabels, ARRAY_SIZE(axislabels));

743 744
	InitPointerDeviceStruct((DevicePtr)dev,
				driver_data->options.btnmap,
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
				nbuttons,
				btnlabels,
				xf86libinput_ptr_ctl,
				GetMotionHistorySize(),
				TOUCHPAD_NUM_AXES,
				axislabels);
	min = 0;
	max = TOUCH_AXIS_MAX;
	res = 0;

	xf86InitValuatorAxisStruct(dev, 0,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	xf86InitValuatorAxisStruct(dev, 1,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y),
				   min, max, res * 1000, 0, res * 1000, Absolute);

762 763
	SetScrollValuator(dev, 2, SCROLL_TYPE_HORIZONTAL, driver_data->scroll.hdist, 0);
	SetScrollValuator(dev, 3, SCROLL_TYPE_VERTICAL, driver_data->scroll.vdist, 0);
764

765 766
	driver_data->has_abs = TRUE;

767 768
	return Success;
}
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
static void
xf86libinput_kbd_ctrl(DeviceIntPtr device, KeybdCtrl *ctrl)
{
#define CAPSFLAG	1
#define NUMFLAG		2
#define SCROLLFLAG	4

    static struct { int xbit, code; } bits[] = {
        { CAPSFLAG,	LIBINPUT_LED_CAPS_LOCK },
        { NUMFLAG,	LIBINPUT_LED_NUM_LOCK },
        { SCROLLFLAG,	LIBINPUT_LED_SCROLL_LOCK },
	{ 0, 0 },
    };
    int i = 0;
    enum libinput_led leds = 0;
    InputInfoPtr pInfo = device->public.devicePrivate;
    struct xf86libinput *driver_data = pInfo->private;
786
    struct libinput_device *ldevice = driver_data->shared_device->device;
787 788

    while (bits[i].xbit) {
789 790
	    if (ctrl->leds & bits[i].xbit)
		    leds |= bits[i].code;
791 792 793 794 795 796 797 798 799 800
	    i++;
    }

    libinput_device_led_update(ldevice, leds);
}

static void
xf86libinput_init_keyboard(InputInfoPtr pInfo)
{
	DeviceIntPtr dev= pInfo->dev;
801
	XkbRMLVOSet rmlvo = {0};
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	XkbRMLVOSet defaults = {0};

	XkbGetRulesDflts(&defaults);

	rmlvo.rules = xf86SetStrOption(pInfo->options,
				       "xkb_rules",
				       defaults.rules);
	rmlvo.model = xf86SetStrOption(pInfo->options,
				       "xkb_model",
				       defaults.model);
	rmlvo.layout = xf86SetStrOption(pInfo->options,
					"xkb_layout",
					defaults.layout);
	rmlvo.variant = xf86SetStrOption(pInfo->options,
					 "xkb_variant",
					 defaults.variant);
	rmlvo.options = xf86SetStrOption(pInfo->options,
					 "xkb_options",
					 defaults.options);
821 822

	InitKeyboardDeviceStruct(dev, &rmlvo, NULL,
823
				 xf86libinput_kbd_ctrl);
824
	XkbFreeRMLVOSet(&rmlvo, FALSE);
825
	XkbFreeRMLVOSet(&defaults, FALSE);
826 827
}

Peter Hutterer's avatar
Peter Hutterer committed
828 829 830 831
static void
xf86libinput_init_touch(InputInfoPtr pInfo)
{
	DeviceIntPtr dev = pInfo->dev;
832
	struct xf86libinput *driver_data = pInfo->private;
Peter Hutterer's avatar
Peter Hutterer committed
833
	int min, max, res;
834 835
	unsigned char btnmap[MAX_BUTTONS + 1];
	Atom btnlabels[MAX_BUTTONS];
Peter Hutterer's avatar
Peter Hutterer committed
836
	Atom axislabels[TOUCHPAD_NUM_AXES];
837
	int nbuttons = 7;
Peter Hutterer's avatar
Peter Hutterer committed
838 839 840 841 842

	init_button_map(btnmap, ARRAY_SIZE(btnmap));
	init_button_labels(btnlabels, ARRAY_SIZE(btnlabels));
	init_axis_labels(axislabels, ARRAY_SIZE(axislabels));

843 844
	InitPointerDeviceStruct((DevicePtr)dev,
				driver_data->options.btnmap,
845
				nbuttons,
Peter Hutterer's avatar
Peter Hutterer committed
846 847 848 849 850 851 852 853 854 855
				btnlabels,
				xf86libinput_ptr_ctl,
				GetMotionHistorySize(),
				TOUCHPAD_NUM_AXES,
				axislabels);
	min = 0;
	max = TOUCH_AXIS_MAX;
	res = 0;

	xf86InitValuatorAxisStruct(dev, 0,
856
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_X),
Peter Hutterer's avatar
Peter Hutterer committed
857 858
				   min, max, res * 1000, 0, res * 1000, Absolute);
	xf86InitValuatorAxisStruct(dev, 1,
859
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_Y),
Peter Hutterer's avatar
Peter Hutterer committed
860 861 862 863 864
				   min, max, res * 1000, 0, res * 1000, Absolute);
	InitTouchClassDeviceStruct(dev, TOUCH_MAX_SLOTS, XIDirectTouch, 2);

}

865
static int
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
xf86libinput_init_tablet_pen_or_eraser(InputInfoPtr pInfo,
				       struct libinput_tablet_tool *tool)
{
	DeviceIntPtr dev = pInfo->dev;
	int min, max, res;
	int axis;

	min = 0;
	max = TABLET_PRESSURE_AXIS_MAX;
	res = 0;
	axis = 2;
	if (libinput_tablet_tool_has_pressure(tool))
		xf86InitValuatorAxisStruct(dev, axis++,
					   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE),
					   min, max, res * 1000, 0, res * 1000, Absolute);
	max = TABLET_TILT_AXIS_MAX;
	min = -TABLET_TILT_AXIS_MAX;
	if (libinput_tablet_tool_has_tilt(tool)) {
		xf86InitValuatorAxisStruct(dev, axis++,
					   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_X),
					   min, max, res * 1000, 0, res * 1000, Absolute);
		xf86InitValuatorAxisStruct(dev, axis++,
					   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_Y),
					   min, max, res * 1000, 0, res * 1000, Absolute);
	}
891

892 893 894 895 896 897
	min = -TABLET_AXIS_MAX;
	max = TABLET_AXIS_MAX;
	if (libinput_tablet_tool_has_rotation(tool))
		xf86InitValuatorAxisStruct(dev, axis++,
					   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_RZ),
					   min, max, res * 1000, 0, res * 1000, Absolute);
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	return axis;
}

static void
xf86libinput_init_tablet_airbrush(InputInfoPtr pInfo,
				  struct libinput_tablet_tool *tool)
{
	DeviceIntPtr dev = pInfo->dev;
	int min, max, res;
	int axis;

	/* first axes are shared */
	axis = xf86libinput_init_tablet_pen_or_eraser(pInfo, tool);
	if (axis < 5) {
		xf86IDrvMsg(pInfo, X_ERROR, "Airbrush tool has missing pressure or tilt axes\n");
		return;
	}

	if (!libinput_tablet_tool_has_slider(tool)) {
		xf86IDrvMsg(pInfo, X_ERROR, "Airbrush tool is missing the slider axis\n");
		return;
	}

	min = -TABLET_AXIS_MAX;
	max = TABLET_AXIS_MAX;
	res = 0;

	xf86InitValuatorAxisStruct(dev, axis,
				   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_THROTTLE),
				   min, max, res * 1000, 0, res * 1000, Absolute);
928 929
}

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
static void
xf86libinput_init_tablet_mouse(InputInfoPtr pInfo,
			       struct libinput_tablet_tool *tool)
{
	DeviceIntPtr dev = pInfo->dev;
	int min, max, res;
	int axis;

	if (!libinput_tablet_tool_has_rotation(tool)) {
		xf86IDrvMsg(pInfo, X_ERROR, "Mouse tool is missing the rotation axis\n");
		return;
	}

	min = 0;
	max = TABLET_AXIS_MAX;
	res = 0;

	/* The mouse/lens tool don't have pressure, but for backwards-compat
	   with the xorg wacom driver we initialize the the axis anyway */
	axis = 2;
	xf86InitValuatorAxisStruct(dev, axis,
				   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE),
				   min, max, res * 1000, 0, res * 1000, Absolute);

	axis = 3;
	min = -TABLET_AXIS_MAX;
	max = TABLET_AXIS_MAX;
	xf86InitValuatorAxisStruct(dev, axis,
				   XIGetKnownProperty(AXIS_LABEL_PROP_ABS_RZ),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	return;
}

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
static void
xf86libinput_init_tablet(InputInfoPtr pInfo)
{
	DeviceIntPtr dev = pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
	struct libinput_tablet_tool *tool;
	int min, max, res;
	unsigned char btnmap[TABLET_NUM_BUTTONS];
	Atom btnlabels[TABLET_NUM_BUTTONS] = {0};
	Atom axislabels[TOUCHPAD_NUM_AXES] = {0};
	int nbuttons = TABLET_NUM_BUTTONS;
	int naxes = 2;

	BUG_RETURN(driver_data->tablet_tool == NULL);

	tool = driver_data->tablet_tool;

	init_button_map(btnmap, ARRAY_SIZE(btnmap));

	if (libinput_tablet_tool_has_pressure(tool))
		naxes++;
	if (libinput_tablet_tool_has_tilt(tool))
		naxes += 2;
986 987
	if (libinput_tablet_tool_has_slider(tool))
		naxes++;
988 989
	if (libinput_tablet_tool_has_rotation(tool))
		naxes++;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

	InitPointerDeviceStruct((DevicePtr)dev,
				driver_data->options.btnmap,
				nbuttons,
				btnlabels,
				xf86libinput_ptr_ctl,
				GetMotionHistorySize(),
				naxes,
				axislabels);

	min = 0;
	max = TABLET_AXIS_MAX;
	res = 0;
	xf86InitValuatorAxisStruct(dev, 0,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	xf86InitValuatorAxisStruct(dev, 1,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y),
				   min, max, res * 1000, 0, res * 1000, Absolute);

	switch (libinput_tablet_tool_get_type(tool)) {
	case LIBINPUT_TABLET_TOOL_TYPE_PEN:
	case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
		xf86libinput_init_tablet_pen_or_eraser(pInfo, tool);
		break;
1015 1016 1017
	case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
		xf86libinput_init_tablet_airbrush(pInfo, tool);
		break;
1018 1019 1020 1021
	case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
	case LIBINPUT_TABLET_TOOL_TYPE_LENS:
		xf86libinput_init_tablet_mouse(pInfo, tool);
		break;
1022 1023 1024 1025 1026 1027 1028 1029
	default:
		xf86IDrvMsg(pInfo, X_ERROR, "Tool type not supported yet\n");
		break;
	}

	InitProximityClassDeviceStruct(dev);
}

Peter Hutterer's avatar
Peter Hutterer committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
static void
xf86libinput_init_tablet_pad(InputInfoPtr pInfo)
{
	DeviceIntPtr dev = pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
	struct libinput_device *device = driver_data->shared_device->device;
	int min, max, res;
	unsigned char btnmap[MAX_BUTTONS];
	Atom btnlabels[MAX_BUTTONS] = {0};
	Atom axislabels[TOUCHPAD_NUM_AXES] = {0};
	int nbuttons;
	int naxes = 7;

1043
	nbuttons = libinput_device_tablet_pad_get_num_buttons(device) + 4;
Peter Hutterer's avatar
Peter Hutterer committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	init_button_map(btnmap, nbuttons);

	InitPointerDeviceStruct((DevicePtr)dev,
				driver_data->options.btnmap,
				nbuttons,
				btnlabels,
				xf86libinput_ptr_ctl,
				GetMotionHistorySize(),
				naxes,
				axislabels);

	/* For compat with xf86-input-wacom we init x, y, pressure, followed
	 * by strip x, strip y, ring, ring2*/
	min = 0;
	max = TABLET_AXIS_MAX;
	res = 0;
	xf86InitValuatorAxisStruct(dev, 0,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	xf86InitValuatorAxisStruct(dev, 1,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	xf86InitValuatorAxisStruct(dev, 2,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE),
				   min, max, res * 1000, 0, res * 1000, Absolute);

	/* strip x */
	max = TABLET_STRIP_AXIS_MAX;
	xf86InitValuatorAxisStruct(dev, 3,
			           None,
				   min, max, res * 1000, 0, res * 1000, Absolute);
	/* strip y */
	xf86InitValuatorAxisStruct(dev, 4,
			           None,
				   min, max, res * 1000, 0, res * 1000, Absolute);
	/* first ring */
	max = TABLET_RING_AXIS_MAX;
	xf86InitValuatorAxisStruct(dev, 5,
			           XIGetKnownProperty(AXIS_LABEL_PROP_ABS_WHEEL),
				   min, max, res * 1000, 0, res * 1000, Absolute);
	/* second ring */
	xf86InitValuatorAxisStruct(dev, 6,
			           None,
				   min, max, res * 1000, 0, res * 1000, Absolute);
}

1090 1091 1092 1093 1094
static int
xf86libinput_init(DeviceIntPtr dev)
{
	InputInfoPtr pInfo = dev->public.devicePrivate;
	struct xf86libinput *driver_data = pInfo->private;
1095 1096 1097 1098
	struct xf86libinput_device *shared_device = driver_data->shared_device;
	struct libinput_device *device = shared_device->device;

	BUG_RETURN_VAL(device == NULL, !Success);
1099 1100 1101

	dev->public.on = FALSE;

1102
	if (driver_data->capabilities & CAP_KEYBOARD)
Peter Hutterer's avatar
Peter Hutterer committed
1103
		xf86libinput_init_keyboard(pInfo);
1104
	if (driver_data->capabilities & CAP_POINTER) {
1105 1106
		if (libinput_device_config_calibration_has_matrix(device) &&
		    !libinput_device_config_accel_is_available(device))
1107 1108 1109 1110
			xf86libinput_init_pointer_absolute(pInfo);
		else
			xf86libinput_init_pointer(pInfo);
	}
1111
	if (driver_data->capabilities & CAP_TOUCH)
Peter Hutterer's avatar
Peter Hutterer committed
1112
		xf86libinput_init_touch(pInfo);
1113 1114
	if (driver_data->capabilities & CAP_TABLET_TOOL)
		xf86libinput_init_tablet(pInfo);
Peter Hutterer's avatar
Peter Hutterer committed
1115 1116
	if (driver_data->capabilities & CAP_TABLET_PAD)
		xf86libinput_init_tablet_pad(pInfo);
1117

1118
	LibinputApplyConfig(dev);
1119 1120 1121
	LibinputInitProperty(dev);
	XIRegisterPropertyHandler(dev, LibinputSetProperty, NULL, NULL);

1122 1123 1124 1125 1126 1127 1128 1129
	/* If we have a device but it's not yet enabled it's the
	 * already-removed device from PreInit. Drop the ref to clean up,
	 * we'll get a new libinput_device during DEVICE_ON when we re-add
	 * it. */
	if (!xf86libinput_shared_is_enabled(shared_device)) {
		libinput_device_unref(device);
		shared_device->device = NULL;
	}
1130 1131 1132 1133

	return 0;
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
static bool
is_libinput_device(InputInfoPtr pInfo)
{
	char *driver;
	BOOL rc;

	driver = xf86CheckStrOption(pInfo->options, "driver", "");
	rc = strcmp(driver, "libinput") == 0;
	free(driver);

	return rc;
}

static void
swap_registered_device(InputInfoPtr pInfo)
{
	InputInfoPtr next;

	if (pInfo != driver_context.registered_InputInfoPtr)
		return;

	next = xf86FirstLocalDevice();
	while (next == pInfo || !is_libinput_device(next))
		next = next->next;

1159
#if HAVE_THREADED_INPUT
1160
	input_lock();
1161 1162 1163
#else
	int sigstate = xf86BlockSIGIO();
#endif
1164 1165 1166 1167
	xf86RemoveEnabledDevice(pInfo);
	if (next) /* shouldn't ever be NULL anyway */
		xf86AddEnabledDevice(next);
	driver_context.registered_InputInfoPtr = next;
1168
#if HAVE_THREADED_INPUT
1169
	input_unlock();
1170 1171 1172
#else
	xf86UnblockSIGIO(sigstate);
#endif
1173 1174
}

1175 1176 1177
static void
xf86libinput_destroy(DeviceIntPtr dev)
{
1178 1179 1180 1181
	InputInfoPtr pInfo = dev->public.devicePrivate;
	struct xf86libinput *driver_data = pInfo->private;
	struct xf86libinput_device *shared_device = driver_data->shared_device;

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	/* If the device being destroyed is the one we used for
	 * xf86AddEnabledDevice(), we need to swap it out for one that is
	 * still live. xf86AddEnabledDevice() buffers some data and once the
	 * deletes pInfo (when DEVICE_OFF completes) the thread will keep
	 * calling that struct's read_input because we never removed it.
	 * Avoid this by removing ours and substituting one that's still
	 * valid, the fd is the same anyway (libinput's epollfd).
	 */
	if (driver_context.device_enabled_count > 0)
		swap_registered_device(pInfo);

1193 1194
	xorg_list_del(&driver_data->shared_device_link);

1195 1196 1197
	if (driver_data->tablet_tool)
		libinput_tablet_tool_unref(driver_data->tablet_tool);

1198
	xf86libinput_shared_unref(shared_device);
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
}

static int
xf86libinput_device_control(DeviceIntPtr dev, int mode)
{
	int rc = BadValue;

	switch(mode) {
		case DEVICE_INIT:
			rc = xf86libinput_init(dev);
			break;
		case DEVICE_ON:
			rc = xf86libinput_on(dev);
			break;
		case DEVICE_OFF:
			rc = xf86libinput_off(dev);
			break;
		case DEVICE_CLOSE:
			xf86libinput_destroy(dev);
Peter Hutterer's avatar
Peter Hutterer committed
1218
			rc = Success;
1219 1220 1221 1222 1223 1224 1225
			break;
	}

	return rc;
}

static void
Peter Hutterer's avatar
Peter Hutterer committed
1226
xf86libinput_handle_motion(InputInfoPtr pInfo, struct libinput_event_pointer *event)
1227 1228
{
	DeviceIntPtr dev = pInfo->dev;
1229 1230
	struct xf86libinput *driver_data = pInfo->private;
	ValuatorMask *mask = driver_data->valuators;
1231
	double x, y;
1232

1233 1234 1235
	if ((driver_data->capabilities & CAP_POINTER) == 0)
		return;

1236 1237
	x = libinput_event_pointer_get_dx(event);
	y = libinput_event_pointer_get_dy(event);
1238

1239
	valuator_mask_zero(mask);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

#if HAVE_VMASK_UNACCEL
	{
		double ux, uy;

		ux = libinput_event_pointer_get_dx_unaccelerated(event);
		uy = libinput_event_pointer_get_dy_unaccelerated(event);

		valuator_mask_set_unaccelerated(mask, 0, x, ux);
		valuator_mask_set_unaccelerated(mask, 1, y, uy);
	}
#else
1252 1253
	valuator_mask_set_double(mask, 0, x);
	valuator_mask_set_double(mask, 1, y);
1254
#endif
1255
	xf86PostMotionEventM(dev, Relative, mask);
1256 1257
}

1258 1259 1260 1261 1262 1263 1264 1265
static void
xf86libinput_handle_absmotion(InputInfoPtr pInfo, struct libinput_event_pointer *event)
{
	DeviceIntPtr dev = pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
	ValuatorMask *mask = driver_data->valuators;
	double x, y;

1266 1267 1268 1269 1270 1271 1272
	if (!driver_data->has_abs) {
		xf86IDrvMsg(pInfo, X_ERROR,
			    "Discarding absolute event from relative device. "
			    "Please file a bug\n");
		return;
	}

1273 1274 1275
	if ((driver_data->capabilities & CAP_POINTER) == 0)
		return;

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	x = libinput_event_pointer_get_absolute_x_transformed(event, TOUCH_AXIS_MAX);
	y = libinput_event_pointer_get_absolute_y_transformed(event, TOUCH_AXIS_MAX);

	valuator_mask_zero(mask);
	valuator_mask_set_double(mask, 0, x);
	valuator_mask_set_double(mask, 1, y);

	xf86PostMotionEventM(dev, Absolute, mask);
}

1286
static void
Peter Hutterer's avatar
Peter Hutterer committed
1287
xf86libinput_handle_button(InputInfoPtr pInfo, struct libinput_event_pointer *event)
1288 1289
{
	DeviceIntPtr dev = pInfo->dev;
Peter Hutterer's avatar
Peter Hutterer committed
1290
	struct xf86libinput *driver_data = pInfo->private;
1291 1292 1293
	int button;
	int is_press;

1294 1295 1296
	if ((driver_data->capabilities & CAP_POINTER) == 0)
		return;

1297
	button = btn_linux2xorg(libinput_event_pointer_get_button(event));
Peter Hutterer's avatar
Peter Hutterer committed
1298
	is_press = (libinput_event_pointer_get_button_state(event) == LIBINPUT_BUTTON_STATE_PRESSED);
Peter Hutterer's avatar
Peter Hutterer committed
1299 1300 1301 1302

	if (draglock_get_mode(&driver_data->draglock) != DRAGLOCK_DISABLED)
		draglock_filter_button(&driver_data->draglock, &button, &is_press);

Peter Hutterer's avatar
Peter Hutterer committed
1303
	if (button && button < 256)
Peter Hutterer's avatar
Peter Hutterer committed
1304
		xf86PostButtonEvent(dev, Relative, button, is_press, 0, 0);
1305 1306 1307
}

static void
Peter Hutterer's avatar
Peter Hutterer committed
1308
xf86libinput_handle_key(InputInfoPtr pInfo, struct libinput_event_keyboard *event)
1309 1310
{
	DeviceIntPtr dev = pInfo->dev;
1311
	struct xf86libinput *driver_data = pInfo->private;
1312
	int is_press;
Peter Hutterer's avatar
Peter Hutterer committed
1313
	int key = libinput_event_keyboard_get_key(event);
1314

1315 1316 1317
	if ((driver_data->capabilities & CAP_KEYBOARD) == 0)
		return;

1318 1319
	key += XORG_KEYCODE_OFFSET;

1320
	is_press = (libinput_event_keyboard_get_key_state(event) == LIBINPUT_KEY_STATE_PRESSED);
1321 1322 1323
	xf86PostKeyboardEvent(dev, key, is_press);
}

Peter Hutterer's avatar
Peter Hutterer committed
1324
static void
Peter Hutterer's avatar
Peter Hutterer committed
1325
xf86libinput_handle_axis(InputInfoPtr pInfo, struct libinput_event_pointer *event)
Peter Hutterer's avatar
Peter Hutterer committed
1326 1327
{
	DeviceIntPtr dev = pInfo->dev;
1328 1329
	struct xf86libinput *driver_data = pInfo->private;
	ValuatorMask *mask = driver_data->valuators;
1330
	double value;
1331 1332
	enum libinput_pointer_axis axis;
	enum libinput_pointer_axis_source source;
Peter Hutterer's avatar
Peter Hutterer committed
1333

1334 1335 1336
	if ((driver_data->capabilities & CAP_POINTER) == 0)
		return;

1337
	valuator_mask_zero(mask);
Peter Hutterer's avatar
Peter Hutterer committed
1338

1339 1340 1341 1342 1343 1344 1345 1346 1347
	source = libinput_event_pointer_get_axis_source(event);
	switch(source) {
		case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
		case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
		case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
			break;
		default:
			return;
	}
1348

1349 1350
	axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
	if (libinput_event_pointer_has_axis(event, axis)) {
1351
		if (source == LIBINPUT_POINTER_AXIS_SOURCE_WHEEL) {
1352
			value = libinput_event_pointer_get_axis_value_discrete(event, axis);
Peter Hutterer's avatar
Peter Hutterer committed
1353
			value *= driver_data->scroll.vdist;
1354
		} else {
1355
			value = libinput_event_pointer_get_axis_value(event, axis);
1356
		}
1357 1358
		valuator_mask_set_double(mask, 3, value);
	}
1359 1360 1361 1362

	if (!driver_data->options.horiz_scrolling_enabled)
		goto out;

1363 1364
	axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
	if (libinput_event_pointer_has_axis(event, axis)) {
1365
		if (source == LIBINPUT_POINTER_AXIS_SOURCE_WHEEL) {
1366
			value = libinput_event_pointer_get_axis_value_discrete(event, axis);
Peter Hutterer's avatar
Peter Hutterer committed
1367
			value *= driver_data->scroll.hdist;
1368
		} else {
1369
			value = libinput_event_pointer_get_axis_value(event, axis);
1370
		}
1371 1372
		valuator_mask_set_double(mask, 2, value);
	}
1373

1374
out:
1375
	xf86PostMotionEventM(dev, Relative, mask);
Peter Hutterer's avatar
Peter Hutterer committed
1376 1377
}

Peter Hutterer's avatar
Peter Hutterer committed
1378
static void
1379 1380 1381
xf86libinput_handle_touch(InputInfoPtr pInfo,
			  struct libinput_event_touch *event,
			  enum libinput_event_type event_type)
Peter Hutterer's avatar
Peter Hutterer committed
1382 1383
{
	DeviceIntPtr dev = pInfo->dev;
1384
	struct xf86libinput *driver_data = pInfo->private;
Peter Hutterer's avatar
Peter Hutterer committed
1385 1386
	int type;
	int slot;
1387
	ValuatorMask *m = driver_data->valuators;
1388
	double val;
Peter Hutterer's avatar
Peter Hutterer committed
1389 1390 1391

	/* libinput doesn't give us hw touch ids which X expects, so
	   emulate them here */
1392 1393
	static unsigned int next_touchid;
	static unsigned int touchids[TOUCH_MAX_SLOTS] = {0};
Peter Hutterer's avatar
Peter Hutterer committed
1394

1395 1396 1397
	if ((driver_data->capabilities & CAP_TOUCH) == 0)
		return;

Peter Hutterer's avatar
Peter Hutterer committed
1398
	slot = libinput_event_touch_get_slot(event);
Peter Hutterer's avatar
Peter Hutterer committed
1399

1400 1401
	switch (event_type) {
		case LIBINPUT_EVENT_TOUCH_DOWN:
Peter Hutterer's avatar
Peter Hutterer committed
1402 1403 1404
			type = XI_TouchBegin;
			touchids[slot] = next_touchid++;
			break;
1405
		case LIBINPUT_EVENT_TOUCH_UP:
Peter Hutterer's avatar
Peter Hutterer committed
1406 1407
			type = XI_TouchEnd;
			break;
1408
		case LIBINPUT_EVENT_TOUCH_MOTION:
Peter Hutterer's avatar
Peter Hutterer committed
1409 1410 1411
			type = XI_TouchUpdate;
			break;
		default:
Peter Hutterer's avatar
Peter Hutterer committed
1412
			return;
Peter Hutterer's avatar
Peter Hutterer committed
1413 1414
	};

1415 1416
	valuator_mask_zero(m);

1417 1418
	if (event_type != LIBINPUT_EVENT_TOUCH_UP) {
		val = libinput_event_touch_get_x_transformed(event, TOUCH_AXIS_MAX);
1419
		valuator_mask_set_double(m, 0, val);
1420

1421
		val = libinput_event_touch_get_y_transformed(event, TOUCH_AXIS_MAX);
1422
		valuator_mask_set_double(m, 1, val);
1423
	}
Peter Hutterer's avatar
Peter Hutterer committed
1424 1425 1426 1427

	xf86PostTouchEvent(dev, touchids[slot], type, 0, m);
}

1428 1429
static InputInfoPtr
xf86libinput_pick_device(struct xf86libinput_device *shared_device,
1430
			 struct libinput_event *event)
1431 1432 1433
{
	struct xf86libinput *driver_data;
	uint32_t needed_cap;
1434
	enum libinput_event_type type = libinput_event_get_type(event);
1435 1436 1437 1438 1439 1440 1441 1442

	if (shared_device == NULL)
		return NULL;

	switch(type) {
	case LIBINPUT_EVENT_KEYBOARD_KEY:
		needed_cap = CAP_KEYBOARD;
		break;
1443 1444 1445 1446 1447 1448 1449 1450
	case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
		needed_cap = CAP_TABLET;
		break;
	case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
	case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
	case LIBINPUT_EVENT_TABLET_TOOL_TIP:
		needed_cap = CAP_TABLET_TOOL;
		break;
1451 1452 1453 1454 1455 1456 1457 1458
	default:
		needed_cap = ~CAP_KEYBOARD;
		break;
	}

	xorg_list_for_each_entry(driver_data,
				 &shared_device->device_list,
				 shared_device_link) {
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
		if (driver_data->capabilities & needed_cap) {
			struct libinput_tablet_tool *tool;

			if (needed_cap != CAP_TABLET_TOOL)
				return driver_data->pInfo;

			tool = libinput_event_tablet_tool_get_tool(
					   libinput_event_get_tablet_tool_event(event));
			if (libinput_tablet_tool_get_serial(driver_data->tablet_tool) ==
			    libinput_tablet_tool_get_serial(tool) &&
			    libinput_tablet_tool_get_tool_id(driver_data->tablet_tool) ==
			    libinput_tablet_tool_get_tool_id(tool))
				return driver_data->pInfo;
		}
1473 1474 1475 1476 1477
	}

	return NULL;
}

1478
static void
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
xf86libinput_tool_destroy_queued_event(struct xf86libinput_tablet_tool_queued_event *qe)
{
	struct libinput_event *e;

	e = libinput_event_tablet_tool_get_base_event(qe->event);
	libinput_event_destroy(e);
	xorg_list_del(&qe->node);
	free(qe);
}

static void
xf86libinput_tool_replay_events(struct xf86libinput_tablet_tool_event_queue *queue)
{
	struct xf86libinput_tablet_tool_queued_event *qe, *tmp;

	xorg_list_for_each_entry_safe(qe, tmp, &queue->event_list, node) {
		struct libinput_event *e;

		e = libinput_event_tablet_tool_get_base_event(qe->event);
		xf86libinput_handle_event(e);
		xf86libinput_tool_destroy_queued_event(qe);
	}
}

static bool
xf86libinput_tool_queue_event(struct libinput_event_tablet_tool *event)
{
	struct libinput_event *e;
	struct libinput_tablet_tool *tool;
	struct xf86libinput_tablet_tool_event_queue *queue;
	struct xf86libinput_tablet_tool_queued_event *qe;

	tool = libinput_event_tablet_tool_get_tool(event);
	if (!tool)
		return true;

	queue = libinput_tablet_tool_get_user_data(tool);
	if (!queue)
		return false;

	if (!queue->need_to_queue) {
		if (!xorg_list_is_empty(&queue->event_list)) {
			libinput_tablet_tool_set_user_data(tool, NULL);
			xf86libinput_tool_replay_events(queue);
			free(queue);
		}

		return false;
	}

	/* We got the prox out while still queuing, just ditch the whole
	 * series of events and the event queue with it. */
	if (libinput_event_tablet_tool_get_proximity_state(event) ==
	    LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) {
		struct xf86libinput_tablet_tool_queued_event *tmp;

		xorg_list_for_each_entry_safe(qe, tmp, &queue->event_list, node)
			xf86libinput_tool_destroy_queued_event(qe);

		libinput_tablet_tool_set_user_data(tool, NULL);
		free(queue);

		/* we destroy the event here but return true
		 * to make sure the event looks like it got queued and the
		 * caller doesn't destroy it for us
		 */
		e = libinput_event_tablet_tool_get_base_event(event);
		libinput_event_destroy(e);
		return true;
	}

	qe = calloc(1, sizeof(*qe));
	if (!qe) {
		e = libinput_event_tablet_tool_get_base_event(event);
		libinput_event_destroy(e);
		return true;
	}

	qe->event = event;
	xorg_list_append(&qe->node, &queue->event_list);

	return true;
}

static enum event_handling
1564 1565 1566 1567 1568 1569
xf86libinput_handle_tablet_tip(InputInfoPtr pInfo,
			       struct libinput_event_tablet_tool *event)
{
	enum libinput_tablet_tool_tip_state state;
	const BOOL is_absolute = TRUE;

1570 1571 1572
	if (xf86libinput_tool_queue_event(event))
		return EVENT_QUEUED;

1573 1574 1575 1576 1577 1578
	state = libinput_event_tablet_tool_get_tip_state(event);

	xf86PostButtonEventP(pInfo->dev,
			     is_absolute, 1,
			     state == LIBINPUT_TABLET_TOOL_TIP_DOWN ? 1 : 0,
			     0, 0, NULL);
1579 1580

	return EVENT_HANDLED;
1581 1582
}

1583
static enum event_handling
1584 1585 1586 1587 1588 1589
xf86libinput_handle_tablet_button(InputInfoPtr pInfo,
				  struct libinput_event_tablet_tool *event)
{
	enum libinput_button_state state;
	uint32_t button, b;

1590 1591 1592
	if (xf86libinput_tool_queue_event(event))
		return EVENT_QUEUED;

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
	button = libinput_event_tablet_tool_get_button(event);
	state = libinput_event_tablet_tool_get_button_state(event);

	b = btn_linux2xorg(button);

	xf86PostButtonEventP(pInfo->dev,
			     TRUE,
			     b,
			     state == LIBINPUT_BUTTON_STATE_PRESSED ? 1 : 0,
			     0, 0, NULL);
1603 1604

	return EVENT_HANDLED;
1605 1606
}

1607
static enum event_handling
1608 1609 1610 1611 1612 1613 1614 1615 1616
xf86libinput_handle_tablet_axis(InputInfoPtr pInfo,
				struct libinput_event_tablet_tool *event)
{
	DeviceIntPtr dev = pInfo->dev;
	struct xf86libinput *driver_data = pInfo->private;
	ValuatorMask *mask = driver_data->valuators;
	struct libinput_tablet_tool *tool;
	double value;

1617 1618 1619
	if (xf86libinput_tool_queue_event(event))
		return EVENT_QUEUED;

1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
	value = libinput_event_tablet_tool_get_x_transformed(event,
							TABLET_AXIS_MAX);
	valuator_mask_set_double(mask, 0, value);
	value = libinput_event_tablet_tool_get_y_transformed(event,
							TABLET_AXIS_MAX);
	valuator_mask_set_double(mask, 1, value);

	tool = libinput_event_tablet_tool_get_tool(event);

	if (libinput_tablet_tool_has_pressure(tool)) {
		value = libinput_event_tablet_tool_get_pressure(event);
		value *= TABLET_PRESSURE_AXIS_MAX;
		valuator_mask_set_double(mask, 2, value);
	}

	if (libinput_tablet_tool_has_tilt(tool)) {
		value = libinput_event_tablet_tool_get_tilt_x(event);
		valuator_mask_set_double(mask, 3, value);

		value = libinput_event_tablet_tool_get_tilt_y(event);
		valuator_mask_set_double(mask, 4, value);
	}

1643 1644 1645 1646 1647 1648
	if (libinput_tablet_tool_has_slider(tool)) {
		value = libinput_event_tablet_tool_get_slider_position(event);
		value *= TABLET_AXIS_MAX;
		valuator_mask_set_double(mask, 5, value);
	}

1649
	if (libinput_tablet_tool_has_rotation(tool)) {
1650 1651
		int valuator;

1652 1653
		value = libinput_event_tablet_tool_get_rotation(event);
		value *= TABLET_AXIS_MAX;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

		switch (libinput_tablet_tool_get_type(tool)) {
		case LIBINPUT_TABLET_TOOL_TYPE_PEN:
		case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
			valuator = 5;
			break;
		case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
		case LIBINPUT_TABLET_TOOL_TYPE_LENS:
			valuator = 3;
			break;
		default:
			xf86IDrvMsg(pInfo, X_ERROR,
				    "Invalid rotation axis on tool\n");
1667
			return EVENT_HANDLED;
1668 1669 1670
		}

		valuator_mask_set_double(mask, valuator, value);
1671 1672
	}

1673
	xf86PostMotionEventM(dev, Absolute, mask);
1674 1675

	return EVENT_HANDLED;
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
}

static inline const char *
tool_type_to_str(enum libinput_tablet_tool_type type)
{
	const char *str;

	switch (type) {
	case LIBINPUT_TABLET_TOOL_TYPE_PEN: str = "Pen"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_BRUSH: str = "Brush"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_PENCIL: str = "Pencil"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH: str = "Airbrush"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_ERASER: str = "Eraser"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_MOUSE: str = "Mouse"; break;
	case LIBINPUT_TABLET_TOOL_TYPE_LENS: str = "Lens"; break;
	default:
		str = "unknown tool";
		break;
	}

	return str;
}

1699 1700 1701
static inline void
xf86libinput_create_tool_subdevice(InputInfoPtr pInfo,
				   struct libinput_event_tablet_tool *event)
1702 1703 1704 1705
{
	struct xf86libinput *driver_data = pInfo->private;
	struct xf86libinput_device *shared_device = driver_data->shared_device;
	struct xf86libinput_tablet_tool *t;
1706 1707
	struct xf86libinput_tablet_tool_event_queue *queue;
	struct libinput_tablet_tool *tool;
1708 1709 1710 1711
	uint64_t serial, tool_id;
	XF86OptionPtr options = NULL;
	char name[64];

1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
	t = calloc(1, sizeof *t);
	if (!t)
		return;

	queue = calloc(1, sizeof(*queue));
	if (!queue) {
		free(t);
		return;
	}
	queue->need_to_queue = true;
	xorg_list_init(&queue->event_list);