test-tablet.c 139 KB
Newer Older
1
/*
2
 * Copyright © 2014-2015 Red Hat, Inc.
3
 * Copyright © 2014 Lyude Paul
4
 *
5 6 7 8 9 10
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
11
 *
12 13 14 15 16 17 18 19 20 21 22
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
23 24 25 26 27 28 29 30 31 32
 */

#include <config.h>

#include <check.h>
#include <errno.h>
#include <fcntl.h>
#include <libinput.h>
#include <unistd.h>
#include <stdbool.h>
33
#include <stdarg.h>
34 35 36 37 38

#include "libinput-util.h"
#include "evdev-tablet.h"
#include "litest.h"

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
START_TEST(button_down_up)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	struct libinput_event_tablet_tool *tev;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
		{ ABS_PRESSURE, 0 },
		{ -1, -1 }
	};

	if (!libevdev_has_event_code(dev->evdev, EV_KEY, BTN_STYLUS))
		return;

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_PRESSED);
	libinput_event_destroy(event);
	litest_assert_empty_queue(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_RELEASED);
	libinput_event_destroy(event);
	litest_assert_empty_queue(li);

}
END_TEST

START_TEST(button_seat_count)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	struct libinput_event_tablet_tool *tev;
	struct litest_device *dev2;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
		{ ABS_PRESSURE, 0 },
		{ -1, -1 }
	};

	if (!libevdev_has_event_code(dev->evdev, EV_KEY, BTN_STYLUS))
		return;

	dev2 = litest_add_device(li, LITEST_WACOM_CINTIQ_13HDT_PEN);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_tablet_proximity_in(dev2, 10, 10, axes);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_event(dev2, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev2, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_PRESSED);
	ck_assert_int_eq(libinput_event_tablet_tool_get_seat_button_count(tev), 1);
	libinput_event_destroy(event);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_PRESSED);
	ck_assert_int_eq(libinput_event_tablet_tool_get_seat_button_count(tev), 2);
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

	litest_event(dev2, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev2, EV_SYN, SYN_REPORT, 0);
	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_RELEASED);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_seat_button_count(tev), 1);
	libinput_event_destroy(event);

	event = libinput_get_event(li);
	tev = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
			 LIBINPUT_BUTTON_STATE_RELEASED);
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
			 BTN_STYLUS);
	ck_assert_int_eq(libinput_event_tablet_tool_get_seat_button_count(tev), 0);
	libinput_event_destroy(event);
	litest_assert_empty_queue(li);

	litest_delete_device(dev2);
}
END_TEST

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
START_TEST(button_up_on_delete)
{
	struct libinput *li = litest_create_context();
	struct litest_device *dev = litest_add_device(li, LITEST_WACOM_INTUOS);
	struct libevdev *evdev = libevdev_new();
	unsigned int code;

	litest_tablet_proximity_in(dev, 10, 10, NULL);
	litest_drain_events(li);

	for (code = BTN_LEFT; code <= BTN_TASK; code++) {
		if (!libevdev_has_event_code(dev->evdev, EV_KEY, code))
			continue;

		libevdev_enable_event_code(evdev, EV_KEY, code, NULL);
		litest_event(dev, EV_KEY, code, 1);
		litest_event(dev, EV_SYN, SYN_REPORT, 0);
		libinput_dispatch(li);
	}

	litest_drain_events(li);
	litest_delete_device(dev);
	libinput_dispatch(li);

	for (code = BTN_LEFT; code <= BTN_TASK; code++) {
		if (!libevdev_has_event_code(evdev, EV_KEY, code))
			continue;

		litest_assert_tablet_button_event(li,
					  code,
					  LIBINPUT_BUTTON_STATE_RELEASED);
	}

	litest_assert_tablet_proximity_event(li,
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
	libevdev_free(evdev);
	libinput_unref(li);
}
END_TEST

202 203 204 205 206
START_TEST(tip_down_up)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
207
	struct libinput_event_tablet_tool *tablet_event;
208 209
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
210
		{ ABS_PRESSURE, 0 },
211 212 213 214 215 216
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

217 218 219 220
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
221
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
222
	litest_pop_event_frame(dev);
223 224

	libinput_dispatch(li);
225

226 227
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
228
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
229
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
230
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
231 232 233
	libinput_event_destroy(event);
	litest_assert_empty_queue(li);

234 235 236 237
	litest_axis_set_value(axes, ABS_DISTANCE, 10);
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
238
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
239
	litest_pop_event_frame(dev);
240 241 242 243

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
244
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
245
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
246
			 LIBINPUT_TABLET_TOOL_TIP_UP);
