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