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