2 * Copyright © 2013 Red Hat, Inc.
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.
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.
33 static const char default_seat[] = "seat0";
34 static const char default_seat_name[] = "default";
36 int path_input_process_event(struct libinput_event);
37 static void path_seat_destroy(struct libinput_seat *seat);
40 path_disable_device(struct libinput *libinput,
41 struct evdev_device *device)
43 struct libinput_seat *seat = device->base.seat;
44 struct evdev_device *dev, *next;
46 list_for_each_safe(dev, next,
47 &seat->devices_list, base.link) {
51 evdev_device_remove(device);
57 path_input_disable(struct libinput *libinput)
59 struct path_input *input = (struct path_input*)libinput;
60 struct path_seat *seat, *tmp;
61 struct evdev_device *device, *next;
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);
73 path_seat_destroy(struct libinput_seat *seat)
75 struct path_seat *pseat = (struct path_seat*)seat;
79 static struct path_seat*
80 path_seat_create(struct path_input *input,
81 const char *seat_name,
82 const char *seat_logical_name)
84 struct path_seat *seat;
86 seat = zalloc(sizeof(*seat));
90 libinput_seat_init(&seat->base, &input->base, seat_name,
91 seat_logical_name, path_seat_destroy);
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)
101 struct path_seat *seat;
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)
113 path_get_udev_properties(const char *path,
116 char **seat_logical_name)
118 struct udev *udev = NULL;
119 struct udev_device *device = NULL;
128 if (stat(path, &st) < 0)
131 device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
135 *sysname = strdup(udev_device_get_sysname(device));
137 seat = udev_device_get_property_value(device, "ID_SEAT");
138 *seat_name = strdup(seat ? seat : default_seat);
140 seat = udev_device_get_property_value(device, "WL_SEAT");
141 *seat_logical_name = strdup(seat ? seat : default_seat_name);
147 udev_device_unref(device);
153 static struct libinput_device *
154 path_device_enable(struct path_input *input, const char *devnode)
156 struct path_seat *seat;
157 struct evdev_device *device = NULL;
158 char *sysname = NULL;
159 char *seat_name = NULL, *seat_logical_name = NULL;
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);
167 seat = path_seat_get_named(input, seat_name, seat_logical_name);
170 libinput_seat_ref(&seat->base);
172 seat = path_seat_create(input, seat_name, seat_logical_name);
174 log_info("failed to create seat for device '%s'.\n", devnode);
179 device = evdev_device_create(&seat->base, devnode, sysname);
180 libinput_seat_unref(&seat->base);
182 if (device == EVDEV_UNHANDLED_DEVICE) {
184 log_info("not using input device '%s'.\n", devnode);
186 } else if (device == NULL) {
187 log_info("failed to create input device '%s'.\n", devnode);
194 free(seat_logical_name);
196 return device ? &device->base : NULL;
200 path_input_enable(struct libinput *libinput)
202 struct path_input *input = (struct path_input*)libinput;
203 struct path_device *dev;
205 list_for_each(dev, &input->path_list, link) {
206 if (path_device_enable(input, dev->path) == NULL) {
207 path_input_disable(libinput);
216 path_input_destroy(struct libinput *input)
218 struct path_input *path_input = (struct path_input*)input;
219 struct path_device *dev, *tmp;
221 list_for_each_safe(dev, tmp, &path_input->path_list, link) {
228 static const struct libinput_interface_backend interface_backend = {
229 .resume = path_input_enable,
230 .suspend = path_input_disable,
231 .destroy = path_input_destroy,
234 LIBINPUT_EXPORT struct libinput *
235 libinput_path_create_context(const struct libinput_interface *interface,
238 struct path_input *input;
243 input = zalloc(sizeof *input);
247 if (libinput_init(&input->base, interface,
248 &interface_backend, user_data) != 0) {
253 list_init(&input->path_list);
258 LIBINPUT_EXPORT struct libinput_device *
259 libinput_path_add_device(struct libinput *libinput,
262 struct path_input *input = (struct path_input*)libinput;
263 struct path_device *dev;
264 struct libinput_device *device;
266 if (libinput->interface_backend != &interface_backend) {
267 log_error("Mismatching backends. This is an application bug.\n");
271 dev = zalloc(sizeof *dev);
275 dev->path = strdup(path);
281 list_insert(&input->path_list, &dev->link);
283 device = path_device_enable(input, dev->path);
286 list_remove(&dev->link);
295 libinput_path_remove_device(struct libinput_device *device)
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;
303 if (libinput->interface_backend != &interface_backend) {
304 log_error("Mismatching backends. This is an application bug.\n");
308 list_for_each(dev, &input->path_list, link) {
309 if (strcmp(evdev->devnode, dev->path) == 0) {
310 list_remove(&dev->link);
318 libinput_seat_ref(seat);
319 path_disable_device(libinput, evdev);
320 libinput_seat_unref(seat);