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 } else if (device == NULL) {
132 log_info(&input->base,
133 "%-7s - failed to create input device '%s'\n",
134 sysname,
135 devnode);
136 return 0;
137 }
138
139 evdev_read_calibration_prop(device);
140
141 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
142 device->output_name = safe_strdup(output_name);
143
144 return 0;
145 }
146
147 static void
device_removed(struct udev_device * udev_device,struct udev_input * input)148 device_removed(struct udev_device *udev_device, struct udev_input *input)
149 {
150 struct evdev_device *device, *next;
151 struct udev_seat *seat;
152 const char *syspath;
153
154 syspath = udev_device_get_syspath(udev_device);
155 list_for_each(seat, &input->base.seat_list, base.link) {
156 list_for_each_safe(device, next,
157 &seat->base.devices_list, base.link) {
158 if (streq(syspath,
159 udev_device_get_syspath(device->udev_device))) {
160 evdev_device_remove(device);
161 break;
162 }
163 }
164 }
165 }
166
167 static int
udev_input_add_devices(struct udev_input * input,struct udev * udev)168 udev_input_add_devices(struct udev_input *input, struct udev *udev)
169 {
170 struct udev_enumerate *e;
171 struct udev_list_entry *entry;
172 struct udev_device *device;
173 const char *path, *sysname;
174
175 e = udev_enumerate_new(udev);
176 udev_enumerate_add_match_subsystem(e, "input");
177 udev_enumerate_scan_devices(e);
178 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
179 path = udev_list_entry_get_name(entry);
180 device = udev_device_new_from_syspath(udev, path);
181 if (!device)
182 continue;
183
184 sysname = udev_device_get_sysname(device);
185 if (strncmp("event", sysname, 5) != 0) {
186 udev_device_unref(device);
187 continue;
188 }
189
190 /* Skip unconfigured device. udev will send an event
191 * when device is fully configured */
192 if (!udev_device_get_is_initialized(device)) {
193 log_debug(&input->base,
194 "%-7s - skip unconfigured input device '%s'\n",
195 sysname,
196 udev_device_get_devnode(device));
197 udev_device_unref(device);
198 continue;
199 }
200
201 if (device_added(device, input, NULL) < 0) {
202 udev_device_unref(device);
203 udev_enumerate_unref(e);
204 return -1;
205 }
206
207 udev_device_unref(device);
208 }
209 udev_enumerate_unref(e);
210
211 return 0;
212 }
213
214 static void
evdev_udev_handler(void * data)215 evdev_udev_handler(void *data)
216 {
217 struct udev_input *input = data;
218 struct udev_device *udev_device;
219 const char *action;
220
221 udev_device = udev_monitor_receive_device(input->udev_monitor);
222 if (!udev_device)
223 return;
224
225 action = udev_device_get_action(udev_device);
226 if (!action)
227 goto out;
228
229 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
230 goto out;
231
232 if (streq(action, "add"))
233 device_added(udev_device, input, NULL);
234 else if (streq(action, "remove"))
235 device_removed(udev_device, input);
236
237 out:
238 udev_device_unref(udev_device);
239 }
240
241 static void
udev_input_remove_devices(struct udev_input * input)242 udev_input_remove_devices(struct udev_input *input)
243 {
244 struct evdev_device *device, *next;
245 struct udev_seat *seat, *tmp;
246
247 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
248 libinput_seat_ref(&seat->base);
249 list_for_each_safe(device, next,
250 &seat->base.devices_list, base.link) {
251 evdev_device_remove(device);
252 }
253 libinput_seat_unref(&seat->base);
254 }
255 }
256
257 static void
udev_input_disable(struct libinput * libinput)258 udev_input_disable(struct libinput *libinput)
259 {
260 struct udev_input *input = (struct udev_input*)libinput;
261
262 if (!input->udev_monitor)
263 return;
264
265 udev_monitor_unref(input->udev_monitor);
266 input->udev_monitor = NULL;
267 libinput_remove_source(&input->base, input->udev_monitor_source);
268 input->udev_monitor_source = NULL;
269
270 udev_input_remove_devices(input);
271 }
272
273 static int
udev_input_enable(struct libinput * libinput)274 udev_input_enable(struct libinput *libinput)
275 {
276 struct udev_input *input = (struct udev_input*)libinput;
277 struct udev *udev = input->udev;
278 int fd;
279
280 if (input->udev_monitor || !input->seat_id)
281 return 0;
282
283 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
284 if (!input->udev_monitor) {
285 log_info(libinput,
286 "udev: failed to create the udev monitor\n");
287 return -1;
288 }
289
290 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
291 "input", NULL);
292
293 if (udev_monitor_enable_receiving(input->udev_monitor)) {
294 log_info(libinput, "udev: failed to bind the udev monitor\n");
295 udev_monitor_unref(input->udev_monitor);
296 input->udev_monitor = NULL;
297 return -1;
298 }
299
300 fd = udev_monitor_get_fd(input->udev_monitor);
301 input->udev_monitor_source = libinput_add_fd(&input->base,
302 fd,
303 evdev_udev_handler,
304 input);
305 if (!input->udev_monitor_source) {
306 udev_monitor_unref(input->udev_monitor);
307 input->udev_monitor = NULL;
308 return -1;
309 }
310
311 if (udev_input_add_devices(input, udev) < 0) {
312 udev_input_disable(libinput);
313 return -1;
314 }
315
316 return 0;
317 }
318
319 static void
udev_input_destroy(struct libinput * input)320 udev_input_destroy(struct libinput *input)
321 {
322 struct udev_input *udev_input = (struct udev_input*)input;
323
324 if (input == NULL)
325 return;
326
327 udev_unref(udev_input->udev);
328 free(udev_input->seat_id);
329 }
330
331 static void
udev_seat_destroy(struct libinput_seat * seat)332 udev_seat_destroy(struct libinput_seat *seat)
333 {
334 struct udev_seat *useat = (struct udev_seat*)seat;
335 free(useat);
336 }
337
338 static struct udev_seat *
udev_seat_create(struct udev_input * input,const char * device_seat,const char * seat_name)339 udev_seat_create(struct udev_input *input,
340 const char *device_seat,
341 const char *seat_name)
342 {
343 struct udev_seat *seat;
344
345 seat = zalloc(sizeof *seat);
346
347 libinput_seat_init(&seat->base, &input->base,
348 device_seat, seat_name,
349 udev_seat_destroy);
350
351 return seat;
352 }
353
354 static struct udev_seat *
udev_seat_get_named(struct udev_input * input,const char * seat_name)355 udev_seat_get_named(struct udev_input *input, const char *seat_name)
356 {
357 struct udev_seat *seat;
358
359 list_for_each(seat, &input->base.seat_list, base.link) {
360 if (streq(seat->base.logical_name, seat_name))
361 return seat;
362 }
363
364 return NULL;
365 }
366
367 static int
udev_device_change_seat(struct libinput_device * device,const char * seat_name)368 udev_device_change_seat(struct libinput_device *device,
369 const char *seat_name)
370 {
371 struct libinput *libinput = device->seat->libinput;
372 struct udev_input *input = (struct udev_input *)libinput;
373 struct evdev_device *evdev = evdev_device(device);
374 struct udev_device *udev_device = evdev->udev_device;
375 int rc;
376
377 udev_device_ref(udev_device);
378 device_removed(udev_device, input);
379 rc = device_added(udev_device, input, seat_name);
380 udev_device_unref(udev_device);
381
382 return rc;
383 }
384
385 static const struct libinput_interface_backend interface_backend = {
386 .resume = udev_input_enable,
387 .suspend = udev_input_disable,
388 .destroy = udev_input_destroy,
389 .device_change_seat = udev_device_change_seat,
390 };
391
392 LIBINPUT_EXPORT struct libinput *
libinput_udev_create_context(const struct libinput_interface * interface,void * user_data,struct udev * udev)393 libinput_udev_create_context(const struct libinput_interface *interface,
394 void *user_data,
395 struct udev *udev)
396 {
397 struct udev_input *input;
398
399 if (!interface || !udev)
400 return NULL;
401
402 input = zalloc(sizeof *input);
403
404 if (libinput_init(&input->base, interface,
405 &interface_backend, user_data) != 0) {
406 libinput_unref(&input->base);
407 free(input);
408 return NULL;
409 }
410
411 input->udev = udev_ref(udev);
412
413 return &input->base;
414 }
415
416 LIBINPUT_EXPORT int
libinput_udev_assign_seat(struct libinput * libinput,const char * seat_id)417 libinput_udev_assign_seat(struct libinput *libinput,
418 const char *seat_id)
419 {
420 struct udev_input *input = (struct udev_input*)libinput;
421
422 if (!seat_id)
423 return -1;
424
425 if (strlen(seat_id) > 256) {
426 log_bug_client(libinput,
427 "Unexpected seat id, limited to 256 characters.\n");
428 return -1;
429 }
430
431 if (libinput->interface_backend != &interface_backend) {
432 log_bug_client(libinput, "Mismatching backends.\n");
433 return -1;
434 }
435
436 if (input->seat_id != NULL)
437 return -1;
438
439 /* We cannot do this during udev_create_context because the log
440 * handler isn't set up there but we really want to log to the right
441 * place if the quirks run into parser errors. So we have to do it
442 * here since we can expect the log handler to be set up by now.
443 */
444 libinput_init_quirks(libinput);
445
446 input->seat_id = safe_strdup(seat_id);
447
448 if (udev_input_enable(&input->base) < 0)
449 return -1;
450
451 return 0;
452 }
453