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 #include <unistd.h>
31 #include <fcntl.h>
32
33 #include "evdev.h"
34 #include "udev-seat.h"
35
36 static const char default_seat[] = "seat0";
37 static const char default_seat_name[] = "default";
38
39 static struct udev_seat *
40 udev_seat_create(struct udev_input *input,
41 const char *device_seat,
42 const char *seat_name);
43 static struct udev_seat *
44 udev_seat_get_named(struct udev_input *input, const char *seat_name);
45
46 static int
device_added(struct udev_device * udev_device,struct udev_input * input,const char * seat_name)47 device_added(struct udev_device *udev_device,
48 struct udev_input *input,
49 const char *seat_name)
50 {
51 struct evdev_device *device;
52 const char *devnode, *sysname;
53 const char *device_seat, *output_name;
54 struct udev_seat *seat;
55
56 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
57 if (!device_seat)
58 device_seat = default_seat;
59
60 if (!streq(device_seat, input->seat_id))
61 return 0;
62
63 if (ignore_litest_test_suite_device(udev_device))
64 return 0;
65
66 devnode = udev_device_get_devnode(udev_device);
67 sysname = udev_device_get_sysname(udev_device);
68
69 /* Search for matching logical seat */
70 if (!seat_name)
71 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
72 if (!seat_name)
73 seat_name = default_seat_name;
74
75 seat = udev_seat_get_named(input, seat_name);
76
77 if (seat)
78 libinput_seat_ref(&seat->base);
79 else {
80 seat = udev_seat_create(input, device_seat, seat_name);
81 if (!seat)
82 return -1;
83 }
84
85 device = evdev_device_create(&seat->base, udev_device);
86 libinput_seat_unref(&seat->base);
87
88 if (device == EVDEV_UNHANDLED_DEVICE) {
89 log_info(&input->base,
90 "%-7s - not using input device '%s'\n",
91 sysname,
92 devnode);
93 return 0;
94 } else if (device == NULL) {
95 log_info(&input->base,
96 "%-7s - failed to create input device '%s'\n",
97 sysname,
98 devnode);
99 return 0;
100 }
101
102 evdev_read_calibration_prop(device);
103
104 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
105 device->output_name = safe_strdup(output_name);
106
107 return 0;
108 }
109
110 static void
device_removed(struct udev_device * udev_device,struct udev_input * input)111 device_removed(struct udev_device *udev_device, struct udev_input *input)
112 {
113 struct evdev_device *device, *next;
114 struct udev_seat *seat;
115 const char *syspath;
116
117 syspath = udev_device_get_syspath(udev_device);
118 list_for_each(seat, &input->base.seat_list, base.link) {
119 list_for_each_safe(device, next,
120 &seat->base.devices_list, base.link) {
121 if (streq(syspath,
122 udev_device_get_syspath(device->udev_device))) {
123 evdev_device_remove(device);
124 break;
125 }
126 }
127 }
128 }
129
130 static int
udev_input_add_devices(struct udev_input * input,struct udev * udev)131 udev_input_add_devices(struct udev_input *input, struct udev *udev)
132 {
133 struct udev_enumerate *e;
134 struct udev_list_entry *entry;
135 struct udev_device *device;
136 const char *path, *sysname;
137
138 e = udev_enumerate_new(udev);
139 udev_enumerate_add_match_subsystem(e, "input");
140 udev_enumerate_scan_devices(e);
141 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
142 path = udev_list_entry_get_name(entry);
143 device = udev_device_new_from_syspath(udev, path);
144 if (!device)
145 continue;
146
147 sysname = udev_device_get_sysname(device);
148 if (strncmp("event", sysname, 5) != 0) {
149 udev_device_unref(device);
150 continue;
151 }
152
153 /* Skip unconfigured device. udev will send an event
154 * when device is fully configured */
155 if (!udev_device_get_is_initialized(device)) {
156 log_debug(&input->base,
157 "%-7s - skip unconfigured input device '%s'\n",
158 sysname,
159 udev_device_get_devnode(device));
160 udev_device_unref(device);
161 continue;
162 }
163
164 if (device_added(device, input, NULL) < 0) {
165 udev_device_unref(device);
166 udev_enumerate_unref(e);
167 return -1;
168 }
169
170 udev_device_unref(device);
171 }
172 udev_enumerate_unref(e);
173
174 return 0;
175 }
176
177 static void
evdev_udev_handler(void * data)178 evdev_udev_handler(void *data)
179 {
180 struct udev_input *input = data;
181 struct udev_device *udev_device;
182 const char *action;
183
184 udev_device = udev_monitor_receive_device(input->udev_monitor);
185 if (!udev_device)
186 return;
187
188 action = udev_device_get_action(udev_device);
189 if (!action)
190 goto out;
191
192 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
193 goto out;
194
195 if (streq(action, "add"))
196 device_added(udev_device, input, NULL);
197 else if (streq(action, "remove"))
198 device_removed(udev_device, input);
199
200 out:
201 udev_device_unref(udev_device);
202 }
203
204 static void
udev_input_remove_devices(struct udev_input * input)205 udev_input_remove_devices(struct udev_input *input)
206 {
207 struct evdev_device *device, *next;
208 struct udev_seat *seat, *tmp;
209
210 list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
211 libinput_seat_ref(&seat->base);
212 list_for_each_safe(device, next,
213 &seat->base.devices_list, base.link) {
214 evdev_device_remove(device);
215 }
216 libinput_seat_unref(&seat->base);
217 }
218 }
219
220 static void
udev_input_disable(struct libinput * libinput)221 udev_input_disable(struct libinput *libinput)
222 {
223 struct udev_input *input = (struct udev_input*)libinput;
224
225 if (!input->udev_monitor)
226 return;
227
228 udev_monitor_unref(input->udev_monitor);
229 input->udev_monitor = NULL;
230 libinput_remove_source(&input->base, input->udev_monitor_source);
231 input->udev_monitor_source = NULL;
232
233 udev_input_remove_devices(input);
234 }
235
236 static int
udev_input_enable(struct libinput * libinput)237 udev_input_enable(struct libinput *libinput)
238 {
239 struct udev_input *input = (struct udev_input*)libinput;
240 struct udev *udev = input->udev;
241 int fd;
242
243 if (input->udev_monitor || !input->seat_id)
244 return 0;
245
246 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
247 if (!input->udev_monitor) {
248 log_info(libinput,
249 "udev: failed to create the udev monitor\n");
250 return -1;
251 }
252
253 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
254 "input", NULL);
255
256 if (udev_monitor_enable_receiving(input->udev_monitor)) {
257 log_info(libinput, "udev: failed to bind the udev monitor\n");
258 udev_monitor_unref(input->udev_monitor);
259 input->udev_monitor = NULL;
260 return -1;
261 }
262
263 fd = udev_monitor_get_fd(input->udev_monitor);
264 input->udev_monitor_source = libinput_add_fd(&input->base,
265 fd,
266 evdev_udev_handler,
267 input);
268 if (!input->udev_monitor_source) {
269 udev_monitor_unref(input->udev_monitor);
270 input->udev_monitor = NULL;
271 return -1;
272 }
273
274 if (udev_input_add_devices(input, udev) < 0) {
275 udev_input_disable(libinput);
276 return -1;
277 }
278
279 return 0;
280 }
281
282 static void
udev_input_destroy(struct libinput * input)283 udev_input_destroy(struct libinput *input)
284 {
285 struct udev_input *udev_input = (struct udev_input*)input;
286
287 if (input == NULL)
288 return;
289
290 udev_unref(udev_input->udev);
291 free(udev_input->seat_id);
292 }
293
294 static void
udev_seat_destroy(struct libinput_seat * seat)295 udev_seat_destroy(struct libinput_seat *seat)
296 {
297 struct udev_seat *useat = (struct udev_seat*)seat;
298 free(useat);
299 }
300
301 static struct udev_seat *
udev_seat_create(struct udev_input * input,const char * device_seat,const char * seat_name)302 udev_seat_create(struct udev_input *input,
303 const char *device_seat,
304 const char *seat_name)
305 {
306 struct udev_seat *seat;
307
308 seat = zalloc(sizeof *seat);
309
310 libinput_seat_init(&seat->base, &input->base,
311 device_seat, seat_name,
312 udev_seat_destroy);
313
314 return seat;
315 }
316
317 static struct udev_seat *
udev_seat_get_named(struct udev_input * input,const char * seat_name)318 udev_seat_get_named(struct udev_input *input, const char *seat_name)
319 {
320 struct udev_seat *seat;
321
322 list_for_each(seat, &input->base.seat_list, base.link) {
323 if (streq(seat->base.logical_name, seat_name))
324 return seat;
325 }
326
327 return NULL;
328 }
329
330 static int
udev_device_change_seat(struct libinput_device * device,const char * seat_name)331 udev_device_change_seat(struct libinput_device *device,
332 const char *seat_name)
333 {
334 struct libinput *libinput = device->seat->libinput;
335 struct udev_input *input = (struct udev_input *)libinput;
336 struct evdev_device *evdev = evdev_device(device);
337 struct udev_device *udev_device = evdev->udev_device;
338 int rc;
339
340 udev_device_ref(udev_device);
341 device_removed(udev_device, input);
342 rc = device_added(udev_device, input, seat_name);
343 udev_device_unref(udev_device);
344
345 return rc;
346 }
347
348 static const struct libinput_interface_backend interface_backend = {
349 .resume = udev_input_enable,
350 .suspend = udev_input_disable,
351 .destroy = udev_input_destroy,
352 .device_change_seat = udev_device_change_seat,
353 };
354
355 LIBINPUT_EXPORT struct libinput *
libinput_udev_create_context(const struct libinput_interface * interface,void * user_data,struct udev * udev)356 libinput_udev_create_context(const struct libinput_interface *interface,
357 void *user_data,
358 struct udev *udev)
359 {
360 struct udev_input *input;
361
362 if (!interface || !udev)
363 return NULL;
364
365 input = zalloc(sizeof *input);
366
367 if (libinput_init(&input->base, interface,
368 &interface_backend, user_data) != 0) {
369 libinput_unref(&input->base);
370 free(input);
371 return NULL;
372 }
373
374 input->udev = udev_ref(udev);
375
376 return &input->base;
377 }
378
379 LIBINPUT_EXPORT int
libinput_udev_assign_seat(struct libinput * libinput,const char * seat_id)380 libinput_udev_assign_seat(struct libinput *libinput,
381 const char *seat_id)
382 {
383 struct udev_input *input = (struct udev_input*)libinput;
384
385 /* We cannot do this during udev_create_context because the log
386 * handler isn't set up there but we really want to log to the right
387 * place if the quirks run into parser errors. So we have to do it
388 * here since we can expect the log handler to be set up by now.
389 */
390 libinput_init_quirks(libinput);
391
392 if (!seat_id)
393 return -1;
394
395 if (libinput->interface_backend != &interface_backend) {
396 log_bug_client(libinput, "Mismatching backends.\n");
397 return -1;
398 }
399
400 if (input->seat_id != NULL)
401 return -1;
402
403 input->seat_id = safe_strdup(seat_id);
404
405 if (udev_input_enable(&input->base) < 0)
406 return -1;
407
408 return 0;
409 }
410