Merge branch 'master' into udev
[platform/upstream/libinput.git] / src / libinput.c
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 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/epoll.h>
29 #include <unistd.h>
30 #include <assert.h>
31
32 #include "libinput.h"
33 #include "evdev.h"
34 #include "libinput-private.h"
35
36 struct libinput_source {
37         libinput_source_dispatch_t dispatch;
38         void *user_data;
39         int fd;
40         struct list link;
41 };
42
43 struct libinput_source *
44 libinput_add_fd(struct libinput *libinput,
45                 int fd,
46                 libinput_source_dispatch_t dispatch,
47                 void *user_data)
48 {
49         struct libinput_source *source;
50         struct epoll_event ep;
51
52         source = malloc(sizeof *source);
53         if (!source)
54                 return NULL;
55
56         source->dispatch = dispatch;
57         source->user_data = user_data;
58         source->fd = fd;
59
60         memset(&ep, 0, sizeof ep);
61         ep.events = EPOLLIN;
62         ep.data.ptr = source;
63
64         if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
65                 close(source->fd);
66                 free(source);
67                 return NULL;
68         }
69
70         return source;
71 }
72
73 void
74 libinput_remove_source(struct libinput *libinput,
75                        struct libinput_source *source)
76 {
77         epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
78         close(source->fd);
79         source->fd = -1;
80         list_insert(&libinput->source_destroy_list, &source->link);
81 }
82
83 LIBINPUT_EXPORT struct libinput *
84 libinput_create(const struct libinput_interface *interface, void *user_data)
85 {
86         struct libinput *libinput;
87
88         libinput = zalloc(sizeof *libinput);
89         if (!libinput)
90                 return NULL;
91
92         list_init(&libinput->source_destroy_list);
93
94         libinput->interface = interface;
95         libinput->user_data = user_data;
96
97         libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);;
98         if (libinput->epoll_fd < 0)
99                 return NULL;
100
101         return libinput;
102 }
103
104 LIBINPUT_EXPORT void
105 libinput_destroy(struct libinput *libinput)
106 {
107         struct libinput_event *event;
108
109         while ((event = libinput_get_event(libinput)))
110                free(event);
111         free(libinput->events);
112
113         close(libinput->epoll_fd);
114         free(libinput);
115 }
116
117 LIBINPUT_EXPORT int
118 libinput_get_fd(struct libinput *libinput)
119 {
120         return libinput->epoll_fd;
121 }
122
123 LIBINPUT_EXPORT int
124 libinput_dispatch(struct libinput *libinput)
125 {
126         struct libinput_source *source, *next;
127         struct epoll_event ep[32];
128         int i, count;
129
130         count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
131         if (count < 0)
132                 return -1;
133
134         for (i = 0; i < count; ++i) {
135                 source = ep[i].data.ptr;
136                 if (source->fd == -1)
137                         continue;
138
139                 source->dispatch(source->user_data);
140         }
141
142         list_for_each_safe(source, next, &libinput->source_destroy_list, link)
143                 free(source);
144         list_init(&libinput->source_destroy_list);
145
146         return 0;
147 }
148
149 static void
150 init_event_base(struct libinput_event *event,
151                 enum libinput_event_type type,
152                 struct libinput_device *device)
153 {
154         event->type = type;
155         event->device = device;
156 }
157
158 static void
159 post_device_event(struct libinput_device *device,
160                   enum libinput_event_type type,
161                   struct libinput_event *event)
162 {
163         init_event_base(event, type, device);
164         libinput_post_event(device->libinput, event);
165 }
166
167 void
168 device_register_capability(struct libinput_device *device,
169                            enum libinput_device_capability capability)
170 {
171         struct libinput_event_device_register_capability *capability_event;
172
173         capability_event = malloc(sizeof *capability_event);
174
175         *capability_event = (struct libinput_event_device_register_capability) {
176                 .capability = capability,
177         };
178
179         post_device_event(device,
180                           LIBINPUT_EVENT_DEVICE_REGISTER_CAPABILITY,
181                           &capability_event->base);
182 }
183
184 void
185 device_unregister_capability(struct libinput_device *device,
186                              enum libinput_device_capability capability)
187 {
188         struct libinput_event_device_unregister_capability *capability_event;
189
190         capability_event = malloc(sizeof *capability_event);
191
192         *capability_event = (struct libinput_event_device_unregister_capability) {
193                 .capability = capability,
194         };
195
196         post_device_event(device,
197                           LIBINPUT_EVENT_DEVICE_UNREGISTER_CAPABILITY,
198                           &capability_event->base);
199 }
200
201 void
202 keyboard_notify_key(struct libinput_device *device,
203                     uint32_t time,
204                     uint32_t key,
205                     enum libinput_keyboard_key_state state)
206 {
207         struct libinput_event_keyboard_key *key_event;
208
209         key_event = malloc(sizeof *key_event);
210         if (!key_event)
211                 return;
212
213         *key_event = (struct libinput_event_keyboard_key) {
214                 .time = time,
215                 .key = key,
216                 .state = state,
217         };
218
219         post_device_event(device,
220                           LIBINPUT_EVENT_KEYBOARD_KEY,
221                           &key_event->base);
222 }
223
224 void
225 pointer_notify_motion(struct libinput_device *device,
226                       uint32_t time,
227                       li_fixed_t dx,
228                       li_fixed_t dy)
229 {
230         struct libinput_event_pointer_motion *motion_event;
231
232         motion_event = malloc(sizeof *motion_event);
233         if (!motion_event)
234                 return;
235
236         *motion_event = (struct libinput_event_pointer_motion) {
237                 .time = time,
238                 .dx = dx,
239                 .dy = dy,
240         };
241
242         post_device_event(device,
243                           LIBINPUT_EVENT_POINTER_MOTION,
244                           &motion_event->base);
245 }
246
247 void
248 pointer_notify_motion_absolute(struct libinput_device *device,
249                                uint32_t time,
250                                li_fixed_t x,
251                                li_fixed_t y)
252 {
253         struct libinput_event_pointer_motion_absolute *motion_absolute_event;
254
255         motion_absolute_event = malloc(sizeof *motion_absolute_event);
256         if (!motion_absolute_event)
257                 return;
258
259         *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
260                 .time = time,
261                 .x = x,
262                 .y = y,
263         };
264
265         post_device_event(device,
266                           LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
267                           &motion_absolute_event->base);
268 }
269
270 void
271 pointer_notify_button(struct libinput_device *device,
272                       uint32_t time,
273                       int32_t button,
274                       enum libinput_pointer_button_state state)
275 {
276         struct libinput_event_pointer_button *button_event;
277
278         button_event = malloc(sizeof *button_event);
279         if (!button_event)
280                 return;
281
282         *button_event = (struct libinput_event_pointer_button) {
283                 .time = time,
284                 .button = button,
285                 .state = state,
286         };
287
288         post_device_event(device,
289                           LIBINPUT_EVENT_POINTER_BUTTON,
290                           &button_event->base);
291 }
292
293 void
294 pointer_notify_axis(struct libinput_device *device,
295                     uint32_t time,
296                     enum libinput_pointer_axis axis,
297                     li_fixed_t value)
298 {
299         struct libinput_event_pointer_axis *axis_event;
300
301         axis_event = malloc(sizeof *axis_event);
302         if (!axis_event)
303                 return;
304
305         *axis_event = (struct libinput_event_pointer_axis) {
306                 .time = time,
307                 .axis = axis,
308                 .value = value,
309         };
310
311         post_device_event(device,
312                           LIBINPUT_EVENT_POINTER_AXIS,
313                           &axis_event->base);
314 }
315
316 void
317 touch_notify_touch(struct libinput_device *device,
318                    uint32_t time,
319                    int32_t slot,
320                    li_fixed_t x,
321                    li_fixed_t y,
322                    enum libinput_touch_type touch_type)
323 {
324         struct libinput_event_touch_touch *touch_event;
325
326         touch_event = malloc(sizeof *touch_event);
327         if (!touch_event)
328                 return;
329
330         *touch_event = (struct libinput_event_touch_touch) {
331                 .time = time,
332                 .slot = slot,
333                 .x = x,
334                 .y = y,
335                 .touch_type = touch_type,
336         };
337
338         post_device_event(device,
339                           LIBINPUT_EVENT_TOUCH_TOUCH,
340                           &touch_event->base);
341 }
342
343 void
344 libinput_post_event(struct libinput *libinput,
345                     struct libinput_event *event)
346 {
347         struct libinput_event **events = libinput->events;
348         size_t events_len = libinput->events_len;
349         size_t events_count = libinput->events_count;
350         size_t move_len;
351         size_t new_out;
352
353         events_count++;
354         if (events_count > events_len) {
355                 if (events_len == 0)
356                         events_len = 4;
357                 else
358                         events_len *= 2;
359                 events = realloc(events, events_len * sizeof *events);
360                 if (!events) {
361                         fprintf(stderr, "Failed to reallocate event ring "
362                                 "buffer");
363                         return;
364                 }
365
366                 if (libinput->events_count > 0 && libinput->events_in == 0) {
367                         libinput->events_in = libinput->events_len;
368                 } else if (libinput->events_count > 0 &&
369                            libinput->events_out >= libinput->events_in) {
370                         move_len = libinput->events_len - libinput->events_out;
371                         new_out = events_len - move_len;
372                         memmove(events + new_out,
373                                 libinput->events + libinput->events_out,
374                                 move_len * sizeof *events);
375                         libinput->events_out = new_out;
376                 }
377
378                 libinput->events = events;
379                 libinput->events_len = events_len;
380         }
381
382         libinput->events_count = events_count;
383         events[libinput->events_in] = event;
384         libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
385 }
386
387 LIBINPUT_EXPORT struct libinput_event *
388 libinput_get_event(struct libinput *libinput)
389 {
390         struct libinput_event *event;
391
392         if (libinput->events_count == 0)
393                 return NULL;
394
395         event = libinput->events[libinput->events_out];
396         libinput->events_out =
397                 (libinput->events_out + 1) % libinput->events_len;
398         libinput->events_count--;
399
400         return event;
401 }
402
403 LIBINPUT_EXPORT void
404 libinput_device_terminate(struct libinput_device *device)
405 {
406         evdev_device_terminate((struct evdev_device *) device);
407         device->terminated = 1;
408 }
409
410 LIBINPUT_EXPORT void
411 libinput_device_destroy(struct libinput_device *device)
412 {
413         assert(device->terminated);
414         evdev_device_destroy((struct evdev_device *) device);
415 }
416
417 LIBINPUT_EXPORT void *
418 libinput_device_get_user_data(struct libinput_device *device)
419 {
420         return device->user_data;
421 }
422
423 LIBINPUT_EXPORT void
424 libinput_device_led_update(struct libinput_device *device,
425                            enum libinput_led leds)
426 {
427         evdev_device_led_update((struct evdev_device *) device, leds);
428 }
429
430 LIBINPUT_EXPORT int
431 libinput_device_get_keys(struct libinput_device *device,
432                          char *keys, size_t size)
433 {
434         return evdev_device_get_keys((struct evdev_device *) device,
435                                      keys,
436                                      size);
437 }
438
439 LIBINPUT_EXPORT void
440 libinput_device_calibrate(struct libinput_device *device,
441                           float calibration[6])
442 {
443         evdev_device_calibrate((struct evdev_device *) device, calibration);
444 }