blob: c0a87eadcf556772e04bf12afc2f4a7c92bfd412 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2013 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <libinput.h>
31#include <libudev.h>
32
33#include "compositor.h"
34#include "launcher-util.h"
35#include "libinput-seat.h"
36#include "libinput-device.h"
37
38static const char default_seat[] = "seat0";
39static const char default_seat_name[] = "default";
40
41static void
42process_events(struct udev_input *input);
43static struct udev_seat *
44udev_seat_create(struct udev_input *input, const char *seat_name);
45static void
46udev_seat_destroy(struct udev_seat *seat);
47
48static void
49device_added(struct udev_input *input, struct libinput_device *libinput_device)
50{
51 struct weston_compositor *c;
52 struct evdev_device *device;
53 struct weston_output *output;
54 const char *seat_name;
55 const char *output_name;
56 struct libinput_seat *libinput_seat;
57 struct weston_seat *seat;
58 struct udev_seat *udev_seat;
59
60 c = input->compositor;
61 libinput_seat = libinput_device_get_seat(libinput_device);
62
63 seat_name = libinput_seat_get_logical_name(libinput_seat);
64 udev_seat = udev_seat_get_named(input, seat_name);
65 if (!udev_seat)
66 return;
67
68 seat = &udev_seat->base;
69 device = evdev_device_create(libinput_device, seat);
70 if (device == NULL)
71 return;
72
73 udev_seat = (struct udev_seat *) seat;
74 wl_list_insert(udev_seat->devices_list.prev, &device->link);
75
76 if (seat->output && seat->pointer)
77 weston_pointer_clamp(seat->pointer,
78 &seat->pointer->x,
79 &seat->pointer->y);
80
81 output_name = libinput_device_get_output_name(libinput_device);
82 if (output_name) {
83 device->output_name = strdup(output_name);
84 wl_list_for_each(output, &c->output_list, link)
Derek Foremanf3723d92015-03-17 13:22:35 -050085 if (output->name &&
86 strcmp(output->name, device->output_name) == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +010087 evdev_device_set_output(device, output);
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +030088 } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +010089 output = container_of(c->output_list.next,
90 struct weston_output, link);
91 evdev_device_set_output(device, output);
92 }
93
94 if (!input->suspended)
95 weston_seat_repick(seat);
96}
97
98static void
99udev_seat_remove_devices(struct udev_seat *seat)
100{
101 struct evdev_device *device, *next;
102
103 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
104 evdev_device_destroy(device);
105 }
106}
107
108void
109udev_input_disable(struct udev_input *input)
110{
111 if (input->suspended)
112 return;
113
114 libinput_suspend(input->libinput);
115 process_events(input);
116 input->suspended = 1;
117}
118
119static int
120udev_input_process_event(struct libinput_event *event)
121{
122 struct libinput *libinput = libinput_event_get_context(event);
123 struct libinput_device *libinput_device =
124 libinput_event_get_device(event);
125 struct udev_input *input = libinput_get_user_data(libinput);
126 struct evdev_device *device;
127 int handled = 1;
128
129 switch (libinput_event_get_type(event)) {
130 case LIBINPUT_EVENT_DEVICE_ADDED:
131 device_added(input, libinput_device);
132 break;
133 case LIBINPUT_EVENT_DEVICE_REMOVED:
134 device = libinput_device_get_user_data(libinput_device);
135 evdev_device_destroy(device);
136 break;
137 default:
138 handled = 0;
139 }
140
141 return handled;
142}
143
144static void
145process_event(struct libinput_event *event)
146{
147 if (udev_input_process_event(event))
148 return;
149 if (evdev_device_process_event(event))
150 return;
151}
152
153static void
154process_events(struct udev_input *input)
155{
156 struct libinput_event *event;
157
158 while ((event = libinput_get_event(input->libinput))) {
159 process_event(event);
160 libinput_event_destroy(event);
161 }
162}
163
164static int
165udev_input_dispatch(struct udev_input *input)
166{
167 if (libinput_dispatch(input->libinput) != 0)
168 weston_log("libinput: Failed to dispatch libinput\n");
169
170 process_events(input);
171
172 return 0;
173}
174
175static int
176libinput_source_dispatch(int fd, uint32_t mask, void *data)
177{
178 struct udev_input *input = data;
179
180 return udev_input_dispatch(input) != 0;
181}
182
183static int
184open_restricted(const char *path, int flags, void *user_data)
185{
186 struct udev_input *input = user_data;
187 struct weston_launcher *launcher = input->compositor->launcher;
188
189 return weston_launcher_open(launcher, path, flags);
190}
191
192static void
193close_restricted(int fd, void *user_data)
194{
195 struct udev_input *input = user_data;
196 struct weston_launcher *launcher = input->compositor->launcher;
197
198 weston_launcher_close(launcher, fd);
199}
200
201const struct libinput_interface libinput_interface = {
202 open_restricted,
203 close_restricted,
204};
205
206int
207udev_input_enable(struct udev_input *input)
208{
209 struct wl_event_loop *loop;
210 struct weston_compositor *c = input->compositor;
211 int fd;
212 struct udev_seat *seat;
213 int devices_found = 0;
214
215 loop = wl_display_get_event_loop(c->wl_display);
216 fd = libinput_get_fd(input->libinput);
217 input->libinput_source =
218 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
219 libinput_source_dispatch, input);
220 if (!input->libinput_source) {
221 return -1;
222 }
223
224 if (input->suspended) {
225 if (libinput_resume(input->libinput) != 0) {
226 wl_event_source_remove(input->libinput_source);
227 input->libinput_source = NULL;
228 return -1;
229 }
230 input->suspended = 0;
231 process_events(input);
232 }
233
234 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
235 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
236
237 if (!wl_list_empty(&seat->devices_list))
238 devices_found = 1;
239 }
240
241 if (devices_found == 0) {
242 weston_log(
243 "warning: no input devices on entering Weston. "
244 "Possible causes:\n"
245 "\t- no permissions to read /dev/input/event*\n"
246 "\t- seats misconfigured "
247 "(Weston backend option 'seat', "
248 "udev device property ID_SEAT)\n");
249 return -1;
250 }
251
252 return 0;
253}
254
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700255static void
Peter Hutterer3b843d32014-06-25 14:07:36 +1000256libinput_log_func(struct libinput *libinput,
257 enum libinput_log_priority priority,
258 const char *format, va_list args)
U. Artie Eoff71db0fd2014-04-17 07:53:22 -0700259{
260 weston_vlog(format, args);
261}
262
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100263int
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300264udev_input_init(struct udev_input *input, struct weston_compositor *c,
265 struct udev *udev, const char *seat_id)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100266{
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300267 enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700268 const char *log_priority = NULL;
269
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100270 memset(input, 0, sizeof *input);
271
272 input->compositor = c;
273
U. Artie Eoffc81c4242014-04-17 07:53:23 -0700274 log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
U. Artie Eoff24713f62014-05-09 11:24:40 -0700275
Peter Hutterer3b843d32014-06-25 14:07:36 +1000276 input->libinput = libinput_udev_create_context(&libinput_interface,
277 input, udev);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100278 if (!input->libinput) {
279 return -1;
280 }
Peter Hutterer3b843d32014-06-25 14:07:36 +1000281
282 libinput_log_set_handler(input->libinput, &libinput_log_func);
283
284 if (log_priority) {
285 if (strcmp(log_priority, "debug") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300286 priority = LIBINPUT_LOG_PRIORITY_DEBUG;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000287 } else if (strcmp(log_priority, "info") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300288 priority = LIBINPUT_LOG_PRIORITY_INFO;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000289 } else if (strcmp(log_priority, "error") == 0) {
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300290 priority = LIBINPUT_LOG_PRIORITY_ERROR;
Peter Hutterer3b843d32014-06-25 14:07:36 +1000291 }
292 }
293
Pekka Paalaneneb4fd352014-09-12 12:07:43 +0300294 libinput_log_set_priority(input->libinput, priority);
295
Peter Hutterer3b843d32014-06-25 14:07:36 +1000296 if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
297 libinput_unref(input->libinput);
298 return -1;
299 }
300
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100301 process_events(input);
302
303 return udev_input_enable(input);
304}
305
306void
307udev_input_destroy(struct udev_input *input)
308{
309 struct udev_seat *seat, *next;
310
311 wl_event_source_remove(input->libinput_source);
312 wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
313 udev_seat_destroy(seat);
Peter Hutterer3b843d32014-06-25 14:07:36 +1000314 libinput_unref(input->libinput);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100315}
316
317static void
318udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
319{
320 struct udev_seat *seat = (struct udev_seat *) seat_base;
321 struct evdev_device *device;
322
323 wl_list_for_each(device, &seat->devices_list, link)
324 evdev_led_update(device, leds);
325}
326
327static void
328notify_output_create(struct wl_listener *listener, void *data)
329{
330 struct udev_seat *seat = container_of(listener, struct udev_seat,
331 output_create_listener);
332 struct evdev_device *device;
333 struct weston_output *output = data;
334
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300335 wl_list_for_each(device, &seat->devices_list, link) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100336 if (device->output_name &&
337 strcmp(output->name, device->output_name) == 0) {
338 evdev_device_set_output(device, output);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339 }
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300340
341 if (device->output_name == NULL && device->output == NULL)
342 evdev_device_set_output(device, output);
343 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100344}
345
346static struct udev_seat *
347udev_seat_create(struct udev_input *input, const char *seat_name)
348{
349 struct weston_compositor *c = input->compositor;
350 struct udev_seat *seat;
351
352 seat = zalloc(sizeof *seat);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100353 if (!seat)
354 return NULL;
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000355
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356 weston_seat_init(&seat->base, c, seat_name);
357 seat->base.led_update = udev_seat_led_update;
358
359 seat->output_create_listener.notify = notify_output_create;
360 wl_signal_add(&c->output_created_signal,
361 &seat->output_create_listener);
362
363 wl_list_init(&seat->devices_list);
364
365 return seat;
366}
367
368static void
369udev_seat_destroy(struct udev_seat *seat)
370{
371 udev_seat_remove_devices(seat);
372 if (seat->base.keyboard)
373 notify_keyboard_focus_out(&seat->base);
374 weston_seat_release(&seat->base);
375 wl_list_remove(&seat->output_create_listener.link);
376 free(seat);
377}
378
379struct udev_seat *
380udev_seat_get_named(struct udev_input *input, const char *seat_name)
381{
382 struct udev_seat *seat;
383
384 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
385 if (strcmp(seat->base.seat_name, seat_name) == 0)
386 return seat;
387 }
388
389 return udev_seat_create(input, seat_name);
390}