blob: 49fbc3474e5406c542411ee33250bd5d877b456e [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
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010030#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <libinput.h>
35#include <libudev.h>
36
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020037#include <libweston/libweston.h>
Marius Vlad5d649b62019-07-16 23:44:21 +030038#include "backend.h"
Marius Vlad0bf3f5a2019-07-10 17:32:42 +030039#include "libweston-internal.h"
Marius Vlad4e036292019-07-09 00:36:20 +030040#include "weston-log-internal.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010041#include "launcher-util.h"
42#include "libinput-seat.h"
43#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070044#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010045
Jonas Ådahle0de3c22014-03-12 22:08:42 +010046static void
47process_events(struct udev_input *input);
48static struct udev_seat *
49udev_seat_create(struct udev_input *input, const char *seat_name);
50static void
51udev_seat_destroy(struct udev_seat *seat);
52
Derek Foremana6714fa2015-05-05 15:01:51 -050053static struct udev_seat *
54get_udev_seat(struct udev_input *input, struct libinput_device *device)
55{
56 struct libinput_seat *libinput_seat;
57 const char *seat_name;
58
59 libinput_seat = libinput_device_get_seat(device);
60 seat_name = libinput_seat_get_logical_name(libinput_seat);
61 return udev_seat_get_named(input, seat_name);
62}
63
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020064static struct weston_output *
65output_find_by_head_name(struct weston_compositor *compositor,
66 const char *head_name)
67{
68 struct weston_output *output;
69 struct weston_head *head;
70
71 if (!head_name)
72 return NULL;
73
Pekka Paalanen8dc6db82018-03-20 13:29:40 +020074 /* Only enabled outputs with connected heads.
75 * This means force-enabled outputs but with disconnected heads
76 * will be ignored; if the touchscreen doesn't have a video signal,
77 * touching it is meaningless.
78 */
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020079 wl_list_for_each(output, &compositor->output_list, link) {
80 wl_list_for_each(head, &output->head_list, output_link) {
Pekka Paalanen8dc6db82018-03-20 13:29:40 +020081 if (!weston_head_is_connected(head))
82 continue;
83
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020084 if (strcmp(head_name, head->name) == 0)
85 return output;
86 }
87 }
88
89 return NULL;
90}
91
Jonas Ådahle0de3c22014-03-12 22:08:42 +010092static void
93device_added(struct udev_input *input, struct libinput_device *libinput_device)
94{
95 struct weston_compositor *c;
96 struct evdev_device *device;
97 struct weston_output *output;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010098 const char *output_name;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010099 struct weston_seat *seat;
100 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500101 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100102
103 c = input->compositor;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100104
Derek Foremana6714fa2015-05-05 15:01:51 -0500105 udev_seat = get_udev_seat(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100106 if (!udev_seat)
107 return;
108
109 seat = &udev_seat->base;
110 device = evdev_device_create(libinput_device, seat);
111 if (device == NULL)
112 return;
113
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300114 if (input->configure_device != NULL)
115 input->configure_device(c, device->device);
116 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100117 udev_seat = (struct udev_seat *) seat;
118 wl_list_insert(udev_seat->devices_list.prev, &device->link);
119
Derek Foreman1281a362015-07-31 16:55:32 -0500120 pointer = weston_seat_get_pointer(seat);
121 if (seat->output && pointer)
122 weston_pointer_clamp(pointer,
123 &pointer->x,
124 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100125
126 output_name = libinput_device_get_output_name(libinput_device);
127 if (output_name) {
128 device->output_name = strdup(output_name);
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200129 output = output_find_by_head_name(c, output_name);
130 evdev_device_set_output(device, output);
131 } else if (!wl_list_empty(&c->output_list)) {
132 /* default assignment to an arbitrary output */
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100133 output = container_of(c->output_list.next,
134 struct weston_output, link);
135 evdev_device_set_output(device, output);
136 }
137
Samu Nuutamo58a2faf2018-10-29 17:42:48 +0200138 if (!input->suspended) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100139 weston_seat_repick(seat);
Samu Nuutamo58a2faf2018-10-29 17:42:48 +0200140
141 /* Sync device leds with the actual state */
142 if (seat->led_update && seat->keyboard_state)
143 seat->led_update(seat, seat->keyboard_state->xkb_state.leds);
144 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100145}
146
147static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500148device_removed(struct udev_input *input, struct libinput_device *libinput_device)
149{
150 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500151
Derek Foreman6b557a72015-05-05 15:01:52 -0500152 device = libinput_device_get_user_data(libinput_device);
153 evdev_device_destroy(device);
154}
155
156static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100157udev_seat_remove_devices(struct udev_seat *seat)
158{
159 struct evdev_device *device, *next;
160
161 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
162 evdev_device_destroy(device);
163 }
164}
165
166void
167udev_input_disable(struct udev_input *input)
168{
169 if (input->suspended)
170 return;
171
Derek Foreman0e4e5702017-07-25 16:39:20 -0500172 wl_event_source_remove(input->libinput_source);
173 input->libinput_source = NULL;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100174 libinput_suspend(input->libinput);
175 process_events(input);
176 input->suspended = 1;
177}
178
179static int
180udev_input_process_event(struct libinput_event *event)
181{
182 struct libinput *libinput = libinput_event_get_context(event);
183 struct libinput_device *libinput_device =
184 libinput_event_get_device(event);
185 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100186 int handled = 1;
187
188 switch (libinput_event_get_type(event)) {
189 case LIBINPUT_EVENT_DEVICE_ADDED:
190 device_added(input, libinput_device);
191 break;
192 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500193 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100194 break;
195 default:
196 handled = 0;
197 }
198
199 return handled;
200}
201
202static void
203process_event(struct libinput_event *event)
204{
205 if (udev_input_process_event(event))
206 return;
207 if (evdev_device_process_event(event))
208 return;
209}
210
211static void
212process_events(struct udev_input *input)
213{
214 struct libinput_event *event;
215
216 while ((event = libinput_get_event(input->libinput))) {
217 process_event(event);
218 libinput_event_destroy(event);
219 }
220}
221
222static int
223udev_input_dispatch(struct udev_input *input)
224{
225 if (libinput_dispatch(input->libinput) != 0)
226 weston_log("libinput: Failed to dispatch libinput\n");
227
228 process_events(input);
229
230 return 0;
231}
232
233static int
234libinput_source_dispatch(int fd, uint32_t mask, void *data)
235{
236 struct udev_input *input = data;
237
238 return udev_input_dispatch(input) != 0;
239}
240
241static int
242open_restricted(const char *path, int flags, void *user_data)
243{
244 struct udev_input *input = user_data;
245 struct weston_launcher *launcher = input->compositor->launcher;
246
247 return weston_launcher_open(launcher, path, flags);
248}
249
250static void
251close_restricted(int fd, void *user_data)
252{
253 struct udev_input *input = user_data;
254 struct weston_launcher *launcher = input->compositor->launcher;
255
256 weston_launcher_close(launcher, fd);
257}
258
259const struct libinput_interface libinput_interface = {
260 open_restricted,
261 close_restricted,
262};
263
264int
265udev_input_enable(struct udev_input *input)
266{
267 struct wl_event_loop *loop;
268 struct weston_compositor *c = input->compositor;
269 int fd;
270 struct udev_seat *seat;
271 int devices_found = 0;
272
273 loop = wl_display_get_event_loop(c->wl_display);
274 fd = libinput_get_fd(input->libinput);
275 input->libinput_source =
276 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
277 libinput_source_dispatch, input);
278 if (!input->libinput_source) {
279 return -1;
280 }
281
282 if (input->suspended) {
283 if (libinput_resume(input->libinput) != 0) {
284 wl_event_source_remove(input->libinput_source);
285 input->libinput_source = NULL;
286 return -1;
287 }
288 input->suspended = 0;
289 process_events(input);
290 }
291
292 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
293 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
294
295 if (!wl_list_empty(&seat->devices_list))
296 devices_found = 1;
297 }
298
Daniel Díaz75b71972016-10-21 14:03:13 -0500299 if (devices_found == 0 && !c->require_input) {
300 weston_log("warning: no input devices found, but none required "
301 "as per configuration.\n");
302 return 0;
303 }
304
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100305 if (devices_found == 0) {
306 weston_log(
307 "warning: no input devices on entering Weston. "
308 "Possible causes:\n"
309 "\t- no permissions to read /dev/input/event*\n"
310 "\t- seats misconfigured "
311 "(Weston backend option 'seat', "
312 "udev device property ID_SEAT)\n");
313 return -1;
314 }
315
316 return 0;
317}
318
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700319static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000320libinput_log_func(struct libinput *libinput,
321 enum libinput_log_priority priority,
322 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700323{
324 weston_vlog(format, args);
325}
326
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100327int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300328udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300329 struct udev *udev, const char *seat_id,
330 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100331{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300332 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700333 const char *log_priority = NULL;
334
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100335 memset(input, 0, sizeof *input);
336
337 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300338 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700340 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700341
Peter Hutterer3b843d32014-06-25 14:07:36 +1000342 input->libinput = libinput_udev_create_context(&libinput_interface,
343 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100344 if (!input->libinput) {
345 return -1;
346 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000347
348 libinput_log_set_handler(input->libinput, &libinput_log_func);
349
350 if (log_priority) {
351 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300352 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000353 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300354 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000355 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300356 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000357 }
358 }
359
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300360 libinput_log_set_priority(input->libinput, priority);
361
Peter Hutterer3b843d32014-06-25 14:07:36 +1000362 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
363 libinput_unref(input->libinput);
364 return -1;
365 }
366
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100367 process_events(input);
368
369 return udev_input_enable(input);
370}
371
372void
373udev_input_destroy(struct udev_input *input)
374{
375 struct udev_seat *seat, *next;
376
Derek Foreman0e4e5702017-07-25 16:39:20 -0500377 if (input->libinput_source)
378 wl_event_source_remove(input->libinput_source);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100379 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
380 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000381 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100382}
383
384static void
385udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
386{
387 struct udev_seat *seat = (struct udev_seat *) seat_base;
388 struct evdev_device *device;
389
390 wl_list_for_each(device, &seat->devices_list, link)
391 evdev_led_update(device, leds);
392}
393
394static void
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200395udev_seat_output_changed(struct udev_seat *seat, struct weston_output *output)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100396{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100397 struct evdev_device *device;
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200398 struct weston_output *found;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100399
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300400 wl_list_for_each(device, &seat->devices_list, link) {
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200401 /* If we find any input device without an associated output
402 * or an output name to associate with, just tie it with the
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +0100403 * output we got here - the default assignment.
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200404 */
405 if (!device->output_name) {
406 if (!device->output)
407 evdev_device_set_output(device, output);
408
409 continue;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100410 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300411
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200412 /* Update all devices' output associations, may they gain or
413 * lose it.
414 */
415 found = output_find_by_head_name(output->compositor,
416 device->output_name);
417 evdev_device_set_output(device, found);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300418 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100419}
420
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200421static void
422notify_output_create(struct wl_listener *listener, void *data)
423{
424 struct udev_seat *seat = container_of(listener, struct udev_seat,
425 output_create_listener);
426 struct weston_output *output = data;
427
428 udev_seat_output_changed(seat, output);
429}
430
431static void
432notify_output_heads_changed(struct wl_listener *listener, void *data)
433{
434 struct udev_seat *seat = container_of(listener, struct udev_seat,
435 output_heads_listener);
436 struct weston_output *output = data;
437
438 udev_seat_output_changed(seat, output);
439}
440
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100441static struct udev_seat *
442udev_seat_create(struct udev_input *input, const char *seat_name)
443{
444 struct weston_compositor *c = input->compositor;
445 struct udev_seat *seat;
446
447 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100448 if (!seat)
449 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000450
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100451 weston_seat_init(&seat->base, c, seat_name);
452 seat->base.led_update = udev_seat_led_update;
453
454 seat->output_create_listener.notify = notify_output_create;
455 wl_signal_add(&c->output_created_signal,
456 &seat->output_create_listener);
457
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200458 seat->output_heads_listener.notify = notify_output_heads_changed;
459 wl_signal_add(&c->output_heads_changed_signal,
460 &seat->output_heads_listener);
461
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100462 wl_list_init(&seat->devices_list);
463
464 return seat;
465}
466
467static void
468udev_seat_destroy(struct udev_seat *seat)
469{
Derek Foreman1281a362015-07-31 16:55:32 -0500470 struct weston_keyboard *keyboard =
471 weston_seat_get_keyboard(&seat->base);
472
Derek Foreman1281a362015-07-31 16:55:32 -0500473 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100474 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500475
476 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100477 weston_seat_release(&seat->base);
478 wl_list_remove(&seat->output_create_listener.link);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200479 wl_list_remove(&seat->output_heads_listener.link);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100480 free(seat);
481}
482
483struct udev_seat *
484udev_seat_get_named(struct udev_input *input, const char *seat_name)
485{
486 struct udev_seat *seat;
487
488 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
489 if (strcmp(seat->base.seat_name, seat_name) == 0)
490 return seat;
491 }
492
493 return udev_seat_create(input, seat_name);
494}