blob: d47a0323d3d35e92b4cca40ee1a5a534dc6325c1 [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 Vlad0bf3f5a2019-07-10 17:32:42 +030038#include "libweston-internal.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010039#include "launcher-util.h"
40#include "libinput-seat.h"
41#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070042#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043
Jonas Ådahle0de3c22014-03-12 22:08:42 +010044static void
45process_events(struct udev_input *input);
46static struct udev_seat *
47udev_seat_create(struct udev_input *input, const char *seat_name);
48static void
49udev_seat_destroy(struct udev_seat *seat);
50
Derek Foremana6714fa2015-05-05 15:01:51 -050051static struct udev_seat *
52get_udev_seat(struct udev_input *input, struct libinput_device *device)
53{
54 struct libinput_seat *libinput_seat;
55 const char *seat_name;
56
57 libinput_seat = libinput_device_get_seat(device);
58 seat_name = libinput_seat_get_logical_name(libinput_seat);
59 return udev_seat_get_named(input, seat_name);
60}
61
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020062static struct weston_output *
63output_find_by_head_name(struct weston_compositor *compositor,
64 const char *head_name)
65{
66 struct weston_output *output;
67 struct weston_head *head;
68
69 if (!head_name)
70 return NULL;
71
Pekka Paalanen8dc6db82018-03-20 13:29:40 +020072 /* Only enabled outputs with connected heads.
73 * This means force-enabled outputs but with disconnected heads
74 * will be ignored; if the touchscreen doesn't have a video signal,
75 * touching it is meaningless.
76 */
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020077 wl_list_for_each(output, &compositor->output_list, link) {
78 wl_list_for_each(head, &output->head_list, output_link) {
Pekka Paalanen8dc6db82018-03-20 13:29:40 +020079 if (!weston_head_is_connected(head))
80 continue;
81
Pekka Paalanenfd02efb2018-03-19 14:55:41 +020082 if (strcmp(head_name, head->name) == 0)
83 return output;
84 }
85 }
86
87 return NULL;
88}
89
Jonas Ådahle0de3c22014-03-12 22:08:42 +010090static void
91device_added(struct udev_input *input, struct libinput_device *libinput_device)
92{
93 struct weston_compositor *c;
94 struct evdev_device *device;
95 struct weston_output *output;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010096 const char *output_name;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010097 struct weston_seat *seat;
98 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -050099 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100100
101 c = input->compositor;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100102
Derek Foremana6714fa2015-05-05 15:01:51 -0500103 udev_seat = get_udev_seat(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100104 if (!udev_seat)
105 return;
106
107 seat = &udev_seat->base;
108 device = evdev_device_create(libinput_device, seat);
109 if (device == NULL)
110 return;
111
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300112 if (input->configure_device != NULL)
113 input->configure_device(c, device->device);
114 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100115 udev_seat = (struct udev_seat *) seat;
116 wl_list_insert(udev_seat->devices_list.prev, &device->link);
117
Derek Foreman1281a362015-07-31 16:55:32 -0500118 pointer = weston_seat_get_pointer(seat);
119 if (seat->output && pointer)
120 weston_pointer_clamp(pointer,
121 &pointer->x,
122 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100123
124 output_name = libinput_device_get_output_name(libinput_device);
125 if (output_name) {
126 device->output_name = strdup(output_name);
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200127 output = output_find_by_head_name(c, output_name);
128 evdev_device_set_output(device, output);
129 } else if (!wl_list_empty(&c->output_list)) {
130 /* default assignment to an arbitrary output */
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100131 output = container_of(c->output_list.next,
132 struct weston_output, link);
133 evdev_device_set_output(device, output);
134 }
135
136 if (!input->suspended)
137 weston_seat_repick(seat);
138}
139
140static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500141device_removed(struct udev_input *input, struct libinput_device *libinput_device)
142{
143 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500144
Derek Foreman6b557a72015-05-05 15:01:52 -0500145 device = libinput_device_get_user_data(libinput_device);
146 evdev_device_destroy(device);
147}
148
149static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100150udev_seat_remove_devices(struct udev_seat *seat)
151{
152 struct evdev_device *device, *next;
153
154 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
155 evdev_device_destroy(device);
156 }
157}
158
159void
160udev_input_disable(struct udev_input *input)
161{
162 if (input->suspended)
163 return;
164
Derek Foreman0e4e5702017-07-25 16:39:20 -0500165 wl_event_source_remove(input->libinput_source);
166 input->libinput_source = NULL;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100167 libinput_suspend(input->libinput);
168 process_events(input);
169 input->suspended = 1;
170}
171
172static int
173udev_input_process_event(struct libinput_event *event)
174{
175 struct libinput *libinput = libinput_event_get_context(event);
176 struct libinput_device *libinput_device =
177 libinput_event_get_device(event);
178 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100179 int handled = 1;
180
181 switch (libinput_event_get_type(event)) {
182 case LIBINPUT_EVENT_DEVICE_ADDED:
183 device_added(input, libinput_device);
184 break;
185 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500186 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100187 break;
188 default:
189 handled = 0;
190 }
191
192 return handled;
193}
194
195static void
196process_event(struct libinput_event *event)
197{
198 if (udev_input_process_event(event))
199 return;
200 if (evdev_device_process_event(event))
201 return;
202}
203
204static void
205process_events(struct udev_input *input)
206{
207 struct libinput_event *event;
208
209 while ((event = libinput_get_event(input->libinput))) {
210 process_event(event);
211 libinput_event_destroy(event);
212 }
213}
214
215static int
216udev_input_dispatch(struct udev_input *input)
217{
218 if (libinput_dispatch(input->libinput) != 0)
219 weston_log("libinput: Failed to dispatch libinput\n");
220
221 process_events(input);
222
223 return 0;
224}
225
226static int
227libinput_source_dispatch(int fd, uint32_t mask, void *data)
228{
229 struct udev_input *input = data;
230
231 return udev_input_dispatch(input) != 0;
232}
233
234static int
235open_restricted(const char *path, int flags, void *user_data)
236{
237 struct udev_input *input = user_data;
238 struct weston_launcher *launcher = input->compositor->launcher;
239
240 return weston_launcher_open(launcher, path, flags);
241}
242
243static void
244close_restricted(int fd, void *user_data)
245{
246 struct udev_input *input = user_data;
247 struct weston_launcher *launcher = input->compositor->launcher;
248
249 weston_launcher_close(launcher, fd);
250}
251
252const struct libinput_interface libinput_interface = {
253 open_restricted,
254 close_restricted,
255};
256
257int
258udev_input_enable(struct udev_input *input)
259{
260 struct wl_event_loop *loop;
261 struct weston_compositor *c = input->compositor;
262 int fd;
263 struct udev_seat *seat;
264 int devices_found = 0;
265
266 loop = wl_display_get_event_loop(c->wl_display);
267 fd = libinput_get_fd(input->libinput);
268 input->libinput_source =
269 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
270 libinput_source_dispatch, input);
271 if (!input->libinput_source) {
272 return -1;
273 }
274
275 if (input->suspended) {
276 if (libinput_resume(input->libinput) != 0) {
277 wl_event_source_remove(input->libinput_source);
278 input->libinput_source = NULL;
279 return -1;
280 }
281 input->suspended = 0;
282 process_events(input);
283 }
284
285 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
286 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
287
288 if (!wl_list_empty(&seat->devices_list))
289 devices_found = 1;
290 }
291
Daniel Díaz75b71972016-10-21 14:03:13 -0500292 if (devices_found == 0 && !c->require_input) {
293 weston_log("warning: no input devices found, but none required "
294 "as per configuration.\n");
295 return 0;
296 }
297
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100298 if (devices_found == 0) {
299 weston_log(
300 "warning: no input devices on entering Weston. "
301 "Possible causes:\n"
302 "\t- no permissions to read /dev/input/event*\n"
303 "\t- seats misconfigured "
304 "(Weston backend option 'seat', "
305 "udev device property ID_SEAT)\n");
306 return -1;
307 }
308
309 return 0;
310}
311
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700312static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000313libinput_log_func(struct libinput *libinput,
314 enum libinput_log_priority priority,
315 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700316{
317 weston_vlog(format, args);
318}
319
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100320int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300321udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300322 struct udev *udev, const char *seat_id,
323 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100324{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300325 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700326 const char *log_priority = NULL;
327
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100328 memset(input, 0, sizeof *input);
329
330 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300331 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100332
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700333 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700334
Peter Hutterer3b843d32014-06-25 14:07:36 +1000335 input->libinput = libinput_udev_create_context(&libinput_interface,
336 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100337 if (!input->libinput) {
338 return -1;
339 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000340
341 libinput_log_set_handler(input->libinput, &libinput_log_func);
342
343 if (log_priority) {
344 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300345 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000346 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300347 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000348 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300349 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000350 }
351 }
352
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300353 libinput_log_set_priority(input->libinput, priority);
354
Peter Hutterer3b843d32014-06-25 14:07:36 +1000355 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
356 libinput_unref(input->libinput);
357 return -1;
358 }
359
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100360 process_events(input);
361
362 return udev_input_enable(input);
363}
364
365void
366udev_input_destroy(struct udev_input *input)
367{
368 struct udev_seat *seat, *next;
369
Derek Foreman0e4e5702017-07-25 16:39:20 -0500370 if (input->libinput_source)
371 wl_event_source_remove(input->libinput_source);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100372 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
373 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000374 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100375}
376
377static void
378udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
379{
380 struct udev_seat *seat = (struct udev_seat *) seat_base;
381 struct evdev_device *device;
382
383 wl_list_for_each(device, &seat->devices_list, link)
384 evdev_led_update(device, leds);
385}
386
387static void
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200388udev_seat_output_changed(struct udev_seat *seat, struct weston_output *output)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100389{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100390 struct evdev_device *device;
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200391 struct weston_output *found;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100392
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300393 wl_list_for_each(device, &seat->devices_list, link) {
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200394 /* If we find any input device without an associated output
395 * or an output name to associate with, just tie it with the
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +0100396 * output we got here - the default assignment.
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200397 */
398 if (!device->output_name) {
399 if (!device->output)
400 evdev_device_set_output(device, output);
401
402 continue;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100403 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300404
Pekka Paalanenfd02efb2018-03-19 14:55:41 +0200405 /* Update all devices' output associations, may they gain or
406 * lose it.
407 */
408 found = output_find_by_head_name(output->compositor,
409 device->output_name);
410 evdev_device_set_output(device, found);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300411 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100412}
413
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200414static void
415notify_output_create(struct wl_listener *listener, void *data)
416{
417 struct udev_seat *seat = container_of(listener, struct udev_seat,
418 output_create_listener);
419 struct weston_output *output = data;
420
421 udev_seat_output_changed(seat, output);
422}
423
424static void
425notify_output_heads_changed(struct wl_listener *listener, void *data)
426{
427 struct udev_seat *seat = container_of(listener, struct udev_seat,
428 output_heads_listener);
429 struct weston_output *output = data;
430
431 udev_seat_output_changed(seat, output);
432}
433
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100434static struct udev_seat *
435udev_seat_create(struct udev_input *input, const char *seat_name)
436{
437 struct weston_compositor *c = input->compositor;
438 struct udev_seat *seat;
439
440 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100441 if (!seat)
442 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000443
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100444 weston_seat_init(&seat->base, c, seat_name);
445 seat->base.led_update = udev_seat_led_update;
446
447 seat->output_create_listener.notify = notify_output_create;
448 wl_signal_add(&c->output_created_signal,
449 &seat->output_create_listener);
450
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200451 seat->output_heads_listener.notify = notify_output_heads_changed;
452 wl_signal_add(&c->output_heads_changed_signal,
453 &seat->output_heads_listener);
454
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100455 wl_list_init(&seat->devices_list);
456
457 return seat;
458}
459
460static void
461udev_seat_destroy(struct udev_seat *seat)
462{
Derek Foreman1281a362015-07-31 16:55:32 -0500463 struct weston_keyboard *keyboard =
464 weston_seat_get_keyboard(&seat->base);
465
Derek Foreman1281a362015-07-31 16:55:32 -0500466 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100467 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500468
469 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100470 weston_seat_release(&seat->base);
471 wl_list_remove(&seat->output_create_listener.link);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +0200472 wl_list_remove(&seat->output_heads_listener.link);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100473 free(seat);
474}
475
476struct udev_seat *
477udev_seat_get_named(struct udev_input *input, const char *seat_name)
478{
479 struct udev_seat *seat;
480
481 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
482 if (strcmp(seat->base.seat_name, seat_name) == 0)
483 return seat;
484 }
485
486 return udev_seat_create(input, seat_name);
487}