blob: c9f9ed20b63845093e07bfe7ad9167f033ac5868 [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
52static void
53device_added(struct udev_input *input, struct libinput_device *libinput_device)
54{
55 struct weston_compositor *c;
56 struct evdev_device *device;
57 struct weston_output *output;
58 const char *seat_name;
59 const char *output_name;
60 struct libinput_seat *libinput_seat;
61 struct weston_seat *seat;
62 struct udev_seat *udev_seat;
Derek Foreman1281a362015-07-31 16:55:32 -050063 struct weston_pointer *pointer;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010064
65 c = input->compositor;
66 libinput_seat = libinput_device_get_seat(libinput_device);
67
68 seat_name = libinput_seat_get_logical_name(libinput_seat);
69 udev_seat = udev_seat_get_named(input, seat_name);
70 if (!udev_seat)
71 return;
72
73 seat = &udev_seat->base;
74 device = evdev_device_create(libinput_device, seat);
75 if (device == NULL)
76 return;
77
78 udev_seat = (struct udev_seat *) seat;
79 wl_list_insert(udev_seat->devices_list.prev, &device->link);
80
Derek Foreman1281a362015-07-31 16:55:32 -050081 pointer = weston_seat_get_pointer(seat);
82 if (seat->output && pointer)
83 weston_pointer_clamp(pointer,
84 &pointer->x,
85 &pointer->y);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010086
87 output_name = libinput_device_get_output_name(libinput_device);
88 if (output_name) {
89 device->output_name = strdup(output_name);
90 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050091 if (output->name &&
92 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +010093 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030094 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010095 output = container_of(c->output_list.next,
96 struct weston_output, link);
97 evdev_device_set_output(device, output);
98 }
99
100 if (!input->suspended)
101 weston_seat_repick(seat);
102}
103
104static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500105device_removed(struct udev_input *input, struct libinput_device *libinput_device)
106{
107 struct evdev_device *device;
108 device = libinput_device_get_user_data(libinput_device);
109 evdev_device_destroy(device);
110}
111
112static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100113udev_seat_remove_devices(struct udev_seat *seat)
114{
115 struct evdev_device *device, *next;
116
117 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
118 evdev_device_destroy(device);
119 }
120}
121
122void
123udev_input_disable(struct udev_input *input)
124{
125 if (input->suspended)
126 return;
127
128 libinput_suspend(input->libinput);
129 process_events(input);
130 input->suspended = 1;
131}
132
133static int
134udev_input_process_event(struct libinput_event *event)
135{
136 struct libinput *libinput = libinput_event_get_context(event);
137 struct libinput_device *libinput_device =
138 libinput_event_get_device(event);
139 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100140 int handled = 1;
141
142 switch (libinput_event_get_type(event)) {
143 case LIBINPUT_EVENT_DEVICE_ADDED:
144 device_added(input, libinput_device);
145 break;
146 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500147 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100148 break;
149 default:
150 handled = 0;
151 }
152
153 return handled;
154}
155
156static void
157process_event(struct libinput_event *event)
158{
159 if (udev_input_process_event(event))
160 return;
161 if (evdev_device_process_event(event))
162 return;
163}
164
165static void
166process_events(struct udev_input *input)
167{
168 struct libinput_event *event;
169
170 while ((event = libinput_get_event(input->libinput))) {
171 process_event(event);
172 libinput_event_destroy(event);
173 }
174}
175
176static int
177udev_input_dispatch(struct udev_input *input)
178{
179 if (libinput_dispatch(input->libinput) != 0)
180 weston_log("libinput: Failed to dispatch libinput\n");
181
182 process_events(input);
183
184 return 0;
185}
186
187static int
188libinput_source_dispatch(int fd, uint32_t mask, void *data)
189{
190 struct udev_input *input = data;
191
192 return udev_input_dispatch(input) != 0;
193}
194
195static int
196open_restricted(const char *path, int flags, void *user_data)
197{
198 struct udev_input *input = user_data;
199 struct weston_launcher *launcher = input->compositor->launcher;
200
201 return weston_launcher_open(launcher, path, flags);
202}
203
204static void
205close_restricted(int fd, void *user_data)
206{
207 struct udev_input *input = user_data;
208 struct weston_launcher *launcher = input->compositor->launcher;
209
210 weston_launcher_close(launcher, fd);
211}
212
213const struct libinput_interface libinput_interface = {
214 open_restricted,
215 close_restricted,
216};
217
218int
219udev_input_enable(struct udev_input *input)
220{
221 struct wl_event_loop *loop;
222 struct weston_compositor *c = input->compositor;
223 int fd;
224 struct udev_seat *seat;
225 int devices_found = 0;
226
227 loop = wl_display_get_event_loop(c->wl_display);
228 fd = libinput_get_fd(input->libinput);
229 input->libinput_source =
230 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
231 libinput_source_dispatch, input);
232 if (!input->libinput_source) {
233 return -1;
234 }
235
236 if (input->suspended) {
237 if (libinput_resume(input->libinput) != 0) {
238 wl_event_source_remove(input->libinput_source);
239 input->libinput_source = NULL;
240 return -1;
241 }
242 input->suspended = 0;
243 process_events(input);
244 }
245
246 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
247 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
248
249 if (!wl_list_empty(&seat->devices_list))
250 devices_found = 1;
251 }
252
253 if (devices_found == 0) {
254 weston_log(
255 "warning: no input devices on entering Weston. "
256 "Possible causes:\n"
257 "\t- no permissions to read /dev/input/event*\n"
258 "\t- seats misconfigured "
259 "(Weston backend option 'seat', "
260 "udev device property ID_SEAT)\n");
261 return -1;
262 }
263
264 return 0;
265}
266
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700267static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000268libinput_log_func(struct libinput *libinput,
269 enum libinput_log_priority priority,
270 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700271{
272 weston_vlog(format, args);
273}
274
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100275int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300276udev_input_init(struct udev_input *input, struct weston_compositor *c,
277 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100278{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300279 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700280 const char *log_priority = NULL;
281
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100282 memset(input, 0, sizeof *input);
283
284 input->compositor = c;
285
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700286 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700287
Peter Hutterer3b843d32014-06-25 14:07:36 +1000288 input->libinput = libinput_udev_create_context(&libinput_interface,
289 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100290 if (!input->libinput) {
291 return -1;
292 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000293
294 libinput_log_set_handler(input->libinput, &libinput_log_func);
295
296 if (log_priority) {
297 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300298 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000299 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300300 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000301 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300302 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000303 }
304 }
305
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300306 libinput_log_set_priority(input->libinput, priority);
307
Peter Hutterer3b843d32014-06-25 14:07:36 +1000308 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
309 libinput_unref(input->libinput);
310 return -1;
311 }
312
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100313 process_events(input);
314
315 return udev_input_enable(input);
316}
317
318void
319udev_input_destroy(struct udev_input *input)
320{
321 struct udev_seat *seat, *next;
322
323 wl_event_source_remove(input->libinput_source);
324 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
325 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000326 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100327}
328
329static void
330udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
331{
332 struct udev_seat *seat = (struct udev_seat *) seat_base;
333 struct evdev_device *device;
334
335 wl_list_for_each(device, &seat->devices_list, link)
336 evdev_led_update(device, leds);
337}
338
339static void
340notify_output_create(struct wl_listener *listener, void *data)
341{
342 struct udev_seat *seat = container_of(listener, struct udev_seat,
343 output_create_listener);
344 struct evdev_device *device;
345 struct weston_output *output = data;
346
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300347 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100348 if (device->output_name &&
349 strcmp(output->name, device->output_name) == 0) {
350 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100351 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300352
353 if (device->output_name == NULL && device->output == NULL)
354 evdev_device_set_output(device, output);
355 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356}
357
358static struct udev_seat *
359udev_seat_create(struct udev_input *input, const char *seat_name)
360{
361 struct weston_compositor *c = input->compositor;
362 struct udev_seat *seat;
363
364 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100365 if (!seat)
366 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000367
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100368 weston_seat_init(&seat->base, c, seat_name);
369 seat->base.led_update = udev_seat_led_update;
370
371 seat->output_create_listener.notify = notify_output_create;
372 wl_signal_add(&c->output_created_signal,
373 &seat->output_create_listener);
374
375 wl_list_init(&seat->devices_list);
376
377 return seat;
378}
379
380static void
381udev_seat_destroy(struct udev_seat *seat)
382{
Derek Foreman1281a362015-07-31 16:55:32 -0500383 struct weston_keyboard *keyboard =
384 weston_seat_get_keyboard(&seat->base);
385
Derek Foreman1281a362015-07-31 16:55:32 -0500386 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100387 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500388
389 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100390 weston_seat_release(&seat->base);
391 wl_list_remove(&seat->output_create_listener.link);
392 free(seat);
393}
394
395struct udev_seat *
396udev_seat_get_named(struct udev_input *input, const char *seat_name)
397{
398 struct udev_seat *seat;
399
400 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
401 if (strcmp(seat->base.seat_name, seat_name) == 0)
402 return seat;
403 }
404
405 return udev_seat_create(input, seat_name);
406}