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.
26 #include <linux/input.h>
32 #define DEFAULT_CONSTANT_ACCEL_NUMERATOR 50
33 #define DEFAULT_MIN_ACCEL_FACTOR 0.16
34 #define DEFAULT_MAX_ACCEL_FACTOR 1.0
35 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
38 TOUCHPAD_MODEL_UNKNOWN = 0,
39 TOUCHPAD_MODEL_SYNAPTICS,
41 TOUCHPAD_MODEL_APPLETOUCH,
42 TOUCHPAD_MODEL_ELANTECH
45 #define TOUCHPAD_EVENT_NONE 0
46 #define TOUCHPAD_EVENT_ABSOLUTE_ANY (1 << 0)
47 #define TOUCHPAD_EVENT_ABSOLUTE_X (1 << 1)
48 #define TOUCHPAD_EVENT_ABSOLUTE_Y (1 << 2)
49 #define TOUCHPAD_EVENT_REPORT (1 << 3)
51 struct touchpad_model_spec {
54 enum touchpad_model model;
57 static struct touchpad_model_spec touchpad_spec_table[] = {
58 {0x0002, 0x0007, TOUCHPAD_MODEL_SYNAPTICS},
59 {0x0002, 0x0008, TOUCHPAD_MODEL_ALPS},
60 {0x05ac, 0x0000, TOUCHPAD_MODEL_APPLETOUCH},
61 {0x0002, 0x000e, TOUCHPAD_MODEL_ELANTECH},
62 {0x0000, 0x0000, TOUCHPAD_MODEL_UNKNOWN}
66 TOUCHPAD_STATE_NONE = 0,
71 #define TOUCHPAD_HISTORY_LENGTH 4
73 struct touchpad_motion {
78 enum touchpad_fingers_state {
79 TOUCHPAD_FINGERS_ONE = (1 << 0),
80 TOUCHPAD_FINGERS_TWO = (1 << 1),
81 TOUCHPAD_FINGERS_THREE = (1 << 2)
84 struct touchpad_dispatch {
85 struct evdev_dispatch base;
86 struct evdev_device *device;
88 enum touchpad_model model;
89 enum touchpad_state state;
91 int last_finger_state;
93 double constant_accel_factor;
94 double min_accel_factor;
95 double max_accel_factor;
97 unsigned int event_mask;
98 unsigned int event_mask_filter;
121 struct touchpad_motion motion_history[TOUCHPAD_HISTORY_LENGTH];
123 unsigned int motion_count;
125 struct wl_list motion_filters;
128 static enum touchpad_model
129 get_touchpad_model(struct evdev_device *device)
134 if (ioctl(device->fd, EVIOCGID, &id) < 0)
135 return TOUCHPAD_MODEL_UNKNOWN;
137 for (i = 0; ARRAY_LENGTH(touchpad_spec_table); i++)
138 if (touchpad_spec_table[i].vendor == id.vendor &&
139 (!touchpad_spec_table[i].product ||
140 touchpad_spec_table[i].product == id.product))
141 return touchpad_spec_table[i].model;
143 return TOUCHPAD_MODEL_UNKNOWN;
147 configure_touchpad_pressure(struct touchpad_dispatch *touchpad,
148 int32_t pressure_min, int32_t pressure_max)
150 int32_t range = pressure_max - pressure_min + 1;
152 touchpad->has_pressure = 1;
154 /* Magic numbers from xf86-input-synaptics */
155 switch (touchpad->model) {
156 case TOUCHPAD_MODEL_ELANTECH:
157 touchpad->pressure.touch_low = pressure_min + 1;
158 touchpad->pressure.touch_high = pressure_min + 1;
161 touchpad->pressure.touch_low =
162 pressure_min + range * (25.0/256.0);
163 touchpad->pressure.touch_high =
164 pressure_min + range * (30.0/256.0);
167 touchpad->pressure.press = pressure_min + range;
171 touchpad_profile(struct weston_motion_filter *filter,
176 struct touchpad_dispatch *touchpad =
177 (struct touchpad_dispatch *) data;
181 accel_factor = velocity * touchpad->constant_accel_factor;
183 if (accel_factor > touchpad->max_accel_factor)
184 accel_factor = touchpad->max_accel_factor;
185 else if (accel_factor < touchpad->min_accel_factor)
186 accel_factor = touchpad->min_accel_factor;
192 configure_touchpad(struct touchpad_dispatch *touchpad,
193 struct evdev_device *device)
195 struct weston_motion_filter *accel;
197 struct input_absinfo absinfo;
198 unsigned long abs_bits[NBITS(ABS_MAX)];
205 touchpad->model = get_touchpad_model(device);
207 /* Configure pressure */
208 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits);
209 if (TEST_BIT(abs_bits, ABS_PRESSURE)) {
210 ioctl(device->fd, EVIOCGABS(ABS_PRESSURE), &absinfo);
211 configure_touchpad_pressure(touchpad,
216 /* Configure acceleration factor */
217 width = abs(device->abs.max_x - device->abs.min_x);
218 height = abs(device->abs.max_y - device->abs.min_y);
219 diagonal = sqrt(width*width + height*height);
221 touchpad->constant_accel_factor =
222 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
224 touchpad->min_accel_factor = DEFAULT_MIN_ACCEL_FACTOR;
225 touchpad->max_accel_factor = DEFAULT_MAX_ACCEL_FACTOR;
227 touchpad->hysteresis.margin_x =
228 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
229 touchpad->hysteresis.margin_y =
230 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
231 touchpad->hysteresis.center_x = 0;
232 touchpad->hysteresis.center_y = 0;
234 /* Configure acceleration profile */
235 accel = create_pointer_accelator_filter(touchpad_profile);
236 wl_list_insert(&touchpad->motion_filters, &accel->link);
238 /* Setup initial state */
241 memset(touchpad->motion_history, 0, sizeof touchpad->motion_history);
242 touchpad->motion_index = 0;
243 touchpad->motion_count = 0;
245 touchpad->state = TOUCHPAD_STATE_NONE;
246 touchpad->last_finger_state = 0;
247 touchpad->finger_state = 0;
250 static inline struct touchpad_motion *
251 motion_history_offset(struct touchpad_dispatch *touchpad, int offset)
254 (touchpad->motion_index - offset + TOUCHPAD_HISTORY_LENGTH) %
255 TOUCHPAD_HISTORY_LENGTH;
257 return &touchpad->motion_history[offset_index];
261 estimate_delta(int x0, int x1, int x2, int x3)
263 return (x0 + x1 - x2 - x3) / 4;
267 hysteresis(int in, int center, int margin)
269 int diff = in - center;
270 if (abs(diff) <= margin)
274 return center + diff - margin;
275 else if (diff < -margin)
276 return center + diff + margin;
277 return center + diff;
281 touchpad_get_delta(struct touchpad_dispatch *touchpad, double *dx, double *dy)
283 *dx = estimate_delta(motion_history_offset(touchpad, 0)->x,
284 motion_history_offset(touchpad, 1)->x,
285 motion_history_offset(touchpad, 2)->x,
286 motion_history_offset(touchpad, 3)->x);
287 *dy = estimate_delta(motion_history_offset(touchpad, 0)->y,
288 motion_history_offset(touchpad, 1)->y,
289 motion_history_offset(touchpad, 2)->y,
290 motion_history_offset(touchpad, 3)->y);
294 filter_motion(struct touchpad_dispatch *touchpad,
295 double *dx, double *dy, uint32_t time)
297 struct weston_motion_filter *filter;
298 struct weston_motion_params motion;
303 wl_list_for_each(filter, &touchpad->motion_filters, link)
304 weston_filter_dispatch(filter, &motion, touchpad, time);
311 touchpad_update_state(struct touchpad_dispatch *touchpad, uint32_t time)
314 int center_x, center_y;
317 if (touchpad->reset ||
318 touchpad->last_finger_state != touchpad->finger_state) {
320 touchpad->motion_count = 0;
321 touchpad->event_mask = TOUCHPAD_EVENT_NONE;
322 touchpad->event_mask_filter =
323 TOUCHPAD_EVENT_ABSOLUTE_X | TOUCHPAD_EVENT_ABSOLUTE_Y;
325 touchpad->last_finger_state = touchpad->finger_state;
329 touchpad->last_finger_state = touchpad->finger_state;
331 if (!(touchpad->event_mask & TOUCHPAD_EVENT_REPORT))
334 touchpad->event_mask &= ~TOUCHPAD_EVENT_REPORT;
336 if ((touchpad->event_mask & touchpad->event_mask_filter) !=
337 touchpad->event_mask_filter)
340 touchpad->event_mask_filter = TOUCHPAD_EVENT_ABSOLUTE_ANY;
341 touchpad->event_mask = 0;
343 /* Avoid noice by moving center only when delta reaches a threshold
344 * distance from the old center. */
345 if (touchpad->motion_count > 0) {
346 center_x = hysteresis(touchpad->hw_abs.x,
347 touchpad->hysteresis.center_x,
348 touchpad->hysteresis.margin_x);
349 center_y = hysteresis(touchpad->hw_abs.y,
350 touchpad->hysteresis.center_y,
351 touchpad->hysteresis.margin_y);
354 center_x = touchpad->hw_abs.x;
355 center_y = touchpad->hw_abs.y;
357 touchpad->hysteresis.center_x = center_x;
358 touchpad->hysteresis.center_y = center_y;
359 touchpad->hw_abs.x = center_x;
360 touchpad->hw_abs.y = center_y;
362 /* Update motion history tracker */
363 motion_index = (touchpad->motion_index + 1) % TOUCHPAD_HISTORY_LENGTH;
364 touchpad->motion_index = motion_index;
365 touchpad->motion_history[motion_index].x = touchpad->hw_abs.x;
366 touchpad->motion_history[motion_index].y = touchpad->hw_abs.y;
367 if (touchpad->motion_count < 4)
368 touchpad->motion_count++;
370 if (touchpad->motion_count >= 4) {
371 touchpad_get_delta(touchpad, &dx, &dy);
373 filter_motion(touchpad, &dx, &dy, time);
375 touchpad->device->rel.dx = wl_fixed_from_double(dx);
376 touchpad->device->rel.dy = wl_fixed_from_double(dy);
377 touchpad->device->pending_events |= EVDEV_RELATIVE_MOTION;
382 process_absolute(struct touchpad_dispatch *touchpad,
383 struct evdev_device *device,
384 struct input_event *e)
388 if (e->value > touchpad->pressure.press)
389 touchpad->state = TOUCHPAD_STATE_PRESS;
390 else if (e->value > touchpad->pressure.touch_high)
391 touchpad->state = TOUCHPAD_STATE_TOUCH;
392 else if (e->value < touchpad->pressure.touch_low) {
393 if (touchpad->state > TOUCHPAD_STATE_NONE)
396 touchpad->state = TOUCHPAD_STATE_NONE;
401 if (touchpad->state >= TOUCHPAD_STATE_TOUCH) {
402 touchpad->hw_abs.x = e->value;
403 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
404 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_X;
408 if (touchpad->state >= TOUCHPAD_STATE_TOUCH) {
409 touchpad->hw_abs.y = e->value;
410 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_ANY;
411 touchpad->event_mask |= TOUCHPAD_EVENT_ABSOLUTE_Y;
418 process_key(struct touchpad_dispatch *touchpad,
419 struct evdev_device *device,
420 struct input_event *e,
425 if (!touchpad->has_pressure) {
427 touchpad->state = TOUCHPAD_STATE_NONE;
432 TOUCHPAD_STATE_TOUCH :
445 notify_button(device->seat,
447 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
448 WL_POINTER_BUTTON_STATE_RELEASED);
451 case BTN_TOOL_RUBBER:
453 case BTN_TOOL_PENCIL:
454 case BTN_TOOL_AIRBRUSH:
459 case BTN_TOOL_FINGER:
460 touchpad->finger_state &= ~TOUCHPAD_FINGERS_ONE;
462 touchpad->finger_state |= TOUCHPAD_FINGERS_ONE;
464 case BTN_TOOL_DOUBLETAP:
465 touchpad->finger_state &= ~TOUCHPAD_FINGERS_TWO;
467 touchpad->finger_state |= TOUCHPAD_FINGERS_TWO;
469 case BTN_TOOL_TRIPLETAP:
470 touchpad->finger_state &= ~TOUCHPAD_FINGERS_THREE;
472 touchpad->finger_state |= TOUCHPAD_FINGERS_THREE;
478 touchpad_process(struct evdev_dispatch *dispatch,
479 struct evdev_device *device,
480 struct input_event *e,
483 struct touchpad_dispatch *touchpad =
484 (struct touchpad_dispatch *) dispatch;
488 if (e->code == SYN_REPORT)
489 touchpad->event_mask |= TOUCHPAD_EVENT_REPORT;
492 process_absolute(touchpad, device, e);
495 process_key(touchpad, device, e, time);
499 touchpad_update_state(touchpad, time);
503 touchpad_destroy(struct evdev_dispatch *dispatch)
505 struct touchpad_dispatch *touchpad =
506 (struct touchpad_dispatch *) dispatch;
507 struct weston_motion_filter *filter;
508 struct weston_motion_filter *next;
510 wl_list_for_each_safe(filter, next, &touchpad->motion_filters, link)
511 filter->interface->destroy(filter);
516 struct evdev_dispatch_interface touchpad_interface = {
521 struct evdev_dispatch *
522 evdev_touchpad_create(struct evdev_device *device)
524 struct touchpad_dispatch *touchpad;
526 touchpad = malloc(sizeof *touchpad);
527 if (touchpad == NULL)
530 touchpad->base.interface = &touchpad_interface;
532 touchpad->device = device;
533 wl_list_init(&touchpad->motion_filters);
535 configure_touchpad(touchpad, device);
537 return &touchpad->base;