1 /*
2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2013-2015 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "evdev.h"
32 #include "udev-seat.h"
33
34 static const char default_seat[] = "seat0";
35 static const char default_seat_name[] = "default";
36
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);
43
44
45 static inline bool
filter_duplicates(struct udev_seat * udev_seat,struct udev_device * udev_device)46 filter_duplicates(struct udev_seat *udev_seat,
47 struct udev_device *udev_device)
48 {
49 struct libinput_device *device;
50 const char *new_syspath = udev_device_get_syspath(udev_device);
51 bool ignore_device = false;
52
53 if (!udev_seat)
54 return false;
55
56 list_for_each(device, &udev_seat->base.devices_list, link) {
57 const char *syspath;
58 struct udev_device *ud;
59
60 ud = libinput_device_get_udev_device(device);
61 if (!ud)
62 continue;
63
64 syspath = udev_device_get_syspath(ud);
65 if (syspath && new_syspath && streq(syspath, new_syspath))
66 ignore_device = true;
67 udev_device_unref(ud);
68
69 if (ignore_device)
70 break;
71 }
72
73 return ignore_device;
74 }
75
76 static int
device_added(struct udev_device * udev_device,struct udev_input * input,const char * seat_name)77 device_added(struct udev_device *udev_device,
78 struct udev_input *input,
79 const char *seat_name)
80 {
81 struct evdev_device *device;
82 const char *devnode, *sysname;
83 const char *device_seat, *output_name;
84 struct udev_seat *seat;
85
86 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
87 if (!device_seat)
88 device_seat = default_seat;
89
90 if (!streq(device_seat, input->seat_id))
91 return 0;
92
93 if (ignore_litest_test_suite_device(udev_device))
94 return 0;
95
96 devnode = udev_device_get_devnode(udev_device);
97 sysname = udev_device_get_sysname(udev_device);
98
99 /* Search for matching logical seat */
100 if (!seat_name)
101 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
102 if (!seat_name)
103 seat_name = default_seat_name;
104
105 seat = udev_seat_get_named(input, seat_name);
106
107 /* There is a race at startup: a device added between setting
108 * up the udev monitor and enumerating all current devices may show
109 * up in both lists. Filter those out.
110 */
111 if (filter_duplicates(seat, udev_device))
112 return 0;
113
114 if (seat)
115 libinput_seat_ref(&seat->base);
116 else {
117 seat = udev_seat_create(input, device_seat, seat_name);
118 if (!seat)
119 return -1;
120 }
121
122 device = evdev_device_create(&seat->base, udev_device);
123 libinput_seat_unref(&seat->base);
124
125 if (device == EVDEV_UNHANDLED_DEVICE) {
126 log_info(&input->base,
127 "%-7s - not using input device '%s'\n",
128 sysname,
129 devnode);
130 return 0;
131 }
132
133 if (device == NULL) {
134 log_info(&input->base,
135 "%-7s - failed to create input device '%s'\n",
136 sysname,
137 devnode);
138 return 0;
139 }
140
141 evdev_read_calibration_prop(device);
142
143 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
144 device->output_name = safe_strdup(output_name);
145
146 return 0;
147 }
148
149 static void
device_removed(struct udev_device * udev_device,struct udev_input * input)150 device_removed(struct udev_device *udev_device, struct udev_input *input)
151 {
152 struct evdev_device *device;
153 struct udev_seat *seat;
154 const char *syspath;
155
156 syspath = udev_device_get_syspath(udev_device);
157 list_for_each(seat, &input->base.seat_list, base.link) {
158 list_for_each_safe(device,
159 &seat->base.devices_list, base.link) {
160 if (streq(syspath,
161 udev_device_get_syspath(device->udev_device))) {
162 evdev_device_remove(device);
163 break;
164 }
165 }
166 }
167 }
168
169 static int
udev_input_add_devices(struct udev_input * input,struct udev * udev)170 udev_input_add_devices(struct udev_input *input, struct udev *udev)
171 {
172 struct udev_enumerate *e;
173 struct udev_list_entry *entry;
174 struct udev_device *device;
175 const char *path, *sysname;
176
177 e = udev_enumerate_new(udev);
178 udev_enumerate_add_match_subsystem(e, "input");
179 udev_enumerate_scan_devices(e);
180 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
181 path = udev_list_entry_get_name(entry);
182 device = udev_device_new_from_syspath(udev, path);
183 if (!device)
184 continue;
185
186 sysname = udev_device_get_sysname(device);
187 if (!strneq("event", sysname, 5)) {
188 udev_device_unref(device);
189 continue;
190 }
191
192 /* Skip unconfigured device. udev will send an event
193 * when device is fully configured */
194 if (!udev_device_get_is_initialized(device)) {
195 log_debug(&input->base,
196 "%-7s - skip unconfigured input device '%s'\n",
197 sysname,
198 udev_device_get_devnode(device));
199 udev_device_unref(device);
200 continue;
201 }
202
203 if (device_added(device, input, NULL) < 0) {
204 udev_device_unref(device);
205 udev_enumerate_unref(e);
206 return -1;
207 }
208
209 udev_device_unref(device);
210 }
211 udev_enumerate_unref(e);
212
213 return 0;
214 }
215
216 static void
evdev_udev_handler(void * data)217 evdev_udev_handler(void *data)
218 {
219 struct udev_input *input = data;
220 struct udev_device *udev_device;
221 const char *action;
222
223 udev_device = udev_monitor_receive_device(input->udev_monitor);
224 if (!udev_device)
225 return;
226
227 action = udev_device_get_action(udev_device);
228 if (!action)
229 goto out;
230
231 if (!strneq("event", udev_device_get_sysname(udev_device), 5))
232 goto out;
233
234 if (streq(action, "add"))
235 device_added(udev_device, input, NULL);
236 else if (streq(action, "remove"))
237 device_removed(udev_device, input);
238
239 out:
240 udev_device_unref(udev_device);
241 }
242
243 static void
udev_input_remove_devices(struct udev_input * input)244 udev_input_remove_devices(struct udev_input *input)
245 {
246 struct evdev_device *device;
247 struct udev_seat *seat;
248
249 list_for_each_safe(seat, &input->base.seat_list, base.link) {
250 libinput_seat_ref(&seat->base);
251 list_for_each_safe(device,
252 &seat->base.devices_list, base.link) {
253 evdev_device_remove(device);
254 }
255 libinput_seat_unref(&seat->base);
256 }
257 }
258
259 static void
udev_input_disable(struct libinput * libinput)260 udev_input_disable(struct libinput *libinput)
261 {
262 struct udev_input *input = (struct udev_input*)libinput;
263
264 if (!input->udev_monitor)
265 return;
266
267 udev_monitor_unref(input->udev_monitor);
268 input->udev_monitor = NULL;
269 libinput_remove_source(&input->base, input->udev_monitor_source);
270 input->udev_monitor_source = NULL;
271
272 udev_input_remove_devices(input);
273 }
274
275 static int
udev_input_enable(struct libinput * libinput)276 udev_input_enable(struct libinput *libinput)
277 {
278 struct udev_input *input = (struct udev_input*)libinput;
279 struct udev *udev = input->udev;
280 int fd;
281
282 if (input->udev_monitor || !input->seat_id)
283 return 0;
284
285 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
286 if (!input->udev_monitor) {
287 log_info(libinput,
288 "udev: failed to create the udev monitor\n");
289 return -1;
290 }
291
292 if (udev_monitor_filter_add_match_subsystem_devtype(
293 input->udev_monitor, "input", NULL)) {
294 log_info(libinput, "udev: failed to set up filter\n");
295 return -1;
296 }
297
298 if (udev_monitor_enable_receiving(input->udev_monitor)) {
299 log_info(libinput, "udev: failed to bind the udev monitor\n");
300 udev_monitor_unref(input->udev_monitor);
301 input->udev_monitor = NULL;
302 return -1;
303 }
304
305 fd = udev_monitor_get_fd(input->udev_monitor);
306 input->udev_monitor_source = libinput_add_fd(&input->base,
307 fd,
308 evdev_udev_handler,
309 input);
310 if (!input->udev_monitor_source) {
311 udev_monitor_unref(input->udev_monitor);
312 input->udev_monitor = NULL;
313 return -1;
314 }
315
316 if (udev_input_add_devices(input, udev) < 0) {
317 udev_input_disable(libinput);
318 return -1;
319 }
320
321 return 0;
322 }
323
324 static void
udev_input_destroy(struct libinput * input)325 udev_input_destroy(struct libinput *input)
326 {
327 struct udev_input *udev_input = (struct udev_input*)input;
328
329 if (input == NULL)
330 return;
331
332 udev_unref(udev_input->udev);
333 free(udev_input->seat_id);
334 }
335
336 static void
udev_seat_destroy(struct libinput_seat * seat)337 udev_seat_destroy(struct libinput_seat *seat)
338 {
339 struct udev_seat *useat = (struct udev_seat*)seat;
340 free(useat);
341 }
342
343 static struct udev_seat *
udev_seat_create(struct udev_input * input,const char * device_seat,const char * seat_name)344 udev_seat_create(struct udev_input *input,
345 const char *device_seat,
346 const char *seat_name)
347 {
348 struct udev_seat *seat;
349
350 seat = zalloc(sizeof *seat);
351
352 libinput_seat_init(&seat->base, &input->base,
353 device_seat, seat_name,
354 udev_seat_destroy);
355
356 return seat;
357 }
358
359 static struct udev_seat *
udev_seat_get_named(struct udev_input * input,const char * seat_name)360 udev_seat_get_named(struct udev_input *input, const char *seat_name)
361 {
362 struct udev_seat *seat;
363
364 list_for_each(seat, &input->base.seat_list, base.link) {
365 if (streq(seat->base.logical_name, seat_name))
366 return seat;
367 }
368
369 return NULL;
370 }
371
372 static int
udev_device_change_seat(struct libinput_device * device,const char * seat_name)373 udev_device_change_seat(struct libinput_device *device,
374 const char *seat_name)
375 {
376 struct libinput *libinput = device->seat->libinput;
377 struct udev_input *input = (struct udev_input *)libinput;
378 struct evdev_device *evdev = evdev_device(device);
379 struct udev_device *udev_device = evdev->udev_device;
380 int rc;
381
382 udev_device_ref(udev_device);
383 device_removed(udev_device, input);
384 rc = device_added(udev_device, input, seat_name);
385 udev_device_unref(udev_device);
386
387 return rc;
388 }
389
390 static const struct libinput_interface_backend interface_backend = {
391 .resume = udev_input_enable,
392 .suspend = udev_input_disable,
393 .destroy = udev_input_destroy,
394 .device_change_seat = udev_device_change_seat,
395 };
396
397 LIBINPUT_EXPORT struct libinput *
libinput_udev_create_context(const struct libinput_interface * interface,void * user_data,struct udev * udev)398 libinput_udev_create_context(const struct libinput_interface *interface,
399 void *user_data,
400 struct udev *udev)
401 {
402 struct udev_input *input;
403
404 if (!interface || !udev)
405 return NULL;
406
407 input = zalloc(sizeof *input);
408
409 if (libinput_init(&input->base, interface,
410 &interface_backend, user_data) != 0) {
411 libinput_unref(&input->base);
412 free(input);
413 return NULL;
414 }
415
416 input->udev = udev_ref(udev);
417
418 return &input->base;
419 }
420
421 LIBINPUT_EXPORT int
libinput_udev_assign_seat(struct libinput * libinput,const char * seat_id)422 libinput_udev_assign_seat(struct libinput *libinput,
423 const char *seat_id)
424 {
425 struct udev_input *input = (struct udev_input*)libinput;
426
427 if (!seat_id)
428 return -1;
429
430 if (strlen(seat_id) > 256) {
431 log_bug_client(libinput,
432 "Unexpected seat id, limited to 256 characters.\n");
433 return -1;
434 }
435
436 if (libinput->interface_backend != &interface_backend) {
437 log_bug_client(libinput, "Mismatching backends.\n");
438 return -1;
439 }
440
441 if (input->seat_id != NULL)
442 return -1;
443
444 /* We cannot do this during udev_create_context because the log
445 * handler isn't set up there but we really want to log to the right
446 * place if the quirks run into parser errors. So we have to do it
447 * here since we can expect the log handler to be set up by now.
448 */
449 libinput_init_quirks(libinput);
450
451 input->seat_id = safe_strdup(seat_id);
452
453 if (udev_input_enable(&input->base) < 0)
454 return -1;
455
456 return 0;
457 }
458