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);
72 libinput_seat_ref(&seat->base);
74 seat = udev_seat_create(input, seat_name);
79 /* Use non-blocking mode so that we can loop on read on
80 * evdev_device_data() until all events on the fd are
81 * read. mtdev_get() also expects this. */
82 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
84 log_info("opening input device '%s' failed (%s).\n", devnode, strerror(-fd));
88 device = evdev_device_create(&seat->base, devnode, sysname, fd);
89 if (device == EVDEV_UNHANDLED_DEVICE) {
90 close_restricted(libinput, fd);
91 log_info("not using input device '%s'.\n", devnode);
93 } else if (device == NULL) {
94 close_restricted(libinput, fd);
95 log_info("failed to create input device '%s'.\n", devnode);
100 udev_device_get_property_value(udev_device,
103 if (calibration_values && sscanf(calibration_values,
105 &device->abs.calibration[0],
106 &device->abs.calibration[1],
107 &device->abs.calibration[2],
108 &device->abs.calibration[3],
109 &device->abs.calibration[4],
110 &device->abs.calibration[5]) == 6) {
111 device->abs.apply_calibration = 1;
112 log_info("Applying calibration: %f %f %f %f %f %f\n",
113 device->abs.calibration[0],
114 device->abs.calibration[1],
115 device->abs.calibration[2],
116 device->abs.calibration[3],
117 device->abs.calibration[4],
118 device->abs.calibration[5]);
121 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
123 device->output_name = strdup(output_name);
128 libinput_seat_unref(&seat->base);
133 udev_input_add_devices(struct udev_input *input, struct udev *udev)
135 struct udev_enumerate *e;
136 struct udev_list_entry *entry;
137 struct udev_device *device;
138 const char *path, *sysname;
140 e = udev_enumerate_new(udev);
141 udev_enumerate_add_match_subsystem(e, "input");
142 udev_enumerate_scan_devices(e);
143 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
144 path = udev_list_entry_get_name(entry);
145 device = udev_device_new_from_syspath(udev, path);
147 sysname = udev_device_get_sysname(device);
148 if (strncmp("event", sysname, 5) != 0) {
149 udev_device_unref(device);
153 if (device_added(device, input) < 0) {
154 udev_device_unref(device);
155 udev_enumerate_unref(e);
159 udev_device_unref(device);
161 udev_enumerate_unref(e);
167 evdev_udev_handler(void *data)
169 struct udev_input *input = data;
170 struct libinput *libinput = &input->base;
171 struct udev_device *udev_device;
172 struct evdev_device *device, *next;
175 struct udev_seat *seat;
177 udev_device = udev_monitor_receive_device(input->udev_monitor);
181 action = udev_device_get_action(udev_device);
185 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
188 if (!strcmp(action, "add")) {
189 device_added(udev_device, input);
191 else if (!strcmp(action, "remove")) {
192 devnode = udev_device_get_devnode(udev_device);
193 list_for_each(seat, &input->base.seat_list, base.link) {
194 list_for_each_safe(device, next,
195 &seat->base.devices_list, base.link)
196 if (!strcmp(device->devnode, devnode)) {
197 log_info("input device %s, %s removed\n",
198 device->devname, device->devnode);
199 close_restricted(libinput, device->fd);
200 evdev_device_remove(device);
201 if (list_empty(&seat->base.devices_list))
202 notify_removed_seat(&seat->base);
203 libinput_seat_unref(&seat->base);
210 udev_device_unref(udev_device);
214 udev_input_remove_devices(struct udev_input *input)
216 struct evdev_device *device, *next;
217 struct udev_seat *seat, *tmp;
219 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
220 list_for_each_safe(device, next,
221 &seat->base.devices_list, base.link) {
222 close_restricted(&input->base, device->fd);
223 evdev_device_remove(device);
224 if (list_empty(&seat->base.devices_list)) {
225 notify_removed_seat(&seat->base);
226 /* seat is referenced by the event, so make
227 sure it's dropped from the seat list now,
228 to be freed whenever the device is
230 list_remove(&seat->base.link);
231 list_init(&seat->base.link);
233 libinput_seat_unref(&seat->base);
240 udev_input_disable(struct libinput *libinput)
242 struct udev_input *input = (struct udev_input*)libinput;
244 if (!input->udev_monitor)
247 udev_monitor_unref(input->udev_monitor);
248 input->udev_monitor = NULL;
249 libinput_remove_source(&input->base, input->udev_monitor_source);
250 input->udev_monitor_source = NULL;
252 udev_input_remove_devices(input);
256 udev_input_enable(struct libinput *libinput)
258 struct udev_input *input = (struct udev_input*)libinput;
259 struct udev *udev = input->udev;
262 if (input->udev_monitor)
265 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
266 if (!input->udev_monitor) {
267 log_info("udev: failed to create the udev monitor\n");
271 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
274 if (udev_monitor_enable_receiving(input->udev_monitor)) {
275 log_info("udev: failed to bind the udev monitor\n");
276 udev_monitor_unref(input->udev_monitor);
277 input->udev_monitor = NULL;
281 fd = udev_monitor_get_fd(input->udev_monitor);
282 input->udev_monitor_source = libinput_add_fd(&input->base,
286 if (!input->udev_monitor_source) {
287 udev_monitor_unref(input->udev_monitor);
288 input->udev_monitor = NULL;
292 if (udev_input_add_devices(input, udev) < 0) {
293 udev_input_disable(libinput);
301 udev_input_destroy(struct libinput *input)
303 struct udev_input *udev_input = (struct udev_input*)input;
308 udev_input_disable(input);
309 udev_unref(udev_input->udev);
310 free(udev_input->seat_id);
314 udev_seat_destroy(struct libinput_seat *seat)
316 struct udev_seat *useat = (struct udev_seat*)seat;
320 static struct udev_seat *
321 udev_seat_create(struct udev_input *input, const char *seat_name)
323 struct udev_seat *seat;
325 seat = zalloc(sizeof *seat);
329 libinput_seat_init(&seat->base, &input->base, seat_name, udev_seat_destroy);
330 list_insert(&input->base.seat_list, &seat->base.link);
331 notify_added_seat(&seat->base);
336 static struct udev_seat *
337 udev_seat_get_named(struct udev_input *input, const char *seat_name)
339 struct udev_seat *seat;
341 list_for_each(seat, &input->base.seat_list, base.link) {
342 if (strcmp(seat->base.name, seat_name) == 0)
349 static const struct libinput_interface_backend interface_backend = {
350 .resume = udev_input_enable,
351 .suspend = udev_input_disable,
352 .destroy = udev_input_destroy,
355 LIBINPUT_EXPORT struct libinput *
356 libinput_create_from_udev(const struct libinput_interface *interface,
361 struct udev_input *input;
363 if (!interface || !udev || !seat_id)
366 input = zalloc(sizeof *input);
370 if (libinput_init(&input->base, interface,
371 &interface_backend, user_data) != 0) {
376 input->udev = udev_ref(udev);
377 input->seat_id = strdup(seat_id);
379 if (udev_input_enable(&input->base) < 0) {
381 libinput_destroy(&input->base);