247 248 249 250 251 252 253 254 255 256 257 258
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

}
END_TEST

START_TEST(tip_down_prox_in)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
259
	struct libinput_event_tablet_tool *tablet_event;
260
	struct axis_replacement axes[] = {
261 262
		{ ABS_DISTANCE, 0 },
		{ ABS_PRESSURE, 30 },
263 264 265 266 267 268 269
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
270
	litest_tablet_motion(dev, 10, 10, axes);
271 272 273 274 275 276
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_pop_event_frame(dev);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
277
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
278
	ck_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tablet_event),
279
			 LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
280 281 282 283
	libinput_event_destroy(event);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
284
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
285
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
286
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
287 288 289 290 291 292 293
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

}
END_TEST

294 295 296
static inline bool
tablet_has_proxout_quirk(struct litest_device *dev)
{
297 298 299 300 301
	bool is_set = false;
	if (!quirks_get_bool(dev->quirks,
			     QUIRK_MODEL_TABLET_NO_PROXIMITY_OUT,
			     &is_set))
		return false;
302

303
	return is_set;
304 305
}

306 307 308 309 310
START_TEST(tip_up_prox_out)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
311
	struct libinput_event_tablet_tool *tablet_event;
312
	struct axis_replacement axes[] = {
313 314
		{ ABS_DISTANCE, 0 },
		{ ABS_PRESSURE, 30 },
315 316 317
		{ -1, -1 }
	};

318 319 320
	if (tablet_has_proxout_quirk(dev))
		return;

321 322 323 324 325
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_drain_events(li);

326 327
	litest_axis_set_value(axes, ABS_DISTANCE, 30);
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
328
	litest_push_event_frame(dev);
329
	litest_tablet_motion(dev, 10, 10, axes);
330 331 332 333 334 335 336
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
	litest_tablet_proximity_out(dev);
	litest_pop_event_frame(dev);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
337
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
338
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
339
			 LIBINPUT_TABLET_TOOL_TIP_UP);
340 341
	libinput_event_destroy(event);

342
	litest_timeout_tablet_proxout();
343 344 345
	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
346
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
347
	ck_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tablet_event),
348
			 LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
349 350 351 352 353 354 355 356 357 358 359 360
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

}
END_TEST

START_TEST(tip_up_btn_change)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
361
	struct libinput_event_tablet_tool *tablet_event;
362
	struct axis_replacement axes[] = {
363 364
		{ ABS_DISTANCE, 0 },
		{ ABS_PRESSURE, 30 },
365 366 367
		{ -1, -1 }
	};

368
	litest_push_event_frame(dev);
369
	litest_tablet_proximity_in(dev, 10, 10, axes);
370
	litest_tablet_motion(dev, 10, 10, axes);
371
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
372
	litest_pop_event_frame(dev);
373 374
	litest_drain_events(li);

375 376 377 378
	litest_axis_set_value(axes, ABS_DISTANCE, 30);
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 20, axes);
379 380
	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
381
	litest_pop_event_frame(dev);
382 383

	libinput_dispatch(li);
384

385 386
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
387 388 389
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
			 LIBINPUT_TABLET_TOOL_TIP_UP);
390 391
	libinput_event_destroy(event);

392 393
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
394
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
395
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event),
396
			 BTN_STYLUS);
397
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event),
398 399 400 401 402
			 LIBINPUT_BUTTON_STATE_PRESSED);
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

403 404 405 406
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
407
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
408
	litest_pop_event_frame(dev);
409 410 411
	litest_drain_events(li);

	/* same thing with a release at tip-up */
412 413 414 415
	litest_axis_set_value(axes, ABS_DISTANCE, 30);
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
416 417
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
418
	litest_pop_event_frame(dev);
419 420

	libinput_dispatch(li);
421

422 423
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
424 425 426
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
			 LIBINPUT_TABLET_TOOL_TIP_UP);
427 428
	libinput_event_destroy(event);

429 430
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
431
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
432
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event),
433
			 BTN_STYLUS);
434
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event),
435 436 437 438 439 440 441 442 443 444 445 446
			 LIBINPUT_BUTTON_STATE_RELEASED);
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(tip_down_btn_change)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
447
	struct libinput_event_tablet_tool *tablet_event;
448 449
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
450
		{ ABS_PRESSURE, 0 },
451 452 453 454 455 456
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

457 458 459 460
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 20, axes);
461 462
	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
463
	litest_pop_event_frame(dev);
464 465

	libinput_dispatch(li);
466

467 468
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
469
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
470
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
471
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
472 473 474 475 476
	libinput_event_destroy(event);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
