udev: notify about a removed seat when the last device is removed
[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                                         if (list_empty(&seat->base.devices_list))
202                                                 notify_removed_seat(&seat->base);
203                                         libinput_seat_unref(&seat->base);
204                                         break;
205                                 }
206                 }
207         }
208
209 out:
210         udev_device_unref(udev_device);
211 }
212
213 static void
214 udev_input_remove_devices(struct udev_input *input)
215 {
216         struct evdev_device *device, *next;
217         struct udev_seat *seat, *tmp;
218
219         list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
220                 list_for_each_safe(device, next,
221                                    &seat->base.devices_list, base.link) {
222                         close_restricted(&input->base, device->fd);
223                         evdev_device_remove(device);
224                         if (list_empty(&seat->base.devices_list)) {
225                                 notify_removed_seat(&seat->base);
226                                 /* seat is referenced by the event, so make
227                                    sure it's dropped from the seat list now,
228                                    to be freed whenever the device is
229                                    removed */
230                                 list_remove(&seat->base.link);
231                                 list_init(&seat->base.link);
232                         }
233                         libinput_seat_unref(&seat->base);
234                 }
235         }
236 }
237
238
239 static void
240 udev_input_disable(struct libinput *libinput)
241 {
242         struct udev_input *input = (struct udev_input*)libinput;
243
244         if (!input->udev_monitor)
245                 return;
246
247         udev_monitor_unref(input->udev_monitor);
248         input->udev_monitor = NULL;
249         libinput_remove_source(&input->base, input->udev_monitor_source);
250         input->udev_monitor_source = NULL;
251
252         udev_input_remove_devices(input);
253 }
254
255 static int
256 udev_input_enable(struct libinput *libinput)
257 {
258         struct udev_input *input = (struct udev_input*)libinput;
259         struct udev *udev = input->udev;
260         int fd;
261
262         if (input->udev_monitor)
263                 return 0;
264
265         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
266         if (!input->udev_monitor) {
267                 log_info("udev: failed to create the udev monitor\n");
268                 return -1;
269         }
270
271         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
272                         "input", NULL);
273
274         if (udev_monitor_enable_receiving(input->udev_monitor)) {
275                 log_info("udev: failed to bind the udev monitor\n");
276                 udev_monitor_unref(input->udev_monitor);
277                 input->udev_monitor = NULL;
278                 return -1;
279         }
280
281         fd = udev_monitor_get_fd(input->udev_monitor);
282         input->udev_monitor_source = libinput_add_fd(&input->base,
283                                                      fd,
284                                                      evdev_udev_handler,
285                                                      input);
286         if (!input->udev_monitor_source) {
287                 udev_monitor_unref(input->udev_monitor);
288                 input->udev_monitor = NULL;
289                 return -1;
290         }
291
292         if (udev_input_add_devices(input, udev) < 0) {
293                 udev_input_disable(libinput);
294                 return -1;
295         }
296
297         return 0;
298 }
299
300 static void
301 udev_input_destroy(struct libinput *input)
302 {
303         struct udev_input *udev_input = (struct udev_input*)input;
304
305         if (input == NULL)
306                 return;
307
308         udev_input_disable(input);
309         udev_unref(udev_input->udev);
310         free(udev_input->seat_id);
311 }
312
313 static void
314 udev_seat_destroy(struct libinput_seat *seat)
315 {
316         struct udev_seat *useat = (struct udev_seat*)seat;
317         free(useat);
318 }
319
320 static struct udev_seat *
321 udev_seat_create(struct udev_input *input, const char *seat_name)
322 {
323         struct udev_seat *seat;
324
325         seat = zalloc(sizeof *seat);
326         if (!seat)
327                 return NULL;
328
329         libinput_seat_init(&seat->base, &input->base, seat_name, udev_seat_destroy);
330         list_insert(&input->base.seat_list, &seat->base.link);
331         notify_added_seat(&seat->base);
332
333         return seat;
334 }
335
336 static struct udev_seat *
337 udev_seat_get_named(struct udev_input *input, const char *seat_name)
338 {
339         struct udev_seat *seat;
340
341         list_for_each(seat, &input->base.seat_list, base.link) {
342                 if (strcmp(seat->base.name, seat_name) == 0)
343                         return seat;
344         }
345
346         return NULL;
347 }
348
349 static const struct libinput_interface_backend interface_backend = {
350         .resume = udev_input_enable,
351         .suspend = udev_input_disable,
352         .destroy = udev_input_destroy,
353 };
354
355 LIBINPUT_EXPORT struct libinput *
356 libinput_create_from_udev(const struct libinput_interface *interface,
357                           void *user_data,
358                           struct udev *udev,
359                           const char *seat_id)
360 {
361         struct udev_input *input;
362
363         if (!interface || !udev || !seat_id)
364                 return NULL;
365
366         input = zalloc(sizeof *input);
367         if (!input)
368                 return NULL;
369
370         if (libinput_init(&input->base, interface,
371                           &interface_backend, user_data) != 0) {
372                 free(input);
373                 return NULL;
374         }
375
376         input->udev = udev_ref(udev);
377         input->seat_id = strdup(seat_id);
378
379         if (udev_input_enable(&input->base) < 0) {
380                 udev_unref(udev);
381                 libinput_destroy(&input->base);
382                 free(input);
383                 return NULL;
384         }
385
386         return &input->base;
387 }