blob: 78a5fc4494871e7189b7fb4eb4c74753e36cdc7f [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
137 libinput_suspend(input->libinput);
138 process_events(input);
139 input->suspended = 1;
140}
141
142static int
143udev_input_process_event(struct libinput_event *event)
144{
145 struct libinput *libinput = libinput_event_get_context(event);
146 struct libinput_device *libinput_device =
147 libinput_event_get_device(event);
148 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100149 int handled = 1;
150
151 switch (libinput_event_get_type(event)) {
152 case LIBINPUT_EVENT_DEVICE_ADDED:
153 device_added(input, libinput_device);
154 break;
155 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500156 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100157 break;
158 default:
159 handled = 0;
160 }
161
162 return handled;
163}
164
165static void
166process_event(struct libinput_event *event)
167{
168 if (udev_input_process_event(event))
169 return;
170 if (evdev_device_process_event(event))
171 return;
172}
173
174static void
175process_events(struct udev_input *input)
176{
177 struct libinput_event *event;
178
179 while ((event = libinput_get_event(input->libinput))) {
180 process_event(event);
181 libinput_event_destroy(event);
182 }
183}
184
185static int
186udev_input_dispatch(struct udev_input *input)
187{
188 if (libinput_dispatch(input->libinput) != 0)
189 weston_log("libinput: Failed to dispatch libinput\n");
190
191 process_events(input);
192
193 return 0;
194}
195
196static int
197libinput_source_dispatch(int fd, uint32_t mask, void *data)
198{
199 struct udev_input *input = data;
200
201 return udev_input_dispatch(input) != 0;
202}
203
204static int
205open_restricted(const char *path, int flags, void *user_data)
206{
207 struct udev_input *input = user_data;
208 struct weston_launcher *launcher = input->compositor->launcher;
209
210 return weston_launcher_open(launcher, path, flags);
211}
212
213static void
214close_restricted(int fd, void *user_data)
215{
216 struct udev_input *input = user_data;
217 struct weston_launcher *launcher = input->compositor->launcher;
218
219 weston_launcher_close(launcher, fd);
220}
221
222const struct libinput_interface libinput_interface = {
223 open_restricted,
224 close_restricted,
225};
226
227int
228udev_input_enable(struct udev_input *input)
229{
230 struct wl_event_loop *loop;
231 struct weston_compositor *c = input->compositor;
232 int fd;
233 struct udev_seat *seat;
234 int devices_found = 0;
235
236 loop = wl_display_get_event_loop(c->wl_display);
237 fd = libinput_get_fd(input->libinput);
238 input->libinput_source =
239 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
240 libinput_source_dispatch, input);
241 if (!input->libinput_source) {
242 return -1;
243 }
244
245 if (input->suspended) {
246 if (libinput_resume(input->libinput) != 0) {
247 wl_event_source_remove(input->libinput_source);
248 input->libinput_source = NULL;
249 return -1;
250 }
251 input->suspended = 0;
252 process_events(input);
253 }
254
255 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
256 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
257
258 if (!wl_list_empty(&seat->devices_list))
259 devices_found = 1;
260 }
261
262 if (devices_found == 0) {
263 weston_log(
264 "warning: no input devices on entering Weston. "
265 "Possible causes:\n"
266 "\t- no permissions to read /dev/input/event*\n"
267 "\t- seats misconfigured "
268 "(Weston backend option 'seat', "
269 "udev device property ID_SEAT)\n");
270 return -1;
271 }
272
273 return 0;
274}
275
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700276static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000277libinput_log_func(struct libinput *libinput,
278 enum libinput_log_priority priority,
279 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700280{
281 weston_vlog(format, args);
282}
283
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100284int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300285udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300286 struct udev *udev, const char *seat_id,
287 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100288{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300289 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700290 const char *log_priority = NULL;
291
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100292 memset(input, 0, sizeof *input);
293
294 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300295 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100296
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700297 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700298
Peter Hutterer3b843d32014-06-25 14:07:36 +1000299 input->libinput = libinput_udev_create_context(&libinput_interface,
300 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100301 if (!input->libinput) {
302 return -1;
303 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000304
305 libinput_log_set_handler(input->libinput, &libinput_log_func);
306
307 if (log_priority) {
308 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300309 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000310 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300311 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000312 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300313 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000314 }
315 }
316
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300317 libinput_log_set_priority(input->libinput, priority);
318
Peter Hutterer3b843d32014-06-25 14:07:36 +1000319 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
320 libinput_unref(input->libinput);
321 return -1;
322 }
323
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100324 process_events(input);
325
326 return udev_input_enable(input);
327}
328
329void
330udev_input_destroy(struct udev_input *input)
331{
332 struct udev_seat *seat, *next;
333
334 wl_event_source_remove(input->libinput_source);
335 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
336 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000337 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100338}
339
340static void
341udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
342{
343 struct udev_seat *seat = (struct udev_seat *) seat_base;
344 struct evdev_device *device;
345
346 wl_list_for_each(device, &seat->devices_list, link)
347 evdev_led_update(device, leds);
348}
349
350static void
351notify_output_create(struct wl_listener *listener, void *data)
352{
353 struct udev_seat *seat = container_of(listener, struct udev_seat,
354 output_create_listener);
355 struct evdev_device *device;
356 struct weston_output *output = data;
357
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300358 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100359 if (device->output_name &&
360 strcmp(output->name, device->output_name) == 0) {
361 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100362 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300363
364 if (device->output_name == NULL && device->output == NULL)
365 evdev_device_set_output(device, output);
366 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100367}
368
369static struct udev_seat *
370udev_seat_create(struct udev_input *input, const char *seat_name)
371{
372 struct weston_compositor *c = input->compositor;
373 struct udev_seat *seat;
374
375 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100376 if (!seat)
377 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000378
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100379 weston_seat_init(&seat->base, c, seat_name);
380 seat->base.led_update = udev_seat_led_update;
381
382 seat->output_create_listener.notify = notify_output_create;
383 wl_signal_add(&c->output_created_signal,
384 &seat->output_create_listener);
385
386 wl_list_init(&seat->devices_list);
387
388 return seat;
389}
390
391static void
392udev_seat_destroy(struct udev_seat *seat)
393{
Derek Foreman1281a362015-07-31 16:55:32 -0500394 struct weston_keyboard *keyboard =
395 weston_seat_get_keyboard(&seat->base);
396
Derek Foreman1281a362015-07-31 16:55:32 -0500397 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100398 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500399
400 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100401 weston_seat_release(&seat->base);
402 wl_list_remove(&seat->output_create_listener.link);
403 free(seat);
404}
405
406struct udev_seat *
407udev_seat_get_named(struct udev_input *input, const char *seat_name)
408{
409 struct udev_seat *seat;
410
411 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
412 if (strcmp(seat->base.seat_name, seat_name) == 0)
413 return seat;
414 }
415
416 return udev_seat_create(input, seat_name);
417}