477
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
478
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event),
479
			 BTN_STYLUS);
480
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event),
481 482 483 484 485
			 LIBINPUT_BUTTON_STATE_PRESSED);
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);

486 487 488 489
	litest_axis_set_value(axes, ABS_DISTANCE, 30);
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 20, axes);
490
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
491
	litest_pop_event_frame(dev);
492 493 494
	litest_drain_events(li);

	/* same thing with a release at tip-down */
495 496 497 498
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 20, axes);
499 500
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
501
	litest_pop_event_frame(dev);
502 503

	libinput_dispatch(li);
504

505 506
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
507
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
508
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
509
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
510 511 512 513 514
	libinput_event_destroy(event);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
515
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
516
	ck_assert_int_eq(libinput_event_tablet_tool_get_button(tablet_event),
517
			 BTN_STYLUS);
518
	ck_assert_int_eq(libinput_event_tablet_tool_get_button_state(tablet_event),
519 520 521 522 523 524 525 526 527 528 529 530
			 LIBINPUT_BUTTON_STATE_RELEASED);
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(tip_down_motion)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
531
	struct libinput_event_tablet_tool *tablet_event;
532 533
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
534
		{ ABS_PRESSURE, 0 },
535 536 537 538 539 540
		{ -1, -1 }
	};
	double x, y, last_x, last_y;

	litest_drain_events(li);

541 542 543 544 545 546 547 548 549 550
	litest_tablet_proximity_in(dev, 10, 10, axes);
	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
	last_x = libinput_event_tablet_tool_get_x(tablet_event);
	last_y = libinput_event_tablet_tool_get_y(tablet_event);
	libinput_event_destroy(event);

	/* move x/y on tip down, make sure x/y changed */
551 552
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 20);
553 554 555 556 557 558 559 560 561
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 70, 70, axes);
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_pop_event_frame(dev);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
562
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
563
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
564
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
565 566
	ck_assert(libinput_event_tablet_tool_x_has_changed(tablet_event));
	ck_assert(libinput_event_tablet_tool_y_has_changed(tablet_event));
567 568
	x = libinput_event_tablet_tool_get_x(tablet_event);
	y = libinput_event_tablet_tool_get_y(tablet_event);
569 570
	ck_assert_double_lt(last_x, x);
	ck_assert_double_lt(last_y, y);
571 572 573 574 575 576 577 578 579 580 581
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(tip_up_motion)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
582
	struct libinput_event_tablet_tool *tablet_event;
583
	struct axis_replacement axes[] = {
584
		{ ABS_DISTANCE, 0 },
585
		{ ABS_PRESSURE, 0 },
586 587 588 589 590 591 592
		{ -1, -1 }
	};
	double x, y, last_x, last_y;

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

593
	litest_axis_set_value(axes, ABS_PRESSURE, 20);
594 595
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 70, 70, axes);
596
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
597 598 599 600 601 602
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_pop_event_frame(dev);

	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
603
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
604 605
	last_x = libinput_event_tablet_tool_get_x(tablet_event);
	last_y = libinput_event_tablet_tool_get_y(tablet_event);
606 607
	libinput_event_destroy(event);

608 609 610 611 612 613 614
	/* move x/y on tip up, make sure x/y changed */
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 40, 40, axes);
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
	litest_pop_event_frame(dev);

615 616 617
	libinput_dispatch(li);
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
618
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
619
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
620
			 LIBINPUT_TABLET_TOOL_TIP_UP);
621 622
	ck_assert(libinput_event_tablet_tool_x_has_changed(tablet_event));
	ck_assert(libinput_event_tablet_tool_y_has_changed(tablet_event));
623 624
	x = libinput_event_tablet_tool_get_x(tablet_event);
	y = libinput_event_tablet_tool_get_y(tablet_event);
625 626
	ck_assert_double_ne(last_x, x);
	ck_assert_double_ne(last_y, y);
627 628 629 630 631 632
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

633 634 635 636 637
START_TEST(tip_state_proximity)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
638
	struct libinput_event_tablet_tool *tablet_event;
639 640
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
641
		{ ABS_PRESSURE, 0 },
642 643 644 645 646 647 648 649 650 651
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_tablet_proximity_in(dev, 10, 10, axes);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
652
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
653
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
654
			 LIBINPUT_TABLET_TOOL_TIP_UP);
655 656
	libinput_event_destroy(event);

657 658 659 660
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
661
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
662 663 664 665 666 667
	litest_pop_event_frame(dev);

	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_axis_set_value(axes, ABS_DISTANCE, 10);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
