blob: 9801551805b22c7368b9fb55f4d50e40bfa1fc62 [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
138 if (!input->suspended)
139 weston_seat_repick(seat);
140}
141
142static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500143device_removed(struct udev_input *input, struct libinput_device *libinput_device)
144{
145 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500146
Derek Foreman6b557a72015-05-05 15:01:52 -0500147 device = libinput_device_get_user_data(libinput_device);
148 evdev_device_destroy(device);
149}
150
151static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100152udev_seat_remove_devices(struct udev_seat *seat)
153{
154 struct evdev_device *device, *next;
155
156 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
157 evdev_device_destroy(device);
158 }
159}
160
161void
162udev_input_disable(struct udev_input *input)
163{
164 if (input->suspended)
165 return;
166
Derek Foreman0e4e5702017-07-25 16:39:20 -0500167 wl_event_source_remove(input->libinput_source);
168 input->libinput_source = NULL;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100169 libinput_suspend(input->libinput);
170 process_events(input);
171 input->suspended = 1;
172}
173
174static int
175udev_input_process_event(struct libinput_event *event)
176{
177 struct libinput *libinput = libinput_event_get_context(event);
178 struct libinput_device *libinput_device =
179 libinput_event_get_device(event);
180 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100181 int handled = 1;
182
183 switch (libinput_event_get_type(event)) {
184 case LIBINPUT_EVENT_DEVICE_ADDED:
185 device_added(input, libinput_device);
186 break;
187 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500188 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100189 break;
190 default:
191 handled = 0;
192 }
193
194 return handled;
195}
196
197static void
198process_event(struct libinput_event *event)
199{
200 if (udev_input_process_event(event))
201 return;
202 if (evdev_device_process_event(event))
203 return;
204}
205
206static void
207process_events(struct udev_input *input)
208{
209 struct libinput_event *event;
210
211 while ((event = libinput_get_event(input->libinput))) {
212 process_event(event);
213 libinput_event_destroy(event);
214 }
215}
216
217static int
218udev_input_dispatch(struct udev_input *input)
219{
220 if (libinput_dispatch(input->libinput) != 0)
221 weston_log("libinput: Failed to dispatch libinput\n");
222
223 process_events(input);
224
225 return 0;
226}
227
228static int
229libinput_source_dispatch(int fd, uint32_t mask, void *data)
230{
231 struct udev_input *input = data;
232
233 return udev_input_dispatch(input) != 0;
234}
235
236static int
237open_restricted(const char *path, int flags, void *user_data)
238{
239 struct udev_input *input = user_data;
240 struct weston_launcher *launcher = input->compositor->launcher;
241
242 return weston_launcher_open(launcher, path, flags);
243}
244
245static void
246close_restricted(int fd, void *user_data)
247{
248 struct udev_input *input = user_data;
249 struct weston_launcher *launcher = input->compositor->launcher;
250
251 weston_launcher_close(launcher, fd);
252}
253
254const struct libinput_interface libinput_interface = {
255 open_restricted,
256 close_restricted,
257};
258
259int
260udev_input_enable(struct udev_input *input)
261{
262 struct wl_event_loop *loop;
263 struct weston_compositor *c = input->compositor;
264 int fd;
265 struct udev_seat *seat;
266 int devices_found = 0;
267
268 loop = wl_display_get_event_loop(c->wl_display);
269 fd = libinput_get_fd(input->libinput);
270 input->libinput_source =
271 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
272 libinput_source_dispatch, input);
273 if (!input->libinput_source) {
274 return -1;
275 }
276
277 if (input->suspended) {
278 if (libinput_resume(input->libinput) != 0) {
279 wl_event_source_remove(input->libinput_source);
280 input->libinput_source = NULL;
281 return -1;
282 }
283 input->suspended = 0;
284 process_events(input);
285 }
286
287 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
288 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
289
290 if (!wl_list_empty(&seat->devices_list))
291 devices_found = 1;
292 }
293
Daniel Díaz75b71972016-10-21 14:03:13 -0500294 if (devices_found == 0 && !c->require_input) {
295 weston_log("warning: no input devices found, but none required "
296 "as per configuration.\n");
297 return 0;
298 }
299
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100300 if (devices_found == 0) {
301 weston_log(
302 "warning: no input devices on entering Weston. "
303 "Possible causes:\n"
304 "\t- no permissions to read /dev/input/event*\n"
305 "\t- seats misconfigured "
306 "(Weston backend option 'seat', "
307 "udev device property ID_SEAT)\n");
308 return -1;
309 }
310
311 return 0;
312}
313
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700314static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000315libinput_log_func(struct libinput *libinput,
316 enum libinput_log_priority priority,
317 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700318{
319 weston_vlog(format, args);
320}
321
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100322int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300323udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300324 struct udev *udev, const char *seat_id,
325 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100326{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300327 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700328 const char *log_priority = NULL;
329
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100330 memset(input, 0, sizeof *input);
331
332 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300333 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100334
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700335 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700336
Peter Hutterer3b843d32014-06-25 14:07:36 +1000337 input->libinput = libinput_udev_create_context(&libinput_interface,
338 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339 if (!input->libinput) {
340 return -1;
341 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000342
343 libinput_log_set_handler(input->libinput, &libinput_log_func);
344
345 if (log_priority) {
346 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300347 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000348 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300349 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000350 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300351 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000352 }
353 }
354
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300355 libinput_log_set_priority(input->libinput, priority);
356
Peter Hutterer3b843d32014-06-25 14:07:36 +1000357 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
358 libinput_unref(input->libinput);
359 return -1;
360 }
361
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100362 process_events(input);
363
364 return udev_input_enable(input);
365}
366
367void
368udev_input_destroy(struct udev_input *input)
369{
370 struct udev_seat *seat, *next;
371
Derek Foreman0e4e5702017-07-25 16:39:20 -0500372 if (input->libinput_source)
373 wl_event_source_remove(input->libinput_source);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100374 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
375 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000376 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100377}
378
379static void
380udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
381{
382 struct udev_seat *seat = (struct udev_seat *) seat_base;
383 struct evdev_device *device;
384
385 wl_list_for_each(device, &seat->devices_list, link)
386 evdev_led_update(device, leds);
387}
388
389static void
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200390udev_seat_output_changed(struct udev_seat *seat, struct weston_output *output)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100391{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100392 struct evdev_device *device;
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200393 struct weston_output *found;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100394
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300395 wl_list_for_each(device, &seat->devices_list, link) {
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200396 /* If we find any input device without an associated output
397 * or an output name to associate with, just tie it with the
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +0100398 * output we got here - the default assignment.
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200399 */
400 if (!device->output_name) {
401 if (!device->output)
402 evdev_device_set_output(device, output);
403
404 continue;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100405 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300406
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200407 /* Update all devices' output associations, may they gain or
408 * lose it.
409 */
410 found = output_find_by_head_name(output->compositor,
411 device->output_name);
412 evdev_device_set_output(device, found);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300413 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100414}
415
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200416static void
417notify_output_create(struct wl_listener *listener, void *data)
418{
419 struct udev_seat *seat = container_of(listener, struct udev_seat,
420 output_create_listener);
421 struct weston_output *output = data;
422
423 udev_seat_output_changed(seat, output);
424}
425
426static void
427notify_output_heads_changed(struct wl_listener *listener, void *data)
428{
429 struct udev_seat *seat = container_of(listener, struct udev_seat,
430 output_heads_listener);
431 struct weston_output *output = data;
432
433 udev_seat_output_changed(seat, output);
434}
435
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100436static struct udev_seat *
437udev_seat_create(struct udev_input *input, const char *seat_name)
438{
439 struct weston_compositor *c = input->compositor;
440 struct udev_seat *seat;
441
442 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100443 if (!seat)
444 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000445
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100446 weston_seat_init(&seat->base, c, seat_name);
447 seat->base.led_update = udev_seat_led_update;
448
449 seat->output_create_listener.notify = notify_output_create;
450 wl_signal_add(&c->output_created_signal,
451 &seat->output_create_listener);
452
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200453 seat->output_heads_listener.notify = notify_output_heads_changed;
454 wl_signal_add(&c->output_heads_changed_signal,
455 &seat->output_heads_listener);
456
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100457 wl_list_init(&seat->devices_list);
458
459 return seat;
460}
461
462static void
463udev_seat_destroy(struct udev_seat *seat)
464{
Derek Foreman1281a362015-07-31 16:55:32 -0500465 struct weston_keyboard *keyboard =
466 weston_seat_get_keyboard(&seat->base);
467
Derek Foreman1281a362015-07-31 16:55:32 -0500468 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100469 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500470
471 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100472 weston_seat_release(&seat->base);
473 wl_list_remove(&seat->output_create_listener.link);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200474 wl_list_remove(&seat->output_heads_listener.link);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100475 free(seat);
476}
477
478struct udev_seat *
479udev_seat_get_named(struct udev_input *input, const char *seat_name)
480{
481 struct udev_seat *seat;
482
483 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
484 if (strcmp(seat->base.seat_name, seat_name) == 0)
485 return seat;
486 }
487
488 return udev_seat_create(input, seat_name);
489}