blob: 953f6205940ab7ba2fb11388e036351f2fecef73 [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
37#include "compositor.h"
38#include "launcher-util.h"
39#include "libinput-seat.h"
40#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010042
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043static void
44process_events(struct udev_input *input);
45static struct udev_seat *
46udev_seat_create(struct udev_input *input, const char *seat_name);
47static void
48udev_seat_destroy(struct udev_seat *seat);
49
Derek Foremana6714fa2015-05-05 15:01:51 -050050static struct udev_seat *
51get_udev_seat(struct udev_input *input, struct libinput_device *device)
52{
53 struct libinput_seat *libinput_seat;
54 const char *seat_name;
55
56 libinput_seat = libinput_device_get_seat(device);
57 seat_name = libinput_seat_get_logical_name(libinput_seat);
58 return udev_seat_get_named(input, seat_name);
59}
60
Jonas Ådahle0de3c22014-03-12 22:08:42 +010061static void
62device_added(struct udev_input *input, struct libinput_device *libinput_device)
63{
64 struct weston_compositor *c;
65 struct evdev_device *device;
66 struct weston_output *output;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010067 const char *output_name;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010068 struct weston_seat *seat;
69 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -050070 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010071
72 c = input->compositor;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010073
Derek Foremana6714fa2015-05-05 15:01:51 -050074 udev_seat = get_udev_seat(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010075 if (!udev_seat)
76 return;
77
78 seat = &udev_seat->base;
79 device = evdev_device_create(libinput_device, seat);
80 if (device == NULL)
81 return;
82
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +030083 if (input->configure_device != NULL)
84 input->configure_device(c, device->device);
85 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010086 udev_seat = (struct udev_seat *) seat;
87 wl_list_insert(udev_seat->devices_list.prev, &device->link);
88
Derek Foreman1281a362015-07-31 16:55:32 -050089 pointer = weston_seat_get_pointer(seat);
90 if (seat->output && pointer)
91 weston_pointer_clamp(pointer,
92 &pointer->x,
93 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010094
95 output_name = libinput_device_get_output_name(libinput_device);
96 if (output_name) {
97 device->output_name = strdup(output_name);
98 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050099 if (output->name &&
100 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100101 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300102 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100103 output = container_of(c->output_list.next,
104 struct weston_output, link);
105 evdev_device_set_output(device, output);
106 }
107
108 if (!input->suspended)
109 weston_seat_repick(seat);
110}
111
112static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500113device_removed(struct udev_input *input, struct libinput_device *libinput_device)
114{
115 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500116
Derek Foreman6b557a72015-05-05 15:01:52 -0500117 device = libinput_device_get_user_data(libinput_device);
118 evdev_device_destroy(device);
119}
120
121static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100122udev_seat_remove_devices(struct udev_seat *seat)
123{
124 struct evdev_device *device, *next;
125
126 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
127 evdev_device_destroy(device);
128 }
129}
130
131void
132udev_input_disable(struct udev_input *input)
133{
134 if (input->suspended)
135 return;
136
Derek Foreman0e4e5702017-07-25 16:39:20 -0500137 wl_event_source_remove(input->libinput_source);
138 input->libinput_source = NULL;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100139 libinput_suspend(input->libinput);
140 process_events(input);
141 input->suspended = 1;
142}
143
144static int
145udev_input_process_event(struct libinput_event *event)
146{
147 struct libinput *libinput = libinput_event_get_context(event);
148 struct libinput_device *libinput_device =
149 libinput_event_get_device(event);
150 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100151 int handled = 1;
152
153 switch (libinput_event_get_type(event)) {
154 case LIBINPUT_EVENT_DEVICE_ADDED:
155 device_added(input, libinput_device);
156 break;
157 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500158 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100159 break;
160 default:
161 handled = 0;
162 }
163
164 return handled;
165}
166
167static void
168process_event(struct libinput_event *event)
169{
170 if (udev_input_process_event(event))
171 return;
172 if (evdev_device_process_event(event))
173 return;
174}
175
176static void
177process_events(struct udev_input *input)
178{
179 struct libinput_event *event;
180
181 while ((event = libinput_get_event(input->libinput))) {
182 process_event(event);
183 libinput_event_destroy(event);
184 }
185}
186
187static int
188udev_input_dispatch(struct udev_input *input)
189{
190 if (libinput_dispatch(input->libinput) != 0)
191 weston_log("libinput: Failed to dispatch libinput\n");
192
193 process_events(input);
194
195 return 0;
196}
197
198static int
199libinput_source_dispatch(int fd, uint32_t mask, void *data)
200{
201 struct udev_input *input = data;
202
203 return udev_input_dispatch(input) != 0;
204}
205
206static int
207open_restricted(const char *path, int flags, void *user_data)
208{
209 struct udev_input *input = user_data;
210 struct weston_launcher *launcher = input->compositor->launcher;
211
212 return weston_launcher_open(launcher, path, flags);
213}
214
215static void
216close_restricted(int fd, void *user_data)
217{
218 struct udev_input *input = user_data;
219 struct weston_launcher *launcher = input->compositor->launcher;
220
221 weston_launcher_close(launcher, fd);
222}
223
224const struct libinput_interface libinput_interface = {
225 open_restricted,
226 close_restricted,
227};
228
229int
230udev_input_enable(struct udev_input *input)
231{
232 struct wl_event_loop *loop;
233 struct weston_compositor *c = input->compositor;
234 int fd;
235 struct udev_seat *seat;
236 int devices_found = 0;
237
238 loop = wl_display_get_event_loop(c->wl_display);
239 fd = libinput_get_fd(input->libinput);
240 input->libinput_source =
241 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
242 libinput_source_dispatch, input);
243 if (!input->libinput_source) {
244 return -1;
245 }
246
247 if (input->suspended) {
248 if (libinput_resume(input->libinput) != 0) {
249 wl_event_source_remove(input->libinput_source);
250 input->libinput_source = NULL;
251 return -1;
252 }
253 input->suspended = 0;
254 process_events(input);
255 }
256
257 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
258 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
259
260 if (!wl_list_empty(&seat->devices_list))
261 devices_found = 1;
262 }
263
Daniel Díaz75b71972016-10-21 14:03:13 -0500264 if (devices_found == 0 && !c->require_input) {
265 weston_log("warning: no input devices found, but none required "
266 "as per configuration.\n");
267 return 0;
268 }
269
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100270 if (devices_found == 0) {
271 weston_log(
272 "warning: no input devices on entering Weston. "
273 "Possible causes:\n"
274 "\t- no permissions to read /dev/input/event*\n"
275 "\t- seats misconfigured "
276 "(Weston backend option 'seat', "
277 "udev device property ID_SEAT)\n");
278 return -1;
279 }
280
281 return 0;
282}
283
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700284static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000285libinput_log_func(struct libinput *libinput,
286 enum libinput_log_priority priority,
287 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700288{
289 weston_vlog(format, args);
290}
291
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100292int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300293udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300294 struct udev *udev, const char *seat_id,
295 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100296{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300297 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700298 const char *log_priority = NULL;
299
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100300 memset(input, 0, sizeof *input);
301
302 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300303 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100304
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700305 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700306
Peter Hutterer3b843d32014-06-25 14:07:36 +1000307 input->libinput = libinput_udev_create_context(&libinput_interface,
308 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100309 if (!input->libinput) {
310 return -1;
311 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000312
313 libinput_log_set_handler(input->libinput, &libinput_log_func);
314
315 if (log_priority) {
316 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300317 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000318 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300319 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000320 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300321 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000322 }
323 }
324
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300325 libinput_log_set_priority(input->libinput, priority);
326
Peter Hutterer3b843d32014-06-25 14:07:36 +1000327 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
328 libinput_unref(input->libinput);
329 return -1;
330 }
331
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100332 process_events(input);
333
334 return udev_input_enable(input);
335}
336
337void
338udev_input_destroy(struct udev_input *input)
339{
340 struct udev_seat *seat, *next;
341
Derek Foreman0e4e5702017-07-25 16:39:20 -0500342 if (input->libinput_source)
343 wl_event_source_remove(input->libinput_source);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100344 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
345 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000346 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100347}
348
349static void
350udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
351{
352 struct udev_seat *seat = (struct udev_seat *) seat_base;
353 struct evdev_device *device;
354
355 wl_list_for_each(device, &seat->devices_list, link)
356 evdev_led_update(device, leds);
357}
358
359static void
360notify_output_create(struct wl_listener *listener, void *data)
361{
362 struct udev_seat *seat = container_of(listener, struct udev_seat,
363 output_create_listener);
364 struct evdev_device *device;
365 struct weston_output *output = data;
366
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300367 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100368 if (device->output_name &&
369 strcmp(output->name, device->output_name) == 0) {
370 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100371 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300372
373 if (device->output_name == NULL && device->output == NULL)
374 evdev_device_set_output(device, output);
375 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100376}
377
378static struct udev_seat *
379udev_seat_create(struct udev_input *input, const char *seat_name)
380{
381 struct weston_compositor *c = input->compositor;
382 struct udev_seat *seat;
383
384 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100385 if (!seat)
386 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000387
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100388 weston_seat_init(&seat->base, c, seat_name);
389 seat->base.led_update = udev_seat_led_update;
390
391 seat->output_create_listener.notify = notify_output_create;
392 wl_signal_add(&c->output_created_signal,
393 &seat->output_create_listener);
394
395 wl_list_init(&seat->devices_list);
396
397 return seat;
398}
399
400static void
401udev_seat_destroy(struct udev_seat *seat)
402{
Derek Foreman1281a362015-07-31 16:55:32 -0500403 struct weston_keyboard *keyboard =
404 weston_seat_get_keyboard(&seat->base);
405
Derek Foreman1281a362015-07-31 16:55:32 -0500406 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100407 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500408
409 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100410 weston_seat_release(&seat->base);
411 wl_list_remove(&seat->output_create_listener.link);
412 free(seat);
413}
414
415struct udev_seat *
416udev_seat_get_named(struct udev_input *input, const char *seat_name)
417{
418 struct udev_seat *seat;
419
420 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
421 if (strcmp(seat->base.seat_name, seat_name) == 0)
422 return seat;
423 }
424
425 return udev_seat_create(input, seat_name);
426}