touchpad: move button-related code into a separate file
[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 scroll_state {
50         SCROLL_STATE_NONE,
51         SCROLL_STATE_SCROLLING
52 };
53
54 enum tp_tap_state {
55         TAP_STATE_IDLE = 4,
56         TAP_STATE_TOUCH,
57         TAP_STATE_HOLD,
58         TAP_STATE_TAPPED,
59         TAP_STATE_TOUCH_2,
60         TAP_STATE_TOUCH_2_HOLD,
61         TAP_STATE_TOUCH_3,
62         TAP_STATE_TOUCH_3_HOLD,
63         TAP_STATE_DRAGGING_OR_DOUBLETAP,
64         TAP_STATE_DRAGGING,
65         TAP_STATE_DRAGGING_WAIT,
66         TAP_STATE_DRAGGING_2,
67         TAP_STATE_DEAD, /**< finger count exceeded */
68 };
69
70 struct tp_motion {
71         int32_t x;
72         int32_t y;
73 };
74
75 struct tp_touch {
76         enum touch_state state;
77         bool dirty;
78         bool fake;                              /* a fake touch */
79         bool is_pointer;                        /* the pointer-controlling touch */
80         int32_t x;
81         int32_t y;
82         uint32_t millis;
83
84         struct {
85                 struct tp_motion samples[TOUCHPAD_HISTORY_LENGTH];
86                 unsigned int index;
87                 unsigned int count;
88         } history;
89
90         struct {
91                 int32_t center_x;
92                 int32_t center_y;
93         } hysteresis;
94
95         /* A pinned touchpoint is the one that pressed the physical button
96          * on a clickpad. After the release, it won't move until the center
97          * moves more than a threshold away from the original coordinates
98          */
99         struct {
100                 bool is_pinned;
101                 int32_t center_x;
102                 int32_t center_y;
103         } pinned;
104 };
105
106 struct tp_dispatch {
107         struct evdev_dispatch base;
108         struct evdev_device *device;
109         unsigned int nfingers_down;             /* number of fingers down */
110         unsigned int slot;                      /* current slot */
111         bool has_mt;
112
113         unsigned int ntouches;                  /* number of slots */
114         struct tp_touch *touches;               /* len == ntouches */
115         unsigned int fake_touches;              /* fake touch mask */
116
117         struct {
118                 int32_t margin_x;
119                 int32_t margin_y;
120         } hysteresis;
121
122         struct motion_filter *filter;
123
124         struct {
125                 double constant_factor;
126                 double min_factor;
127                 double max_factor;
128         } accel;
129
130         struct {
131                 bool has_buttons;               /* true for physical LMR buttons */
132                 uint32_t state;
133                 uint32_t old_state;
134                 uint32_t motion_dist;           /* for pinned touches */
135         } buttons;                              /* physical buttons */
136
137         struct {
138                 enum scroll_state state;
139                 enum libinput_pointer_axis direction;
140         } scroll;
141
142         enum touchpad_event queued;
143
144         struct {
145                 bool enabled;
146                 int timer_fd;
147                 struct libinput_source *source;
148                 unsigned int timeout;
149                 enum tp_tap_state state;
150         } tap;
151 };
152
153 #define tp_for_each_touch(_tp, _t) \
154         for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
155
156 void
157 tp_get_delta(struct tp_touch *t, double *dx, double *dy);
158
159 int
160 tp_tap_handle_state(struct tp_dispatch *tp, uint32_t time);
161
162 unsigned int
163 tp_tap_handle_timeout(struct tp_dispatch *tp, uint32_t time);
164
165 int
166 tp_init_tap(struct tp_dispatch *tp);
167
168 void
169 tp_destroy_tap(struct tp_dispatch *tp);
170
171 int
172 tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);
173
174 int
175 tp_process_button(struct tp_dispatch *tp,
176                   const struct input_event *e,
177                   uint32_t time);
178
179 int
180 tp_post_button_events(struct tp_dispatch *tp, uint32_t time);
181
182 #endif