668
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
669 670
	litest_pop_event_frame(dev);

671 672 673 674 675
	litest_drain_events(li);

	litest_tablet_proximity_out(dev);
	libinput_dispatch(li);

676 677 678
	litest_timeout_tablet_proxout();
	libinput_dispatch(li);

679 680
	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
681
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
682
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
683
			 LIBINPUT_TABLET_TOOL_TIP_UP);
684 685 686 687 688 689 690 691 692
	libinput_event_destroy(event);
}
END_TEST

START_TEST(tip_state_axis)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
693
	struct libinput_event_tablet_tool *tablet_event;
694 695
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
696
		{ ABS_PRESSURE, 0 },
697 698 699 700 701 702 703 704 705 706 707
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

	litest_tablet_motion(dev, 70, 70, axes);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
708
					      LIBINPUT_EVENT_TABLET_TOOL_AXIS);
709
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
710
			 LIBINPUT_TABLET_TOOL_TIP_UP);
711 712
	libinput_event_destroy(event);

713 714 715 716
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 40, 40, axes);
717
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
718
	litest_pop_event_frame(dev);
719 720 721 722 723 724 725
	litest_drain_events(li);

	litest_tablet_motion(dev, 30, 30, axes);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
726
					      LIBINPUT_EVENT_TABLET_TOOL_AXIS);
727
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
728
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
729 730
	libinput_event_destroy(event);

731 732 733 734
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_axis_set_value(axes, ABS_DISTANCE, 10);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 40, 40, axes);
735
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
736
	litest_pop_event_frame(dev);
737 738 739 740 741 742 743
	litest_drain_events(li);

	litest_tablet_motion(dev, 40, 80, axes);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
744
					      LIBINPUT_EVENT_TABLET_TOOL_AXIS);
745
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
746
			 LIBINPUT_TABLET_TOOL_TIP_UP);
747 748 749 750 751 752 753 754 755 756 757
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(tip_state_button)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
758
	struct libinput_event_tablet_tool *tablet_event;
759 760
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
761
		{ ABS_PRESSURE, 0 },
762 763 764 765 766 767 768 769 770 771 772 773
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
774
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
775
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
776
			 LIBINPUT_TABLET_TOOL_TIP_UP);
777 778
	libinput_event_destroy(event);

779 780 781 782
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 40, 40, axes);
783
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
784
	litest_pop_event_frame(dev);
785 786 787 788 789 790 791 792
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
793
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
794
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
795
			 LIBINPUT_TABLET_TOOL_TIP_DOWN);
796 797
	libinput_event_destroy(event);

798 799 800 801
	litest_axis_set_value(axes, ABS_PRESSURE, 0);
	litest_axis_set_value(axes, ABS_DISTANCE, 10);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 40, 40, axes);
802
	litest_event(dev, EV_KEY, BTN_TOUCH, 0);
803
	litest_pop_event_frame(dev);
804 805 806 807 808 809 810 811
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
812
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
813
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
814
			 LIBINPUT_TABLET_TOOL_TIP_UP);
815 816 817 818 819 820 821 822
	libinput_event_destroy(event);

	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
823
					      LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
824
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
825
			 LIBINPUT_TABLET_TOOL_TIP_UP);
826 827 828 829 830 831
	libinput_event_destroy(event);

	litest_assert_empty_queue(li);
}
END_TEST

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
START_TEST(tip_up_on_delete)
{
	struct libinput *li = litest_create_context();
	struct litest_device *dev = litest_add_device(li, LITEST_WACOM_INTUOS);
	struct libinput_event *event;
	struct libinput_event_tablet_tool *tablet_event;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
		{ ABS_PRESSURE, 0 },
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_drain_events(li);

	litest_axis_set_value(axes, ABS_DISTANCE, 0);
	litest_axis_set_value(axes, ABS_PRESSURE, 30);
	litest_push_event_frame(dev);
	litest_tablet_motion(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_pop_event_frame(dev);

	litest_drain_events(li);
	litest_delete_device(dev);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	tablet_event = litest_is_tablet_event(event,
					      LIBINPUT_EVENT_TABLET_TOOL_TIP);
	ck_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tablet_event),
			 LIBINPUT_TABLET_TOOL_TIP_UP);
	libinput_event_destroy(event);

	libinput_unref(li);
}
END_TEST

869 870 871 872
START_TEST(proximity_in_out)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
873
	struct libinput_event_tablet_tool *tablet_event;
874 875 876 877 878 879
	struct libinput_event *event;
	bool have_tool_update = false,
	     have_proximity_out = false;

	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
880
		{ ABS_PRESSURE, 0 },
