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