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.
30 #include "compositor.h"
31 #include "launcher-util.h"
33 #include "udev-seat.h"
35 static const char default_seat[] = "seat0";
36 static const char default_seat_name[] = "default";
38 static struct udev_seat *
39 udev_seat_create(struct weston_compositor *c, const char *seat_name);
41 udev_seat_destroy(struct udev_seat *seat);
44 device_added(struct udev_device *udev_device, struct udev_input *input)
46 struct weston_compositor *c;
47 struct evdev_device *device;
49 const char *device_seat, *seat_name;
50 const char *calibration_values;
52 struct udev_seat *seat;
54 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
56 device_seat = default_seat;
58 if (strcmp(device_seat, input->seat_id))
61 c = input->compositor;
62 devnode = udev_device_get_devnode(udev_device);
64 /* Search for matching logical seat */
65 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
67 seat_name = default_seat_name;
69 seat = udev_seat_get_named(c, seat_name);
74 /* Use non-blocking mode so that we can loop on read on
75 * evdev_device_data() until all events on the fd are
76 * read. mtdev_get() also expects this. */
77 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
79 weston_log("opening input device '%s' failed.\n", devnode);
83 device = evdev_device_create(&seat->base, devnode, fd);
84 if (device == EVDEV_UNHANDLED_DEVICE) {
86 weston_log("not using input device '%s'.\n", devnode);
88 } else if (device == NULL) {
90 weston_log("failed to create input device '%s'.\n", devnode);
95 udev_device_get_property_value(udev_device,
98 if (calibration_values && sscanf(calibration_values,
100 &device->abs.calibration[0],
101 &device->abs.calibration[1],
102 &device->abs.calibration[2],
103 &device->abs.calibration[3],
104 &device->abs.calibration[4],
105 &device->abs.calibration[5]) == 6) {
106 device->abs.apply_calibration = 1;
107 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
108 device->abs.calibration[0],
109 device->abs.calibration[1],
110 device->abs.calibration[2],
111 device->abs.calibration[3],
112 device->abs.calibration[4],
113 device->abs.calibration[5]);
116 wl_list_insert(seat->devices_list.prev, &device->link);
122 udev_input_add_devices(struct udev_input *input, struct udev *udev)
124 struct udev_enumerate *e;
125 struct udev_list_entry *entry;
126 struct udev_device *device;
127 const char *path, *sysname;
128 struct udev_seat *seat;
129 int devices_found = 0;
131 e = udev_enumerate_new(udev);
132 udev_enumerate_add_match_subsystem(e, "input");
133 udev_enumerate_scan_devices(e);
134 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
135 path = udev_list_entry_get_name(entry);
136 device = udev_device_new_from_syspath(udev, path);
138 sysname = udev_device_get_sysname(device);
139 if (strncmp("event", sysname, 5) != 0) {
140 udev_device_unref(device);
144 if (device_added(device, input) < 0) {
145 udev_device_unref(device);
146 udev_enumerate_unref(e);
150 udev_device_unref(device);
152 udev_enumerate_unref(e);
154 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
155 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
157 if (!wl_list_empty(&seat->devices_list))
161 if (devices_found == 0) {
163 "warning: no input devices on entering Weston. "
165 "\t- no permissions to read /dev/input/event*\n"
166 "\t- seats misconfigured "
167 "(Weston backend option 'seat', "
168 "udev device property ID_SEAT)\n");
176 evdev_udev_handler(int fd, uint32_t mask, void *data)
178 struct udev_input *input = data;
179 struct udev_device *udev_device;
180 struct evdev_device *device, *next;
183 struct udev_seat *seat;
185 udev_device = udev_monitor_receive_device(input->udev_monitor);
189 action = udev_device_get_action(udev_device);
193 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
196 if (!strcmp(action, "add")) {
197 device_added(udev_device, input);
199 else if (!strcmp(action, "remove")) {
200 devnode = udev_device_get_devnode(udev_device);
201 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
202 wl_list_for_each_safe(device, next, &seat->devices_list, link)
203 if (!strcmp(device->devnode, devnode)) {
204 weston_log("input device %s, %s removed\n",
205 device->devname, device->devnode);
206 evdev_device_destroy(device);
213 udev_device_unref(udev_device);
219 udev_input_enable(struct udev_input *input, struct udev *udev)
221 struct wl_event_loop *loop;
222 struct weston_compositor *c = input->compositor;
225 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
226 if (!input->udev_monitor) {
227 weston_log("udev: failed to create the udev monitor\n");
231 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
234 if (udev_monitor_enable_receiving(input->udev_monitor)) {
235 weston_log("udev: failed to bind the udev monitor\n");
236 udev_monitor_unref(input->udev_monitor);
240 loop = wl_display_get_event_loop(c->wl_display);
241 fd = udev_monitor_get_fd(input->udev_monitor);
242 input->udev_monitor_source =
243 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
244 evdev_udev_handler, input);
245 if (!input->udev_monitor_source) {
246 udev_monitor_unref(input->udev_monitor);
250 if (udev_input_add_devices(input, udev) < 0)
257 udev_input_remove_devices(struct udev_input *input)
259 struct evdev_device *device, *next;
260 struct udev_seat *seat;
262 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
263 wl_list_for_each_safe(device, next, &seat->devices_list, link)
264 evdev_device_destroy(device);
266 if (seat->base.keyboard)
267 notify_keyboard_focus_out(&seat->base);
272 udev_input_disable(struct udev_input *input)
274 if (!input->udev_monitor)
277 udev_monitor_unref(input->udev_monitor);
278 input->udev_monitor = NULL;
279 wl_event_source_remove(input->udev_monitor_source);
280 input->udev_monitor_source = NULL;
282 udev_input_remove_devices(input);
287 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
290 memset(input, 0, sizeof *input);
291 input->seat_id = strdup(seat_id);
292 input->compositor = c;
293 if (udev_input_enable(input, udev) < 0)
299 free(input->seat_id);
304 udev_input_destroy(struct udev_input *input)
306 struct udev_seat *seat, *next;
307 udev_input_disable(input);
308 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
309 udev_seat_destroy(seat);
310 free(input->seat_id);
314 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
316 struct udev_seat *seat = (struct udev_seat *) seat_base;
317 struct evdev_device *device;
319 wl_list_for_each(device, &seat->devices_list, link)
320 evdev_led_update(device, leds);
323 static struct udev_seat *
324 udev_seat_create(struct weston_compositor *c, const char *seat_name)
326 struct udev_seat *seat;
328 seat = malloc(sizeof *seat);
332 memset(seat, 0, sizeof *seat);
333 weston_seat_init(&seat->base, c, seat_name);
334 seat->base.led_update = drm_led_update;
336 wl_list_init(&seat->devices_list);
341 udev_seat_destroy(struct udev_seat *seat)
343 weston_seat_release(&seat->base);
348 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
350 struct udev_seat *seat;
352 wl_list_for_each(seat, &c->seat_list, base.link) {
353 if (strcmp(seat->base.seat_name, seat_name) == 0)
357 seat = udev_seat_create(c, seat_name);