881 882 883
		{ -1, -1 }
	};

884
	litest_drain_events(li);
885 886 887 888 889 890

	litest_tablet_proximity_in(dev, 10, 10, axes);
	libinput_dispatch(li);

	while ((event = libinput_get_event(li))) {
		if (libinput_event_get_type(event) ==
891
		    LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) {
892
			struct libinput_tablet_tool * tool;
893

894 895
			ck_assert(!have_tool_update);
			have_tool_update = true;
896 897
			tablet_event = libinput_event_get_tablet_tool_event(event);
			tool = libinput_event_tablet_tool_get_tool(tablet_event);
898
			ck_assert_int_eq(libinput_tablet_tool_get_type(tool),
899
					 LIBINPUT_TABLET_TOOL_TYPE_PEN);
900 901 902 903 904 905 906 907
		}
		libinput_event_destroy(event);
	}
	ck_assert(have_tool_update);

	litest_tablet_proximity_out(dev);
	libinput_dispatch(li);

908 909 910
	litest_timeout_tablet_proxout();
	libinput_dispatch(li);

911 912
	while ((event = libinput_get_event(li))) {
		if (libinput_event_get_type(event) ==
913
		    LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY) {
914 915
			struct libinput_event_tablet_tool *t =
				libinput_event_get_tablet_tool_event(event);
916

917
			if (libinput_event_tablet_tool_get_proximity_state(t) ==
918
			    LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT)
919 920
				have_proximity_out = true;
		}
921 922 923 924 925 926

		libinput_event_destroy(event);
	}
	ck_assert(have_proximity_out);

	/* Proximity out must not emit axis events */
927
	litest_assert_empty_queue(li);
928 929 930
}
END_TEST

931 932 933 934 935 936
START_TEST(proximity_in_button_down)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
937
		{ ABS_PRESSURE, 0 },
938 939 940 941 942 943 944 945 946 947 948 949
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_pop_event_frame(dev);
	libinput_dispatch(li);

	litest_assert_tablet_proximity_event(li,
950
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
951 952 953 954 955 956 957 958 959 960 961 962 963
	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_PRESSED);
	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(proximity_out_button_up)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
964
		{ ABS_PRESSURE, 0 },
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
		{ -1, -1 }
	};

	litest_tablet_proximity_in(dev, 10, 10, axes);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_out(dev);
	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_pop_event_frame(dev);
	libinput_dispatch(li);

	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_RELEASED);
	litest_assert_tablet_proximity_event(li,
984
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
985 986 987 988
	litest_assert_empty_queue(li);
}
END_TEST

989 990 991 992
START_TEST(proximity_out_clear_buttons)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
993
	struct libinput_event_tablet_tool *tablet_event;
994 995 996 997 998
	struct libinput_event *event;
	uint32_t button;

	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
999
		{ ABS_PRESSURE, 0 },
1000 1001 1002
		{ -1, -1 }
	};

1003
	litest_drain_events(li);
1004 1005 1006 1007

	/* Test that proximity out events send button releases for any currently
	 * pressed stylus buttons
	 */
1008
	for (button = BTN_TOUCH + 1; button <= BTN_STYLUS2; button++) {
1009
		bool button_released = false;
1010
		uint32_t event_button = 0;
1011 1012
		enum libinput_button_state state;

1013 1014 1015
		if (!libevdev_has_event_code(dev->evdev, EV_KEY, button))
			continue;

1016 1017 1018 1019
		litest_tablet_proximity_in(dev, 10, 10, axes);
		litest_event(dev, EV_KEY, button, 1);
		litest_event(dev, EV_SYN, SYN_REPORT, 0);
		litest_tablet_proximity_out(dev);
1020
		libinput_dispatch(li);
1021

1022
		litest_timeout_tablet_proxout();
1023 1024 1025
		libinput_dispatch(li);

		while ((event = libinput_get_event(li))) {
1026
			tablet_event = libinput_event_get_tablet_tool_event(event);
1027 1028

			if (libinput_event_get_type(event) ==
1029
			    LIBINPUT_EVENT_TABLET_TOOL_BUTTON) {
1030

1031 1032
				event_button = libinput_event_tablet_tool_get_button(tablet_event);
				state = libinput_event_tablet_tool_get_button_state(tablet_event);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

				if (event_button == button &&
				    state == LIBINPUT_BUTTON_STATE_RELEASED)
					button_released = true;
			}

			libinput_event_destroy(event);
		}

		ck_assert_msg(button_released,
1043 1044
			      "Button %s (%d) was not released.",
			      libevdev_event_code_get_name(EV_KEY, button),
1045 1046
			      event_button);
	}
