launcher: Collect launcher state in new struct weston_launcher
[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         return 0;
132 }
133
134 static int
135 udev_input_add_devices(struct udev_input *input, struct udev *udev)
136 {
137         struct udev_enumerate *e;
138         struct udev_list_entry *entry;
139         struct udev_device *device;
140         const char *path, *sysname;
141         struct udev_seat *seat;
142         int devices_found = 0;
143
144         e = udev_enumerate_new(udev);
145         udev_enumerate_add_match_subsystem(e, "input");
146         udev_enumerate_scan_devices(e);
147         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
148                 path = udev_list_entry_get_name(entry);
149                 device = udev_device_new_from_syspath(udev, path);
150
151                 sysname = udev_device_get_sysname(device);
152                 if (strncmp("event", sysname, 5) != 0) {
153                         udev_device_unref(device);
154                         continue;
155                 }
156
157                 if (device_added(device, input) < 0) {
158                         udev_device_unref(device);
159                         udev_enumerate_unref(e);
160                         return -1;
161                 }
162
163                 udev_device_unref(device);
164         }
165         udev_enumerate_unref(e);
166
167         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
168                 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
169
170                 if (!wl_list_empty(&seat->devices_list))
171                         devices_found = 1;
172         }
173
174         if (devices_found == 0) {
175                 weston_log(
176                         "warning: no input devices on entering Weston. "
177                         "Possible causes:\n"
178                         "\t- no permissions to read /dev/input/event*\n"
179                         "\t- seats misconfigured "
180                         "(Weston backend option 'seat', "
181                         "udev device property ID_SEAT)\n");
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 static int
189 evdev_udev_handler(int fd, uint32_t mask, void *data)
190 {
191         struct udev_input *input = data;
192         struct udev_device *udev_device;
193         struct evdev_device *device, *next;
194         const char *action;
195         const char *devnode;
196         struct udev_seat *seat;
197
198         udev_device = udev_monitor_receive_device(input->udev_monitor);
199         if (!udev_device)
200                 return 1;
201
202         action = udev_device_get_action(udev_device);
203         if (!action)
204                 goto out;
205
206         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
207                 goto out;
208
209         if (!strcmp(action, "add")) {
210                 device_added(udev_device, input);
211         }
212         else if (!strcmp(action, "remove")) {
213                 devnode = udev_device_get_devnode(udev_device);
214                 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
215                         wl_list_for_each_safe(device, next, &seat->devices_list, link)
216                                 if (!strcmp(device->devnode, devnode)) {
217                                         weston_log("input device %s, %s removed\n",
218                                                         device->devname, device->devnode);
219                                         evdev_device_destroy(device);
220                                 break;
221                         }
222                 }
223         }
224
225 out:
226         udev_device_unref(udev_device);
227
228         return 0;
229 }
230
231 int
232 udev_input_enable(struct udev_input *input, struct udev *udev)
233 {
234         struct wl_event_loop *loop;
235         struct weston_compositor *c = input->compositor;
236         int fd;
237
238         input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
239         if (!input->udev_monitor) {
240                 weston_log("udev: failed to create the udev monitor\n");
241                 return -1;
242         }
243
244         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
245                         "input", NULL);
246
247         if (udev_monitor_enable_receiving(input->udev_monitor)) {
248                 weston_log("udev: failed to bind the udev monitor\n");
249                 udev_monitor_unref(input->udev_monitor);
250                 return -1;
251         }
252
253         loop = wl_display_get_event_loop(c->wl_display);
254         fd = udev_monitor_get_fd(input->udev_monitor);
255         input->udev_monitor_source =
256                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
257                                      evdev_udev_handler, input);
258         if (!input->udev_monitor_source) {
259                 udev_monitor_unref(input->udev_monitor);
260                 return -1;
261         }
262
263         if (udev_input_add_devices(input, udev) < 0)
264                 return -1;
265
266         return 0;
267 }
268
269 static void
270 udev_input_remove_devices(struct udev_input *input)
271 {
272         struct evdev_device *device, *next;
273         struct udev_seat *seat;
274
275         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
276                 wl_list_for_each_safe(device, next, &seat->devices_list, link)
277                         evdev_device_destroy(device);
278
279                 if (seat->base.keyboard)
280                         notify_keyboard_focus_out(&seat->base);
281         }
282 }
283
284 void
285 udev_input_disable(struct udev_input *input)
286 {
287         if (!input->udev_monitor)
288                 return;
289
290         udev_monitor_unref(input->udev_monitor);
291         input->udev_monitor = NULL;
292         wl_event_source_remove(input->udev_monitor_source);
293         input->udev_monitor_source = NULL;
294
295         udev_input_remove_devices(input);
296 }
297
298
299 int
300 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
301                 const char *seat_id)
302 {
303         memset(input, 0, sizeof *input);
304         input->seat_id = strdup(seat_id);
305         input->compositor = c;
306         if (udev_input_enable(input, udev) < 0)
307                 goto err;
308
309         return 0;
310
311  err:
312         free(input->seat_id);
313         return -1;
314 }
315
316 void
317 udev_input_destroy(struct udev_input *input)
318 {
319         struct udev_seat *seat, *next;
320         udev_input_disable(input);
321         wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
322                 udev_seat_destroy(seat);
323         free(input->seat_id);
324 }
325
326 static void
327 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
328 {
329         struct udev_seat *seat = (struct udev_seat *) seat_base;
330         struct evdev_device *device;
331
332         wl_list_for_each(device, &seat->devices_list, link)
333                 evdev_led_update(device, leds);
334 }
335
336 static struct udev_seat *
337 udev_seat_create(struct weston_compositor *c, const char *seat_name)
338 {
339         struct udev_seat *seat;
340
341         seat = zalloc(sizeof *seat);
342
343         if (!seat)
344                 return NULL;
345         weston_seat_init(&seat->base, c, seat_name);
346         seat->base.led_update = drm_led_update;
347
348         wl_list_init(&seat->devices_list);
349         return seat;
350 }
351
352 static void
353 udev_seat_destroy(struct udev_seat *seat)
354 {
355         weston_seat_release(&seat->base);
356         free(seat);
357 }
358
359 struct udev_seat *
360 udev_seat_get_named(struct weston_compositor *c, const char *seat_name)
361 {
362         struct udev_seat *seat;
363
364         wl_list_for_each(seat, &c->seat_list, base.link) {
365                 if (strcmp(seat->base.seat_name, seat_name) == 0)
366                         return seat;
367         }
368
369         seat = udev_seat_create(c, seat_name);
370
371         if (!seat)
372                 return NULL;
373
374         return seat;
375 }