evdev-mt-touchpad.h 15.4 KB
Newer Older
1
/*
Peter Hutterer's avatar
Peter Hutterer committed
2
 * Copyright © 2014-2015 Red Hat, Inc.
3
 *
4 5 6 7 8 9
 * 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:
10
 *
11 12 13 14 15 16 17 18 19 20 21
 * 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.
22 23 24 25 26 27 28 29 30
 */

#ifndef EVDEV_MT_TOUCHPAD_H
#define EVDEV_MT_TOUCHPAD_H

#include <stdbool.h>

#include "evdev.h"
#include "filter.h"
31
#include "timer.h"
32 33

#define TOUCHPAD_HISTORY_LENGTH 4
34
#define TOUCHPAD_MIN_SAMPLES 4
35

36
/* Convert mm to a distance normalized to DEFAULT_MOUSE_DPI */
37
#define TP_MM_TO_DPI_NORMALIZED(mm) (DEFAULT_MOUSE_DPI/25.4 * mm)
38

39 40 41 42 43
enum touchpad_event {
	TOUCHPAD_EVENT_NONE		= 0,
	TOUCHPAD_EVENT_MOTION		= (1 << 0),
	TOUCHPAD_EVENT_BUTTON_PRESS	= (1 << 1),
	TOUCHPAD_EVENT_BUTTON_RELEASE	= (1 << 2),
44
	TOUCHPAD_EVENT_OTHERAXIS	= (1 << 3),
45 46
};

47 48
enum touch_state {
	TOUCH_NONE = 0,
49 50 51 52 53
	TOUCH_HOVERING = 1,
	TOUCH_BEGIN = 2,
	TOUCH_UPDATE = 3,
	TOUCH_MAYBE_END = 4,
	TOUCH_END = 5,
54 55
};

56 57 58
enum touch_palm_state {
	PALM_NONE = 0,
	PALM_EDGE,
59
	PALM_TYPING,
60
	PALM_TRACKPOINT,
61
	PALM_TOOL_PALM,
62
	PALM_PRESSURE,
63
	PALM_TOUCH_SIZE,
64
	PALM_ARBITRATION,
65 66
};

67
enum button_event {
68
	BUTTON_EVENT_IN_BOTTOM_R = 30,
69
	BUTTON_EVENT_IN_BOTTOM_M,
70 71 72 73 74 75 76 77 78
	BUTTON_EVENT_IN_BOTTOM_L,
	BUTTON_EVENT_IN_TOP_R,
	BUTTON_EVENT_IN_TOP_M,
	BUTTON_EVENT_IN_TOP_L,
	BUTTON_EVENT_IN_AREA,
	BUTTON_EVENT_UP,
	BUTTON_EVENT_PRESS,
	BUTTON_EVENT_RELEASE,
	BUTTON_EVENT_TIMEOUT,
79 80 81
};

enum button_state {
82 83 84 85 86 87 88
	BUTTON_STATE_NONE,
	BUTTON_STATE_AREA,
	BUTTON_STATE_BOTTOM,
	BUTTON_STATE_TOP,
	BUTTON_STATE_TOP_NEW,
	BUTTON_STATE_TOP_TO_IGNORE,
	BUTTON_STATE_IGNORE,
89 90
};

91 92 93 94 95 96 97
enum tp_tap_state {
	TAP_STATE_IDLE = 4,
	TAP_STATE_TOUCH,
	TAP_STATE_HOLD,
	TAP_STATE_TAPPED,
	TAP_STATE_TOUCH_2,
	TAP_STATE_TOUCH_2_HOLD,
98
	TAP_STATE_TOUCH_2_RELEASE,
99 100 101
	TAP_STATE_TOUCH_3,
	TAP_STATE_TOUCH_3_HOLD,
	TAP_STATE_DRAGGING_OR_DOUBLETAP,
102
	TAP_STATE_DRAGGING_OR_TAP,
103 104 105
	TAP_STATE_DRAGGING,
	TAP_STATE_DRAGGING_WAIT,
	TAP_STATE_DRAGGING_2,
106 107
	TAP_STATE_MULTITAP,
	TAP_STATE_MULTITAP_DOWN,
108
	TAP_STATE_MULTITAP_PALM,
109 110 111
	TAP_STATE_DEAD, /**< finger count exceeded */
};

