2 * Copyright © 2013 Intel Corporation
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.
32 #include "udev-seat.h"
34 static const char default_seat[] = "seat0";
35 static const char default_seat_name[] = "default";
37 static struct udev_seat *
38 udev_seat_create(struct udev_input *input, const char *seat_name);
39 static struct udev_seat *
40 udev_seat_get_named(struct udev_input *input, const char *seat_name);
43 device_added(struct udev_device *udev_device, struct udev_input *input)
45 struct libinput *libinput = &input->base;
46 struct evdev_device *device;
49 const char *device_seat, *seat_name, *output_name;
50 const char *calibration_values;
52 struct udev_seat *seat;
54 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
56 device_seat = default_seat;
58 if (strcmp(device_seat, input->seat_id))
61 devnode = udev_device_get_devnode(udev_device);
62 sysname = udev_device_get_sysname(udev_device);
64 /* Search for matching logical seat */
65 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
67 seat_name = default_seat_name;
69 seat = udev_seat_get_named(input, seat_name);
74 /* Use non-blocking mode so that we can loop on read on
75 * evdev_device_data() until all events on the fd are
76 * read. mtdev_get() also expects this. */
77 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
79 log_info("opening input device '%s' failed (%s).\n", devnode, strerror(-fd));
83 device = evdev_device_create(&seat->base, devnode, sysname, fd);
84 if (device == EVDEV_UNHANDLED_DEVICE) {
85 close_restricted(libinput, fd);
86 log_info("not using input device '%s'.\n", devnode);
88 } else if (device == NULL) {
89 close_restricted(libinput, fd);
90 log_info("failed to create input device '%s'.\n", devnode);
95 udev_device_get_property_value(udev_device,
98 if (calibration_values && sscanf(calibration_values,
100 &device->abs.calibration[0],
101 &device->abs.calibration[1],
102 &device->abs.calibration[2],
103 &device->abs.calibration[3],
104 &device->abs.calibration[4],
105 &device->abs.calibration[5]) == 6) {
106 device->abs.apply_calibration = 1;
107 log_info("Applying calibration: %f %f %f %f %f %f\n",
108 device->abs.calibration[0],
109 device->abs.calibration[1],
110 device->abs.calibration[2],
111 device->abs.calibration[3],
112 device->abs.calibration[4],
113 device->abs.calibration[5]);
116 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
118 device->output_name = strdup(output_name);
124 udev_input_add_devices(struct udev_input *input, struct udev *udev)
126 struct udev_enumerate *e;
127 struct udev_list_entry *entry;
128 struct udev_device *device;
129 const char *path, *sysname;
131 e = udev_enumerate_new(udev);
132 udev_enumerate_add_match_subsystem(e, "input");
133 udev_enumerate_scan_devices(e);
134 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
135 path = udev_list_entry_get_name(entry);
136 device = udev_device_new_from_syspath(udev, path);
138 sysname = udev_device_get_sysname(device);
139 if (strncmp("event", sysname, 5) != 0) {
140 udev_device_unref(device);
144 if (device_added(device, input) < 0) {
145 udev_device_unref(device);
146 udev_enumerate_unref(e);
150 udev_device_unref(device);
152 udev_enumerate_unref(e);
158 evdev_udev_handler(void *data)
160 struct udev_input *input = data;
161 struct libinput *libinput = &input->base;
162 struct udev_device *udev_device;
163 struct evdev_device *device, *next;
166 struct udev_seat *seat;
168 udev_device = udev_monitor_receive_device(input->udev_monitor);
172 action = udev_device_get_action(udev_device);
176 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
179 if (!strcmp(action, "add")) {
180 device_added(udev_device, input);
182 else if (!strcmp(action, "remove")) {
183 devnode = udev_device_get_devnode(udev_device);
184 list_for_each(seat, &input->base.seat_list, base.link) {
185 list_for_each_safe(device, next,
186 &seat->base.devices_list, base.link)
187 if (!strcmp(device->devnode, devnode)) {
188 log_info("input device %s, %s removed\n",
189 device->devname, device->devnode);
190 close_restricted(libinput, device->fd);
191 evdev_device_remove(device);
198 udev_device_unref(udev_device);
202 udev_input_remove_devices(struct udev_input *input)
204 struct evdev_device *device, *next;
205 struct udev_seat *seat;
207 list_for_each(seat, &input->base.seat_list, base.link) {
208 list_for_each_safe(device, next,
209 &seat->base.devices_list, base.link) {
210 close_restricted(&input->base, device->fd);
211 evdev_device_remove(device);
218 udev_input_disable(struct libinput *libinput)
220 struct udev_input *input = (struct udev_input*)libinput;
222 if (!input->udev_monitor)
225 udev_monitor_unref(input->udev_monitor);
226 input->udev_monitor = NULL;
227 libinput_remove_source(&input->base, input->udev_monitor_source);
228 input->udev_monitor_source = NULL;
230 udev_input_remove_devices(input);
234 udev_input_enable(struct libinput *libinput)
236 struct udev_input *input = (struct udev_input*)libinput;
237 struct udev *udev = input->udev;
240 if (input->udev_monitor)
243 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
244 if (!input->udev_monitor) {
245 log_info("udev: failed to create the udev monitor\n");
249 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
252 if (udev_monitor_enable_receiving(input->udev_monitor)) {
253 log_info("udev: failed to bind the udev monitor\n");
254 udev_monitor_unref(input->udev_monitor);
255 input->udev_monitor = NULL;
259 fd = udev_monitor_get_fd(input->udev_monitor);
260 input->udev_monitor_source = libinput_add_fd(&input->base,
264 if (!input->udev_monitor_source) {
265 udev_monitor_unref(input->udev_monitor);
266 input->udev_monitor = NULL;
270 if (udev_input_add_devices(input, udev) < 0) {
271 udev_input_disable(libinput);
279 udev_input_destroy(struct libinput *input)
281 struct libinput_seat *seat, *next;
282 struct udev_input *udev_input = (struct udev_input*)input;
287 udev_input_disable(input);
288 list_for_each_safe(seat, next, &input->seat_list, link) {
289 libinput_seat_unref(seat);
291 udev_unref(udev_input->udev);
292 free(udev_input->seat_id);
296 udev_seat_destroy(struct libinput_seat *seat)
298 struct udev_seat *useat = (struct udev_seat*)seat;
302 static struct udev_seat *
303 udev_seat_create(struct udev_input *input, const char *seat_name)
305 struct udev_seat *seat;
307 seat = zalloc(sizeof *seat);
311 libinput_seat_init(&seat->base, &input->base, seat_name, udev_seat_destroy);
312 list_insert(&input->base.seat_list, &seat->base.link);
313 notify_added_seat(&seat->base);
318 static struct udev_seat *
319 udev_seat_get_named(struct udev_input *input, const char *seat_name)
321 struct udev_seat *seat;
323 list_for_each(seat, &input->base.seat_list, base.link) {
324 if (strcmp(seat->base.name, seat_name) == 0)
328 seat = udev_seat_create(input, seat_name);
336 static const struct libinput_interface_backend interface_backend = {
337 .resume = udev_input_enable,
338 .suspend = udev_input_disable,
339 .destroy = udev_input_destroy,
342 LIBINPUT_EXPORT struct libinput *
343 libinput_create_from_udev(const struct libinput_interface *interface,
348 struct udev_input *input;
350 if (!interface || !udev || !seat_id)
353 input = zalloc(sizeof *input);
357 if (libinput_init(&input->base, interface,
358 &interface_backend, user_data) != 0) {
363 input->udev = udev_ref(udev);
364 input->seat_id = strdup(seat_id);
366 if (udev_input_enable(&input->base) < 0) {
368 libinput_destroy(&input->base);