blob: 6353667ffbc01969ae582f9622815d461965afeb [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;
63
64 c = input->compositor;
65 libinput_seat = libinput_device_get_seat(libinput_device);
66
67 seat_name = libinput_seat_get_logical_name(libinput_seat);
68 udev_seat = udev_seat_get_named(input, seat_name);
69 if (!udev_seat)
70 return;
71
72 seat = &udev_seat->base;
73 device = evdev_device_create(libinput_device, seat);
74 if (device == NULL)
75 return;
76
77 udev_seat = (struct udev_seat *) seat;
78 wl_list_insert(udev_seat->devices_list.prev, &device->link);
79
80 if (seat->output && seat->pointer)
81 weston_pointer_clamp(seat->pointer,
82 &seat->pointer->x,
83 &seat->pointer->y);
84
85 output_name = libinput_device_get_output_name(libinput_device);
86 if (output_name) {
87 device->output_name = strdup(output_name);
88 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050089 if (output->name &&
90 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +010091 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030092 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010093 output = container_of(c->output_list.next,
94 struct weston_output, link);
95 evdev_device_set_output(device, output);
96 }
97
98 if (!input->suspended)
99 weston_seat_repick(seat);
100}
101
102static void
Derek Foreman6b557a72015-05-05 15:01:52 -0500103device_removed(struct udev_input *input, struct libinput_device *libinput_device)
104{
105 struct evdev_device *device;
106 device = libinput_device_get_user_data(libinput_device);
107 evdev_device_destroy(device);
108}
109
110static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100111udev_seat_remove_devices(struct udev_seat *seat)
112{
113 struct evdev_device *device, *next;
114
115 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
116 evdev_device_destroy(device);
117 }
118}
119
120void
121udev_input_disable(struct udev_input *input)
122{
123 if (input->suspended)
124 return;
125
126 libinput_suspend(input->libinput);
127 process_events(input);
128 input->suspended = 1;
129}
130
131static int
132udev_input_process_event(struct libinput_event *event)
133{
134 struct libinput *libinput = libinput_event_get_context(event);
135 struct libinput_device *libinput_device =
136 libinput_event_get_device(event);
137 struct udev_input *input = libinput_get_user_data(libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100138 int handled = 1;
139
140 switch (libinput_event_get_type(event)) {
141 case LIBINPUT_EVENT_DEVICE_ADDED:
142 device_added(input, libinput_device);
143 break;
144 case LIBINPUT_EVENT_DEVICE_REMOVED:
Derek Foreman6b557a72015-05-05 15:01:52 -0500145 device_removed(input, libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100146 break;
147 default:
148 handled = 0;
149 }
150
151 return handled;
152}
153
154static void
155process_event(struct libinput_event *event)
156{
157 if (udev_input_process_event(event))
158 return;
159 if (evdev_device_process_event(event))
160 return;
161}
162
163static void
164process_events(struct udev_input *input)
165{
166 struct libinput_event *event;
167
168 while ((event = libinput_get_event(input->libinput))) {
169 process_event(event);
170 libinput_event_destroy(event);
171 }
172}
173
174static int
175udev_input_dispatch(struct udev_input *input)
176{
177 if (libinput_dispatch(input->libinput) != 0)
178 weston_log("libinput: Failed to dispatch libinput\n");
179
180 process_events(input);
181
182 return 0;
183}
184
185static int
186libinput_source_dispatch(int fd, uint32_t mask, void *data)
187{
188 struct udev_input *input = data;
189
190 return udev_input_dispatch(input) != 0;
191}
192
193static int
194open_restricted(const char *path, int flags, void *user_data)
195{
196 struct udev_input *input = user_data;
197 struct weston_launcher *launcher = input->compositor->launcher;
198
199 return weston_launcher_open(launcher, path, flags);
200}
201
202static void
203close_restricted(int fd, void *user_data)
204{
205 struct udev_input *input = user_data;
206 struct weston_launcher *launcher = input->compositor->launcher;
207
208 weston_launcher_close(launcher, fd);
209}
210
211const struct libinput_interface libinput_interface = {
212 open_restricted,
213 close_restricted,
214};
215
216int
217udev_input_enable(struct udev_input *input)
218{
219 struct wl_event_loop *loop;
220 struct weston_compositor *c = input->compositor;
221 int fd;
222 struct udev_seat *seat;
223 int devices_found = 0;
224
225 loop = wl_display_get_event_loop(c->wl_display);
226 fd = libinput_get_fd(input->libinput);
227 input->libinput_source =
228 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
229 libinput_source_dispatch, input);
230 if (!input->libinput_source) {
231 return -1;
232 }
233
234 if (input->suspended) {
235 if (libinput_resume(input->libinput) != 0) {
236 wl_event_source_remove(input->libinput_source);
237 input->libinput_source = NULL;
238 return -1;
239 }
240 input->suspended = 0;
241 process_events(input);
242 }
243
244 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
245 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
246
247 if (!wl_list_empty(&seat->devices_list))
248 devices_found = 1;
249 }
250
251 if (devices_found == 0) {
252 weston_log(
253 "warning: no input devices on entering Weston. "
254 "Possible causes:\n"
255 "\t- no permissions to read /dev/input/event*\n"
256 "\t- seats misconfigured "
257 "(Weston backend option 'seat', "
258 "udev device property ID_SEAT)\n");
259 return -1;
260 }
261
262 return 0;
263}
264
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700265static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000266libinput_log_func(struct libinput *libinput,
267 enum libinput_log_priority priority,
268 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700269{
270 weston_vlog(format, args);
271}
272
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100273int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300274udev_input_init(struct udev_input *input, struct weston_compositor *c,
275 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100276{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300277 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700278 const char *log_priority = NULL;
279
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100280 memset(input, 0, sizeof *input);
281
282 input->compositor = c;
283
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700284 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700285
Peter Hutterer3b843d32014-06-25 14:07:36 +1000286 input->libinput = libinput_udev_create_context(&libinput_interface,
287 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100288 if (!input->libinput) {
289 return -1;
290 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000291
292 libinput_log_set_handler(input->libinput, &libinput_log_func);
293
294 if (log_priority) {
295 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300296 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000297 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300298 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000299 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300300 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000301 }
302 }
303
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300304 libinput_log_set_priority(input->libinput, priority);
305
Peter Hutterer3b843d32014-06-25 14:07:36 +1000306 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
307 libinput_unref(input->libinput);
308 return -1;
309 }
310
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100311 process_events(input);
312
313 return udev_input_enable(input);
314}
315
316void
317udev_input_destroy(struct udev_input *input)
318{
319 struct udev_seat *seat, *next;
320
321 wl_event_source_remove(input->libinput_source);
322 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
323 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000324 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100325}
326
327static void
328udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
329{
330 struct udev_seat *seat = (struct udev_seat *) seat_base;
331 struct evdev_device *device;
332
333 wl_list_for_each(device, &seat->devices_list, link)
334 evdev_led_update(device, leds);
335}
336
337static void
338notify_output_create(struct wl_listener *listener, void *data)
339{
340 struct udev_seat *seat = container_of(listener, struct udev_seat,
341 output_create_listener);
342 struct evdev_device *device;
343 struct weston_output *output = data;
344
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300345 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100346 if (device->output_name &&
347 strcmp(output->name, device->output_name) == 0) {
348 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100349 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300350
351 if (device->output_name == NULL && device->output == NULL)
352 evdev_device_set_output(device, output);
353 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100354}
355
356static struct udev_seat *
357udev_seat_create(struct udev_input *input, const char *seat_name)
358{
359 struct weston_compositor *c = input->compositor;
360 struct udev_seat *seat;
361
362 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100363 if (!seat)
364 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000365
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100366 weston_seat_init(&seat->base, c, seat_name);
367 seat->base.led_update = udev_seat_led_update;
368
369 seat->output_create_listener.notify = notify_output_create;
370 wl_signal_add(&c->output_created_signal,
371 &seat->output_create_listener);
372
373 wl_list_init(&seat->devices_list);
374
375 return seat;
376}
377
378static void
379udev_seat_destroy(struct udev_seat *seat)
380{
381 udev_seat_remove_devices(seat);
382 if (seat->base.keyboard)
383 notify_keyboard_focus_out(&seat->base);
384 weston_seat_release(&seat->base);
385 wl_list_remove(&seat->output_create_listener.link);
386 free(seat);
387}
388
389struct udev_seat *
390udev_seat_get_named(struct udev_input *input, const char *seat_name)
391{
392 struct udev_seat *seat;
393
394 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
395 if (strcmp(seat->base.seat_name, seat_name) == 0)
396 return seat;
397 }
398
399 return udev_seat_create(input, seat_name);
400}