112 113 114 115 116 117
enum tp_tap_touch_state {
	TAP_TOUCH_STATE_IDLE = 16,	/**< not in touch */
	TAP_TOUCH_STATE_TOUCH,		/**< touching, may tap */
	TAP_TOUCH_STATE_DEAD,		/**< exceeded motion/timeout */
};

118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* For edge scrolling, so we only care about right and bottom */
enum tp_edge {
	EDGE_NONE = 0,
	EDGE_RIGHT = (1 << 0),
	EDGE_BOTTOM = (1 << 1),
};

enum tp_edge_scroll_touch_state {
	EDGE_SCROLL_TOUCH_STATE_NONE,
	EDGE_SCROLL_TOUCH_STATE_EDGE_NEW,
	EDGE_SCROLL_TOUCH_STATE_EDGE,
	EDGE_SCROLL_TOUCH_STATE_AREA,
};

132 133 134 135 136
enum tp_gesture_state {
	GESTURE_STATE_NONE,
	GESTURE_STATE_UNKNOWN,
	GESTURE_STATE_SCROLL,
	GESTURE_STATE_PINCH,
137
	GESTURE_STATE_SWIPE,
138 139
};

140 141 142 143 144 145
enum tp_thumb_state {
	THUMB_STATE_NO,
	THUMB_STATE_YES,
	THUMB_STATE_MAYBE,
};

146
struct tp_touch {
147
	struct tp_dispatch *tp;
148
	unsigned int index;
149
	enum touch_state state;
150
	bool has_ended;				/* TRACKING_ID == -1 */
151
	bool dirty;
152
	struct device_coords point;
153
	uint64_t time;
154
	int pressure;
155
	bool is_tool_palm; /* MT_TOOL_PALM */
156
	int major, minor;
157

158 159 160
	bool was_down; /* if distance == 0, false for pure hovering
			  touches */

161 162 163 164 165 166 167 168 169
	struct {
		/* A quirk mostly used on Synaptics touchpads. In a
		   transition to/from fake touches > num_slots, the current
		   event data is likely garbage and the subsequent event
		   is likely too. This marker tells us to reset the motion
		   history again -> this effectively swallows any motion */
		bool reset_motion_history;
	} quirks;

170
	struct {
171 172 173 174
		struct tp_history_point {
			uint64_t time;
			struct device_coords point;
		} samples[TOUCHPAD_HISTORY_LENGTH];
175 176 177 178
		unsigned int index;
		unsigned int count;
	} history;

179 180
	struct {
		struct device_coords center;
181
		uint8_t x_motion_history;
182
	} hysteresis;
183 184 185 186 187 188 189

	/* A pinned touchpoint is the one that pressed the physical button
	 * on a clickpad. After the release, it won't move until the center
	 * moves more than a threshold away from the original coordinates
	 */
	struct {
		bool is_pinned;
190
		struct device_coords center;
191
	} pinned;
192 193 194 195 196 197

	/* Software-button state and timeout if applicable */
	struct {
		enum button_state state;
		/* We use button_event here so we can use == on events */
		enum button_event curr;
198
		struct libinput_timer timer;
199
	} button;
200 201 202

	struct {
		enum tp_tap_touch_state state;
203
		struct device_coords initial;
204
		bool is_thumb;
205
		bool is_palm;
206
	} tap;
207

208
	struct {
209
		enum tp_edge_scroll_touch_state edge_state;
210 211 212
		uint32_t edge;
		int direction;
		struct libinput_timer timer;
213
		struct device_coords initial;
214 215
	} scroll;

216
	struct {
217
		enum touch_palm_state state;
218
		struct device_coords first; /* first coordinates if is_palm == true */
219
		uint64_t time; /* first timestamp if is_palm == true */
220
	} palm;
221 222 223 224

	struct {
		struct device_coords initial;
	} gesture;
225 226 227 228

	struct {
		enum tp_thumb_state state;
		uint64_t first_touch_time;
229
		struct device_coords initial;
230
	} thumb;
231 232 233 234 235

	struct {
		double last_speed; /* speed in mm/s at last sample */
		unsigned int exceeded_count;
	} speed;
236 237
};

238 239 240 241 242 243 244 245
enum suspend_trigger {
	SUSPEND_NO_FLAG         = 0x0,
	SUSPEND_EXTERNAL_MOUSE  = 0x1,
	SUSPEND_SENDEVENTS      = 0x2,
	SUSPEND_LID             = 0x4,
	SUSPEND_TABLET_MODE     = 0x8,
};

