blob: f97afcf8db33f5bd35e3f52905ba3a83352b36ef [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010031#include <stdlib.h>
32#include <string.h>
33#include <linux/input.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <mtdev.h>
37#include <assert.h>
38#include <libinput.h>
39
40#include "compositor.h"
41#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070042#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043
Jonas Ådahle0de3c22014-03-12 22:08:42 +010044void
45evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
46{
47 enum libinput_led leds = 0;
48
49 if (weston_leds & LED_NUM_LOCK)
50 leds |= LIBINPUT_LED_NUM_LOCK;
51 if (weston_leds & LED_CAPS_LOCK)
52 leds |= LIBINPUT_LED_CAPS_LOCK;
53 if (weston_leds & LED_SCROLL_LOCK)
54 leds |= LIBINPUT_LED_SCROLL_LOCK;
55
56 libinput_device_led_update(device->device, leds);
57}
58
59static void
60handle_keyboard_key(struct libinput_device *libinput_device,
61 struct libinput_event_keyboard *keyboard_event)
62{
63 struct evdev_device *device =
64 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080065 int key_state =
66 libinput_event_keyboard_get_key_state(keyboard_event);
67 int seat_key_count =
68 libinput_event_keyboard_get_seat_key_count(keyboard_event);
69
70 /* Ignore key events that are not seat wide state changes. */
71 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
72 seat_key_count != 1) ||
73 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
74 seat_key_count != 0))
75 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010076
77 notify_key(device->seat,
78 libinput_event_keyboard_get_time(keyboard_event),
79 libinput_event_keyboard_get_key(keyboard_event),
Chris Michael7e7f7932016-02-22 08:47:24 -050080 key_state, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010081}
82
Peter Hutterer87743e92016-01-18 16:38:22 +100083static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +010084handle_pointer_motion(struct libinput_device *libinput_device,
85 struct libinput_event_pointer *pointer_event)
86{
87 struct evdev_device *device =
88 libinput_device_get_user_data(libinput_device);
Jonas Ådahld2510102014-10-05 21:39:14 +020089 struct weston_pointer_motion_event event = { 0 };
Jonas Ådahlde1ed2e2015-07-29 14:18:02 +080090 uint64_t time_usec =
91 libinput_event_pointer_get_time_usec(pointer_event);
Jonas Ådahl3845b322014-10-21 23:51:29 +020092 double dx_unaccel, dy_unaccel;
93
94 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
95 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010096
Jonas Ådahld2510102014-10-05 21:39:14 +020097 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +020098 .mask = WESTON_POINTER_MOTION_REL |
99 WESTON_POINTER_MOTION_REL_UNACCEL,
Jonas Ådahlde1ed2e2015-07-29 14:18:02 +0800100 .time_usec = time_usec,
Jonas Ådahld2510102014-10-05 21:39:14 +0200101 .dx = libinput_event_pointer_get_dx(pointer_event),
102 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200103 .dx_unaccel = dx_unaccel,
104 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200105 };
106
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100107 notify_motion(device->seat,
108 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahld2510102014-10-05 21:39:14 +0200109 &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000110
111 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100112}
113
Peter Hutterer87743e92016-01-18 16:38:22 +1000114static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100115handle_pointer_motion_absolute(
116 struct libinput_device *libinput_device,
117 struct libinput_event_pointer *pointer_event)
118{
119 struct evdev_device *device =
120 libinput_device_get_user_data(libinput_device);
121 struct weston_output *output = device->output;
122 uint32_t time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200123 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100124 uint32_t width, height;
125
126 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000127 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100128
129 time = libinput_event_pointer_get_time(pointer_event);
130 width = device->output->current_mode->width;
131 height = device->output->current_mode->height;
132
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200133 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
134 width);
135 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
136 height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100137
138 weston_output_transform_coordinate(device->output, x, y, &x, &y);
139 notify_motion_absolute(device->seat, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000140
141 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100142}
143
Peter Hutterer87743e92016-01-18 16:38:22 +1000144static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100145handle_pointer_button(struct libinput_device *libinput_device,
146 struct libinput_event_pointer *pointer_event)
147{
148 struct evdev_device *device =
149 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800150 int button_state =
151 libinput_event_pointer_get_button_state(pointer_event);
152 int seat_button_count =
153 libinput_event_pointer_get_seat_button_count(pointer_event);
154
155 /* Ignore button events that are not seat wide state changes. */
156 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
157 seat_button_count != 1) ||
158 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
159 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000160 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100161
162 notify_button(device->seat,
163 libinput_event_pointer_get_time(pointer_event),
164 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500165 button_state);
166
Peter Hutterer87743e92016-01-18 16:38:22 +1000167 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100168}
169
Peter Hutterer5dddd412015-01-15 13:14:43 +1000170static double
171normalize_scroll(struct libinput_event_pointer *pointer_event,
172 enum libinput_pointer_axis axis)
173{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000174 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000175 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000176
177 source = libinput_event_pointer_get_axis_source(pointer_event);
178 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
179 the value is the angle of the click in degrees. To keep
180 backwards-compat with existing clients, we just send multiples of
181 the click count.
182 */
183 switch (source) {
184 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
185 value = 10 * libinput_event_pointer_get_axis_value_discrete(
186 pointer_event,
187 axis);
188 break;
189 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
190 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
191 value = libinput_event_pointer_get_axis_value(pointer_event,
192 axis);
193 break;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000194 }
195
196 return value;
197}
198
Peter Hutterer87743e92016-01-18 16:38:22 +1000199static int32_t
200get_axis_discrete(struct libinput_event_pointer *pointer_event,
201 enum libinput_pointer_axis axis)
202{
203 enum libinput_pointer_axis_source source;
204
205 source = libinput_event_pointer_get_axis_source(pointer_event);
206
207 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
208 return 0;
209
210 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
211 axis);
212}
213
214static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100215handle_pointer_axis(struct libinput_device *libinput_device,
216 struct libinput_event_pointer *pointer_event)
217{
Peter Hutterer87743e92016-01-18 16:38:22 +1000218 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100219 struct evdev_device *device =
220 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000221 double vert, horiz;
222 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000223 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000224 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000225 enum libinput_pointer_axis_source source;
226 uint32_t wl_axis_source;
227 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100228
Peter Hutterer87743e92016-01-18 16:38:22 +1000229 has_vert = libinput_event_pointer_has_axis(pointer_event,
230 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
231 has_horiz = libinput_event_pointer_has_axis(pointer_event,
232 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
233
234 if (!has_vert && !has_horiz)
235 return false;
236
237 source = libinput_event_pointer_get_axis_source(pointer_event);
238 switch (source) {
239 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
240 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
241 break;
242 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
243 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
244 break;
245 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
246 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
247 break;
248 default:
249 if (warned < 5) {
250 weston_log("Unknown scroll source %d.\n", source);
251 warned++;
252 }
253 return false;
254 }
255
256 notify_axis_source(device->seat, wl_axis_source);
257
258 if (has_vert) {
259 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
260 vert_discrete = get_axis_discrete(pointer_event, axis);
261 vert = normalize_scroll(pointer_event, axis);
262
Peter Hutterer89b6a492016-01-18 15:58:17 +1000263 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200264 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000265 weston_event.discrete = vert_discrete;
266 weston_event.has_discrete = (vert_discrete != 0);
267
Peter Huttererc54f23d2015-01-13 11:55:37 +1000268 notify_axis(device->seat,
269 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000270 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000271 }
272
Peter Hutterer87743e92016-01-18 16:38:22 +1000273 if (has_horiz) {
274 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
275 horiz_discrete = get_axis_discrete(pointer_event, axis);
276 horiz = normalize_scroll(pointer_event, axis);
277
Peter Hutterer89b6a492016-01-18 15:58:17 +1000278 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200279 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000280 weston_event.discrete = horiz_discrete;
281 weston_event.has_discrete = (horiz_discrete != 0);
282
Peter Huttererc54f23d2015-01-13 11:55:37 +1000283 notify_axis(device->seat,
284 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000285 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000286 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000287
288 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100289}
290
291static void
292handle_touch_with_coords(struct libinput_device *libinput_device,
293 struct libinput_event_touch *touch_event,
294 int touch_type)
295{
296 struct evdev_device *device =
297 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200298 double x;
299 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100300 uint32_t width, height;
301 uint32_t time;
302 int32_t slot;
303
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300304 if (!device->output)
305 return;
306
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100307 time = libinput_event_touch_get_time(touch_event);
308 slot = libinput_event_touch_get_seat_slot(touch_event);
309
310 width = device->output->current_mode->width;
311 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200312 x = libinput_event_touch_get_x_transformed(touch_event, width);
313 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100314
315 weston_output_transform_coordinate(device->output,
316 x, y, &x, &y);
317
318 notify_touch(device->seat, time, slot, x, y, touch_type);
319}
320
321static void
322handle_touch_down(struct libinput_device *device,
323 struct libinput_event_touch *touch_event)
324{
325 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
326}
327
328static void
329handle_touch_motion(struct libinput_device *device,
330 struct libinput_event_touch *touch_event)
331{
332 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
333}
334
335static void
336handle_touch_up(struct libinput_device *libinput_device,
337 struct libinput_event_touch *touch_event)
338{
339 struct evdev_device *device =
340 libinput_device_get_user_data(libinput_device);
341 uint32_t time = libinput_event_touch_get_time(touch_event);
342 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
343
344 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
345}
346
Jonas Ådahl1679f232014-04-12 09:39:51 +0200347static void
348handle_touch_frame(struct libinput_device *libinput_device,
349 struct libinput_event_touch *touch_event)
350{
351 struct evdev_device *device =
352 libinput_device_get_user_data(libinput_device);
353 struct weston_seat *seat = device->seat;
354
355 notify_touch_frame(seat);
356}
357
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100358int
359evdev_device_process_event(struct libinput_event *event)
360{
361 struct libinput_device *libinput_device =
362 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000363 struct evdev_device *device =
364 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100365 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000366 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100367
368 switch (libinput_event_get_type(event)) {
369 case LIBINPUT_EVENT_KEYBOARD_KEY:
370 handle_keyboard_key(libinput_device,
371 libinput_event_get_keyboard_event(event));
372 break;
373 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000374 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100375 libinput_event_get_pointer_event(event));
376 break;
377 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000378 need_frame = handle_pointer_motion_absolute(
379 libinput_device,
380 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100381 break;
382 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000383 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100384 libinput_event_get_pointer_event(event));
385 break;
386 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000387 need_frame = handle_pointer_axis(
388 libinput_device,
389 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100390 break;
391 case LIBINPUT_EVENT_TOUCH_DOWN:
392 handle_touch_down(libinput_device,
393 libinput_event_get_touch_event(event));
394 break;
395 case LIBINPUT_EVENT_TOUCH_MOTION:
396 handle_touch_motion(libinput_device,
397 libinput_event_get_touch_event(event));
398 break;
399 case LIBINPUT_EVENT_TOUCH_UP:
400 handle_touch_up(libinput_device,
401 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700402 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200403 case LIBINPUT_EVENT_TOUCH_FRAME:
404 handle_touch_frame(libinput_device,
405 libinput_event_get_touch_event(event));
406 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100407 default:
408 handled = 0;
409 weston_log("unknown libinput event %d\n",
410 libinput_event_get_type(event));
411 }
412
Peter Hutterer87743e92016-01-18 16:38:22 +1000413 if (need_frame)
414 notify_pointer_frame(device->seat);
415
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100416 return handled;
417}
418
419static void
420notify_output_destroy(struct wl_listener *listener, void *data)
421{
422 struct evdev_device *device =
423 container_of(listener,
424 struct evdev_device, output_destroy_listener);
425 struct weston_compositor *c = device->seat->compositor;
426 struct weston_output *output;
427
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300428 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100429 output = container_of(c->output_list.next,
430 struct weston_output, link);
431 evdev_device_set_output(device, output);
432 } else {
433 device->output = NULL;
434 }
435}
436
Peter Hutterer3fbba492014-09-09 13:02:25 +1000437/**
438 * The WL_CALIBRATION property requires a pixel-specific matrix to be
439 * applied after scaling device coordinates to screen coordinates. libinput
440 * can't do that, so we need to convert the calibration to the normalized
441 * format libinput expects.
442 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300443void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000444evdev_device_set_calibration(struct evdev_device *device)
445{
446 struct udev *udev;
447 struct udev_device *udev_device = NULL;
448 const char *sysname = libinput_device_get_sysname(device->device);
449 const char *calibration_values;
450 uint32_t width, height;
451 float calibration[6];
452 enum libinput_config_status status;
453
454 if (!device->output)
455 return;
456
457 width = device->output->width;
458 height = device->output->height;
459 if (width == 0 || height == 0)
460 return;
461
462 /* If libinput has a pre-set calibration matrix, don't override it */
463 if (!libinput_device_config_calibration_has_matrix(device->device) ||
464 libinput_device_config_calibration_get_default_matrix(
465 device->device,
466 calibration) != 0)
467 return;
468
469 udev = udev_new();
470 if (!udev)
471 return;
472
473 udev_device = udev_device_new_from_subsystem_sysname(udev,
474 "input",
475 sysname);
476 if (!udev_device)
477 goto out;
478
479 calibration_values =
480 udev_device_get_property_value(udev_device,
481 "WL_CALIBRATION");
482
483 if (!calibration_values || sscanf(calibration_values,
484 "%f %f %f %f %f %f",
485 &calibration[0],
486 &calibration[1],
487 &calibration[2],
488 &calibration[3],
489 &calibration[4],
490 &calibration[5]) != 6)
491 goto out;
492
493 weston_log("Applying calibration: %f %f %f %f %f %f "
494 "(normalized %f %f)\n",
495 calibration[0],
496 calibration[1],
497 calibration[2],
498 calibration[3],
499 calibration[4],
500 calibration[5],
501 calibration[2] / width,
502 calibration[5] / height);
503
504 /* normalize to a format libinput can use. There is a chance of
505 this being wrong if the width/height don't match the device
506 width/height but I'm not sure how to fix that */
507 calibration[2] /= width;
508 calibration[5] /= height;
509
510 status = libinput_device_config_calibration_set_matrix(device->device,
511 calibration);
512 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
513 weston_log("Failed to apply calibration.\n");
514
515out:
516 if (udev_device)
517 udev_device_unref(udev_device);
518 udev_unref(udev);
519}
520
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100521void
522evdev_device_set_output(struct evdev_device *device,
523 struct weston_output *output)
524{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700525 if (device->output_destroy_listener.notify) {
526 wl_list_remove(&device->output_destroy_listener.link);
527 device->output_destroy_listener.notify = NULL;
528 }
529
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100530 device->output = output;
531 device->output_destroy_listener.notify = notify_output_destroy;
532 wl_signal_add(&output->destroy_signal,
533 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000534 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100535}
536
537struct evdev_device *
538evdev_device_create(struct libinput_device *libinput_device,
539 struct weston_seat *seat)
540{
541 struct evdev_device *device;
542
543 device = zalloc(sizeof *device);
544 if (device == NULL)
545 return NULL;
546
547 device->seat = seat;
548 wl_list_init(&device->link);
549 device->device = libinput_device;
550
551 if (libinput_device_has_capability(libinput_device,
552 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
553 weston_seat_init_keyboard(seat, NULL);
554 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
555 }
556 if (libinput_device_has_capability(libinput_device,
557 LIBINPUT_DEVICE_CAP_POINTER)) {
558 weston_seat_init_pointer(seat);
559 device->seat_caps |= EVDEV_SEAT_POINTER;
560 }
561 if (libinput_device_has_capability(libinput_device,
562 LIBINPUT_DEVICE_CAP_TOUCH)) {
563 weston_seat_init_touch(seat);
564 device->seat_caps |= EVDEV_SEAT_TOUCH;
565 }
566
567 libinput_device_set_user_data(libinput_device, device);
568 libinput_device_ref(libinput_device);
569
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100570 return device;
571}
572
573void
574evdev_device_destroy(struct evdev_device *device)
575{
576 if (device->seat_caps & EVDEV_SEAT_POINTER)
577 weston_seat_release_pointer(device->seat);
578 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
579 weston_seat_release_keyboard(device->seat);
580 if (device->seat_caps & EVDEV_SEAT_TOUCH)
581 weston_seat_release_touch(device->seat);
582
583 if (device->output)
584 wl_list_remove(&device->output_destroy_listener.link);
585 wl_list_remove(&device->link);
586 libinput_device_unref(device->device);
587 free(device->devnode);
588 free(device->output_name);
589 free(device);
590}
591
592void
593evdev_notify_keyboard_focus(struct weston_seat *seat,
594 struct wl_list *evdev_devices)
595{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100596 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100597
Derek Foremand621df22014-11-19 11:04:12 -0600598 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100599 return;
600
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100601 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100602 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100603 wl_array_release(&keys);
604}