blob: 5e7182da153218d2d39199e1a7768e5925612f98 [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;
Daniel Stone4933ca52017-03-14 17:24:04 +0000194 default:
195 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000196 }
197
198 return value;
199}
200
Peter Hutterer87743e92016-01-18 16:38:22 +1000201static int32_t
202get_axis_discrete(struct libinput_event_pointer *pointer_event,
203 enum libinput_pointer_axis axis)
204{
205 enum libinput_pointer_axis_source source;
206
207 source = libinput_event_pointer_get_axis_source(pointer_event);
208
209 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
210 return 0;
211
212 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
213 axis);
214}
215
216static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100217handle_pointer_axis(struct libinput_device *libinput_device,
218 struct libinput_event_pointer *pointer_event)
219{
Peter Hutterer87743e92016-01-18 16:38:22 +1000220 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100221 struct evdev_device *device =
222 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000223 double vert, horiz;
224 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000225 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000226 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000227 enum libinput_pointer_axis_source source;
228 uint32_t wl_axis_source;
229 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100230
Peter Hutterer87743e92016-01-18 16:38:22 +1000231 has_vert = libinput_event_pointer_has_axis(pointer_event,
232 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
233 has_horiz = libinput_event_pointer_has_axis(pointer_event,
234 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
235
236 if (!has_vert && !has_horiz)
237 return false;
238
239 source = libinput_event_pointer_get_axis_source(pointer_event);
240 switch (source) {
241 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
242 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
243 break;
244 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
245 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
246 break;
247 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
248 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
249 break;
250 default:
251 if (warned < 5) {
252 weston_log("Unknown scroll source %d.\n", source);
253 warned++;
254 }
255 return false;
256 }
257
258 notify_axis_source(device->seat, wl_axis_source);
259
260 if (has_vert) {
261 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
262 vert_discrete = get_axis_discrete(pointer_event, axis);
263 vert = normalize_scroll(pointer_event, axis);
264
Peter Hutterer89b6a492016-01-18 15:58:17 +1000265 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200266 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000267 weston_event.discrete = vert_discrete;
268 weston_event.has_discrete = (vert_discrete != 0);
269
Peter Huttererc54f23d2015-01-13 11:55:37 +1000270 notify_axis(device->seat,
271 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000272 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000273 }
274
Peter Hutterer87743e92016-01-18 16:38:22 +1000275 if (has_horiz) {
276 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
277 horiz_discrete = get_axis_discrete(pointer_event, axis);
278 horiz = normalize_scroll(pointer_event, axis);
279
Peter Hutterer89b6a492016-01-18 15:58:17 +1000280 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200281 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000282 weston_event.discrete = horiz_discrete;
283 weston_event.has_discrete = (horiz_discrete != 0);
284
Peter Huttererc54f23d2015-01-13 11:55:37 +1000285 notify_axis(device->seat,
286 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000287 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000288 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000289
290 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100291}
292
293static void
294handle_touch_with_coords(struct libinput_device *libinput_device,
295 struct libinput_event_touch *touch_event,
296 int touch_type)
297{
298 struct evdev_device *device =
299 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200300 double x;
301 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100302 uint32_t width, height;
303 uint32_t time;
304 int32_t slot;
305
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300306 if (!device->output)
307 return;
308
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100309 time = libinput_event_touch_get_time(touch_event);
310 slot = libinput_event_touch_get_seat_slot(touch_event);
311
312 width = device->output->current_mode->width;
313 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200314 x = libinput_event_touch_get_x_transformed(touch_event, width);
315 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100316
317 weston_output_transform_coordinate(device->output,
318 x, y, &x, &y);
319
320 notify_touch(device->seat, time, slot, x, y, touch_type);
321}
322
323static void
324handle_touch_down(struct libinput_device *device,
325 struct libinput_event_touch *touch_event)
326{
327 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
328}
329
330static void
331handle_touch_motion(struct libinput_device *device,
332 struct libinput_event_touch *touch_event)
333{
334 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
335}
336
337static void
338handle_touch_up(struct libinput_device *libinput_device,
339 struct libinput_event_touch *touch_event)
340{
341 struct evdev_device *device =
342 libinput_device_get_user_data(libinput_device);
343 uint32_t time = libinput_event_touch_get_time(touch_event);
344 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
345
346 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
347}
348
Jonas Ådahl1679f232014-04-12 09:39:51 +0200349static void
350handle_touch_frame(struct libinput_device *libinput_device,
351 struct libinput_event_touch *touch_event)
352{
353 struct evdev_device *device =
354 libinput_device_get_user_data(libinput_device);
355 struct weston_seat *seat = device->seat;
356
357 notify_touch_frame(seat);
358}
359
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100360int
361evdev_device_process_event(struct libinput_event *event)
362{
363 struct libinput_device *libinput_device =
364 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000365 struct evdev_device *device =
366 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100367 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000368 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100369
370 switch (libinput_event_get_type(event)) {
371 case LIBINPUT_EVENT_KEYBOARD_KEY:
372 handle_keyboard_key(libinput_device,
373 libinput_event_get_keyboard_event(event));
374 break;
375 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000376 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100377 libinput_event_get_pointer_event(event));
378 break;
379 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000380 need_frame = handle_pointer_motion_absolute(
381 libinput_device,
382 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100383 break;
384 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000385 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100386 libinput_event_get_pointer_event(event));
387 break;
388 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000389 need_frame = handle_pointer_axis(
390 libinput_device,
391 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100392 break;
393 case LIBINPUT_EVENT_TOUCH_DOWN:
394 handle_touch_down(libinput_device,
395 libinput_event_get_touch_event(event));
396 break;
397 case LIBINPUT_EVENT_TOUCH_MOTION:
398 handle_touch_motion(libinput_device,
399 libinput_event_get_touch_event(event));
400 break;
401 case LIBINPUT_EVENT_TOUCH_UP:
402 handle_touch_up(libinput_device,
403 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700404 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200405 case LIBINPUT_EVENT_TOUCH_FRAME:
406 handle_touch_frame(libinput_device,
407 libinput_event_get_touch_event(event));
408 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100409 default:
410 handled = 0;
411 weston_log("unknown libinput event %d\n",
412 libinput_event_get_type(event));
413 }
414
Peter Hutterer87743e92016-01-18 16:38:22 +1000415 if (need_frame)
416 notify_pointer_frame(device->seat);
417
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100418 return handled;
419}
420
421static void
422notify_output_destroy(struct wl_listener *listener, void *data)
423{
424 struct evdev_device *device =
425 container_of(listener,
426 struct evdev_device, output_destroy_listener);
427 struct weston_compositor *c = device->seat->compositor;
428 struct weston_output *output;
429
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300430 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100431 output = container_of(c->output_list.next,
432 struct weston_output, link);
433 evdev_device_set_output(device, output);
434 } else {
435 device->output = NULL;
436 }
437}
438
Peter Hutterer3fbba492014-09-09 13:02:25 +1000439/**
440 * The WL_CALIBRATION property requires a pixel-specific matrix to be
441 * applied after scaling device coordinates to screen coordinates. libinput
442 * can't do that, so we need to convert the calibration to the normalized
443 * format libinput expects.
444 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300445void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000446evdev_device_set_calibration(struct evdev_device *device)
447{
448 struct udev *udev;
449 struct udev_device *udev_device = NULL;
450 const char *sysname = libinput_device_get_sysname(device->device);
451 const char *calibration_values;
452 uint32_t width, height;
453 float calibration[6];
454 enum libinput_config_status status;
455
456 if (!device->output)
457 return;
458
459 width = device->output->width;
460 height = device->output->height;
461 if (width == 0 || height == 0)
462 return;
463
464 /* If libinput has a pre-set calibration matrix, don't override it */
465 if (!libinput_device_config_calibration_has_matrix(device->device) ||
466 libinput_device_config_calibration_get_default_matrix(
467 device->device,
468 calibration) != 0)
469 return;
470
471 udev = udev_new();
472 if (!udev)
473 return;
474
475 udev_device = udev_device_new_from_subsystem_sysname(udev,
476 "input",
477 sysname);
478 if (!udev_device)
479 goto out;
480
481 calibration_values =
482 udev_device_get_property_value(udev_device,
483 "WL_CALIBRATION");
484
485 if (!calibration_values || sscanf(calibration_values,
486 "%f %f %f %f %f %f",
487 &calibration[0],
488 &calibration[1],
489 &calibration[2],
490 &calibration[3],
491 &calibration[4],
492 &calibration[5]) != 6)
493 goto out;
494
495 weston_log("Applying calibration: %f %f %f %f %f %f "
496 "(normalized %f %f)\n",
497 calibration[0],
498 calibration[1],
499 calibration[2],
500 calibration[3],
501 calibration[4],
502 calibration[5],
503 calibration[2] / width,
504 calibration[5] / height);
505
506 /* normalize to a format libinput can use. There is a chance of
507 this being wrong if the width/height don't match the device
508 width/height but I'm not sure how to fix that */
509 calibration[2] /= width;
510 calibration[5] /= height;
511
512 status = libinput_device_config_calibration_set_matrix(device->device,
513 calibration);
514 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
515 weston_log("Failed to apply calibration.\n");
516
517out:
518 if (udev_device)
519 udev_device_unref(udev_device);
520 udev_unref(udev);
521}
522
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100523void
524evdev_device_set_output(struct evdev_device *device,
525 struct weston_output *output)
526{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700527 if (device->output_destroy_listener.notify) {
528 wl_list_remove(&device->output_destroy_listener.link);
529 device->output_destroy_listener.notify = NULL;
530 }
531
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100532 device->output = output;
533 device->output_destroy_listener.notify = notify_output_destroy;
534 wl_signal_add(&output->destroy_signal,
535 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000536 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100537}
538
539struct evdev_device *
540evdev_device_create(struct libinput_device *libinput_device,
541 struct weston_seat *seat)
542{
543 struct evdev_device *device;
544
545 device = zalloc(sizeof *device);
546 if (device == NULL)
547 return NULL;
548
549 device->seat = seat;
550 wl_list_init(&device->link);
551 device->device = libinput_device;
552
553 if (libinput_device_has_capability(libinput_device,
554 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
555 weston_seat_init_keyboard(seat, NULL);
556 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
557 }
558 if (libinput_device_has_capability(libinput_device,
559 LIBINPUT_DEVICE_CAP_POINTER)) {
560 weston_seat_init_pointer(seat);
561 device->seat_caps |= EVDEV_SEAT_POINTER;
562 }
563 if (libinput_device_has_capability(libinput_device,
564 LIBINPUT_DEVICE_CAP_TOUCH)) {
565 weston_seat_init_touch(seat);
566 device->seat_caps |= EVDEV_SEAT_TOUCH;
567 }
568
569 libinput_device_set_user_data(libinput_device, device);
570 libinput_device_ref(libinput_device);
571
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100572 return device;
573}
574
575void
576evdev_device_destroy(struct evdev_device *device)
577{
578 if (device->seat_caps & EVDEV_SEAT_POINTER)
579 weston_seat_release_pointer(device->seat);
580 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
581 weston_seat_release_keyboard(device->seat);
582 if (device->seat_caps & EVDEV_SEAT_TOUCH)
583 weston_seat_release_touch(device->seat);
584
585 if (device->output)
586 wl_list_remove(&device->output_destroy_listener.link);
587 wl_list_remove(&device->link);
588 libinput_device_unref(device->device);
589 free(device->devnode);
590 free(device->output_name);
591 free(device);
592}
593
594void
595evdev_notify_keyboard_focus(struct weston_seat *seat,
596 struct wl_list *evdev_devices)
597{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100598 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100599
Derek Foremand621df22014-11-19 11:04:12 -0600600 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100601 return;
602
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100603 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100604 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100605 wl_array_release(&keys);
606}