blob: acb29d7809b27047702bb63ca61cf0fd86186773 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
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.
14 *
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.
22 */
23
24#include "config.h"
25
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <libinput.h>
31#include <libudev.h>
32
33#include "compositor.h"
34#include "launcher-util.h"
35#include "libinput-seat.h"
36#include "libinput-device.h"
37
38static const char default_seat[] = "seat0";
39static const char default_seat_name[] = "default";
40
41static void
42process_events(struct udev_input *input);
43static struct udev_seat *
44udev_seat_create(struct udev_input *input, const char *seat_name);
45static void
46udev_seat_destroy(struct udev_seat *seat);
47
48static void
49device_added(struct udev_input *input, struct libinput_device *libinput_device)
50{
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;
59
60 c = input->compositor;
61 libinput_seat = libinput_device_get_seat(libinput_device);
62
63 seat_name = libinput_seat_get_logical_name(libinput_seat);
64 udev_seat = udev_seat_get_named(input, seat_name);
65 if (!udev_seat)
66 return;
67
68 seat = &udev_seat->base;
69 device = evdev_device_create(libinput_device, seat);
70 if (device == NULL)
71 return;
72
73 udev_seat = (struct udev_seat *) seat;
74 wl_list_insert(udev_seat->devices_list.prev, &device->link);
75
76 if (seat->output && seat->pointer)
77 weston_pointer_clamp(seat->pointer,
78 &seat->pointer->x,
79 &seat->pointer->y);
80
81 output_name = libinput_device_get_output_name(libinput_device);
82 if (output_name) {
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);
U. Artie Eoff161c6c52014-04-17 07:53:25 -070087 }
88
89 if (device->output == NULL) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010090 output = container_of(c->output_list.next,
91 struct weston_output, link);
92 evdev_device_set_output(device, output);
93 }
94
95 if (!input->suspended)
96 weston_seat_repick(seat);
97}
98
99static void
100udev_seat_remove_devices(struct udev_seat *seat)
101{
102 struct evdev_device *device, *next;
103
104 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
105 evdev_device_destroy(device);
106 }
107}
108
109void
110udev_input_disable(struct udev_input *input)
111{
112 if (input->suspended)
113 return;
114
115 libinput_suspend(input->libinput);
116 process_events(input);
117 input->suspended = 1;
118}
119
120static int
121udev_input_process_event(struct libinput_event *event)
122{
123 struct libinput *libinput = libinput_event_get_context(event);
124 struct libinput_device *libinput_device =
125 libinput_event_get_device(event);
126 struct udev_input *input = libinput_get_user_data(libinput);
127 struct evdev_device *device;
128 int handled = 1;
129
130 switch (libinput_event_get_type(event)) {
131 case LIBINPUT_EVENT_DEVICE_ADDED:
132 device_added(input, libinput_device);
133 break;
134 case LIBINPUT_EVENT_DEVICE_REMOVED:
135 device = libinput_device_get_user_data(libinput_device);
136 evdev_device_destroy(device);
137 break;
138 default:
139 handled = 0;
140 }
141
142 return handled;
143}
144
145static void
146process_event(struct libinput_event *event)
147{
148 if (udev_input_process_event(event))
149 return;
150 if (evdev_device_process_event(event))
151 return;
152}
153
154static void
155process_events(struct udev_input *input)
156{
157 struct libinput_event *event;
158
159 while ((event = libinput_get_event(input->libinput))) {
160 process_event(event);
161 libinput_event_destroy(event);
162 }
163}
164
165static int
166udev_input_dispatch(struct udev_input *input)
167{
168 if (libinput_dispatch(input->libinput) != 0)
169 weston_log("libinput: Failed to dispatch libinput\n");
170
171 process_events(input);
172
173 return 0;
174}
175
176static int
177libinput_source_dispatch(int fd, uint32_t mask, void *data)
178{
179 struct udev_input *input = data;
180
181 return udev_input_dispatch(input) != 0;
182}
183
184static int
185open_restricted(const char *path, int flags, void *user_data)
186{
187 struct udev_input *input = user_data;
188 struct weston_launcher *launcher = input->compositor->launcher;
189
190 return weston_launcher_open(launcher, path, flags);
191}
192
193static void
194close_restricted(int fd, void *user_data)
195{
196 struct udev_input *input = user_data;
197 struct weston_launcher *launcher = input->compositor->launcher;
198
199 weston_launcher_close(launcher, fd);
200}
201
202const struct libinput_interface libinput_interface = {
203 open_restricted,
204 close_restricted,
205};
206
207int
208udev_input_enable(struct udev_input *input)
209{
210 struct wl_event_loop *loop;
211 struct weston_compositor *c = input->compositor;
212 int fd;
213 struct udev_seat *seat;
214 int devices_found = 0;
215
216 loop = wl_display_get_event_loop(c->wl_display);
217 fd = libinput_get_fd(input->libinput);
218 input->libinput_source =
219 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
220 libinput_source_dispatch, input);
221 if (!input->libinput_source) {
222 return -1;
223 }
224
225 if (input->suspended) {
226 if (libinput_resume(input->libinput) != 0) {
227 wl_event_source_remove(input->libinput_source);
228 input->libinput_source = NULL;
229 return -1;
230 }
231 input->suspended = 0;
232 process_events(input);
233 }
234
235 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
236 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
237
238 if (!wl_list_empty(&seat->devices_list))
239 devices_found = 1;
240 }
241
242 if (devices_found == 0) {
243 weston_log(
244 "warning: no input devices on entering Weston. "
245 "Possible causes:\n"
246 "\t- no permissions to read /dev/input/event*\n"
247 "\t- seats misconfigured "
248 "(Weston backend option 'seat', "
249 "udev device property ID_SEAT)\n");
250 return -1;
251 }
252
253 return 0;
254}
255
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700256static void
257libinput_log_func(enum libinput_log_priority priority, void *user_data,
258 const char *format, va_list args)
259{
260 weston_vlog(format, args);
261}
262
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100263int
264udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
265 const char *seat_id)
266{
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700267 const char *log_priority = NULL;
268
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100269 memset(input, 0, sizeof *input);
270
271 input->compositor = c;
272
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700273 libinput_log_set_handler(&libinput_log_func, NULL);
274
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700275 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
276 if (log_priority) {
277 libinput_log_set_priority(strtol(log_priority, NULL, 10));
278 }
279
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100280 input->libinput = libinput_udev_create_for_seat(&libinput_interface, input,
281 udev, seat_id);
282 if (!input->libinput) {
283 return -1;
284 }
285 process_events(input);
286
287 return udev_input_enable(input);
288}
289
290void
291udev_input_destroy(struct udev_input *input)
292{
293 struct udev_seat *seat, *next;
294
295 wl_event_source_remove(input->libinput_source);
296 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
297 udev_seat_destroy(seat);
298 libinput_destroy(input->libinput);
299}
300
301static void
302udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
303{
304 struct udev_seat *seat = (struct udev_seat *) seat_base;
305 struct evdev_device *device;
306
307 wl_list_for_each(device, &seat->devices_list, link)
308 evdev_led_update(device, leds);
309}
310
311static void
312notify_output_create(struct wl_listener *listener, void *data)
313{
314 struct udev_seat *seat = container_of(listener, struct udev_seat,
315 output_create_listener);
316 struct evdev_device *device;
317 struct weston_output *output = data;
318
319 wl_list_for_each(device, &seat->devices_list, link)
320 if (device->output_name &&
321 strcmp(output->name, device->output_name) == 0) {
322 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100323 }
324}
325
326static struct udev_seat *
327udev_seat_create(struct udev_input *input, const char *seat_name)
328{
329 struct weston_compositor *c = input->compositor;
330 struct udev_seat *seat;
331
332 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100333 if (!seat)
334 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000335
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100336 weston_seat_init(&seat->base, c, seat_name);
337 seat->base.led_update = udev_seat_led_update;
338
339 seat->output_create_listener.notify = notify_output_create;
340 wl_signal_add(&c->output_created_signal,
341 &seat->output_create_listener);
342
343 wl_list_init(&seat->devices_list);
344
345 return seat;
346}
347
348static void
349udev_seat_destroy(struct udev_seat *seat)
350{
351 udev_seat_remove_devices(seat);
352 if (seat->base.keyboard)
353 notify_keyboard_focus_out(&seat->base);
354 weston_seat_release(&seat->base);
355 wl_list_remove(&seat->output_create_listener.link);
356 free(seat);
357}
358
359struct udev_seat *
360udev_seat_get_named(struct udev_input *input, const char *seat_name)
361{
362 struct udev_seat *seat;
363
364 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
365 if (strcmp(seat->base.seat_name, seat_name) == 0)
366 return seat;
367 }
368
369 return udev_seat_create(input, seat_name);
370}