downstream: Added string conversion utility functions
[profile/ivi/weston-ivi-shell.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 void
39 udev_seat_destroy(struct udev_seat *seat);
40
41 static int
42 device_added(struct udev_device *udev_device, struct udev_input *input)
43 {
44         struct weston_compositor *c;
45         struct evdev_device *device;
46         struct weston_output *output;
47         const char *devnode;
48         const char *device_seat, *seat_name, *output_name;
49         const char *calibration_values;
50         int fd;
51         struct udev_seat *seat;
52
53         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
54         if (!device_seat)
55                 device_seat = default_seat;
56
57         if (strcmp(device_seat, input->seat_id))
58                 return 0;
59
60         c = input->compositor;
61         devnode = udev_device_get_devnode(udev_device);
62
63         /* Search for matching logical seat */
64         seat_name = udev_device_get_property_value(udev_device, "WL_SEAT");
65         if (!seat_name)
66                 seat_name = default_seat_name;
67
68         seat = udev_seat_get_named(input, seat_name);
69
70         if (seat == NULL)
71                 return -1;
72
73         /* Use non-blocking mode so that we can loop on read on
74          * evdev_device_data() until all events on the fd are
75          * read.  mtdev_get() also expects this. */
76         fd = weston_launcher_open(c->launcher, devnode, O_RDWR | O_NONBLOCK);
77         if (fd < 0) {
78                 weston_log("opening input device '%s' failed.\n", devnode);
79                 return 0;
80         }
81
82         device = evdev_device_create(&seat->base, devnode, fd);
83         if (device == EVDEV_UNHANDLED_DEVICE) {
84                 weston_launcher_close(c->launcher, fd);
85                 weston_log("not using input device '%s'.\n", devnode);
86                 return 0;
87         } else if (device == NULL) {
88                 weston_launcher_close(c->launcher, fd);
89                 weston_log("failed to create input device '%s'.\n", devnode);
90                 return 0;
91         }
92
93         calibration_values =
94                 udev_device_get_property_value(udev_device,
95                                                "WL_CALIBRATION");
96
97         if (calibration_values && sscanf(calibration_values,
98                                          "%f %f %f %f %f %f",
99                                          &device->abs.calibration[0],
100                                          &device->abs.calibration[1],
101                                          &device->abs.calibration[2],
102                                          &device->abs.calibration[3],
103                                          &device->abs.calibration[4],
104                                          &device->abs.calibration[5]) == 6) {
105                 device->abs.apply_calibration = 1;
106                 weston_log ("Applying calibration: %f %f %f %f %f %f\n",
107                             device->abs.calibration[0],
108                             device->abs.calibration[1],
109                             device->abs.calibration[2],
110                             device->abs.calibration[3],
111                             device->abs.calibration[4],
112                             device->abs.calibration[5]);
113         }
114
115         wl_list_insert(seat->devices_list.prev, &device->link);
116
117         if (seat->base.output && seat->base.pointer)
118                 weston_pointer_clamp(seat->base.pointer,
119                                      &seat->base.pointer->x,
120                                      &seat->base.pointer->y);
121
122         output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
123         if (output_name) {
124                 device->output_name = strdup(output_name);
125                 wl_list_for_each(output, &c->output_list, link)
126                         if (strcmp(output->name, device->output_name) == 0)
127                                 evdev_device_set_output(device, output);
128         } else if (device->output == NULL && !wl_list_empty(&c->output_list)) {
129                 output = container_of(c->output_list.next,
130                                       struct weston_output, link);
131                 evdev_device_set_output(device, output);
132         }
133
134         if (input->enabled == 1)
135                 weston_seat_repick(&seat->base);
136
137         return 0;
138 }
139
140 static int
141 udev_input_add_devices(struct udev_input *input, struct udev *udev)
142 {
143         struct udev_enumerate *e;
144         struct udev_list_entry *entry;
145         struct udev_device *device;
146         const char *path, *sysname;
147         struct udev_seat *seat;
148         int devices_found = 0;
149
150         e = udev_enumerate_new(udev);
151         udev_enumerate_add_match_subsystem(e, "input");
152         udev_enumerate_scan_devices(e);
153         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
154                 path = udev_list_entry_get_name(entry);
155                 device = udev_device_new_from_syspath(udev, path);
156
157                 sysname = udev_device_get_sysname(device);
158                 if (strncmp("event", sysname, 5) != 0) {
159                         udev_device_unref(device);
160                         continue;
161                 }
162
163                 if (device_added(device, input) < 0) {
164                         udev_device_unref(device);
165                         udev_enumerate_unref(e);
166                         return -1;
167                 }
168
169                 udev_device_unref(device);
170         }
171         udev_enumerate_unref(e);
172
173         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
174                 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
175
176                 if (!wl_list_empty(&seat->devices_list))
177                         devices_found = 1;
178         }
179
180         if (devices_found == 0) {
181                 weston_log(
182                         "warning: no input devices on entering Weston. "
183                         "Possible causes:\n"
184                         "\t- no permissions to read /dev/input/event*\n"
185                         "\t- seats misconfigured "
186                         "(Weston backend option 'seat', "
187                         "udev device property ID_SEAT)\n");
188                 return -1;
189         }
190
191         return 0;
192 }
193
194 static int
195 evdev_udev_handler(int fd, uint32_t mask, void *data)
196 {
197         struct udev_input *input = data;
198         struct udev_device *udev_device;
199         struct evdev_device *device, *next;
200         const char *action;
201         const char *devnode;
202         struct udev_seat *seat;
203
204         udev_device = udev_monitor_receive_device(input->udev_monitor);
205         if (!udev_device)
206                 return 1;
207
208         action = udev_device_get_action(udev_device);
209         if (!action)
210                 goto out;
211
212         if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
213                 goto out;
214
215         if (!strcmp(action, "add")) {
216                 device_added(udev_device, input);
217         }
218         else if (!strcmp(action, "remove")) {
219                 devnode = udev_device_get_devnode(udev_device);
220                 wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
221                         wl_list_for_each_safe(device, next, &seat->devices_list, link)
222                                 if (!strcmp(device->devnode, devnode)) {
223                                         weston_log("input device %s, %s removed\n",
224                                                         device->devname, device->devnode);
225                                         weston_launcher_close(input->compositor->launcher,
226                                                               device->fd);
227                                         evdev_device_destroy(device);
228                                         break;
229                                 }
230                 }
231         }
232
233 out:
234         udev_device_unref(udev_device);
235
236         return 0;
237 }
238
239 int
240 udev_input_enable(struct udev_input *input)
241 {
242         struct wl_event_loop *loop;
243         struct weston_compositor *c = input->compositor;
244         int fd;
245
246         input->udev_monitor = udev_monitor_new_from_netlink(input->udev, "udev");
247         if (!input->udev_monitor) {
248                 weston_log("udev: failed to create the udev monitor\n");
249                 return -1;
250         }
251
252         udev_monitor_filter_add_match_subsystem_devtype(input->udev_monitor,
253                         "input", NULL);
254
255         if (udev_monitor_enable_receiving(input->udev_monitor)) {
256                 weston_log("udev: failed to bind the udev monitor\n");
257                 udev_monitor_unref(input->udev_monitor);
258                 return -1;
259         }
260
261         loop = wl_display_get_event_loop(c->wl_display);
262         fd = udev_monitor_get_fd(input->udev_monitor);
263         input->udev_monitor_source =
264                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
265                                      evdev_udev_handler, input);
266         if (!input->udev_monitor_source) {
267                 udev_monitor_unref(input->udev_monitor);
268                 return -1;
269         }
270
271         if (udev_input_add_devices(input, input->udev) < 0)
272                 return -1;
273
274         input->enabled = 1;
275
276         return 0;
277 }
278
279 static void
280 udev_input_remove_devices(struct udev_input *input)
281 {
282         struct evdev_device *device, *next;
283         struct udev_seat *seat;
284
285         wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
286                 wl_list_for_each_safe(device, next, &seat->devices_list, link) {
287                         weston_launcher_close(input->compositor->launcher,
288                                               device->fd);
289                         evdev_device_destroy(device);
290                 }
291
292                 if (seat->base.keyboard)
293                         notify_keyboard_focus_out(&seat->base);
294         }
295 }
296
297 void
298 udev_input_disable(struct udev_input *input)
299 {
300         if (!input->udev_monitor)
301                 return;
302
303         udev_monitor_unref(input->udev_monitor);
304         input->udev_monitor = NULL;
305         wl_event_source_remove(input->udev_monitor_source);
306         input->udev_monitor_source = NULL;
307
308         udev_input_remove_devices(input);
309 }
310
311
312 int
313 udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
314                 const char *seat_id)
315 {
316         memset(input, 0, sizeof *input);
317         input->seat_id = strdup(seat_id);
318         input->compositor = c;
319         input->udev = udev;
320         input->udev = udev_ref(udev);
321         if (udev_input_enable(input) < 0)
322                 goto err;
323
324         return 0;
325
326  err:
327         free(input->seat_id);
328         return -1;
329 }
330
331 void
332 udev_input_destroy(struct udev_input *input)
333 {
334         struct udev_seat *seat, *next;
335         udev_input_disable(input);
336         wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
337                 udev_seat_destroy(seat);
338         udev_unref(input->udev);
339         free(input->seat_id);
340 }
341
342 static void
343 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
344 {
345         struct udev_seat *seat = (struct udev_seat *) seat_base;
346         struct evdev_device *device;
347
348         wl_list_for_each(device, &seat->devices_list, link)
349                 evdev_led_update(device, leds);
350 }
351
352 static void
353 notify_output_create(struct wl_listener *listener, void *data)
354 {
355         struct udev_seat *seat = container_of(listener, struct udev_seat,
356                                               output_create_listener);
357         struct evdev_device *device;
358         struct weston_output *output = data;
359
360         wl_list_for_each(device, &seat->devices_list, link) {
361                 if (device->output_name &&
362                     strcmp(output->name, device->output_name) == 0) {
363                         evdev_device_set_output(device, output);
364                 }
365
366                 if (device->output_name == NULL && device->output == NULL)
367                         evdev_device_set_output(device, output);
368         }
369 }
370
371 static struct udev_seat *
372 udev_seat_create(struct udev_input *input, const char *seat_name)
373 {
374         struct weston_compositor *c = input->compositor;
375         struct udev_seat *seat;
376
377         seat = zalloc(sizeof *seat);
378         if (!seat)
379                 return NULL;
380
381         weston_seat_init(&seat->base, c, seat_name);
382         seat->base.led_update = drm_led_update;
383
384         seat->output_create_listener.notify = notify_output_create;
385         wl_signal_add(&c->output_created_signal,
386                       &seat->output_create_listener);
387
388         wl_list_init(&seat->devices_list);
389         return seat;
390 }
391
392 static void
393 udev_seat_destroy(struct udev_seat *seat)
394 {
395         weston_seat_release(&seat->base);
396         wl_list_remove(&seat->output_create_listener.link);
397         free(seat);
398 }
399
400 struct udev_seat *
401 udev_seat_get_named(struct udev_input *input, const char *seat_name)
402 {
403         struct weston_compositor *c = input->compositor;
404         struct udev_seat *seat;
405
406         wl_list_for_each(seat, &c->seat_list, base.link) {
407                 if (strcmp(seat->base.seat_name, seat_name) == 0)
408                         return seat;
409         }
410
411         seat = udev_seat_create(input, seat_name);
412
413         if (!seat)
414                 return NULL;
415
416         return seat;
417 }