1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  * Copyright 2017-2018 Collabora, Ltd.
5  * Copyright 2017-2018 General Electric Company
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28 
29 #include "config.h"
30 
31 #include <errno.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <linux/input.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <assert.h>
39 #include <libinput.h>
40 
41 #include <libweston/libweston.h>
42 #include "backend.h"
43 #include "libweston-internal.h"
44 #include "libinput-device.h"
45 #include "shared/helpers.h"
46 #include "shared/timespec-util.h"
47 
48 void
evdev_led_update(struct evdev_device * device,enum weston_led weston_leds)49 evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
50 {
51 	enum libinput_led leds = 0;
52 
53 	if (weston_leds & LED_NUM_LOCK)
54 		leds |= LIBINPUT_LED_NUM_LOCK;
55 	if (weston_leds & LED_CAPS_LOCK)
56 		leds |= LIBINPUT_LED_CAPS_LOCK;
57 	if (weston_leds & LED_SCROLL_LOCK)
58 		leds |= LIBINPUT_LED_SCROLL_LOCK;
59 
60 	libinput_device_led_update(device->device, leds);
61 }
62 
63 static void
handle_keyboard_key(struct libinput_device * libinput_device,struct libinput_event_keyboard * keyboard_event)64 handle_keyboard_key(struct libinput_device *libinput_device,
65 		    struct libinput_event_keyboard *keyboard_event)
66 {
67 	struct evdev_device *device =
68 		libinput_device_get_user_data(libinput_device);
69 	int key_state =
70 		libinput_event_keyboard_get_key_state(keyboard_event);
71 	int seat_key_count =
72 		libinput_event_keyboard_get_seat_key_count(keyboard_event);
73 	struct timespec time;
74 
75 	/* Ignore key events that are not seat wide state changes. */
76 	if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
77 	     seat_key_count != 1) ||
78 	    (key_state == LIBINPUT_KEY_STATE_RELEASED &&
79 	     seat_key_count != 0))
80 		return;
81 
82 	timespec_from_usec(&time,
83 			   libinput_event_keyboard_get_time_usec(keyboard_event));
84 
85 	notify_key(device->seat, &time,
86 		   libinput_event_keyboard_get_key(keyboard_event),
87 		   key_state, STATE_UPDATE_AUTOMATIC);
88 }
89 
90 static bool
handle_pointer_motion(struct libinput_device * libinput_device,struct libinput_event_pointer * pointer_event)91 handle_pointer_motion(struct libinput_device *libinput_device,
92 		      struct libinput_event_pointer *pointer_event)
93 {
94 	struct evdev_device *device =
95 		libinput_device_get_user_data(libinput_device);
96 	struct weston_pointer_motion_event event = { 0 };
97 	struct timespec time;
98 	double dx_unaccel, dy_unaccel;
99 
100 	timespec_from_usec(&time,
101 			   libinput_event_pointer_get_time_usec(pointer_event));
102 	dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
103 	dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
104 
105 	event = (struct weston_pointer_motion_event) {
106 		.mask = WESTON_POINTER_MOTION_REL |
107 			WESTON_POINTER_MOTION_REL_UNACCEL,
108 		.time = time,
109 		.dx = libinput_event_pointer_get_dx(pointer_event),
110 		.dy = libinput_event_pointer_get_dy(pointer_event),
111 		.dx_unaccel = dx_unaccel,
112 		.dy_unaccel = dy_unaccel,
113 	};
114 
115 	notify_motion(device->seat, &time, &event);
116 
117 	return true;
118 }
119 
120 static bool
handle_pointer_motion_absolute(struct libinput_device * libinput_device,struct libinput_event_pointer * pointer_event)121 handle_pointer_motion_absolute(
122 	struct libinput_device *libinput_device,
123 	struct libinput_event_pointer *pointer_event)
124 {
125 	struct evdev_device *device =
126 		libinput_device_get_user_data(libinput_device);
127 	struct weston_output *output = device->output;
128 	struct timespec time;
129 	double x, y;
130 	uint32_t width, height;
131 
132 	if (!output)
133 		return false;
134 
135 	timespec_from_usec(&time,
136 			   libinput_event_pointer_get_time_usec(pointer_event));
137 	width = device->output->current_mode->width;
138 	height = device->output->current_mode->height;
139 
140 	x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
141 							      width);
142 	y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
143 							      height);
144 
145 	weston_output_transform_coordinate(device->output, x, y, &x, &y);
146 	notify_motion_absolute(device->seat, &time, x, y);
147 
148 	return true;
149 }
150 
151 static bool
handle_pointer_button(struct libinput_device * libinput_device,struct libinput_event_pointer * pointer_event)152 handle_pointer_button(struct libinput_device *libinput_device,
153 		      struct libinput_event_pointer *pointer_event)
154 {
155 	struct evdev_device *device =
156 		libinput_device_get_user_data(libinput_device);
157 	int button_state =
158 		libinput_event_pointer_get_button_state(pointer_event);
159 	int seat_button_count =
160 		libinput_event_pointer_get_seat_button_count(pointer_event);
161 	struct timespec time;
162 
163 	/* Ignore button events that are not seat wide state changes. */
164 	if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
165 	     seat_button_count != 1) ||
166 	    (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
167 	     seat_button_count != 0))
168 		return false;
169 
170 	timespec_from_usec(&time,
171 			   libinput_event_pointer_get_time_usec(pointer_event));
172 
173 	notify_button(device->seat, &time,
174 		      libinput_event_pointer_get_button(pointer_event),
175                       button_state);
176 
177 	return true;
178 }
179 
180 static double
normalize_scroll(struct libinput_event_pointer * pointer_event,enum libinput_pointer_axis axis)181 normalize_scroll(struct libinput_event_pointer *pointer_event,
182 		 enum libinput_pointer_axis axis)
183 {
184 	enum libinput_pointer_axis_source source;
185 	double value = 0.0;
186 
187 	source = libinput_event_pointer_get_axis_source(pointer_event);
188 	/* libinput < 0.8 sent wheel click events with value 10. Since 0.8
189 	   the value is the angle of the click in degrees. To keep
190 	   backwards-compat with existing clients, we just send multiples of
191 	   the click count.
192 	 */
193 	switch (source) {
194 	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
195 		value = 10 * libinput_event_pointer_get_axis_value_discrete(
196 								   pointer_event,
197 								   axis);
198 		break;
199 	case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
200 	case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
201 		value = libinput_event_pointer_get_axis_value(pointer_event,
202 							      axis);
203 		break;
204 	default:
205 		assert(!"unhandled event source in normalize_scroll");
206 	}
207 
208 	return value;
209 }
210 
211 static int32_t
get_axis_discrete(struct libinput_event_pointer * pointer_event,enum libinput_pointer_axis axis)212 get_axis_discrete(struct libinput_event_pointer *pointer_event,
213 		  enum libinput_pointer_axis axis)
214 {
215 	enum libinput_pointer_axis_source source;
216 
217 	source = libinput_event_pointer_get_axis_source(pointer_event);
218 
219 	if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
220 		return 0;
221 
222 	return libinput_event_pointer_get_axis_value_discrete(pointer_event,
223 							      axis);
224 }
225 
226 static bool
handle_pointer_axis(struct libinput_device * libinput_device,struct libinput_event_pointer * pointer_event)227 handle_pointer_axis(struct libinput_device *libinput_device,
228 		    struct libinput_event_pointer *pointer_event)
229 {
230 	static int warned;
231 	struct evdev_device *device =
232 		libinput_device_get_user_data(libinput_device);
233 	double vert, horiz;
234 	int32_t vert_discrete, horiz_discrete;
235 	enum libinput_pointer_axis axis;
236 	struct weston_pointer_axis_event weston_event;
237 	enum libinput_pointer_axis_source source;
238 	uint32_t wl_axis_source;
239 	bool has_vert, has_horiz;
240 	struct timespec time;
241 
242 	has_vert = libinput_event_pointer_has_axis(pointer_event,
243 				   LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
244 	has_horiz = libinput_event_pointer_has_axis(pointer_event,
245 				   LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
246 
247 	if (!has_vert && !has_horiz)
248 		return false;
249 
250 	source = libinput_event_pointer_get_axis_source(pointer_event);
251 	switch (source) {
252 	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
253 		wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
254 		break;
255 	case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
256 		wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
257 		break;
258 	case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
259 		wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
260 		break;
261 	default:
262 		if (warned < 5) {
263 			weston_log("Unknown scroll source %d.\n", source);
264 			warned++;
265 		}
266 		return false;
267 	}
268 
269 	notify_axis_source(device->seat, wl_axis_source);
270 
271 	timespec_from_usec(&time,
272 			   libinput_event_pointer_get_time_usec(pointer_event));
273 
274 	if (has_vert) {
275 		axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
276 		vert_discrete = get_axis_discrete(pointer_event, axis);
277 		vert = normalize_scroll(pointer_event, axis);
278 
279 		weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
280 		weston_event.value = vert;
281 		weston_event.discrete = vert_discrete;
282 		weston_event.has_discrete = (vert_discrete != 0);
283 
284 		notify_axis(device->seat, &time, &weston_event);
285 	}
286 
287 	if (has_horiz) {
288 		axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
289 		horiz_discrete = get_axis_discrete(pointer_event, axis);
290 		horiz = normalize_scroll(pointer_event, axis);
291 
292 		weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
293 		weston_event.value = horiz;
294 		weston_event.discrete = horiz_discrete;
295 		weston_event.has_discrete = (horiz_discrete != 0);
296 
297 		notify_axis(device->seat, &time, &weston_event);
298 	}
299 
300 	return true;
301 }
302 
303 static struct weston_output *
touch_get_output(struct weston_touch_device * device)304 touch_get_output(struct weston_touch_device *device)
305 {
306 	struct evdev_device *evdev_device = device->backend_data;
307 
308 	return evdev_device->output;
309 }
310 
311 static const char *
touch_get_calibration_head_name(struct weston_touch_device * device)312 touch_get_calibration_head_name(struct weston_touch_device *device)
313 {
314 	struct evdev_device *evdev_device = device->backend_data;
315 	struct weston_output *output = evdev_device->output;
316 	struct weston_head *head;
317 
318 	if (!output)
319 		return NULL;
320 
321 	assert(output->enabled);
322 	if (evdev_device->output_name)
323 		return evdev_device->output_name;
324 
325 	/* No specific head was configured, so the association was made by
326 	 * the default rule. Just grab whatever head's name.
327 	 */
328 	wl_list_for_each(head, &output->head_list, output_link)
329 		return head->name;
330 
331 	assert(0);
332 	return NULL;
333 }
334 
335 static void
touch_get_calibration(struct weston_touch_device * device,struct weston_touch_device_matrix * cal)336 touch_get_calibration(struct weston_touch_device *device,
337 		      struct weston_touch_device_matrix *cal)
338 {
339 	struct evdev_device *evdev_device = device->backend_data;
340 
341 	libinput_device_config_calibration_get_matrix(evdev_device->device,
342 						      cal->m);
343 }
344 
345 static void
do_set_calibration(struct evdev_device * evdev_device,const struct weston_touch_device_matrix * cal)346 do_set_calibration(struct evdev_device *evdev_device,
347 		   const struct weston_touch_device_matrix *cal)
348 {
349 	enum libinput_config_status status;
350 
351 	weston_log("input device %s: applying calibration:\n",
352 		   libinput_device_get_sysname(evdev_device->device));
353 	weston_log_continue(STAMP_SPACE "  %f %f %f\n",
354 			    cal->m[0], cal->m[1], cal->m[2]);
355 	weston_log_continue(STAMP_SPACE "  %f %f %f\n",
356 			    cal->m[3], cal->m[4], cal->m[5]);
357 
358 	status = libinput_device_config_calibration_set_matrix(evdev_device->device,
359 							       cal->m);
360 	if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
361 		weston_log("Error: Failed to apply calibration.\n");
362 }
363 
364 static void
touch_set_calibration(struct weston_touch_device * device,const struct weston_touch_device_matrix * cal)365 touch_set_calibration(struct weston_touch_device *device,
366 		      const struct weston_touch_device_matrix *cal)
367 {
368 	struct evdev_device *evdev_device = device->backend_data;
369 
370 	/* Stop output hotplug from reloading the WL_CALIBRATION values.
371 	 * libinput will maintain the latest calibration for us.
372 	 */
373 	evdev_device->override_wl_calibration = true;
374 
375 	do_set_calibration(evdev_device, cal);
376 }
377 
378 static const struct weston_touch_device_ops touch_calibration_ops = {
379 	.get_output = touch_get_output,
380 	.get_calibration_head_name = touch_get_calibration_head_name,
381 	.get_calibration = touch_get_calibration,
382 	.set_calibration = touch_set_calibration
383 };
384 
385 static struct weston_touch_device *
create_touch_device(struct evdev_device * device)386 create_touch_device(struct evdev_device *device)
387 {
388 	const struct weston_touch_device_ops *ops = NULL;
389 	struct weston_touch_device *touch_device;
390 	struct udev_device *udev_device;
391 
392 	if (libinput_device_config_calibration_has_matrix(device->device))
393 		ops = &touch_calibration_ops;
394 
395 	udev_device = libinput_device_get_udev_device(device->device);
396 	if (!udev_device)
397 		return NULL;
398 
399 	touch_device = weston_touch_create_touch_device(device->seat->touch_state,
400 					udev_device_get_syspath(udev_device),
401 					device, ops);
402 
403 	udev_device_unref(udev_device);
404 
405 	if (!touch_device)
406 		return NULL;
407 
408 	weston_log("Touchscreen - %s - %s\n",
409 		   libinput_device_get_name(device->device),
410 		   touch_device->syspath);
411 
412 	return touch_device;
413 }
414 
415 static void
handle_touch_with_coords(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event,int touch_type)416 handle_touch_with_coords(struct libinput_device *libinput_device,
417 			 struct libinput_event_touch *touch_event,
418 			 int touch_type)
419 {
420 	struct evdev_device *device =
421 		libinput_device_get_user_data(libinput_device);
422 	double x;
423 	double y;
424 	struct weston_point2d_device_normalized norm;
425 	uint32_t width, height;
426 	struct timespec time;
427 	int32_t slot;
428 
429 	if (!device->output)
430 		return;
431 
432 	timespec_from_usec(&time,
433 			   libinput_event_touch_get_time_usec(touch_event));
434 	slot = libinput_event_touch_get_seat_slot(touch_event);
435 
436 	width = device->output->current_mode->width;
437 	height = device->output->current_mode->height;
438 	x =  libinput_event_touch_get_x_transformed(touch_event, width);
439 	y =  libinput_event_touch_get_y_transformed(touch_event, height);
440 
441 	weston_output_transform_coordinate(device->output,
442 					   x, y, &x, &y);
443 
444 	if (weston_touch_device_can_calibrate(device->touch_device)) {
445 		norm.x = libinput_event_touch_get_x_transformed(touch_event, 1);
446 		norm.y = libinput_event_touch_get_y_transformed(touch_event, 1);
447 		notify_touch_normalized(device->touch_device, &time, slot,
448 					x, y, &norm, touch_type);
449 	} else {
450 		notify_touch(device->touch_device, &time, slot, x, y, touch_type);
451 	}
452 }
453 
454 static void
handle_touch_down(struct libinput_device * device,struct libinput_event_touch * touch_event)455 handle_touch_down(struct libinput_device *device,
456 		  struct libinput_event_touch *touch_event)
457 {
458 	handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
459 }
460 
461 static void
handle_touch_motion(struct libinput_device * device,struct libinput_event_touch * touch_event)462 handle_touch_motion(struct libinput_device *device,
463 		    struct libinput_event_touch *touch_event)
464 {
465 	handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
466 }
467 
468 static void
handle_touch_up(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event)469 handle_touch_up(struct libinput_device *libinput_device,
470 		struct libinput_event_touch *touch_event)
471 {
472 	struct evdev_device *device =
473 		libinput_device_get_user_data(libinput_device);
474 	struct timespec time;
475 	int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
476 
477 	timespec_from_usec(&time,
478 			   libinput_event_touch_get_time_usec(touch_event));
479 
480 	notify_touch(device->touch_device, &time, slot, 0, 0, WL_TOUCH_UP);
481 }
482 
483 static void
handle_touch_frame(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event)484 handle_touch_frame(struct libinput_device *libinput_device,
485 		   struct libinput_event_touch *touch_event)
486 {
487 	struct evdev_device *device =
488 		libinput_device_get_user_data(libinput_device);
489 
490 	notify_touch_frame(device->touch_device);
491 }
492 
493 int
evdev_device_process_event(struct libinput_event * event)494 evdev_device_process_event(struct libinput_event *event)
495 {
496 	struct libinput_device *libinput_device =
497 		libinput_event_get_device(event);
498 	struct evdev_device *device =
499 		libinput_device_get_user_data(libinput_device);
500 	int handled = 1;
501 	bool need_frame = false;
502 
503 	switch (libinput_event_get_type(event)) {
504 	case LIBINPUT_EVENT_KEYBOARD_KEY:
505 		handle_keyboard_key(libinput_device,
506 				    libinput_event_get_keyboard_event(event));
507 		break;
508 	case LIBINPUT_EVENT_POINTER_MOTION:
509 		need_frame = handle_pointer_motion(libinput_device,
510 				      libinput_event_get_pointer_event(event));
511 		break;
512 	case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
513 		need_frame = handle_pointer_motion_absolute(
514 				libinput_device,
515 				libinput_event_get_pointer_event(event));
516 		break;
517 	case LIBINPUT_EVENT_POINTER_BUTTON:
518 		need_frame = handle_pointer_button(libinput_device,
519 				      libinput_event_get_pointer_event(event));
520 		break;
521 	case LIBINPUT_EVENT_POINTER_AXIS:
522 		need_frame = handle_pointer_axis(
523 				 libinput_device,
524 				 libinput_event_get_pointer_event(event));
525 		break;
526 	case LIBINPUT_EVENT_TOUCH_DOWN:
527 		handle_touch_down(libinput_device,
528 				  libinput_event_get_touch_event(event));
529 		break;
530 	case LIBINPUT_EVENT_TOUCH_MOTION:
531 		handle_touch_motion(libinput_device,
532 				    libinput_event_get_touch_event(event));
533 		break;
534 	case LIBINPUT_EVENT_TOUCH_UP:
535 		handle_touch_up(libinput_device,
536 				libinput_event_get_touch_event(event));
537 		break;
538 	case LIBINPUT_EVENT_TOUCH_FRAME:
539 		handle_touch_frame(libinput_device,
540 				   libinput_event_get_touch_event(event));
541 		break;
542 	default:
543 		handled = 0;
544 		weston_log("unknown libinput event %d\n",
545 			   libinput_event_get_type(event));
546 	}
547 
548 	if (need_frame)
549 		notify_pointer_frame(device->seat);
550 
551 	return handled;
552 }
553 
554 static void
notify_output_destroy(struct wl_listener * listener,void * data)555 notify_output_destroy(struct wl_listener *listener, void *data)
556 {
557 	struct evdev_device *device =
558 		container_of(listener,
559 			     struct evdev_device, output_destroy_listener);
560 
561 	evdev_device_set_output(device, NULL);
562 }
563 
564 /**
565  * The WL_CALIBRATION property requires a pixel-specific matrix to be
566  * applied after scaling device coordinates to screen coordinates. libinput
567  * can't do that, so we need to convert the calibration to the normalized
568  * format libinput expects.
569  */
570 void
evdev_device_set_calibration(struct evdev_device * device)571 evdev_device_set_calibration(struct evdev_device *device)
572 {
573 	struct udev *udev;
574 	struct udev_device *udev_device = NULL;
575 	const char *sysname = libinput_device_get_sysname(device->device);
576 	const char *calibration_values;
577 	uint32_t width, height;
578 	struct weston_touch_device_matrix calibration;
579 
580 	if (!libinput_device_config_calibration_has_matrix(device->device))
581 		return;
582 
583 	/* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
584 	 * override it with WL_CALIBRATION. It also means we don't need an
585 	 * output to load a calibration. */
586 	if (libinput_device_config_calibration_get_default_matrix(
587 							  device->device,
588 							  calibration.m) != 0)
589 		return;
590 
591 	/* touch_set_calibration() has updated the values, do not load old
592 	 * values from WL_CALIBRATION.
593 	 */
594 	if (device->override_wl_calibration)
595 		return;
596 
597 	if (!device->output) {
598 		weston_log("input device %s has no enabled output associated "
599 			   "(%s named), skipping calibration for now.\n",
600 			   sysname, device->output_name ?: "none");
601 		return;
602 	}
603 
604 	width = device->output->width;
605 	height = device->output->height;
606 	if (width == 0 || height == 0)
607 		return;
608 
609 	udev = udev_new();
610 	if (!udev)
611 		return;
612 
613 	udev_device = udev_device_new_from_subsystem_sysname(udev,
614 							     "input",
615 							     sysname);
616 	if (!udev_device)
617 		goto out;
618 
619 	calibration_values =
620 		udev_device_get_property_value(udev_device,
621 					       "WL_CALIBRATION");
622 
623 	if (calibration_values) {
624 		weston_log("Warning: input device %s has WL_CALIBRATION property set. "
625 			   "Support for it will be removed in the future. "
626 			   "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
627 			   sysname);
628 	}
629 
630 	if (!calibration_values || sscanf(calibration_values,
631 					  "%f %f %f %f %f %f",
632 					  &calibration.m[0],
633 					  &calibration.m[1],
634 					  &calibration.m[2],
635 					  &calibration.m[3],
636 					  &calibration.m[4],
637 					  &calibration.m[5]) != 6)
638 		goto out;
639 
640 	/* normalize to a format libinput can use. There is a chance of
641 	   this being wrong if the width/height don't match the device
642 	   width/height but I'm not sure how to fix that */
643 	calibration.m[2] /= width;
644 	calibration.m[5] /= height;
645 
646 	do_set_calibration(device, &calibration);
647 
648 	weston_log_continue(STAMP_SPACE "  raw translation %f %f for output %s\n",
649 		   calibration.m[2] * width,
650 		   calibration.m[5] * height,
651 		   device->output->name);
652 
653 out:
654 	if (udev_device)
655 		udev_device_unref(udev_device);
656 	udev_unref(udev);
657 }
658 
659 void
evdev_device_set_output(struct evdev_device * device,struct weston_output * output)660 evdev_device_set_output(struct evdev_device *device,
661 			struct weston_output *output)
662 {
663 	if (device->output == output)
664 		return;
665 
666 	if (device->output_destroy_listener.notify) {
667 		wl_list_remove(&device->output_destroy_listener.link);
668 		device->output_destroy_listener.notify = NULL;
669 	}
670 
671 	if (!output) {
672 		weston_log("output for input device %s removed\n",
673 			   libinput_device_get_sysname(device->device));
674 
675 		device->output = NULL;
676 		return;
677 	}
678 
679 	weston_log("associating input device %s with output %s "
680 		   "(%s by udev)\n",
681 		   libinput_device_get_sysname(device->device),
682 		   output->name,
683 		   device->output_name ?: "none");
684 
685 	device->output = output;
686 	device->output_destroy_listener.notify = notify_output_destroy;
687 	wl_signal_add(&output->destroy_signal,
688 		      &device->output_destroy_listener);
689 	evdev_device_set_calibration(device);
690 }
691 
692 struct evdev_device *
evdev_device_create(struct libinput_device * libinput_device,struct weston_seat * seat)693 evdev_device_create(struct libinput_device *libinput_device,
694 		    struct weston_seat *seat)
695 {
696 	struct evdev_device *device;
697 
698 	device = zalloc(sizeof *device);
699 	if (device == NULL)
700 		return NULL;
701 
702 	device->seat = seat;
703 	wl_list_init(&device->link);
704 	device->device = libinput_device;
705 
706 	if (libinput_device_has_capability(libinput_device,
707 					   LIBINPUT_DEVICE_CAP_KEYBOARD)) {
708 		weston_seat_init_keyboard(seat, NULL);
709 		device->seat_caps |= EVDEV_SEAT_KEYBOARD;
710 	}
711 	if (libinput_device_has_capability(libinput_device,
712 					   LIBINPUT_DEVICE_CAP_POINTER)) {
713 		weston_seat_init_pointer(seat);
714 		device->seat_caps |= EVDEV_SEAT_POINTER;
715 	}
716 	if (libinput_device_has_capability(libinput_device,
717 					   LIBINPUT_DEVICE_CAP_TOUCH)) {
718 		weston_seat_init_touch(seat);
719 		device->seat_caps |= EVDEV_SEAT_TOUCH;
720 		device->touch_device = create_touch_device(device);
721 	}
722 
723 	libinput_device_set_user_data(libinput_device, device);
724 	libinput_device_ref(libinput_device);
725 
726 	return device;
727 }
728 
729 void
evdev_device_destroy(struct evdev_device * device)730 evdev_device_destroy(struct evdev_device *device)
731 {
732 	if (device->seat_caps & EVDEV_SEAT_POINTER)
733 		weston_seat_release_pointer(device->seat);
734 	if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
735 		weston_seat_release_keyboard(device->seat);
736 	if (device->seat_caps & EVDEV_SEAT_TOUCH) {
737 		weston_touch_device_destroy(device->touch_device);
738 		weston_seat_release_touch(device->seat);
739 	}
740 
741 	if (device->output)
742 		wl_list_remove(&device->output_destroy_listener.link);
743 	wl_list_remove(&device->link);
744 	libinput_device_unref(device->device);
745 	free(device->output_name);
746 	free(device);
747 }
748 
749 void
evdev_notify_keyboard_focus(struct weston_seat * seat,struct wl_list * evdev_devices)750 evdev_notify_keyboard_focus(struct weston_seat *seat,
751 			    struct wl_list *evdev_devices)
752 {
753 	struct wl_array keys;
754 
755 	if (seat->keyboard_device_count == 0)
756 		return;
757 
758 	wl_array_init(&keys);
759 	notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
760 	wl_array_release(&keys);
761 }
762