Add a customizable log handler
[platform/upstream/libinput.git] / src / path.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
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 <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <libudev.h>
29
30 #include "path.h"
31 #include "evdev.h"
32
33 static const char default_seat[] = "seat0";
34 static const char default_seat_name[] = "default";
35
36 int path_input_process_event(struct libinput_event);
37 static void path_seat_destroy(struct libinput_seat *seat);
38
39 static void
40 path_disable_device(struct libinput *libinput,
41                     struct evdev_device *device)
42 {
43         struct libinput_seat *seat = device->base.seat;
44         struct evdev_device *dev, *next;
45
46         list_for_each_safe(dev, next,
47                            &seat->devices_list, base.link) {
48                 if (dev != device)
49                         continue;
50
51                 evdev_device_remove(device);
52                 break;
53         }
54 }
55
56 static void
57 path_input_disable(struct libinput *libinput)
58 {
59         struct path_input *input = (struct path_input*)libinput;
60         struct path_seat *seat, *tmp;
61         struct evdev_device *device, *next;
62
63         list_for_each_safe(seat, tmp, &input->base.seat_list, base.link) {
64                 libinput_seat_ref(&seat->base);
65                 list_for_each_safe(device, next,
66                                    &seat->base.devices_list, base.link)
67                         path_disable_device(libinput, device);
68                 libinput_seat_unref(&seat->base);
69         }
70 }
71
72 static void
73 path_seat_destroy(struct libinput_seat *seat)
74 {
75         struct path_seat *pseat = (struct path_seat*)seat;
76         free(pseat);
77 }
78
79 static struct path_seat*
80 path_seat_create(struct path_input *input,
81                  const char *seat_name,
82                  const char *seat_logical_name)
83 {
84         struct path_seat *seat;
85
86         seat = zalloc(sizeof(*seat));
87         if (!seat)
88                 return NULL;
89
90         libinput_seat_init(&seat->base, &input->base, seat_name,
91                            seat_logical_name, path_seat_destroy);
92
93         return seat;
94 }
95
96 static struct path_seat*
97 path_seat_get_named(struct path_input *input,
98                     const char *seat_name_physical,
99                     const char *seat_name_logical)
100 {
101         struct path_seat *seat;
102
103         list_for_each(seat, &input->base.seat_list, base.link) {
104                 if (strcmp(seat->base.physical_name, seat_name_physical) == 0 &&
105                     strcmp(seat->base.logical_name, seat_name_logical) == 0)
106                         return seat;
107         }
108
109         return NULL;
110 }
111
112 static int
113 path_get_udev_properties(const char *path,
114                          char **sysname,
115                          char **seat_name,
116                          char **seat_logical_name)
117 {
118         struct udev *udev = NULL;
119         struct udev_device *device = NULL;
120         struct stat st;
121         const char *seat;
122         int rc = -1;
123
124         udev = udev_new();
125         if (!udev)
126                 goto out;
127
128         if (stat(path, &st) < 0)
129                 goto out;
130
131         device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
132         if (!device)
133                 goto out;
134
135         *sysname = strdup(udev_device_get_sysname(device));
136
137         seat = udev_device_get_property_value(device, "ID_SEAT");
138         *seat_name = strdup(seat ? seat : default_seat);
139
140         seat = udev_device_get_property_value(device, "WL_SEAT");
141         *seat_logical_name = strdup(seat ? seat : default_seat_name);
142
143         rc = 0;
144
145 out:
146         if (device)
147                 udev_device_unref(device);
148         if (udev)
149                 udev_unref(udev);
150         return rc;
151 }
152
153 static struct libinput_device *
154 path_device_enable(struct path_input *input, const char *devnode)
155 {
156         struct path_seat *seat;
157         struct evdev_device *device = NULL;
158         char *sysname;
159         char *seat_name, *seat_logical_name;
160
161         if (path_get_udev_properties(devnode, &sysname,
162                                      &seat_name, &seat_logical_name) == -1) {
163                 log_info("failed to obtain sysname for device '%s'.\n", devnode);
164                 return NULL;
165         }
166
167         seat = path_seat_get_named(input, seat_name, seat_logical_name);
168
169         if (seat) {
170                 libinput_seat_ref(&seat->base);
171         } else {
172                 seat = path_seat_create(input, seat_name, seat_logical_name);
173                 if (!seat) {
174                         log_info("failed to create seat for device '%s'.\n", devnode);
175                         goto out;
176                 }
177         }
178
179         device = evdev_device_create(&seat->base, devnode, sysname);
180         libinput_seat_unref(&seat->base);
181
182         if (device == EVDEV_UNHANDLED_DEVICE) {
183                 device = NULL;
184                 log_info("not using input device '%s'.\n", devnode);
185                 goto out;
186         } else if (device == NULL) {
187                 log_info("failed to create input device '%s'.\n", devnode);
188                 goto out;
189         }
190
191 out:
192         free(sysname);
193         free(seat_name);
194         free(seat_logical_name);
195
196         return device ? &device->base : NULL;
197 }
198
199 static int
200 path_input_enable(struct libinput *libinput)
201 {
202         struct path_input *input = (struct path_input*)libinput;
203         struct path_device *dev;
204
205         list_for_each(dev, &input->path_list, link) {
206                 if (path_device_enable(input, dev->path) == NULL) {
207                         path_input_disable(libinput);
208                         return -1;
209                 }
210         }
211
212         return 0;
213 }
214
215 static void
216 path_input_destroy(struct libinput *input)
217 {
218         struct path_input *path_input = (struct path_input*)input;
219         struct path_device *dev, *tmp;
220
221         list_for_each_safe(dev, tmp, &path_input->path_list, link) {
222                 free(dev->path);
223                 free(dev);
224         }
225
226 }
227
228 static const struct libinput_interface_backend interface_backend = {
229         .resume = path_input_enable,
230         .suspend = path_input_disable,
231         .destroy = path_input_destroy,
232 };
233
234 LIBINPUT_EXPORT struct libinput *
235 libinput_path_create_context(const struct libinput_interface *interface,
236                              void *user_data)
237 {
238         struct path_input *input;
239
240         if (!interface)
241                 return NULL;
242
243         input = zalloc(sizeof *input);
244         if (!input)
245                 return NULL;
246
247         if (libinput_init(&input->base, interface,
248                           &interface_backend, user_data) != 0) {
249                 free(input);
250                 return NULL;
251         }
252
253         list_init(&input->path_list);
254
255         return &input->base;
256 }
257
258 LIBINPUT_EXPORT struct libinput_device *
259 libinput_path_add_device(struct libinput *libinput,
260                          const char *path)
261 {
262         struct path_input *input = (struct path_input*)libinput;
263         struct path_device *dev;
264         struct libinput_device *device;
265
266         if (libinput->interface_backend != &interface_backend) {
267                 log_error("Mismatching backends. This is an application bug.\n");
268                 return NULL;
269         }
270
271         dev = zalloc(sizeof *dev);
272         if (!dev)
273                 return NULL;
274
275         dev->path = strdup(path);
276         if (!dev->path) {
277                 free(dev);
278                 return NULL;
279         }
280
281         list_insert(&input->path_list, &dev->link);
282
283         device = path_device_enable(input, dev->path);
284
285         if (!device) {
286                 list_remove(&dev->link);
287                 free(dev->path);
288                 free(dev);
289         }
290
291         return device;
292 }
293
294 LIBINPUT_EXPORT void
295 libinput_path_remove_device(struct libinput_device *device)
296 {
297         struct libinput *libinput = device->seat->libinput;
298         struct path_input *input = (struct path_input*)libinput;
299         struct libinput_seat *seat;
300         struct evdev_device *evdev = (struct evdev_device*)device;
301         struct path_device *dev;
302
303         if (libinput->interface_backend != &interface_backend) {
304                 log_error("Mismatching backends. This is an application bug.\n");
305                 return;
306         }
307
308         list_for_each(dev, &input->path_list, link) {
309                 if (strcmp(evdev->devnode, dev->path) == 0) {
310                         list_remove(&dev->link);
311                         free(dev->path);
312                         free(dev);
313                         break;
314                 }
315         }
316
317         seat = device->seat;
318         libinput_seat_ref(seat);
319         path_disable_device(libinput, evdev);
320         libinput_seat_unref(seat);
321 }