2 * Copyright © 2012 Jonas Ådahl
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <linux/input.h>
33 #define DEFAULT_CONSTANT_ACCEL_NUMERATOR 50
34 #define DEFAULT_MIN_ACCEL_FACTOR 0.16
35 #define DEFAULT_MAX_ACCEL_FACTOR 1.0
36 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
38 #define DEFAULT_TOUCHPAD_SINGLE_TAP_BUTTON BTN_LEFT
39 #define DEFAULT_TOUCHPAD_SINGLE_TAP_TIMEOUT 100
42 TOUCHPAD_MODEL_UNKNOWN = 0,
43 TOUCHPAD_MODEL_SYNAPTICS,
45 TOUCHPAD_MODEL_APPLETOUCH,
46 TOUCHPAD_MODEL_ELANTECH
50 TOUCHPAD_EVENT_NONE = 0,
51 TOUCHPAD_EVENT_ABSOLUTE_ANY = (1 << 0),
52 TOUCHPAD_EVENT_ABSOLUTE_X = (1 << 1),
53 TOUCHPAD_EVENT_ABSOLUTE_Y = (1 << 2),
54 TOUCHPAD_EVENT_REPORT = (1 << 3)
57 struct touchpad_model_spec {
60 enum touchpad_model model;
63 static struct touchpad_model_spec touchpad_spec_table[] = {
64 {0x0002, 0x0007, TOUCHPAD_MODEL_SYNAPTICS},
65 {0x0002, 0x0008, TOUCHPAD_MODEL_ALPS},
66 {0x05ac, 0x0000, TOUCHPAD_MODEL_APPLETOUCH},
67 {0x0002, 0x000e, TOUCHPAD_MODEL_ELANTECH},
68 {0x0000, 0x0000, TOUCHPAD_MODEL_UNKNOWN}
72 TOUCHPAD_STATE_NONE = 0,
73 TOUCHPAD_STATE_TOUCH = (1 << 0),
74 TOUCHPAD_STATE_MOVE = (1 << 1)
77 #define TOUCHPAD_HISTORY_LENGTH 4
79 struct touchpad_motion {
84 enum touchpad_fingers_state {
85 TOUCHPAD_FINGERS_ONE = (1 << 0),
86 TOUCHPAD_FINGERS_TWO = (1 << 1),
87 TOUCHPAD_FINGERS_THREE = (1 << 2)
105 struct touchpad_dispatch {
106 struct evdev_dispatch base;
107 struct evdev_device *device;
109 enum touchpad_model model;
112 int last_finger_state;
114 double constant_accel_factor;
115 double min_accel_factor;
116 double max_accel_factor;
118 unsigned int event_mask;
119 unsigned int event_mask_filter;
124 struct wl_array events;
125 enum fsm_state state;
126 struct wl_event_source *timer_source;
147 struct touchpad_motion motion_history[TOUCHPAD_HISTORY_LENGTH];
149 unsigned int motion_count;
151 struct weston_motion_filter *filter;
154 static enum touchpad_model
155 get_touchpad_model(struct evdev_device *device)
160 if (ioctl(device->fd, EVIOCGID, &id) < 0)
161 return TOUCHPAD_MODEL_UNKNOWN;
163 for (i = 0; ARRAY_LENGTH(touchpad_spec_table); i++)
164 if (touchpad_spec_table[i].vendor == id.vendor &&
165 (!touchpad_spec_table[i].product ||
166 touchpad_spec_table[i].product == id.product))
167 return touchpad_spec_table[i].model;
169 return TOUCHPAD_MODEL_UNKNOWN;
173 configure_touchpad_pressure(struct touchpad_dispatch *touchpad,
174 int32_t pressure_min, int32_t pressure_max)
176 int32_t range = pressure_max - pressure_min + 1;
178 touchpad->has_pressure = 1;
180 /* Magic numbers from xf86-input-synaptics */
181 switch (touchpad->model) {
182 case TOUCHPAD_MODEL_ELANTECH:
183 touchpad->pressure.touch_low = pressure_min + 1;
184 touchpad->pressure.touch_high = pressure_min + 1;
187 touchpad->pressure.touch_low =
188 pressure_min + range * (25.0/256.0);
189 touchpad->pressure.touch_high =
190 pressure_min + range * (30.0/256.0);
195 touchpad_profile(struct weston_motion_filter *filter,
200 struct touchpad_dispatch *touchpad =
201 (struct touchpad_dispatch *) data;
205 accel_factor = velocity * touchpad->constant_accel_factor;
207 if (accel_factor > touchpad->max_accel_factor)
208 accel_factor = touchpad->max_accel_factor;
209 else if (accel_factor < touchpad->min_accel_factor)
210 accel_factor = touchpad->min_accel_factor;
215 static inline struct touchpad_motion *
216 motion_history_offset(struct touchpad_dispatch *touchpad, int offset)
219 (touchpad->motion_index - offset + TOUCHPAD_HISTORY_LENGTH) %
220 TOUCHPAD_HISTORY_LENGTH;
222 return &touchpad->motion_history[offset_index];
226 estimate_delta(int x0, int x1, int x2, int x3)
228 return (x0 + x1 - x2 - x3) / 4;
232 hysteresis(int in, int center, int margin)
234 int diff = in - center;
235 if (abs(diff) <= margin)
239 return center + diff - margin;
240 else if (diff < -margin)
241 return center + diff + margin;
242 return center + diff;
246 touchpad_get_delta(struct touchpad_dispatch *touchpad, double *dx, double *dy)
248 *dx = estimate_delta(motion_history_offset(touchpad, 0)->x,
249 motion_history_offset(touchpad, 1)->x,
250 motion_history_offset(touchpad, 2)->x,
251 motion_history_offset(touchpad, 3)->x);
252 *dy = estimate_delta(motion_history_offset(touchpad, 0)->y,
253 motion_history_offset(touchpad, 1)->y,
254 motion_history_offset(touchpad, 2)->y,
255 motion_history_offset(touchpad, 3)->y);
259 filter_motion(struct touchpad_dispatch *touchpad,
260 double *dx, double *dy, uint32_t time)
262 struct weston_motion_params motion;
267 weston_filter_dispatch(touchpad->filter, &motion, touchpad, time);
274 notify_button_pressed(struct touchpad_dispatch *touchpad, uint32_t time)
276 notify_button(touchpad->device->seat, time,
277 DEFAULT_TOUCHPAD_SINGLE_TAP_BUTTON,
278 WL_POINTER_BUTTON_STATE_PRESSED);
282 notify_button_released(struct touchpad_dispatch *touchpad, uint32_t time)
284 notify_button(touchpad->device->seat, time,
285 DEFAULT_TOUCHPAD_SINGLE_TAP_BUTTON,
286 WL_POINTER_BUTTON_STATE_RELEASED);
290 notify_tap(struct touchpad_dispatch *touchpad, uint32_t time)
292 notify_button_pressed(touchpad, time);
293 notify_button_released(touchpad, time);
297 process_fsm_events(struct touchpad_dispatch *touchpad, uint32_t time)
299 uint32_t timeout = UINT32_MAX;
300 enum fsm_event *pevent;
301 enum fsm_event event;
303 if (touchpad->fsm.events.size == 0)
306 wl_array_for_each(pevent, &touchpad->fsm.events) {
310 switch (touchpad->fsm.state) {
313 case FSM_EVENT_TOUCH:
314 touchpad->fsm.state = FSM_TOUCH;
322 case FSM_EVENT_RELEASE:
323 timeout = DEFAULT_TOUCHPAD_SINGLE_TAP_TIMEOUT;
324 touchpad->fsm.state = FSM_TAP;
327 touchpad->fsm.state = FSM_IDLE;
333 case FSM_EVENT_TIMEOUT:
334 notify_tap(touchpad, time);
335 touchpad->fsm.state = FSM_IDLE;
337 case FSM_EVENT_TOUCH:
338 notify_button_pressed(touchpad, time);
339 touchpad->fsm.state = FSM_TAP_2;
342 touchpad->fsm.state = FSM_IDLE;
348 case FSM_EVENT_MOTION:
349 touchpad->fsm.state = FSM_DRAG;
351 case FSM_EVENT_RELEASE:
352 notify_button_released(touchpad, time);
353 notify_tap(touchpad, time);
354 touchpad->fsm.state = FSM_IDLE;
357 touchpad->fsm.state = FSM_IDLE;
363 case FSM_EVENT_RELEASE:
364 notify_button_released(touchpad, time);
365 touchpad->fsm.state = FSM_IDLE;
368 touchpad->fsm.state = FSM_IDLE;
373 weston_log("evdev-touchpad: Unknown state %d",
374 touchpad->fsm.state);
375 touchpad->fsm.state = FSM_IDLE;
380 if (timeout != UINT32_MAX)
381 wl_event_source_timer_update(touchpad->fsm.timer_source,
384 wl_array_release(&touchpad->fsm.events);
385 wl_array_init(&touchpad->fsm.events);
389 push_fsm_event(struct touchpad_dispatch *touchpad,
390 enum fsm_event event)
392 enum fsm_event *pevent;
394 pevent = wl_array_add(&touchpad->fsm.events, sizeof event);
398 touchpad->fsm.state = FSM_IDLE;
402 fsm_timout_handler(void *data)
404 struct touchpad_dispatch *touchpad = data;
406 if (touchpad->fsm.events.size == 0) {
407 push_fsm_event(touchpad, FSM_EVENT_TIMEOUT);
408 process_fsm_events(touchpad, weston_compositor_get_time());
415 touchpad_update_state(struct touchpad_dispatch *touchpad, uint32_t time)
418 int center_x, center_y;
419 double dx = 0.0, dy = 0.0;
421 if (touchpad->reset ||
422 touchpad->last_finger_state != touchpad->finger_state) {
424 touchpad->motion_count = 0;
425 touchpad->event_mask = TOUCHPAD_EVENT_NONE;
426 touchpad->event_mask_filter =
427 TOUCHPAD_EVENT_ABSOLUTE_X | TOUCHPAD_EVENT_ABSOLUTE_Y;
429 touchpad->last_finger_state = touchpad->finger_state;
431 process_fsm_events(touchpad, time);
435 touchpad->last_finger_state = touchpad->finger_state;
437 if (!(touchpad->event_mask & TOUCHPAD_EVENT_REPORT))
440 touchpad->event_mask &= ~TOUCHPAD_EVENT_REPORT;
442 if ((touchpad->event_mask & touchpad->event_mask_filter) !=
443 touchpad->event_mask_filter)
446 touchpad->event_mask_filter = TOUCHPAD_EVENT_ABSOLUTE_ANY;
447 touchpad->event_mask = 0;
449 /* Avoid noice by moving center only when delta reaches a threshold
450 * distance from the old center. */
451 if (touchpad->motion_count > 0) {
452 center_x = hysteresis(touchpad->hw_abs.x,
453 touchpad->hysteresis.center_x,
454 touchpad->hysteresis.margin_x);
455 center_y = hysteresis(touchpad->hw_abs.y,
456 touchpad->hysteresis.center_y,
457 touchpad->hysteresis.margin_y);
459 center_x = touchpad->hw_abs.x;
460 center_y = touchpad->hw_abs.y;
462 touchpad->hysteresis.center_x = center_x;
463 touchpad->hysteresis.center_y = center_y;
464 touchpad->hw_abs.x = center_x;
465 touchpad->hw_abs.y = center_y;
467 /* Update motion history tracker */
468 motion_index = (touchpad->motion_index + 1) % TOUCHPAD_HISTORY_LENGTH;
469 touchpad->motion_index = motion_index;
470 touchpad->motion_history[motion_index].x = touchpad->hw_abs.x;
471 touchpad->motion_history[motion_index].y = touchpad->hw_abs.y;
472 if (touchpad->motion_count < 4)
473 touchpad->motion_count++;
475 if (touchpad->motion_count >= 4) {
476 touchpad_get_delta(touchpad, &dx, &dy);
478 filter_motion(touchpad, &dx, &dy, time);
480 if (touchpad->finger_state == TOUCHPAD_FINGERS_ONE) {
481 touchpad->device->rel.dx = wl_fixed_from_double(dx);
482 touchpad->device->rel.dy = wl_fixed_from_double(dy);
483 touchpad->device->pending_events |=
484 EVDEV_RELATIVE_MOTION;
485 } else if (touchpad->finger_state == TOUCHPAD_FINGERS_TWO) {
487 notify_axis(touchpad->device->seat,
489 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
490 wl_fixed_from_double(dx));
492 notify_axis(touchpad->device->seat,
494 WL_POINTER_AXIS_VERTICAL_SCROLL,
495 wl_fixed_from_double(dy));
499 if (!(touchpad->state & TOUCHPAD_STATE_MOVE) &&
500 ((int)dx || (int)dy)) {
501 touchpad->state |= TOUCHPAD_STATE_MOVE;
502 push_fsm_event(touchpad, FSM_EVENT_MOTION);
505 process_fsm_events(touchpad, time);
509 on_touch(struct touchpad_dispatch *touchpad)
511 touchpad->state |= TOUCHPAD_STATE_TOUCH;
513 push_fsm_event(touchpad, FSM_EVENT_TOUCH);
517 on_release(struct touchpad_dispatch *touchpad)
521 touchpad->state &= ~(TOUCHPAD_STATE_MOVE | TOUCHPAD_STATE_TOUCH);
523 push_fsm_event(touchpad, FSM_EVENT_RELEASE);
527 process_absolute(struct touchpad_dispatch *touchpad,
528 struct evdev_device *device,
529 struct input_event *e)
533 if (e->value > touchpad->pressure.touch_high &&
534 !(touchpad->state & TOUCHPAD_STATE_TOUCH))
536 else if (e->value < touchpad->pressure.touch_low &&
537 touchpad->state & TOUCHPAD_STATE_TOUCH)
538 on_release(touchpad);
542 if (touchpad->state & TOUCHPAD_STATE_TOUCH) {
543 touchpad->hw_abs.x = e->value;
544 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
545 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_X;
549 if (touchpad->state & TOUCHPAD_STATE_TOUCH) {
550 touchpad->hw_abs.y = e->value;
551 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
552 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_Y;
559 process_key(struct touchpad_dispatch *touchpad,
560 struct evdev_device *device,
561 struct input_event *e,
566 if (!touchpad->has_pressure) {
567 if (e->value && !(touchpad->state & TOUCHPAD_STATE_TOUCH))
570 on_release(touchpad);
581 notify_button(device->seat,
583 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
584 WL_POINTER_BUTTON_STATE_RELEASED);
587 case BTN_TOOL_RUBBER:
589 case BTN_TOOL_PENCIL:
590 case BTN_TOOL_AIRBRUSH:
595 case BTN_TOOL_FINGER:
597 touchpad->finger_state |= TOUCHPAD_FINGERS_ONE;
599 touchpad->finger_state &= ~TOUCHPAD_FINGERS_ONE;
601 case BTN_TOOL_DOUBLETAP:
603 touchpad->finger_state |= TOUCHPAD_FINGERS_TWO;
605 touchpad->finger_state &= ~TOUCHPAD_FINGERS_TWO;
607 case BTN_TOOL_TRIPLETAP:
609 touchpad->finger_state |= TOUCHPAD_FINGERS_THREE;
611 touchpad->finger_state &= ~TOUCHPAD_FINGERS_THREE;
617 touchpad_process(struct evdev_dispatch *dispatch,
618 struct evdev_device *device,
619 struct input_event *e,
622 struct touchpad_dispatch *touchpad =
623 (struct touchpad_dispatch *) dispatch;
627 if (e->code == SYN_REPORT)
628 touchpad->event_mask |= TOUCHPAD_EVENT_REPORT;
631 process_absolute(touchpad, device, e);
634 process_key(touchpad, device, e, time);
638 touchpad_update_state(touchpad, time);
642 touchpad_destroy(struct evdev_dispatch *dispatch)
644 struct touchpad_dispatch *touchpad =
645 (struct touchpad_dispatch *) dispatch;
647 touchpad->filter->interface->destroy(touchpad->filter);
648 wl_event_source_remove(touchpad->fsm.timer_source);
652 struct evdev_dispatch_interface touchpad_interface = {
658 touchpad_init(struct touchpad_dispatch *touchpad,
659 struct evdev_device *device)
661 struct weston_motion_filter *accel;
662 struct wl_event_loop *loop;
664 struct input_absinfo absinfo;
665 unsigned long abs_bits[NBITS(ABS_MAX)];
671 touchpad->base.interface = &touchpad_interface;
672 touchpad->device = device;
675 touchpad->model = get_touchpad_model(device);
677 /* Configure pressure */
678 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits);
679 if (TEST_BIT(abs_bits, ABS_PRESSURE)) {
680 ioctl(device->fd, EVIOCGABS(ABS_PRESSURE), &absinfo);
681 configure_touchpad_pressure(touchpad,
686 /* Configure acceleration factor */
687 width = abs(device->abs.max_x - device->abs.min_x);
688 height = abs(device->abs.max_y - device->abs.min_y);
689 diagonal = sqrt(width*width + height*height);
691 touchpad->constant_accel_factor =
692 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
694 touchpad->min_accel_factor = DEFAULT_MIN_ACCEL_FACTOR;
695 touchpad->max_accel_factor = DEFAULT_MAX_ACCEL_FACTOR;
697 touchpad->hysteresis.margin_x =
698 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
699 touchpad->hysteresis.margin_y =
700 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
701 touchpad->hysteresis.center_x = 0;
702 touchpad->hysteresis.center_y = 0;
704 /* Configure acceleration profile */
705 accel = create_pointer_accelator_filter(touchpad_profile);
708 touchpad->filter = accel;
710 /* Setup initial state */
713 memset(touchpad->motion_history, 0, sizeof touchpad->motion_history);
714 touchpad->motion_index = 0;
715 touchpad->motion_count = 0;
717 touchpad->state = TOUCHPAD_STATE_NONE;
718 touchpad->last_finger_state = 0;
719 touchpad->finger_state = 0;
721 wl_array_init(&touchpad->fsm.events);
722 touchpad->fsm.state = FSM_IDLE;
724 loop = wl_display_get_event_loop(device->seat->compositor->wl_display);
725 touchpad->fsm.timer_source =
726 wl_event_loop_add_timer(loop, fsm_timout_handler, touchpad);
727 if (touchpad->fsm.timer_source == NULL) {
728 accel->interface->destroy(accel);
735 struct evdev_dispatch *
736 evdev_touchpad_create(struct evdev_device *device)
738 struct touchpad_dispatch *touchpad;
740 touchpad = malloc(sizeof *touchpad);
741 if (touchpad == NULL)
744 if (touchpad_init(touchpad, device) != 0) {
749 return &touchpad->base;