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;
48 struct weston_output *output;
50 const char *device_seat, *seat_name, *output_name;
51 const char *calibration_values;
53 struct udev_seat *seat;
55 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
57 device_seat = default_seat;
59 if (strcmp(device_seat, input->seat_id))
62 c = input->compositor;
63 devnode = udev_device_get_devnode(udev_device);
65 /* Search for matching logical seat */
66 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
68 seat_name = default_seat_name;
70 seat = udev_seat_get_named(c, seat_name);
75 /* Use non-blocking mode so that we can loop on read on
76 * evdev_device_data() until all events on the fd are
77 * read. mtdev_get() also expects this. */
78 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
80 weston_log("opening input device '%s' failed.\n", devnode);
84 device = evdev_device_create(&seat->base, devnode, fd);
85 if (device == EVDEV_UNHANDLED_DEVICE) {
87 weston_log("not using input device '%s'.\n", devnode);
89 } else if (device == NULL) {
91 weston_log("failed to create input device '%s'.\n", devnode);
96 udev_device_get_property_value(udev_device,
99 if (calibration_values && sscanf(calibration_values,
101 &device->abs.calibration[0],
102 &device->abs.calibration[1],
103 &device->abs.calibration[2],
104 &device->abs.calibration[3],
105 &device->abs.calibration[4],
106 &device->abs.calibration[5]) == 6) {
107 device->abs.apply_calibration = 1;
108 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
109 device->abs.calibration[0],
110 device->abs.calibration[1],
111 device->abs.calibration[2],
112 device->abs.calibration[3],
113 device->abs.calibration[4],
114 device->abs.calibration[5]);
117 wl_list_insert(seat->devices_list.prev, &device->link);
119 if (seat->base.output && seat->base.pointer)
120 weston_pointer_clamp(seat->base.pointer,
121 &seat->base.pointer->x,
122 &seat->base.pointer->y);
124 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
126 wl_list_for_each(output, &c->output_list, link)
127 if (strcmp(output->name, output_name) == 0)
128 device->output = output;
135 udev_input_add_devices(struct udev_input *input, struct udev *udev)
137 struct udev_enumerate *e;
138 struct udev_list_entry *entry;
139 struct udev_device *device;
140 const char *path, *sysname;
141 struct udev_seat *seat;
142 int devices_found = 0;
144 e = udev_enumerate_new(udev);
145 udev_enumerate_add_match_subsystem(e, "input");
146 udev_enumerate_scan_devices(e);
147 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
148 path = udev_list_entry_get_name(entry);
149 device = udev_device_new_from_syspath(udev, path);
151 sysname = udev_device_get_sysname(device);
152 if (strncmp("event", sysname, 5) != 0) {
153 udev_device_unref(device);
157 if (device_added(device, input) < 0) {
158 udev_device_unref(device);
159 udev_enumerate_unref(e);
163 udev_device_unref(device);
165 udev_enumerate_unref(e);
167 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
168 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
170 if (!wl_list_empty(&seat->devices_list))
174 if (devices_found == 0) {
176 "warning: no input devices on entering Weston. "
178 "\t- no permissions to read /dev/input/event*\n"
179 "\t- seats misconfigured "
180 "(Weston backend option 'seat', "
181 "udev device property ID_SEAT)\n");
189 evdev_udev_handler(int fd, uint32_t mask, void *data)
191 struct udev_input *input = data;
192 struct udev_device *udev_device;
193 struct evdev_device *device, *next;
196 struct udev_seat *seat;
198 udev_device = udev_monitor_receive_device(input->udev_monitor);
202 action = udev_device_get_action(udev_device);
206 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
209 if (!strcmp(action, "add")) {
210 device_added(udev_device, input);
212 else if (!strcmp(action, "remove")) {
213 devnode = udev_device_get_devnode(udev_device);
214 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
215 wl_list_for_each_safe(device, next, &seat->devices_list, link)
216 if (!strcmp(device->devnode, devnode)) {
217 weston_log("input device %s, %s removed\n",
218 device->devname, device->devnode);
219 evdev_device_destroy(device);
226 udev_device_unref(udev_device);
232 udev_input_enable(struct udev_input *input, struct udev *udev)
234 struct wl_event_loop *loop;
235 struct weston_compositor *c = input->compositor;
238 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
239 if (!input->udev_monitor) {
240 weston_log("udev: failed to create the udev monitor\n");
244 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
247 if (udev_monitor_enable_receiving(input->udev_monitor)) {
248 weston_log("udev: failed to bind the udev monitor\n");
249 udev_monitor_unref(input->udev_monitor);
253 loop = wl_display_get_event_loop(c->wl_display);
254 fd = udev_monitor_get_fd(input->udev_monitor);
255 input->udev_monitor_source =
256 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
257 evdev_udev_handler, input);
258 if (!input->udev_monitor_source) {
259 udev_monitor_unref(input->udev_monitor);
263 if (udev_input_add_devices(input, udev) < 0)
270 udev_input_remove_devices(struct udev_input *input)
272 struct evdev_device *device, *next;
273 struct udev_seat *seat;
275 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
276 wl_list_for_each_safe(device, next, &seat->devices_list, link)
277 evdev_device_destroy(device);
279 if (seat->base.keyboard)
280 notify_keyboard_focus_out(&seat->base);
285 udev_input_disable(struct udev_input *input)
287 if (!input->udev_monitor)
290 udev_monitor_unref(input->udev_monitor);
291 input->udev_monitor = NULL;
292 wl_event_source_remove(input->udev_monitor_source);
293 input->udev_monitor_source = NULL;
295 udev_input_remove_devices(input);
300 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
303 memset(input, 0, sizeof *input);
304 input->seat_id = strdup(seat_id);
305 input->compositor = c;
306 if (udev_input_enable(input, udev) < 0)
312 free(input->seat_id);
317 udev_input_destroy(struct udev_input *input)
319 struct udev_seat *seat, *next;
320 udev_input_disable(input);
321 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
322 udev_seat_destroy(seat);
323 free(input->seat_id);
327 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
329 struct udev_seat *seat = (struct udev_seat *) seat_base;
330 struct evdev_device *device;
332 wl_list_for_each(device, &seat->devices_list, link)
333 evdev_led_update(device, leds);
336 static struct udev_seat *
337 udev_seat_create(struct weston_compositor *c, const char *seat_name)
339 struct udev_seat *seat;
341 seat = zalloc(sizeof *seat);
345 weston_seat_init(&seat->base, c, seat_name);
346 seat->base.led_update = drm_led_update;
348 wl_list_init(&seat->devices_list);
353 udev_seat_destroy(struct udev_seat *seat)
355 weston_seat_release(&seat->base);
360 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
362 struct udev_seat *seat;
364 wl_list_for_each(seat, &c->seat_list, base.link) {
365 if (strcmp(seat->base.seat_name, seat_name) == 0)
369 seat = udev_seat_create(c, seat_name);