1047 1048

	litest_assert_empty_queue(li);
1049 1050 1051
}
END_TEST

1052 1053 1054 1055
START_TEST(proximity_has_axes)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
1056
	struct libinput_event_tablet_tool *tablet_event;
1057
	struct libinput_event *event;
1058
	struct libinput_tablet_tool *tool;
1059 1060
	double x, y,
	       distance;
1061 1062 1063
	double last_x, last_y,
	       last_distance = 0.0,
	       last_tx = 0.0, last_ty = 0.0;
1064 1065 1066

	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
1067
		{ ABS_PRESSURE, 0 },
1068 1069 1070 1071 1072
		{ ABS_TILT_X, 10 },
		{ ABS_TILT_Y, 10 },
		{ -1, -1}
	};

1073
	litest_drain_events(li);
1074 1075

	litest_tablet_proximity_in(dev, 10, 10, axes);
1076
	libinput_dispatch(li);
1077 1078

	event = libinput_get_event(li);
1079 1080
	tablet_event = litest_is_tablet_event(event,
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1081
	tool = libinput_event_tablet_tool_get_tool(tablet_event);
1082

1083 1084
	ck_assert(libinput_event_tablet_tool_x_has_changed(tablet_event));
	ck_assert(libinput_event_tablet_tool_y_has_changed(tablet_event));
1085

1086 1087
	x = libinput_event_tablet_tool_get_x(tablet_event);
	y = libinput_event_tablet_tool_get_y(tablet_event);
1088 1089 1090 1091

	litest_assert_double_ne(x, 0);
	litest_assert_double_ne(y, 0);

1092
	if (libinput_tablet_tool_has_distance(tool)) {
1093 1094
		ck_assert(libinput_event_tablet_tool_distance_has_changed(
				tablet_event));
1095

1096
		distance = libinput_event_tablet_tool_get_distance(tablet_event);
1097 1098 1099
		litest_assert_double_ne(distance, 0);
	}

1100
	if (libinput_tablet_tool_has_tilt(tool)) {
1101 1102 1103 1104
		ck_assert(libinput_event_tablet_tool_tilt_x_has_changed(
				tablet_event));
		ck_assert(libinput_event_tablet_tool_tilt_y_has_changed(
				tablet_event));
1105

1106 1107
		x = libinput_event_tablet_tool_get_tilt_x(tablet_event);
		y = libinput_event_tablet_tool_get_tilt_y(tablet_event);
1108 1109 1110 1111 1112 1113 1114 1115

		litest_assert_double_ne(x, 0);
		litest_assert_double_ne(y, 0);
	}

	litest_assert_empty_queue(li);
	libinput_event_destroy(event);

1116 1117 1118
	litest_axis_set_value(axes, ABS_DISTANCE, 20);
	litest_axis_set_value(axes, ABS_TILT_X, 15);
	litest_axis_set_value(axes, ABS_TILT_Y, 25);
1119 1120 1121 1122 1123 1124 1125

	/* work around axis smoothing */
	litest_tablet_motion(dev, 20, 30, axes);
	litest_tablet_motion(dev, 20, 29, axes);
	litest_tablet_motion(dev, 20, 31, axes);
	litest_drain_events(li);

1126 1127 1128
	litest_tablet_motion(dev, 20, 30, axes);
	libinput_dispatch(li);
	event = libinput_get_event(li);
1129
	tablet_event = litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_AXIS);
1130

1131 1132
	last_x = libinput_event_tablet_tool_get_x(tablet_event);
	last_y = libinput_event_tablet_tool_get_y(tablet_event);
1133
	if (libinput_tablet_tool_has_distance(tool))
1134 1135
		last_distance = libinput_event_tablet_tool_get_distance(
					     tablet_event);
1136
	if (libinput_tablet_tool_has_tilt(tool)) {
1137 1138
		last_tx = libinput_event_tablet_tool_get_tilt_x(tablet_event);
		last_ty = libinput_event_tablet_tool_get_tilt_y(tablet_event);
1139 1140 1141 1142
	}

	libinput_event_destroy(event);

1143 1144
	/* Make sure that the axes are still present on proximity out */
	litest_tablet_proximity_out(dev);
1145
	libinput_dispatch(li);
1146

1147 1148 1149
	litest_timeout_tablet_proxout();
	libinput_dispatch(li);

1150
	event = libinput_get_event(li);
