blob: 782f6569f719d9642bf2b58a23d37f81adf0acf2 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Jonas Ådahle0de3c22014-03-12 22:08:42 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Jonas Ådahle0de3c22014-03-12 22:08:42 +010025 */
26
27#include "config.h"
28
29#include <errno.h>
30#include <stdlib.h>
31#include <string.h>
32#include <linux/input.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <mtdev.h>
36#include <assert.h>
37#include <libinput.h>
38
39#include "compositor.h"
40#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010042
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043void
44evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
45{
46 enum libinput_led leds = 0;
47
48 if (weston_leds & LED_NUM_LOCK)
49 leds |= LIBINPUT_LED_NUM_LOCK;
50 if (weston_leds & LED_CAPS_LOCK)
51 leds |= LIBINPUT_LED_CAPS_LOCK;
52 if (weston_leds & LED_SCROLL_LOCK)
53 leds |= LIBINPUT_LED_SCROLL_LOCK;
54
55 libinput_device_led_update(device->device, leds);
56}
57
58static void
59handle_keyboard_key(struct libinput_device *libinput_device,
60 struct libinput_event_keyboard *keyboard_event)
61{
62 struct evdev_device *device =
63 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080064 int key_state =
65 libinput_event_keyboard_get_key_state(keyboard_event);
66 int seat_key_count =
67 libinput_event_keyboard_get_seat_key_count(keyboard_event);
68
69 /* Ignore key events that are not seat wide state changes. */
70 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
71 seat_key_count != 1) ||
72 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
73 seat_key_count != 0))
74 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010075
76 notify_key(device->seat,
77 libinput_event_keyboard_get_time(keyboard_event),
78 libinput_event_keyboard_get_key(keyboard_event),
Chris Michael7e7f7932016-02-22 08:47:24 -050079 key_state, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010080}
81
Peter Hutterer87743e92016-01-18 16:38:22 +100082static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +010083handle_pointer_motion(struct libinput_device *libinput_device,
84 struct libinput_event_pointer *pointer_event)
85{
86 struct evdev_device *device =
87 libinput_device_get_user_data(libinput_device);
Jonas Ådahld2510102014-10-05 21:39:14 +020088 struct weston_pointer_motion_event event = { 0 };
Jonas Ådahlde1ed2e2015-07-29 14:18:02 +080089 uint64_t time_usec =
90 libinput_event_pointer_get_time_usec(pointer_event);
Jonas Ådahl3845b322014-10-21 23:51:29 +020091 double dx_unaccel, dy_unaccel;
92
93 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
94 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010095
Jonas Ådahld2510102014-10-05 21:39:14 +020096 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +020097 .mask = WESTON_POINTER_MOTION_REL |
98 WESTON_POINTER_MOTION_REL_UNACCEL,
Jonas Ådahlde1ed2e2015-07-29 14:18:02 +080099 .time_usec = time_usec,
Jonas Ådahld2510102014-10-05 21:39:14 +0200100 .dx = libinput_event_pointer_get_dx(pointer_event),
101 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200102 .dx_unaccel = dx_unaccel,
103 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200104 };
105
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100106 notify_motion(device->seat,
107 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahld2510102014-10-05 21:39:14 +0200108 &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000109
110 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100111}
112
Peter Hutterer87743e92016-01-18 16:38:22 +1000113static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100114handle_pointer_motion_absolute(
115 struct libinput_device *libinput_device,
116 struct libinput_event_pointer *pointer_event)
117{
118 struct evdev_device *device =
119 libinput_device_get_user_data(libinput_device);
120 struct weston_output *output = device->output;
121 uint32_t time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200122 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100123 uint32_t width, height;
124
125 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000126 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100127
128 time = libinput_event_pointer_get_time(pointer_event);
129 width = device->output->current_mode->width;
130 height = device->output->current_mode->height;
131
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200132 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
133 width);
134 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
135 height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100136
137 weston_output_transform_coordinate(device->output, x, y, &x, &y);
138 notify_motion_absolute(device->seat, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000139
140 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100141}
142
Peter Hutterer87743e92016-01-18 16:38:22 +1000143static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100144handle_pointer_button(struct libinput_device *libinput_device,
145 struct libinput_event_pointer *pointer_event)
146{
147 struct evdev_device *device =
148 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800149 int button_state =
150 libinput_event_pointer_get_button_state(pointer_event);
151 int seat_button_count =
152 libinput_event_pointer_get_seat_button_count(pointer_event);
153
154 /* Ignore button events that are not seat wide state changes. */
155 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
156 seat_button_count != 1) ||
157 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
158 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000159 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100160
161 notify_button(device->seat,
162 libinput_event_pointer_get_time(pointer_event),
163 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500164 button_state);
165
Peter Hutterer87743e92016-01-18 16:38:22 +1000166 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100167}
168
Peter Hutterer5dddd412015-01-15 13:14:43 +1000169static double
170normalize_scroll(struct libinput_event_pointer *pointer_event,
171 enum libinput_pointer_axis axis)
172{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000173 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000174 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000175
176 source = libinput_event_pointer_get_axis_source(pointer_event);
177 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
178 the value is the angle of the click in degrees. To keep
179 backwards-compat with existing clients, we just send multiples of
180 the click count.
181 */
182 switch (source) {
183 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
184 value = 10 * libinput_event_pointer_get_axis_value_discrete(
185 pointer_event,
186 axis);
187 break;
188 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
189 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
190 value = libinput_event_pointer_get_axis_value(pointer_event,
191 axis);
192 break;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000193 }
194
195 return value;
196}
197
Peter Hutterer87743e92016-01-18 16:38:22 +1000198static int32_t
199get_axis_discrete(struct libinput_event_pointer *pointer_event,
200 enum libinput_pointer_axis axis)
201{
202 enum libinput_pointer_axis_source source;
203
204 source = libinput_event_pointer_get_axis_source(pointer_event);
205
206 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
207 return 0;
208
209 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
210 axis);
211}
212
213static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100214handle_pointer_axis(struct libinput_device *libinput_device,
215 struct libinput_event_pointer *pointer_event)
216{
Peter Hutterer87743e92016-01-18 16:38:22 +1000217 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100218 struct evdev_device *device =
219 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000220 double vert, horiz;
221 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000222 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000223 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000224 enum libinput_pointer_axis_source source;
225 uint32_t wl_axis_source;
226 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100227
Peter Hutterer87743e92016-01-18 16:38:22 +1000228 has_vert = libinput_event_pointer_has_axis(pointer_event,
229 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
230 has_horiz = libinput_event_pointer_has_axis(pointer_event,
231 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
232
233 if (!has_vert && !has_horiz)
234 return false;
235
236 source = libinput_event_pointer_get_axis_source(pointer_event);
237 switch (source) {
238 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
239 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
240 break;
241 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
242 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
243 break;
244 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
245 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
246 break;
247 default:
248 if (warned < 5) {
249 weston_log("Unknown scroll source %d.\n", source);
250 warned++;
251 }
252 return false;
253 }
254
255 notify_axis_source(device->seat, wl_axis_source);
256
257 if (has_vert) {
258 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
259 vert_discrete = get_axis_discrete(pointer_event, axis);
260 vert = normalize_scroll(pointer_event, axis);
261
Peter Hutterer89b6a492016-01-18 15:58:17 +1000262 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200263 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000264 weston_event.discrete = vert_discrete;
265 weston_event.has_discrete = (vert_discrete != 0);
266
Peter Huttererc54f23d2015-01-13 11:55:37 +1000267 notify_axis(device->seat,
268 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000269 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000270 }
271
Peter Hutterer87743e92016-01-18 16:38:22 +1000272 if (has_horiz) {
273 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
274 horiz_discrete = get_axis_discrete(pointer_event, axis);
275 horiz = normalize_scroll(pointer_event, axis);
276
Peter Hutterer89b6a492016-01-18 15:58:17 +1000277 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200278 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000279 weston_event.discrete = horiz_discrete;
280 weston_event.has_discrete = (horiz_discrete != 0);
281
Peter Huttererc54f23d2015-01-13 11:55:37 +1000282 notify_axis(device->seat,
283 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000284 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000285 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000286
287 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100288}
289
290static void
291handle_touch_with_coords(struct libinput_device *libinput_device,
292 struct libinput_event_touch *touch_event,
293 int touch_type)
294{
295 struct evdev_device *device =
296 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200297 double x;
298 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100299 uint32_t width, height;
300 uint32_t time;
301 int32_t slot;
302
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300303 if (!device->output)
304 return;
305
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100306 time = libinput_event_touch_get_time(touch_event);
307 slot = libinput_event_touch_get_seat_slot(touch_event);
308
309 width = device->output->current_mode->width;
310 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200311 x = libinput_event_touch_get_x_transformed(touch_event, width);
312 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100313
314 weston_output_transform_coordinate(device->output,
315 x, y, &x, &y);
316
317 notify_touch(device->seat, time, slot, x, y, touch_type);
318}
319
320static void
321handle_touch_down(struct libinput_device *device,
322 struct libinput_event_touch *touch_event)
323{
324 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
325}
326
327static void
328handle_touch_motion(struct libinput_device *device,
329 struct libinput_event_touch *touch_event)
330{
331 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
332}
333
334static void
335handle_touch_up(struct libinput_device *libinput_device,
336 struct libinput_event_touch *touch_event)
337{
338 struct evdev_device *device =
339 libinput_device_get_user_data(libinput_device);
340 uint32_t time = libinput_event_touch_get_time(touch_event);
341 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
342
343 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
344}
345
Jonas Ådahl1679f232014-04-12 09:39:51 +0200346static void
347handle_touch_frame(struct libinput_device *libinput_device,
348 struct libinput_event_touch *touch_event)
349{
350 struct evdev_device *device =
351 libinput_device_get_user_data(libinput_device);
352 struct weston_seat *seat = device->seat;
353
354 notify_touch_frame(seat);
355}
356
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100357int
358evdev_device_process_event(struct libinput_event *event)
359{
360 struct libinput_device *libinput_device =
361 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000362 struct evdev_device *device =
363 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100364 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000365 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100366
367 switch (libinput_event_get_type(event)) {
368 case LIBINPUT_EVENT_KEYBOARD_KEY:
369 handle_keyboard_key(libinput_device,
370 libinput_event_get_keyboard_event(event));
371 break;
372 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000373 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100374 libinput_event_get_pointer_event(event));
375 break;
376 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000377 need_frame = handle_pointer_motion_absolute(
378 libinput_device,
379 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100380 break;
381 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000382 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100383 libinput_event_get_pointer_event(event));
384 break;
385 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000386 need_frame = handle_pointer_axis(
387 libinput_device,
388 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100389 break;
390 case LIBINPUT_EVENT_TOUCH_DOWN:
391 handle_touch_down(libinput_device,
392 libinput_event_get_touch_event(event));
393 break;
394 case LIBINPUT_EVENT_TOUCH_MOTION:
395 handle_touch_motion(libinput_device,
396 libinput_event_get_touch_event(event));
397 break;
398 case LIBINPUT_EVENT_TOUCH_UP:
399 handle_touch_up(libinput_device,
400 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700401 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200402 case LIBINPUT_EVENT_TOUCH_FRAME:
403 handle_touch_frame(libinput_device,
404 libinput_event_get_touch_event(event));
405 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100406 default:
407 handled = 0;
408 weston_log("unknown libinput event %d\n",
409 libinput_event_get_type(event));
410 }
411
Peter Hutterer87743e92016-01-18 16:38:22 +1000412 if (need_frame)
413 notify_pointer_frame(device->seat);
414
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100415 return handled;
416}
417
418static void
419notify_output_destroy(struct wl_listener *listener, void *data)
420{
421 struct evdev_device *device =
422 container_of(listener,
423 struct evdev_device, output_destroy_listener);
424 struct weston_compositor *c = device->seat->compositor;
425 struct weston_output *output;
426
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300427 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100428 output = container_of(c->output_list.next,
429 struct weston_output, link);
430 evdev_device_set_output(device, output);
431 } else {
432 device->output = NULL;
433 }
434}
435
Peter Hutterer3fbba492014-09-09 13:02:25 +1000436/**
437 * The WL_CALIBRATION property requires a pixel-specific matrix to be
438 * applied after scaling device coordinates to screen coordinates. libinput
439 * can't do that, so we need to convert the calibration to the normalized
440 * format libinput expects.
441 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300442void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000443evdev_device_set_calibration(struct evdev_device *device)
444{
445 struct udev *udev;
446 struct udev_device *udev_device = NULL;
447 const char *sysname = libinput_device_get_sysname(device->device);
448 const char *calibration_values;
449 uint32_t width, height;
450 float calibration[6];
451 enum libinput_config_status status;
452
453 if (!device->output)
454 return;
455
456 width = device->output->width;
457 height = device->output->height;
458 if (width == 0 || height == 0)
459 return;
460
461 /* If libinput has a pre-set calibration matrix, don't override it */
462 if (!libinput_device_config_calibration_has_matrix(device->device) ||
463 libinput_device_config_calibration_get_default_matrix(
464 device->device,
465 calibration) != 0)
466 return;
467
468 udev = udev_new();
469 if (!udev)
470 return;
471
472 udev_device = udev_device_new_from_subsystem_sysname(udev,
473 "input",
474 sysname);
475 if (!udev_device)
476 goto out;
477
478 calibration_values =
479 udev_device_get_property_value(udev_device,
480 "WL_CALIBRATION");
481
482 if (!calibration_values || sscanf(calibration_values,
483 "%f %f %f %f %f %f",
484 &calibration[0],
485 &calibration[1],
486 &calibration[2],
487 &calibration[3],
488 &calibration[4],
489 &calibration[5]) != 6)
490 goto out;
491
492 weston_log("Applying calibration: %f %f %f %f %f %f "
493 "(normalized %f %f)\n",
494 calibration[0],
495 calibration[1],
496 calibration[2],
497 calibration[3],
498 calibration[4],
499 calibration[5],
500 calibration[2] / width,
501 calibration[5] / height);
502
503 /* normalize to a format libinput can use. There is a chance of
504 this being wrong if the width/height don't match the device
505 width/height but I'm not sure how to fix that */
506 calibration[2] /= width;
507 calibration[5] /= height;
508
509 status = libinput_device_config_calibration_set_matrix(device->device,
510 calibration);
511 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
512 weston_log("Failed to apply calibration.\n");
513
514out:
515 if (udev_device)
516 udev_device_unref(udev_device);
517 udev_unref(udev);
518}
519
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100520void
521evdev_device_set_output(struct evdev_device *device,
522 struct weston_output *output)
523{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700524 if (device->output_destroy_listener.notify) {
525 wl_list_remove(&device->output_destroy_listener.link);
526 device->output_destroy_listener.notify = NULL;
527 }
528
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100529 device->output = output;
530 device->output_destroy_listener.notify = notify_output_destroy;
531 wl_signal_add(&output->destroy_signal,
532 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000533 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100534}
535
536struct evdev_device *
537evdev_device_create(struct libinput_device *libinput_device,
538 struct weston_seat *seat)
539{
540 struct evdev_device *device;
541
542 device = zalloc(sizeof *device);
543 if (device == NULL)
544 return NULL;
545
546 device->seat = seat;
547 wl_list_init(&device->link);
548 device->device = libinput_device;
549
550 if (libinput_device_has_capability(libinput_device,
551 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
552 weston_seat_init_keyboard(seat, NULL);
553 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
554 }
555 if (libinput_device_has_capability(libinput_device,
556 LIBINPUT_DEVICE_CAP_POINTER)) {
557 weston_seat_init_pointer(seat);
558 device->seat_caps |= EVDEV_SEAT_POINTER;
559 }
560 if (libinput_device_has_capability(libinput_device,
561 LIBINPUT_DEVICE_CAP_TOUCH)) {
562 weston_seat_init_touch(seat);
563 device->seat_caps |= EVDEV_SEAT_TOUCH;
564 }
565
566 libinput_device_set_user_data(libinput_device, device);
567 libinput_device_ref(libinput_device);
568
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100569 return device;
570}
571
572void
573evdev_device_destroy(struct evdev_device *device)
574{
575 if (device->seat_caps & EVDEV_SEAT_POINTER)
576 weston_seat_release_pointer(device->seat);
577 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
578 weston_seat_release_keyboard(device->seat);
579 if (device->seat_caps & EVDEV_SEAT_TOUCH)
580 weston_seat_release_touch(device->seat);
581
582 if (device->output)
583 wl_list_remove(&device->output_destroy_listener.link);
584 wl_list_remove(&device->link);
585 libinput_device_unref(device->device);
586 free(device->devnode);
587 free(device->output_name);
588 free(device);
589}
590
591void
592evdev_notify_keyboard_focus(struct weston_seat *seat,
593 struct wl_list *evdev_devices)
594{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100595 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100596
Derek Foremand621df22014-11-19 11:04:12 -0600597 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100598 return;
599
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100600 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100601 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100602 wl_array_release(&keys);
603}