evdev: Add a remove callback to the evdev_dispatch_interface
[platform/upstream/libinput.git] / src / evdev.h
1 /*
2  * Copyright © 2011, 2012 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #ifndef EVDEV_H
25 #define EVDEV_H
26
27 #include "config.h"
28
29 #include <stdbool.h>
30 #include "linux/input.h"
31 #include <libevdev/libevdev.h>
32
33 #include "libinput-private.h"
34 #include "timer.h"
35
36 enum evdev_event_type {
37         EVDEV_NONE,
38         EVDEV_ABSOLUTE_TOUCH_DOWN,
39         EVDEV_ABSOLUTE_MOTION,
40         EVDEV_ABSOLUTE_TOUCH_UP,
41         EVDEV_ABSOLUTE_MT_DOWN,
42         EVDEV_ABSOLUTE_MT_MOTION,
43         EVDEV_ABSOLUTE_MT_UP,
44         EVDEV_RELATIVE_MOTION,
45 };
46
47 enum evdev_device_seat_capability {
48         EVDEV_DEVICE_POINTER = (1 << 0),
49         EVDEV_DEVICE_KEYBOARD = (1 << 1),
50         EVDEV_DEVICE_TOUCH = (1 << 2)
51 };
52
53 enum evdev_device_tags {
54         EVDEV_TAG_EXTERNAL_MOUSE = (1 << 0),
55         EVDEV_TAG_INTERNAL_TOUCHPAD = (1 << 1),
56         EVDEV_TAG_TRACKPOINT = (1 << 2),
57 };
58
59 struct mt_slot {
60         int32_t seat_slot;
61         int32_t x, y;
62 };
63
64 struct evdev_device {
65         struct libinput_device base;
66
67         struct libinput_source *source;
68
69         struct evdev_dispatch *dispatch;
70         struct libevdev *evdev;
71         struct udev_device *udev_device;
72         char *output_name;
73         const char *devname;
74         bool was_removed;
75         int fd;
76         struct {
77                 const struct input_absinfo *absinfo_x, *absinfo_y;
78                 int fake_resolution;
79
80                 int32_t x, y;
81                 int32_t seat_slot;
82
83                 int apply_calibration;
84                 struct matrix calibration;
85                 struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
86                 struct matrix usermatrix; /* as supplied by the caller */
87         } abs;
88
89         struct {
90                 int slot;
91                 struct mt_slot *slots;
92                 size_t slots_len;
93         } mt;
94         struct mtdev *mtdev;
95
96         struct {
97                 int dx, dy;
98         } rel;
99
100         struct {
101                 struct libinput_timer timer;
102                 struct libinput_device_config_scroll_method config;
103                 /* Currently enabled method, button */
104                 enum libinput_config_scroll_method method;
105                 uint32_t button;
106                 /* set during device init, used at runtime to delay changes
107                  * until all buttons are up */
108                 enum libinput_config_scroll_method want_method;
109                 uint32_t want_button;
110                 /* Checks if buttons are down and commits the setting */
111                 void (*change_scroll_method)(struct evdev_device *device);
112                 bool button_scroll_active;
113                 double threshold;
114                 uint32_t direction;
115                 double buildup_vertical;
116                 double buildup_horizontal;
117
118                 struct libinput_device_config_natural_scroll config_natural;
119                 /* set during device init if we want natural scrolling,
120                  * used at runtime to enable/disable the feature */
121                 bool natural_scrolling_enabled;
122         } scroll;
123
124         enum evdev_event_type pending_event;
125         enum evdev_device_seat_capability seat_caps;
126         enum evdev_device_tags tags;
127
128         int is_mt;
129         int suspended;
130
131         struct {
132                 struct libinput_device_config_accel config;
133                 struct motion_filter *filter;
134         } pointer;
135
136         /* Bitmask of pressed keys used to ignore initial release events from
137          * the kernel. */
138         unsigned long hw_key_mask[NLONGS(KEY_CNT)];
139         /* Key counter used for multiplexing button events internally in
140          * libinput. */
141         uint8_t key_count[KEY_CNT];
142
143         struct {
144                 struct libinput_device_config_left_handed config_left_handed;
145                 /* left-handed currently enabled */
146                 bool left_handed;
147                 /* set during device init if we want left_handed config,
148                  * used at runtime to delay the effect until buttons are up */
149                 bool want_left_handed;
150                 /* Checks if buttons are down and commits the setting */
151                 void (*change_to_left_handed)(struct evdev_device *device);
152         } buttons;
153
154         int dpi; /* HW resolution */
155         struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
156 };
157
158 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
159
160 struct evdev_dispatch;
161
162 struct evdev_dispatch_interface {
163         /* Process an evdev input event. */
164         void (*process)(struct evdev_dispatch *dispatch,
165                         struct evdev_device *device,
166                         struct input_event *event,
167                         uint64_t time);
168
169         /* Device is being removed (may be NULL) */
170         void (*remove)(struct evdev_dispatch *dispatch);
171
172         /* Destroy an event dispatch handler and free all its resources. */
173         void (*destroy)(struct evdev_dispatch *dispatch);
174
175         /* A new device was added */
176         void (*device_added)(struct evdev_device *device,
177                              struct evdev_device *added_device);
178
179         /* A device was removed */
180         void (*device_removed)(struct evdev_device *device,
181                                struct evdev_device *removed_device);
182
183         /* A device was suspended */
184         void (*device_suspended)(struct evdev_device *device,
185                                  struct evdev_device *suspended_device);
186
187         /* A device was resumed */
188         void (*device_resumed)(struct evdev_device *device,
189                                struct evdev_device *resumed_device);
190
191         /* Tag device with one of EVDEV_TAG */
192         void (*tag_device)(struct evdev_device *device,
193                            struct udev_device *udev_device);
194 };
195
196 struct evdev_dispatch {
197         struct evdev_dispatch_interface *interface;
198         struct libinput_device_config_calibration calibration;
199
200         struct {
201                 struct libinput_device_config_send_events config;
202                 enum libinput_config_send_events_mode current_mode;
203         } sendevents;
204 };
205
206 struct evdev_device *
207 evdev_device_create(struct libinput_seat *seat,
208                     struct udev_device *device);
209
210 int
211 evdev_device_init_pointer_acceleration(struct evdev_device *device);
212
213 struct evdev_dispatch *
214 evdev_touchpad_create(struct evdev_device *device);
215
216 struct evdev_dispatch *
217 evdev_mt_touchpad_create(struct evdev_device *device);
218
219 void
220 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
221
222 int
223 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
224
225 const char *
226 evdev_device_get_output(struct evdev_device *device);
227
228 const char *
229 evdev_device_get_sysname(struct evdev_device *device);
230
231 const char *
232 evdev_device_get_name(struct evdev_device *device);
233
234 unsigned int
235 evdev_device_get_id_product(struct evdev_device *device);
236
237 unsigned int
238 evdev_device_get_id_vendor(struct evdev_device *device);
239
240 struct udev_device *
241 evdev_device_get_udev_device(struct evdev_device *device);
242
243 void
244 evdev_device_set_default_calibration(struct evdev_device *device,
245                                      const float calibration[6]);
246 void
247 evdev_device_calibrate(struct evdev_device *device,
248                        const float calibration[6]);
249
250 int
251 evdev_device_has_capability(struct evdev_device *device,
252                             enum libinput_device_capability capability);
253
254 int
255 evdev_device_get_size(struct evdev_device *device,
256                       double *w,
257                       double *h);
258
259 int
260 evdev_device_has_button(struct evdev_device *device, uint32_t code);
261
262 double
263 evdev_device_transform_x(struct evdev_device *device,
264                          double x,
265                          uint32_t width);
266
267 double
268 evdev_device_transform_y(struct evdev_device *device,
269                          double y,
270                          uint32_t height);
271 int
272 evdev_device_suspend(struct evdev_device *device);
273
274 int
275 evdev_device_resume(struct evdev_device *device);
276
277 void
278 evdev_notify_suspended_device(struct evdev_device *device);
279
280 void
281 evdev_notify_resumed_device(struct evdev_device *device);
282
283 void
284 evdev_keyboard_notify_key(struct evdev_device *device,
285                           uint32_t time,
286                           int key,
287                           enum libinput_key_state state);
288
289 void
290 evdev_pointer_notify_button(struct evdev_device *device,
291                             uint32_t time,
292                             int button,
293                             enum libinput_button_state state);
294
295 void
296 evdev_init_natural_scroll(struct evdev_device *device);
297
298 void
299 evdev_post_scroll(struct evdev_device *device,
300                   uint64_t time,
301                   double dx,
302                   double dy);
303
304
305 void
306 evdev_stop_scroll(struct evdev_device *device, uint64_t time);
307
308 void
309 evdev_device_remove(struct evdev_device *device);
310
311 void
312 evdev_device_destroy(struct evdev_device *device);
313
314 static inline double
315 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
316 {
317         double value = v - absinfo->minimum;
318         return value/absinfo->resolution;
319 }
320
321 int
322 evdev_init_left_handed(struct evdev_device *device,
323                        void (*change_to_left_handed)(struct evdev_device *));
324
325 static inline uint32_t
326 evdev_to_left_handed(struct evdev_device *device,
327                      uint32_t button)
328 {
329         if (device->buttons.left_handed) {
330                 if (button == BTN_LEFT)
331                         return BTN_RIGHT;
332                 else if (button == BTN_RIGHT)
333                         return BTN_LEFT;
334         }
335         return button;
336 }
337
338 #endif /* EVDEV_H */