Rename libinput_create_udev to libinput_create_from_udev
[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                 udev_input_disable(input);
232                 return -1;
233         }
234
235         return 0;
236 }
237
238 static void
239 udev_input_remove_devices(struct udev_input *input)
240 {
241         struct evdev_device *device, *next;
242         struct udev_seat *seat;
243
244         list_for_each(seat, &input->base.seat_list, base.link) {
245                 list_for_each_safe(device, next,
246                                    &seat->base.devices_list, base.link) {
247                         close_restricted(&input->base, device->fd);
248                         evdev_device_remove(device);
249                 }
250         }
251 }
252
253 void
254 udev_input_disable(struct udev_input *input)
255 {
256         if (!input->udev_monitor)
257                 return;
258
259         udev_monitor_unref(input->udev_monitor);
260         input->udev_monitor = NULL;
261         libinput_remove_source(&input->base, input->udev_monitor_source);
262         input->udev_monitor_source = NULL;
263
264         udev_input_remove_devices(input);
265 }
266
267 void
268 udev_input_destroy(struct udev_input *input)
269 {
270         struct libinput_seat *seat, *next;
271         udev_input_disable(input);
272         list_for_each_safe(seat, next, &input->base.seat_list, link) {
273                 notify_removed_seat(seat);
274                 libinput_seat_unref(seat);
275         }
276         udev_unref(input->udev);
277         free(input->seat_id);
278 }
279
280 static struct udev_seat *
281 udev_seat_create(struct udev_input *input, const char *seat_name)
282 {
283         struct udev_seat *seat;
284
285         seat = zalloc(sizeof *seat);
286         if (!seat)
287                 return NULL;
288
289         libinput_seat_init(&seat->base, &input->base, seat_name);
290         list_insert(&input->base.seat_list, &seat->base.link);
291         notify_added_seat(&seat->base);
292
293         return seat;
294 }
295
296 void
297 udev_seat_destroy(struct udev_seat *seat)
298 {
299         list_remove(&seat->base.link);
300         free(seat);
301 }
302
303 static struct udev_seat *
304 udev_seat_get_named(struct udev_input *input, const char *seat_name)
305 {
306         struct udev_seat *seat;
307
308         list_for_each(seat, &input->base.seat_list, base.link) {
309                 if (strcmp(seat->base.name, seat_name) == 0)
310                         return seat;
311         }
312
313         seat = udev_seat_create(input, seat_name);
314
315         if (!seat)
316                 return NULL;
317
318         return seat;
319 }
320
321 LIBINPUT_EXPORT struct libinput *
322 libinput_create_from_udev(const struct libinput_interface *interface,
323                           void *user_data,
324                           struct udev *udev,
325                           const char *seat_id)
326 {
327         struct udev_input *input;
328
329         input = zalloc(sizeof *input);
330         if (!input)
331                 return NULL;
332
333         if (libinput_init(&input->base, interface, user_data) != 0) {
334                 free(input);
335                 return NULL;
336         }
337
338         input->udev = udev_ref(udev);
339         input->seat_id = strdup(seat_id);
340
341         if (udev_input_enable(input) < 0) {
342                 udev_unref(udev);
343                 libinput_destroy(&input->base);
344                 free(input);
345                 return NULL;
346         }
347
348         return &input->base;
349 }