udev-seat: Fail input setup only if no devices are found
[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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "compositor.h"
29 #include "launcher-util.h"
30 #include "evdev.h"
31 #include "udev-seat.h"
32
33 static const char default_seat[] = "seat0";
34
35 static int
36 device_added(struct udev_device *udev_device, struct udev_seat *master)
37 {
38         struct weston_compositor *c;
39         struct evdev_device *device;
40         const char *devnode;
41         const char *device_seat;
42         const char *calibration_values;
43         int fd;
44
45         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
46         if (!device_seat)
47                 device_seat = default_seat;
48
49         if (strcmp(device_seat, master->seat_id))
50                 return 0;
51
52         c = master->base.compositor;
53         devnode = udev_device_get_devnode(udev_device);
54
55         /* Use non-blocking mode so that we can loop on read on
56          * evdev_device_data() until all events on the fd are
57          * read.  mtdev_get() also expects this. */
58         fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
59         if (fd < 0) {
60                 weston_log("opening input device '%s' failed.\n", devnode);
61                 return 0;
62         }
63
64         device = evdev_device_create(&master->base, devnode, fd);
65         if (device == EVDEV_UNHANDLED_DEVICE) {
66                 close(fd);
67                 weston_log("not using input device '%s'.\n", devnode);
68                 return 0;
69         } else if (device == NULL) {
70                 close(fd);
71                 weston_log("failed to create input device '%s'.\n", devnode);
72                 return 0;
73         }
74
75         calibration_values =
76                 udev_device_get_property_value(udev_device,
77                                                "WL_CALIBRATION");
78
79         if (calibration_values && sscanf(calibration_values,
80                                          "%f %f %f %f %f %f",
81                                          &device->abs.calibration[0],
82                                          &device->abs.calibration[1],
83                                          &device->abs.calibration[2],
84                                          &device->abs.calibration[3],
85                                          &device->abs.calibration[4],
86                                          &device->abs.calibration[5]) == 6) {
87                 device->abs.apply_calibration = 1;
88                 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
89                             device->abs.calibration[0],
90                             device->abs.calibration[1],
91                             device->abs.calibration[2],
92                             device->abs.calibration[3],
93                             device->abs.calibration[4],
94                             device->abs.calibration[5]);
95         }
96
97         wl_list_insert(master->devices_list.prev, &device->link);
98
99         return 0;
100 }
101
102 static int
103 udev_seat_add_devices(struct udev_seat *seat, struct udev *udev)
104 {
105         struct udev_enumerate *e;
106         struct udev_list_entry *entry;
107         struct udev_device *device;
108         const char *path, *sysname;
109
110         e = udev_enumerate_new(udev);
111         udev_enumerate_add_match_subsystem(e, "input");
112         udev_enumerate_scan_devices(e);
113         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
114                 path = udev_list_entry_get_name(entry);
115                 device = udev_device_new_from_syspath(udev, path);
116
117                 sysname = udev_device_get_sysname(device);
118                 if (strncmp("event", sysname, 5) != 0) {
119                         udev_device_unref(device);
120                         continue;
121                 }
122
123                 if (device_added(device, seat) < 0) {
124                         udev_device_unref(device);
125                         udev_enumerate_unref(e);
126                         return -1;
127                 }
128
129                 udev_device_unref(device);
130         }
131         udev_enumerate_unref(e);
132
133         evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
134
135         if (wl_list_empty(&seat->devices_list)) {
136                 weston_log(
137                         "warning: no input devices on entering Weston. "
138                         "Possible causes:\n"
139                         "\t- no permissions to read /dev/input/event*\n"
140                         "\t- seats misconfigured "
141                         "(Weston backend option 'seat', "
142                         "udev device property ID_SEAT)\n");
143                 return -1;
144         }
145
146         return 0;
147 }
148
149 static int
150 evdev_udev_handler(int fd, uint32_t mask, void *data)
151 {
152         struct udev_seat *seat = data;
153         struct udev_device *udev_device;
154         struct evdev_device *device, *next;
155         const char *action;
156         const char *devnode;
157
158         udev_device = udev_monitor_receive_device(seat->udev_monitor);
159         if (!udev_device)
160                 return 1;
161
162         action = udev_device_get_action(udev_device);
163         if (!action)
164                 goto out;
165
166         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
167                 goto out;
168
169         if (!strcmp(action, "add")) {
170                 device_added(udev_device, seat);
171         }
172         else if (!strcmp(action, "remove")) {
173                 devnode = udev_device_get_devnode(udev_device);
174                 wl_list_for_each_safe(device, next, &seat->devices_list, link)
175                         if (!strcmp(device->devnode, devnode)) {
176                                 weston_log("input device %s, %s removed\n",
177                                            device->devname, device->devnode);
178                                 evdev_device_destroy(device);
179                                 break;
180                         }
181         }
182
183 out:
184         udev_device_unref(udev_device);
185
186         return 0;
187 }
188
189 int
190 udev_seat_enable(struct udev_seat *seat, struct udev *udev)
191 {
192         struct wl_event_loop *loop;
193         struct weston_compositor *c = seat->base.compositor;
194         int fd;
195
196         seat->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
197         if (!seat->udev_monitor) {
198                 weston_log("udev: failed to create the udev monitor\n");
199                 return -1;
200         }
201
202         udev_monitor_filter_add_match_subsystem_devtype(seat->udev_monitor,
203                         "input", NULL);
204
205         if (udev_monitor_enable_receiving(seat->udev_monitor)) {
206                 weston_log("udev: failed to bind the udev monitor\n");
207                 udev_monitor_unref(seat->udev_monitor);
208                 return -1;
209         }
210
211         loop = wl_display_get_event_loop(c->wl_display);
212         fd = udev_monitor_get_fd(seat->udev_monitor);
213         seat->udev_monitor_source =
214                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
215                                      evdev_udev_handler, seat);
216         if (!seat->udev_monitor_source) {
217                 udev_monitor_unref(seat->udev_monitor);
218                 return -1;
219         }
220
221         if (udev_seat_add_devices(seat, udev) < 0)
222                 return -1;
223
224         return 0;
225 }
226
227 static void
228 udev_seat_remove_devices(struct udev_seat *seat)
229 {
230         struct evdev_device *device, *next;
231
232         wl_list_for_each_safe(device, next, &seat->devices_list, link)
233                 evdev_device_destroy(device);
234
235         if (seat->base.keyboard)
236                 notify_keyboard_focus_out(&seat->base);
237 }
238
239 void
240 udev_seat_disable(struct udev_seat *seat)
241 {
242         if (!seat->udev_monitor)
243                 return;
244
245         udev_monitor_unref(seat->udev_monitor);
246         seat->udev_monitor = NULL;
247         wl_event_source_remove(seat->udev_monitor_source);
248         seat->udev_monitor_source = NULL;
249
250         udev_seat_remove_devices(seat);
251 }
252
253 static void
254 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
255 {
256         struct udev_seat *seat = (struct udev_seat *) seat_base;
257         struct evdev_device *device;
258
259         wl_list_for_each(device, &seat->devices_list, link)
260                 evdev_led_update(device, leds);
261 }
262
263 struct udev_seat *
264 udev_seat_create(struct weston_compositor *c, struct udev *udev,
265                 const char *seat_id)
266 {
267         struct udev_seat *seat;
268
269         seat = malloc(sizeof *seat);
270         if (seat == NULL)
271                 return NULL;
272
273         memset(seat, 0, sizeof *seat);
274         weston_seat_init(&seat->base, c);
275         seat->base.led_update = drm_led_update;
276
277         wl_list_init(&seat->devices_list);
278         seat->seat_id = strdup(seat_id);
279         if (udev_seat_enable(seat, udev) < 0)
280                 goto err;
281
282         return seat;
283
284  err:
285         free(seat->seat_id);
286         free(seat);
287         return NULL;
288 }
289
290 void
291 udev_seat_destroy(struct udev_seat *seat)
292 {
293         udev_seat_disable(seat);
294
295         weston_seat_release(&seat->base);
296         free(seat->seat_id);
297         free(seat);
298 }