udev: Refcount the seat for each device
[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, const char *seat_name);
39 static struct udev_seat *
40 udev_seat_get_named(struct udev_input *input, const char *seat_name);
41
42 static int
43 device_added(struct udev_device *udev_device, struct udev_input *input)
44 {
45         struct libinput *libinput = &input->base;
46         struct evdev_device *device;
47         const char *devnode;
48         const char *sysname;
49         const char *device_seat, *seat_name, *output_name;
50         const char *calibration_values;
51         int fd;
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, seat_name);
75                 if (!seat)
76                         return -1;
77         }
78
79         /* Use non-blocking mode so that we can loop on read on
80          * evdev_device_data() until all events on the fd are
81          * read.  mtdev_get() also expects this. */
82         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
83         if (fd < 0) {
84                 log_info("opening input device '%s' failed (%s).\n", devnode, strerror(-fd));
85                 goto error;
86         }
87
88         device = evdev_device_create(&seat->base, devnode, sysname, fd);
89         if (device == EVDEV_UNHANDLED_DEVICE) {
90                 close_restricted(libinput, fd);
91                 log_info("not using input device '%s'.\n", devnode);
92                 goto error;
93         } else if (device == NULL) {
94                 close_restricted(libinput, fd);
95                 log_info("failed to create input device '%s'.\n", devnode);
96                 goto error;
97         }
98
99         calibration_values =
100                 udev_device_get_property_value(udev_device,
101                                                "WL_CALIBRATION");
102
103         if (calibration_values && sscanf(calibration_values,
104                                          "%f %f %f %f %f %f",
105                                          &device->abs.calibration[0],
106                                          &device->abs.calibration[1],
107                                          &device->abs.calibration[2],
108                                          &device->abs.calibration[3],
109                                          &device->abs.calibration[4],
110                                          &device->abs.calibration[5]) == 6) {
111                 device->abs.apply_calibration = 1;
112                 log_info("Applying calibration: %f %f %f %f %f %f\n",
113                          device->abs.calibration[0],
114                          device->abs.calibration[1],
115                          device->abs.calibration[2],
116                          device->abs.calibration[3],
117                          device->abs.calibration[4],
118                          device->abs.calibration[5]);
119         }
120
121         output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
122         if (output_name)
123                 device->output_name = strdup(output_name);
124
125         return 0;
126 error:
127         if (seat)
128                 libinput_seat_unref(&seat->base);
129         return 0;
130 }
131
132 static int
133 udev_input_add_devices(struct udev_input *input, struct udev *udev)
134 {
135         struct udev_enumerate *e;
136         struct udev_list_entry *entry;
137         struct udev_device *device;
138         const char *path, *sysname;
139
140         e = udev_enumerate_new(udev);
141         udev_enumerate_add_match_subsystem(e, "input");
142         udev_enumerate_scan_devices(e);
143         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
144                 path = udev_list_entry_get_name(entry);
145                 device = udev_device_new_from_syspath(udev, path);
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                 if (device_added(device, input) < 0) {
154                         udev_device_unref(device);
155                         udev_enumerate_unref(e);
156                         return -1;
157                 }
158
159                 udev_device_unref(device);
160         }
161         udev_enumerate_unref(e);
162
163         return 0;
164 }
165
166 static void
167 evdev_udev_handler(void *data)
168 {
169         struct udev_input *input = data;
170         struct libinput *libinput = &input->base;
171         struct udev_device *udev_device;
172         struct evdev_device *device, *next;
173         const char *action;
174         const char *devnode;
175         struct udev_seat *seat;
176
177         udev_device = udev_monitor_receive_device(input->udev_monitor);
178         if (!udev_device)
179                 return;
180
181         action = udev_device_get_action(udev_device);
182         if (!action)
183                 goto out;
184
185         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
186                 goto out;
187
188         if (!strcmp(action, "add")) {
189                 device_added(udev_device, input);
190         }
191         else if (!strcmp(action, "remove")) {
192                 devnode = udev_device_get_devnode(udev_device);
193                 list_for_each(seat, &input->base.seat_list, base.link) {
194                         list_for_each_safe(device, next,
195                                            &seat->base.devices_list, base.link)
196                                 if (!strcmp(device->devnode, devnode)) {
197                                         log_info("input device %s, %s removed\n",
198                                                  device->devname, device->devnode);
199                                         close_restricted(libinput, device->fd);
200                                         evdev_device_remove(device);
201                                         libinput_seat_unref(&seat->base);
202                                         break;
203                                 }
204                 }
205         }
206
207 out:
208         udev_device_unref(udev_device);
209 }
210
211 static void
212 udev_input_remove_devices(struct udev_input *input)
213 {
214         struct evdev_device *device, *next;
215         struct udev_seat *seat, *tmp;
216
217         list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
218                 list_for_each_safe(device, next,
219                                    &seat->base.devices_list, base.link) {
220                         close_restricted(&input->base, device->fd);
221                         evdev_device_remove(device);
222                         libinput_seat_unref(&seat->base);
223                 }
224         }
225 }
226
227
228 static void
229 udev_input_disable(struct libinput *libinput)
230 {
231         struct udev_input *input = (struct udev_input*)libinput;
232
233         if (!input->udev_monitor)
234                 return;
235
236         udev_monitor_unref(input->udev_monitor);
237         input->udev_monitor = NULL;
238         libinput_remove_source(&input->base, input->udev_monitor_source);
239         input->udev_monitor_source = NULL;
240
241         udev_input_remove_devices(input);
242 }
243
244 static int
245 udev_input_enable(struct libinput *libinput)
246 {
247         struct udev_input *input = (struct udev_input*)libinput;
248         struct udev *udev = input->udev;
249         int fd;
250
251         if (input->udev_monitor)
252                 return 0;
253
254         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
255         if (!input->udev_monitor) {
256                 log_info("udev: failed to create the udev monitor\n");
257                 return -1;
258         }
259
260         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
261                         "input", NULL);
262
263         if (udev_monitor_enable_receiving(input->udev_monitor)) {
264                 log_info("udev: failed to bind the udev monitor\n");
265                 udev_monitor_unref(input->udev_monitor);
266                 input->udev_monitor = NULL;
267                 return -1;
268         }
269
270         fd = udev_monitor_get_fd(input->udev_monitor);
271         input->udev_monitor_source = libinput_add_fd(&input->base,
272                                                      fd,
273                                                      evdev_udev_handler,
274                                                      input);
275         if (!input->udev_monitor_source) {
276                 udev_monitor_unref(input->udev_monitor);
277                 input->udev_monitor = NULL;
278                 return -1;
279         }
280
281         if (udev_input_add_devices(input, udev) < 0) {
282                 udev_input_disable(libinput);
283                 return -1;
284         }
285
286         return 0;
287 }
288
289 static void
290 udev_input_destroy(struct libinput *input)
291 {
292         struct udev_input *udev_input = (struct udev_input*)input;
293
294         if (input == NULL)
295                 return;
296
297         udev_input_disable(input);
298         udev_unref(udev_input->udev);
299         free(udev_input->seat_id);
300 }
301
302 static void
303 udev_seat_destroy(struct libinput_seat *seat)
304 {
305         struct udev_seat *useat = (struct udev_seat*)seat;
306         free(useat);
307 }
308
309 static struct udev_seat *
310 udev_seat_create(struct udev_input *input, const char *seat_name)
311 {
312         struct udev_seat *seat;
313
314         seat = zalloc(sizeof *seat);
315         if (!seat)
316                 return NULL;
317
318         libinput_seat_init(&seat->base, &input->base, seat_name, udev_seat_destroy);
319         list_insert(&input->base.seat_list, &seat->base.link);
320         notify_added_seat(&seat->base);
321
322         return seat;
323 }
324
325 static struct udev_seat *
326 udev_seat_get_named(struct udev_input *input, const char *seat_name)
327 {
328         struct udev_seat *seat;
329
330         list_for_each(seat, &input->base.seat_list, base.link) {
331                 if (strcmp(seat->base.name, seat_name) == 0)
332                         return seat;
333         }
334
335         return NULL;
336 }
337
338 static const struct libinput_interface_backend interface_backend = {
339         .resume = udev_input_enable,
340         .suspend = udev_input_disable,
341         .destroy = udev_input_destroy,
342 };
343
344 LIBINPUT_EXPORT struct libinput *
345 libinput_create_from_udev(const struct libinput_interface *interface,
346                           void *user_data,
347                           struct udev *udev,
348                           const char *seat_id)
349 {
350         struct udev_input *input;
351
352         if (!interface || !udev || !seat_id)
353                 return NULL;
354
355         input = zalloc(sizeof *input);
356         if (!input)
357                 return NULL;
358
359         if (libinput_init(&input->base, interface,
360                           &interface_backend, user_data) != 0) {
361                 free(input);
362                 return NULL;
363         }
364
365         input->udev = udev_ref(udev);
366         input->seat_id = strdup(seat_id);
367
368         if (udev_input_enable(&input->base) < 0) {
369                 udev_unref(udev);
370                 libinput_destroy(&input->base);
371                 free(input);
372                 return NULL;
373         }
374
375         return &input->base;
376 }