Port evdev code to be used as a shared library
[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 <linux/input.h>
30
31 #include "evdev.h"
32 #include "libinput-private.h"
33
34 static inline void *
35 zalloc(size_t size)
36 {
37         return calloc(1, size);
38 }
39
40 #define MAX_SLOTS 16
41
42 enum evdev_event_type {
43         EVDEV_NONE,
44         EVDEV_ABSOLUTE_TOUCH_DOWN,
45         EVDEV_ABSOLUTE_MOTION,
46         EVDEV_ABSOLUTE_TOUCH_UP,
47         EVDEV_ABSOLUTE_MT_DOWN,
48         EVDEV_ABSOLUTE_MT_MOTION,
49         EVDEV_ABSOLUTE_MT_UP,
50         EVDEV_RELATIVE_MOTION,
51 };
52
53 enum evdev_device_capability {
54         EVDEV_KEYBOARD = (1 << 0),
55         EVDEV_BUTTON = (1 << 1),
56         EVDEV_MOTION_ABS = (1 << 2),
57         EVDEV_MOTION_REL = (1 << 3),
58         EVDEV_TOUCH = (1 << 4),
59 };
60
61 enum evdev_device_seat_capability {
62         EVDEV_SEAT_POINTER = (1 << 0),
63         EVDEV_SEAT_KEYBOARD = (1 << 1),
64         EVDEV_SEAT_TOUCH = (1 << 2)
65 };
66
67 struct evdev_device {
68         struct libinput_device base;
69
70         struct evdev_dispatch *dispatch;
71         char *devnode;
72         char *devname;
73         int fd;
74         struct {
75                 int min_x, max_x, min_y, max_y;
76                 int32_t x, y;
77
78                 int apply_calibration;
79                 float calibration[6];
80         } abs;
81
82         struct {
83                 int slot;
84                 struct {
85                         int32_t x, y;
86                 } slots[MAX_SLOTS];
87         } mt;
88         struct mtdev *mtdev;
89
90         struct {
91                 li_fixed_t dx, dy;
92         } rel;
93
94         enum evdev_event_type pending_event;
95         enum evdev_device_capability caps;
96         enum evdev_device_seat_capability seat_caps;
97
98         int is_mt;
99 };
100
101 /* copied from udev/extras/input_id/input_id.c */
102 /* we must use this kernel-compatible implementation */
103 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
104 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
105 #define OFF(x)  ((x)%BITS_PER_LONG)
106 #define BIT(x)  (1UL<<OFF(x))
107 #define LONG(x) ((x)/BITS_PER_LONG)
108 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
109 /* end copied */
110
111 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
112
113 struct evdev_dispatch;
114
115 struct evdev_dispatch_interface {
116         /* Process an evdev input event. */
117         void (*process)(struct evdev_dispatch *dispatch,
118                         struct evdev_device *device,
119                         struct input_event *event,
120                         uint32_t time);
121
122         /* Destroy an event dispatch handler and free all its resources. */
123         void (*destroy)(struct evdev_dispatch *dispatch);
124 };
125
126 struct evdev_dispatch {
127         struct evdev_dispatch_interface *interface;
128 };
129
130 struct evdev_dispatch *
131 evdev_touchpad_create(struct evdev_device *device);
132
133 int
134 evdev_device_dispatch(struct evdev_device *device);
135
136 void
137 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
138
139 int
140 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
141
142 void
143 evdev_device_calibrate(struct evdev_device *device, float calibration[6]);
144
145 void
146 evdev_device_destroy(struct evdev_device *device);
147
148 #endif /* EVDEV_H */