touchpad: rename scroll.state to scroll.edge_state
[platform/upstream/libinput.git] / src / evdev-mt-touchpad.h
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
24 #ifndef EVDEV_MT_TOUCHPAD_H
25 #define EVDEV_MT_TOUCHPAD_H
26
27 #include <stdbool.h>
28
29 #include "evdev.h"
30 #include "filter.h"
31 #include "timer.h"
32
33 #define TOUCHPAD_HISTORY_LENGTH 4
34 #define TOUCHPAD_MIN_SAMPLES 4
35
36 #define VENDOR_ID_APPLE 0x5ac
37
38 enum touchpad_event {
39         TOUCHPAD_EVENT_NONE             = 0,
40         TOUCHPAD_EVENT_MOTION           = (1 << 0),
41         TOUCHPAD_EVENT_BUTTON_PRESS     = (1 << 1),
42         TOUCHPAD_EVENT_BUTTON_RELEASE   = (1 << 2),
43 };
44
45 enum touchpad_model {
46         MODEL_UNKNOWN = 0,
47         MODEL_SYNAPTICS,
48         MODEL_ALPS,
49         MODEL_APPLETOUCH,
50         MODEL_ELANTECH,
51         MODEL_UNIBODY_MACBOOK
52 };
53
54 enum touch_state {
55         TOUCH_NONE = 0,
56         TOUCH_BEGIN,
57         TOUCH_UPDATE,
58         TOUCH_END
59 };
60
61 enum button_event {
62         BUTTON_EVENT_IN_BOTTOM_R = 30,
63         BUTTON_EVENT_IN_BOTTOM_L,
64         BUTTON_EVENT_IN_TOP_R,
65         BUTTON_EVENT_IN_TOP_M,
66         BUTTON_EVENT_IN_TOP_L,
67         BUTTON_EVENT_IN_AREA,
68         BUTTON_EVENT_UP,
69         BUTTON_EVENT_PRESS,
70         BUTTON_EVENT_RELEASE,
71         BUTTON_EVENT_TIMEOUT,
72 };
73
74 enum button_state {
75         BUTTON_STATE_NONE,
76         BUTTON_STATE_AREA,
77         BUTTON_STATE_BOTTOM,
78         BUTTON_STATE_TOP,
79         BUTTON_STATE_TOP_NEW,
80         BUTTON_STATE_TOP_TO_IGNORE,
81         BUTTON_STATE_IGNORE,
82 };
83
84 enum tp_tap_state {
85         TAP_STATE_IDLE = 4,
86         TAP_STATE_TOUCH,
87         TAP_STATE_HOLD,
88         TAP_STATE_TAPPED,
89         TAP_STATE_TOUCH_2,
90         TAP_STATE_TOUCH_2_HOLD,
91         TAP_STATE_TOUCH_3,
92         TAP_STATE_TOUCH_3_HOLD,
93         TAP_STATE_DRAGGING_OR_DOUBLETAP,
94         TAP_STATE_DRAGGING,
95         TAP_STATE_DRAGGING_WAIT,
96         TAP_STATE_DRAGGING_2,
97         TAP_STATE_DEAD, /**< finger count exceeded */
98 };
99
100 enum tp_tap_touch_state {
101         TAP_TOUCH_STATE_IDLE = 16,      /**< not in touch */
102         TAP_TOUCH_STATE_TOUCH,          /**< touching, may tap */
103         TAP_TOUCH_STATE_DEAD,           /**< exceeded motion/timeout */
104 };
105
106 /* For edge scrolling, so we only care about right and bottom */
107 enum tp_edge {
108         EDGE_NONE = 0,
109         EDGE_RIGHT = (1 << 0),
110         EDGE_BOTTOM = (1 << 1),
111 };
112
113 enum tp_edge_scroll_touch_state {
114         EDGE_SCROLL_TOUCH_STATE_NONE,
115         EDGE_SCROLL_TOUCH_STATE_EDGE_NEW,
116         EDGE_SCROLL_TOUCH_STATE_EDGE,
117         EDGE_SCROLL_TOUCH_STATE_AREA,
118 };
119
120 struct tp_motion {
121         int32_t x;
122         int32_t y;
123 };
124
125 struct tp_touch {
126         struct tp_dispatch *tp;
127         enum touch_state state;
128         bool dirty;
129         bool is_pointer;                        /* the pointer-controlling touch */
130         int32_t x;
131         int32_t y;
132         uint64_t millis;
133
134         struct {
135                 struct tp_motion samples[TOUCHPAD_HISTORY_LENGTH];
136                 unsigned int index;
137                 unsigned int count;
138         } history;
139
140         struct {
141                 int32_t center_x;
142                 int32_t center_y;
143         } hysteresis;
144
145         /* A pinned touchpoint is the one that pressed the physical button
146          * on a clickpad. After the release, it won't move until the center
147          * moves more than a threshold away from the original coordinates
148          */
149         struct {
150                 bool is_pinned;
151                 int32_t center_x;
152                 int32_t center_y;
153         } pinned;
154
155         /* Software-button state and timeout if applicable */
156         struct {
157                 enum button_state state;
158                 /* We use button_event here so we can use == on events */
159                 enum button_event curr;
160                 struct libinput_timer timer;
161         } button;
162
163         struct {
164                 enum tp_tap_touch_state state;
165         } tap;
166
167         struct {
168                 enum tp_edge_scroll_touch_state edge_state;
169                 uint32_t edge;
170                 int direction;
171                 double threshold;
172                 struct libinput_timer timer;
173         } scroll;
174
175         struct {
176                 bool is_palm;
177                 int32_t x, y;  /* first coordinates if is_palm == true */
178                 uint32_t time; /* first timestamp if is_palm == true */
179         } palm;
180 };
181
182 struct tp_dispatch {
183         struct evdev_dispatch base;
184         struct evdev_device *device;
185         unsigned int nfingers_down;             /* number of fingers down */
186         unsigned int old_nfingers_down;         /* previous no fingers down */
187         unsigned int slot;                      /* current slot */
188         bool has_mt;
189         bool semi_mt;
190         enum touchpad_model model;
191
192         unsigned int real_touches;              /* number of slots */
193         unsigned int ntouches;                  /* no slots inc. fakes */
194         struct tp_touch *touches;               /* len == ntouches */
195         unsigned int fake_touches;              /* fake touch mask */
196
197         struct {
198                 int32_t margin_x;
199                 int32_t margin_y;
200         } hysteresis;
201
202         struct {
203                 double x_scale_coeff;
204                 double y_scale_coeff;
205         } accel;
206
207         struct {
208                 bool is_clickpad;               /* true for clickpads */
209                 bool has_topbuttons;
210                 bool use_clickfinger;           /* number of fingers decides button number */
211                 bool click_pending;
212                 uint32_t state;
213                 uint32_t old_state;
214                 uint32_t motion_dist;           /* for pinned touches */
215                 unsigned int active;            /* currently active button, for release event */
216                 bool active_is_topbutton;       /* is active a top button? */
217
218                 /* Only used for clickpads. The software button areas are
219                  * always 2 horizontal stripes across the touchpad.
220                  * The buttons are split according to the edge settings.
221                  */
222                 struct {
223                         int32_t top_edge;
224                         int32_t rightbutton_left_edge;
225                 } bottom_area;
226
227                 struct {
228                         int32_t bottom_edge;
229                         int32_t rightbutton_left_edge;
230                         int32_t leftbutton_right_edge;
231                 } top_area;
232
233                 struct evdev_device *trackpoint;
234         } buttons;                              /* physical buttons */
235
236         struct {
237                 struct libinput_device_config_scroll_method config_method;
238                 enum libinput_config_scroll_method method;
239                 int32_t right_edge;
240                 int32_t bottom_edge;
241         } scroll;
242
243         enum touchpad_event queued;
244
245         struct {
246                 struct libinput_device_config_tap config;
247                 bool enabled;
248                 bool suspended;
249                 struct libinput_timer timer;
250                 enum tp_tap_state state;
251                 uint32_t buttons_pressed;
252         } tap;
253
254         struct {
255                 int32_t right_edge;
256                 int32_t left_edge;
257         } palm;
258
259         struct {
260                 struct libinput_device_config_send_events config;
261                 enum libinput_config_send_events_mode current_mode;
262                 bool trackpoint_active;
263                 struct libinput_event_listener trackpoint_listener;
264                 struct libinput_timer trackpoint_timer;
265         } sendevents;
266 };
267
268 #define tp_for_each_touch(_tp, _t) \
269         for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
270
271 void
272 tp_get_delta(struct tp_touch *t, double *dx, double *dy);
273
274 void
275 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t);
276
277 void
278 tp_filter_motion(struct tp_dispatch *tp,
279                  double *dx, double *dy,
280                  double *dx_unaccel, double *dy_unaccel,
281                  uint64_t time);
282
283 int
284 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time);
285
286 int
287 tp_init_tap(struct tp_dispatch *tp);
288
289 void
290 tp_remove_tap(struct tp_dispatch *tp);
291
292 int
293 tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);
294
295 void
296 tp_init_softbuttons(struct tp_dispatch *tp,
297                     struct evdev_device *device,
298                     double topbutton_size_mult);
299
300 void
301 tp_remove_buttons(struct tp_dispatch *tp);
302
303 int
304 tp_process_button(struct tp_dispatch *tp,
305                   const struct input_event *e,
306                   uint64_t time);
307
308 void
309 tp_release_all_buttons(struct tp_dispatch *tp,
310                        uint64_t time);
311
312 int
313 tp_post_button_events(struct tp_dispatch *tp, uint64_t time);
314
315 int
316 tp_button_handle_state(struct tp_dispatch *tp, uint64_t time);
317
318 int
319 tp_button_touch_active(struct tp_dispatch *tp, struct tp_touch *t);
320
321 bool
322 tp_button_is_inside_softbutton_area(struct tp_dispatch *tp, struct tp_touch *t);
323
324 void
325 tp_release_all_taps(struct tp_dispatch *tp,
326                     uint64_t time);
327
328 void
329 tp_tap_suspend(struct tp_dispatch *tp, uint64_t time);
330
331 void
332 tp_tap_resume(struct tp_dispatch *tp, uint64_t time);
333
334 bool
335 tp_tap_dragging(struct tp_dispatch *tp);
336
337 int
338 tp_edge_scroll_init(struct tp_dispatch *tp, struct evdev_device *device);
339
340 void
341 tp_remove_edge_scroll(struct tp_dispatch *tp);
342
343 void
344 tp_edge_scroll_handle_state(struct tp_dispatch *tp, uint64_t time);
345
346 int
347 tp_edge_scroll_post_events(struct tp_dispatch *tp, uint64_t time);
348
349 void
350 tp_edge_scroll_stop_events(struct tp_dispatch *tp, uint64_t time);
351
352 int
353 tp_edge_scroll_touch_active(struct tp_dispatch *tp, struct tp_touch *t);
354
355 #endif