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.
28 #include "compositor.h"
29 #include "launcher-util.h"
31 #include "udev-seat.h"
33 static const char default_seat[] = "seat0";
35 static struct udev_seat *
36 udev_seat_create(struct weston_compositor *c);
38 udev_seat_destroy(struct udev_seat *seat);
41 device_added(struct udev_device *udev_device, struct udev_input *input)
43 struct weston_compositor *c;
44 struct evdev_device *device;
46 const char *device_seat;
47 const char *calibration_values;
49 struct udev_seat *seat;
51 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
53 device_seat = default_seat;
55 if (strcmp(device_seat, input->seat_id))
58 c = input->compositor;
59 devnode = udev_device_get_devnode(udev_device);
61 /* Single default seat for now */
63 input->seat = udev_seat_create(c);
70 /* Use non-blocking mode so that we can loop on read on
71 * evdev_device_data() until all events on the fd are
72 * read. mtdev_get() also expects this. */
73 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
75 weston_log("opening input device '%s' failed.\n", devnode);
79 device = evdev_device_create(&seat->base, devnode, fd);
80 if (device == EVDEV_UNHANDLED_DEVICE) {
82 weston_log("not using input device '%s'.\n", devnode);
84 } else if (device == NULL) {
86 weston_log("failed to create input device '%s'.\n", devnode);
91 udev_device_get_property_value(udev_device,
94 if (calibration_values && sscanf(calibration_values,
96 &device->abs.calibration[0],
97 &device->abs.calibration[1],
98 &device->abs.calibration[2],
99 &device->abs.calibration[3],
100 &device->abs.calibration[4],
101 &device->abs.calibration[5]) == 6) {
102 device->abs.apply_calibration = 1;
103 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
104 device->abs.calibration[0],
105 device->abs.calibration[1],
106 device->abs.calibration[2],
107 device->abs.calibration[3],
108 device->abs.calibration[4],
109 device->abs.calibration[5]);
112 wl_list_insert(seat->devices_list.prev, &device->link);
118 udev_input_add_devices(struct udev_input *input, struct udev *udev)
120 struct udev_enumerate *e;
121 struct udev_list_entry *entry;
122 struct udev_device *device;
123 const char *path, *sysname;
125 e = udev_enumerate_new(udev);
126 udev_enumerate_add_match_subsystem(e, "input");
127 udev_enumerate_scan_devices(e);
128 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
129 path = udev_list_entry_get_name(entry);
130 device = udev_device_new_from_syspath(udev, path);
132 sysname = udev_device_get_sysname(device);
133 if (strncmp("event", sysname, 5) != 0) {
134 udev_device_unref(device);
138 if (device_added(device, input) < 0) {
139 udev_device_unref(device);
140 udev_enumerate_unref(e);
144 udev_device_unref(device);
146 udev_enumerate_unref(e);
148 evdev_notify_keyboard_focus(&input->seat->base, &input->seat->devices_list);
150 if (wl_list_empty(&input->seat->devices_list)) {
152 "warning: no input devices on entering Weston. "
154 "\t- no permissions to read /dev/input/event*\n"
155 "\t- seats misconfigured "
156 "(Weston backend option 'seat', "
157 "udev device property ID_SEAT)\n");
165 evdev_udev_handler(int fd, uint32_t mask, void *data)
167 struct udev_input *input = data;
168 struct udev_device *udev_device;
169 struct evdev_device *device, *next;
173 udev_device = udev_monitor_receive_device(input->udev_monitor);
177 action = udev_device_get_action(udev_device);
181 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
184 if (!strcmp(action, "add")) {
185 device_added(udev_device, input);
187 else if (!strcmp(action, "remove")) {
188 devnode = udev_device_get_devnode(udev_device);
189 wl_list_for_each_safe(device, next, &input->seat->devices_list, link)
190 if (!strcmp(device->devnode, devnode)) {
191 weston_log("input device %s, %s removed\n",
192 device->devname, device->devnode);
193 evdev_device_destroy(device);
199 udev_device_unref(udev_device);
205 udev_input_enable(struct udev_input *input, struct udev *udev)
207 struct wl_event_loop *loop;
208 struct weston_compositor *c = input->compositor;
211 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
212 if (!input->udev_monitor) {
213 weston_log("udev: failed to create the udev monitor\n");
217 udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
220 if (udev_monitor_enable_receiving(input->udev_monitor)) {
221 weston_log("udev: failed to bind the udev monitor\n");
222 udev_monitor_unref(input->udev_monitor);
226 loop = wl_display_get_event_loop(c->wl_display);
227 fd = udev_monitor_get_fd(input->udev_monitor);
228 input->udev_monitor_source =
229 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
230 evdev_udev_handler, input);
231 if (!input->udev_monitor_source) {
232 udev_monitor_unref(input->udev_monitor);
236 if (udev_input_add_devices(input, udev) < 0)
243 udev_input_remove_devices(struct udev_input *input)
245 struct evdev_device *device, *next;
247 wl_list_for_each_safe(device, next, &input->seat->devices_list, link)
248 evdev_device_destroy(device);
250 if (input->seat->base.keyboard)
251 notify_keyboard_focus_out(&input->seat->base);
255 udev_input_disable(struct udev_input *input)
257 if (!input->udev_monitor)
260 udev_monitor_unref(input->udev_monitor);
261 input->udev_monitor = NULL;
262 wl_event_source_remove(input->udev_monitor_source);
263 input->udev_monitor_source = NULL;
265 udev_input_remove_devices(input);
270 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
273 memset(input, 0, sizeof *input);
274 input->seat_id = strdup(seat_id);
275 input->compositor = c;
276 if (udev_input_enable(input, udev) < 0)
282 free(input->seat_id);
287 udev_input_destroy(struct udev_input *input)
289 udev_input_disable(input);
290 udev_seat_destroy(input->seat);
291 free(input->seat_id);
295 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
297 struct udev_seat *seat = (struct udev_seat *) seat_base;
298 struct evdev_device *device;
300 wl_list_for_each(device, &seat->devices_list, link)
301 evdev_led_update(device, leds);
304 static struct udev_seat *
305 udev_seat_create(struct weston_compositor *c)
307 struct udev_seat *seat;
309 seat = malloc(sizeof *seat);
313 memset(seat, 0, sizeof *seat);
314 weston_seat_init(&seat->base, c, "default");
315 seat->base.led_update = drm_led_update;
317 wl_list_init(&seat->devices_list);
322 udev_seat_destroy(struct udev_seat *seat)
324 weston_seat_release(&seat->base);