touchpad: move softbutton initialization to separate function
[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 touch_state {
46         TOUCH_NONE = 0,
47         TOUCH_BEGIN,
48         TOUCH_UPDATE,
49         TOUCH_END
50 };
51
52 enum button_event {
53         BUTTON_EVENT_IN_BOTTOM_R = 30,
54         BUTTON_EVENT_IN_BOTTOM_L,
55         BUTTON_EVENT_IN_TOP_R,
56         BUTTON_EVENT_IN_TOP_M,
57         BUTTON_EVENT_IN_TOP_L,
58         BUTTON_EVENT_IN_AREA,
59         BUTTON_EVENT_UP,
60         BUTTON_EVENT_PRESS,
61         BUTTON_EVENT_RELEASE,
62         BUTTON_EVENT_TIMEOUT,
63 };
64
65 enum button_state {
66         BUTTON_STATE_NONE,
67         BUTTON_STATE_AREA,
68         BUTTON_STATE_BOTTOM,
69         BUTTON_STATE_TOP,
70         BUTTON_STATE_TOP_NEW,
71         BUTTON_STATE_TOP_TO_IGNORE,
72         BUTTON_STATE_IGNORE,
73 };
74
75 enum tp_tap_state {
76         TAP_STATE_IDLE = 4,
77         TAP_STATE_TOUCH,
78         TAP_STATE_HOLD,
79         TAP_STATE_TAPPED,
80         TAP_STATE_TOUCH_2,
81         TAP_STATE_TOUCH_2_HOLD,
82         TAP_STATE_TOUCH_3,
83         TAP_STATE_TOUCH_3_HOLD,
84         TAP_STATE_DRAGGING_OR_DOUBLETAP,
85         TAP_STATE_DRAGGING,
86         TAP_STATE_DRAGGING_WAIT,
87         TAP_STATE_DRAGGING_2,
88         TAP_STATE_DEAD, /**< finger count exceeded */
89 };
90
91 enum tp_tap_touch_state {
92         TAP_TOUCH_STATE_IDLE = 16,      /**< not in touch */
93         TAP_TOUCH_STATE_TOUCH,          /**< touching, may tap */
94         TAP_TOUCH_STATE_DEAD,           /**< exceeded motion/timeout */
95 };
96
97 struct tp_motion {
98         int32_t x;
99         int32_t y;
100 };
101
102 struct tp_touch {
103         struct tp_dispatch *tp;
104         enum touch_state state;
105         bool dirty;
106         bool is_pointer;                        /* the pointer-controlling touch */
107         int32_t x;
108         int32_t y;
109         uint64_t millis;
110
111         struct {
112                 struct tp_motion samples[TOUCHPAD_HISTORY_LENGTH];
113                 unsigned int index;
114                 unsigned int count;
115         } history;
116
117         struct {
118                 int32_t center_x;
119                 int32_t center_y;
120         } hysteresis;
121
122         /* A pinned touchpoint is the one that pressed the physical button
123          * on a clickpad. After the release, it won't move until the center
124          * moves more than a threshold away from the original coordinates
125          */
126         struct {
127                 bool is_pinned;
128                 int32_t center_x;
129                 int32_t center_y;
130         } pinned;
131
132         /* Software-button state and timeout if applicable */
133         struct {
134                 enum button_state state;
135                 /* We use button_event here so we can use == on events */
136                 enum button_event curr;
137                 struct libinput_timer timer;
138         } button;
139
140         struct {
141                 enum tp_tap_touch_state state;
142         } tap;
143
144         struct {
145                 bool is_palm;
146                 int32_t x, y;  /* first coordinates if is_palm == true */
147                 uint32_t time; /* first timestamp if is_palm == true */
148         } palm;
149 };
150
151 struct tp_dispatch {
152         struct evdev_dispatch base;
153         struct evdev_device *device;
154         unsigned int nfingers_down;             /* number of fingers down */
155         unsigned int old_nfingers_down;         /* previous no fingers down */
156         unsigned int slot;                      /* current slot */
157         bool has_mt;
158         bool semi_mt;
159
160         unsigned int real_touches;              /* number of slots */
161         unsigned int ntouches;                  /* no slots inc. fakes */
162         struct tp_touch *touches;               /* len == ntouches */
163         unsigned int fake_touches;              /* fake touch mask */
164
165         struct {
166                 int32_t margin_x;
167                 int32_t margin_y;
168         } hysteresis;
169
170         struct motion_filter *filter;
171
172         struct {
173                 double x_scale_coeff;
174                 double y_scale_coeff;
175         } accel;
176
177         struct {
178                 bool is_clickpad;               /* true for clickpads */
179                 bool has_topbuttons;
180                 bool use_clickfinger;           /* number of fingers decides button number */
181                 bool click_pending;
182                 uint32_t state;
183                 uint32_t old_state;
184                 uint32_t motion_dist;           /* for pinned touches */
185                 unsigned int active;            /* currently active button, for release event */
186
187                 /* Only used for clickpads. The software button areas are
188                  * always 2 horizontal stripes across the touchpad.
189                  * The buttons are split according to the edge settings.
190                  */
191                 struct {
192                         int32_t top_edge;
193                         int32_t rightbutton_left_edge;
194                 } bottom_area;
195
196                 struct {
197                         int32_t bottom_edge;
198                         int32_t rightbutton_left_edge;
199                         int32_t leftbutton_right_edge;
200                 } top_area;
201         } buttons;                              /* physical buttons */
202
203         struct {
204                 enum libinput_pointer_axis direction;
205         } scroll;
206
207         enum touchpad_event queued;
208
209         struct {
210                 struct libinput_device_config_tap config;
211                 bool enabled;
212                 struct libinput_timer timer;
213                 enum tp_tap_state state;
214         } tap;
215
216         struct {
217                 int32_t right_edge;
218                 int32_t left_edge;
219         } palm;
220
221         struct {
222                 struct libinput_device_config_send_events config;
223                 enum libinput_config_send_events_mode current_mode;
224         } sendevents;
225 };
226
227 #define tp_for_each_touch(_tp, _t) \
228         for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
229
230 void
231 tp_get_delta(struct tp_touch *t, double *dx, double *dy);
232
233 void
234 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t);
235
236 int
237 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time);
238
239 int
240 tp_init_tap(struct tp_dispatch *tp);
241
242 void
243 tp_destroy_tap(struct tp_dispatch *tp);
244
245 int
246 tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);
247
248 void
249 tp_init_softbuttons(struct tp_dispatch *tp, struct evdev_device *device);
250
251 void
252 tp_destroy_buttons(struct tp_dispatch *tp);
253
254 int
255 tp_process_button(struct tp_dispatch *tp,
256                   const struct input_event *e,
257                   uint64_t time);
258
259 void
260 tp_release_all_buttons(struct tp_dispatch *tp,
261                        uint64_t time);
262
263 int
264 tp_post_button_events(struct tp_dispatch *tp, uint64_t time);
265
266 int
267 tp_button_handle_state(struct tp_dispatch *tp, uint64_t time);
268
269 int
270 tp_button_touch_active(struct tp_dispatch *tp, struct tp_touch *t);
271
272 bool
273 tp_button_is_inside_softbutton_area(struct tp_dispatch *tp, struct tp_touch *t);
274
275 void
276 tp_release_all_taps(struct tp_dispatch *tp,
277                     uint64_t time);
278
279 #endif