blob: 94e19f5ee7974d6549defd45bfa1d38e717977e0 [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
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <libinput.h>
34#include <libudev.h>
35
36#include "compositor.h"
37#include "launcher-util.h"
38#include "libinput-seat.h"
39#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070040#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010041
Jonas Ådahle0de3c22014-03-12 22:08:42 +010042static void
43process_events(struct udev_input *input);
44static struct udev_seat *
45udev_seat_create(struct udev_input *input, const char *seat_name);
46static void
47udev_seat_destroy(struct udev_seat *seat);
48
Derek Foremana6714fa2015-05-05 15:01:51 -050049static struct udev_seat *
50get_udev_seat(struct udev_input *input, struct libinput_device *device)
51{
52 struct libinput_seat *libinput_seat;
53 const char *seat_name;
54
55 libinput_seat = libinput_device_get_seat(device);
56 seat_name = libinput_seat_get_logical_name(libinput_seat);
57 return udev_seat_get_named(input, seat_name);
58}
59
Jonas Ådahle0de3c22014-03-12 22:08:42 +010060static void
61device_added(struct udev_input *input, struct libinput_device *libinput_device)
62{
63 struct weston_compositor *c;
64 struct evdev_device *device;
65 struct weston_output *output;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010066 const char *output_name;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010067 struct weston_seat *seat;
68 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -050069 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010070
71 c = input->compositor;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010072
Derek Foremana6714fa2015-05-05 15:01:51 -050073 udev_seat = get_udev_seat(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010074 if (!udev_seat)
75 return;
76
77 seat = &udev_seat->base;
78 device = evdev_device_create(libinput_device, seat);
79 if (device == NULL)
80 return;
81
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +030082 if (input->configure_device != NULL)
83 input->configure_device(c, device->device);
84 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010085 udev_seat = (struct udev_seat *) seat;
86 wl_list_insert(udev_seat->devices_list.prev, &device->link);
87
Derek Foreman1281a362015-07-31 16:55:32 -050088 pointer = weston_seat_get_pointer(seat);
89 if (seat->output && pointer)
90 weston_pointer_clamp(pointer,
91 &pointer->x,
92 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010093
94 output_name = libinput_device_get_output_name(libinput_device);
95 if (output_name) {
96 device->output_name = strdup(output_name);
97 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050098 if (output->name &&
99 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100100 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300101 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100102 output = container_of(c->output_list.next,
103 struct weston_output, link);
104 evdev_device_set_output(device, output);
105 }
106
107 if (!input->suspended)
108 weston_seat_repick(seat);
109}
110
111static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500112device_removed(struct udev_input *input, struct libinput_device *libinput_device)
113{
114 struct evdev_device *device;
Derek Foreman0b7da012015-09-30 13:34:57 -0500115
Derek Foreman6b557a72015-05-05 15:01:52 -0500116 device = libinput_device_get_user_data(libinput_device);
117 evdev_device_destroy(device);
118}
119
120static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100121udev_seat_remove_devices(struct udev_seat *seat)
122{
123 struct evdev_device *device, *next;
124
125 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
126 evdev_device_destroy(device);
127 }
128}
129
130void
131udev_input_disable(struct udev_input *input)
132{
133 if (input->suspended)
134 return;
135
136 libinput_suspend(input->libinput);
137 process_events(input);
138 input->suspended = 1;
139}
140
141static int
142udev_input_process_event(struct libinput_event *event)
143{
144 struct libinput *libinput = libinput_event_get_context(event);
145 struct libinput_device *libinput_device =
146 libinput_event_get_device(event);
147 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100148 int handled = 1;
149
150 switch (libinput_event_get_type(event)) {
151 case LIBINPUT_EVENT_DEVICE_ADDED:
152 device_added(input, libinput_device);
153 break;
154 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500155 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100156 break;
157 default:
158 handled = 0;
159 }
160
161 return handled;
162}
163
164static void
165process_event(struct libinput_event *event)
166{
167 if (udev_input_process_event(event))
168 return;
169 if (evdev_device_process_event(event))
170 return;
171}
172
173static void
174process_events(struct udev_input *input)
175{
176 struct libinput_event *event;
177
178 while ((event = libinput_get_event(input->libinput))) {
179 process_event(event);
180 libinput_event_destroy(event);
181 }
182}
183
184static int
185udev_input_dispatch(struct udev_input *input)
186{
187 if (libinput_dispatch(input->libinput) != 0)
188 weston_log("libinput: Failed to dispatch libinput\n");
189
190 process_events(input);
191
192 return 0;
193}
194
195static int
196libinput_source_dispatch(int fd, uint32_t mask, void *data)
197{
198 struct udev_input *input = data;
199
200 return udev_input_dispatch(input) != 0;
201}
202
203static int
204open_restricted(const char *path, int flags, void *user_data)
205{
206 struct udev_input *input = user_data;
207 struct weston_launcher *launcher = input->compositor->launcher;
208
209 return weston_launcher_open(launcher, path, flags);
210}
211
212static void
213close_restricted(int fd, void *user_data)
214{
215 struct udev_input *input = user_data;
216 struct weston_launcher *launcher = input->compositor->launcher;
217
218 weston_launcher_close(launcher, fd);
219}
220
221const struct libinput_interface libinput_interface = {
222 open_restricted,
223 close_restricted,
224};
225
226int
227udev_input_enable(struct udev_input *input)
228{
229 struct wl_event_loop *loop;
230 struct weston_compositor *c = input->compositor;
231 int fd;
232 struct udev_seat *seat;
233 int devices_found = 0;
234
235 loop = wl_display_get_event_loop(c->wl_display);
236 fd = libinput_get_fd(input->libinput);
237 input->libinput_source =
238 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
239 libinput_source_dispatch, input);
240 if (!input->libinput_source) {
241 return -1;
242 }
243
244 if (input->suspended) {
245 if (libinput_resume(input->libinput) != 0) {
246 wl_event_source_remove(input->libinput_source);
247 input->libinput_source = NULL;
248 return -1;
249 }
250 input->suspended = 0;
251 process_events(input);
252 }
253
254 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
255 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
256
257 if (!wl_list_empty(&seat->devices_list))
258 devices_found = 1;
259 }
260
261 if (devices_found == 0) {
262 weston_log(
263 "warning: no input devices on entering Weston. "
264 "Possible causes:\n"
265 "\t- no permissions to read /dev/input/event*\n"
266 "\t- seats misconfigured "
267 "(Weston backend option 'seat', "
268 "udev device property ID_SEAT)\n");
269 return -1;
270 }
271
272 return 0;
273}
274
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700275static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000276libinput_log_func(struct libinput *libinput,
277 enum libinput_log_priority priority,
278 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700279{
280 weston_vlog(format, args);
281}
282
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100283int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300284udev_input_init(struct udev_input *input, struct weston_compositor *c,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300285 struct udev *udev, const char *seat_id,
286 udev_configure_device_t configure_device)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100287{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300288 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700289 const char *log_priority = NULL;
290
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100291 memset(input, 0, sizeof *input);
292
293 input->compositor = c;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300294 input->configure_device = configure_device;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100295
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700296 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700297
Peter Hutterer3b843d32014-06-25 14:07:36 +1000298 input->libinput = libinput_udev_create_context(&libinput_interface,
299 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100300 if (!input->libinput) {
301 return -1;
302 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000303
304 libinput_log_set_handler(input->libinput, &libinput_log_func);
305
306 if (log_priority) {
307 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300308 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000309 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300310 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000311 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300312 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000313 }
314 }
315
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300316 libinput_log_set_priority(input->libinput, priority);
317
Peter Hutterer3b843d32014-06-25 14:07:36 +1000318 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
319 libinput_unref(input->libinput);
320 return -1;
321 }
322
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100323 process_events(input);
324
325 return udev_input_enable(input);
326}
327
328void
329udev_input_destroy(struct udev_input *input)
330{
331 struct udev_seat *seat, *next;
332
333 wl_event_source_remove(input->libinput_source);
334 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
335 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000336 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100337}
338
339static void
340udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
341{
342 struct udev_seat *seat = (struct udev_seat *) seat_base;
343 struct evdev_device *device;
344
345 wl_list_for_each(device, &seat->devices_list, link)
346 evdev_led_update(device, leds);
347}
348
349static void
350notify_output_create(struct wl_listener *listener, void *data)
351{
352 struct udev_seat *seat = container_of(listener, struct udev_seat,
353 output_create_listener);
354 struct evdev_device *device;
355 struct weston_output *output = data;
356
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300357 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100358 if (device->output_name &&
359 strcmp(output->name, device->output_name) == 0) {
360 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100361 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300362
363 if (device->output_name == NULL && device->output == NULL)
364 evdev_device_set_output(device, output);
365 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100366}
367
368static struct udev_seat *
369udev_seat_create(struct udev_input *input, const char *seat_name)
370{
371 struct weston_compositor *c = input->compositor;
372 struct udev_seat *seat;
373
374 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100375 if (!seat)
376 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000377
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100378 weston_seat_init(&seat->base, c, seat_name);
379 seat->base.led_update = udev_seat_led_update;
380
381 seat->output_create_listener.notify = notify_output_create;
382 wl_signal_add(&c->output_created_signal,
383 &seat->output_create_listener);
384
385 wl_list_init(&seat->devices_list);
386
387 return seat;
388}
389
390static void
391udev_seat_destroy(struct udev_seat *seat)
392{
Derek Foreman1281a362015-07-31 16:55:32 -0500393 struct weston_keyboard *keyboard =
394 weston_seat_get_keyboard(&seat->base);
395
Derek Foreman1281a362015-07-31 16:55:32 -0500396 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100397 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500398
399 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100400 weston_seat_release(&seat->base);
401 wl_list_remove(&seat->output_create_listener.link);
402 free(seat);
403}
404
405struct udev_seat *
406udev_seat_get_named(struct udev_input *input, const char *seat_name)
407{
408 struct udev_seat *seat;
409
410 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
411 if (strcmp(seat->base.seat_name, seat_name) == 0)
412 return seat;
413 }
414
415 return udev_seat_create(input, seat_name);
416}