2 * Copyright © 2013 Jonas Ådahl
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.
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.
31 #include "libinput-private.h"
34 post_event(struct libinput_device *device,
35 enum libinput_event_type type,
36 struct libinput_event *event);
39 keyboard_notify_key(struct libinput_device *device,
42 enum libinput_keyboard_key_state state)
44 struct libinput_event_keyboard_key *key_event;
46 key_event = malloc(sizeof *key_event);
50 *key_event = (struct libinput_event_keyboard_key) {
56 post_event(device, LIBINPUT_EVENT_KEYBOARD_KEY, &key_event->base);
60 pointer_notify_motion(struct libinput_device *device,
65 struct libinput_event_pointer_motion *motion_event;
67 motion_event = malloc(sizeof *motion_event);
71 *motion_event = (struct libinput_event_pointer_motion) {
77 post_event(device, LIBINPUT_EVENT_POINTER_MOTION, &motion_event->base);
81 pointer_notify_motion_absolute(struct libinput_device *device,
86 struct libinput_event_pointer_motion_absolute *motion_absolute_event;
88 motion_absolute_event = malloc(sizeof *motion_absolute_event);
89 if (!motion_absolute_event)
92 *motion_absolute_event = (struct libinput_event_pointer_motion_absolute) {
99 LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
100 &motion_absolute_event->base);
104 pointer_notify_button(struct libinput_device *device,
107 enum libinput_pointer_button_state state)
109 struct libinput_event_pointer_button *button_event;
111 button_event = malloc(sizeof *button_event);
115 *button_event = (struct libinput_event_pointer_button) {
121 post_event(device, LIBINPUT_EVENT_POINTER_BUTTON, &button_event->base);
125 pointer_notify_axis(struct libinput_device *device,
127 enum libinput_pointer_axis axis,
130 struct libinput_event_pointer_axis *axis_event;
132 axis_event = malloc(sizeof *axis_event);
136 *axis_event = (struct libinput_event_pointer_axis) {
142 post_event(device, LIBINPUT_EVENT_POINTER_AXIS, &axis_event->base);
146 touch_notify_touch(struct libinput_device *device,
151 enum libinput_touch_type touch_type)
153 struct libinput_event_touch_touch *touch_event;
155 touch_event = malloc(sizeof *touch_event);
159 *touch_event = (struct libinput_event_touch_touch) {
164 .touch_type = touch_type,
167 post_event(device, LIBINPUT_EVENT_TOUCH_TOUCH, &touch_event->base);
171 init_event_base(struct libinput_event *event, enum libinput_event_type type)
177 post_event(struct libinput_device *device,
178 enum libinput_event_type type,
179 struct libinput_event *event)
181 struct libinput_event **events = device->events;
182 size_t events_len = device->events_len;
183 size_t events_count = device->events_count;
188 if (events_count > events_len) {
193 events = realloc(events, events_len * sizeof *events);
195 fprintf(stderr, "Failed to reallocate event ring "
200 if (device->events_count > 0 && device->events_in == 0) {
201 device->events_in = device->events_len;
202 } else if (device->events_count > 0 &&
203 device->events_out >= device->events_in) {
204 move_len = device->events_len - device->events_out;
205 new_out = events_len - move_len;
206 memmove(events + new_out,
207 device->events + device->events_out,
208 move_len * sizeof *events);
209 device->events_out = new_out;
212 device->events = events;
213 device->events_len = events_len;
216 init_event_base(event, type);
218 device->events_count = events_count;
219 events[device->events_in] = event;
220 device->events_in = (device->events_in + 1) % device->events_len;
223 LIBINPUT_EXPORT struct libinput_event *
224 libinput_device_get_event(struct libinput_device *device)
226 struct libinput_event *event;
228 if (device->events_count == 0)
231 event = device->events[device->events_out];
232 device->events_out = (device->events_out + 1) % device->events_len;
233 device->events_count--;
239 libinput_device_dispatch(struct libinput_device *device)
241 return evdev_device_dispatch((struct evdev_device *) device);
245 libinput_device_destroy(struct libinput_device *device)
247 struct libinput_event *event;
249 while ((event = libinput_device_get_event(device)))
251 free(device->events);
253 evdev_device_destroy((struct evdev_device *) device);
257 libinput_device_led_update(struct libinput_device *device,
258 enum libinput_led leds)
260 evdev_device_led_update((struct evdev_device *) device, leds);
264 libinput_device_get_keys(struct libinput_device *device,
265 char *keys, size_t size)
267 return evdev_device_get_keys((struct evdev_device *) device,
273 libinput_device_calibrate(struct libinput_device *device,
274 float calibration[6])
276 evdev_device_calibrate((struct evdev_device *) device, calibration);