1151 1152
	tablet_event = litest_is_tablet_event(event,
					      LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1153
	tool = libinput_event_tablet_tool_get_tool(tablet_event);
1154

1155 1156
	ck_assert(!libinput_event_tablet_tool_x_has_changed(tablet_event));
	ck_assert(!libinput_event_tablet_tool_y_has_changed(tablet_event));
1157

1158 1159
	x = libinput_event_tablet_tool_get_x(tablet_event);
	y = libinput_event_tablet_tool_get_y(tablet_event);
1160 1161 1162 1163
	litest_assert_double_ge(x, last_x - 1);
	litest_assert_double_le(x, last_x + 1);
	litest_assert_double_ge(y, last_y - 1);
	litest_assert_double_le(y, last_y + 1);
1164

1165
	if (libinput_tablet_tool_has_distance(tool)) {
1166 1167
		ck_assert(!libinput_event_tablet_tool_distance_has_changed(
				tablet_event));
1168

1169 1170
		distance = libinput_event_tablet_tool_get_distance(
						tablet_event);
1171
		litest_assert_double_eq(distance, last_distance);
1172 1173
	}

1174
	if (libinput_tablet_tool_has_tilt(tool)) {
1175 1176 1177 1178
		ck_assert(!libinput_event_tablet_tool_tilt_x_has_changed(
				tablet_event));
		ck_assert(!libinput_event_tablet_tool_tilt_y_has_changed(
				tablet_event));
1179

1180 1181
		x = libinput_event_tablet_tool_get_tilt_x(tablet_event);
		y = libinput_event_tablet_tool_get_tilt_y(tablet_event);
1182

1183 1184
		litest_assert_double_eq(x, last_tx);
		litest_assert_double_eq(y, last_ty);
1185 1186 1187 1188 1189 1190 1191
	}

	litest_assert_empty_queue(li);
	libinput_event_destroy(event);
}
END_TEST

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
START_TEST(proximity_range_enter)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 90 },
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1);
	litest_pop_event_frame(dev);
	litest_assert_empty_queue(li);

1209
	litest_axis_set_value(axes, ABS_DISTANCE, 20);
1210 1211 1212 1213
	litest_tablet_motion(dev, 10, 10, axes);
	libinput_dispatch(li);

	litest_assert_tablet_proximity_event(li,
1214
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
1215 1216

	litest_axis_set_value(axes, ABS_DISTANCE, 90);
1217 1218 1219
	litest_tablet_motion(dev, 10, 10, axes);
	libinput_dispatch(li);
	litest_assert_tablet_proximity_event(li,
1220
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

	litest_tablet_proximity_out(dev);
	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(proximity_range_in_out)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 20 },
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1);
	litest_pop_event_frame(dev);
	libinput_dispatch(li);
	litest_assert_tablet_proximity_event(li,
1244
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
1245

1246
	litest_axis_set_value(axes, ABS_DISTANCE, 90);
1247 1248 1249
	litest_tablet_motion(dev, 10, 10, axes);
	libinput_dispatch(li);
	litest_assert_tablet_proximity_event(li,
1250
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
1251 1252 1253 1254

	litest_tablet_motion(dev, 30, 30, axes);
	litest_assert_empty_queue(li);

1255
	litest_axis_set_value(axes, ABS_DISTANCE, 20);
1256 1257 1258
	litest_tablet_motion(dev, 10, 10, axes);
	libinput_dispatch(li);
	litest_assert_tablet_proximity_event(li,
1259
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
1260 1261 1262

	litest_tablet_proximity_out(dev);
	litest_assert_tablet_proximity_event(li,
1263
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(proximity_range_button_click)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 90 },
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1);
	litest_pop_event_frame(dev);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);
	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	litest_tablet_proximity_out(dev);
	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(proximity_range_button_press)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 20 },
		{ -1, -1 }
	};

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1);
	litest_pop_event_frame(dev);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_PRESSED);

1320
	litest_axis_set_value(axes, ABS_DISTANCE, 90);
1321 1322 1323 1324 1325 1326 1327 1328
	litest_tablet_motion(dev, 15, 15, axes);
	libinput_dispatch(li);

	/* expect fake button release */
	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_RELEASED);
	litest_assert_tablet_proximity_event(li,
1329
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	litest_tablet_proximity_out(dev);
	litest_assert_empty_queue(li);
}
END_TEST

