89160ff892dab89beb0b8831d30acb321c4a47c0
[platform/upstream/libinput.git] / src / udev-seat.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
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.
13  *
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.
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fcntl.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 static int
45 device_added(struct udev_device *udev_device, struct udev_input *input)
46 {
47         struct evdev_device *device;
48         const char *devnode;
49         const char *sysname;
50         const char *device_seat, *seat_name, *output_name;
51         const char *calibration_values;
52         struct udev_seat *seat;
53
54         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
55         if (!device_seat)
56                 device_seat = default_seat;
57
58         if (strcmp(device_seat, input->seat_id))
59                 return 0;
60
61         devnode = udev_device_get_devnode(udev_device);
62         sysname = udev_device_get_sysname(udev_device);
63
64         /* Search for matching logical seat */
65         seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
66         if (!seat_name)
67                 seat_name = default_seat_name;
68
69         seat = udev_seat_get_named(input, seat_name);
70
71         if (seat)
72                 libinput_seat_ref(&seat->base);
73         else {
74                 seat = udev_seat_create(input, device_seat, seat_name);
75                 if (!seat)
76                         return -1;
77         }
78
79         device = evdev_device_create(&seat->base, devnode, sysname);
80         libinput_seat_unref(&seat->base);
81
82         if (device == EVDEV_UNHANDLED_DEVICE) {
83                 log_info("not using input device '%s'.\n", devnode);
84                 return 0;
85         } else if (device == NULL) {
86                 log_info("failed to create input device '%s'.\n", devnode);
87                 return 0;
88         }
89
90         calibration_values =
91                 udev_device_get_property_value(udev_device,
92                                                "WL_CALIBRATION");
93
94         if (calibration_values && sscanf(calibration_values,
95                                          "%f %f %f %f %f %f",
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]);
110         }
111
112         output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
113         if (output_name)
114                 device->output_name = strdup(output_name);
115
116         return 0;
117 }
118
119 static void
120 device_removed(struct udev_device *udev_device, struct udev_input *input)
121 {
122         const char *devnode;
123         struct evdev_device *device, *next;
124         struct udev_seat *seat;
125
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);
134                                 break;
135                         }
136                 }
137         }
138 }
139
140 static int
141 udev_input_add_devices(struct udev_input *input, struct udev *udev)
142 {
143         struct udev_enumerate *e;
144         struct udev_list_entry *entry;
145         struct udev_device *device;
146         const char *path, *sysname;
147
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);
154
155                 sysname = udev_device_get_sysname(device);
156                 if (strncmp("event", sysname, 5) != 0) {
157                         udev_device_unref(device);
158                         continue;
159                 }
160
161                 if (device_added(device, input) < 0) {
162                         udev_device_unref(device);
163                         udev_enumerate_unref(e);
164                         return -1;
165                 }
166
167                 udev_device_unref(device);
168         }
169         udev_enumerate_unref(e);
170
171         return 0;
172 }
173
174 static void
175 evdev_udev_handler(void *data)
176 {
177         struct udev_input *input = data;
178         struct udev_device *udev_device;
179         const char *action;
180
181         udev_device = udev_monitor_receive_device(input->udev_monitor);
182         if (!udev_device)
183                 return;
184
185         action = udev_device_get_action(udev_device);
186         if (!action)
187                 goto out;
188
189         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
190                 goto out;
191
192         if (!strcmp(action, "add"))
193                 device_added(udev_device, input);
194         else if (!strcmp(action, "remove"))
195                 device_removed(udev_device, input);
196
197 out:
198         udev_device_unref(udev_device);
199 }
200
201 static void
202 udev_input_remove_devices(struct udev_input *input)
203 {
204         struct evdev_device *device, *next;
205         struct udev_seat *seat, *tmp;
206
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);
212                 }
213                 libinput_seat_unref(&seat->base);
214         }
215 }
216
217
218 static void
219 udev_input_disable(struct libinput *libinput)
220 {
221         struct udev_input *input = (struct udev_input*)libinput;
222
223         if (!input->udev_monitor)
224                 return;
225
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;
230
231         udev_input_remove_devices(input);
232 }
233
234 static int
235 udev_input_enable(struct libinput *libinput)
236 {
237         struct udev_input *input = (struct udev_input*)libinput;
238         struct udev *udev = input->udev;
239         int fd;
240
241         if (input->udev_monitor)
242                 return 0;
243
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");
247                 return -1;
248         }
249
250         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
251                         "input", NULL);
252
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;
257                 return -1;
258         }
259
260         fd = udev_monitor_get_fd(input->udev_monitor);
261         input->udev_monitor_source = libinput_add_fd(&input->base,
262                                                      fd,
263                                                      evdev_udev_handler,
264                                                      input);
265         if (!input->udev_monitor_source) {
266                 udev_monitor_unref(input->udev_monitor);
267                 input->udev_monitor = NULL;
268                 return -1;
269         }
270
271         if (udev_input_add_devices(input, udev) < 0) {
272                 udev_input_disable(libinput);
273                 return -1;
274         }
275
276         return 0;
277 }
278
279 static void
280 udev_input_destroy(struct libinput *input)
281 {
282         struct udev_input *udev_input = (struct udev_input*)input;
283
284         if (input == NULL)
285                 return;
286
287         udev_unref(udev_input->udev);
288         free(udev_input->seat_id);
289 }
290
291 static void
292 udev_seat_destroy(struct libinput_seat *seat)
293 {
294         struct udev_seat *useat = (struct udev_seat*)seat;
295         free(useat);
296 }
297
298 static struct udev_seat *
299 udev_seat_create(struct udev_input *input,
300                  const char *device_seat,
301                  const char *seat_name)
302 {
303         struct udev_seat *seat;
304
305         seat = zalloc(sizeof *seat);
306         if (!seat)
307                 return NULL;
308
309         libinput_seat_init(&seat->base, &input->base,
310                            device_seat, seat_name,
311                            udev_seat_destroy);
312
313         return seat;
314 }
315
316 static struct udev_seat *
317 udev_seat_get_named(struct udev_input *input, const char *seat_name)
318 {
319         struct udev_seat *seat;
320
321         list_for_each(seat, &input->base.seat_list, base.link) {
322                 if (strcmp(seat->base.logical_name, seat_name) == 0)
323                         return seat;
324         }
325
326         return NULL;
327 }
328
329 static const struct libinput_interface_backend interface_backend = {
330         .resume = udev_input_enable,
331         .suspend = udev_input_disable,
332         .destroy = udev_input_destroy,
333 };
334
335 LIBINPUT_EXPORT struct libinput *
336 libinput_udev_create_context(const struct libinput_interface *interface,
337                              void *user_data,
338                              struct udev *udev)
339 {
340         struct udev_input *input;
341
342         if (!interface || !udev)
343                 return NULL;
344
345         input = zalloc(sizeof *input);
346         if (!input)
347                 return NULL;
348
349         if (libinput_init(&input->base, interface,
350                           &interface_backend, user_data) != 0) {
351                 free(input);
352                 return NULL;
353         }
354
355         input->udev = udev_ref(udev);
356
357         return &input->base;
358 }
359
360 LIBINPUT_EXPORT int
361 libinput_udev_assign_seat(struct libinput *libinput,
362                           const char *seat_id)
363 {
364         struct udev_input *input = (struct udev_input*)libinput;
365
366         if (!seat_id)
367                 return -1;
368         if (input->seat_id != NULL)
369                 return -1;
370
371         if (libinput->interface_backend != &interface_backend) {
372                 log_bug_client("Mismatching backends.\n");
373                 return -1;
374         }
375
376         input->seat_id = strdup(seat_id);
377
378         if (udev_input_enable(&input->base) < 0)
379                 return -1;
380
381         return 0;
382 }
383
384 LIBINPUT_EXPORT struct libinput *
385 libinput_udev_create_for_seat(const struct libinput_interface *interface,
386                               void *user_data,
387                               struct udev *udev,
388                               const char *seat_id)
389 {
390         struct libinput *libinput;
391
392         if (!interface || !udev || !seat_id)
393                 return NULL;
394
395         libinput = libinput_udev_create_context(interface, user_data, udev);
396         if (!libinput)
397                 return NULL;
398
399         if (libinput_udev_assign_seat(libinput, seat_id) != 0) {
400                 libinput_destroy(libinput);
401                 libinput = NULL;
402         }
403
404         return libinput;
405 }