blob: 77571701d747cd87670b7cbfe8ffe271d731f41a [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"
40
41static const char default_seat[] = "seat0";
42static const char default_seat_name[] = "default";
43
44static void
45process_events(struct udev_input *input);
46static struct udev_seat *
47udev_seat_create(struct udev_input *input, const char *seat_name);
48static void
49udev_seat_destroy(struct udev_seat *seat);
50
51static void
52device_added(struct udev_input *input, struct libinput_device *libinput_device)
53{
54 struct weston_compositor *c;
55 struct evdev_device *device;
56 struct weston_output *output;
57 const char *seat_name;
58 const char *output_name;
59 struct libinput_seat *libinput_seat;
60 struct weston_seat *seat;
61 struct udev_seat *udev_seat;
62
63 c = input->compositor;
64 libinput_seat = libinput_device_get_seat(libinput_device);
65
66 seat_name = libinput_seat_get_logical_name(libinput_seat);
67 udev_seat = udev_seat_get_named(input, seat_name);
68 if (!udev_seat)
69 return;
70
71 seat = &udev_seat->base;
72 device = evdev_device_create(libinput_device, seat);
73 if (device == NULL)
74 return;
75
76 udev_seat = (struct udev_seat *) seat;
77 wl_list_insert(udev_seat->devices_list.prev, &device->link);
78
79 if (seat->output && seat->pointer)
80 weston_pointer_clamp(seat->pointer,
81 &seat->pointer->x,
82 &seat->pointer->y);
83
84 output_name = libinput_device_get_output_name(libinput_device);
85 if (output_name) {
86 device->output_name = strdup(output_name);
87 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050088 if (output->name &&
89 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +010090 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030091 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010092 output = container_of(c->output_list.next,
93 struct weston_output, link);
94 evdev_device_set_output(device, output);
95 }
96
97 if (!input->suspended)
98 weston_seat_repick(seat);
99}
100
101static void
102udev_seat_remove_devices(struct udev_seat *seat)
103{
104 struct evdev_device *device, *next;
105
106 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
107 evdev_device_destroy(device);
108 }
109}
110
111void
112udev_input_disable(struct udev_input *input)
113{
114 if (input->suspended)
115 return;
116
117 libinput_suspend(input->libinput);
118 process_events(input);
119 input->suspended = 1;
120}
121
122static int
123udev_input_process_event(struct libinput_event *event)
124{
125 struct libinput *libinput = libinput_event_get_context(event);
126 struct libinput_device *libinput_device =
127 libinput_event_get_device(event);
128 struct udev_input *input = libinput_get_user_data(libinput);
129 struct evdev_device *device;
130 int handled = 1;
131
132 switch (libinput_event_get_type(event)) {
133 case LIBINPUT_EVENT_DEVICE_ADDED:
134 device_added(input, libinput_device);
135 break;
136 case LIBINPUT_EVENT_DEVICE_REMOVED:
137 device = libinput_device_get_user_data(libinput_device);
138 evdev_device_destroy(device);
139 break;
140 default:
141 handled = 0;
142 }
143
144 return handled;
145}
146
147static void
148process_event(struct libinput_event *event)
149{
150 if (udev_input_process_event(event))
151 return;
152 if (evdev_device_process_event(event))
153 return;
154}
155
156static void
157process_events(struct udev_input *input)
158{
159 struct libinput_event *event;
160
161 while ((event = libinput_get_event(input->libinput))) {
162 process_event(event);
163 libinput_event_destroy(event);
164 }
165}
166
167static int
168udev_input_dispatch(struct udev_input *input)
169{
170 if (libinput_dispatch(input->libinput) != 0)
171 weston_log("libinput: Failed to dispatch libinput\n");
172
173 process_events(input);
174
175 return 0;
176}
177
178static int
179libinput_source_dispatch(int fd, uint32_t mask, void *data)
180{
181 struct udev_input *input = data;
182
183 return udev_input_dispatch(input) != 0;
184}
185
186static int
187open_restricted(const char *path, int flags, void *user_data)
188{
189 struct udev_input *input = user_data;
190 struct weston_launcher *launcher = input->compositor->launcher;
191
192 return weston_launcher_open(launcher, path, flags);
193}
194
195static void
196close_restricted(int fd, void *user_data)
197{
198 struct udev_input *input = user_data;
199 struct weston_launcher *launcher = input->compositor->launcher;
200
201 weston_launcher_close(launcher, fd);
202}
203
204const struct libinput_interface libinput_interface = {
205 open_restricted,
206 close_restricted,
207};
208
209int
210udev_input_enable(struct udev_input *input)
211{
212 struct wl_event_loop *loop;
213 struct weston_compositor *c = input->compositor;
214 int fd;
215 struct udev_seat *seat;
216 int devices_found = 0;
217
218 loop = wl_display_get_event_loop(c->wl_display);
219 fd = libinput_get_fd(input->libinput);
220 input->libinput_source =
221 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
222 libinput_source_dispatch, input);
223 if (!input->libinput_source) {
224 return -1;
225 }
226
227 if (input->suspended) {
228 if (libinput_resume(input->libinput) != 0) {
229 wl_event_source_remove(input->libinput_source);
230 input->libinput_source = NULL;
231 return -1;
232 }
233 input->suspended = 0;
234 process_events(input);
235 }
236
237 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
238 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
239
240 if (!wl_list_empty(&seat->devices_list))
241 devices_found = 1;
242 }
243
244 if (devices_found == 0) {
245 weston_log(
246 "warning: no input devices on entering Weston. "
247 "Possible causes:\n"
248 "\t- no permissions to read /dev/input/event*\n"
249 "\t- seats misconfigured "
250 "(Weston backend option 'seat', "
251 "udev device property ID_SEAT)\n");
252 return -1;
253 }
254
255 return 0;
256}
257
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700258static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000259libinput_log_func(struct libinput *libinput,
260 enum libinput_log_priority priority,
261 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700262{
263 weston_vlog(format, args);
264}
265
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100266int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300267udev_input_init(struct udev_input *input, struct weston_compositor *c,
268 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100269{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300270 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700271 const char *log_priority = NULL;
272
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100273 memset(input, 0, sizeof *input);
274
275 input->compositor = c;
276
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700277 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700278
Peter Hutterer3b843d32014-06-25 14:07:36 +1000279 input->libinput = libinput_udev_create_context(&libinput_interface,
280 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100281 if (!input->libinput) {
282 return -1;
283 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000284
285 libinput_log_set_handler(input->libinput, &libinput_log_func);
286
287 if (log_priority) {
288 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300289 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000290 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300291 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000292 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300293 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000294 }
295 }
296
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300297 libinput_log_set_priority(input->libinput, priority);
298
Peter Hutterer3b843d32014-06-25 14:07:36 +1000299 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
300 libinput_unref(input->libinput);
301 return -1;
302 }
303
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100304 process_events(input);
305
306 return udev_input_enable(input);
307}
308
309void
310udev_input_destroy(struct udev_input *input)
311{
312 struct udev_seat *seat, *next;
313
314 wl_event_source_remove(input->libinput_source);
315 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
316 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000317 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100318}
319
320static void
321udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
322{
323 struct udev_seat *seat = (struct udev_seat *) seat_base;
324 struct evdev_device *device;
325
326 wl_list_for_each(device, &seat->devices_list, link)
327 evdev_led_update(device, leds);
328}
329
330static void
331notify_output_create(struct wl_listener *listener, void *data)
332{
333 struct udev_seat *seat = container_of(listener, struct udev_seat,
334 output_create_listener);
335 struct evdev_device *device;
336 struct weston_output *output = data;
337
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300338 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339 if (device->output_name &&
340 strcmp(output->name, device->output_name) == 0) {
341 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100342 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300343
344 if (device->output_name == NULL && device->output == NULL)
345 evdev_device_set_output(device, output);
346 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100347}
348
349static struct udev_seat *
350udev_seat_create(struct udev_input *input, const char *seat_name)
351{
352 struct weston_compositor *c = input->compositor;
353 struct udev_seat *seat;
354
355 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356 if (!seat)
357 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000358
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100359 weston_seat_init(&seat->base, c, seat_name);
360 seat->base.led_update = udev_seat_led_update;
361
362 seat->output_create_listener.notify = notify_output_create;
363 wl_signal_add(&c->output_created_signal,
364 &seat->output_create_listener);
365
366 wl_list_init(&seat->devices_list);
367
368 return seat;
369}
370
371static void
372udev_seat_destroy(struct udev_seat *seat)
373{
374 udev_seat_remove_devices(seat);
375 if (seat->base.keyboard)
376 notify_keyboard_focus_out(&seat->base);
377 weston_seat_release(&seat->base);
378 wl_list_remove(&seat->output_create_listener.link);
379 free(seat);
380}
381
382struct udev_seat *
383udev_seat_get_named(struct udev_input *input, const char *seat_name)
384{
385 struct udev_seat *seat;
386
387 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
388 if (strcmp(seat->base.seat_name, seat_name) == 0)
389 return seat;
390 }
391
392 return udev_seat_create(input, seat_name);
393}