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(&input->base,
164 "failed to obtain sysname for device '%s'.\n",
169 seat = path_seat_get_named(input, seat_name, seat_logical_name);
172 libinput_seat_ref(&seat->base);
174 seat = path_seat_create(input, seat_name, seat_logical_name);
176 log_info(&input->base,
177 "failed to create seat for device '%s'.\n",
183 device = evdev_device_create(&seat->base, devnode, sysname);
184 libinput_seat_unref(&seat->base);
186 if (device == EVDEV_UNHANDLED_DEVICE) {
188 log_info(&input->base,
189 "not using input device '%s'.\n",
192 } else if (device == NULL) {
193 log_info(&input->base,
194 "failed to create input device '%s'.\n",
202 free(seat_logical_name);
204 return device ? &device->base : NULL;
208 path_input_enable(struct libinput *libinput)
210 struct path_input *input = (struct path_input*)libinput;
211 struct path_device *dev;
213 list_for_each(dev, &input->path_list, link) {
214 if (path_device_enable(input, dev->path) == NULL) {
215 path_input_disable(libinput);
224 path_input_destroy(struct libinput *input)
226 struct path_input *path_input = (struct path_input*)input;
227 struct path_device *dev, *tmp;
229 list_for_each_safe(dev, tmp, &path_input->path_list, link) {
236 static const struct libinput_interface_backend interface_backend = {
237 .resume = path_input_enable,
238 .suspend = path_input_disable,
239 .destroy = path_input_destroy,
242 LIBINPUT_EXPORT struct libinput *
243 libinput_path_create_context(const struct libinput_interface *interface,
246 struct path_input *input;
251 input = zalloc(sizeof *input);
255 if (libinput_init(&input->base, interface,
256 &interface_backend, user_data) != 0) {
261 list_init(&input->path_list);
266 LIBINPUT_EXPORT struct libinput_device *
267 libinput_path_add_device(struct libinput *libinput,
270 struct path_input *input = (struct path_input*)libinput;
271 struct path_device *dev;
272 struct libinput_device *device;
274 if (libinput->interface_backend != &interface_backend) {
275 log_bug_client(libinput, "Mismatching backends.\n");
279 dev = zalloc(sizeof *dev);
283 dev->path = strdup(path);
289 list_insert(&input->path_list, &dev->link);
291 device = path_device_enable(input, dev->path);
294 list_remove(&dev->link);
303 libinput_path_remove_device(struct libinput_device *device)
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;
311 if (libinput->interface_backend != &interface_backend) {
312 log_bug_client(libinput, "Mismatching backends.\n");
316 list_for_each(dev, &input->path_list, link) {
317 if (strcmp(evdev->devnode, dev->path) == 0) {
318 list_remove(&dev->link);
326 libinput_seat_ref(seat);
327 path_disable_device(libinput, evdev);
328 libinput_seat_unref(seat);