2 * Copyright © 2013 Intel Corporation
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.
28 #include "compositor.h"
29 #include "launcher-util.h"
31 #include "udev-seat.h"
33 static const char default_seat[] = "seat0";
36 device_added(struct udev_device *udev_device, struct udev_seat *master)
38 struct weston_compositor *c;
39 struct evdev_device *device;
41 const char *device_seat;
42 const char *calibration_values;
45 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
47 device_seat = default_seat;
49 if (strcmp(device_seat, master->seat_id))
52 c = master->base.compositor;
53 devnode = udev_device_get_devnode(udev_device);
55 /* Use non-blocking mode so that we can loop on read on
56 * evdev_device_data() until all events on the fd are
57 * read. mtdev_get() also expects this. */
58 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
60 weston_log("opening input device '%s' failed.\n", devnode);
64 device = evdev_device_create(&master->base, devnode, fd);
65 if (device == EVDEV_UNHANDLED_DEVICE) {
67 weston_log("not using input device '%s'.\n", devnode);
69 } else if (device == NULL) {
71 weston_log("failed to create input device '%s'.\n", devnode);
76 udev_device_get_property_value(udev_device,
79 if (calibration_values && sscanf(calibration_values,
81 &device->abs.calibration[0],
82 &device->abs.calibration[1],
83 &device->abs.calibration[2],
84 &device->abs.calibration[3],
85 &device->abs.calibration[4],
86 &device->abs.calibration[5]) == 6) {
87 device->abs.apply_calibration = 1;
88 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
89 device->abs.calibration[0],
90 device->abs.calibration[1],
91 device->abs.calibration[2],
92 device->abs.calibration[3],
93 device->abs.calibration[4],
94 device->abs.calibration[5]);
97 wl_list_insert(master->devices_list.prev, &device->link);
103 udev_seat_add_devices(struct udev_seat *seat, struct udev *udev)
105 struct udev_enumerate *e;
106 struct udev_list_entry *entry;
107 struct udev_device *device;
108 const char *path, *sysname;
110 e = udev_enumerate_new(udev);
111 udev_enumerate_add_match_subsystem(e, "input");
112 udev_enumerate_scan_devices(e);
113 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
114 path = udev_list_entry_get_name(entry);
115 device = udev_device_new_from_syspath(udev, path);
117 sysname = udev_device_get_sysname(device);
118 if (strncmp("event", sysname, 5) != 0) {
119 udev_device_unref(device);
123 if (device_added(device, seat) < 0) {
124 udev_device_unref(device);
125 udev_enumerate_unref(e);
129 udev_device_unref(device);
131 udev_enumerate_unref(e);
133 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
135 if (wl_list_empty(&seat->devices_list)) {
137 "warning: no input devices on entering Weston. "
139 "\t- no permissions to read /dev/input/event*\n"
140 "\t- seats misconfigured "
141 "(Weston backend option 'seat', "
142 "udev device property ID_SEAT)\n");
150 evdev_udev_handler(int fd, uint32_t mask, void *data)
152 struct udev_seat *seat = data;
153 struct udev_device *udev_device;
154 struct evdev_device *device, *next;
158 udev_device = udev_monitor_receive_device(seat->udev_monitor);
162 action = udev_device_get_action(udev_device);
166 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
169 if (!strcmp(action, "add")) {
170 device_added(udev_device, seat);
172 else if (!strcmp(action, "remove")) {
173 devnode = udev_device_get_devnode(udev_device);
174 wl_list_for_each_safe(device, next, &seat->devices_list, link)
175 if (!strcmp(device->devnode, devnode)) {
176 weston_log("input device %s, %s removed\n",
177 device->devname, device->devnode);
178 evdev_device_destroy(device);
184 udev_device_unref(udev_device);
190 udev_seat_enable(struct udev_seat *seat, struct udev *udev)
192 struct wl_event_loop *loop;
193 struct weston_compositor *c = seat->base.compositor;
196 seat->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
197 if (!seat->udev_monitor) {
198 weston_log("udev: failed to create the udev monitor\n");
202 udev_monitor_filter_add_match_subsystem_devtype(seat->udev_monitor,
205 if (udev_monitor_enable_receiving(seat->udev_monitor)) {
206 weston_log("udev: failed to bind the udev monitor\n");
207 udev_monitor_unref(seat->udev_monitor);
211 loop = wl_display_get_event_loop(c->wl_display);
212 fd = udev_monitor_get_fd(seat->udev_monitor);
213 seat->udev_monitor_source =
214 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
215 evdev_udev_handler, seat);
216 if (!seat->udev_monitor_source) {
217 udev_monitor_unref(seat->udev_monitor);
221 if (udev_seat_add_devices(seat, udev) < 0)
228 udev_seat_remove_devices(struct udev_seat *seat)
230 struct evdev_device *device, *next;
232 wl_list_for_each_safe(device, next, &seat->devices_list, link)
233 evdev_device_destroy(device);
235 if (seat->base.keyboard)
236 notify_keyboard_focus_out(&seat->base);
240 udev_seat_disable(struct udev_seat *seat)
242 if (!seat->udev_monitor)
245 udev_monitor_unref(seat->udev_monitor);
246 seat->udev_monitor = NULL;
247 wl_event_source_remove(seat->udev_monitor_source);
248 seat->udev_monitor_source = NULL;
250 udev_seat_remove_devices(seat);
254 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
256 struct udev_seat *seat = (struct udev_seat *) seat_base;
257 struct evdev_device *device;
259 wl_list_for_each(device, &seat->devices_list, link)
260 evdev_led_update(device, leds);
264 udev_seat_create(struct weston_compositor *c, struct udev *udev,
267 struct udev_seat *seat;
269 seat = malloc(sizeof *seat);
273 memset(seat, 0, sizeof *seat);
274 weston_seat_init(&seat->base, c, "default");
275 seat->base.led_update = drm_led_update;
277 wl_list_init(&seat->devices_list);
278 seat->seat_id = strdup(seat_id);
279 if (udev_seat_enable(seat, udev) < 0)
291 udev_seat_destroy(struct udev_seat *seat)
293 udev_seat_disable(seat);
295 weston_seat_release(&seat->base);