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