Fix two doxygen errors
[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 = NULL;
159         char *seat_name = NULL, *seat_logical_name = NULL;
160
161         if (path_get_udev_properties(devnode, &sysname,
162                                      &seat_name, &seat_logical_name) == -1) {
163                 log_info(&input->base,
164                          "failed to obtain sysname for device '%s'.\n",
165                          devnode);
166                 return NULL;
167         }
168
169         seat = path_seat_get_named(input, seat_name, seat_logical_name);
170
171         if (seat) {
172                 libinput_seat_ref(&seat->base);
173         } else {
174                 seat = path_seat_create(input, seat_name, seat_logical_name);
175                 if (!seat) {
176                         log_info(&input->base,
177                                  "failed to create seat for device '%s'.\n",
178                                  devnode);
179                         goto out;
180                 }
181         }
182
183         device = evdev_device_create(&seat->base, devnode, sysname);
184         libinput_seat_unref(&seat->base);
185
186         if (device == EVDEV_UNHANDLED_DEVICE) {
187                 device = NULL;
188                 log_info(&input->base,
189                          "not using input device '%s'.\n",
190                          devnode);
191                 goto out;
192         } else if (device == NULL) {
193                 log_info(&input->base,
194                          "failed to create input device '%s'.\n",
195                          devnode);
196                 goto out;
197         }
198
199 out:
200         free(sysname);
201         free(seat_name);
202         free(seat_logical_name);
203
204         return device ? &device->base : NULL;
205 }
206
207 static int
208 path_input_enable(struct libinput *libinput)
209 {
210         struct path_input *input = (struct path_input*)libinput;
211         struct path_device *dev;
212
213         list_for_each(dev, &input->path_list, link) {
214                 if (path_device_enable(input, dev->path) == NULL) {
215                         path_input_disable(libinput);
216                         return -1;
217                 }
218         }
219
220         return 0;
221 }
222
223 static void
224 path_input_destroy(struct libinput *input)
225 {
226         struct path_input *path_input = (struct path_input*)input;
227         struct path_device *dev, *tmp;
228
229         list_for_each_safe(dev, tmp, &path_input->path_list, link) {
230                 free(dev->path);
231                 free(dev);
232         }
233
234 }
235
236 static const struct libinput_interface_backend interface_backend = {
237         .resume = path_input_enable,
238         .suspend = path_input_disable,
239         .destroy = path_input_destroy,
240 };
241
242 LIBINPUT_EXPORT struct libinput *
243 libinput_path_create_context(const struct libinput_interface *interface,
244                              void *user_data)
245 {
246         struct path_input *input;
247
248         if (!interface)
249                 return NULL;
250
251         input = zalloc(sizeof *input);
252         if (!input)
253                 return NULL;
254
255         if (libinput_init(&input->base, interface,
256                           &interface_backend, user_data) != 0) {
257                 free(input);
258                 return NULL;
259         }
260
261         list_init(&input->path_list);
262
263         return &input->base;
264 }
265
266 LIBINPUT_EXPORT struct libinput_device *
267 libinput_path_add_device(struct libinput *libinput,
268                          const char *path)
269 {
270         struct path_input *input = (struct path_input*)libinput;
271         struct path_device *dev;
272         struct libinput_device *device;
273
274         if (libinput->interface_backend != &interface_backend) {
275                 log_bug_client(libinput, "Mismatching backends.\n");
276                 return NULL;
277         }
278
279         dev = zalloc(sizeof *dev);
280         if (!dev)
281                 return NULL;
282
283         dev->path = strdup(path);
284         if (!dev->path) {
285                 free(dev);
286                 return NULL;
287         }
288
289         list_insert(&input->path_list, &dev->link);
290
291         device = path_device_enable(input, dev->path);
292
293         if (!device) {
294                 list_remove(&dev->link);
295                 free(dev->path);
296                 free(dev);
297         }
298
299         return device;
300 }
301
302 LIBINPUT_EXPORT void
303 libinput_path_remove_device(struct libinput_device *device)
304 {
305         struct libinput *libinput = device->seat->libinput;
306         struct path_input *input = (struct path_input*)libinput;
307         struct libinput_seat *seat;
308         struct evdev_device *evdev = (struct evdev_device*)device;
309         struct path_device *dev;
310
311         if (libinput->interface_backend != &interface_backend) {
312                 log_bug_client(libinput, "Mismatching backends.\n");
313                 return;
314         }
315
316         list_for_each(dev, &input->path_list, link) {
317                 if (strcmp(evdev->devnode, dev->path) == 0) {
318                         list_remove(&dev->link);
319                         free(dev->path);
320                         free(dev);
321                         break;
322                 }
323         }
324
325         seat = device->seat;
326         libinput_seat_ref(seat);
327         path_disable_device(libinput, evdev);
328         libinput_seat_unref(seat);
329 }