START_TEST(proximity_range_button_release)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 90 },
		{ -1, -1 }
	};

	litest_push_event_frame(dev);
	litest_tablet_proximity_in(dev, 10, 10, axes);
	litest_event(dev, EV_KEY, BTN_TOOL_MOUSE, 1);
	litest_pop_event_frame(dev);
	litest_drain_events(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_assert_empty_queue(li);

1359
	litest_axis_set_value(axes, ABS_DISTANCE, 20);
1360 1361 1362 1363
	litest_tablet_motion(dev, 15, 15, axes);
	libinput_dispatch(li);

	litest_assert_tablet_proximity_event(li,
1364
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN);
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	/* expect fake button press */
	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_PRESSED);
	litest_assert_empty_queue(li);

	litest_event(dev, EV_KEY, BTN_STYLUS, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);
	litest_assert_tablet_button_event(li,
					  BTN_STYLUS,
					  LIBINPUT_BUTTON_STATE_RELEASED);

	litest_tablet_proximity_out(dev);
	litest_assert_tablet_proximity_event(li,
1380
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
1381 1382 1383
}
END_TEST

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
START_TEST(proximity_out_on_delete)
{
	struct libinput *li = litest_create_context();
	struct litest_device *dev = litest_add_device(li, LITEST_WACOM_INTUOS);

	litest_tablet_proximity_in(dev, 10, 10, NULL);
	litest_drain_events(li);

	litest_delete_device(dev);
	libinput_dispatch(li);

	litest_assert_tablet_proximity_event(li,
					     LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT);
	libinput_unref(li);
}
END_TEST

1401 1402 1403 1404
START_TEST(motion)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
1405
	struct libinput_event_tablet_tool *tablet_event;
1406 1407
	struct libinput_event *event;
	int test_x, test_y;
1408
	double last_reported_x = 0, last_reported_y = 0;
1409 1410 1411
	enum libinput_event_type type;
	struct axis_replacement axes[] = {
		{ ABS_DISTANCE, 10 },
1412
		{ ABS_PRESSURE, 0 },
1413 1414 1415 1416 1417 1418 1419 1420
		{ -1, -1 }
	};

	litest_drain_events(li);

	litest_tablet_proximity_in(dev, 5, 100, axes);
	libinput_dispatch(li);

1421
	event = libinput_get_event(li);
1422

1423
	do {
1424 1425 1426
		bool x_changed, y_changed;
		double reported_x, reported_y;

1427
		tablet_event = libinput_event_get_tablet_tool_event(event);
1428
		ck_assert_int_eq(libinput_event_get_type(event),
1429
				 LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1430

1431 1432 1433 1434
		x_changed = libinput_event_tablet_tool_x_has_changed(
							tablet_event);
		y_changed = libinput_event_tablet_tool_y_has_changed(
							tablet_event);
1435

1436 1437
		ck_assert(x_changed);
		ck_assert(y_changed);
1438

1439 1440
		reported_x = libinput_event_tablet_tool_get_x(tablet_event);
		reported_y = libinput_event_tablet_tool_get_y(tablet_event);
1441

1442
		litest_assert_double_lt(reported_x, reported_y);
1443

1444 1445
		last_reported_x = reported_x;
		last_reported_y = reported_y;
1446 1447

		libinput_event_destroy(event);
1448 1449
		event = libinput_get_event(li);
	} while (event != NULL);
1450 1451 1452 1453 1454 1455 1456

	for (test_x = 10, test_y = 90;
	     test_x <= 100;
	     test_x += 10, test_y -= 10) {
		bool x_changed, y_changed;
		double reported_x, reported_y;

1457
		litest_tablet_motion(dev, test_x, test_y, axes);
1458 1459 1460
		libinput_dispatch(li);

		while ((event = libinput_get_event(li))) {
1461
			tablet_event = libinput_event_get_tablet_tool_event(event);
1462 1463
			type = libinput_event_get_type(event);

1464
			if (type == LIBINPUT_EVENT_TABLET_TOOL_AXIS) {
1465 1466 1467 1468
				x_changed = libinput_event_tablet_tool_x_has_changed(
							    tablet_event);
				y_changed = libinput_event_tablet_tool_y_has_changed(
							    tablet_event);
1469 1470 1471 1472

				ck_assert(x_changed);
				ck_assert(y_changed);

1473 1474 1475 1476
				reported_x = libinput_event_tablet_tool_get_x(
								tablet_event);
				reported_y = libinput_event_tablet_tool_get_y(
								tablet_event);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492

				litest_assert_double_gt(reported_x,
							last_reported_x);
				litest_assert_double_lt(reported_y,
							last_reported_y);

				last_reported_x = reported_x;
				last_reported_y = reported_y;
			}

			libinput_event_destroy(event);
		}
	}
}
END_TEST

1493 1494
START_TEST(left_handed)
{
1495
#if HAVE_LIBWACOM
1496 1497 1498
	struct litest_device *dev = litest_current_device