2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include "compositor.h"
34 #include "launcher-util.h"
35 #include "libinput-seat.h"
36 #include "libinput-device.h"
38 static const char default_seat[] = "seat0";
39 static const char default_seat_name[] = "default";
42 process_events(struct udev_input *input);
43 static struct udev_seat *
44 udev_seat_create(struct weston_compositor *c, const char *seat_name);
46 udev_seat_destroy(struct udev_seat *seat);
49 udev_seat_has_output(struct udev_seat *seat)
51 struct weston_output *output;
52 wl_list_for_each(output, &seat->base.compositor->output_list, link) {
53 if (seat && (&seat->base == output->seat_data.seat))
60 device_added(struct udev_input *input, struct libinput_device *libinput_device)
62 struct weston_compositor *c;
63 struct evdev_device *device;
64 struct weston_output *output;
65 const char *seat_name;
66 const char *output_name;
67 struct libinput_seat *libinput_seat;
68 struct weston_seat *seat;
69 struct udev_seat *udev_seat;
71 c = input->compositor;
72 libinput_seat = libinput_device_get_seat(libinput_device);
74 seat_name = libinput_seat_get_logical_name(libinput_seat);
75 udev_seat = udev_seat_get_named(c, seat_name);
79 seat = &udev_seat->base;
80 device = evdev_device_create(libinput_device, seat);
84 wl_list_insert(udev_seat->devices_list.prev, &device->link);
86 if (udev_seat_has_output(udev_seat) && seat->pointer)
87 weston_pointer_clamp(seat->pointer,
91 output_name = libinput_device_get_output_name(libinput_device);
93 device->output_name = strdup(output_name);
94 wl_list_for_each(output, &c->output_list, link)
95 if (strcmp(output->name, device->output_name) == 0)
96 evdev_device_set_output(device, output);
97 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
98 output = container_of(c->output_list.next,
99 struct weston_output, link);
100 evdev_device_set_output(device, output);
103 if (!input->suspended)
104 weston_seat_repick(seat);
109 udev_seat_remove_devices(struct udev_seat *seat)
111 struct evdev_device *device, *next;
113 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
114 evdev_device_destroy(device);
119 udev_input_disable(struct udev_input *input)
121 if (input->suspended)
124 libinput_suspend(input->libinput);
125 process_events(input);
126 input->suspended = 1;
130 udev_input_process_event(struct libinput_event *event)
132 struct libinput *libinput = libinput_event_get_context(event);
133 struct libinput_device *libinput_device =
134 libinput_event_get_device(event);
135 struct udev_input *input = libinput_get_user_data(libinput);
136 struct evdev_device *device;
139 switch (libinput_event_get_type(event)) {
140 case LIBINPUT_EVENT_DEVICE_ADDED:
141 device_added(input, libinput_device);
143 case LIBINPUT_EVENT_DEVICE_REMOVED:
144 device = libinput_device_get_user_data(libinput_device);
145 evdev_device_destroy(device);
155 process_event(struct libinput_event *event)
157 if (udev_input_process_event(event))
159 if (evdev_device_process_event(event))
164 process_events(struct udev_input *input)
166 struct libinput_event *event;
168 while ((event = libinput_get_event(input->libinput))) {
169 process_event(event);
170 libinput_event_destroy(event);
175 udev_input_dispatch(struct udev_input *input)
177 if (libinput_dispatch(input->libinput) != 0)
178 weston_log("libinput: Failed to dispatch libinput\n");
180 process_events(input);
186 libinput_source_dispatch(int fd, uint32_t mask, void *data)
188 struct udev_input *input = data;
190 return udev_input_dispatch(input) != 0;
194 open_restricted(const char *path, int flags, void *user_data)
196 struct udev_input *input = user_data;
197 struct weston_launcher *launcher = input->compositor->launcher;
199 return weston_launcher_open(launcher, path, flags);
203 close_restricted(int fd, void *user_data)
205 struct udev_input *input = user_data;
206 struct weston_launcher *launcher = input->compositor->launcher;
208 weston_launcher_close(launcher, fd);
211 const struct libinput_interface libinput_interface = {
217 udev_input_enable(struct udev_input *input)
219 struct wl_event_loop *loop;
220 struct weston_compositor *c = input->compositor;
222 struct udev_seat *seat;
223 int devices_found = 0;
225 loop = wl_display_get_event_loop(c->wl_display);
226 fd = libinput_get_fd(input->libinput);
227 input->libinput_source =
228 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
229 libinput_source_dispatch, input);
230 if (!input->libinput_source) {
234 if (input->suspended) {
235 if (libinput_resume(input->libinput) != 0) {
236 wl_event_source_remove(input->libinput_source);
237 input->libinput_source = NULL;
240 input->suspended = 0;
241 process_events(input);
244 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
245 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
247 if (!wl_list_empty(&seat->devices_list))
251 if (devices_found == 0) {
253 "warning: no input devices on entering Weston. "
255 "\t- no permissions to read /dev/input/event*\n"
256 "\t- seats misconfigured "
257 "(Weston backend option 'seat', "
258 "udev device property ID_SEAT)\n");
266 libinput_log_func(struct libinput *libinput,
267 enum libinput_log_priority priority,
268 const char *format, va_list args)
270 weston_vlog(format, args);
274 udev_input_init(struct udev_input *input, struct weston_compositor *c,
275 struct udev *udev, const char *seat_id)
277 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
278 const char *log_priority = NULL;
280 memset(input, 0, sizeof *input);
282 input->compositor = c;
284 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
286 input->libinput = libinput_udev_create_context(&libinput_interface,
288 if (!input->libinput) {
292 libinput_log_set_handler(input->libinput, &libinput_log_func);
295 if (strcmp(log_priority, "debug") == 0) {
296 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
297 } else if (strcmp(log_priority, "info") == 0) {
298 priority = LIBINPUT_LOG_PRIORITY_INFO;
299 } else if (strcmp(log_priority, "error") == 0) {
300 priority = LIBINPUT_LOG_PRIORITY_ERROR;
304 libinput_log_set_priority(input->libinput, priority);
306 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
307 libinput_unref(input->libinput);
311 process_events(input);
313 return udev_input_enable(input);
317 udev_input_destroy(struct udev_input *input)
319 struct udev_seat *seat, *next;
321 wl_event_source_remove(input->libinput_source);
322 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
323 udev_seat_destroy(seat);
324 libinput_unref(input->libinput);
328 udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
330 struct udev_seat *seat = (struct udev_seat *) seat_base;
331 struct evdev_device *device;
333 wl_list_for_each(device, &seat->devices_list, link)
334 evdev_led_update(device, leds);
338 udev_seat_is_output_linked(struct udev_seat *seat, struct weston_output *output)
340 if (seat && output->seat_data.seat &&
341 (&seat->base == output->seat_data.seat)) {
342 weston_log("output seatname (%s) is linked already with seat\n",
343 output->seat_data.seatname);
350 udev_seat_link_output(struct udev_seat *seat, struct weston_output *output)
352 if (udev_seat_is_output_linked(seat, output)) return 1;
354 if (!seat || !output || !seat->base.seat_name ||
355 !output->seat_data.seatname ||
356 strcmp(seat->base.seat_name, output->seat_data.seatname) != 0) {
360 free(output->seat_data.seatname);
361 output->seat_data.seatname = strdup(seat->base.seat_name);
362 output->seat_data.seat = &seat->base;
363 weston_log("link output to seat %s\n", seat->base.seat_name);
368 notify_output_create(struct wl_listener *listener, void *data)
370 struct udev_seat *seat = container_of(listener, struct udev_seat,
371 output_create_listener);
372 struct evdev_device *device, *next;
373 struct weston_output *output = data;
375 weston_log("notify received for output created for seat %s\n",
376 seat->base.seat_name);
377 udev_seat_link_output(seat, output);
379 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
380 if (device->output_name && output->name &&
381 strcmp(output->name, device->output_name) == 0) {
382 evdev_device_set_output(device, output);
385 if (device->output_name == NULL && device->output == NULL)
386 evdev_device_set_output(device, output);
390 static struct udev_seat *
391 udev_seat_create(struct weston_compositor *c, const char *seat_name)
393 struct udev_seat *seat;
395 seat = zalloc(sizeof *seat);
399 weston_seat_init(&seat->base, c, seat_name);
400 seat->base.led_update = udev_seat_led_update;
402 seat->output_create_listener.notify = notify_output_create;
403 wl_signal_add(&c->output_created_signal,
404 &seat->output_create_listener);
406 wl_list_init(&seat->devices_list);
408 weston_log("seat created with name %s\n", seat_name);
413 udev_seat_destroy(struct udev_seat *seat)
415 udev_seat_remove_devices(seat);
416 if (seat->base.keyboard)
417 notify_keyboard_focus_out(&seat->base);
418 weston_seat_release(&seat->base);
419 wl_list_remove(&seat->output_create_listener.link);
424 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
426 struct udev_seat *seat;
428 wl_list_for_each(seat, &c->seat_list, base.link) {
429 if (strcmp(seat->base.seat_name, seat_name) == 0)
433 return udev_seat_create(c, seat_name);