Make it possible to have persistent libinput_seat instances
[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 int
120 udev_input_add_devices(struct udev_input *input, struct udev *udev)
121 {
122         struct udev_enumerate *e;
123         struct udev_list_entry *entry;
124         struct udev_device *device;
125         const char *path, *sysname;
126
127         e = udev_enumerate_new(udev);
128         udev_enumerate_add_match_subsystem(e, "input");
129         udev_enumerate_scan_devices(e);
130         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
131                 path = udev_list_entry_get_name(entry);
132                 device = udev_device_new_from_syspath(udev, path);
133
134                 sysname = udev_device_get_sysname(device);
135                 if (strncmp("event", sysname, 5) != 0) {
136                         udev_device_unref(device);
137                         continue;
138                 }
139
140                 if (device_added(device, input) < 0) {
141                         udev_device_unref(device);
142                         udev_enumerate_unref(e);
143                         return -1;
144                 }
145
146                 udev_device_unref(device);
147         }
148         udev_enumerate_unref(e);
149
150         return 0;
151 }
152
153 static void
154 evdev_udev_handler(void *data)
155 {
156         struct udev_input *input = data;
157         struct udev_device *udev_device;
158         struct evdev_device *device, *next;
159         const char *action;
160         const char *devnode;
161         struct udev_seat *seat;
162
163         udev_device = udev_monitor_receive_device(input->udev_monitor);
164         if (!udev_device)
165                 return;
166
167         action = udev_device_get_action(udev_device);
168         if (!action)
169                 goto out;
170
171         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
172                 goto out;
173
174         if (!strcmp(action, "add")) {
175                 device_added(udev_device, input);
176         }
177         else if (!strcmp(action, "remove")) {
178                 devnode = udev_device_get_devnode(udev_device);
179                 list_for_each(seat, &input->base.seat_list, base.link) {
180                         list_for_each_safe(device, next,
181                                            &seat->base.devices_list, base.link)
182                                 if (!strcmp(device->devnode, devnode)) {
183                                         log_info("input device %s, %s removed\n",
184                                                  device->devname, device->devnode);
185                                         evdev_device_remove(device);
186                                         break;
187                                 }
188                 }
189         }
190
191 out:
192         udev_device_unref(udev_device);
193 }
194
195 static void
196 udev_input_remove_devices(struct udev_input *input)
197 {
198         struct evdev_device *device, *next;
199         struct udev_seat *seat, *tmp;
200
201         list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
202                 libinput_seat_ref(&seat->base);
203                 list_for_each_safe(device, next,
204                                    &seat->base.devices_list, base.link) {
205                         evdev_device_remove(device);
206                 }
207                 libinput_seat_unref(&seat->base);
208         }
209 }
210
211
212 static void
213 udev_input_disable(struct libinput *libinput)
214 {
215         struct udev_input *input = (struct udev_input*)libinput;
216
217         if (!input->udev_monitor)
218                 return;
219
220         udev_monitor_unref(input->udev_monitor);
221         input->udev_monitor = NULL;
222         libinput_remove_source(&input->base, input->udev_monitor_source);
223         input->udev_monitor_source = NULL;
224
225         udev_input_remove_devices(input);
226 }
227
228 static int
229 udev_input_enable(struct libinput *libinput)
230 {
231         struct udev_input *input = (struct udev_input*)libinput;
232         struct udev *udev = input->udev;
233         int fd;
234
235         if (input->udev_monitor)
236                 return 0;
237
238         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
239         if (!input->udev_monitor) {
240                 log_info("udev: failed to create the udev monitor\n");
241                 return -1;
242         }
243
244         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
245                         "input", NULL);
246
247         if (udev_monitor_enable_receiving(input->udev_monitor)) {
248                 log_info("udev: failed to bind the udev monitor\n");
249                 udev_monitor_unref(input->udev_monitor);
250                 input->udev_monitor = NULL;
251                 return -1;
252         }
253
254         fd = udev_monitor_get_fd(input->udev_monitor);
255         input->udev_monitor_source = libinput_add_fd(&input->base,
256                                                      fd,
257                                                      evdev_udev_handler,
258                                                      input);
259         if (!input->udev_monitor_source) {
260                 udev_monitor_unref(input->udev_monitor);
261                 input->udev_monitor = NULL;
262                 return -1;
263         }
264
265         if (udev_input_add_devices(input, udev) < 0) {
266                 udev_input_disable(libinput);
267                 return -1;
268         }
269
270         return 0;
271 }
272
273 static void
274 udev_input_destroy(struct libinput *input)
275 {
276         struct udev_input *udev_input = (struct udev_input*)input;
277
278         if (input == NULL)
279                 return;
280
281         udev_unref(udev_input->udev);
282         free(udev_input->seat_id);
283 }
284
285 static void
286 udev_seat_destroy(struct libinput_seat *seat)
287 {
288         struct udev_seat *useat = (struct udev_seat*)seat;
289         free(useat);
290 }
291
292 static struct udev_seat *
293 udev_seat_create(struct udev_input *input,
294                  const char *device_seat,
295                  const char *seat_name)
296 {
297         struct udev_seat *seat;
298
299         seat = zalloc(sizeof *seat);
300         if (!seat)
301                 return NULL;
302
303         libinput_seat_init(&seat->base, &input->base,
304                            device_seat, seat_name,
305                            udev_seat_destroy);
306
307         return seat;
308 }
309
310 static struct udev_seat *
311 udev_seat_get_named(struct udev_input *input, const char *seat_name)
312 {
313         struct udev_seat *seat;
314
315         list_for_each(seat, &input->base.seat_list, base.link) {
316                 if (strcmp(seat->base.logical_name, seat_name) == 0)
317                         return seat;
318         }
319
320         return NULL;
321 }
322
323 static const struct libinput_interface_backend interface_backend = {
324         .resume = udev_input_enable,
325         .suspend = udev_input_disable,
326         .destroy = udev_input_destroy,
327 };
328
329 LIBINPUT_EXPORT struct libinput *
330 libinput_udev_create_for_seat(const struct libinput_interface *interface,
331                               void *user_data,
332                               struct udev *udev,
333                               const char *seat_id)
334 {
335         struct udev_input *input;
336
337         if (!interface || !udev || !seat_id)
338                 return NULL;
339
340         input = zalloc(sizeof *input);
341         if (!input)
342                 return NULL;
343
344         if (libinput_init(&input->base, interface,
345                           &interface_backend, user_data) != 0) {
346                 free(input);
347                 return NULL;
348         }
349
350         input->udev = udev_ref(udev);
351         input->seat_id = strdup(seat_id);
352
353         if (udev_input_enable(&input->base) < 0) {
354                 udev_unref(udev);
355                 libinput_destroy(&input->base);
356                 free(input);
357                 return NULL;
358         }
359
360         return &input->base;
361 }