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";
39 udev_seat_destroy(struct udev_seat *seat);
42 device_added(struct udev_device *udev_device, struct udev_input *input)
44 struct weston_compositor *c;
45 struct evdev_device *device;
46 struct weston_output *output;
48 const char *device_seat, *seat_name, *output_name;
49 const char *calibration_values;
51 struct udev_seat *seat;
53 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
55 device_seat = default_seat;
57 if (strcmp(device_seat, input->seat_id))
60 c = input->compositor;
61 devnode = udev_device_get_devnode(udev_device);
63 /* Search for matching logical seat */
64 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
66 seat_name = default_seat_name;
68 seat = udev_seat_get_named(input, seat_name);
73 /* Use non-blocking mode so that we can loop on read on
74 * evdev_device_data() until all events on the fd are
75 * read. mtdev_get() also expects this. */
76 fd = weston_launcher_open(c->launcher, devnode, O_RDWR | O_NONBLOCK);
78 weston_log("opening input device '%s' failed.\n", devnode);
82 device = evdev_device_create(&seat->base, devnode, fd);
83 if (device == EVDEV_UNHANDLED_DEVICE) {
84 weston_launcher_close(c->launcher, fd);
85 weston_log("not using input device '%s'.\n", devnode);
87 } else if (device == NULL) {
88 weston_launcher_close(c->launcher, fd);
89 weston_log("failed to create input device '%s'.\n", devnode);
94 udev_device_get_property_value(udev_device,
97 if (calibration_values && sscanf(calibration_values,
99 &device->abs.calibration[0],
100 &device->abs.calibration[1],
101 &device->abs.calibration[2],
102 &device->abs.calibration[3],
103 &device->abs.calibration[4],
104 &device->abs.calibration[5]) == 6) {
105 device->abs.apply_calibration = 1;
106 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
107 device->abs.calibration[0],
108 device->abs.calibration[1],
109 device->abs.calibration[2],
110 device->abs.calibration[3],
111 device->abs.calibration[4],
112 device->abs.calibration[5]);
115 wl_list_insert(seat->devices_list.prev, &device->link);
117 if (seat->base.output && seat->base.pointer)
118 weston_pointer_clamp(seat->base.pointer,
119 &seat->base.pointer->x,
120 &seat->base.pointer->y);
122 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
124 device->output_name = strdup(output_name);
125 wl_list_for_each(output, &c->output_list, link)
126 if (strcmp(output->name, device->output_name) == 0)
127 evdev_device_set_output(device, output);
128 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
129 output = container_of(c->output_list.next,
130 struct weston_output, link);
131 evdev_device_set_output(device, output);
134 if (input->enabled == 1)
135 weston_seat_repick(&seat->base);
141 udev_input_add_devices(struct udev_input *input, struct udev *udev)
143 struct udev_enumerate *e;
144 struct udev_list_entry *entry;
145 struct udev_device *device;
146 const char *path, *sysname;
147 struct udev_seat *seat;
148 int devices_found = 0;
150 e = udev_enumerate_new(udev);
151 udev_enumerate_add_match_subsystem(e, "input");
152 udev_enumerate_scan_devices(e);
153 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
154 path = udev_list_entry_get_name(entry);
155 device = udev_device_new_from_syspath(udev, path);
157 sysname = udev_device_get_sysname(device);
158 if (strncmp("event", sysname, 5) != 0) {
159 udev_device_unref(device);
163 if (device_added(device, input) < 0) {
164 udev_device_unref(device);
165 udev_enumerate_unref(e);
169 udev_device_unref(device);
171 udev_enumerate_unref(e);
173 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
174 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
176 if (!wl_list_empty(&seat->devices_list))
180 if (devices_found == 0) {
182 "warning: no input devices on entering Weston. "
184 "\t- no permissions to read /dev/input/event*\n"
185 "\t- seats misconfigured "
186 "(Weston backend option 'seat', "
187 "udev device property ID_SEAT)\n");
195 evdev_udev_handler(int fd, uint32_t mask, void *data)
197 struct udev_input *input = data;
198 struct udev_device *udev_device;
199 struct evdev_device *device, *next;
202 struct udev_seat *seat;
204 udev_device = udev_monitor_receive_device(input->udev_monitor);
208 action = udev_device_get_action(udev_device);
212 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
215 if (!strcmp(action, "add")) {
216 device_added(udev_device, input);
218 else if (!strcmp(action, "remove")) {
219 devnode = udev_device_get_devnode(udev_device);
220 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
221 wl_list_for_each_safe(device, next, &seat->devices_list, link)
222 if (!strcmp(device->devnode, devnode)) {
223 weston_log("input device %s, %s removed\n",
224 device->devname, device->devnode);
225 weston_launcher_close(input->compositor->launcher,
227 evdev_device_destroy(device);
234 udev_device_unref(udev_device);
240 udev_input_enable(struct udev_input *input)
242 struct wl_event_loop *loop;
243 struct weston_compositor *c = input->compositor;
246 input->udev_monitor = udev_monitor_new_from_netlink(input->udev, "udev");
247 if (!input->udev_monitor) {
248 weston_log("udev: failed to create the udev monitor\n");
252 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
255 if (udev_monitor_enable_receiving(input->udev_monitor)) {
256 weston_log("udev: failed to bind the udev monitor\n");
257 udev_monitor_unref(input->udev_monitor);
261 loop = wl_display_get_event_loop(c->wl_display);
262 fd = udev_monitor_get_fd(input->udev_monitor);
263 input->udev_monitor_source =
264 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
265 evdev_udev_handler, input);
266 if (!input->udev_monitor_source) {
267 udev_monitor_unref(input->udev_monitor);
271 if (udev_input_add_devices(input, input->udev) < 0)
280 udev_input_remove_devices(struct udev_input *input)
282 struct evdev_device *device, *next;
283 struct udev_seat *seat;
285 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
286 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
287 weston_launcher_close(input->compositor->launcher,
289 evdev_device_destroy(device);
292 if (seat->base.keyboard)
293 notify_keyboard_focus_out(&seat->base);
298 udev_input_disable(struct udev_input *input)
300 if (!input->udev_monitor)
303 udev_monitor_unref(input->udev_monitor);
304 input->udev_monitor = NULL;
305 wl_event_source_remove(input->udev_monitor_source);
306 input->udev_monitor_source = NULL;
308 udev_input_remove_devices(input);
313 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
316 memset(input, 0, sizeof *input);
317 input->seat_id = strdup(seat_id);
318 input->compositor = c;
320 input->udev = udev_ref(udev);
321 if (udev_input_enable(input) < 0)
327 free(input->seat_id);
332 udev_input_destroy(struct udev_input *input)
334 struct udev_seat *seat, *next;
335 udev_input_disable(input);
336 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
337 udev_seat_destroy(seat);
338 udev_unref(input->udev);
339 free(input->seat_id);
343 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
345 struct udev_seat *seat = (struct udev_seat *) seat_base;
346 struct evdev_device *device;
348 wl_list_for_each(device, &seat->devices_list, link)
349 evdev_led_update(device, leds);
353 notify_output_create(struct wl_listener *listener, void *data)
355 struct udev_seat *seat = container_of(listener, struct udev_seat,
356 output_create_listener);
357 struct evdev_device *device;
358 struct weston_output *output = data;
360 wl_list_for_each(device, &seat->devices_list, link) {
361 if (device->output_name &&
362 strcmp(output->name, device->output_name) == 0) {
363 evdev_device_set_output(device, output);
366 if (device->output_name == NULL && device->output == NULL)
367 evdev_device_set_output(device, output);
371 static struct udev_seat *
372 udev_seat_create(struct udev_input *input, const char *seat_name)
374 struct weston_compositor *c = input->compositor;
375 struct udev_seat *seat;
377 seat = zalloc(sizeof *seat);
381 weston_seat_init(&seat->base, c, seat_name);
382 seat->base.led_update = drm_led_update;
384 seat->output_create_listener.notify = notify_output_create;
385 wl_signal_add(&c->output_created_signal,
386 &seat->output_create_listener);
388 wl_list_init(&seat->devices_list);
393 udev_seat_destroy(struct udev_seat *seat)
395 weston_seat_release(&seat->base);
396 wl_list_remove(&seat->output_create_listener.link);
401 udev_seat_get_named(struct udev_input *input, const char *seat_name)
403 struct weston_compositor *c = input->compositor;
404 struct udev_seat *seat;
406 wl_list_for_each(seat, &c->seat_list, base.link) {
407 if (strcmp(seat->base.seat_name, seat_name) == 0)
411 seat = udev_seat_create(input, seat_name);