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_enable(struct udev_input *input)
204 struct udev *udev = input->udev;
207 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
208 if (!input->udev_monitor) {
209 log_info("udev: failed to create the udev monitor\n");
213 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
216 if (udev_monitor_enable_receiving(input->udev_monitor)) {
217 log_info("udev: failed to bind the udev monitor\n");
218 udev_monitor_unref(input->udev_monitor);
222 fd = udev_monitor_get_fd(input->udev_monitor);
223 input->udev_monitor_source = libinput_add_fd(&input->base,
227 if (!input->udev_monitor_source) {
228 udev_monitor_unref(input->udev_monitor);
232 if (udev_input_add_devices(input, udev) < 0) {
233 udev_input_disable(input);
241 udev_input_remove_devices(struct udev_input *input)
243 struct evdev_device *device, *next;
244 struct udev_seat *seat;
246 list_for_each(seat, &input->base.seat_list, base.link) {
247 list_for_each_safe(device, next,
248 &seat->base.devices_list, base.link) {
249 close_restricted(&input->base, device->fd);
250 evdev_device_remove(device);
256 udev_input_disable(struct udev_input *input)
258 if (!input->udev_monitor)
261 udev_monitor_unref(input->udev_monitor);
262 input->udev_monitor = NULL;
263 libinput_remove_source(&input->base, input->udev_monitor_source);
264 input->udev_monitor_source = NULL;
266 udev_input_remove_devices(input);
270 udev_input_destroy(struct udev_input *input)
272 struct libinput_seat *seat, *next;
277 udev_input_disable(input);
278 list_for_each_safe(seat, next, &input->base.seat_list, link) {
279 libinput_seat_unref(seat);
281 udev_unref(input->udev);
282 free(input->seat_id);
285 static struct udev_seat *
286 udev_seat_create(struct udev_input *input, const char *seat_name)
288 struct udev_seat *seat;
290 seat = zalloc(sizeof *seat);
294 libinput_seat_init(&seat->base, &input->base, seat_name);
295 list_insert(&input->base.seat_list, &seat->base.link);
296 notify_added_seat(&seat->base);
302 udev_seat_destroy(struct udev_seat *seat)
304 list_remove(&seat->base.link);
308 static struct udev_seat *
309 udev_seat_get_named(struct udev_input *input, const char *seat_name)
311 struct udev_seat *seat;
313 list_for_each(seat, &input->base.seat_list, base.link) {
314 if (strcmp(seat->base.name, seat_name) == 0)
318 seat = udev_seat_create(input, seat_name);
326 LIBINPUT_EXPORT struct libinput *
327 libinput_create_from_udev(const struct libinput_interface *interface,
332 struct udev_input *input;
334 if (!interface || !udev || !seat_id)
337 input = zalloc(sizeof *input);
341 if (libinput_init(&input->base, interface, user_data) != 0) {
346 input->udev = udev_ref(udev);
347 input->seat_id = strdup(seat_id);
349 if (udev_input_enable(input) < 0) {
351 libinput_destroy(&input->base);