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