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 libinput_seat_unref(&seat->base);
208 udev_device_unref(udev_device);
212 udev_input_remove_devices(struct udev_input *input)
214 struct evdev_device *device, *next;
215 struct udev_seat *seat, *tmp;
217 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
218 list_for_each_safe(device, next,
219 &seat->base.devices_list, base.link) {
220 close_restricted(&input->base, device->fd);
221 evdev_device_remove(device);
222 libinput_seat_unref(&seat->base);
229 udev_input_disable(struct libinput *libinput)
231 struct udev_input *input = (struct udev_input*)libinput;
233 if (!input->udev_monitor)
236 udev_monitor_unref(input->udev_monitor);
237 input->udev_monitor = NULL;
238 libinput_remove_source(&input->base, input->udev_monitor_source);
239 input->udev_monitor_source = NULL;
241 udev_input_remove_devices(input);
245 udev_input_enable(struct libinput *libinput)
247 struct udev_input *input = (struct udev_input*)libinput;
248 struct udev *udev = input->udev;
251 if (input->udev_monitor)
254 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
255 if (!input->udev_monitor) {
256 log_info("udev: failed to create the udev monitor\n");
260 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
263 if (udev_monitor_enable_receiving(input->udev_monitor)) {
264 log_info("udev: failed to bind the udev monitor\n");
265 udev_monitor_unref(input->udev_monitor);
266 input->udev_monitor = NULL;
270 fd = udev_monitor_get_fd(input->udev_monitor);
271 input->udev_monitor_source = libinput_add_fd(&input->base,
275 if (!input->udev_monitor_source) {
276 udev_monitor_unref(input->udev_monitor);
277 input->udev_monitor = NULL;
281 if (udev_input_add_devices(input, udev) < 0) {
282 udev_input_disable(libinput);
290 udev_input_destroy(struct libinput *input)
292 struct udev_input *udev_input = (struct udev_input*)input;
297 udev_input_disable(input);
298 udev_unref(udev_input->udev);
299 free(udev_input->seat_id);
303 udev_seat_destroy(struct libinput_seat *seat)
305 struct udev_seat *useat = (struct udev_seat*)seat;
309 static struct udev_seat *
310 udev_seat_create(struct udev_input *input, const char *seat_name)
312 struct udev_seat *seat;
314 seat = zalloc(sizeof *seat);
318 libinput_seat_init(&seat->base, &input->base, seat_name, udev_seat_destroy);
319 list_insert(&input->base.seat_list, &seat->base.link);
320 notify_added_seat(&seat->base);
325 static struct udev_seat *
326 udev_seat_get_named(struct udev_input *input, const char *seat_name)
328 struct udev_seat *seat;
330 list_for_each(seat, &input->base.seat_list, base.link) {
331 if (strcmp(seat->base.name, seat_name) == 0)
338 static const struct libinput_interface_backend interface_backend = {
339 .resume = udev_input_enable,
340 .suspend = udev_input_disable,
341 .destroy = udev_input_destroy,
344 LIBINPUT_EXPORT struct libinput *
345 libinput_create_from_udev(const struct libinput_interface *interface,
350 struct udev_input *input;
352 if (!interface || !udev || !seat_id)
355 input = zalloc(sizeof *input);
359 if (libinput_init(&input->base, interface,
360 &interface_backend, user_data) != 0) {
365 input->udev = udev_ref(udev);
366 input->seat_id = strdup(seat_id);
368 if (udev_input_enable(&input->base) < 0) {
370 libinput_destroy(&input->base);