downstream: Multiseat support for drm/wayland backends
[profile/ivi/weston-ivi-shell.git] / src / udev-seat.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "compositor.h"
31 #include "launcher-util.h"
32 #include "evdev.h"
33 #include "udev-seat.h"
34
35 static const char default_seat[] = "seat0";
36 static const char default_seat_name[] = "seat0";
37
38 static void
39 udev_seat_destroy(struct udev_seat *seat);
40
41 static int
42 udev_seat_has_output(struct udev_seat *seat)
43 {
44         struct weston_output *output;
45         wl_list_for_each(output, &seat->base.compositor->output_list, link) {
46                 if (seat && (&seat->base == output->seat_data.seat))
47                         return 1;
48         }
49         return 0;
50 }
51
52 static int
53 device_added(struct udev_device *udev_device, struct udev_input *input)
54 {
55         struct weston_compositor *c;
56         struct evdev_device *device;
57         struct weston_output *output;
58         const char *devnode;
59         const char *device_seat, *seat_name, *output_name;
60         const char *calibration_values;
61         int fd;
62         struct udev_seat *seat;
63
64         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
65         if (!device_seat)
66                 device_seat = default_seat;
67
68         if (strcmp(device_seat, input->seat_id))
69                 return 0;
70
71         c = input->compositor;
72         devnode = udev_device_get_devnode(udev_device);
73
74         /* Search for matching logical seat */
75         seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
76         if (!seat_name)
77                 seat_name = default_seat_name;
78
79         seat = udev_seat_get_named(c, seat_name);
80
81         if (seat == NULL)
82                 return -1;
83
84         /* Use non-blocking mode so that we can loop on read on
85          * evdev_device_data() until all events on the fd are
86          * read.  mtdev_get() also expects this. */
87         fd = weston_launcher_open(c->launcher, devnode, O_RDWR | O_NONBLOCK);
88         if (fd < 0) {
89                 weston_log("opening input device '%s' failed.\n", devnode);
90                 return 0;
91         }
92
93         device = evdev_device_create(&seat->base, devnode, fd);
94         if (device == EVDEV_UNHANDLED_DEVICE) {
95                 weston_launcher_close(c->launcher, fd);
96                 weston_log("not using input device '%s'.\n", devnode);
97                 return 0;
98         } else if (device == NULL) {
99                 weston_launcher_close(c->launcher, fd);
100                 weston_log("failed to create input device '%s'.\n", devnode);
101                 return 0;
102         }
103
104         calibration_values =
105                 udev_device_get_property_value(udev_device,
106                                                "WL_CALIBRATION");
107
108         if (calibration_values && sscanf(calibration_values,
109                                          "%f %f %f %f %f %f",
110                                          &device->abs.calibration[0],
111                                          &device->abs.calibration[1],
112                                          &device->abs.calibration[2],
113                                          &device->abs.calibration[3],
114                                          &device->abs.calibration[4],
115                                          &device->abs.calibration[5]) == 6) {
116                 device->abs.apply_calibration = 1;
117                 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
118                             device->abs.calibration[0],
119                             device->abs.calibration[1],
120                             device->abs.calibration[2],
121                             device->abs.calibration[3],
122                             device->abs.calibration[4],
123                             device->abs.calibration[5]);
124         }
125
126         wl_list_insert(seat->devices_list.prev, &device->link);
127
128         if (udev_seat_has_output(seat) && seat->base.pointer)
129                 weston_pointer_clamp(seat->base.pointer,
130                                      &seat->base.pointer->x,
131                                      &seat->base.pointer->y);
132
133         output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
134         if (output_name) {
135                 device->output_name = strdup(output_name);
136                 wl_list_for_each(output, &c->output_list, link)
137                         if (strcmp(output->name, device->output_name) == 0)
138                                 evdev_device_set_output(device, output);
139         } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
140                 output = container_of(c->output_list.next,
141                                       struct weston_output, link);
142                 evdev_device_set_output(device, output);
143         }
144
145         if (input->enabled == 1)
146                 weston_seat_repick(&seat->base);
147
148         weston_log("device with path %s is added to seat %s\n", devnode,
149                 input->seat_id);
150         return 0;
151 }
152
153 static int
154 udev_input_add_devices(struct udev_input *input, struct udev *udev)
155 {
156         struct udev_enumerate *e;
157         struct udev_list_entry *entry;
158         struct udev_device *device;
159         const char *path, *sysname;
160         struct udev_seat *seat;
161         int devices_found = 0;
162
163         e = udev_enumerate_new(udev);
164         udev_enumerate_add_match_subsystem(e, "input");
165         udev_enumerate_scan_devices(e);
166         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
167                 path = udev_list_entry_get_name(entry);
168                 device = udev_device_new_from_syspath(udev, path);
169
170                 sysname = udev_device_get_sysname(device);
171                 if (strncmp("event", sysname, 5) != 0) {
172                         udev_device_unref(device);
173                         continue;
174                 }
175
176                 if (device_added(device, input) < 0) {
177                         udev_device_unref(device);
178                         udev_enumerate_unref(e);
179                         return -1;
180                 }
181
182                 udev_device_unref(device);
183         }
184         udev_enumerate_unref(e);
185
186         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
187                 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
188
189                 if (!wl_list_empty(&seat->devices_list))
190                         devices_found = 1;
191         }
192
193         if (devices_found == 0) {
194                 weston_log(
195                         "warning: no input devices on entering Weston. "
196                         "Possible causes:\n"
197                         "\t- no permissions to read /dev/input/event*\n"
198                         "\t- seats misconfigured "
199                         "(Weston backend option 'seat', "
200                         "udev device property ID_SEAT)\n");
201                 return -1;
202         }
203
204         return 0;
205 }
206
207 static int
208 evdev_udev_handler(int fd, uint32_t mask, void *data)
209 {
210         struct udev_input *input = data;
211         struct udev_device *udev_device;
212         struct evdev_device *device, *next;
213         const char *action;
214         const char *devnode;
215         struct udev_seat *seat;
216
217         udev_device = udev_monitor_receive_device(input->udev_monitor);
218         if (!udev_device)
219                 return 1;
220
221         action = udev_device_get_action(udev_device);
222         if (!action)
223                 goto out;
224
225         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
226                 goto out;
227
228         if (!strcmp(action, "add")) {
229                 device_added(udev_device, input);
230         }
231         else if (!strcmp(action, "remove")) {
232                 devnode = udev_device_get_devnode(udev_device);
233                 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
234                         wl_list_for_each_safe(device, next, &seat->devices_list, link)
235                                 if (!strcmp(device->devnode, devnode)) {
236                                         weston_log("input device %s, %s removed\n",
237                                                         device->devname, device->devnode);
238                                         weston_launcher_close(input->compositor->launcher,
239                                                               device->fd);
240                                         evdev_device_destroy(device);
241                                         break;
242                                 }
243                 }
244         }
245
246 out:
247         udev_device_unref(udev_device);
248
249         return 0;
250 }
251
252 int
253 udev_input_enable(struct udev_input *input)
254 {
255         struct wl_event_loop *loop;
256         struct weston_compositor *c = input->compositor;
257         int fd;
258
259         input->udev_monitor = udev_monitor_new_from_netlink(input->udev, "udev");
260         if (!input->udev_monitor) {
261                 weston_log("udev: failed to create the udev monitor\n");
262                 return -1;
263         }
264
265         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
266                         "input", NULL);
267
268         if (udev_monitor_enable_receiving(input->udev_monitor)) {
269                 weston_log("udev: failed to bind the udev monitor\n");
270                 udev_monitor_unref(input->udev_monitor);
271                 return -1;
272         }
273
274         loop = wl_display_get_event_loop(c->wl_display);
275         fd = udev_monitor_get_fd(input->udev_monitor);
276         input->udev_monitor_source =
277                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
278                                      evdev_udev_handler, input);
279         if (!input->udev_monitor_source) {
280                 udev_monitor_unref(input->udev_monitor);
281                 return -1;
282         }
283
284         if (udev_input_add_devices(input, input->udev) < 0)
285                 return -1;
286
287         input->enabled = 1;
288
289         return 0;
290 }
291
292 static void
293 udev_input_remove_devices(struct udev_input *input)
294 {
295         struct evdev_device *device, *next;
296         struct udev_seat *seat;
297
298         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
299                 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
300                         weston_launcher_close(input->compositor->launcher,
301                                               device->fd);
302                         evdev_device_destroy(device);
303                 }
304
305                 if (seat->base.keyboard)
306                         notify_keyboard_focus_out(&seat->base);
307         }
308 }
309
310 void
311 udev_input_disable(struct udev_input *input)
312 {
313         if (!input->udev_monitor)
314                 return;
315
316         udev_monitor_unref(input->udev_monitor);
317         input->udev_monitor = NULL;
318         wl_event_source_remove(input->udev_monitor_source);
319         input->udev_monitor_source = NULL;
320
321         udev_input_remove_devices(input);
322 }
323
324
325 int
326 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
327                 const char *seat_id)
328 {
329         memset(input, 0, sizeof *input);
330         input->seat_id = strdup(seat_id);
331         input->compositor = c;
332         input->udev = udev_ref(udev);
333         if (udev_input_enable(input) < 0)
334                 goto err;
335
336         return 0;
337
338  err:
339         return -1;
340 }
341
342 void
343 udev_input_destroy(struct udev_input *input)
344 {
345         struct udev_seat *seat, *next;
346         udev_input_disable(input);
347         wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
348                 udev_seat_destroy(seat);
349         udev_unref(input->udev);
350         free(input->seat_id);
351 }
352
353 static void
354 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
355 {
356         struct udev_seat *seat = (struct udev_seat *) seat_base;
357         struct evdev_device *device, *next;
358
359         wl_list_for_each_safe(device, next, &seat->devices_list, link)
360                 evdev_led_update(device, leds);
361 }
362
363 static int
364 udev_seat_is_output_linked(struct udev_seat *seat, struct weston_output *output)
365 {
366         if (seat && output->seat_data.seat &&
367                         (&seat->base == output->seat_data.seat)) {
368                 weston_log("output seatname (%s) is linked already with seat\n",
369                                 output->seat_data.seatname);
370                 return 1;
371         }
372         return 0;
373 }
374
375 int
376 udev_seat_link_output(struct udev_seat *seat, struct weston_output *output)
377 {
378         if (udev_seat_is_output_linked(seat, output)) return 1;
379
380         if (!seat || !output || !seat->base.seat_name ||
381             !output->seat_data.seatname ||
382             strcmp(seat->base.seat_name, output->seat_data.seatname) != 0) {
383                 return 0;
384         }
385
386         weston_log("link output to seat %s\n", seat->base.seat_name);
387         free(output->seat_data.seatname);
388         output->seat_data.seatname = strdup(seat->base.seat_name);
389         output->seat_data.seat = &seat->base;
390         return 1;
391 }
392
393 static void
394 notify_output_create(struct wl_listener *listener, void *data)
395 {
396         struct udev_seat *seat = container_of(listener, struct udev_seat,
397                                               output_create_listener);
398         struct evdev_device *device, *next;
399         struct weston_output *output = data;
400
401         weston_log("notify received for output created for seat %s\n",
402                         seat->base.seat_name);
403         if (!udev_seat_link_output(seat, output))
404                 return;
405
406         wl_list_for_each_safe(device, next, &seat->devices_list, link) {
407                 if (device->output_name && output->name &&
408                     strcmp(output->name, device->output_name) == 0) {
409                         evdev_device_set_output(device, output);
410                 }
411
412                 if (device->output_name == NULL && device->output == NULL)
413                         evdev_device_set_output(device, output);
414         }
415 }
416
417 static struct udev_seat *
418 udev_seat_create(struct weston_compositor *c, const char *seat_name)
419 {
420         struct udev_seat *seat;
421
422         seat = zalloc(sizeof *seat);
423         if (!seat)
424                 return NULL;
425
426         weston_seat_init(&seat->base, c, seat_name);
427         seat->base.led_update = drm_led_update;
428
429         seat->output_create_listener.notify = notify_output_create;
430         wl_signal_add(&c->output_created_signal,
431                       &seat->output_create_listener);
432
433         wl_list_init(&seat->devices_list);
434         weston_log("created seat with name %s\n", seat_name);
435
436         return seat;
437 }
438
439 static void
440 udev_seat_destroy(struct udev_seat *seat)
441 {
442         weston_log("seat destroyed with name %s\n", seat->base.seat_name);
443         weston_seat_release(&seat->base);
444         wl_list_remove(&seat->output_create_listener.link);
445         free(seat);
446 }
447
448 struct udev_seat *
449 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
450 {
451         struct udev_seat *seat, *next;
452
453         wl_list_for_each_safe(seat, next, &c->seat_list, base.link) {
454                 if (strcmp(seat->base.seat_name, seat_name) == 0)
455                         return seat;
456         }
457
458         seat = udev_seat_create(c, seat_name);
459
460         if (!seat)
461                 return NULL;
462
463         return seat;
464 }