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