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->launcher, 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;
131 if (input->enabled == 1)
132 weston_seat_repick(&seat->base);
138 udev_input_add_devices(struct udev_input *input, struct udev *udev)
140 struct udev_enumerate *e;
141 struct udev_list_entry *entry;
142 struct udev_device *device;
143 const char *path, *sysname;
144 struct udev_seat *seat;
145 int devices_found = 0;
147 e = udev_enumerate_new(udev);
148 udev_enumerate_add_match_subsystem(e, "input");
149 udev_enumerate_scan_devices(e);
150 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
151 path = udev_list_entry_get_name(entry);
152 device = udev_device_new_from_syspath(udev, path);
154 sysname = udev_device_get_sysname(device);
155 if (strncmp("event", sysname, 5) != 0) {
156 udev_device_unref(device);
160 if (device_added(device, input) < 0) {
161 udev_device_unref(device);
162 udev_enumerate_unref(e);
166 udev_device_unref(device);
168 udev_enumerate_unref(e);
170 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
171 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
173 if (!wl_list_empty(&seat->devices_list))
177 if (devices_found == 0) {
179 "warning: no input devices on entering Weston. "
181 "\t- no permissions to read /dev/input/event*\n"
182 "\t- seats misconfigured "
183 "(Weston backend option 'seat', "
184 "udev device property ID_SEAT)\n");
192 evdev_udev_handler(int fd, uint32_t mask, void *data)
194 struct udev_input *input = data;
195 struct udev_device *udev_device;
196 struct evdev_device *device, *next;
199 struct udev_seat *seat;
201 udev_device = udev_monitor_receive_device(input->udev_monitor);
205 action = udev_device_get_action(udev_device);
209 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
212 if (!strcmp(action, "add")) {
213 device_added(udev_device, input);
215 else if (!strcmp(action, "remove")) {
216 devnode = udev_device_get_devnode(udev_device);
217 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
218 wl_list_for_each_safe(device, next, &seat->devices_list, link)
219 if (!strcmp(device->devnode, devnode)) {
220 weston_log("input device %s, %s removed\n",
221 device->devname, device->devnode);
222 evdev_device_destroy(device);
229 udev_device_unref(udev_device);
235 udev_input_enable(struct udev_input *input, struct udev *udev)
237 struct wl_event_loop *loop;
238 struct weston_compositor *c = input->compositor;
241 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
242 if (!input->udev_monitor) {
243 weston_log("udev: failed to create the udev monitor\n");
247 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
250 if (udev_monitor_enable_receiving(input->udev_monitor)) {
251 weston_log("udev: failed to bind the udev monitor\n");
252 udev_monitor_unref(input->udev_monitor);
256 loop = wl_display_get_event_loop(c->wl_display);
257 fd = udev_monitor_get_fd(input->udev_monitor);
258 input->udev_monitor_source =
259 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
260 evdev_udev_handler, input);
261 if (!input->udev_monitor_source) {
262 udev_monitor_unref(input->udev_monitor);
266 if (udev_input_add_devices(input, udev) < 0)
275 udev_input_remove_devices(struct udev_input *input)
277 struct evdev_device *device, *next;
278 struct udev_seat *seat;
280 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
281 wl_list_for_each_safe(device, next, &seat->devices_list, link)
282 evdev_device_destroy(device);
284 if (seat->base.keyboard)
285 notify_keyboard_focus_out(&seat->base);
290 udev_input_disable(struct udev_input *input)
292 if (!input->udev_monitor)
295 udev_monitor_unref(input->udev_monitor);
296 input->udev_monitor = NULL;
297 wl_event_source_remove(input->udev_monitor_source);
298 input->udev_monitor_source = NULL;
300 udev_input_remove_devices(input);
305 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
308 memset(input, 0, sizeof *input);
309 input->seat_id = strdup(seat_id);
310 input->compositor = c;
311 if (udev_input_enable(input, udev) < 0)
317 free(input->seat_id);
322 udev_input_destroy(struct udev_input *input)
324 struct udev_seat *seat, *next;
325 udev_input_disable(input);
326 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
327 udev_seat_destroy(seat);
328 free(input->seat_id);
332 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
334 struct udev_seat *seat = (struct udev_seat *) seat_base;
335 struct evdev_device *device;
337 wl_list_for_each(device, &seat->devices_list, link)
338 evdev_led_update(device, leds);
341 static struct udev_seat *
342 udev_seat_create(struct weston_compositor *c, const char *seat_name)
344 struct udev_seat *seat;
346 seat = zalloc(sizeof *seat);
350 weston_seat_init(&seat->base, c, seat_name);
351 seat->base.led_update = drm_led_update;
353 wl_list_init(&seat->devices_list);
358 udev_seat_destroy(struct udev_seat *seat)
360 weston_seat_release(&seat->base);
365 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
367 struct udev_seat *seat;
369 wl_list_for_each(seat, &c->seat_list, base.link) {
370 if (strcmp(seat->base.seat_name, seat_name) == 0)
374 seat = udev_seat_create(c, seat_name);