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