blob: dcf335eb661a1dd8f2daeb0b567982e3df23f812 [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
Marius Vlade9a0c062021-08-27 14:01:38 +030092static int
Jonas Ådahle0de3c22014-03-12 22:08:42 +010093device_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);
Marius Vlade9a0c062021-08-27 14:01:38 +0300106 if (!udev_seat) {
107 weston_log("Failed to get a seat\n");
108 return 1;
109 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100110
111 seat = &udev_seat->base;
112 device = evdev_device_create(libinput_device, seat);
Marius Vlade9a0c062021-08-27 14:01:38 +0300113 if (device == NULL) {
114 weston_log("Failed to create a device\n");
115 return 1;
116 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100117
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300118 if (input->configure_device != NULL)
119 input->configure_device(c, device->device);
120 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100121 udev_seat = (struct udev_seat *) seat;
122 wl_list_insert(udev_seat->devices_list.prev, &device->link);
123
Derek Foreman1281a362015-07-31 16:55:32 -0500124 pointer = weston_seat_get_pointer(seat);
125 if (seat->output && pointer)
126 weston_pointer_clamp(pointer,
127 &pointer->x,
128 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100129
130 output_name = libinput_device_get_output_name(libinput_device);
131 if (output_name) {
132 device->output_name = strdup(output_name);
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200133 output = output_find_by_head_name(c, output_name);
134 evdev_device_set_output(device, output);
135 } else if (!wl_list_empty(&c->output_list)) {
136 /* default assignment to an arbitrary output */
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100137 output = container_of(c->output_list.next,
138 struct weston_output, link);
139 evdev_device_set_output(device, output);
140 }
141
Samu Nuutamo58a2faf2018-10-29 17:42:48 +0200142 if (!input->suspended) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100143 weston_seat_repick(seat);
Samu Nuutamo58a2faf2018-10-29 17:42:48 +0200144
145 /* Sync device leds with the actual state */
146 if (seat->led_update && seat->keyboard_state)
147 seat->led_update(seat, seat->keyboard_state->xkb_state.leds);
148 }
Marius Vlade9a0c062021-08-27 14:01:38 +0300149
150 return 0;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100151}
152
Marius Vlade9a0c062021-08-27 14:01:38 +0300153static int
Derek Foreman6b557a72015-05-05 15:01:52 -0500154device_removed(struct udev_input *input, struct libinput_device *libinput_device)
155{
156 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500157
Derek Foreman6b557a72015-05-05 15:01:52 -0500158 device = libinput_device_get_user_data(libinput_device);
Marius Vlade9a0c062021-08-27 14:01:38 +0300159 if (!device) {
160 weston_log("Failed to retrieve device\n");
161 return 1;
162 }
Marius Vladeb34f822021-03-30 23:09:05 +0300163
Derek Foreman6b557a72015-05-05 15:01:52 -0500164 evdev_device_destroy(device);
Marius Vlade9a0c062021-08-27 14:01:38 +0300165 return 0;
Derek Foreman6b557a72015-05-05 15:01:52 -0500166}
167
168static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100169udev_seat_remove_devices(struct udev_seat *seat)
170{
171 struct evdev_device *device, *next;
172
173 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
174 evdev_device_destroy(device);
175 }
176}
177
178void
179udev_input_disable(struct udev_input *input)
180{
181 if (input->suspended)
182 return;
limin.tian930711f2024-11-08 08:11:08 +0000183 if (input && input->libinput_source) {
184 wl_event_source_remove(input->libinput_source);
185 input->libinput_source = NULL;
186 //libinput_suspend(input->libinput);
187 process_events(input);
188 //input->suspended = 1;
189 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100190}
191
192static int
193udev_input_process_event(struct libinput_event *event)
194{
195 struct libinput *libinput = libinput_event_get_context(event);
196 struct libinput_device *libinput_device =
197 libinput_event_get_device(event);
198 struct udev_input *input = libinput_get_user_data(libinput);
Marius Vlade9a0c062021-08-27 14:01:38 +0300199 int ret = 0;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100200
201 switch (libinput_event_get_type(event)) {
202 case LIBINPUT_EVENT_DEVICE_ADDED:
Marius Vlade9a0c062021-08-27 14:01:38 +0300203 ret = device_added(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100204 break;
205 case LIBINPUT_EVENT_DEVICE_REMOVED:
Marius Vlade9a0c062021-08-27 14:01:38 +0300206 ret = device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100207 break;
208 default:
Marius Vlade9a0c062021-08-27 14:01:38 +0300209 evdev_device_process_event(event);
210 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100211 }
212
Marius Vlade9a0c062021-08-27 14:01:38 +0300213 return ret;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100214}
215
216static void
217process_event(struct libinput_event *event)
218{
219 if (udev_input_process_event(event))
Marius Vlade9a0c062021-08-27 14:01:38 +0300220 exit(EXIT_FAILURE);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100221}
222
223static void
224process_events(struct udev_input *input)
225{
226 struct libinput_event *event;
227
228 while ((event = libinput_get_event(input->libinput))) {
229 process_event(event);
230 libinput_event_destroy(event);
231 }
232}
233
234static int
235udev_input_dispatch(struct udev_input *input)
236{
237 if (libinput_dispatch(input->libinput) != 0)
238 weston_log("libinput: Failed to dispatch libinput\n");
239
240 process_events(input);
241
242 return 0;
243}
244
245static int
246libinput_source_dispatch(int fd, uint32_t mask, void *data)
247{
248 struct udev_input *input = data;
249
250 return udev_input_dispatch(input) != 0;
251}
252
253static int
254open_restricted(const char *path, int flags, void *user_data)
255{
256 struct udev_input *input = user_data;
257 struct weston_launcher *launcher = input->compositor->launcher;
258
259 return weston_launcher_open(launcher, path, flags);
260}
261
262static void
263close_restricted(int fd, void *user_data)
264{
265 struct udev_input *input = user_data;
266 struct weston_launcher *launcher = input->compositor->launcher;
267
268 weston_launcher_close(launcher, fd);
269}
270
271const struct libinput_interface libinput_interface = {
272 open_restricted,
273 close_restricted,
274};
275
276int
277udev_input_enable(struct udev_input *input)
278{
279 struct wl_event_loop *loop;
280 struct weston_compositor *c = input->compositor;
281 int fd;
282 struct udev_seat *seat;
283 int devices_found = 0;
284
285 loop = wl_display_get_event_loop(c->wl_display);
286 fd = libinput_get_fd(input->libinput);
287 input->libinput_source =
288 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
289 libinput_source_dispatch, input);
290 if (!input->libinput_source) {
291 return -1;
292 }
293
294 if (input->suspended) {
295 if (libinput_resume(input->libinput) != 0) {
296 wl_event_source_remove(input->libinput_source);
297 input->libinput_source = NULL;
298 return -1;
299 }
300 input->suspended = 0;
301 process_events(input);
302 }
303
304 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
305 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
306
307 if (!wl_list_empty(&seat->devices_list))
308 devices_found = 1;
309 }
310
Daniel Díaz75b71972016-10-21 14:03:13 -0500311 if (devices_found == 0 && !c->require_input) {
312 weston_log("warning: no input devices found, but none required "
313 "as per configuration.\n");
314 return 0;
315 }
316
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100317 if (devices_found == 0) {
318 weston_log(
319 "warning: no input devices on entering Weston. "
320 "Possible causes:\n"
321 "\t- no permissions to read /dev/input/event*\n"
322 "\t- seats misconfigured "
323 "(Weston backend option 'seat', "
324 "udev device property ID_SEAT)\n");
325 return -1;
326 }
327
328 return 0;
329}
330
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700331static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000332libinput_log_func(struct libinput *libinput,
333 enum libinput_log_priority priority,
334 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700335{
336 weston_vlog(format, args);
337}
338
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300340udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300341 struct udev *udev, const char *seat_id,
342 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100343{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300344 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700345 const char *log_priority = NULL;
346
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100347 memset(input, 0, sizeof *input);
348
349 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300350 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100351
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700352 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700353
Peter Hutterer3b843d32014-06-25 14:07:36 +1000354 input->libinput = libinput_udev_create_context(&libinput_interface,
355 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356 if (!input->libinput) {
357 return -1;
358 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000359
360 libinput_log_set_handler(input->libinput, &libinput_log_func);
361
362 if (log_priority) {
363 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300364 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000365 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300366 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000367 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300368 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000369 }
370 }
371
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300372 libinput_log_set_priority(input->libinput, priority);
373
Peter Hutterer3b843d32014-06-25 14:07:36 +1000374 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
375 libinput_unref(input->libinput);
376 return -1;
377 }
378
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100379 process_events(input);
380
381 return udev_input_enable(input);
382}
383
384void
385udev_input_destroy(struct udev_input *input)
386{
387 struct udev_seat *seat, *next;
388
Derek Foreman0e4e5702017-07-25 16:39:20 -0500389 if (input->libinput_source)
390 wl_event_source_remove(input->libinput_source);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100391 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
392 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000393 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100394}
395
396static void
397udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
398{
399 struct udev_seat *seat = (struct udev_seat *) seat_base;
400 struct evdev_device *device;
401
402 wl_list_for_each(device, &seat->devices_list, link)
403 evdev_led_update(device, leds);
404}
405
406static void
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200407udev_seat_output_changed(struct udev_seat *seat, struct weston_output *output)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100408{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100409 struct evdev_device *device;
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200410 struct weston_output *found;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100411
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300412 wl_list_for_each(device, &seat->devices_list, link) {
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200413 /* If we find any input device without an associated output
414 * or an output name to associate with, just tie it with the
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +0100415 * output we got here - the default assignment.
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200416 */
417 if (!device->output_name) {
418 if (!device->output)
419 evdev_device_set_output(device, output);
420
421 continue;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100422 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300423
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200424 /* Update all devices' output associations, may they gain or
425 * lose it.
426 */
427 found = output_find_by_head_name(output->compositor,
428 device->output_name);
429 evdev_device_set_output(device, found);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300430 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100431}
432
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200433static void
434notify_output_create(struct wl_listener *listener, void *data)
435{
436 struct udev_seat *seat = container_of(listener, struct udev_seat,
437 output_create_listener);
438 struct weston_output *output = data;
439
440 udev_seat_output_changed(seat, output);
441}
442
443static void
444notify_output_heads_changed(struct wl_listener *listener, void *data)
445{
446 struct udev_seat *seat = container_of(listener, struct udev_seat,
447 output_heads_listener);
448 struct weston_output *output = data;
449
450 udev_seat_output_changed(seat, output);
451}
452
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100453static struct udev_seat *
454udev_seat_create(struct udev_input *input, const char *seat_name)
455{
456 struct weston_compositor *c = input->compositor;
457 struct udev_seat *seat;
458
459 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100460 if (!seat)
461 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000462
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100463 weston_seat_init(&seat->base, c, seat_name);
464 seat->base.led_update = udev_seat_led_update;
465
466 seat->output_create_listener.notify = notify_output_create;
467 wl_signal_add(&c->output_created_signal,
468 &seat->output_create_listener);
469
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200470 seat->output_heads_listener.notify = notify_output_heads_changed;
471 wl_signal_add(&c->output_heads_changed_signal,
472 &seat->output_heads_listener);
473
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100474 wl_list_init(&seat->devices_list);
475
476 return seat;
477}
478
479static void
480udev_seat_destroy(struct udev_seat *seat)
481{
Derek Foreman1281a362015-07-31 16:55:32 -0500482 struct weston_keyboard *keyboard =
483 weston_seat_get_keyboard(&seat->base);
484
Derek Foreman1281a362015-07-31 16:55:32 -0500485 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100486 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500487
488 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100489 weston_seat_release(&seat->base);
490 wl_list_remove(&seat->output_create_listener.link);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200491 wl_list_remove(&seat->output_heads_listener.link);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100492 free(seat);
493}
494
495struct udev_seat *
496udev_seat_get_named(struct udev_input *input, const char *seat_name)
497{
498 struct udev_seat *seat;
499
500 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
501 if (strcmp(seat->base.seat_name, seat_name) == 0)
502 return seat;
503 }
504
505 return udev_seat_create(input, seat_name);
506}