evdev: add a internal device notification mechanism
[platform/upstream/libinput.git] / src / libinput-private.h
1 /*
2  * Copyright © 2013 Jonas Ådahl
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 #ifndef LIBINPUT_PRIVATE_H
24 #define LIBINPUT_PRIVATE_H
25
26 #include <errno.h>
27
28 #include "linux/input.h"
29
30 #include "libinput.h"
31 #include "libinput-util.h"
32
33 struct libinput_source;
34
35 struct libinput_interface_backend {
36         int (*resume)(struct libinput *libinput);
37         void (*suspend)(struct libinput *libinput);
38         void (*destroy)(struct libinput *libinput);
39 };
40
41 struct libinput {
42         int epoll_fd;
43         struct list source_destroy_list;
44
45         struct list seat_list;
46
47         struct {
48                 struct list list;
49                 struct libinput_source *source;
50                 int fd;
51         } timer;
52
53         struct libinput_event **events;
54         size_t events_count;
55         size_t events_len;
56         size_t events_in;
57         size_t events_out;
58
59         const struct libinput_interface *interface;
60         const struct libinput_interface_backend *interface_backend;
61
62         libinput_log_handler log_handler;
63         enum libinput_log_priority log_priority;
64         void *user_data;
65         int refcount;
66 };
67
68 typedef void (*libinput_seat_destroy_func) (struct libinput_seat *seat);
69
70 struct libinput_seat {
71         struct libinput *libinput;
72         struct list link;
73         struct list devices_list;
74         void *user_data;
75         int refcount;
76         libinput_seat_destroy_func destroy;
77
78         char *physical_name;
79         char *logical_name;
80
81         uint32_t slot_map;
82
83         uint32_t button_count[KEY_CNT];
84 };
85
86 struct libinput_device_config_tap {
87         int (*count)(struct libinput_device *device);
88         enum libinput_config_status (*set_enabled)(struct libinput_device *device,
89                                                    enum libinput_config_tap_state enable);
90         enum libinput_config_tap_state (*get_enabled)(struct libinput_device *device);
91         enum libinput_config_tap_state (*get_default)(struct libinput_device *device);
92 };
93
94 struct libinput_device_config_calibration {
95         int (*has_matrix)(struct libinput_device *device);
96         enum libinput_config_status (*set_matrix)(struct libinput_device *device,
97                                                   const float matrix[6]);
98         int (*get_matrix)(struct libinput_device *device,
99                           float matrix[6]);
100         int (*get_default_matrix)(struct libinput_device *device,
101                                                           float matrix[6]);
102 };
103
104 struct libinput_device_config_send_events {
105         uint32_t (*get_modes)(struct libinput_device *device);
106         enum libinput_config_status (*set_mode)(struct libinput_device *device,
107                                                    enum libinput_config_send_events_mode mode);
108         enum libinput_config_send_events_mode (*get_mode)(struct libinput_device *device);
109         enum libinput_config_send_events_mode (*get_default_mode)(struct libinput_device *device);
110 };
111
112 struct libinput_device_config {
113         struct libinput_device_config_tap *tap;
114         struct libinput_device_config_calibration *calibration;
115         struct libinput_device_config_send_events *sendevents;
116 };
117
118 struct libinput_device {
119         struct libinput_seat *seat;
120         struct list link;
121         void *user_data;
122         int terminated;
123         int refcount;
124         struct libinput_device_config config;
125 };
126
127 typedef void (*libinput_source_dispatch_t)(void *data);
128
129
130 #define log_debug(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
131 #define log_info(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
132 #define log_error(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
133 #define log_bug_kernel(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
134 #define log_bug_libinput(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__);
135 #define log_bug_client(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__);
136
137 void
138 log_msg(struct libinput *libinput,
139         enum libinput_log_priority priority,
140         const char *format, ...);
141
142 void
143 log_msg_va(struct libinput *libinput,
144            enum libinput_log_priority priority,
145            const char *format,
146            va_list args);
147
148 int
149 libinput_init(struct libinput *libinput,
150               const struct libinput_interface *interface,
151               const struct libinput_interface_backend *interface_backend,
152               void *user_data);
153
154 struct libinput_source *
155 libinput_add_fd(struct libinput *libinput,
156                 int fd,
157                 libinput_source_dispatch_t dispatch,
158                 void *data);
159
160 void
161 libinput_remove_source(struct libinput *libinput,
162                        struct libinput_source *source);
163
164 int
165 open_restricted(struct libinput *libinput,
166                 const char *path, int flags);
167
168 void
169 close_restricted(struct libinput *libinput, int fd);
170
171 void
172 libinput_seat_init(struct libinput_seat *seat,
173                    struct libinput *libinput,
174                    const char *physical_name,
175                    const char *logical_name,
176                    libinput_seat_destroy_func destroy);
177
178 void
179 libinput_device_init(struct libinput_device *device,
180                      struct libinput_seat *seat);
181
182 void
183 notify_added_device(struct libinput_device *device);
184
185 void
186 notify_removed_device(struct libinput_device *device);
187
188 void
189 keyboard_notify_key(struct libinput_device *device,
190                     uint32_t time,
191                     uint32_t key,
192                     enum libinput_key_state state);
193
194 void
195 pointer_notify_motion(struct libinput_device *device,
196                       uint32_t time,
197                       double dx,
198                       double dy);
199
200 void
201 pointer_notify_motion_absolute(struct libinput_device *device,
202                                uint32_t time,
203                                double x,
204                                double y);
205
206 void
207 pointer_notify_button(struct libinput_device *device,
208                       uint32_t time,
209                       int32_t button,
210                       enum libinput_button_state state);
211
212 void
213 pointer_notify_axis(struct libinput_device *device,
214                     uint32_t time,
215                     enum libinput_pointer_axis axis,
216                     double value);
217
218 void
219 touch_notify_touch_down(struct libinput_device *device,
220                         uint32_t time,
221                         int32_t slot,
222                         int32_t seat_slot,
223                         double x,
224                         double y);
225
226 void
227 touch_notify_touch_motion(struct libinput_device *device,
228                           uint32_t time,
229                           int32_t slot,
230                           int32_t seat_slot,
231                           double x,
232                           double y);
233
234 void
235 touch_notify_touch_up(struct libinput_device *device,
236                       uint32_t time,
237                       int32_t slot,
238                       int32_t seat_slot);
239
240 void
241 touch_notify_frame(struct libinput_device *device,
242                    uint32_t time);
243
244 static inline uint64_t
245 libinput_now(struct libinput *libinput)
246 {
247         struct timespec ts = { 0, 0 };
248
249         if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
250                 log_error(libinput, "clock_gettime failed: %s\n", strerror(errno));
251                 return 0;
252         }
253
254         return ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
255 }
256 #endif /* LIBINPUT_PRIVATE_H */