launcher: add weston_launcher_close() dummy
[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                 weston_launcher_close(c->launcher, fd);
87                 weston_log("not using input device '%s'.\n", devnode);
88                 return 0;
89         } else if (device == NULL) {
90                 weston_launcher_close(c->launcher, 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                                         weston_launcher_close(input->compositor->launcher,
223                                                               device->fd);
224                                         evdev_device_destroy(device);
225                                         break;
226                                 }
227                 }
228         }
229
230 out:
231         udev_device_unref(udev_device);
232
233         return 0;
234 }
235
236 int
237 udev_input_enable(struct udev_input *input, struct udev *udev)
238 {
239         struct wl_event_loop *loop;
240         struct weston_compositor *c = input->compositor;
241         int fd;
242
243         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
244         if (!input->udev_monitor) {
245                 weston_log("udev: failed to create the udev monitor\n");
246                 return -1;
247         }
248
249         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
250                         "input", NULL);
251
252         if (udev_monitor_enable_receiving(input->udev_monitor)) {
253                 weston_log("udev: failed to bind the udev monitor\n");
254                 udev_monitor_unref(input->udev_monitor);
255                 return -1;
256         }
257
258         loop = wl_display_get_event_loop(c->wl_display);
259         fd = udev_monitor_get_fd(input->udev_monitor);
260         input->udev_monitor_source =
261                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
262                                      evdev_udev_handler, input);
263         if (!input->udev_monitor_source) {
264                 udev_monitor_unref(input->udev_monitor);
265                 return -1;
266         }
267
268         if (udev_input_add_devices(input, udev) < 0)
269                 return -1;
270
271         input->enabled = 1;
272
273         return 0;
274 }
275
276 static void
277 udev_input_remove_devices(struct udev_input *input)
278 {
279         struct evdev_device *device, *next;
280         struct udev_seat *seat;
281
282         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
283                 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
284                         weston_launcher_close(input->compositor->launcher,
285                                               device->fd);
286                         evdev_device_destroy(device);
287                 }
288
289                 if (seat->base.keyboard)
290                         notify_keyboard_focus_out(&seat->base);
291         }
292 }
293
294 void
295 udev_input_disable(struct udev_input *input)
296 {
297         if (!input->udev_monitor)
298                 return;
299
300         udev_monitor_unref(input->udev_monitor);
301         input->udev_monitor = NULL;
302         wl_event_source_remove(input->udev_monitor_source);
303         input->udev_monitor_source = NULL;
304
305         udev_input_remove_devices(input);
306 }
307
308
309 int
310 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
311                 const char *seat_id)
312 {
313         memset(input, 0, sizeof *input);
314         input->seat_id = strdup(seat_id);
315         input->compositor = c;
316         if (udev_input_enable(input, udev) < 0)
317                 goto err;
318
319         return 0;
320
321  err:
322         free(input->seat_id);
323         return -1;
324 }
325
326 void
327 udev_input_destroy(struct udev_input *input)
328 {
329         struct udev_seat *seat, *next;
330         udev_input_disable(input);
331         wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
332                 udev_seat_destroy(seat);
333         free(input->seat_id);
334 }
335
336 static void
337 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
338 {
339         struct udev_seat *seat = (struct udev_seat *) seat_base;
340         struct evdev_device *device;
341
342         wl_list_for_each(device, &seat->devices_list, link)
343                 evdev_led_update(device, leds);
344 }
345
346 static struct udev_seat *
347 udev_seat_create(struct weston_compositor *c, const char *seat_name)
348 {
349         struct udev_seat *seat;
350
351         seat = zalloc(sizeof *seat);
352
353         if (!seat)
354                 return NULL;
355         weston_seat_init(&seat->base, c, seat_name);
356         seat->base.led_update = drm_led_update;
357
358         wl_list_init(&seat->devices_list);
359         return seat;
360 }
361
362 static void
363 udev_seat_destroy(struct udev_seat *seat)
364 {
365         weston_seat_release(&seat->base);
366         free(seat);
367 }
368
369 struct udev_seat *
370 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
371 {
372         struct udev_seat *seat;
373
374         wl_list_for_each(seat, &c->seat_list, base.link) {
375                 if (strcmp(seat->base.seat_name, seat_name) == 0)
376                         return seat;
377         }
378
379         seat = udev_seat_create(c, seat_name);
380
381         if (!seat)
382                 return NULL;
383
384         return seat;
385 }