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;
48 const char *device_seat, *seat_name, *output_name;
49 const char *calibration_values;
51 struct udev_seat *seat;
53 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
55 device_seat = default_seat;
57 if (strcmp(device_seat, input->seat_id))
60 devnode = udev_device_get_devnode(udev_device);
62 /* Search for matching logical seat */
63 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
65 seat_name = default_seat_name;
67 seat = udev_seat_get_named(input, seat_name);
72 /* Use non-blocking mode so that we can loop on read on
73 * evdev_device_data() until all events on the fd are
74 * read. mtdev_get() also expects this. */
75 fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
77 log_info("opening input device '%s' failed.\n", devnode);
81 device = evdev_device_create(&seat->base, devnode, fd);
82 if (device == EVDEV_UNHANDLED_DEVICE) {
83 close_restricted(libinput, fd);
84 log_info("not using input device '%s'.\n", devnode);
86 } else if (device == NULL) {
87 close_restricted(libinput, fd);
88 log_info("failed to create input device '%s'.\n", devnode);
93 udev_device_get_property_value(udev_device,
96 if (calibration_values && sscanf(calibration_values,
98 &device->abs.calibration[0],
99 &device->abs.calibration[1],
100 &device->abs.calibration[2],
101 &device->abs.calibration[3],
102 &device->abs.calibration[4],
103 &device->abs.calibration[5]) == 6) {
104 device->abs.apply_calibration = 1;
105 log_info("Applying calibration: %f %f %f %f %f %f\n",
106 device->abs.calibration[0],
107 device->abs.calibration[1],
108 device->abs.calibration[2],
109 device->abs.calibration[3],
110 device->abs.calibration[4],
111 device->abs.calibration[5]);
114 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
116 device->output_name = strdup(output_name);
122 udev_input_add_devices(struct udev_input *input, struct udev *udev)
124 struct udev_enumerate *e;
125 struct udev_list_entry *entry;
126 struct udev_device *device;
127 const char *path, *sysname;
129 e = udev_enumerate_new(udev);
130 udev_enumerate_add_match_subsystem(e, "input");
131 udev_enumerate_scan_devices(e);
132 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
133 path = udev_list_entry_get_name(entry);
134 device = udev_device_new_from_syspath(udev, path);
136 sysname = udev_device_get_sysname(device);
137 if (strncmp("event", sysname, 5) != 0) {
138 udev_device_unref(device);
142 if (device_added(device, input) < 0) {
143 udev_device_unref(device);
144 udev_enumerate_unref(e);
148 udev_device_unref(device);
150 udev_enumerate_unref(e);
156 evdev_udev_handler(void *data)
158 struct udev_input *input = data;
159 struct libinput *libinput = &input->base;
160 struct udev_device *udev_device;
161 struct evdev_device *device, *next;
164 struct udev_seat *seat;
166 udev_device = udev_monitor_receive_device(input->udev_monitor);
170 action = udev_device_get_action(udev_device);
174 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
177 if (!strcmp(action, "add")) {
178 device_added(udev_device, input);
180 else if (!strcmp(action, "remove")) {
181 devnode = udev_device_get_devnode(udev_device);
182 list_for_each(seat, &input->base.seat_list, base.link) {
183 list_for_each_safe(device, next,
184 &seat->base.devices_list, base.link)
185 if (!strcmp(device->devnode, devnode)) {
186 log_info("input device %s, %s removed\n",
187 device->devname, device->devnode);
188 close_restricted(libinput, device->fd);
189 evdev_device_remove(device);
196 udev_device_unref(udev_device);
200 udev_input_enable(struct udev_input *input)
202 struct udev *udev = input->udev;
205 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
206 if (!input->udev_monitor) {
207 log_info("udev: failed to create the udev monitor\n");
211 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
214 if (udev_monitor_enable_receiving(input->udev_monitor)) {
215 log_info("udev: failed to bind the udev monitor\n");
216 udev_monitor_unref(input->udev_monitor);
220 fd = udev_monitor_get_fd(input->udev_monitor);
221 input->udev_monitor_source = libinput_add_fd(&input->base,
225 if (!input->udev_monitor_source) {
226 udev_monitor_unref(input->udev_monitor);
230 if (udev_input_add_devices(input, udev) < 0)
237 udev_input_remove_devices(struct udev_input *input)
239 struct evdev_device *device, *next;
240 struct udev_seat *seat;
242 list_for_each(seat, &input->base.seat_list, base.link) {
243 list_for_each_safe(device, next,
244 &seat->base.devices_list, base.link) {
245 close_restricted(&input->base, device->fd);
246 evdev_device_remove(device);
252 udev_input_disable(struct udev_input *input)
254 if (!input->udev_monitor)
257 udev_monitor_unref(input->udev_monitor);
258 input->udev_monitor = NULL;
259 libinput_remove_source(&input->base, input->udev_monitor_source);
260 input->udev_monitor_source = NULL;
262 udev_input_remove_devices(input);
266 udev_input_destroy(struct udev_input *input)
268 struct libinput_seat *seat, *next;
269 udev_input_disable(input);
270 list_for_each_safe(seat, next, &input->base.seat_list, link) {
271 notify_removed_seat(seat);
272 libinput_seat_unref(seat);
274 udev_unref(input->udev);
275 free(input->seat_id);
278 static struct udev_seat *
279 udev_seat_create(struct udev_input *input, const char *seat_name)
281 struct udev_seat *seat;
283 seat = zalloc(sizeof *seat);
287 libinput_seat_init(&seat->base, &input->base, seat_name);
288 list_insert(&input->base.seat_list, &seat->base.link);
289 notify_added_seat(&seat->base);
295 udev_seat_destroy(struct udev_seat *seat)
297 list_remove(&seat->base.link);
301 static struct udev_seat *
302 udev_seat_get_named(struct udev_input *input, const char *seat_name)
304 struct udev_seat *seat;
306 list_for_each(seat, &input->base.seat_list, base.link) {
307 if (strcmp(seat->base.name, seat_name) == 0)
311 seat = udev_seat_create(input, seat_name);
319 LIBINPUT_EXPORT struct libinput *
320 libinput_create_udev(const struct libinput_interface *interface,
325 struct udev_input *input;
327 input = zalloc(sizeof *input);
331 if (libinput_init(&input->base, interface, user_data) != 0) {
336 input->udev = udev_ref(udev);
337 input->seat_id = strdup(seat_id);
339 if (udev_input_enable(input) < 0) {
341 libinput_destroy(&input->base);