downstream: Multiseat support for drm/wayland backends
[profile/ivi/weston-ivi-shell.git] / src / libinput-seat.c
1 /*
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
38 static const char default_seat[] = "seat0";
39 static const char default_seat_name[] = "default";
40
41 static void
42 process_events(struct udev_input *input);
43 static struct udev_seat *
44 udev_seat_create(struct weston_compositor *c, const char *seat_name);
45 static void
46 udev_seat_destroy(struct udev_seat *seat);
47
48 static int
49 udev_seat_has_output(struct udev_seat *seat)
50 {
51         struct weston_output *output;
52         wl_list_for_each(output, &seat->base.compositor->output_list, link) {
53                 if (seat && (&seat->base == output->seat_data.seat))
54                         return 1;
55         }
56         return 0;
57 }
58
59 static void
60 device_added(struct udev_input *input, struct libinput_device *libinput_device)
61 {
62         struct weston_compositor *c;
63         struct evdev_device *device;
64         struct weston_output *output;
65         const char *seat_name;
66         const char *output_name;
67         struct libinput_seat *libinput_seat;
68         struct weston_seat *seat;
69         struct udev_seat *udev_seat;
70
71         c = input->compositor;
72         libinput_seat = libinput_device_get_seat(libinput_device);
73
74         seat_name = libinput_seat_get_logical_name(libinput_seat);
75         udev_seat = udev_seat_get_named(c, seat_name);
76         if (!udev_seat)
77                 return;
78
79         seat = &udev_seat->base;
80         device = evdev_device_create(libinput_device, seat);
81         if (device == NULL)
82                 return;
83
84         wl_list_insert(udev_seat->devices_list.prev, &device->link);
85
86         if (udev_seat_has_output(udev_seat) && seat->pointer)
87                 weston_pointer_clamp(seat->pointer,
88                                      &seat->pointer->x,
89                                      &seat->pointer->y);
90
91         output_name = libinput_device_get_output_name(libinput_device);
92         if (output_name) {
93                 device->output_name = strdup(output_name);
94                 wl_list_for_each(output, &c->output_list, link)
95                         if (strcmp(output->name, device->output_name) == 0)
96                                 evdev_device_set_output(device, output);
97         } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
98                 output = container_of(c->output_list.next,
99                                       struct weston_output, link);
100                 evdev_device_set_output(device, output);
101         }
102
103         if (!input->suspended)
104                 weston_seat_repick(seat);
105
106 }
107
108 static void
109 udev_seat_remove_devices(struct udev_seat *seat)
110 {
111         struct evdev_device *device, *next;
112
113         wl_list_for_each_safe(device, next, &seat->devices_list, link) {
114                 evdev_device_destroy(device);
115         }
116 }
117
118 void
119 udev_input_disable(struct udev_input *input)
120 {
121         if (input->suspended)
122                 return;
123
124         libinput_suspend(input->libinput);
125         process_events(input);
126         input->suspended = 1;
127 }
128
129 static int
130 udev_input_process_event(struct libinput_event *event)
131 {
132         struct libinput *libinput = libinput_event_get_context(event);
133         struct libinput_device *libinput_device =
134                 libinput_event_get_device(event);
135         struct udev_input *input = libinput_get_user_data(libinput);
136         struct evdev_device *device;
137         int handled = 1;
138
139         switch (libinput_event_get_type(event)) {
140         case LIBINPUT_EVENT_DEVICE_ADDED:
141                 device_added(input, libinput_device);
142                 break;
143         case LIBINPUT_EVENT_DEVICE_REMOVED:
144                 device = libinput_device_get_user_data(libinput_device);
145                 evdev_device_destroy(device);
146                 break;
147         default:
148                 handled = 0;
149         }
150
151         return handled;
152 }
153
154 static void
155 process_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
163 static void
164 process_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
174 static int
175 udev_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
185 static int
186 libinput_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
193 static int
194 open_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
202 static void
203 close_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
211 const struct libinput_interface libinput_interface = {
212         open_restricted,
213         close_restricted,
214 };
215
216 int
217 udev_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
265 static void
266 libinput_log_func(struct libinput *libinput,
267                   enum libinput_log_priority priority,
268                   const char *format, va_list args)
269 {
270         weston_vlog(format, args);
271 }
272
273 int
274 udev_input_init(struct udev_input *input, struct weston_compositor *c,
275                 struct udev *udev, const char *seat_id)
276 {
277         enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO;
278         const char *log_priority = NULL;
279
280         memset(input, 0, sizeof *input);
281
282         input->compositor = c;
283
284         log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY");
285
286         input->libinput = libinput_udev_create_context(&libinput_interface,
287                                                        input, udev);
288         if (!input->libinput) {
289                 return -1;
290         }
291
292         libinput_log_set_handler(input->libinput, &libinput_log_func);
293
294         if (log_priority) {
295                 if (strcmp(log_priority, "debug") == 0) {
296                         priority = LIBINPUT_LOG_PRIORITY_DEBUG;
297                 } else if (strcmp(log_priority, "info") == 0) {
298                         priority = LIBINPUT_LOG_PRIORITY_INFO;
299                 } else if (strcmp(log_priority, "error") == 0) {
300                         priority = LIBINPUT_LOG_PRIORITY_ERROR;
301                 }
302         }
303
304         libinput_log_set_priority(input->libinput, priority);
305
306         if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) {
307                 libinput_unref(input->libinput);
308                 return -1;
309         }
310
311         process_events(input);
312
313         return udev_input_enable(input);
314 }
315
316 void
317 udev_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);
324         libinput_unref(input->libinput);
325 }
326
327 static void
328 udev_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
337 static int
338 udev_seat_is_output_linked(struct udev_seat *seat, struct weston_output *output)
339 {
340         if (seat && output->seat_data.seat &&
341                         (&seat->base == output->seat_data.seat)) {
342                 weston_log("output seatname (%s) is linked already with seat\n",
343                                 output->seat_data.seatname);
344                 return 1;
345         }
346         return 0;
347 }
348
349 int
350 udev_seat_link_output(struct udev_seat *seat, struct weston_output *output)
351 {
352         if (udev_seat_is_output_linked(seat, output)) return 1;
353
354         if (!seat || !output || !seat->base.seat_name ||
355             !output->seat_data.seatname ||
356             strcmp(seat->base.seat_name, output->seat_data.seatname) != 0) {
357                 return 0;
358         }
359
360         free(output->seat_data.seatname);
361         output->seat_data.seatname = strdup(seat->base.seat_name);
362         output->seat_data.seat = &seat->base;
363         weston_log("link output to seat %s\n", seat->base.seat_name);
364         return 1;
365 }
366
367 static void
368 notify_output_create(struct wl_listener *listener, void *data)
369 {
370         struct udev_seat *seat = container_of(listener, struct udev_seat,
371                                               output_create_listener);
372         struct evdev_device *device, *next;
373         struct weston_output *output = data;
374
375         weston_log("notify received for output created for seat %s\n",
376                         seat->base.seat_name);
377         udev_seat_link_output(seat, output);
378
379         wl_list_for_each_safe(device, next, &seat->devices_list, link) {
380                 if (device->output_name && output->name &&
381                     strcmp(output->name, device->output_name) == 0) {
382                         evdev_device_set_output(device, output);
383                 }
384
385                 if (device->output_name == NULL && device->output == NULL)
386                         evdev_device_set_output(device, output);
387         }
388 }
389
390 static struct udev_seat *
391 udev_seat_create(struct weston_compositor *c, const char *seat_name)
392 {
393         struct udev_seat *seat;
394
395         seat = zalloc(sizeof *seat);
396         if (!seat)
397                 return NULL;
398
399         weston_seat_init(&seat->base, c, seat_name);
400         seat->base.led_update = udev_seat_led_update;
401
402         seat->output_create_listener.notify = notify_output_create;
403         wl_signal_add(&c->output_created_signal,
404                       &seat->output_create_listener);
405
406         wl_list_init(&seat->devices_list);
407
408         weston_log("seat created with name %s\n", seat_name);
409         return seat;
410 }
411
412 static void
413 udev_seat_destroy(struct udev_seat *seat)
414 {
415         udev_seat_remove_devices(seat);
416         if (seat->base.keyboard)
417                 notify_keyboard_focus_out(&seat->base);
418         weston_seat_release(&seat->base);
419         wl_list_remove(&seat->output_create_listener.link);
420         free(seat);
421 }
422
423 struct udev_seat *
424 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
425 {
426         struct udev_seat *seat;
427
428         wl_list_for_each(seat, &c->seat_list, base.link) {
429                 if (strcmp(seat->base.seat_name, seat_name) == 0)
430                         return seat;
431         }
432
433         return udev_seat_create(c, seat_name);
434 }