246 247 248 249
struct tp_dispatch {
	struct evdev_dispatch base;
	struct evdev_device *device;
	unsigned int nfingers_down;		/* number of fingers down */
250
	unsigned int old_nfingers_down;		/* previous no fingers down */
251
	unsigned int slot;			/* current slot */
252
	bool has_mt;
253
	bool semi_mt;
254

255 256
	uint32_t suspend_reason;

257 258 259 260 261
	/* pen/touch arbitration */
	struct {
		bool in_arbitration;
		struct libinput_timer arbitration_timer;
	} arbitration;
Peter Hutterer's avatar
Peter Hutterer committed
262

263
	unsigned int num_slots;			/* number of slots */
264
	unsigned int ntouches;			/* no slots inc. fakes */
265
	struct tp_touch *touches;		/* len == ntouches */
266 267 268 269 270 271
	/* bit 0: BTN_TOUCH
	 * bit 1: BTN_TOOL_FINGER
	 * bit 2: BTN_TOOL_DOUBLETAP
	 * ...
	 */
	unsigned int fake_touches;
272

273 274 275 276 277 278 279 280
	/* if pressure goes above high -> touch down,
	   if pressure then goes below low -> touch up */
	struct {
		bool use_pressure;
		int high;
		int low;
	} pressure;

281 282 283 284 285 286 287 288 289 290 291
	/* If touch size (either axis) goes above high -> touch down,
	   if touch size (either axis) goes below low -> touch up */
	struct  {
		bool use_touch_size;
		int high;
		int low;

		/* convert device units to angle */
		double orientation_to_angle;
	} touch_size;

292
	struct {
293
		bool enabled;
294
		struct device_coords margin;
295 296
		unsigned int other_event_count;
		uint64_t last_motion_time;
297
	} hysteresis;
298 299

	struct {
300 301
		double x_scale_coeff;
		double y_scale_coeff;
302
		double xy_scale_coeff;
303
	} accel;
304

305
	struct {
306
		bool enabled;
307 308 309 310
		bool started;
		unsigned int finger_count;
		unsigned int finger_count_pending;
		struct libinput_timer finger_count_switch_timer;
311
		enum tp_gesture_state state;
312 313 314 315 316 317
		struct tp_touch *touches[2];
		uint64_t initial_time;
		double initial_distance;
		double prev_scale;
		double angle;
		struct device_float_coords center;
318 319
	} gesture;

320
	struct {
321
		bool is_clickpad;		/* true for clickpads */
322
		bool has_topbuttons;
323
		bool use_clickfinger;		/* number of fingers decides button number */
324
		bool click_pending;
325 326
		uint32_t state;
		uint32_t old_state;
327 328 329 330
		struct {
			double x_scale_coeff;
			double y_scale_coeff;
		} motion_dist;			/* for pinned touches */
331
		unsigned int active;		/* currently active button, for release event */
332
		bool active_is_topbutton;	/* is active a top button? */
333

334 335 336
		/* Only used for clickpads. The software button areas are
		 * always 2 horizontal stripes across the touchpad.
		 * The buttons are split according to the edge settings.
337
		 */
338
		struct {
339 340
			int32_t top_edge;	/* in device coordinates */
			int32_t rightbutton_left_edge; /* in device coordinates */
341
			int32_t middlebutton_left_edge; /* in device coordinates */
342
		} bottom_area;
343

344
		struct {
345 346 347
			int32_t bottom_edge;	/* in device coordinates */
			int32_t rightbutton_left_edge; /* in device coordinates */
			int32_t leftbutton_right_edge; /* in device coordinates */
348
		} top_area;
349 350

		struct evdev_device *trackpoint;
351 352 353 354

		enum libinput_config_click_method click_method;
		struct libinput_device_config_click_method config_method;
	} buttons;
355

356
	struct {
357 358
		struct libinput_device_config_scroll_method config_method;
		enum libinput_config_scroll_method method;
359 360
		int32_t right_edge;		/* in device coordinates */
		int32_t bottom_edge;		/* in device coordinates */
361 362
	} scroll;

363
	enum touchpad_event queued;
364 365

