blob: 5b2dbecccfcbd1a5436318550e94c7da4bd7ca85 [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
42static const char default_seat[] = "seat0";
43static const char default_seat_name[] = "default";
44
45static void
46process_events(struct udev_input *input);
47static struct udev_seat *
48udev_seat_create(struct udev_input *input, const char *seat_name);
49static void
50udev_seat_destroy(struct udev_seat *seat);
51
Derek Foremana6714fa2015-05-05 15:01:51 -050052static struct udev_seat *
53get_udev_seat(struct udev_input *input, struct libinput_device *device)
54{
55 struct libinput_seat *libinput_seat;
56 const char *seat_name;
57
58 libinput_seat = libinput_device_get_seat(device);
59 seat_name = libinput_seat_get_logical_name(libinput_seat);
60 return udev_seat_get_named(input, seat_name);
61}
62
Jonas Ådahle0de3c22014-03-12 22:08:42 +010063static void
64device_added(struct udev_input *input, struct libinput_device *libinput_device)
65{
66 struct weston_compositor *c;
67 struct evdev_device *device;
68 struct weston_output *output;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010069 const char *output_name;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010070 struct weston_seat *seat;
71 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -050072 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010073
74 c = input->compositor;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010075
Derek Foremana6714fa2015-05-05 15:01:51 -050076 udev_seat = get_udev_seat(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010077 if (!udev_seat)
78 return;
79
80 seat = &udev_seat->base;
81 device = evdev_device_create(libinput_device, seat);
82 if (device == NULL)
83 return;
84
85 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,
285 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100286{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300287 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700288 const char *log_priority = NULL;
289
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100290 memset(input, 0, sizeof *input);
291
292 input->compositor = c;
293
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700294 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700295
Peter Hutterer3b843d32014-06-25 14:07:36 +1000296 input->libinput = libinput_udev_create_context(&libinput_interface,
297 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100298 if (!input->libinput) {
299 return -1;
300 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000301
302 libinput_log_set_handler(input->libinput, &libinput_log_func);
303
304 if (log_priority) {
305 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300306 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000307 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300308 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000309 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300310 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000311 }
312 }
313
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300314 libinput_log_set_priority(input->libinput, priority);
315
Peter Hutterer3b843d32014-06-25 14:07:36 +1000316 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
317 libinput_unref(input->libinput);
318 return -1;
319 }
320
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100321 process_events(input);
322
323 return udev_input_enable(input);
324}
325
326void
327udev_input_destroy(struct udev_input *input)
328{
329 struct udev_seat *seat, *next;
330
331 wl_event_source_remove(input->libinput_source);
332 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
333 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000334 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100335}
336
337static void
338udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
339{
340 struct udev_seat *seat = (struct udev_seat *) seat_base;
341 struct evdev_device *device;
342
343 wl_list_for_each(device, &seat->devices_list, link)
344 evdev_led_update(device, leds);
345}
346
347static void
348notify_output_create(struct wl_listener *listener, void *data)
349{
350 struct udev_seat *seat = container_of(listener, struct udev_seat,
351 output_create_listener);
352 struct evdev_device *device;
353 struct weston_output *output = data;
354
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300355 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356 if (device->output_name &&
357 strcmp(output->name, device->output_name) == 0) {
358 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100359 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300360
361 if (device->output_name == NULL && device->output == NULL)
362 evdev_device_set_output(device, output);
363 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100364}
365
366static struct udev_seat *
367udev_seat_create(struct udev_input *input, const char *seat_name)
368{
369 struct weston_compositor *c = input->compositor;
370 struct udev_seat *seat;
371
372 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100373 if (!seat)
374 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000375
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100376 weston_seat_init(&seat->base, c, seat_name);
377 seat->base.led_update = udev_seat_led_update;
378
379 seat->output_create_listener.notify = notify_output_create;
380 wl_signal_add(&c->output_created_signal,
381 &seat->output_create_listener);
382
383 wl_list_init(&seat->devices_list);
384
385 return seat;
386}
387
388static void
389udev_seat_destroy(struct udev_seat *seat)
390{
Derek Foreman1281a362015-07-31 16:55:32 -0500391 struct weston_keyboard *keyboard =
392 weston_seat_get_keyboard(&seat->base);
393
Derek Foreman1281a362015-07-31 16:55:32 -0500394 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100395 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500396
397 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100398 weston_seat_release(&seat->base);
399 wl_list_remove(&seat->output_create_listener.link);
400 free(seat);
401}
402
403struct udev_seat *
404udev_seat_get_named(struct udev_input *input, const char *seat_name)
405{
406 struct udev_seat *seat;
407
408 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
409 if (strcmp(seat->base.seat_name, seat_name) == 0)
410 return seat;
411 }
412
413 return udev_seat_create(input, seat_name);
414}