• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014-2015 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include <limits.h>
27 #include <math.h>
28 #include <string.h>
29 #include "linux/input.h"
30 
31 #include "util-input-event.h"
32 #include "evdev-mt-touchpad.h"
33 
34 #define DEFAULT_BUTTON_ENTER_TIMEOUT ms2us(100)
35 #define DEFAULT_BUTTON_LEAVE_TIMEOUT ms2us(300)
36 
37 /*****************************************
38  * BEFORE YOU EDIT THIS FILE, look at the state diagram in
39  * doc/touchpad-softbutton-state-machine.svg (generated with
40  * https://draw.io).
41  * Any changes in this file must be represented in the diagram.
42  *
43  * The state machine only affects the soft button area code.
44  */
45 
46 static inline const char*
button_state_to_str(enum button_state state)47 button_state_to_str(enum button_state state)
48 {
49 	switch(state) {
50 	CASE_RETURN_STRING(BUTTON_STATE_NONE);
51 	CASE_RETURN_STRING(BUTTON_STATE_AREA);
52 	CASE_RETURN_STRING(BUTTON_STATE_BOTTOM);
53 	CASE_RETURN_STRING(BUTTON_STATE_TOP);
54 	CASE_RETURN_STRING(BUTTON_STATE_TOP_NEW);
55 	CASE_RETURN_STRING(BUTTON_STATE_TOP_TO_IGNORE);
56 	CASE_RETURN_STRING(BUTTON_STATE_IGNORE);
57 	}
58 	return NULL;
59 }
60 
61 static inline const char*
button_event_to_str(enum button_event event)62 button_event_to_str(enum button_event event)
63 {
64 	switch(event) {
65 	CASE_RETURN_STRING(BUTTON_EVENT_IN_BOTTOM_R);
66 	CASE_RETURN_STRING(BUTTON_EVENT_IN_BOTTOM_M);
67 	CASE_RETURN_STRING(BUTTON_EVENT_IN_BOTTOM_L);
68 	CASE_RETURN_STRING(BUTTON_EVENT_IN_TOP_R);
69 	CASE_RETURN_STRING(BUTTON_EVENT_IN_TOP_M);
70 	CASE_RETURN_STRING(BUTTON_EVENT_IN_TOP_L);
71 	CASE_RETURN_STRING(BUTTON_EVENT_IN_AREA);
72 	CASE_RETURN_STRING(BUTTON_EVENT_UP);
73 	CASE_RETURN_STRING(BUTTON_EVENT_PRESS);
74 	CASE_RETURN_STRING(BUTTON_EVENT_RELEASE);
75 	CASE_RETURN_STRING(BUTTON_EVENT_TIMEOUT);
76 	}
77 	return NULL;
78 }
79 
80 static inline bool
is_inside_bottom_button_area(const struct tp_dispatch * tp,const struct tp_touch * t)81 is_inside_bottom_button_area(const struct tp_dispatch *tp,
82 			     const struct tp_touch *t)
83 {
84 	return t->point.y >= tp->buttons.bottom_area.top_edge;
85 }
86 
87 static inline bool
is_inside_bottom_right_area(const struct tp_dispatch * tp,const struct tp_touch * t)88 is_inside_bottom_right_area(const struct tp_dispatch *tp,
89 			    const struct tp_touch *t)
90 {
91 	return is_inside_bottom_button_area(tp, t) &&
92 	       t->point.x > tp->buttons.bottom_area.rightbutton_left_edge;
93 }
94 
95 static inline bool
is_inside_bottom_middle_area(const struct tp_dispatch * tp,const struct tp_touch * t)96 is_inside_bottom_middle_area(const struct tp_dispatch *tp,
97 			   const struct tp_touch *t)
98 {
99 	return is_inside_bottom_button_area(tp, t) &&
100 	       !is_inside_bottom_right_area(tp, t) &&
101 	       t->point.x > tp->buttons.bottom_area.middlebutton_left_edge;
102 }
103 
104 static inline bool
is_inside_bottom_left_area(const struct tp_dispatch * tp,const struct tp_touch * t)105 is_inside_bottom_left_area(const struct tp_dispatch *tp,
106 			   const struct tp_touch *t)
107 {
108 	return is_inside_bottom_button_area(tp, t) &&
109 	       !is_inside_bottom_middle_area(tp, t) &&
110 	       !is_inside_bottom_right_area(tp, t);
111 }
112 
113 static inline bool
is_inside_top_button_area(const struct tp_dispatch * tp,const struct tp_touch * t)114 is_inside_top_button_area(const struct tp_dispatch *tp,
115 			  const struct tp_touch *t)
116 {
117 	return t->point.y <= tp->buttons.top_area.bottom_edge;
118 }
119 
120 static inline bool
is_inside_top_right_area(const struct tp_dispatch * tp,const struct tp_touch * t)121 is_inside_top_right_area(const struct tp_dispatch *tp,
122 			 const struct tp_touch *t)
123 {
124 	return is_inside_top_button_area(tp, t) &&
125 	       t->point.x > tp->buttons.top_area.rightbutton_left_edge;
126 }
127 
128 static inline bool
is_inside_top_left_area(const struct tp_dispatch * tp,const struct tp_touch * t)129 is_inside_top_left_area(const struct tp_dispatch *tp,
130 			const struct tp_touch *t)
131 {
132 	return is_inside_top_button_area(tp, t) &&
133 	       t->point.x < tp->buttons.top_area.leftbutton_right_edge;
134 }
135 
136 static inline bool
is_inside_top_middle_area(const struct tp_dispatch * tp,const struct tp_touch * t)137 is_inside_top_middle_area(const struct tp_dispatch *tp,
138 			  const struct tp_touch *t)
139 {
140 	return is_inside_top_button_area(tp, t) &&
141 	       t->point.x >= tp->buttons.top_area.leftbutton_right_edge &&
142 	       t->point.x <= tp->buttons.top_area.rightbutton_left_edge;
143 }
144 
145 static void
tp_button_set_enter_timer(struct tp_dispatch * tp,struct tp_touch * t)146 tp_button_set_enter_timer(struct tp_dispatch *tp, struct tp_touch *t)
147 {
148 	libinput_timer_set(&t->button.timer,
149 			   t->time + DEFAULT_BUTTON_ENTER_TIMEOUT);
150 }
151 
152 static void
tp_button_set_leave_timer(struct tp_dispatch * tp,struct tp_touch * t)153 tp_button_set_leave_timer(struct tp_dispatch *tp, struct tp_touch *t)
154 {
155 	libinput_timer_set(&t->button.timer,
156 			   t->time + DEFAULT_BUTTON_LEAVE_TIMEOUT);
157 }
158 
159 /*
160  * tp_button_set_state, change state and implement on-entry behavior
161  * as described in the state machine diagram.
162  */
163 static void
tp_button_set_state(struct tp_dispatch * tp,struct tp_touch * t,enum button_state new_state,enum button_event event)164 tp_button_set_state(struct tp_dispatch *tp,
165 		    struct tp_touch *t,
166 		    enum button_state new_state,
167 		    enum button_event event)
168 {
169 	libinput_timer_cancel(&t->button.timer);
170 
171 	t->button.state = new_state;
172 
173 	switch (t->button.state) {
174 	case BUTTON_STATE_NONE:
175 		t->button.current = 0;
176 		break;
177 	case BUTTON_STATE_AREA:
178 		t->button.current = BUTTON_EVENT_IN_AREA;
179 		break;
180 	case BUTTON_STATE_BOTTOM:
181 		t->button.current = event;
182 		break;
183 	case BUTTON_STATE_TOP:
184 		break;
185 	case BUTTON_STATE_TOP_NEW:
186 		t->button.current = event;
187 		tp_button_set_enter_timer(tp, t);
188 		break;
189 	case BUTTON_STATE_TOP_TO_IGNORE:
190 		tp_button_set_leave_timer(tp, t);
191 		break;
192 	case BUTTON_STATE_IGNORE:
193 		t->button.current = 0;
194 		break;
195 	}
196 }
197 
198 static void
tp_button_none_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)199 tp_button_none_handle_event(struct tp_dispatch *tp,
200 			    struct tp_touch *t,
201 			    enum button_event event)
202 {
203 	switch (event) {
204 	case BUTTON_EVENT_IN_BOTTOM_R:
205 	case BUTTON_EVENT_IN_BOTTOM_M:
206 	case BUTTON_EVENT_IN_BOTTOM_L:
207 		tp_button_set_state(tp, t, BUTTON_STATE_BOTTOM, event);
208 		break;
209 	case BUTTON_EVENT_IN_TOP_R:
210 	case BUTTON_EVENT_IN_TOP_M:
211 	case BUTTON_EVENT_IN_TOP_L:
212 		tp_button_set_state(tp, t, BUTTON_STATE_TOP_NEW, event);
213 		break;
214 	case BUTTON_EVENT_IN_AREA:
215 		tp_button_set_state(tp, t, BUTTON_STATE_AREA, event);
216 		break;
217 	case BUTTON_EVENT_UP:
218 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
219 		break;
220 	case BUTTON_EVENT_PRESS:
221 	case BUTTON_EVENT_RELEASE:
222 	case BUTTON_EVENT_TIMEOUT:
223 		break;
224 	}
225 }
226 
227 static void
tp_button_area_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)228 tp_button_area_handle_event(struct tp_dispatch *tp,
229 			    struct tp_touch *t,
230 			    enum button_event event)
231 {
232 	switch (event) {
233 	case BUTTON_EVENT_IN_BOTTOM_R:
234 	case BUTTON_EVENT_IN_BOTTOM_M:
235 	case BUTTON_EVENT_IN_BOTTOM_L:
236 	case BUTTON_EVENT_IN_TOP_R:
237 	case BUTTON_EVENT_IN_TOP_M:
238 	case BUTTON_EVENT_IN_TOP_L:
239 	case BUTTON_EVENT_IN_AREA:
240 		break;
241 	case BUTTON_EVENT_UP:
242 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
243 		break;
244 	case BUTTON_EVENT_PRESS:
245 	case BUTTON_EVENT_RELEASE:
246 	case BUTTON_EVENT_TIMEOUT:
247 		break;
248 	}
249 }
250 
251 /**
252  * Release any button in the bottom area, provided it started within a
253  * threshold around start_time (i.e. simultaneously with the other touch
254  * that triggered this call).
255  */
256 static inline void
tp_button_release_other_bottom_touches(struct tp_dispatch * tp,uint64_t other_start_time)257 tp_button_release_other_bottom_touches(struct tp_dispatch *tp,
258 				       uint64_t other_start_time)
259 {
260 	struct tp_touch *t;
261 
262 	tp_for_each_touch(tp, t) {
263 		uint64_t tdelta;
264 
265 		if (t->button.state != BUTTON_STATE_BOTTOM ||
266 		    t->button.has_moved)
267 			continue;
268 
269 		if (other_start_time > t->button.initial_time)
270 			tdelta = other_start_time - t->button.initial_time;
271 		else
272 			tdelta = t->button.initial_time - other_start_time;
273 
274 		if (tdelta > ms2us(80))
275 			continue;
276 
277 		t->button.has_moved = true;
278 	}
279 }
280 
281 static void
tp_button_bottom_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)282 tp_button_bottom_handle_event(struct tp_dispatch *tp,
283 			      struct tp_touch *t,
284 			      enum button_event event)
285 {
286 	switch (event) {
287 	case BUTTON_EVENT_IN_BOTTOM_R:
288 	case BUTTON_EVENT_IN_BOTTOM_M:
289 	case BUTTON_EVENT_IN_BOTTOM_L:
290 		if (event != t->button.current)
291 			tp_button_set_state(tp,
292 					    t,
293 					    BUTTON_STATE_BOTTOM,
294 					    event);
295 		break;
296 	case BUTTON_EVENT_IN_TOP_R:
297 	case BUTTON_EVENT_IN_TOP_M:
298 	case BUTTON_EVENT_IN_TOP_L:
299 	case BUTTON_EVENT_IN_AREA:
300 		tp_button_set_state(tp, t, BUTTON_STATE_AREA, event);
301 
302 		/* We just transitioned one finger from BOTTOM to AREA,
303 		 * if there are other fingers in BOTTOM that started
304 		 * simultaneously with this finger, release those fingers
305 		 * because they're part of a gesture.
306 		 */
307 		tp_button_release_other_bottom_touches(tp,
308 						       t->button.initial_time);
309 		break;
310 	case BUTTON_EVENT_UP:
311 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
312 		break;
313 	case BUTTON_EVENT_PRESS:
314 	case BUTTON_EVENT_RELEASE:
315 	case BUTTON_EVENT_TIMEOUT:
316 		break;
317 	}
318 }
319 
320 static void
tp_button_top_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)321 tp_button_top_handle_event(struct tp_dispatch *tp,
322 			   struct tp_touch *t,
323 			   enum button_event event)
324 {
325 	switch (event) {
326 	case BUTTON_EVENT_IN_BOTTOM_R:
327 	case BUTTON_EVENT_IN_BOTTOM_M:
328 	case BUTTON_EVENT_IN_BOTTOM_L:
329 		tp_button_set_state(tp, t, BUTTON_STATE_TOP_TO_IGNORE, event);
330 		break;
331 	case BUTTON_EVENT_IN_TOP_R:
332 	case BUTTON_EVENT_IN_TOP_M:
333 	case BUTTON_EVENT_IN_TOP_L:
334 		if (event != t->button.current)
335 			tp_button_set_state(tp,
336 					    t,
337 					    BUTTON_STATE_TOP_NEW,
338 					    event);
339 		break;
340 	case BUTTON_EVENT_IN_AREA:
341 		tp_button_set_state(tp, t, BUTTON_STATE_TOP_TO_IGNORE, event);
342 		break;
343 	case BUTTON_EVENT_UP:
344 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
345 		break;
346 	case BUTTON_EVENT_PRESS:
347 	case BUTTON_EVENT_RELEASE:
348 	case BUTTON_EVENT_TIMEOUT:
349 		break;
350 	}
351 }
352 
353 static void
tp_button_top_new_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)354 tp_button_top_new_handle_event(struct tp_dispatch *tp,
355 			       struct tp_touch *t,
356 			       enum button_event event)
357 {
358 	switch(event) {
359 	case BUTTON_EVENT_IN_BOTTOM_R:
360 	case BUTTON_EVENT_IN_BOTTOM_M:
361 	case BUTTON_EVENT_IN_BOTTOM_L:
362 		tp_button_set_state(tp, t, BUTTON_STATE_AREA, event);
363 		break;
364 	case BUTTON_EVENT_IN_TOP_R:
365 	case BUTTON_EVENT_IN_TOP_M:
366 	case BUTTON_EVENT_IN_TOP_L:
367 		if (event != t->button.current)
368 			tp_button_set_state(tp,
369 					    t,
370 					    BUTTON_STATE_TOP_NEW,
371 					    event);
372 		break;
373 	case BUTTON_EVENT_IN_AREA:
374 		tp_button_set_state(tp, t, BUTTON_STATE_AREA, event);
375 		break;
376 	case BUTTON_EVENT_UP:
377 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
378 		break;
379 	case BUTTON_EVENT_PRESS:
380 		tp_button_set_state(tp, t, BUTTON_STATE_TOP, event);
381 		break;
382 	case BUTTON_EVENT_RELEASE:
383 		break;
384 	case BUTTON_EVENT_TIMEOUT:
385 		tp_button_set_state(tp, t, BUTTON_STATE_TOP, event);
386 		break;
387 	}
388 }
389 
390 static void
tp_button_top_to_ignore_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)391 tp_button_top_to_ignore_handle_event(struct tp_dispatch *tp,
392 				     struct tp_touch *t,
393 				     enum button_event event)
394 {
395 	switch(event) {
396 	case BUTTON_EVENT_IN_TOP_R:
397 	case BUTTON_EVENT_IN_TOP_M:
398 	case BUTTON_EVENT_IN_TOP_L:
399 		if (event == t->button.current)
400 			tp_button_set_state(tp,
401 					    t,
402 					    BUTTON_STATE_TOP,
403 					    event);
404 		else
405 			tp_button_set_state(tp,
406 					    t,
407 					    BUTTON_STATE_TOP_NEW,
408 					    event);
409 		break;
410 	case BUTTON_EVENT_IN_BOTTOM_R:
411 	case BUTTON_EVENT_IN_BOTTOM_M:
412 	case BUTTON_EVENT_IN_BOTTOM_L:
413 	case BUTTON_EVENT_IN_AREA:
414 		break;
415 	case BUTTON_EVENT_UP:
416 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
417 		break;
418 	case BUTTON_EVENT_PRESS:
419 	case BUTTON_EVENT_RELEASE:
420 		break;
421 	case BUTTON_EVENT_TIMEOUT:
422 		tp_button_set_state(tp, t, BUTTON_STATE_IGNORE, event);
423 		break;
424 	}
425 }
426 
427 static void
tp_button_ignore_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event)428 tp_button_ignore_handle_event(struct tp_dispatch *tp,
429 			      struct tp_touch *t,
430 			      enum button_event event)
431 {
432 	switch (event) {
433 	case BUTTON_EVENT_IN_BOTTOM_R:
434 	case BUTTON_EVENT_IN_BOTTOM_M:
435 	case BUTTON_EVENT_IN_BOTTOM_L:
436 	case BUTTON_EVENT_IN_TOP_R:
437 	case BUTTON_EVENT_IN_TOP_M:
438 	case BUTTON_EVENT_IN_TOP_L:
439 	case BUTTON_EVENT_IN_AREA:
440 		break;
441 	case BUTTON_EVENT_UP:
442 		tp_button_set_state(tp, t, BUTTON_STATE_NONE, event);
443 		break;
444 	case BUTTON_EVENT_PRESS:
445 		t->button.current = BUTTON_EVENT_IN_AREA;
446 		break;
447 	case BUTTON_EVENT_RELEASE:
448 		break;
449 	case BUTTON_EVENT_TIMEOUT:
450 		break;
451 	}
452 }
453 
454 static void
tp_button_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum button_event event,uint64_t time)455 tp_button_handle_event(struct tp_dispatch *tp,
456 		       struct tp_touch *t,
457 		       enum button_event event,
458 		       uint64_t time)
459 {
460 	enum button_state current = t->button.state;
461 
462 	switch(t->button.state) {
463 	case BUTTON_STATE_NONE:
464 		tp_button_none_handle_event(tp, t, event);
465 		break;
466 	case BUTTON_STATE_AREA:
467 		tp_button_area_handle_event(tp, t, event);
468 		break;
469 	case BUTTON_STATE_BOTTOM:
470 		tp_button_bottom_handle_event(tp, t, event);
471 		break;
472 	case BUTTON_STATE_TOP:
473 		tp_button_top_handle_event(tp, t, event);
474 		break;
475 	case BUTTON_STATE_TOP_NEW:
476 		tp_button_top_new_handle_event(tp, t, event);
477 		break;
478 	case BUTTON_STATE_TOP_TO_IGNORE:
479 		tp_button_top_to_ignore_handle_event(tp, t, event);
480 		break;
481 	case BUTTON_STATE_IGNORE:
482 		tp_button_ignore_handle_event(tp, t, event);
483 		break;
484 	}
485 
486 	if (current != t->button.state)
487 		evdev_log_debug(tp->device,
488 				"button state: touch %d from %-20s event %-24s to %-20s\n",
489 				t->index,
490 				button_state_to_str(current),
491 				button_event_to_str(event),
492 				button_state_to_str(t->button.state));
493 }
494 
495 static inline void
tp_button_check_for_movement(struct tp_dispatch * tp,struct tp_touch * t)496 tp_button_check_for_movement(struct tp_dispatch *tp, struct tp_touch *t)
497 {
498 	struct device_coords delta;
499 	struct phys_coords mm;
500 	double vector_length;
501 
502 	if (t->button.has_moved)
503 		return;
504 
505 	switch (t->button.state) {
506 	case BUTTON_STATE_NONE:
507 	case BUTTON_STATE_AREA:
508 	case BUTTON_STATE_TOP:
509 	case BUTTON_STATE_TOP_NEW:
510 	case BUTTON_STATE_TOP_TO_IGNORE:
511 	case BUTTON_STATE_IGNORE:
512 		/* No point calculating if we're not going to use it */
513 		return;
514 	case BUTTON_STATE_BOTTOM:
515 		break;
516 	}
517 
518 	delta.x = t->point.x - t->button.initial.x;
519 	delta.y = t->point.y - t->button.initial.y;
520 	mm = evdev_device_unit_delta_to_mm(tp->device, &delta);
521 	vector_length = hypot(mm.x, mm.y);
522 
523 	if (vector_length > 5.0 /* mm */) {
524 		t->button.has_moved = true;
525 
526 		tp_button_release_other_bottom_touches(tp,
527 						       t->button.initial_time);
528 	}
529 }
530 
531 void
tp_button_handle_state(struct tp_dispatch * tp,uint64_t time)532 tp_button_handle_state(struct tp_dispatch *tp, uint64_t time)
533 {
534 	struct tp_touch *t;
535 
536 	tp_for_each_touch(tp, t) {
537 		if (t->state == TOUCH_NONE || t->state == TOUCH_HOVERING)
538 			continue;
539 
540 		if (t->state == TOUCH_BEGIN) {
541 			t->button.initial = t->point;
542 			t->button.initial_time = time;
543 			t->button.has_moved = false;
544 		}
545 
546 		if (t->state == TOUCH_END) {
547 			tp_button_handle_event(tp, t, BUTTON_EVENT_UP, time);
548 		} else if (t->dirty) {
549 			enum button_event event;
550 
551 			if (is_inside_bottom_button_area(tp, t)) {
552 				if (is_inside_bottom_right_area(tp, t))
553 					event = BUTTON_EVENT_IN_BOTTOM_R;
554 				else if (is_inside_bottom_middle_area(tp, t))
555 					event = BUTTON_EVENT_IN_BOTTOM_M;
556 				else
557 					event = BUTTON_EVENT_IN_BOTTOM_L;
558 
559 				/* In the bottom area we check for movement
560 				 * within the area. Top area - meh */
561 				tp_button_check_for_movement(tp, t);
562 			} else if (is_inside_top_button_area(tp, t)) {
563 				if (is_inside_top_right_area(tp, t))
564 					event = BUTTON_EVENT_IN_TOP_R;
565 				else if (is_inside_top_middle_area(tp, t))
566 					event = BUTTON_EVENT_IN_TOP_M;
567 				else
568 					event = BUTTON_EVENT_IN_TOP_L;
569 			} else {
570 				event = BUTTON_EVENT_IN_AREA;
571 			}
572 
573 			tp_button_handle_event(tp, t, event, time);
574 		}
575 		if (tp->queued & TOUCHPAD_EVENT_BUTTON_RELEASE)
576 			tp_button_handle_event(tp, t, BUTTON_EVENT_RELEASE, time);
577 		if (tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
578 			tp_button_handle_event(tp, t, BUTTON_EVENT_PRESS, time);
579 	}
580 }
581 
582 static void
tp_button_handle_timeout(uint64_t now,void * data)583 tp_button_handle_timeout(uint64_t now, void *data)
584 {
585 	struct tp_touch *t = data;
586 
587 	tp_button_handle_event(t->tp, t, BUTTON_EVENT_TIMEOUT, now);
588 }
589 
590 void
tp_process_button(struct tp_dispatch * tp,const struct input_event * e,uint64_t time)591 tp_process_button(struct tp_dispatch *tp,
592 		  const struct input_event *e,
593 		  uint64_t time)
594 {
595 	uint32_t mask = 1 << (e->code - BTN_LEFT);
596 
597 	/* Ignore other buttons on clickpads */
598 	if (tp->buttons.is_clickpad && e->code != BTN_LEFT) {
599 		evdev_log_bug_kernel(tp->device,
600 				     "received %s button event on a clickpad\n",
601 				     libevdev_event_code_get_name(EV_KEY, e->code));
602 		return;
603 	}
604 
605 	if (e->value) {
606 		tp->buttons.state |= mask;
607 		tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
608 	} else {
609 		tp->buttons.state &= ~mask;
610 		tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
611 	}
612 }
613 
614 void
tp_release_all_buttons(struct tp_dispatch * tp,uint64_t time)615 tp_release_all_buttons(struct tp_dispatch *tp,
616 		       uint64_t time)
617 {
618 	if (tp->buttons.state) {
619 		tp->buttons.state = 0;
620 		tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
621 	}
622 }
623 
624 static void
tp_init_softbuttons(struct tp_dispatch * tp,struct evdev_device * device)625 tp_init_softbuttons(struct tp_dispatch *tp,
626 		    struct evdev_device *device)
627 {
628 	double width, height;
629 	struct device_coords edges;
630 	int mb_le, mb_re; /* middle button left/right edge */
631 	struct phys_coords mm = { 0.0, 0.0 };
632 
633 	evdev_device_get_size(device, &width, &height);
634 
635 	/* button height: 10mm or 15% or the touchpad height,
636 	   whichever is smaller */
637 	if (height * 0.15 > 10)
638 		mm.y = height - 10;
639 	else
640 		mm.y = height * 0.85;
641 
642 	mm.x = width * 0.5;
643 	edges = evdev_device_mm_to_units(device, &mm);
644 	tp->buttons.bottom_area.top_edge = edges.y;
645 	tp->buttons.bottom_area.rightbutton_left_edge = edges.x;
646 
647 	tp->buttons.bottom_area.middlebutton_left_edge = INT_MAX;
648 
649 	/* if middlebutton emulation is enabled, don't init a software area */
650 	if (device->middlebutton.want_enabled)
651 		return;
652 
653 	/* The middle button is 25% of the touchpad and centered. Many
654 	 * touchpads don't have markings for the middle button at all so we
655 	 * need to make it big enough to reliably hit it but not too big so
656 	 * it takes away all the space.
657 	 *
658 	 * On touchpads with visible markings we reduce the size of the
659 	 * middle button since users have a visual guide.
660 	 */
661 	if (evdev_device_has_model_quirk(device,
662 					 QUIRK_MODEL_TOUCHPAD_VISIBLE_MARKER)) {
663 		mm.x = width/2 - 5; /* 10mm wide */
664 		edges = evdev_device_mm_to_units(device, &mm);
665 		mb_le = edges.x;
666 
667 		mm.x = width/2 + 5; /* 10mm wide */
668 		edges = evdev_device_mm_to_units(device, &mm);
669 		mb_re = edges.x;
670 	} else {
671 		mm.x = width * 0.375;
672 		edges = evdev_device_mm_to_units(device, &mm);
673 		mb_le = edges.x;
674 
675 		mm.x = width * 0.625;
676 		edges = evdev_device_mm_to_units(device, &mm);
677 		mb_re = edges.x;
678 	}
679 
680 	tp->buttons.bottom_area.middlebutton_left_edge = mb_le;
681 	tp->buttons.bottom_area.rightbutton_left_edge = mb_re;
682 }
683 
684 void
tp_init_top_softbuttons(struct tp_dispatch * tp,struct evdev_device * device,double topbutton_size_mult)685 tp_init_top_softbuttons(struct tp_dispatch *tp,
686 			struct evdev_device *device,
687 			double topbutton_size_mult)
688 {
689 	struct device_coords edges;
690 
691 	if (tp->buttons.has_topbuttons) {
692 		/* T440s has the top button line 5mm from the top, event
693 		   analysis has shown events to start down to ~10mm from the
694 		   top - which maps to 15%.  We allow the caller to enlarge the
695 		   area using a multiplier for the touchpad disabled case. */
696 		double topsize_mm = 10 * topbutton_size_mult;
697 		struct phys_coords mm;
698 		double width, height;
699 
700 		evdev_device_get_size(device, &width, &height);
701 
702 		mm.x = width * 0.60;
703 		mm.y = topsize_mm;
704 		edges = evdev_device_mm_to_units(device, &mm);
705 		tp->buttons.top_area.bottom_edge = edges.y;
706 		tp->buttons.top_area.rightbutton_left_edge = edges.x;
707 
708 		mm.x = width * 0.40;
709 		edges = evdev_device_mm_to_units(device, &mm);
710 		tp->buttons.top_area.leftbutton_right_edge = edges.x;
711 	} else {
712 		tp->buttons.top_area.bottom_edge = INT_MIN;
713 	}
714 }
715 
716 static inline uint32_t
tp_button_config_click_get_methods(struct libinput_device * device)717 tp_button_config_click_get_methods(struct libinput_device *device)
718 {
719 	struct evdev_device *evdev = evdev_device(device);
720 	struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
721 	uint32_t methods = LIBINPUT_CONFIG_CLICK_METHOD_NONE;
722 
723 	if (tp->buttons.is_clickpad) {
724 		methods |= LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
725 		if (tp->has_mt)
726 			methods |= LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
727 	}
728 
729 	if (evdev->model_flags & EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON)
730 		methods |= LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
731 
732 	return methods;
733 }
734 
735 static void
tp_switch_click_method(struct tp_dispatch * tp)736 tp_switch_click_method(struct tp_dispatch *tp)
737 {
738 	/*
739 	 * All we need to do when switching click methods is to change the
740 	 * bottom_area.top_edge so that when in clickfinger mode the bottom
741 	 * touchpad area is not dead wrt finger movement starting there.
742 	 *
743 	 * We do not need to take any state into account, fingers which are
744 	 * already down will simply keep the state / area they have assigned
745 	 * until they are released, and the post_button_events path is state
746 	 * agnostic.
747 	 */
748 
749 	switch (tp->buttons.click_method) {
750 	case LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS:
751 		tp_init_softbuttons(tp, tp->device);
752 		break;
753 	case LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER:
754 	case LIBINPUT_CONFIG_CLICK_METHOD_NONE:
755 		tp->buttons.bottom_area.top_edge = INT_MAX;
756 		break;
757 	}
758 }
759 
760 static enum libinput_config_status
tp_button_config_click_set_method(struct libinput_device * device,enum libinput_config_click_method method)761 tp_button_config_click_set_method(struct libinput_device *device,
762 				  enum libinput_config_click_method method)
763 {
764 	struct evdev_device *evdev = evdev_device(device);
765 	struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
766 
767 	tp->buttons.click_method = method;
768 	tp_switch_click_method(tp);
769 
770 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
771 }
772 
773 static enum libinput_config_click_method
tp_button_config_click_get_method(struct libinput_device * device)774 tp_button_config_click_get_method(struct libinput_device *device)
775 {
776 	struct evdev_device *evdev = evdev_device(device);
777 	struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
778 
779 	return tp->buttons.click_method;
780 }
781 
782 static enum libinput_config_click_method
tp_click_get_default_method(struct tp_dispatch * tp)783 tp_click_get_default_method(struct tp_dispatch *tp)
784 {
785 	struct evdev_device *device = tp->device;
786 
787 	if (evdev_device_has_model_quirk(device, QUIRK_MODEL_CHROMEBOOK) ||
788 	    evdev_device_has_model_quirk(device, QUIRK_MODEL_SYSTEM76_BONOBO) ||
789 	    evdev_device_has_model_quirk(device, QUIRK_MODEL_SYSTEM76_GALAGO) ||
790 	    evdev_device_has_model_quirk(device, QUIRK_MODEL_SYSTEM76_KUDU) ||
791 	    evdev_device_has_model_quirk(device, QUIRK_MODEL_CLEVO_W740SU) ||
792 	    evdev_device_has_model_quirk(device, QUIRK_MODEL_APPLE_TOUCHPAD_ONEBUTTON))
793 		return LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
794 
795 	if (!tp->buttons.is_clickpad)
796 		return LIBINPUT_CONFIG_CLICK_METHOD_NONE;
797 	else if (evdev_device_has_model_quirk(device, QUIRK_MODEL_APPLE_TOUCHPAD))
798 		return LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
799 
800 	return LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
801 }
802 
803 static enum libinput_config_click_method
tp_button_config_click_get_default_method(struct libinput_device * device)804 tp_button_config_click_get_default_method(struct libinput_device *device)
805 {
806 	struct evdev_device *evdev = evdev_device(device);
807 	struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
808 
809 	return tp_click_get_default_method(tp);
810 }
811 
812 void
tp_clickpad_middlebutton_apply_config(struct evdev_device * device)813 tp_clickpad_middlebutton_apply_config(struct evdev_device *device)
814 {
815 	struct tp_dispatch *tp = (struct tp_dispatch*)device->dispatch;
816 
817 	if (!tp->buttons.is_clickpad ||
818 	    tp->buttons.state != 0)
819 		return;
820 
821 	if (device->middlebutton.want_enabled ==
822 	    device->middlebutton.enabled)
823 		return;
824 
825 	device->middlebutton.enabled = device->middlebutton.want_enabled;
826 	if (tp->buttons.click_method ==
827 	    LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS)
828 		tp_init_softbuttons(tp, device);
829 }
830 
831 static int
tp_clickpad_middlebutton_is_available(struct libinput_device * device)832 tp_clickpad_middlebutton_is_available(struct libinput_device *device)
833 {
834 	return evdev_middlebutton_is_available(device);
835 }
836 
837 static enum libinput_config_status
tp_clickpad_middlebutton_set(struct libinput_device * device,enum libinput_config_middle_emulation_state enable)838 tp_clickpad_middlebutton_set(struct libinput_device *device,
839 		     enum libinput_config_middle_emulation_state enable)
840 {
841 	struct evdev_device *evdev = evdev_device(device);
842 
843 	switch (enable) {
844 	case LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED:
845 		evdev->middlebutton.want_enabled = true;
846 		break;
847 	case LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED:
848 		evdev->middlebutton.want_enabled = false;
849 		break;
850 	default:
851 		return LIBINPUT_CONFIG_STATUS_INVALID;
852 	}
853 
854 	tp_clickpad_middlebutton_apply_config(evdev);
855 
856 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
857 }
858 
859 static enum libinput_config_middle_emulation_state
tp_clickpad_middlebutton_get(struct libinput_device * device)860 tp_clickpad_middlebutton_get(struct libinput_device *device)
861 {
862 	return evdev_middlebutton_get(device);
863 }
864 
865 static enum libinput_config_middle_emulation_state
tp_clickpad_middlebutton_get_default(struct libinput_device * device)866 tp_clickpad_middlebutton_get_default(struct libinput_device *device)
867 {
868 	return evdev_middlebutton_get_default(device);
869 }
870 
871 static inline void
tp_init_clickpad_middlebutton_emulation(struct tp_dispatch * tp,struct evdev_device * device)872 tp_init_clickpad_middlebutton_emulation(struct tp_dispatch *tp,
873 					struct evdev_device *device)
874 {
875 	device->middlebutton.enabled_default = false;
876 	device->middlebutton.want_enabled = false;
877 	device->middlebutton.enabled = false;
878 
879 	device->middlebutton.config.available = tp_clickpad_middlebutton_is_available;
880 	device->middlebutton.config.set = tp_clickpad_middlebutton_set;
881 	device->middlebutton.config.get = tp_clickpad_middlebutton_get;
882 	device->middlebutton.config.get_default = tp_clickpad_middlebutton_get_default;
883 	device->base.config.middle_emulation = &device->middlebutton.config;
884 }
885 
886 static inline void
tp_init_middlebutton_emulation(struct tp_dispatch * tp,struct evdev_device * device)887 tp_init_middlebutton_emulation(struct tp_dispatch *tp,
888 			       struct evdev_device *device)
889 {
890 	bool enable_by_default,
891 	     want_config_option;
892 
893 	/* On clickpads we provide the config option but disable by default.
894 	   When enabled, the middle software button disappears */
895 	if (tp->buttons.is_clickpad) {
896 		tp_init_clickpad_middlebutton_emulation(tp, device);
897 		return;
898 	}
899 
900 	/* init middle button emulation on non-clickpads, but only if we
901 	 * don't have a middle button. Exception: ALPS touchpads don't know
902 	 * if they have a middle button, so we always want the option there
903 	 * and enabled by default.
904 	 */
905 	if (!libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE)) {
906 		enable_by_default = true;
907 		want_config_option = false;
908 	} else if (evdev_device_has_model_quirk(device,
909 						QUIRK_MODEL_ALPS_SERIAL_TOUCHPAD)) {
910 		enable_by_default = true;
911 		want_config_option = true;
912 	} else
913 		return;
914 
915 	evdev_init_middlebutton(tp->device,
916 				enable_by_default,
917 				want_config_option);
918 }
919 
920 void
tp_init_buttons(struct tp_dispatch * tp,struct evdev_device * device)921 tp_init_buttons(struct tp_dispatch *tp,
922 		struct evdev_device *device)
923 {
924 	struct tp_touch *t;
925 	const struct input_absinfo *absinfo_x, *absinfo_y;
926 	int i;
927 
928 	tp->buttons.is_clickpad = libevdev_has_property(device->evdev,
929 							INPUT_PROP_BUTTONPAD);
930 	tp->buttons.has_topbuttons = libevdev_has_property(device->evdev,
931 						        INPUT_PROP_TOPBUTTONPAD);
932 
933 	if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE) ||
934 	    libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT)) {
935 		if (tp->buttons.is_clickpad)
936 			evdev_log_bug_kernel(device,
937 					     "clickpad advertising right button\n");
938 	} else if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_LEFT) &&
939 		   !tp->buttons.is_clickpad &&
940 		   libevdev_get_id_vendor(device->evdev) != VENDOR_ID_APPLE) {
941 			evdev_log_bug_kernel(device,
942 					     "non clickpad without right button?\n");
943 	}
944 
945 	absinfo_x = device->abs.absinfo_x;
946 	absinfo_y = device->abs.absinfo_y;
947 
948 	/* pinned-finger motion threshold, see tp_unpin_finger. */
949 	tp->buttons.motion_dist.x_scale_coeff = 1.0/absinfo_x->resolution;
950 	tp->buttons.motion_dist.y_scale_coeff = 1.0/absinfo_y->resolution;
951 
952 	tp->buttons.config_method.get_methods = tp_button_config_click_get_methods;
953 	tp->buttons.config_method.set_method = tp_button_config_click_set_method;
954 	tp->buttons.config_method.get_method = tp_button_config_click_get_method;
955 	tp->buttons.config_method.get_default_method = tp_button_config_click_get_default_method;
956 	tp->device->base.config.click_method = &tp->buttons.config_method;
957 
958 	tp->buttons.click_method = tp_click_get_default_method(tp);
959 	tp_switch_click_method(tp);
960 
961 	tp_init_top_softbuttons(tp, device, 1.0);
962 
963 	tp_init_middlebutton_emulation(tp, device);
964 
965 	i = 0;
966 	tp_for_each_touch(tp, t) {
967 		char timer_name[64];
968 		i++;
969 
970 		snprintf(timer_name,
971 			 sizeof(timer_name),
972 			 "%s (%d) button",
973 			 evdev_device_get_sysname(device),
974 			 i);
975 		t->button.state = BUTTON_STATE_NONE;
976 		libinput_timer_init(&t->button.timer,
977 				    tp_libinput_context(tp),
978 				    timer_name,
979 				    tp_button_handle_timeout, t);
980 	}
981 }
982 
983 void
tp_remove_buttons(struct tp_dispatch * tp)984 tp_remove_buttons(struct tp_dispatch *tp)
985 {
986 	struct tp_touch *t;
987 
988 	tp_for_each_touch(tp, t) {
989 		libinput_timer_cancel(&t->button.timer);
990 		libinput_timer_destroy(&t->button.timer);
991 	}
992 }
993 
994 static int
tp_post_physical_buttons(struct tp_dispatch * tp,uint64_t time)995 tp_post_physical_buttons(struct tp_dispatch *tp, uint64_t time)
996 {
997 	uint32_t current, old, button;
998 
999 	current = tp->buttons.state;
1000 	old = tp->buttons.old_state;
1001 	button = BTN_LEFT;
1002 
1003 	while (current || old) {
1004 		enum libinput_button_state state;
1005 
1006 		if ((current & 0x1) ^ (old & 0x1)) {
1007 			uint32_t b;
1008 
1009 			if (!!(current & 0x1))
1010 				state = LIBINPUT_BUTTON_STATE_PRESSED;
1011 			else
1012 				state = LIBINPUT_BUTTON_STATE_RELEASED;
1013 
1014 			b = evdev_to_left_handed(tp->device, button);
1015 			evdev_pointer_notify_physical_button(tp->device,
1016 							     time,
1017 							     b,
1018 							     state);
1019 		}
1020 
1021 		button++;
1022 		current >>= 1;
1023 		old >>= 1;
1024 	}
1025 
1026 	return 0;
1027 }
1028 
1029 static inline bool
tp_clickfinger_within_distance(struct tp_dispatch * tp,struct tp_touch * t1,struct tp_touch * t2)1030 tp_clickfinger_within_distance(struct tp_dispatch *tp,
1031 			       struct tp_touch *t1,
1032 			       struct tp_touch *t2)
1033 {
1034 	double x, y;
1035 	bool within_distance = false;
1036 	int xres, yres;
1037 	int bottom_threshold;
1038 
1039 	if (!t1 || !t2)
1040 		return 0;
1041 
1042 	if (tp_thumb_ignored(tp, t1) || tp_thumb_ignored(tp, t2))
1043 		return 0;
1044 
1045 	x = abs(t1->point.x - t2->point.x);
1046 	y = abs(t1->point.y - t2->point.y);
1047 
1048 	xres = tp->device->abs.absinfo_x->resolution;
1049 	yres = tp->device->abs.absinfo_y->resolution;
1050 	x /= xres;
1051 	y /= yres;
1052 
1053 	/* maximum horiz spread is 40mm horiz, 30mm vert, anything wider
1054 	 * than that is probably a gesture. */
1055 	if (x > 40 || y > 30)
1056 		goto out;
1057 
1058 	within_distance = true;
1059 
1060 	/* if y spread is <= 20mm, they're definitely together. */
1061 	if (y <= 20)
1062 		goto out;
1063 
1064 	/* if they're vertically spread between 20-40mm, they're not
1065 	 * together if:
1066 	 * - the touchpad's vertical size is >50mm, anything smaller is
1067 	 *   unlikely to have a thumb resting on it
1068 	 * - and one of the touches is in the bottom 20mm of the touchpad
1069 	 *   and the other one isn't
1070 	 */
1071 
1072 	if (tp->device->abs.dimensions.y/yres < 50)
1073 		goto out;
1074 
1075 	bottom_threshold = tp->device->abs.absinfo_y->maximum - 20 * yres;
1076 	if ((t1->point.y > bottom_threshold) !=
1077 		    (t2->point.y > bottom_threshold))
1078 		within_distance = 0;
1079 
1080 out:
1081 	return within_distance;
1082 }
1083 
1084 static uint32_t
tp_clickfinger_set_button(struct tp_dispatch * tp)1085 tp_clickfinger_set_button(struct tp_dispatch *tp)
1086 {
1087 	uint32_t button;
1088 	unsigned int nfingers = 0;
1089 	struct tp_touch *t;
1090 	struct tp_touch *first = NULL,
1091 			*second = NULL;
1092 
1093 	tp_for_each_touch(tp, t) {
1094 		if (t->state != TOUCH_BEGIN && t->state != TOUCH_UPDATE)
1095 			continue;
1096 
1097 		if (tp_thumb_ignored(tp, t))
1098 			continue;
1099 
1100 		if (t->palm.state != PALM_NONE)
1101 			continue;
1102 
1103 		nfingers++;
1104 
1105 		if (!first)
1106 			first = t;
1107 		else if (!second)
1108 			second = t;
1109 	}
1110 
1111 	/* Only check for finger distance when there are 2 fingers on the
1112 	 * touchpad */
1113 	if (nfingers != 2)
1114 		goto out;
1115 
1116 	if (tp_clickfinger_within_distance(tp, first, second))
1117 		nfingers = 2;
1118 	else
1119 		nfingers = 1;
1120 
1121 out:
1122 	switch (nfingers) {
1123 	case 0:
1124 	case 1: button = BTN_LEFT; break;
1125 	case 2: button = BTN_RIGHT; break;
1126 	default:
1127 		button = BTN_MIDDLE; break;
1128 		break;
1129 	}
1130 
1131 	return button;
1132 }
1133 
1134 static int
tp_notify_clickpadbutton(struct tp_dispatch * tp,uint64_t time,uint32_t button,uint32_t is_topbutton,enum libinput_button_state state)1135 tp_notify_clickpadbutton(struct tp_dispatch *tp,
1136 			 uint64_t time,
1137 			 uint32_t button,
1138 			 uint32_t is_topbutton,
1139 			 enum libinput_button_state state)
1140 {
1141 	/* If we've a trackpoint, send top buttons through the trackpoint */
1142 	if (tp->buttons.trackpoint) {
1143 		if (is_topbutton) {
1144 			struct evdev_dispatch *dispatch = tp->buttons.trackpoint->dispatch;
1145 			struct input_event event, syn_report;
1146 			int value;
1147 
1148 			value = (state == LIBINPUT_BUTTON_STATE_PRESSED) ? 1 : 0;
1149 			event = input_event_init(time, EV_KEY, button, value);
1150 			syn_report = input_event_init(time, EV_SYN, SYN_REPORT, 0);
1151 			dispatch->interface->process(dispatch,
1152 						     tp->buttons.trackpoint,
1153 						     &event,
1154 						     time);
1155 			dispatch->interface->process(dispatch,
1156 						     tp->buttons.trackpoint,
1157 						     &syn_report,
1158 						     time);
1159 			return 1;
1160 		}
1161 		/* Ignore button events not for the trackpoint while suspended */
1162 		if (tp->device->is_suspended)
1163 			return 0;
1164 	}
1165 
1166 	/* A button click always terminates edge scrolling, even if we
1167 	 * don't end up sending a button event. */
1168 	tp_edge_scroll_stop_events(tp, time);
1169 
1170 	/*
1171 	 * If the user has requested clickfinger replace the button chosen
1172 	 * by the softbutton code with one based on the number of fingers.
1173 	 */
1174 	if (tp->buttons.click_method == LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER &&
1175 	    state == LIBINPUT_BUTTON_STATE_PRESSED) {
1176 		button = tp_clickfinger_set_button(tp);
1177 		tp->buttons.active = button;
1178 
1179 		if (!button)
1180 			return 0;
1181 	}
1182 
1183 	evdev_pointer_notify_button(tp->device, time, button, state);
1184 	return 1;
1185 }
1186 
1187 static int
tp_post_clickpadbutton_buttons(struct tp_dispatch * tp,uint64_t time)1188 tp_post_clickpadbutton_buttons(struct tp_dispatch *tp, uint64_t time)
1189 {
1190 	uint32_t current, old, button, is_top;
1191 	enum libinput_button_state state;
1192 	enum { AREA = 0x01, LEFT = 0x02, MIDDLE = 0x04, RIGHT = 0x08 };
1193 	bool want_left_handed = true;
1194 
1195 	current = tp->buttons.state;
1196 	old = tp->buttons.old_state;
1197 	is_top = 0;
1198 
1199 	if (!tp->buttons.click_pending && current == old)
1200 		return 0;
1201 
1202 	if (current) {
1203 		struct tp_touch *t;
1204 		uint32_t area = 0;
1205 
1206 		tp_for_each_touch(tp, t) {
1207 			switch (t->button.current) {
1208 			case BUTTON_EVENT_IN_AREA:
1209 				area |= AREA;
1210 				break;
1211 			case BUTTON_EVENT_IN_TOP_L:
1212 				is_top = 1;
1213 				/* fallthrough */
1214 			case BUTTON_EVENT_IN_BOTTOM_L:
1215 				area |= LEFT;
1216 				break;
1217 			case BUTTON_EVENT_IN_TOP_M:
1218 				is_top = 1;
1219 				/* fallthrough */
1220 			case BUTTON_EVENT_IN_BOTTOM_M:
1221 				area |= MIDDLE;
1222 				break;
1223 			case BUTTON_EVENT_IN_TOP_R:
1224 				is_top = 1;
1225 				/* fallthrough */
1226 			case BUTTON_EVENT_IN_BOTTOM_R:
1227 				area |= RIGHT;
1228 				break;
1229 			default:
1230 				break;
1231 			}
1232 		}
1233 
1234 		if (area == 0 &&
1235 		    tp->buttons.click_method != LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER) {
1236 			/* No touches, wait for a touch before processing */
1237 			tp->buttons.click_pending = true;
1238 			return 0;
1239 		}
1240 
1241 		if ((tp->device->middlebutton.enabled || is_top) &&
1242 		    (area & LEFT) && (area & RIGHT)) {
1243 			button = BTN_MIDDLE;
1244 		} else if (area & MIDDLE) {
1245 			button = BTN_MIDDLE;
1246 		} else if (area & RIGHT) {
1247 			button = BTN_RIGHT;
1248 		} else if (area & LEFT) {
1249 			button = BTN_LEFT;
1250 		} else { /* main or no area (for clickfinger) is always BTN_LEFT */
1251 			button = BTN_LEFT;
1252 			want_left_handed = false;
1253 		}
1254 
1255 		if (is_top)
1256 			want_left_handed = false;
1257 
1258 		if (want_left_handed)
1259 			button = evdev_to_left_handed(tp->device, button);
1260 
1261 		tp->buttons.active = button;
1262 		tp->buttons.active_is_topbutton = is_top;
1263 		state = LIBINPUT_BUTTON_STATE_PRESSED;
1264 	} else {
1265 		button = tp->buttons.active;
1266 		is_top = tp->buttons.active_is_topbutton;
1267 		tp->buttons.active = 0;
1268 		tp->buttons.active_is_topbutton = 0;
1269 		state = LIBINPUT_BUTTON_STATE_RELEASED;
1270 	}
1271 
1272 	tp->buttons.click_pending = false;
1273 
1274 	if (button)
1275 		return tp_notify_clickpadbutton(tp,
1276 						time,
1277 						button,
1278 						is_top,
1279 						state);
1280 	return 0;
1281 }
1282 
1283 int
tp_post_button_events(struct tp_dispatch * tp,uint64_t time)1284 tp_post_button_events(struct tp_dispatch *tp, uint64_t time)
1285 {
1286 	if (tp->buttons.is_clickpad ||
1287 	    tp->device->model_flags & EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON)
1288 		return tp_post_clickpadbutton_buttons(tp, time);
1289 	else
1290 		return tp_post_physical_buttons(tp, time);
1291 }
1292 
1293 bool
tp_button_touch_active(const struct tp_dispatch * tp,const struct tp_touch * t)1294 tp_button_touch_active(const struct tp_dispatch *tp,
1295 		       const struct tp_touch *t)
1296 {
1297 	return t->button.state == BUTTON_STATE_AREA || t->button.has_moved;
1298 }
1299 
1300 bool
tp_button_is_inside_softbutton_area(const struct tp_dispatch * tp,const struct tp_touch * t)1301 tp_button_is_inside_softbutton_area(const struct tp_dispatch *tp,
1302 				    const struct tp_touch *t)
1303 {
1304 	return is_inside_top_button_area(tp, t) ||
1305 	       is_inside_bottom_button_area(tp, t);
1306 }
1307