• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_l(struct evdev_device * device,enum weston_led weston_leds)49 evdev_led_update_l(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 	enum evdev_device_udev_tags deviceType = libinput_device_get_tags(libinput_device);
156 	if (deviceType & EVDEV_UDEV_TAG_JOYSTICK) {
157 		return false;
158 	}
159 	struct evdev_device *device =
160 		libinput_device_get_user_data(libinput_device);
161 	int button_state =
162 		libinput_event_pointer_get_button_state(pointer_event);
163 	int seat_button_count =
164 		libinput_event_pointer_get_seat_button_count(pointer_event);
165 	struct timespec time;
166 
167 	/* Ignore button events that are not seat wide state changes. */
168 	if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
169 	     seat_button_count != 1) ||
170 	    (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
171 	     seat_button_count != 0))
172 		return false;
173 
174 	timespec_from_usec(&time,
175 			   libinput_event_pointer_get_time_usec(pointer_event));
176 
177 	notify_button(device->seat, &time,
178 		      libinput_event_pointer_get_button(pointer_event),
179                       button_state);
180 
181 	return true;
182 }
183 
184 static double
normalize_scroll(struct libinput_event_pointer * pointer_event,enum libinput_pointer_axis axis)185 normalize_scroll(struct libinput_event_pointer *pointer_event,
186 		 enum libinput_pointer_axis axis)
187 {
188 	enum libinput_pointer_axis_source source;
189 	double value = 0.0;
190 
191 	source = libinput_event_pointer_get_axis_source(pointer_event);
192 	/* libinput < 0.8 sent wheel click events with value 10. Since 0.8
193 	   the value is the angle of the click in degrees. To keep
194 	   backwards-compat with existing clients, we just send multiples of
195 	   the click count.
196 	 */
197 	switch (source) {
198 	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
199 		value = 10 * libinput_event_pointer_get_axis_value_discrete(
200 								   pointer_event,
201 								   axis);
202 		break;
203 	case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
204 	case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
205 		value = libinput_event_pointer_get_axis_value(pointer_event,
206 							      axis);
207 		break;
208 	default:
209 		assert(!"unhandled event source in normalize_scroll");
210 	}
211 
212 	return value;
213 }
214 
215 static int32_t
get_axis_discrete(struct libinput_event_pointer * pointer_event,enum libinput_pointer_axis axis)216 get_axis_discrete(struct libinput_event_pointer *pointer_event,
217 		  enum libinput_pointer_axis axis)
218 {
219 	enum libinput_pointer_axis_source source;
220 
221 	source = libinput_event_pointer_get_axis_source(pointer_event);
222 
223 	if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
224 		return 0;
225 
226 	return libinput_event_pointer_get_axis_value_discrete(pointer_event,
227 							      axis);
228 }
229 
230 static bool
handle_pointer_axis(struct libinput_device * libinput_device,struct libinput_event_pointer * pointer_event)231 handle_pointer_axis(struct libinput_device *libinput_device,
232 		    struct libinput_event_pointer *pointer_event)
233 {
234 	static int warned;
235 	struct evdev_device *device =
236 		libinput_device_get_user_data(libinput_device);
237 	double vert, horiz;
238 	int32_t vert_discrete, horiz_discrete;
239 	enum libinput_pointer_axis axis;
240 	struct weston_pointer_axis_event weston_event;
241 	enum libinput_pointer_axis_source source;
242 	uint32_t wl_axis_source;
243 	bool has_vert, has_horiz;
244 	struct timespec time;
245 
246 	has_vert = libinput_event_pointer_has_axis(pointer_event,
247 				   LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
248 	has_horiz = libinput_event_pointer_has_axis(pointer_event,
249 				   LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
250 
251 	if (!has_vert && !has_horiz)
252 		return false;
253 
254 	source = libinput_event_pointer_get_axis_source(pointer_event);
255 	switch (source) {
256 	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
257 		wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
258 		break;
259 	case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
260 		wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
261 		break;
262 	case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
263 		wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
264 		break;
265 	default:
266 		if (warned < 5) {
267 			weston_log("Unknown scroll source %d.\n", source);
268 			warned++;
269 		}
270 		return false;
271 	}
272 
273 	notify_axis_source(device->seat, wl_axis_source);
274 
275 	timespec_from_usec(&time,
276 			   libinput_event_pointer_get_time_usec(pointer_event));
277 
278 	if (has_vert) {
279 		axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
280 		vert_discrete = get_axis_discrete(pointer_event, axis);
281 		vert = normalize_scroll(pointer_event, axis);
282 
283 		weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
284 		weston_event.value = vert;
285 		weston_event.discrete = vert_discrete;
286 		weston_event.has_discrete = (vert_discrete != 0);
287 
288 		notify_axis(device->seat, &time, &weston_event);
289 	}
290 
291 	if (has_horiz) {
292 		axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
293 		horiz_discrete = get_axis_discrete(pointer_event, axis);
294 		horiz = normalize_scroll(pointer_event, axis);
295 
296 		weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
297 		weston_event.value = horiz;
298 		weston_event.discrete = horiz_discrete;
299 		weston_event.has_discrete = (horiz_discrete != 0);
300 
301 		notify_axis(device->seat, &time, &weston_event);
302 	}
303 
304 	return true;
305 }
306 
307 static struct weston_output *
touch_get_output(struct weston_touch_device * device)308 touch_get_output(struct weston_touch_device *device)
309 {
310 	struct evdev_device *evdev_device = device->backend_data;
311 
312 	return evdev_device->output;
313 }
314 
315 static const char *
touch_get_calibration_head_name(struct weston_touch_device * device)316 touch_get_calibration_head_name(struct weston_touch_device *device)
317 {
318 	struct evdev_device *evdev_device = device->backend_data;
319 	struct weston_output *output = evdev_device->output;
320 	struct weston_head *head;
321 
322 	if (!output)
323 		return NULL;
324 
325 	assert(output->enabled);
326 	if (evdev_device->output_name)
327 		return evdev_device->output_name;
328 
329 	/* No specific head was configured, so the association was made by
330 	 * the default rule. Just grab whatever head's name.
331 	 */
332 	wl_list_for_each(head, &output->head_list, output_link)
333 		return head->name;
334 
335 	assert(0);
336 	return NULL;
337 }
338 
339 static void
touch_get_calibration(struct weston_touch_device * device,struct weston_touch_device_matrix * cal)340 touch_get_calibration(struct weston_touch_device *device,
341 		      struct weston_touch_device_matrix *cal)
342 {
343 	struct evdev_device *evdev_device = device->backend_data;
344 
345 	libinput_device_config_calibration_get_matrix(evdev_device->device,
346 						      cal->m);
347 }
348 
349 static void
do_set_calibration(struct evdev_device * evdev_device,const struct weston_touch_device_matrix * cal)350 do_set_calibration(struct evdev_device *evdev_device,
351 		   const struct weston_touch_device_matrix *cal)
352 {
353 	enum libinput_config_status status;
354 
355 	weston_log("input device %s: applying calibration:\n",
356 		   libinput_device_get_sysname(evdev_device->device));
357 	weston_log_continue(STAMP_SPACE "  %f %f %f\n",
358 			    cal->m[0], cal->m[1], cal->m[2]);
359 	weston_log_continue(STAMP_SPACE "  %f %f %f\n",
360 			    cal->m[3], cal->m[4], cal->m[5]);
361 
362 	status = libinput_device_config_calibration_set_matrix(evdev_device->device,
363 							       cal->m);
364 	if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
365 		weston_log("Error: Failed to apply calibration.\n");
366 }
367 
368 static void
touch_set_calibration(struct weston_touch_device * device,const struct weston_touch_device_matrix * cal)369 touch_set_calibration(struct weston_touch_device *device,
370 		      const struct weston_touch_device_matrix *cal)
371 {
372 	struct evdev_device *evdev_device = device->backend_data;
373 
374 	/* Stop output hotplug from reloading the WL_CALIBRATION values.
375 	 * libinput will maintain the latest calibration for us.
376 	 */
377 	evdev_device->override_wl_calibration = true;
378 
379 	do_set_calibration(evdev_device, cal);
380 }
381 
382 static const struct weston_touch_device_ops touch_calibration_ops = {
383 	.get_output = touch_get_output,
384 	.get_calibration_head_name = touch_get_calibration_head_name,
385 	.get_calibration = touch_get_calibration,
386 	.set_calibration = touch_set_calibration
387 };
388 
389 static struct weston_touch_device *
create_touch_device(struct evdev_device * device)390 create_touch_device(struct evdev_device *device)
391 {
392     const struct weston_touch_device_ops *ops = NULL;
393     struct weston_touch_device *touch_device;
394 #ifndef LIBINPUT_THIRD_HDF
395     struct udev_device *udev_device;
396 #endif
397 
398     if (libinput_device_config_calibration_has_matrix(device->device))
399         ops = &touch_calibration_ops;
400 
401 #ifndef LIBINPUT_THIRD_HDF
402     udev_device = libinput_device_get_udev_device(device->device);
403     if (!udev_device)
404         return NULL;
405 #endif
406 
407     touch_device = weston_touch_create_touch_device(device->seat->touch_state,
408 #ifndef LIBINPUT_THIRD_HDF
409                     udev_device_get_syspath(udev_device),
410 #else
411                     "hdf",
412 #endif
413                     device, ops);
414 
415 #ifndef LIBINPUT_THIRD_HDF
416     udev_device_unref(udev_device);
417 #endif
418 
419     if (!touch_device)
420         return NULL;
421 
422     weston_log("Touchscreen - %s - %s\n",
423            libinput_device_get_name(device->device),
424            touch_device->syspath);
425 
426     return touch_device;
427 }
428 
429 static void
handle_touch_with_coords(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event,int touch_type)430 handle_touch_with_coords(struct libinput_device *libinput_device,
431 			 struct libinput_event_touch *touch_event,
432 			 int touch_type)
433 {
434 	struct evdev_device *device =
435 		libinput_device_get_user_data(libinput_device);
436 	double x;
437 	double y;
438 	struct weston_point2d_device_normalized norm;
439 	uint32_t width, height;
440 	struct timespec time;
441 	int32_t slot;
442 
443 	if (!device->output)
444 		return;
445 
446 	timespec_from_usec(&time,
447 			   libinput_event_touch_get_time_usec(touch_event));
448 	slot = libinput_event_touch_get_seat_slot(touch_event);
449 
450 	width = device->output->current_mode->width;
451 	height = device->output->current_mode->height;
452 	x =  libinput_event_touch_get_x_transformed(touch_event, width);
453 	y =  libinput_event_touch_get_y_transformed(touch_event, height);
454 
455 	weston_output_transform_coordinate(device->output,
456 					   x, y, &x, &y);
457 
458 	if (weston_touch_device_can_calibrate(device->touch_device)) {
459 		norm.x = libinput_event_touch_get_x_transformed(touch_event, 1);
460 		norm.y = libinput_event_touch_get_y_transformed(touch_event, 1);
461 		notify_touch_normalized(device->touch_device, &time, slot,
462 					x, y, &norm, touch_type);
463 	} else {
464 		notify_touch(device->touch_device, &time, slot, x, y, touch_type);
465 	}
466 }
467 
468 static void
handle_touch_down(struct libinput_device * device,struct libinput_event_touch * touch_event)469 handle_touch_down(struct libinput_device *device,
470 		  struct libinput_event_touch *touch_event)
471 {
472 	handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
473 }
474 
475 static void
handle_touch_motion(struct libinput_device * device,struct libinput_event_touch * touch_event)476 handle_touch_motion(struct libinput_device *device,
477 		    struct libinput_event_touch *touch_event)
478 {
479 	handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
480 }
481 
482 static void
handle_touch_up(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event)483 handle_touch_up(struct libinput_device *libinput_device,
484 		struct libinput_event_touch *touch_event)
485 {
486 	struct evdev_device *device =
487 		libinput_device_get_user_data(libinput_device);
488 	struct timespec time;
489 	int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
490 
491 	timespec_from_usec(&time,
492 			   libinput_event_touch_get_time_usec(touch_event));
493 
494 	notify_touch(device->touch_device, &time, slot, 0, 0, WL_TOUCH_UP);
495 }
496 
497 static void
handle_touch_frame(struct libinput_device * libinput_device,struct libinput_event_touch * touch_event)498 handle_touch_frame(struct libinput_device *libinput_device,
499 		   struct libinput_event_touch *touch_event)
500 {
501 	struct evdev_device *device =
502 		libinput_device_get_user_data(libinput_device);
503 
504 	notify_touch_frame(device->touch_device);
505 }
506 
507 int
evdev_device_process_event_l(struct libinput_event * event)508 evdev_device_process_event_l(struct libinput_event *event)
509 {
510 	struct libinput_device *libinput_device =
511 		libinput_event_get_device(event);
512 	struct evdev_device *device =
513 		libinput_device_get_user_data(libinput_device);
514 	int handled = 1;
515 	bool need_frame = false;
516 
517 	switch (libinput_event_get_type(event)) {
518 	case LIBINPUT_EVENT_KEYBOARD_KEY:
519 		handle_keyboard_key(libinput_device,
520 				    libinput_event_get_keyboard_event(event));
521 		break;
522 	case LIBINPUT_EVENT_POINTER_MOTION:
523 		need_frame = handle_pointer_motion(libinput_device,
524 				      libinput_event_get_pointer_event(event));
525 		break;
526 	case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
527 		need_frame = handle_pointer_motion_absolute(
528 				libinput_device,
529 				libinput_event_get_pointer_event(event));
530 		break;
531 	case LIBINPUT_EVENT_POINTER_BUTTON:
532 		need_frame = handle_pointer_button(libinput_device,
533 				      libinput_event_get_pointer_event(event));
534 		break;
535 	case LIBINPUT_EVENT_POINTER_AXIS:
536 		need_frame = handle_pointer_axis(
537 				 libinput_device,
538 				 libinput_event_get_pointer_event(event));
539 		break;
540 	case LIBINPUT_EVENT_TOUCH_DOWN:
541 		handle_touch_down(libinput_device,
542 				  libinput_event_get_touch_event(event));
543 		break;
544 	case LIBINPUT_EVENT_TOUCH_MOTION:
545 		handle_touch_motion(libinput_device,
546 				    libinput_event_get_touch_event(event));
547 		break;
548 	case LIBINPUT_EVENT_TOUCH_UP:
549 		handle_touch_up(libinput_device,
550 				libinput_event_get_touch_event(event));
551 		break;
552 	case LIBINPUT_EVENT_TOUCH_FRAME:
553 		handle_touch_frame(libinput_device,
554 				   libinput_event_get_touch_event(event));
555 		break;
556 	default:
557 		handled = 0;
558 		weston_log("unknown libinput event %d\n",
559 			   libinput_event_get_type(event));
560 	}
561 
562 	if (need_frame)
563 		notify_pointer_frame(device->seat);
564 
565 	return handled;
566 }
567 
568 static void
notify_output_destroy(struct wl_listener * listener,void * data)569 notify_output_destroy(struct wl_listener *listener, void *data)
570 {
571 	struct evdev_device *device =
572 		container_of(listener,
573 			     struct evdev_device, output_destroy_listener);
574 
575 	evdev_device_set_output_l(device, NULL);
576 }
577 
578 /**
579  * The WL_CALIBRATION property requires a pixel-specific matrix to be
580  * applied after scaling device coordinates to screen coordinates. libinput
581  * can't do that, so we need to convert the calibration to the normalized
582  * format libinput expects.
583  */
584 void
evdev_device_set_calibration_l(struct evdev_device * device)585 evdev_device_set_calibration_l(struct evdev_device *device)
586 {
587 	struct udev *udev;
588 	struct udev_device *udev_device = NULL;
589 	const char *sysname = libinput_device_get_sysname(device->device);
590 	const char *calibration_values;
591 	uint32_t width, height;
592 	struct weston_touch_device_matrix calibration;
593 
594 	if (!libinput_device_config_calibration_has_matrix(device->device))
595 		return;
596 
597 	/* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
598 	 * override it with WL_CALIBRATION. It also means we don't need an
599 	 * output to load a calibration. */
600 	if (libinput_device_config_calibration_get_default_matrix(
601 							  device->device,
602 							  calibration.m) != 0)
603 		return;
604 
605 	/* touch_set_calibration() has updated the values, do not load old
606 	 * values from WL_CALIBRATION.
607 	 */
608 	if (device->override_wl_calibration)
609 		return;
610 
611 	if (!device->output) {
612 		weston_log("input device %s has no enabled output associated "
613 			   "(%s named), skipping calibration for now.\n",
614 			   sysname, device->output_name ?: "none");
615 		return;
616 	}
617 
618 	width = device->output->width;
619 	height = device->output->height;
620 	if (width == 0 || height == 0)
621 		return;
622 
623 	udev = udev_new();
624 	if (!udev)
625 		return;
626 
627 	udev_device = udev_device_new_from_subsystem_sysname(udev,
628 							     "input",
629 							     sysname);
630 	if (!udev_device)
631 		goto out;
632 
633 	calibration_values =
634 		udev_device_get_property_value(udev_device,
635 					       "WL_CALIBRATION");
636 
637 	if (calibration_values) {
638 		weston_log("Warning: input device %s has WL_CALIBRATION property set. "
639 			   "Support for it will be removed in the future. "
640 			   "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
641 			   sysname);
642 	}
643 
644 	if (!calibration_values || sscanf(calibration_values,
645 					  "%f %f %f %f %f %f",
646 					  &calibration.m[0],
647 					  &calibration.m[1],
648 					  &calibration.m[2],
649 					  &calibration.m[3],
650 					  &calibration.m[4],
651 					  &calibration.m[5]) != 6)
652 		goto out;
653 
654 	/* normalize to a format libinput can use. There is a chance of
655 	   this being wrong if the width/height don't match the device
656 	   width/height but I'm not sure how to fix that */
657 	calibration.m[2] /= width;
658 	calibration.m[5] /= height;
659 
660 	do_set_calibration(device, &calibration);
661 
662 	weston_log_continue(STAMP_SPACE "  raw translation %f %f for output %s\n",
663 		   calibration.m[2] * width,
664 		   calibration.m[5] * height,
665 		   device->output->name);
666 
667 out:
668 	if (udev_device)
669 		udev_device_unref(udev_device);
670 	udev_unref(udev);
671 }
672 
673 void
evdev_device_set_output_l(struct evdev_device * device,struct weston_output * output)674 evdev_device_set_output_l(struct evdev_device *device,
675 			struct weston_output *output)
676 {
677 	if (device->output == output)
678 		return;
679 
680 	if (device->output_destroy_listener.notify) {
681 		wl_list_remove(&device->output_destroy_listener.link);
682 		device->output_destroy_listener.notify = NULL;
683 	}
684 
685 	if (!output) {
686 		weston_log("output for input device %s removed\n",
687 			   libinput_device_get_sysname(device->device));
688 
689 		device->output = NULL;
690 		return;
691 	}
692 
693 	weston_log("associating input device %s with output %s "
694 		   "(%s by udev)\n",
695 		   libinput_device_get_sysname(device->device),
696 		   output->name,
697 		   device->output_name ?: "none");
698 
699 	device->output = output;
700 	device->output_destroy_listener.notify = notify_output_destroy;
701 	wl_signal_add(&output->destroy_signal,
702 		      &device->output_destroy_listener);
703 	evdev_device_set_calibration_l(device);
704 }
705 
706 struct evdev_device *
evdev_device_create_l(struct libinput_device * libinput_device,struct weston_seat * seat)707 evdev_device_create_l(struct libinput_device *libinput_device,
708 		    struct weston_seat *seat)
709 {
710 	struct evdev_device *device;
711 
712 	device = zalloc(sizeof *device);
713 	if (device == NULL)
714 		return NULL;
715 
716 	device->seat = seat;
717 	wl_list_init(&device->link);
718 	device->device = libinput_device;
719 
720 	if (libinput_device_has_capability(libinput_device,
721 					   LIBINPUT_DEVICE_CAP_KEYBOARD)) {
722 		weston_seat_init_keyboard(seat, NULL);
723 		device->seat_caps |= EVDEV_SEAT_KEYBOARD;
724 	}
725 	if (libinput_device_has_capability(libinput_device,
726 					   LIBINPUT_DEVICE_CAP_POINTER)) {
727 		weston_seat_init_pointer(seat);
728 		device->seat_caps |= EVDEV_SEAT_POINTER;
729 	}
730 	if (libinput_device_has_capability(libinput_device,
731 					   LIBINPUT_DEVICE_CAP_TOUCH)) {
732 		weston_seat_init_touch(seat);
733 		device->seat_caps |= EVDEV_SEAT_TOUCH;
734 		device->touch_device = create_touch_device(device);
735 	}
736 
737 	libinput_device_set_user_data(libinput_device, device);
738 	libinput_device_ref(libinput_device);
739 
740 	return device;
741 }
742 
743 void
evdev_device_destroy_l(struct evdev_device * device)744 evdev_device_destroy_l(struct evdev_device *device)
745 {
746 	if (device->seat_caps & EVDEV_SEAT_POINTER)
747 		weston_seat_release_pointer(device->seat);
748 	if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
749 		weston_seat_release_keyboard(device->seat);
750 	if (device->seat_caps & EVDEV_SEAT_TOUCH) {
751 		weston_touch_device_destroy(device->touch_device);
752 		weston_seat_release_touch(device->seat);
753 	}
754 
755 	if (device->output)
756 		wl_list_remove(&device->output_destroy_listener.link);
757 	wl_list_remove(&device->link);
758 	libinput_device_unref(device->device);
759 	free(device->output_name);
760 	free(device);
761 }
762 
763 void
evdev_notify_keyboard_focus_l(struct weston_seat * seat,struct wl_list * evdev_devices)764 evdev_notify_keyboard_focus_l(struct weston_seat *seat,
765 			    struct wl_list *evdev_devices)
766 {
767 	struct wl_array keys;
768 
769 	if (seat->keyboard_device_count == 0)
770 		return;
771 
772 	wl_array_init(&keys);
773 	notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
774 	wl_array_release(&keys);
775 }
776