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[] = "seat0";
39 udev_seat_destroy(struct udev_seat *seat);
42 udev_seat_has_output(struct udev_seat *seat)
44 struct weston_output *output;
45 wl_list_for_each(output, &seat->base.compositor->output_list, link) {
46 if (seat && (&seat->base == output->seat_data.seat))
53 device_added(struct udev_device *udev_device, struct udev_input *input)
55 struct weston_compositor *c;
56 struct evdev_device *device;
57 struct weston_output *output;
59 const char *device_seat, *seat_name, *output_name;
60 const char *calibration_values;
62 struct udev_seat *seat;
64 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
66 device_seat = default_seat;
68 if (strcmp(device_seat, input->seat_id))
71 c = input->compositor;
72 devnode = udev_device_get_devnode(udev_device);
74 /* Search for matching logical seat */
75 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
77 seat_name = default_seat_name;
79 seat = udev_seat_get_named(c, seat_name);
84 /* Use non-blocking mode so that we can loop on read on
85 * evdev_device_data() until all events on the fd are
86 * read. mtdev_get() also expects this. */
87 fd = weston_launcher_open(c->launcher, devnode, O_RDWR | O_NONBLOCK);
89 weston_log("opening input device '%s' failed.\n", devnode);
93 device = evdev_device_create(&seat->base, devnode, fd);
94 if (device == EVDEV_UNHANDLED_DEVICE) {
95 weston_launcher_close(c->launcher, fd);
96 weston_log("not using input device '%s'.\n", devnode);
98 } else if (device == NULL) {
99 weston_launcher_close(c->launcher, fd);
100 weston_log("failed to create input device '%s'.\n", devnode);
105 udev_device_get_property_value(udev_device,
108 if (calibration_values && sscanf(calibration_values,
110 &device->abs.calibration[0],
111 &device->abs.calibration[1],
112 &device->abs.calibration[2],
113 &device->abs.calibration[3],
114 &device->abs.calibration[4],
115 &device->abs.calibration[5]) == 6) {
116 device->abs.apply_calibration = 1;
117 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
118 device->abs.calibration[0],
119 device->abs.calibration[1],
120 device->abs.calibration[2],
121 device->abs.calibration[3],
122 device->abs.calibration[4],
123 device->abs.calibration[5]);
126 wl_list_insert(seat->devices_list.prev, &device->link);
128 if (udev_seat_has_output(seat) && seat->base.pointer)
129 weston_pointer_clamp(seat->base.pointer,
130 &seat->base.pointer->x,
131 &seat->base.pointer->y);
133 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
135 device->output_name = strdup(output_name);
136 wl_list_for_each(output, &c->output_list, link)
137 if (strcmp(output->name, device->output_name) == 0)
138 evdev_device_set_output(device, output);
139 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
140 output = container_of(c->output_list.next,
141 struct weston_output, link);
142 evdev_device_set_output(device, output);
145 if (input->enabled == 1)
146 weston_seat_repick(&seat->base);
148 weston_log("device with path %s is added to seat %s\n", devnode,
154 udev_input_add_devices(struct udev_input *input, struct udev *udev)
156 struct udev_enumerate *e;
157 struct udev_list_entry *entry;
158 struct udev_device *device;
159 const char *path, *sysname;
160 struct udev_seat *seat;
161 int devices_found = 0;
163 e = udev_enumerate_new(udev);
164 udev_enumerate_add_match_subsystem(e, "input");
165 udev_enumerate_scan_devices(e);
166 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
167 path = udev_list_entry_get_name(entry);
168 device = udev_device_new_from_syspath(udev, path);
170 sysname = udev_device_get_sysname(device);
171 if (strncmp("event", sysname, 5) != 0) {
172 udev_device_unref(device);
176 if (device_added(device, input) < 0) {
177 udev_device_unref(device);
178 udev_enumerate_unref(e);
182 udev_device_unref(device);
184 udev_enumerate_unref(e);
186 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
187 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
189 if (!wl_list_empty(&seat->devices_list))
193 if (devices_found == 0) {
195 "warning: no input devices on entering Weston. "
197 "\t- no permissions to read /dev/input/event*\n"
198 "\t- seats misconfigured "
199 "(Weston backend option 'seat', "
200 "udev device property ID_SEAT)\n");
208 evdev_udev_handler(int fd, uint32_t mask, void *data)
210 struct udev_input *input = data;
211 struct udev_device *udev_device;
212 struct evdev_device *device, *next;
215 struct udev_seat *seat;
217 udev_device = udev_monitor_receive_device(input->udev_monitor);
221 action = udev_device_get_action(udev_device);
225 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
228 if (!strcmp(action, "add")) {
229 device_added(udev_device, input);
231 else if (!strcmp(action, "remove")) {
232 devnode = udev_device_get_devnode(udev_device);
233 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
234 wl_list_for_each_safe(device, next, &seat->devices_list, link)
235 if (!strcmp(device->devnode, devnode)) {
236 weston_log("input device %s, %s removed\n",
237 device->devname, device->devnode);
238 weston_launcher_close(input->compositor->launcher,
240 evdev_device_destroy(device);
247 udev_device_unref(udev_device);
253 udev_input_enable(struct udev_input *input)
255 struct wl_event_loop *loop;
256 struct weston_compositor *c = input->compositor;
259 input->udev_monitor = udev_monitor_new_from_netlink(input->udev, "udev");
260 if (!input->udev_monitor) {
261 weston_log("udev: failed to create the udev monitor\n");
265 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
268 if (udev_monitor_enable_receiving(input->udev_monitor)) {
269 weston_log("udev: failed to bind the udev monitor\n");
270 udev_monitor_unref(input->udev_monitor);
274 loop = wl_display_get_event_loop(c->wl_display);
275 fd = udev_monitor_get_fd(input->udev_monitor);
276 input->udev_monitor_source =
277 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
278 evdev_udev_handler, input);
279 if (!input->udev_monitor_source) {
280 udev_monitor_unref(input->udev_monitor);
284 if (udev_input_add_devices(input, input->udev) < 0)
293 udev_input_remove_devices(struct udev_input *input)
295 struct evdev_device *device, *next;
296 struct udev_seat *seat;
298 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
299 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
300 weston_launcher_close(input->compositor->launcher,
302 evdev_device_destroy(device);
305 if (seat->base.keyboard)
306 notify_keyboard_focus_out(&seat->base);
311 udev_input_disable(struct udev_input *input)
313 if (!input->udev_monitor)
316 udev_monitor_unref(input->udev_monitor);
317 input->udev_monitor = NULL;
318 wl_event_source_remove(input->udev_monitor_source);
319 input->udev_monitor_source = NULL;
321 udev_input_remove_devices(input);
326 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
329 memset(input, 0, sizeof *input);
330 input->seat_id = strdup(seat_id);
331 input->compositor = c;
332 input->udev = udev_ref(udev);
333 if (udev_input_enable(input) < 0)
343 udev_input_destroy(struct udev_input *input)
345 struct udev_seat *seat, *next;
346 udev_input_disable(input);
347 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
348 udev_seat_destroy(seat);
349 udev_unref(input->udev);
350 free(input->seat_id);
354 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
356 struct udev_seat *seat = (struct udev_seat *) seat_base;
357 struct evdev_device *device, *next;
359 wl_list_for_each_safe(device, next, &seat->devices_list, link)
360 evdev_led_update(device, leds);
364 udev_seat_is_output_linked(struct udev_seat *seat, struct weston_output *output)
366 if (seat && output->seat_data.seat &&
367 (&seat->base == output->seat_data.seat)) {
368 weston_log("output seatname (%s) is linked already with seat\n",
369 output->seat_data.seatname);
376 udev_seat_link_output(struct udev_seat *seat, struct weston_output *output)
378 if (udev_seat_is_output_linked(seat, output)) return 1;
380 if (!seat || !output || !seat->base.seat_name ||
381 !output->seat_data.seatname ||
382 strcmp(seat->base.seat_name, output->seat_data.seatname) != 0) {
386 weston_log("link output to seat %s\n", seat->base.seat_name);
387 free(output->seat_data.seatname);
388 output->seat_data.seatname = strdup(seat->base.seat_name);
389 output->seat_data.seat = &seat->base;
394 notify_output_create(struct wl_listener *listener, void *data)
396 struct udev_seat *seat = container_of(listener, struct udev_seat,
397 output_create_listener);
398 struct evdev_device *device, *next;
399 struct weston_output *output = data;
401 weston_log("notify received for output created for seat %s\n",
402 seat->base.seat_name);
403 if (!udev_seat_link_output(seat, output))
406 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
407 if (device->output_name && output->name &&
408 strcmp(output->name, device->output_name) == 0) {
409 evdev_device_set_output(device, output);
412 if (device->output_name == NULL && device->output == NULL)
413 evdev_device_set_output(device, output);
417 static struct udev_seat *
418 udev_seat_create(struct weston_compositor *c, const char *seat_name)
420 struct udev_seat *seat;
422 seat = zalloc(sizeof *seat);
426 weston_seat_init(&seat->base, c, seat_name);
427 seat->base.led_update = drm_led_update;
429 seat->output_create_listener.notify = notify_output_create;
430 wl_signal_add(&c->output_created_signal,
431 &seat->output_create_listener);
433 wl_list_init(&seat->devices_list);
434 weston_log("created seat with name %s\n", seat_name);
440 udev_seat_destroy(struct udev_seat *seat)
442 weston_log("seat destroyed with name %s\n", seat->base.seat_name);
443 weston_seat_release(&seat->base);
444 wl_list_remove(&seat->output_create_listener.link);
449 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
451 struct udev_seat *seat, *next;
453 wl_list_for_each_safe(seat, next, &c->seat_list, base.link) {
454 if (strcmp(seat->base.seat_name, seat_name) == 0)
458 seat = udev_seat_create(c, seat_name);