blob: ce79d34e9ec750ba025780463961ad7e0acce4ee [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Jonas Ådahle0de3c22014-03-12 22:08:42 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Jonas Ådahle0de3c22014-03-12 22:08:42 +010025 */
26
27#include "config.h"
28
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <libinput.h>
34#include <libudev.h>
35
36#include "compositor.h"
37#include "launcher-util.h"
38#include "libinput-seat.h"
39#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070040#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010041
42static const char default_seat[] = "seat0";
43static const char default_seat_name[] = "default";
44
45static void
46process_events(struct udev_input *input);
47static struct udev_seat *
48udev_seat_create(struct udev_input *input, const char *seat_name);
49static void
50udev_seat_destroy(struct udev_seat *seat);
51
52static void
53device_added(struct udev_input *input, struct libinput_device *libinput_device)
54{
55 struct weston_compositor *c;
56 struct evdev_device *device;
57 struct weston_output *output;
58 const char *seat_name;
59 const char *output_name;
60 struct libinput_seat *libinput_seat;
61 struct weston_seat *seat;
62 struct udev_seat *udev_seat;
63
64 c = input->compositor;
65 libinput_seat = libinput_device_get_seat(libinput_device);
66
67 seat_name = libinput_seat_get_logical_name(libinput_seat);
68 udev_seat = udev_seat_get_named(input, seat_name);
69 if (!udev_seat)
70 return;
71
72 seat = &udev_seat->base;
73 device = evdev_device_create(libinput_device, seat);
74 if (device == NULL)
75 return;
76
77 udev_seat = (struct udev_seat *) seat;
78 wl_list_insert(udev_seat->devices_list.prev, &device->link);
79
80 if (seat->output && seat->pointer)
81 weston_pointer_clamp(seat->pointer,
82 &seat->pointer->x,
83 &seat->pointer->y);
84
85 output_name = libinput_device_get_output_name(libinput_device);
86 if (output_name) {
87 device->output_name = strdup(output_name);
88 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050089 if (output->name &&
90 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +010091 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030092 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010093 output = container_of(c->output_list.next,
94 struct weston_output, link);
95 evdev_device_set_output(device, output);
96 }
97
98 if (!input->suspended)
99 weston_seat_repick(seat);
100}
101
102static void
103udev_seat_remove_devices(struct udev_seat *seat)
104{
105 struct evdev_device *device, *next;
106
107 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
108 evdev_device_destroy(device);
109 }
110}
111
112void
113udev_input_disable(struct udev_input *input)
114{
115 if (input->suspended)
116 return;
117
118 libinput_suspend(input->libinput);
119 process_events(input);
120 input->suspended = 1;
121}
122
123static int
124udev_input_process_event(struct libinput_event *event)
125{
126 struct libinput *libinput = libinput_event_get_context(event);
127 struct libinput_device *libinput_device =
128 libinput_event_get_device(event);
129 struct udev_input *input = libinput_get_user_data(libinput);
130 struct evdev_device *device;
131 int handled = 1;
132
133 switch (libinput_event_get_type(event)) {
134 case LIBINPUT_EVENT_DEVICE_ADDED:
135 device_added(input, libinput_device);
136 break;
137 case LIBINPUT_EVENT_DEVICE_REMOVED:
138 device = libinput_device_get_user_data(libinput_device);
139 evdev_device_destroy(device);
140 break;
141 default:
142 handled = 0;
143 }
144
145 return handled;
146}
147
148static void
149process_event(struct libinput_event *event)
150{
151 if (udev_input_process_event(event))
152 return;
153 if (evdev_device_process_event(event))
154 return;
155}
156
157static void
158process_events(struct udev_input *input)
159{
160 struct libinput_event *event;
161
162 while ((event = libinput_get_event(input->libinput))) {
163 process_event(event);
164 libinput_event_destroy(event);
165 }
166}
167
168static int
169udev_input_dispatch(struct udev_input *input)
170{
171 if (libinput_dispatch(input->libinput) != 0)
172 weston_log("libinput: Failed to dispatch libinput\n");
173
174 process_events(input);
175
176 return 0;
177}
178
179static int
180libinput_source_dispatch(int fd, uint32_t mask, void *data)
181{
182 struct udev_input *input = data;
183
184 return udev_input_dispatch(input) != 0;
185}
186
187static int
188open_restricted(const char *path, int flags, void *user_data)
189{
190 struct udev_input *input = user_data;
191 struct weston_launcher *launcher = input->compositor->launcher;
192
193 return weston_launcher_open(launcher, path, flags);
194}
195
196static void
197close_restricted(int fd, void *user_data)
198{
199 struct udev_input *input = user_data;
200 struct weston_launcher *launcher = input->compositor->launcher;
201
202 weston_launcher_close(launcher, fd);
203}
204
205const struct libinput_interface libinput_interface = {
206 open_restricted,
207 close_restricted,
208};
209
210int
211udev_input_enable(struct udev_input *input)
212{
213 struct wl_event_loop *loop;
214 struct weston_compositor *c = input->compositor;
215 int fd;
216 struct udev_seat *seat;
217 int devices_found = 0;
218
219 loop = wl_display_get_event_loop(c->wl_display);
220 fd = libinput_get_fd(input->libinput);
221 input->libinput_source =
222 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
223 libinput_source_dispatch, input);
224 if (!input->libinput_source) {
225 return -1;
226 }
227
228 if (input->suspended) {
229 if (libinput_resume(input->libinput) != 0) {
230 wl_event_source_remove(input->libinput_source);
231 input->libinput_source = NULL;
232 return -1;
233 }
234 input->suspended = 0;
235 process_events(input);
236 }
237
238 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
239 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
240
241 if (!wl_list_empty(&seat->devices_list))
242 devices_found = 1;
243 }
244
245 if (devices_found == 0) {
246 weston_log(
247 "warning: no input devices on entering Weston. "
248 "Possible causes:\n"
249 "\t- no permissions to read /dev/input/event*\n"
250 "\t- seats misconfigured "
251 "(Weston backend option 'seat', "
252 "udev device property ID_SEAT)\n");
253 return -1;
254 }
255
256 return 0;
257}
258
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700259static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000260libinput_log_func(struct libinput *libinput,
261 enum libinput_log_priority priority,
262 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700263{
264 weston_vlog(format, args);
265}
266
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100267int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300268udev_input_init(struct udev_input *input, struct weston_compositor *c,
269 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100270{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300271 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700272 const char *log_priority = NULL;
273
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100274 memset(input, 0, sizeof *input);
275
276 input->compositor = c;
277
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700278 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700279
Peter Hutterer3b843d32014-06-25 14:07:36 +1000280 input->libinput = libinput_udev_create_context(&libinput_interface,
281 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100282 if (!input->libinput) {
283 return -1;
284 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000285
286 libinput_log_set_handler(input->libinput, &libinput_log_func);
287
288 if (log_priority) {
289 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300290 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000291 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300292 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000293 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300294 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000295 }
296 }
297
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300298 libinput_log_set_priority(input->libinput, priority);
299
Peter Hutterer3b843d32014-06-25 14:07:36 +1000300 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
301 libinput_unref(input->libinput);
302 return -1;
303 }
304
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100305 process_events(input);
306
307 return udev_input_enable(input);
308}
309
310void
311udev_input_destroy(struct udev_input *input)
312{
313 struct udev_seat *seat, *next;
314
315 wl_event_source_remove(input->libinput_source);
316 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
317 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000318 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100319}
320
321static void
322udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
323{
324 struct udev_seat *seat = (struct udev_seat *) seat_base;
325 struct evdev_device *device;
326
327 wl_list_for_each(device, &seat->devices_list, link)
328 evdev_led_update(device, leds);
329}
330
331static void
332notify_output_create(struct wl_listener *listener, void *data)
333{
334 struct udev_seat *seat = container_of(listener, struct udev_seat,
335 output_create_listener);
336 struct evdev_device *device;
337 struct weston_output *output = data;
338
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300339 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100340 if (device->output_name &&
341 strcmp(output->name, device->output_name) == 0) {
342 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100343 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300344
345 if (device->output_name == NULL && device->output == NULL)
346 evdev_device_set_output(device, output);
347 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100348}
349
350static struct udev_seat *
351udev_seat_create(struct udev_input *input, const char *seat_name)
352{
353 struct weston_compositor *c = input->compositor;
354 struct udev_seat *seat;
355
356 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100357 if (!seat)
358 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000359
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100360 weston_seat_init(&seat->base, c, seat_name);
361 seat->base.led_update = udev_seat_led_update;
362
363 seat->output_create_listener.notify = notify_output_create;
364 wl_signal_add(&c->output_created_signal,
365 &seat->output_create_listener);
366
367 wl_list_init(&seat->devices_list);
368
369 return seat;
370}
371
372static void
373udev_seat_destroy(struct udev_seat *seat)
374{
375 udev_seat_remove_devices(seat);
376 if (seat->base.keyboard)
377 notify_keyboard_focus_out(&seat->base);
378 weston_seat_release(&seat->base);
379 wl_list_remove(&seat->output_create_listener.link);
380 free(seat);
381}
382
383struct udev_seat *
384udev_seat_get_named(struct udev_input *input, const char *seat_name)
385{
386 struct udev_seat *seat;
387
388 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
389 if (strcmp(seat->base.seat_name, seat_name) == 0)
390 return seat;
391 }
392
393 return udev_seat_create(input, seat_name);
394}