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