blob: 8cf5666b495dc4a2a1d43773cc6de8769c233b72 [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
Daniel Díaz75b71972016-10-21 14:03:13 -0500262 if (devices_found == 0 && !c->require_input) {
263 weston_log("warning: no input devices found, but none required "
264 "as per configuration.\n");
265 return 0;
266 }
267
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100268 if (devices_found == 0) {
269 weston_log(
270 "warning: no input devices on entering Weston. "
271 "Possible causes:\n"
272 "\t- no permissions to read /dev/input/event*\n"
273 "\t- seats misconfigured "
274 "(Weston backend option 'seat', "
275 "udev device property ID_SEAT)\n");
276 return -1;
277 }
278
279 return 0;
280}
281
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700282static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000283libinput_log_func(struct libinput *libinput,
284 enum libinput_log_priority priority,
285 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700286{
287 weston_vlog(format, args);
288}
289
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100290int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300291udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300292 struct udev *udev, const char *seat_id,
293 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100294{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300295 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700296 const char *log_priority = NULL;
297
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100298 memset(input, 0, sizeof *input);
299
300 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300301 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100302
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700303 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700304
Peter Hutterer3b843d32014-06-25 14:07:36 +1000305 input->libinput = libinput_udev_create_context(&libinput_interface,
306 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100307 if (!input->libinput) {
308 return -1;
309 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000310
311 libinput_log_set_handler(input->libinput, &libinput_log_func);
312
313 if (log_priority) {
314 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300315 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000316 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300317 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000318 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300319 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000320 }
321 }
322
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300323 libinput_log_set_priority(input->libinput, priority);
324
Peter Hutterer3b843d32014-06-25 14:07:36 +1000325 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
326 libinput_unref(input->libinput);
327 return -1;
328 }
329
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100330 process_events(input);
331
332 return udev_input_enable(input);
333}
334
335void
336udev_input_destroy(struct udev_input *input)
337{
338 struct udev_seat *seat, *next;
339
340 wl_event_source_remove(input->libinput_source);
341 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
342 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000343 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100344}
345
346static void
347udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
348{
349 struct udev_seat *seat = (struct udev_seat *) seat_base;
350 struct evdev_device *device;
351
352 wl_list_for_each(device, &seat->devices_list, link)
353 evdev_led_update(device, leds);
354}
355
356static void
357notify_output_create(struct wl_listener *listener, void *data)
358{
359 struct udev_seat *seat = container_of(listener, struct udev_seat,
360 output_create_listener);
361 struct evdev_device *device;
362 struct weston_output *output = data;
363
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300364 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100365 if (device->output_name &&
366 strcmp(output->name, device->output_name) == 0) {
367 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100368 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300369
370 if (device->output_name == NULL && device->output == NULL)
371 evdev_device_set_output(device, output);
372 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100373}
374
375static struct udev_seat *
376udev_seat_create(struct udev_input *input, const char *seat_name)
377{
378 struct weston_compositor *c = input->compositor;
379 struct udev_seat *seat;
380
381 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100382 if (!seat)
383 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000384
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100385 weston_seat_init(&seat->base, c, seat_name);
386 seat->base.led_update = udev_seat_led_update;
387
388 seat->output_create_listener.notify = notify_output_create;
389 wl_signal_add(&c->output_created_signal,
390 &seat->output_create_listener);
391
392 wl_list_init(&seat->devices_list);
393
394 return seat;
395}
396
397static void
398udev_seat_destroy(struct udev_seat *seat)
399{
Derek Foreman1281a362015-07-31 16:55:32 -0500400 struct weston_keyboard *keyboard =
401 weston_seat_get_keyboard(&seat->base);
402
Derek Foreman1281a362015-07-31 16:55:32 -0500403 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100404 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500405
406 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100407 weston_seat_release(&seat->base);
408 wl_list_remove(&seat->output_create_listener.link);
409 free(seat);
410}
411
412struct udev_seat *
413udev_seat_get_named(struct udev_input *input, const char *seat_name)
414{
415 struct udev_seat *seat;
416
417 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
418 if (strcmp(seat->base.seat_name, seat_name) == 0)
419 return seat;
420 }
421
422 return udev_seat_create(input, seat_name);
423}