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;
51 const char *device_seat, *seat_name, *output_name;
52 const char *calibration_values;
54 struct udev_seat *seat;
56 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
58 device_seat = default_seat;
60 if (strcmp(device_seat, input->seat_id))
63 devnode = udev_device_get_devnode(udev_device);
64 sysname = udev_device_get_sysname(udev_device);
65 syspath = udev_device_get_syspath(udev_device);
67 /* Search for matching logical seat */
68 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
70 seat_name = default_seat_name;
72 seat = udev_seat_get_named(input, seat_name);
75 libinput_seat_ref(&seat->base);
77 seat = udev_seat_create(input, device_seat, seat_name);
82 device = evdev_device_create(&seat->base, devnode, sysname, syspath);
83 libinput_seat_unref(&seat->base);
85 if (device == EVDEV_UNHANDLED_DEVICE) {
86 log_info(&input->base, "not using input device '%s'.\n", devnode);
88 } else if (device == NULL) {
89 log_info(&input->base, "failed to create input device '%s'.\n", devnode);
94 udev_device_get_property_value(udev_device,
95 "LIBINPUT_CALIBRATION_MATRIX");
97 if (calibration_values && sscanf(calibration_values,
104 &calibration[5]) == 6) {
105 evdev_device_set_default_calibration(device, calibration);
106 log_info(&input->base,
107 "Applying calibration: %f %f %f %f %f %f\n",
116 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
118 device->output_name = strdup(output_name);
124 device_removed(struct udev_device *udev_device, struct udev_input *input)
127 struct evdev_device *device, *next;
128 struct udev_seat *seat;
130 devnode = udev_device_get_devnode(udev_device);
131 list_for_each(seat, &input->base.seat_list, base.link) {
132 list_for_each_safe(device, next,
133 &seat->base.devices_list, base.link) {
134 if (!strcmp(device->devnode, devnode)) {
135 log_info(&input->base,
136 "input device %s, %s removed\n",
137 device->devname, device->devnode);
138 evdev_device_remove(device);
146 udev_input_add_devices(struct udev_input *input, struct udev *udev)
148 struct udev_enumerate *e;
149 struct udev_list_entry *entry;
150 struct udev_device *device;
151 const char *path, *sysname;
153 e = udev_enumerate_new(udev);
154 udev_enumerate_add_match_subsystem(e, "input");
155 udev_enumerate_scan_devices(e);
156 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
157 path = udev_list_entry_get_name(entry);
158 device = udev_device_new_from_syspath(udev, path);
160 sysname = udev_device_get_sysname(device);
161 if (strncmp("event", sysname, 5) != 0) {
162 udev_device_unref(device);
166 if (device_added(device, input) < 0) {
167 udev_device_unref(device);
168 udev_enumerate_unref(e);
172 udev_device_unref(device);
174 udev_enumerate_unref(e);
180 evdev_udev_handler(void *data)
182 struct udev_input *input = data;
183 struct udev_device *udev_device;
186 udev_device = udev_monitor_receive_device(input->udev_monitor);
190 action = udev_device_get_action(udev_device);
194 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
197 if (!strcmp(action, "add"))
198 device_added(udev_device, input);
199 else if (!strcmp(action, "remove"))
200 device_removed(udev_device, input);
203 udev_device_unref(udev_device);
207 udev_input_remove_devices(struct udev_input *input)
209 struct evdev_device *device, *next;
210 struct udev_seat *seat, *tmp;
212 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
213 libinput_seat_ref(&seat->base);
214 list_for_each_safe(device, next,
215 &seat->base.devices_list, base.link) {
216 evdev_device_remove(device);
218 libinput_seat_unref(&seat->base);
223 udev_input_disable(struct libinput *libinput)
225 struct udev_input *input = (struct udev_input*)libinput;
227 if (!input->udev_monitor)
230 udev_monitor_unref(input->udev_monitor);
231 input->udev_monitor = NULL;
232 libinput_remove_source(&input->base, input->udev_monitor_source);
233 input->udev_monitor_source = NULL;
235 udev_input_remove_devices(input);
239 udev_input_enable(struct libinput *libinput)
241 struct udev_input *input = (struct udev_input*)libinput;
242 struct udev *udev = input->udev;
245 if (input->udev_monitor)
248 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
249 if (!input->udev_monitor) {
251 "udev: failed to create the udev monitor\n");
255 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
258 if (udev_monitor_enable_receiving(input->udev_monitor)) {
259 log_info(libinput, "udev: failed to bind the udev monitor\n");
260 udev_monitor_unref(input->udev_monitor);
261 input->udev_monitor = NULL;
265 fd = udev_monitor_get_fd(input->udev_monitor);
266 input->udev_monitor_source = libinput_add_fd(&input->base,
270 if (!input->udev_monitor_source) {
271 udev_monitor_unref(input->udev_monitor);
272 input->udev_monitor = NULL;
276 if (udev_input_add_devices(input, udev) < 0) {
277 udev_input_disable(libinput);
285 udev_input_destroy(struct libinput *input)
287 struct udev_input *udev_input = (struct udev_input*)input;
292 udev_unref(udev_input->udev);
293 free(udev_input->seat_id);
297 udev_seat_destroy(struct libinput_seat *seat)
299 struct udev_seat *useat = (struct udev_seat*)seat;
303 static struct udev_seat *
304 udev_seat_create(struct udev_input *input,
305 const char *device_seat,
306 const char *seat_name)
308 struct udev_seat *seat;
310 seat = zalloc(sizeof *seat);
314 libinput_seat_init(&seat->base, &input->base,
315 device_seat, seat_name,
321 static struct udev_seat *
322 udev_seat_get_named(struct udev_input *input, const char *seat_name)
324 struct udev_seat *seat;
326 list_for_each(seat, &input->base.seat_list, base.link) {
327 if (strcmp(seat->base.logical_name, seat_name) == 0)
334 static const struct libinput_interface_backend interface_backend = {
335 .resume = udev_input_enable,
336 .suspend = udev_input_disable,
337 .destroy = udev_input_destroy,
340 LIBINPUT_EXPORT struct libinput *
341 libinput_udev_create_context(const struct libinput_interface *interface,
345 struct udev_input *input;
347 if (!interface || !udev)
350 input = zalloc(sizeof *input);
354 if (libinput_init(&input->base, interface,
355 &interface_backend, user_data) != 0) {
356 libinput_unref(&input->base);
361 input->udev = udev_ref(udev);
367 libinput_udev_assign_seat(struct libinput *libinput,
370 struct udev_input *input = (struct udev_input*)libinput;
374 if (input->seat_id != NULL)
377 if (libinput->interface_backend != &interface_backend) {
378 log_bug_client(libinput, "Mismatching backends.\n");
382 input->seat_id = strdup(seat_id);
384 if (udev_input_enable(&input->base) < 0)