	struct {
366
		struct libinput_device_config_tap config;
367
		bool enabled;
368
		bool suspended;
369
		struct libinput_timer timer;
370
		enum tp_tap_state state;
371
		uint32_t buttons_pressed;
372 373
		uint64_t saved_press_time,
			 saved_release_time;
374

375 376 377
		enum libinput_config_tap_button_map map;
		enum libinput_config_tap_button_map want_map;

378
		bool drag_enabled;
379
		bool drag_lock_enabled;
380 381

		unsigned int nfingers_down;	/* number of fingers down for tapping (excl. thumb/palm) */
382
	} tap;
383 384

	struct {
385 386
		int32_t right_edge;		/* in device coordinates */
		int32_t left_edge;		/* in device coordinates */
387
		int32_t upper_edge;		/* in device coordinates */
388 389 390 391

		bool trackpoint_active;
		struct libinput_event_listener trackpoint_listener;
		struct libinput_timer trackpoint_timer;
392
		uint64_t trackpoint_last_event_time;
393
		uint32_t trackpoint_event_count;
394
		bool monitor_trackpoint;
395 396

		bool use_mt_tool;
397 398 399

		bool use_pressure;
		int pressure_threshold;
400 401 402

		bool use_size;
		int size_threshold;
403
	} palm;
404 405 406 407

	struct {
		struct libinput_device_config_send_events config;
		enum libinput_config_send_events_mode current_mode;
408
	} sendevents;
409

410
	struct {
411 412 413
		struct libinput_device_config_dwt config;
		bool dwt_enabled;

414 415 416 417 418
		/* We have to allow for more than one device node to be the
		 * internal dwt keyboard (Razer Blade). But they're the same
		 * physical device, so we don't care about per-keyboard
		 * key/modifier masks.
		 */
419
		struct list paired_keyboard_list;
420

421
		unsigned long key_mask[NLONGS(KEY_CNT)];
422
		unsigned long mod_mask[NLONGS(KEY_CNT)];
423 424
		bool keyboard_active;
		struct libinput_timer keyboard_timer;
425
		uint64_t keyboard_last_press_time;
426
	} dwt;
427 428 429 430

	struct {
		bool detect_thumbs;
		int threshold;
431 432
		int upper_thumb_line;
		int lower_thumb_line;
433
	} thumb;
434 435 436 437 438 439 440 441 442 443 444

	struct {
		/* A quirk used on the T450 series Synaptics hardware.
		 * Slowly moving the finger causes multiple events with only
		 * ABS_MT_PRESSURE but no x/y information. When the x/y
		 * event comes, it will be a jump of ~20 units. We use the
		 * below to count non-motion events to discard that first
		 * event with the jump.
		 */
		unsigned int nonmotion_event_count;
	} quirks;
445 446

	struct {
447
		struct libinput_event_listener listener;
448 449
		struct evdev_device *lid_switch;
	} lid_switch;
450 451 452 453 454

	struct {
		struct libinput_event_listener listener;
		struct evdev_device *tablet_mode_switch;
	} tablet_mode_switch;
455 456
};

457 458 459 460 461
static inline struct tp_dispatch*
tp_dispatch(struct evdev_dispatch *dispatch)
{
	evdev_verify_dispatch_type(dispatch, DISPATCH_TOUCHPAD);

462
	return container_of(dispatch, struct tp_dispatch, base);
463 464
}

465 466 467
#define tp_for_each_touch(_tp, _t) \
	for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)

468
static inline struct libinput*
469
tp_libinput_context(const struct tp_dispatch *tp)
470
{
471
	return evdev_libinput_context(tp->device);
472 473
}

474
static inline struct normalized_coords
475 476
tp_normalize_delta(const struct tp_dispatch *tp,
		   struct device_float_coords delta)
477
{
478 479
	struct normalized_coords normalized;

480 481
	normalized.x = delta.x * tp->accel.x_scale_coeff;
	normalized.y = delta.y * tp->accel.y_scale_coeff;
482 483

	return normalized;
484 485
}

486 487 488 489 490 491 492 493 494 495 496 497
static inline struct phys_coords
tp_phys_delta(const struct tp_dispatch *tp,
	      struct device_float_coords delta)
{
	struct phys_coords mm;

	mm.x = delta.x / tp->device->abs.absinfo_x->resolution;
	mm.y = delta.y / tp->device->abs.absinfo_y->resolution;

	return mm;
}

498
/**
499 500
 * Takes a set of device coordinates, returns that set of coordinates in the
 * x-axis' resolution.
501 502
 */
