blob: a38d47058b6e9bc3216491dd9fdcd6976b78fe90 [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);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030087 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010088 output = container_of(c->output_list.next,
89 struct weston_output, link);
90 evdev_device_set_output(device, output);
91 }
92
93 if (!input->suspended)
94 weston_seat_repick(seat);
95}
96
97static void
98udev_seat_remove_devices(struct udev_seat *seat)
99{
100 struct evdev_device *device, *next;
101
102 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
103 evdev_device_destroy(device);
104 }
105}
106
107void
108udev_input_disable(struct udev_input *input)
109{
110 if (input->suspended)
111 return;
112
113 libinput_suspend(input->libinput);
114 process_events(input);
115 input->suspended = 1;
116}
117
118static int
119udev_input_process_event(struct libinput_event *event)
120{
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;
126 int handled = 1;
127
128 switch (libinput_event_get_type(event)) {
129 case LIBINPUT_EVENT_DEVICE_ADDED:
130 device_added(input, libinput_device);
131 break;
132 case LIBINPUT_EVENT_DEVICE_REMOVED:
133 device = libinput_device_get_user_data(libinput_device);
134 evdev_device_destroy(device);
135 break;
136 default:
137 handled = 0;
138 }
139
140 return handled;
141}
142
143static void
144process_event(struct libinput_event *event)
145{
146 if (udev_input_process_event(event))
147 return;
148 if (evdev_device_process_event(event))
149 return;
150}
151
152static void
153process_events(struct udev_input *input)
154{
155 struct libinput_event *event;
156
157 while ((event = libinput_get_event(input->libinput))) {
158 process_event(event);
159 libinput_event_destroy(event);
160 }
161}
162
163static int
164udev_input_dispatch(struct udev_input *input)
165{
166 if (libinput_dispatch(input->libinput) != 0)
167 weston_log("libinput: Failed to dispatch libinput\n");
168
169 process_events(input);
170
171 return 0;
172}
173
174static int
175libinput_source_dispatch(int fd, uint32_t mask, void *data)
176{
177 struct udev_input *input = data;
178
179 return udev_input_dispatch(input) != 0;
180}
181
182static int
183open_restricted(const char *path, int flags, void *user_data)
184{
185 struct udev_input *input = user_data;
186 struct weston_launcher *launcher = input->compositor->launcher;
187
188 return weston_launcher_open(launcher, path, flags);
189}
190
191static void
192close_restricted(int fd, void *user_data)
193{
194 struct udev_input *input = user_data;
195 struct weston_launcher *launcher = input->compositor->launcher;
196
197 weston_launcher_close(launcher, fd);
198}
199
200const struct libinput_interface libinput_interface = {
201 open_restricted,
202 close_restricted,
203};
204
205int
206udev_input_enable(struct udev_input *input)
207{
208 struct wl_event_loop *loop;
209 struct weston_compositor *c = input->compositor;
210 int fd;
211 struct udev_seat *seat;
212 int devices_found = 0;
213
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) {
220 return -1;
221 }
222
223 if (input->suspended) {
224 if (libinput_resume(input->libinput) != 0) {
225 wl_event_source_remove(input->libinput_source);
226 input->libinput_source = NULL;
227 return -1;
228 }
229 input->suspended = 0;
230 process_events(input);
231 }
232
233 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
234 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
235
236 if (!wl_list_empty(&seat->devices_list))
237 devices_found = 1;
238 }
239
240 if (devices_found == 0) {
241 weston_log(
242 "warning: no input devices on entering Weston. "
243 "Possible causes:\n"
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");
248 return -1;
249 }
250
251 return 0;
252}
253
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700254static void
255libinput_log_func(enum libinput_log_priority priority, void *user_data,
256 const char *format, va_list args)
257{
258 weston_vlog(format, args);
259}
260
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100261int
262udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
263 const char *seat_id)
264{
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700265 const char *log_priority = NULL;
266
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100267 memset(input, 0, sizeof *input);
268
269 input->compositor = c;
270
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700271 libinput_log_set_handler(&libinput_log_func, NULL);
272
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700273 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
274 if (log_priority) {
275 libinput_log_set_priority(strtol(log_priority, NULL, 10));
276 }
277
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100278 input->libinput = libinput_udev_create_for_seat(&libinput_interface, input,
279 udev, seat_id);
280 if (!input->libinput) {
281 return -1;
282 }
283 process_events(input);
284
285 return udev_input_enable(input);
286}
287
288void
289udev_input_destroy(struct udev_input *input)
290{
291 struct udev_seat *seat, *next;
292
293 wl_event_source_remove(input->libinput_source);
294 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
295 udev_seat_destroy(seat);
296 libinput_destroy(input->libinput);
297}
298
299static void
300udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
301{
302 struct udev_seat *seat = (struct udev_seat *) seat_base;
303 struct evdev_device *device;
304
305 wl_list_for_each(device, &seat->devices_list, link)
306 evdev_led_update(device, leds);
307}
308
309static void
310notify_output_create(struct wl_listener *listener, void *data)
311{
312 struct udev_seat *seat = container_of(listener, struct udev_seat,
313 output_create_listener);
314 struct evdev_device *device;
315 struct weston_output *output = data;
316
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300317 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100318 if (device->output_name &&
319 strcmp(output->name, device->output_name) == 0) {
320 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100321 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300322
323 if (device->output_name == NULL && device->output == NULL)
324 evdev_device_set_output(device, output);
325 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100326}
327
328static struct udev_seat *
329udev_seat_create(struct udev_input *input, const char *seat_name)
330{
331 struct weston_compositor *c = input->compositor;
332 struct udev_seat *seat;
333
334 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100335 if (!seat)
336 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000337
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100338 weston_seat_init(&seat->base, c, seat_name);
339 seat->base.led_update = udev_seat_led_update;
340
341 seat->output_create_listener.notify = notify_output_create;
342 wl_signal_add(&c->output_created_signal,
343 &seat->output_create_listener);
344
345 wl_list_init(&seat->devices_list);
346
347 return seat;
348}
349
350static void
351udev_seat_destroy(struct udev_seat *seat)
352{
353 udev_seat_remove_devices(seat);
354 if (seat->base.keyboard)
355 notify_keyboard_focus_out(&seat->base);
356 weston_seat_release(&seat->base);
357 wl_list_remove(&seat->output_create_listener.link);
358 free(seat);
359}
360
361struct udev_seat *
362udev_seat_get_named(struct udev_input *input, const char *seat_name)
363{
364 struct udev_seat *seat;
365
366 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
367 if (strcmp(seat->base.seat_name, seat_name) == 0)
368 return seat;
369 }
370
371 return udev_seat_create(input, seat_name);
372}