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 udev_input *input, const char *seat_name);
46 udev_seat_destroy(struct udev_seat *seat);
49 device_added(struct udev_input *input, struct libinput_device *libinput_device)
51 struct weston_compositor *c;
52 struct evdev_device *device;
53 struct weston_output *output;
54 const char *seat_name;
55 const char *output_name;
56 struct libinput_seat *libinput_seat;
57 struct weston_seat *seat;
58 struct udev_seat *udev_seat;
60 c = input->compositor;
61 libinput_seat = libinput_device_get_seat(libinput_device);
63 seat_name = libinput_seat_get_logical_name(libinput_seat);
64 udev_seat = udev_seat_get_named(input, seat_name);
68 seat = &udev_seat->base;
69 device = evdev_device_create(libinput_device, seat);
73 udev_seat = (struct udev_seat *) seat;
74 wl_list_insert(udev_seat->devices_list.prev, &device->link);
76 if (seat->output && seat->pointer)
77 weston_pointer_clamp(seat->pointer,
81 output_name = libinput_device_get_output_name(libinput_device);
83 device->output_name = strdup(output_name);
84 wl_list_for_each(output, &c->output_list, link)
85 if (strcmp(output->name, device->output_name) == 0)
86 evdev_device_set_output(device, output);
87 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
88 output = container_of(c->output_list.next,
89 struct weston_output, link);
90 evdev_device_set_output(device, output);
93 if (!input->suspended)
94 weston_seat_repick(seat);
98 udev_seat_remove_devices(struct udev_seat *seat)
100 struct evdev_device *device, *next;
102 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
103 evdev_device_destroy(device);
108 udev_input_disable(struct udev_input *input)
110 if (input->suspended)
113 libinput_suspend(input->libinput);
114 process_events(input);
115 input->suspended = 1;
119 udev_input_process_event(struct libinput_event *event)
121 struct libinput *libinput = libinput_event_get_context(event);
122 struct libinput_device *libinput_device =
123 libinput_event_get_device(event);
124 struct udev_input *input = libinput_get_user_data(libinput);
125 struct evdev_device *device;
128 switch (libinput_event_get_type(event)) {
129 case LIBINPUT_EVENT_DEVICE_ADDED:
130 device_added(input, libinput_device);
132 case LIBINPUT_EVENT_DEVICE_REMOVED:
133 device = libinput_device_get_user_data(libinput_device);
134 evdev_device_destroy(device);
144 process_event(struct libinput_event *event)
146 if (udev_input_process_event(event))
148 if (evdev_device_process_event(event))
153 process_events(struct udev_input *input)
155 struct libinput_event *event;
157 while ((event = libinput_get_event(input->libinput))) {
158 process_event(event);
159 libinput_event_destroy(event);
164 udev_input_dispatch(struct udev_input *input)
166 if (libinput_dispatch(input->libinput) != 0)
167 weston_log("libinput: Failed to dispatch libinput\n");
169 process_events(input);
175 libinput_source_dispatch(int fd, uint32_t mask, void *data)
177 struct udev_input *input = data;
179 return udev_input_dispatch(input) != 0;
183 open_restricted(const char *path, int flags, void *user_data)
185 struct udev_input *input = user_data;
186 struct weston_launcher *launcher = input->compositor->launcher;
188 return weston_launcher_open(launcher, path, flags);
192 close_restricted(int fd, void *user_data)
194 struct udev_input *input = user_data;
195 struct weston_launcher *launcher = input->compositor->launcher;
197 weston_launcher_close(launcher, fd);
200 const struct libinput_interface libinput_interface = {
206 udev_input_enable(struct udev_input *input)
208 struct wl_event_loop *loop;
209 struct weston_compositor *c = input->compositor;
211 struct udev_seat *seat;
212 int devices_found = 0;
214 loop = wl_display_get_event_loop(c->wl_display);
215 fd = libinput_get_fd(input->libinput);
216 input->libinput_source =
217 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
218 libinput_source_dispatch, input);
219 if (!input->libinput_source) {
223 if (input->suspended) {
224 if (libinput_resume(input->libinput) != 0) {
225 wl_event_source_remove(input->libinput_source);
226 input->libinput_source = NULL;
229 input->suspended = 0;
230 process_events(input);
233 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
234 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
236 if (!wl_list_empty(&seat->devices_list))
240 if (devices_found == 0) {
242 "warning: no input devices on entering Weston. "
244 "\t- no permissions to read /dev/input/event*\n"
245 "\t- seats misconfigured "
246 "(Weston backend option 'seat', "
247 "udev device property ID_SEAT)\n");
255 libinput_log_func(struct libinput *libinput,
256 enum libinput_log_priority priority,
257 const char *format, va_list args)
259 weston_vlog(format, args);
263 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
266 const char *log_priority = NULL;
268 memset(input, 0, sizeof *input);
270 input->compositor = c;
272 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
274 input->libinput = libinput_udev_create_context(&libinput_interface,
276 if (!input->libinput) {
280 libinput_log_set_handler(input->libinput, &libinput_log_func);
283 if (strcmp(log_priority, "debug") == 0) {
284 libinput_log_set_priority(input->libinput,
285 LIBINPUT_LOG_PRIORITY_DEBUG);
286 } else if (strcmp(log_priority, "info") == 0) {
287 libinput_log_set_priority(input->libinput,
288 LIBINPUT_LOG_PRIORITY_INFO);
289 } else if (strcmp(log_priority, "error") == 0) {
290 libinput_log_set_priority(input->libinput,
291 LIBINPUT_LOG_PRIORITY_ERROR);
295 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
296 libinput_unref(input->libinput);
300 process_events(input);
302 return udev_input_enable(input);
306 udev_input_destroy(struct udev_input *input)
308 struct udev_seat *seat, *next;
310 wl_event_source_remove(input->libinput_source);
311 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
312 udev_seat_destroy(seat);
313 libinput_unref(input->libinput);
317 udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
319 struct udev_seat *seat = (struct udev_seat *) seat_base;
320 struct evdev_device *device;
322 wl_list_for_each(device, &seat->devices_list, link)
323 evdev_led_update(device, leds);
327 notify_output_create(struct wl_listener *listener, void *data)
329 struct udev_seat *seat = container_of(listener, struct udev_seat,
330 output_create_listener);
331 struct evdev_device *device;
332 struct weston_output *output = data;
334 wl_list_for_each(device, &seat->devices_list, link) {
335 if (device->output_name &&
336 strcmp(output->name, device->output_name) == 0) {
337 evdev_device_set_output(device, output);
340 if (device->output_name == NULL && device->output == NULL)
341 evdev_device_set_output(device, output);
345 static struct udev_seat *
346 udev_seat_create(struct udev_input *input, const char *seat_name)
348 struct weston_compositor *c = input->compositor;
349 struct udev_seat *seat;
351 seat = zalloc(sizeof *seat);
355 weston_seat_init(&seat->base, c, seat_name);
356 seat->base.led_update = udev_seat_led_update;
358 seat->output_create_listener.notify = notify_output_create;
359 wl_signal_add(&c->output_created_signal,
360 &seat->output_create_listener);
362 wl_list_init(&seat->devices_list);
368 udev_seat_destroy(struct udev_seat *seat)
370 udev_seat_remove_devices(seat);
371 if (seat->base.keyboard)
372 notify_keyboard_focus_out(&seat->base);
373 weston_seat_release(&seat->base);
374 wl_list_remove(&seat->output_create_listener.link);
379 udev_seat_get_named(struct udev_input *input, const char *seat_name)
381 struct udev_seat *seat;
383 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
384 if (strcmp(seat->base.seat_name, seat_name) == 0)
388 return udev_seat_create(input, seat_name);