input: Rename weston_device_repick() to weston_seat_repick()
[platform/upstream/weston.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 -1;
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 -1;
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         }
144
145         return 0;
146 }
147
148 static int
149 evdev_udev_handler(int fd, uint32_t mask, void *data)
150 {
151         struct udev_seat *seat = data;
152         struct udev_device *udev_device;
153         struct evdev_device *device, *next;
154         const char *action;
155         const char *devnode;
156
157         udev_device = udev_monitor_receive_device(seat->udev_monitor);
158         if (!udev_device)
159                 return 1;
160
161         action = udev_device_get_action(udev_device);
162         if (!action)
163                 goto out;
164
165         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
166                 goto out;
167
168         if (!strcmp(action, "add")) {
169                 device_added(udev_device, seat);
170         }
171         else if (!strcmp(action, "remove")) {
172                 devnode = udev_device_get_devnode(udev_device);
173                 wl_list_for_each_safe(device, next, &seat->devices_list, link)
174                         if (!strcmp(device->devnode, devnode)) {
175                                 weston_log("input device %s, %s removed\n",
176                                            device->devname, device->devnode);
177                                 evdev_device_destroy(device);
178                                 break;
179                         }
180         }
181
182 out:
183         udev_device_unref(udev_device);
184
185         return 0;
186 }
187
188 int
189 udev_seat_enable(struct udev_seat *seat, struct udev *udev)
190 {
191         struct wl_event_loop *loop;
192         struct weston_compositor *c = seat->base.compositor;
193         int fd;
194
195         seat->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
196         if (!seat->udev_monitor) {
197                 weston_log("udev: failed to create the udev monitor\n");
198                 return -1;
199         }
200
201         udev_monitor_filter_add_match_subsystem_devtype(seat->udev_monitor,
202                         "input", NULL);
203
204         if (udev_monitor_enable_receiving(seat->udev_monitor)) {
205                 weston_log("udev: failed to bind the udev monitor\n");
206                 udev_monitor_unref(seat->udev_monitor);
207                 return -1;
208         }
209
210         loop = wl_display_get_event_loop(c->wl_display);
211         fd = udev_monitor_get_fd(seat->udev_monitor);
212         seat->udev_monitor_source =
213                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
214                                      evdev_udev_handler, seat);
215         if (!seat->udev_monitor_source) {
216                 udev_monitor_unref(seat->udev_monitor);
217                 return -1;
218         }
219
220         if (udev_seat_add_devices(seat, udev) < 0)
221                 return -1;
222
223         return 0;
224 }
225
226 static void
227 udev_seat_remove_devices(struct udev_seat *seat)
228 {
229         struct evdev_device *device, *next;
230
231         wl_list_for_each_safe(device, next, &seat->devices_list, link)
232                 evdev_device_destroy(device);
233
234         if (seat->base.seat.keyboard)
235                 notify_keyboard_focus_out(&seat->base);
236 }
237
238 void
239 udev_seat_disable(struct udev_seat *seat)
240 {
241         if (!seat->udev_monitor)
242                 return;
243
244         udev_monitor_unref(seat->udev_monitor);
245         seat->udev_monitor = NULL;
246         wl_event_source_remove(seat->udev_monitor_source);
247         seat->udev_monitor_source = NULL;
248
249         udev_seat_remove_devices(seat);
250 }
251
252 static void
253 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
254 {
255         struct udev_seat *seat = (struct udev_seat *) seat_base;
256         struct evdev_device *device;
257
258         wl_list_for_each(device, &seat->devices_list, link)
259                 evdev_led_update(device, leds);
260 }
261
262 struct udev_seat *
263 udev_seat_create(struct weston_compositor *c, struct udev *udev,
264                 const char *seat_id)
265 {
266         struct udev_seat *seat;
267
268         seat = malloc(sizeof *seat);
269         if (seat == NULL)
270                 return NULL;
271
272         memset(seat, 0, sizeof *seat);
273         weston_seat_init(&seat->base, c);
274         seat->base.led_update = drm_led_update;
275
276         wl_list_init(&seat->devices_list);
277         seat->seat_id = strdup(seat_id);
278         if (udev_seat_enable(seat, udev) < 0)
279                 goto err;
280
281         return seat;
282
283  err:
284         free(seat->seat_id);
285         free(seat);
286         return NULL;
287 }
288
289 void
290 udev_seat_destroy(struct udev_seat *seat)
291 {
292         udev_seat_disable(seat);
293
294         weston_seat_release(&seat->base);
295         free(seat->seat_id);
296         free(seat);
297 }