evdev: add support for LIBINPUT_MODEL_* udev tags
[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 #include "filter.h"
36
37 /* The HW DPI rate we normalize to before calculating pointer acceleration */
38 #define DEFAULT_MOUSE_DPI 1000
39
40 /*
41  * The constant (linear) acceleration factor we use to normalize trackpoint
42  * deltas before calculating pointer acceleration.
43  */
44 #define DEFAULT_TRACKPOINT_ACCEL 1.0
45
46 /* The fake resolution value for abs devices without resolution */
47 #define EVDEV_FAKE_RESOLUTION 1
48
49 enum evdev_event_type {
50         EVDEV_NONE,
51         EVDEV_ABSOLUTE_TOUCH_DOWN,
52         EVDEV_ABSOLUTE_MOTION,
53         EVDEV_ABSOLUTE_TOUCH_UP,
54         EVDEV_ABSOLUTE_MT_DOWN,
55         EVDEV_ABSOLUTE_MT_MOTION,
56         EVDEV_ABSOLUTE_MT_UP,
57         EVDEV_RELATIVE_MOTION,
58 };
59
60 enum evdev_device_seat_capability {
61         EVDEV_DEVICE_POINTER = (1 << 0),
62         EVDEV_DEVICE_KEYBOARD = (1 << 1),
63         EVDEV_DEVICE_TOUCH = (1 << 2)
64 };
65
66 enum evdev_device_tags {
67         EVDEV_TAG_EXTERNAL_MOUSE = (1 << 0),
68         EVDEV_TAG_INTERNAL_TOUCHPAD = (1 << 1),
69         EVDEV_TAG_TRACKPOINT = (1 << 2),
70         EVDEV_TAG_TOUCHPAD_TRACKPOINT = (1 << 3),
71 };
72
73 enum evdev_middlebutton_state {
74         MIDDLEBUTTON_IDLE,
75         MIDDLEBUTTON_LEFT_DOWN,
76         MIDDLEBUTTON_RIGHT_DOWN,
77         MIDDLEBUTTON_MIDDLE,
78         MIDDLEBUTTON_LEFT_UP_PENDING,
79         MIDDLEBUTTON_RIGHT_UP_PENDING,
80         MIDDLEBUTTON_IGNORE_LR,
81         MIDDLEBUTTON_IGNORE_L,
82         MIDDLEBUTTON_IGNORE_R,
83         MIDDLEBUTTON_PASSTHROUGH,
84 };
85
86 enum evdev_middlebutton_event {
87         MIDDLEBUTTON_EVENT_L_DOWN,
88         MIDDLEBUTTON_EVENT_R_DOWN,
89         MIDDLEBUTTON_EVENT_OTHER,
90         MIDDLEBUTTON_EVENT_L_UP,
91         MIDDLEBUTTON_EVENT_R_UP,
92         MIDDLEBUTTON_EVENT_TIMEOUT,
93         MIDDLEBUTTON_EVENT_ALL_UP,
94 };
95
96 enum evdev_device_model {
97         EVDEV_MODEL_DEFAULT,
98 };
99
100 struct mt_slot {
101         int32_t seat_slot;
102         struct device_coords point;
103 };
104
105 struct evdev_device {
106         struct libinput_device base;
107
108         struct libinput_source *source;
109
110         struct evdev_dispatch *dispatch;
111         struct libevdev *evdev;
112         struct udev_device *udev_device;
113         char *output_name;
114         const char *devname;
115         bool was_removed;
116         int fd;
117         struct {
118                 const struct input_absinfo *absinfo_x, *absinfo_y;
119                 int fake_resolution;
120
121                 struct device_coords point;
122                 int32_t seat_slot;
123
124                 int apply_calibration;
125                 struct matrix calibration;
126                 struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
127                 struct matrix usermatrix; /* as supplied by the caller */
128         } abs;
129
130         struct {
131                 int slot;
132                 struct mt_slot *slots;
133                 size_t slots_len;
134         } mt;
135         struct mtdev *mtdev;
136
137         struct device_coords rel;
138
139         struct {
140                 struct libinput_timer timer;
141                 struct libinput_device_config_scroll_method config;
142                 /* Currently enabled method, button */
143                 enum libinput_config_scroll_method method;
144                 uint32_t button;
145                 /* set during device init, used at runtime to delay changes
146                  * until all buttons are up */
147                 enum libinput_config_scroll_method want_method;
148                 uint32_t want_button;
149                 /* Checks if buttons are down and commits the setting */
150                 void (*change_scroll_method)(struct evdev_device *device);
151                 bool button_scroll_active;
152                 double threshold;
153                 uint32_t direction;
154                 struct normalized_coords buildup;
155
156                 struct libinput_device_config_natural_scroll config_natural;
157                 /* set during device init if we want natural scrolling,
158                  * used at runtime to enable/disable the feature */
159                 bool natural_scrolling_enabled;
160
161                 /* angle per REL_WHEEL click in degrees */
162                 int wheel_click_angle;
163         } scroll;
164
165         enum evdev_event_type pending_event;
166         enum evdev_device_seat_capability seat_caps;
167         enum evdev_device_tags tags;
168
169         int is_mt;
170         int suspended;
171
172         struct {
173                 struct libinput_device_config_accel config;
174                 struct motion_filter *filter;
175         } pointer;
176
177         /* Bitmask of pressed keys used to ignore initial release events from
178          * the kernel. */
179         unsigned long hw_key_mask[NLONGS(KEY_CNT)];
180         /* Key counter used for multiplexing button events internally in
181          * libinput. */
182         uint8_t key_count[KEY_CNT];
183
184         struct {
185                 struct libinput_device_config_left_handed config;
186                 /* left-handed currently enabled */
187                 bool enabled;
188                 /* set during device init if we want left_handed config,
189                  * used at runtime to delay the effect until buttons are up */
190                 bool want_enabled;
191                 /* Checks if buttons are down and commits the setting */
192                 void (*change_to_enabled)(struct evdev_device *device);
193         } left_handed;
194
195         struct {
196                 struct libinput_device_config_middle_emulation config;
197                 /* middle-button emulation enabled */
198                 bool enabled;
199                 bool enabled_default;
200                 bool want_enabled;
201                 enum evdev_middlebutton_state state;
202                 struct libinput_timer timer;
203                 uint32_t button_mask;
204                 uint64_t first_event_time;
205         } middlebutton;
206
207         int dpi; /* HW resolution */
208         struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
209
210         enum evdev_device_model model;
211 };
212
213 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
214
215 struct evdev_dispatch;
216
217 struct evdev_dispatch_interface {
218         /* Process an evdev input event. */
219         void (*process)(struct evdev_dispatch *dispatch,
220                         struct evdev_device *device,
221                         struct input_event *event,
222                         uint64_t time);
223
224         /* Device is being removed (may be NULL) */
225         void (*remove)(struct evdev_dispatch *dispatch);
226
227         /* Destroy an event dispatch handler and free all its resources. */
228         void (*destroy)(struct evdev_dispatch *dispatch);
229
230         /* A new device was added */
231         void (*device_added)(struct evdev_device *device,
232                              struct evdev_device *added_device);
233
234         /* A device was removed */
235         void (*device_removed)(struct evdev_device *device,
236                                struct evdev_device *removed_device);
237
238         /* A device was suspended */
239         void (*device_suspended)(struct evdev_device *device,
240                                  struct evdev_device *suspended_device);
241
242         /* A device was resumed */
243         void (*device_resumed)(struct evdev_device *device,
244                                struct evdev_device *resumed_device);
245
246         /* Tag device with one of EVDEV_TAG */
247         void (*tag_device)(struct evdev_device *device,
248                            struct udev_device *udev_device);
249 };
250
251 struct evdev_dispatch {
252         struct evdev_dispatch_interface *interface;
253         struct libinput_device_config_calibration calibration;
254
255         struct {
256                 struct libinput_device_config_send_events config;
257                 enum libinput_config_send_events_mode current_mode;
258         } sendevents;
259 };
260
261 struct evdev_device *
262 evdev_device_create(struct libinput_seat *seat,
263                     struct udev_device *device);
264
265 int
266 evdev_fix_abs_resolution(struct evdev_device *device,
267                          unsigned int xcode,
268                          unsigned int ycode,
269                          int yresolution,
270                          int xresolution);
271
272 int
273 evdev_device_init_pointer_acceleration(struct evdev_device *device,
274                                        accel_profile_func_t profile);
275
276 struct evdev_dispatch *
277 evdev_touchpad_create(struct evdev_device *device);
278
279 struct evdev_dispatch *
280 evdev_mt_touchpad_create(struct evdev_device *device);
281
282 void
283 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
284
285 int
286 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
287
288 const char *
289 evdev_device_get_output(struct evdev_device *device);
290
291 const char *
292 evdev_device_get_sysname(struct evdev_device *device);
293
294 const char *
295 evdev_device_get_name(struct evdev_device *device);
296
297 unsigned int
298 evdev_device_get_id_product(struct evdev_device *device);
299
300 unsigned int
301 evdev_device_get_id_vendor(struct evdev_device *device);
302
303 struct udev_device *
304 evdev_device_get_udev_device(struct evdev_device *device);
305
306 void
307 evdev_device_set_default_calibration(struct evdev_device *device,
308                                      const float calibration[6]);
309 void
310 evdev_device_calibrate(struct evdev_device *device,
311                        const float calibration[6]);
312
313 int
314 evdev_device_has_capability(struct evdev_device *device,
315                             enum libinput_device_capability capability);
316
317 int
318 evdev_device_get_size(struct evdev_device *device,
319                       double *w,
320                       double *h);
321
322 int
323 evdev_device_has_button(struct evdev_device *device, uint32_t code);
324
325 int
326 evdev_device_has_key(struct evdev_device *device, uint32_t code);
327
328 double
329 evdev_device_transform_x(struct evdev_device *device,
330                          double x,
331                          uint32_t width);
332
333 double
334 evdev_device_transform_y(struct evdev_device *device,
335                          double y,
336                          uint32_t height);
337 int
338 evdev_device_suspend(struct evdev_device *device);
339
340 int
341 evdev_device_resume(struct evdev_device *device);
342
343 void
344 evdev_notify_suspended_device(struct evdev_device *device);
345
346 void
347 evdev_notify_resumed_device(struct evdev_device *device);
348
349 void
350 evdev_keyboard_notify_key(struct evdev_device *device,
351                           uint32_t time,
352                           int key,
353                           enum libinput_key_state state);
354
355 void
356 evdev_pointer_notify_button(struct evdev_device *device,
357                             uint32_t time,
358                             int button,
359                             enum libinput_button_state state);
360 void
361 evdev_pointer_notify_physical_button(struct evdev_device *device,
362                                      uint32_t time,
363                                      int button,
364                                      enum libinput_button_state state);
365
366 void
367 evdev_init_natural_scroll(struct evdev_device *device);
368
369 void
370 evdev_post_scroll(struct evdev_device *device,
371                   uint64_t time,
372                   enum libinput_pointer_axis_source source,
373                   const struct normalized_coords *delta);
374
375 void
376 evdev_stop_scroll(struct evdev_device *device,
377                   uint64_t time,
378                   enum libinput_pointer_axis_source source);
379
380 void
381 evdev_device_remove(struct evdev_device *device);
382
383 void
384 evdev_device_destroy(struct evdev_device *device);
385
386 bool
387 evdev_middlebutton_filter_button(struct evdev_device *device,
388                                  uint64_t time,
389                                  int button,
390                                  enum libinput_button_state state);
391
392 void
393 evdev_init_middlebutton(struct evdev_device *device,
394                         bool enabled,
395                         bool want_config);
396
397 static inline double
398 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
399 {
400         double value = v - absinfo->minimum;
401         return value/absinfo->resolution;
402 }
403
404 int
405 evdev_init_left_handed(struct evdev_device *device,
406                        void (*change_to_left_handed)(struct evdev_device *));
407
408 static inline uint32_t
409 evdev_to_left_handed(struct evdev_device *device,
410                      uint32_t button)
411 {
412         if (device->left_handed.enabled) {
413                 if (button == BTN_LEFT)
414                         return BTN_RIGHT;
415                 else if (button == BTN_RIGHT)
416                         return BTN_LEFT;
417         }
418         return button;
419 }
420
421 #endif /* EVDEV_H */