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