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>
30 #include "evdev-private.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
39 TOUCHPAD_MODEL_UNKNOWN = 0,
40 TOUCHPAD_MODEL_SYNAPTICS,
42 TOUCHPAD_MODEL_APPLETOUCH,
43 TOUCHPAD_MODEL_ELANTECH
46 #define TOUCHPAD_EVENT_NONE 0
47 #define TOUCHPAD_EVENT_ABSOLUTE_ANY (1 << 0)
48 #define TOUCHPAD_EVENT_ABSOLUTE_X (1 << 1)
49 #define TOUCHPAD_EVENT_ABSOLUTE_Y (1 << 2)
50 #define TOUCHPAD_EVENT_REPORT (1 << 3)
52 struct touchpad_model_spec {
55 enum touchpad_model model;
58 static struct touchpad_model_spec touchpad_spec_table[] = {
59 {0x0002, 0x0007, TOUCHPAD_MODEL_SYNAPTICS},
60 {0x0002, 0x0008, TOUCHPAD_MODEL_ALPS},
61 {0x05ac, 0x0000, TOUCHPAD_MODEL_APPLETOUCH},
62 {0x0002, 0x000e, TOUCHPAD_MODEL_ELANTECH},
63 {0x0000, 0x0000, TOUCHPAD_MODEL_UNKNOWN}
67 TOUCHPAD_STATE_NONE = 0,
72 #define TOUCHPAD_HISTORY_LENGTH 4
74 struct touchpad_motion {
79 enum touchpad_fingers_state {
80 TOUCHPAD_FINGERS_ONE = (1 << 0),
81 TOUCHPAD_FINGERS_TWO = (1 << 1),
82 TOUCHPAD_FINGERS_THREE = (1 << 2)
85 struct touchpad_dispatch {
86 struct evdev_dispatch base;
87 struct evdev_input_device *device;
89 enum touchpad_model model;
90 enum touchpad_state state;
92 int last_finger_state;
94 double constant_accel_factor;
95 double min_accel_factor;
96 double max_accel_factor;
98 unsigned int event_mask;
99 unsigned int event_mask_filter;
122 struct touchpad_motion motion_history[TOUCHPAD_HISTORY_LENGTH];
124 unsigned int motion_count;
126 struct wl_list motion_filters;
129 static enum touchpad_model
130 get_touchpad_model(struct evdev_input_device *device)
135 if (ioctl(device->fd, EVIOCGID, &id) < 0)
136 return TOUCHPAD_MODEL_UNKNOWN;
138 for (i = 0; i < sizeof touchpad_spec_table; i++)
139 if (touchpad_spec_table[i].vendor == id.vendor &&
140 (!touchpad_spec_table[i].product ||
141 touchpad_spec_table[i].product == id.product))
142 return touchpad_spec_table[i].model;
144 return TOUCHPAD_MODEL_UNKNOWN;
148 configure_touchpad_pressure(struct touchpad_dispatch *touchpad,
149 int32_t pressure_min, int32_t pressure_max)
151 int32_t range = pressure_max - pressure_min + 1;
153 touchpad->has_pressure = 1;
155 /* Magic numbers from xf86-input-synaptics */
156 switch (touchpad->model) {
157 case TOUCHPAD_MODEL_ELANTECH:
158 touchpad->pressure.touch_low = pressure_min + 1;
159 touchpad->pressure.touch_high = pressure_min + 1;
162 touchpad->pressure.touch_low =
163 pressure_min + range * (25.0/256.0);
164 touchpad->pressure.touch_high =
165 pressure_min + range * (30.0/256.0);
168 touchpad->pressure.press = pressure_min + range;
172 touchpad_profile(struct weston_motion_filter *filter,
177 struct touchpad_dispatch *touchpad =
178 (struct touchpad_dispatch *) data;
182 accel_factor = velocity * touchpad->constant_accel_factor;
184 if (accel_factor > touchpad->max_accel_factor)
185 accel_factor = touchpad->max_accel_factor;
186 else if (accel_factor < touchpad->min_accel_factor)
187 accel_factor = touchpad->min_accel_factor;
193 configure_touchpad(struct touchpad_dispatch *touchpad,
194 struct evdev_input_device *device)
196 struct weston_motion_filter *accel;
198 struct input_absinfo absinfo;
199 unsigned long abs_bits[NBITS(ABS_MAX)];
206 touchpad->model = get_touchpad_model(device);
208 /* Configure pressure */
209 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits);
210 if (TEST_BIT(abs_bits, ABS_PRESSURE)) {
211 ioctl(device->fd, EVIOCGABS(ABS_PRESSURE), &absinfo);
212 configure_touchpad_pressure(touchpad,
217 /* Configure acceleration factor */
218 width = abs(device->abs.max_x - device->abs.min_x);
219 height = abs(device->abs.max_y - device->abs.min_y);
220 diagonal = sqrt(width*width + height*height);
222 touchpad->constant_accel_factor =
223 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
225 touchpad->min_accel_factor = DEFAULT_MIN_ACCEL_FACTOR;
226 touchpad->max_accel_factor = DEFAULT_MAX_ACCEL_FACTOR;
228 touchpad->hysteresis.margin_x =
229 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
230 touchpad->hysteresis.margin_y =
231 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
232 touchpad->hysteresis.center_x = 0;
233 touchpad->hysteresis.center_y = 0;
235 /* Configure acceleration profile */
236 accel = create_pointer_accelator_filter(touchpad_profile);
237 wl_list_insert(&touchpad->motion_filters, &accel->link);
239 /* Setup initial state */
242 memset(touchpad->motion_history, 0, sizeof touchpad->motion_history);
243 touchpad->motion_index = 0;
244 touchpad->motion_count = 0;
246 touchpad->state = TOUCHPAD_STATE_NONE;
247 touchpad->last_finger_state = 0;
248 touchpad->finger_state = 0;
251 static inline struct touchpad_motion *
252 motion_history_offset(struct touchpad_dispatch *touchpad, int offset)
255 (touchpad->motion_index - offset + TOUCHPAD_HISTORY_LENGTH) %
256 TOUCHPAD_HISTORY_LENGTH;
258 return &touchpad->motion_history[offset_index];
262 estimate_delta(int x0, int x1, int x2, int x3)
264 return (x0 + x1 - x2 - x3) / 4;
268 hysteresis(int in, int center, int margin)
270 int diff = in - center;
271 if (abs(diff) <= margin)
275 return center + diff - margin;
276 else if (diff < -margin)
277 return center + diff + margin;
278 return center + diff;
282 touchpad_get_delta(struct touchpad_dispatch *touchpad, double *dx, double *dy)
284 *dx = estimate_delta(motion_history_offset(touchpad, 0)->x,
285 motion_history_offset(touchpad, 1)->x,
286 motion_history_offset(touchpad, 2)->x,
287 motion_history_offset(touchpad, 3)->x);
288 *dy = estimate_delta(motion_history_offset(touchpad, 0)->y,
289 motion_history_offset(touchpad, 1)->y,
290 motion_history_offset(touchpad, 2)->y,
291 motion_history_offset(touchpad, 3)->y);
295 filter_motion(struct touchpad_dispatch *touchpad,
296 double *dx, double *dy, uint32_t time)
298 struct weston_motion_filter *filter;
299 struct weston_motion_params motion;
304 wl_list_for_each(filter, &touchpad->motion_filters, link)
305 weston_filter_dispatch(filter, &motion, touchpad, time);
312 touchpad_update_state(struct touchpad_dispatch *touchpad, uint32_t time)
315 int center_x, center_y;
318 if (touchpad->reset ||
319 touchpad->last_finger_state != touchpad->finger_state) {
321 touchpad->motion_count = 0;
322 touchpad->event_mask = TOUCHPAD_EVENT_NONE;
323 touchpad->event_mask_filter =
324 TOUCHPAD_EVENT_ABSOLUTE_X | TOUCHPAD_EVENT_ABSOLUTE_Y;
326 touchpad->last_finger_state = touchpad->finger_state;
330 touchpad->last_finger_state = touchpad->finger_state;
332 if (!(touchpad->event_mask & TOUCHPAD_EVENT_REPORT))
335 touchpad->event_mask &= ~TOUCHPAD_EVENT_REPORT;
337 if ((touchpad->event_mask & touchpad->event_mask_filter) !=
338 touchpad->event_mask_filter)
341 touchpad->event_mask_filter = TOUCHPAD_EVENT_ABSOLUTE_ANY;
342 touchpad->event_mask = 0;
344 /* Avoid noice by moving center only when delta reaches a threshold
345 * distance from the old center. */
346 if (touchpad->motion_count > 0) {
347 center_x = hysteresis(touchpad->hw_abs.x,
348 touchpad->hysteresis.center_x,
349 touchpad->hysteresis.margin_x);
350 center_y = hysteresis(touchpad->hw_abs.y,
351 touchpad->hysteresis.center_y,
352 touchpad->hysteresis.margin_y);
355 center_x = touchpad->hw_abs.x;
356 center_y = touchpad->hw_abs.y;
358 touchpad->hysteresis.center_x = center_x;
359 touchpad->hysteresis.center_y = center_y;
360 touchpad->hw_abs.x = center_x;
361 touchpad->hw_abs.y = center_y;
363 /* Update motion history tracker */
364 motion_index = (touchpad->motion_index + 1) % TOUCHPAD_HISTORY_LENGTH;
365 touchpad->motion_index = motion_index;
366 touchpad->motion_history[motion_index].x = touchpad->hw_abs.x;
367 touchpad->motion_history[motion_index].y = touchpad->hw_abs.y;
368 if (touchpad->motion_count < 4)
369 touchpad->motion_count++;
371 if (touchpad->motion_count >= 4) {
372 touchpad_get_delta(touchpad, &dx, &dy);
374 filter_motion(touchpad, &dx, &dy, time);
376 touchpad->device->rel.dx = wl_fixed_from_double(dx);
377 touchpad->device->rel.dy = wl_fixed_from_double(dy);
378 touchpad->device->pending_events |= EVDEV_RELATIVE_MOTION;
383 process_absolute(struct touchpad_dispatch *touchpad,
384 struct evdev_input_device *device,
385 struct input_event *e)
389 if (e->value > touchpad->pressure.press)
390 touchpad->state = TOUCHPAD_STATE_PRESS;
391 else if (e->value > touchpad->pressure.touch_high)
392 touchpad->state = TOUCHPAD_STATE_TOUCH;
393 else if (e->value < touchpad->pressure.touch_low) {
394 if (touchpad->state > TOUCHPAD_STATE_NONE)
397 touchpad->state = TOUCHPAD_STATE_NONE;
402 if (touchpad->state >= TOUCHPAD_STATE_TOUCH) {
403 touchpad->hw_abs.x = e->value;
404 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
405 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_X;
409 if (touchpad->state >= TOUCHPAD_STATE_TOUCH) {
410 touchpad->hw_abs.y = e->value;
411 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
412 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_Y;
419 process_key(struct touchpad_dispatch *touchpad,
420 struct evdev_input_device *device,
421 struct input_event *e,
426 if (!touchpad->has_pressure) {
428 touchpad->state = TOUCHPAD_STATE_NONE;
433 TOUCHPAD_STATE_TOUCH :
446 notify_button(&device->master->base.seat,
447 time, e->code, e->value);
450 case BTN_TOOL_RUBBER:
452 case BTN_TOOL_PENCIL:
453 case BTN_TOOL_AIRBRUSH:
458 case BTN_TOOL_FINGER:
459 touchpad->finger_state =
460 ~TOUCHPAD_FINGERS_ONE | e->value ?
461 TOUCHPAD_FINGERS_ONE : 0;
463 case BTN_TOOL_DOUBLETAP:
464 touchpad->finger_state =
465 ~TOUCHPAD_FINGERS_TWO | e->value ?
466 TOUCHPAD_FINGERS_TWO : 0;
468 case BTN_TOOL_TRIPLETAP:
469 touchpad->finger_state =
470 ~TOUCHPAD_FINGERS_THREE | e->value ?
471 TOUCHPAD_FINGERS_THREE : 0;
477 touchpad_process(struct evdev_dispatch *dispatch,
478 struct evdev_input_device *device,
479 struct input_event *e,
482 struct touchpad_dispatch *touchpad =
483 (struct touchpad_dispatch *) dispatch;
487 if (e->code == SYN_REPORT)
488 touchpad->event_mask |= TOUCHPAD_EVENT_REPORT;
491 process_absolute(touchpad, device, e);
494 process_key(touchpad, device, e, time);
498 touchpad_update_state(touchpad, time);
502 touchpad_destroy(struct evdev_dispatch *dispatch)
504 struct touchpad_dispatch *touchpad =
505 (struct touchpad_dispatch *) dispatch;
506 struct weston_motion_filter *filter;
507 struct weston_motion_filter *next;
509 wl_list_for_each_safe(filter, next, &touchpad->motion_filters, link)
510 filter->interface->destroy(filter);
515 struct evdev_dispatch_interface touchpad_interface = {
520 struct evdev_dispatch *
521 evdev_touchpad_create(struct evdev_input_device *device)
523 struct touchpad_dispatch *touchpad;
525 touchpad = malloc(sizeof *touchpad);
526 if (touchpad == NULL)
529 touchpad->base.interface = &touchpad_interface;
531 touchpad->device = device;
532 wl_list_init(&touchpad->motion_filters);
534 configure_touchpad(touchpad, device);
536 return &touchpad->base;