touchpad: add a touchpad driver based on per-finger tracking
[platform/upstream/libinput.git] / src / evdev-mt-touchpad.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
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.
13  *
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.
21  */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdbool.h>
27
28 #include "evdev.h"
29
30 #define TOUCHPAD_HISTORY_LENGTH 4
31
32 #define tp_for_each_touch(_tp, _t) \
33         for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
34
35 enum touch_state {
36         TOUCH_NONE = 0,
37         TOUCH_BEGIN,
38         TOUCH_UPDATE,
39         TOUCH_END
40 };
41
42 struct tp_motion {
43         int32_t x;
44         int32_t y;
45 };
46
47 struct tp_touch {
48         enum touch_state state;
49         bool dirty;
50         int32_t x;
51         int32_t y;
52         uint32_t millis;
53
54         struct {
55                 struct tp_motion samples[TOUCHPAD_HISTORY_LENGTH];
56                 unsigned int index;
57                 unsigned int count;
58         } history;
59 };
60
61 struct tp_dispatch {
62         struct evdev_dispatch base;
63         struct evdev_device *device;
64         unsigned int nfingers_down;             /* number of fingers down */
65         unsigned int slot;                      /* current slot */
66
67         unsigned int ntouches;                  /* number of slots */
68         struct tp_touch *touches;               /* len == ntouches */
69 };
70
71 static inline struct tp_motion *
72 tp_motion_history_offset(struct tp_touch *t, int offset)
73 {
74         int offset_index =
75                 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
76                 TOUCHPAD_HISTORY_LENGTH;
77
78         return &t->history.samples[offset_index];
79 }
80
81 static inline void
82 tp_motion_history_push(struct tp_touch *t)
83 {
84         int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
85
86         if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
87                 t->history.count++;
88
89         t->history.samples[motion_index].x = t->x;
90         t->history.samples[motion_index].y = t->y;
91         t->history.index = motion_index;
92 }
93
94 static inline void
95 tp_motion_history_reset(struct tp_touch *t)
96 {
97         t->history.count = 0;
98 }
99
100 static inline struct tp_touch *
101 tp_current_touch(struct tp_dispatch *tp)
102 {
103         return &tp->touches[min(tp->slot, tp->ntouches)];
104 }
105
106 static inline void
107 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
108 {
109         if (t->state != TOUCH_UPDATE) {
110                 tp_motion_history_reset(t);
111                 t->dirty = true;
112                 t->state = TOUCH_BEGIN;
113                 tp->nfingers_down++;
114                 assert(tp->nfingers_down >= 1);
115         }
116 }
117
118 static inline void
119 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
120 {
121         if (t->state == TOUCH_NONE)
122                 return;
123
124         t->dirty = true;
125         t->state = TOUCH_END;
126         assert(tp->nfingers_down >= 1);
127         tp->nfingers_down--;
128 }
129
130 static double
131 tp_estimate_delta(int x0, int x1, int x2, int x3)
132 {
133         return (x0 + x1 - x2 - x3) / 4;
134 }
135
136 static void
137 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
138 {
139         if (t->history.count < 4) {
140                 *dx = 0;
141                 *dy = 0;
142                 return;
143         }
144
145         *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
146                                 tp_motion_history_offset(t, 1)->x,
147                                 tp_motion_history_offset(t, 2)->x,
148                                 tp_motion_history_offset(t, 3)->x);
149         *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
150                                 tp_motion_history_offset(t, 1)->y,
151                                 tp_motion_history_offset(t, 2)->y,
152                                 tp_motion_history_offset(t, 3)->y);
153 }
154
155 static void
156 tp_process_absolute(struct tp_dispatch *tp,
157                     const struct input_event *e,
158                     uint32_t time)
159 {
160         struct tp_touch *t = tp_current_touch(tp);
161
162         switch(e->code) {
163         case ABS_MT_POSITION_X:
164                 t->x = e->value;
165                 t->millis = time;
166                 t->dirty = true;
167                 break;
168         case ABS_MT_POSITION_Y:
169                 t->y = e->value;
170                 t->millis = time;
171                 t->dirty = true;
172                 break;
173         case ABS_MT_SLOT:
174                 tp->slot = e->value;
175                 break;
176         case ABS_MT_TRACKING_ID:
177                 t->millis = time;
178                 if (e->value != -1)
179                         tp_begin_touch(tp, t);
180                 else
181                         tp_end_touch(tp, t);
182         }
183 }
184
185 static void
186 tp_process_key(struct tp_dispatch *tp,
187                const struct input_event *e,
188                uint32_t time)
189 {
190         switch (e->code) {
191                 case BTN_LEFT:
192                 case BTN_MIDDLE:
193                 case BTN_RIGHT:
194                         pointer_notify_button(
195                                 &tp->device->base,
196                                 time,
197                                 e->code,
198                                 e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
199                                            LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
200                         break;
201         }
202 }
203
204 static void
205 tp_process_state(struct tp_dispatch *tp, uint32_t time)
206 {
207         struct tp_touch *t;
208
209         tp_for_each_touch(tp, t) {
210                 if (!t->dirty)
211                         continue;
212
213                 tp_motion_history_push(t);
214         }
215 }
216
217 static void
218 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
219 {
220         struct tp_touch *t;
221
222         tp_for_each_touch(tp, t) {
223                 if (!t->dirty)
224                         continue;
225
226                 if (t->state == TOUCH_END)
227                         t->state = TOUCH_NONE;
228                 else if (t->state == TOUCH_BEGIN)
229                         t->state = TOUCH_UPDATE;
230
231                 t->dirty = false;
232         }
233 }
234
235 static void
236 tp_post_events(struct tp_dispatch *tp, uint32_t time)
237 {
238         struct tp_touch *t = tp_current_touch(tp);
239         double dx, dy;
240
241         if (tp->nfingers_down != 1)
242                 return;
243
244         tp_get_delta(t, &dx, &dy);
245
246         if (dx != 0 || dy != 0)
247                 pointer_notify_motion(
248                         &tp->device->base,
249                         time,
250                         li_fixed_from_double(dx),
251                         li_fixed_from_double(dy));
252 }
253
254 static void
255 tp_process(struct evdev_dispatch *dispatch,
256            struct evdev_device *device,
257            struct input_event *e,
258            uint32_t time)
259 {
260         struct tp_dispatch *tp =
261                 (struct tp_dispatch *)dispatch;
262
263         switch (e->type) {
264         case EV_ABS:
265                 tp_process_absolute(tp, e, time);
266                 break;
267         case EV_KEY:
268                 tp_process_key(tp, e, time);
269                 break;
270         case EV_SYN:
271                 tp_process_state(tp, time);
272                 tp_post_events(tp, time);
273                 tp_post_process_state(tp, time);
274                 break;
275         }
276 }
277
278 static void
279 tp_destroy(struct evdev_dispatch *dispatch)
280 {
281         struct tp_dispatch *tp =
282                 (struct tp_dispatch*)dispatch;
283
284         free(tp->touches);
285         free(tp);
286 }
287
288 static struct evdev_dispatch_interface tp_interface = {
289         tp_process,
290         tp_destroy
291 };
292
293 static int
294 tp_init_slots(struct tp_dispatch *tp,
295               struct evdev_device *device)
296 {
297         struct input_absinfo absinfo = {0};
298
299         ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT), &absinfo);
300
301         tp->ntouches = absinfo.maximum + 1;
302         tp->touches = calloc(tp->ntouches,
303                              sizeof(struct tp_touch));
304         tp->slot = absinfo.value;
305
306         return 0;
307 }
308
309
310 static int
311 tp_init(struct tp_dispatch *tp,
312         struct evdev_device *device)
313 {
314         tp->base.interface = &tp_interface;
315         tp->device = device;
316
317         if (tp_init_slots(tp, device) != 0)
318                 return -1;
319
320         return 0;
321 }
322
323 struct evdev_dispatch *
324 evdev_mt_touchpad_create(struct evdev_device *device)
325 {
326         struct tp_dispatch *tp;
327
328         tp = zalloc(sizeof *tp);
329         if (!tp)
330                 return NULL;
331
332         if (tp_init(tp, device) != 0) {
333                 tp_destroy(&tp->base);
334                 return NULL;
335         }
336
337         return  &tp->base;
338 }