static inline struct device_float_coords
503 504
tp_scale_to_xaxis(const struct tp_dispatch *tp,
		  struct device_float_coords delta)
505 506 507
{
	struct device_float_coords raw;

508 509
	raw.x = delta.x;
	raw.y = delta.y * tp->accel.xy_scale_coeff;
510 511 512 513

	return raw;
}

514
struct device_coords
515
tp_get_delta(struct tp_touch *t);
516

517
struct normalized_coords
518
tp_filter_motion(struct tp_dispatch *tp,
519
		 const struct device_float_coords *unaccelerated,
520
		 uint64_t time);
Peter Hutterer's avatar
Peter Hutterer committed
521

522 523
struct normalized_coords
tp_filter_motion_unaccelerated(struct tp_dispatch *tp,
524
			       const struct device_float_coords *unaccelerated,
525
			       uint64_t time);
526

527
bool
528
tp_touch_active(const struct tp_dispatch *tp, const struct tp_touch *t);
529

530
int
531
tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time);
532

533 534 535
void
tp_tap_post_process_state(struct tp_dispatch *tp);

536
void
537 538
tp_init_tap(struct tp_dispatch *tp);

539
void
540
tp_remove_tap(struct tp_dispatch *tp);
541

542
void
543 544
tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);

545
void
546 547 548
tp_init_top_softbuttons(struct tp_dispatch *tp,
			struct evdev_device *device,
			double topbutton_size_mult);
549

550
void
551
tp_remove_buttons(struct tp_dispatch *tp);
552

553
void
554 555
tp_process_button(struct tp_dispatch *tp,
		  const struct input_event *e,
556
		  uint64_t time);
557

558 559 560 561
void
tp_release_all_buttons(struct tp_dispatch *tp,
		       uint64_t time);

562
int
563
tp_post_button_events(struct tp_dispatch *tp, uint64_t time);
564

565
void
566
tp_button_handle_state(struct tp_dispatch *tp, uint64_t time);
567

568
bool
569 570
tp_button_touch_active(const struct tp_dispatch *tp,
		       const struct tp_touch *t);
571

572
bool
573 574
tp_button_is_inside_softbutton_area(const struct tp_dispatch *tp,
				    const struct tp_touch *t);
575

576 577 578 579
void
tp_release_all_taps(struct tp_dispatch *tp,
		    uint64_t time);

580 581 582 583 584 585
void
tp_tap_suspend(struct tp_dispatch *tp, uint64_t time);

void
tp_tap_resume(struct tp_dispatch *tp, uint64_t time);

586
bool
587
tp_tap_dragging(const struct tp_dispatch *tp);
588

589
void
590 591 592
tp_edge_scroll_init(struct tp_dispatch *tp, struct evdev_device *device);

void
593
tp_remove_edge_scroll(struct tp_dispatch *tp);
594 595 596 597 598 599 600 601 602 603 604

void
tp_edge_scroll_handle_state(struct tp_dispatch *tp, uint64_t time);

int
tp_edge_scroll_post_events(struct tp_dispatch *tp, uint64_t time);

void
tp_edge_scroll_stop_events(struct tp_dispatch *tp, uint64_t time);

int
605 606
tp_edge_scroll_touch_active(const struct tp_dispatch *tp,
			    const struct tp_touch *t);
607

608
uint32_t
609
tp_touch_get_edge(const struct tp_dispatch *tp, const struct tp_touch *t);
610

611
void
612 613 614 615 616 617
tp_init_gesture(struct tp_dispatch *tp);

void
tp_remove_gesture(struct tp_dispatch *tp);

void
618 619 620 621
tp_gesture_stop(struct tp_dispatch *tp, uint64_t time);

void
tp_gesture_cancel(struct tp_dispatch *tp, uint64_t time);
622 623 624 625 626 627 628 629 630 631

void
tp_gesture_handle_state(struct tp_dispatch *tp, uint64_t time);

void
tp_gesture_post_events(struct tp_dispatch *tp, uint64_t time);

void
tp_gesture_stop_twofinger_scroll(struct tp_dispatch *tp, uint64_t time);

632
bool
633
tp_palm_tap_is_palm(const struct tp_dispatch *tp, const struct tp_touch *t);
634

635 636 637
void
tp_clickpad_middlebutton_apply_config(struct evdev_device *device);

638
#endif