udev-seat: Refactor out seat lookup and possible creation
[platform/upstream/libinput.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[] = "default";
37
38 static struct udev_seat *
39 udev_seat_create(struct weston_compositor *c, const char *seat_name);
40 static void
41 udev_seat_destroy(struct udev_seat *seat);
42
43 static int
44 device_added(struct udev_device *udev_device, struct udev_input *input)
45 {
46         struct weston_compositor *c;
47         struct evdev_device *device;
48         const char *devnode;
49         const char *device_seat, *seat_name;
50         const char *calibration_values;
51         int fd;
52         struct udev_seat *seat;
53
54         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
55         if (!device_seat)
56                 device_seat = default_seat;
57
58         if (strcmp(device_seat, input->seat_id))
59                 return 0;
60
61         c = input->compositor;
62         devnode = udev_device_get_devnode(udev_device);
63
64         /* Search for matching logical seat */
65         seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
66         if (!seat_name)
67                 seat_name = default_seat_name;
68
69         seat = udev_seat_get_named(c, seat_name);
70
71         if (seat == NULL)
72                 return -1;
73
74         /* Use non-blocking mode so that we can loop on read on
75          * evdev_device_data() until all events on the fd are
76          * read.  mtdev_get() also expects this. */
77         fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
78         if (fd < 0) {
79                 weston_log("opening input device '%s' failed.\n", devnode);
80                 return 0;
81         }
82
83         device = evdev_device_create(&seat->base, devnode, fd);
84         if (device == EVDEV_UNHANDLED_DEVICE) {
85                 close(fd);
86                 weston_log("not using input device '%s'.\n", devnode);
87                 return 0;
88         } else if (device == NULL) {
89                 close(fd);
90                 weston_log("failed to create input device '%s'.\n", devnode);
91                 return 0;
92         }
93
94         calibration_values =
95                 udev_device_get_property_value(udev_device,
96                                                "WL_CALIBRATION");
97
98         if (calibration_values && sscanf(calibration_values,
99                                          "%f %f %f %f %f %f",
100                                          &device->abs.calibration[0],
101                                          &device->abs.calibration[1],
102                                          &device->abs.calibration[2],
103                                          &device->abs.calibration[3],
104                                          &device->abs.calibration[4],
105                                          &device->abs.calibration[5]) == 6) {
106                 device->abs.apply_calibration = 1;
107                 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
108                             device->abs.calibration[0],
109                             device->abs.calibration[1],
110                             device->abs.calibration[2],
111                             device->abs.calibration[3],
112                             device->abs.calibration[4],
113                             device->abs.calibration[5]);
114         }
115
116         wl_list_insert(seat->devices_list.prev, &device->link);
117
118         return 0;
119 }
120
121 static int
122 udev_input_add_devices(struct udev_input *input, struct udev *udev)
123 {
124         struct udev_enumerate *e;
125         struct udev_list_entry *entry;
126         struct udev_device *device;
127         const char *path, *sysname;
128         struct udev_seat *seat;
129         int devices_found = 0;
130
131         e = udev_enumerate_new(udev);
132         udev_enumerate_add_match_subsystem(e, "input");
133         udev_enumerate_scan_devices(e);
134         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
135                 path = udev_list_entry_get_name(entry);
136                 device = udev_device_new_from_syspath(udev, path);
137
138                 sysname = udev_device_get_sysname(device);
139                 if (strncmp("event", sysname, 5) != 0) {
140                         udev_device_unref(device);
141                         continue;
142                 }
143
144                 if (device_added(device, input) < 0) {
145                         udev_device_unref(device);
146                         udev_enumerate_unref(e);
147                         return -1;
148                 }
149
150                 udev_device_unref(device);
151         }
152         udev_enumerate_unref(e);
153
154         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
155                 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
156
157                 if (!wl_list_empty(&seat->devices_list))
158                         devices_found = 1;
159         }
160
161         if (devices_found == 0) {
162                 weston_log(
163                         "warning: no input devices on entering Weston. "
164                         "Possible causes:\n"
165                         "\t- no permissions to read /dev/input/event*\n"
166                         "\t- seats misconfigured "
167                         "(Weston backend option 'seat', "
168                         "udev device property ID_SEAT)\n");
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 static int
176 evdev_udev_handler(int fd, uint32_t mask, void *data)
177 {
178         struct udev_input *input = data;
179         struct udev_device *udev_device;
180         struct evdev_device *device, *next;
181         const char *action;
182         const char *devnode;
183         struct udev_seat *seat;
184
185         udev_device = udev_monitor_receive_device(input->udev_monitor);
186         if (!udev_device)
187                 return 1;
188
189         action = udev_device_get_action(udev_device);
190         if (!action)
191                 goto out;
192
193         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
194                 goto out;
195
196         if (!strcmp(action, "add")) {
197                 device_added(udev_device, input);
198         }
199         else if (!strcmp(action, "remove")) {
200                 devnode = udev_device_get_devnode(udev_device);
201                 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
202                         wl_list_for_each_safe(device, next, &seat->devices_list, link)
203                                 if (!strcmp(device->devnode, devnode)) {
204                                         weston_log("input device %s, %s removed\n",
205                                                         device->devname, device->devnode);
206                                         evdev_device_destroy(device);
207                                 break;
208                         }
209                 }
210         }
211
212 out:
213         udev_device_unref(udev_device);
214
215         return 0;
216 }
217
218 int
219 udev_input_enable(struct udev_input *input, struct udev *udev)
220 {
221         struct wl_event_loop *loop;
222         struct weston_compositor *c = input->compositor;
223         int fd;
224
225         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
226         if (!input->udev_monitor) {
227                 weston_log("udev: failed to create the udev monitor\n");
228                 return -1;
229         }
230
231         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
232                         "input", NULL);
233
234         if (udev_monitor_enable_receiving(input->udev_monitor)) {
235                 weston_log("udev: failed to bind the udev monitor\n");
236                 udev_monitor_unref(input->udev_monitor);
237                 return -1;
238         }
239
240         loop = wl_display_get_event_loop(c->wl_display);
241         fd = udev_monitor_get_fd(input->udev_monitor);
242         input->udev_monitor_source =
243                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
244                                      evdev_udev_handler, input);
245         if (!input->udev_monitor_source) {
246                 udev_monitor_unref(input->udev_monitor);
247                 return -1;
248         }
249
250         if (udev_input_add_devices(input, udev) < 0)
251                 return -1;
252
253         return 0;
254 }
255
256 static void
257 udev_input_remove_devices(struct udev_input *input)
258 {
259         struct evdev_device *device, *next;
260         struct udev_seat *seat;
261
262         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
263                 wl_list_for_each_safe(device, next, &seat->devices_list, link)
264                         evdev_device_destroy(device);
265
266                 if (seat->base.keyboard)
267                         notify_keyboard_focus_out(&seat->base);
268         }
269 }
270
271 void
272 udev_input_disable(struct udev_input *input)
273 {
274         if (!input->udev_monitor)
275                 return;
276
277         udev_monitor_unref(input->udev_monitor);
278         input->udev_monitor = NULL;
279         wl_event_source_remove(input->udev_monitor_source);
280         input->udev_monitor_source = NULL;
281
282         udev_input_remove_devices(input);
283 }
284
285
286 int
287 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
288                 const char *seat_id)
289 {
290         memset(input, 0, sizeof *input);
291         input->seat_id = strdup(seat_id);
292         input->compositor = c;
293         if (udev_input_enable(input, udev) < 0)
294                 goto err;
295
296         return 0;
297
298  err:
299         free(input->seat_id);
300         return -1;
301 }
302
303 void
304 udev_input_destroy(struct udev_input *input)
305 {
306         struct udev_seat *seat, *next;
307         udev_input_disable(input);
308         wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
309                 udev_seat_destroy(seat);
310         free(input->seat_id);
311 }
312
313 static void
314 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
315 {
316         struct udev_seat *seat = (struct udev_seat *) seat_base;
317         struct evdev_device *device;
318
319         wl_list_for_each(device, &seat->devices_list, link)
320                 evdev_led_update(device, leds);
321 }
322
323 static struct udev_seat *
324 udev_seat_create(struct weston_compositor *c, const char *seat_name)
325 {
326         struct udev_seat *seat;
327
328         seat = malloc(sizeof *seat);
329
330         if (!seat)
331                 return NULL;
332         memset(seat, 0, sizeof *seat);
333         weston_seat_init(&seat->base, c, seat_name);
334         seat->base.led_update = drm_led_update;
335
336         wl_list_init(&seat->devices_list);
337         return seat;
338 }
339
340 static void
341 udev_seat_destroy(struct udev_seat *seat)
342 {
343         weston_seat_release(&seat->base);
344         free(seat);
345 }
346
347 struct udev_seat *
348 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
349 {
350         struct udev_seat *seat;
351
352         wl_list_for_each(seat, &c->seat_list, base.link) {
353                 if (strcmp(seat->base.seat_name, seat_name) == 0)
354                         return seat;
355         }
356
357         seat = udev_seat_create(c, seat_name);
358
359         if (!seat)
360                 return NULL;
361
362         return seat;
363 }