blob: 5f91b68008db0de65440dd12778985604adb8e4b [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;
Derek Foreman0b7da012015-09-30 13:34:57 -0500108
Derek Foreman6b557a72015-05-05 15:01:52 -0500109 device = libinput_device_get_user_data(libinput_device);
110 evdev_device_destroy(device);
111}
112
113static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100114udev_seat_remove_devices(struct udev_seat *seat)
115{
116 struct evdev_device *device, *next;
117
118 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
119 evdev_device_destroy(device);
120 }
121}
122
123void
124udev_input_disable(struct udev_input *input)
125{
126 if (input->suspended)
127 return;
128
129 libinput_suspend(input->libinput);
130 process_events(input);
131 input->suspended = 1;
132}
133
134static int
135udev_input_process_event(struct libinput_event *event)
136{
137 struct libinput *libinput = libinput_event_get_context(event);
138 struct libinput_device *libinput_device =
139 libinput_event_get_device(event);
140 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100141 int handled = 1;
142
143 switch (libinput_event_get_type(event)) {
144 case LIBINPUT_EVENT_DEVICE_ADDED:
145 device_added(input, libinput_device);
146 break;
147 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500148 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100149 break;
150 default:
151 handled = 0;
152 }
153
154 return handled;
155}
156
157static void
158process_event(struct libinput_event *event)
159{
160 if (udev_input_process_event(event))
161 return;
162 if (evdev_device_process_event(event))
163 return;
164}
165
166static void
167process_events(struct udev_input *input)
168{
169 struct libinput_event *event;
170
171 while ((event = libinput_get_event(input->libinput))) {
172 process_event(event);
173 libinput_event_destroy(event);
174 }
175}
176
177static int
178udev_input_dispatch(struct udev_input *input)
179{
180 if (libinput_dispatch(input->libinput) != 0)
181 weston_log("libinput: Failed to dispatch libinput\n");
182
183 process_events(input);
184
185 return 0;
186}
187
188static int
189libinput_source_dispatch(int fd, uint32_t mask, void *data)
190{
191 struct udev_input *input = data;
192
193 return udev_input_dispatch(input) != 0;
194}
195
196static int
197open_restricted(const char *path, int flags, void *user_data)
198{
199 struct udev_input *input = user_data;
200 struct weston_launcher *launcher = input->compositor->launcher;
201
202 return weston_launcher_open(launcher, path, flags);
203}
204
205static void
206close_restricted(int fd, void *user_data)
207{
208 struct udev_input *input = user_data;
209 struct weston_launcher *launcher = input->compositor->launcher;
210
211 weston_launcher_close(launcher, fd);
212}
213
214const struct libinput_interface libinput_interface = {
215 open_restricted,
216 close_restricted,
217};
218
219int
220udev_input_enable(struct udev_input *input)
221{
222 struct wl_event_loop *loop;
223 struct weston_compositor *c = input->compositor;
224 int fd;
225 struct udev_seat *seat;
226 int devices_found = 0;
227
228 loop = wl_display_get_event_loop(c->wl_display);
229 fd = libinput_get_fd(input->libinput);
230 input->libinput_source =
231 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
232 libinput_source_dispatch, input);
233 if (!input->libinput_source) {
234 return -1;
235 }
236
237 if (input->suspended) {
238 if (libinput_resume(input->libinput) != 0) {
239 wl_event_source_remove(input->libinput_source);
240 input->libinput_source = NULL;
241 return -1;
242 }
243 input->suspended = 0;
244 process_events(input);
245 }
246
247 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
248 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
249
250 if (!wl_list_empty(&seat->devices_list))
251 devices_found = 1;
252 }
253
254 if (devices_found == 0) {
255 weston_log(
256 "warning: no input devices on entering Weston. "
257 "Possible causes:\n"
258 "\t- no permissions to read /dev/input/event*\n"
259 "\t- seats misconfigured "
260 "(Weston backend option 'seat', "
261 "udev device property ID_SEAT)\n");
262 return -1;
263 }
264
265 return 0;
266}
267
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700268static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000269libinput_log_func(struct libinput *libinput,
270 enum libinput_log_priority priority,
271 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700272{
273 weston_vlog(format, args);
274}
275
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100276int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300277udev_input_init(struct udev_input *input, struct weston_compositor *c,
278 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100279{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300280 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700281 const char *log_priority = NULL;
282
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100283 memset(input, 0, sizeof *input);
284
285 input->compositor = c;
286
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700287 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700288
Peter Hutterer3b843d32014-06-25 14:07:36 +1000289 input->libinput = libinput_udev_create_context(&libinput_interface,
290 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100291 if (!input->libinput) {
292 return -1;
293 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000294
295 libinput_log_set_handler(input->libinput, &libinput_log_func);
296
297 if (log_priority) {
298 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300299 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000300 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300301 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000302 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300303 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000304 }
305 }
306
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300307 libinput_log_set_priority(input->libinput, priority);
308
Peter Hutterer3b843d32014-06-25 14:07:36 +1000309 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
310 libinput_unref(input->libinput);
311 return -1;
312 }
313
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100314 process_events(input);
315
316 return udev_input_enable(input);
317}
318
319void
320udev_input_destroy(struct udev_input *input)
321{
322 struct udev_seat *seat, *next;
323
324 wl_event_source_remove(input->libinput_source);
325 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
326 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000327 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100328}
329
330static void
331udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
332{
333 struct udev_seat *seat = (struct udev_seat *) seat_base;
334 struct evdev_device *device;
335
336 wl_list_for_each(device, &seat->devices_list, link)
337 evdev_led_update(device, leds);
338}
339
340static void
341notify_output_create(struct wl_listener *listener, void *data)
342{
343 struct udev_seat *seat = container_of(listener, struct udev_seat,
344 output_create_listener);
345 struct evdev_device *device;
346 struct weston_output *output = data;
347
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300348 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100349 if (device->output_name &&
350 strcmp(output->name, device->output_name) == 0) {
351 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100352 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300353
354 if (device->output_name == NULL && device->output == NULL)
355 evdev_device_set_output(device, output);
356 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100357}
358
359static struct udev_seat *
360udev_seat_create(struct udev_input *input, const char *seat_name)
361{
362 struct weston_compositor *c = input->compositor;
363 struct udev_seat *seat;
364
365 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100366 if (!seat)
367 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000368
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100369 weston_seat_init(&seat->base, c, seat_name);
370 seat->base.led_update = udev_seat_led_update;
371
372 seat->output_create_listener.notify = notify_output_create;
373 wl_signal_add(&c->output_created_signal,
374 &seat->output_create_listener);
375
376 wl_list_init(&seat->devices_list);
377
378 return seat;
379}
380
381static void
382udev_seat_destroy(struct udev_seat *seat)
383{
Derek Foreman1281a362015-07-31 16:55:32 -0500384 struct weston_keyboard *keyboard =
385 weston_seat_get_keyboard(&seat->base);
386
Derek Foreman1281a362015-07-31 16:55:32 -0500387 if (keyboard)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100388 notify_keyboard_focus_out(&seat->base);
Derek Foreman87c862a2015-08-06 12:19:51 -0500389
390 udev_seat_remove_devices(seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100391 weston_seat_release(&seat->base);
392 wl_list_remove(&seat->output_create_listener.link);
393 free(seat);
394}
395
396struct udev_seat *
397udev_seat_get_named(struct udev_input *input, const char *seat_name)
398{
399 struct udev_seat *seat;
400
401 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
402 if (strcmp(seat->base.seat_name, seat_name) == 0)
403 return seat;
404 }
405
406 return udev_seat_create(input, seat_name);
407}