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