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,
39 const char *device_seat,
40 const char *seat_name);
41 static struct udev_seat *
42 udev_seat_get_named(struct udev_input *input, const char *seat_name);
45 device_added(struct udev_device *udev_device, struct udev_input *input)
47 struct evdev_device *device;
50 const char *device_seat, *seat_name, *output_name;
51 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, device_seat, seat_name);
79 device = evdev_device_create(&seat->base, devnode, sysname);
80 libinput_seat_unref(&seat->base);
82 if (device == EVDEV_UNHANDLED_DEVICE) {
83 log_info("not using input device '%s'.\n", devnode);
85 } else if (device == NULL) {
86 log_info("failed to create input device '%s'.\n", devnode);
91 udev_device_get_property_value(udev_device,
94 if (calibration_values && sscanf(calibration_values,
96 &device->abs.calibration[0],
97 &device->abs.calibration[1],
98 &device->abs.calibration[2],
99 &device->abs.calibration[3],
100 &device->abs.calibration[4],
101 &device->abs.calibration[5]) == 6) {
102 device->abs.apply_calibration = 1;
103 log_info("Applying calibration: %f %f %f %f %f %f\n",
104 device->abs.calibration[0],
105 device->abs.calibration[1],
106 device->abs.calibration[2],
107 device->abs.calibration[3],
108 device->abs.calibration[4],
109 device->abs.calibration[5]);
112 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
114 device->output_name = strdup(output_name);
120 device_removed(struct udev_device *udev_device, struct udev_input *input)
123 struct evdev_device *device, *next;
124 struct udev_seat *seat;
126 devnode = udev_device_get_devnode(udev_device);
127 list_for_each(seat, &input->base.seat_list, base.link) {
128 list_for_each_safe(device, next,
129 &seat->base.devices_list, base.link) {
130 if (!strcmp(device->devnode, devnode)) {
131 log_info("input device %s, %s removed\n",
132 device->devname, device->devnode);
133 evdev_device_remove(device);
141 udev_input_add_devices(struct udev_input *input, struct udev *udev)
143 struct udev_enumerate *e;
144 struct udev_list_entry *entry;
145 struct udev_device *device;
146 const char *path, *sysname;
148 e = udev_enumerate_new(udev);
149 udev_enumerate_add_match_subsystem(e, "input");
150 udev_enumerate_scan_devices(e);
151 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
152 path = udev_list_entry_get_name(entry);
153 device = udev_device_new_from_syspath(udev, path);
155 sysname = udev_device_get_sysname(device);
156 if (strncmp("event", sysname, 5) != 0) {
157 udev_device_unref(device);
161 if (device_added(device, input) < 0) {
162 udev_device_unref(device);
163 udev_enumerate_unref(e);
167 udev_device_unref(device);
169 udev_enumerate_unref(e);
175 evdev_udev_handler(void *data)
177 struct udev_input *input = data;
178 struct udev_device *udev_device;
181 udev_device = udev_monitor_receive_device(input->udev_monitor);
185 action = udev_device_get_action(udev_device);
189 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
192 if (!strcmp(action, "add"))
193 device_added(udev_device, input);
194 else if (!strcmp(action, "remove"))
195 device_removed(udev_device, input);
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, *tmp;
207 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
208 libinput_seat_ref(&seat->base);
209 list_for_each_safe(device, next,
210 &seat->base.devices_list, base.link) {
211 evdev_device_remove(device);
213 libinput_seat_unref(&seat->base);
219 udev_input_disable(struct libinput *libinput)
221 struct udev_input *input = (struct udev_input*)libinput;
223 if (!input->udev_monitor)
226 udev_monitor_unref(input->udev_monitor);
227 input->udev_monitor = NULL;
228 libinput_remove_source(&input->base, input->udev_monitor_source);
229 input->udev_monitor_source = NULL;
231 udev_input_remove_devices(input);
235 udev_input_enable(struct libinput *libinput)
237 struct udev_input *input = (struct udev_input*)libinput;
238 struct udev *udev = input->udev;
241 if (input->udev_monitor)
244 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
245 if (!input->udev_monitor) {
246 log_info("udev: failed to create the udev monitor\n");
250 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
253 if (udev_monitor_enable_receiving(input->udev_monitor)) {
254 log_info("udev: failed to bind the udev monitor\n");
255 udev_monitor_unref(input->udev_monitor);
256 input->udev_monitor = NULL;
260 fd = udev_monitor_get_fd(input->udev_monitor);
261 input->udev_monitor_source = libinput_add_fd(&input->base,
265 if (!input->udev_monitor_source) {
266 udev_monitor_unref(input->udev_monitor);
267 input->udev_monitor = NULL;
271 if (udev_input_add_devices(input, udev) < 0) {
272 udev_input_disable(libinput);
280 udev_input_destroy(struct libinput *input)
282 struct udev_input *udev_input = (struct udev_input*)input;
287 udev_unref(udev_input->udev);
288 free(udev_input->seat_id);
292 udev_seat_destroy(struct libinput_seat *seat)
294 struct udev_seat *useat = (struct udev_seat*)seat;
298 static struct udev_seat *
299 udev_seat_create(struct udev_input *input,
300 const char *device_seat,
301 const char *seat_name)
303 struct udev_seat *seat;
305 seat = zalloc(sizeof *seat);
309 libinput_seat_init(&seat->base, &input->base,
310 device_seat, seat_name,
316 static struct udev_seat *
317 udev_seat_get_named(struct udev_input *input, const char *seat_name)
319 struct udev_seat *seat;
321 list_for_each(seat, &input->base.seat_list, base.link) {
322 if (strcmp(seat->base.logical_name, seat_name) == 0)
329 static const struct libinput_interface_backend interface_backend = {
330 .resume = udev_input_enable,
331 .suspend = udev_input_disable,
332 .destroy = udev_input_destroy,
335 LIBINPUT_EXPORT struct libinput *
336 libinput_udev_create_for_seat(const struct libinput_interface *interface,
341 struct udev_input *input;
343 if (!interface || !udev || !seat_id)
346 input = zalloc(sizeof *input);
350 if (libinput_init(&input->base, interface,
351 &interface_backend, user_data) != 0) {
356 input->udev = udev_ref(udev);
357 input->seat_id = strdup(seat_id);
359 if (udev_input_enable(&input->base) < 0) {
361 libinput_destroy(&input->base);