blob: b1d269dbb520d98b11ca5ff093098a650df4bda0 [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>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010036#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;
Daniel Stone4933ca52017-03-14 17:24:04 +0000193 default:
194 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000195 }
196
197 return value;
198}
199
Peter Hutterer87743e92016-01-18 16:38:22 +1000200static int32_t
201get_axis_discrete(struct libinput_event_pointer *pointer_event,
202 enum libinput_pointer_axis axis)
203{
204 enum libinput_pointer_axis_source source;
205
206 source = libinput_event_pointer_get_axis_source(pointer_event);
207
208 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
209 return 0;
210
211 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
212 axis);
213}
214
215static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100216handle_pointer_axis(struct libinput_device *libinput_device,
217 struct libinput_event_pointer *pointer_event)
218{
Peter Hutterer87743e92016-01-18 16:38:22 +1000219 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100220 struct evdev_device *device =
221 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000222 double vert, horiz;
223 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000224 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000225 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000226 enum libinput_pointer_axis_source source;
227 uint32_t wl_axis_source;
228 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100229
Peter Hutterer87743e92016-01-18 16:38:22 +1000230 has_vert = libinput_event_pointer_has_axis(pointer_event,
231 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
232 has_horiz = libinput_event_pointer_has_axis(pointer_event,
233 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
234
235 if (!has_vert && !has_horiz)
236 return false;
237
238 source = libinput_event_pointer_get_axis_source(pointer_event);
239 switch (source) {
240 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
241 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
242 break;
243 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
244 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
245 break;
246 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
247 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
248 break;
249 default:
250 if (warned < 5) {
251 weston_log("Unknown scroll source %d.\n", source);
252 warned++;
253 }
254 return false;
255 }
256
257 notify_axis_source(device->seat, wl_axis_source);
258
259 if (has_vert) {
260 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
261 vert_discrete = get_axis_discrete(pointer_event, axis);
262 vert = normalize_scroll(pointer_event, axis);
263
Peter Hutterer89b6a492016-01-18 15:58:17 +1000264 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200265 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000266 weston_event.discrete = vert_discrete;
267 weston_event.has_discrete = (vert_discrete != 0);
268
Peter Huttererc54f23d2015-01-13 11:55:37 +1000269 notify_axis(device->seat,
270 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000271 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000272 }
273
Peter Hutterer87743e92016-01-18 16:38:22 +1000274 if (has_horiz) {
275 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
276 horiz_discrete = get_axis_discrete(pointer_event, axis);
277 horiz = normalize_scroll(pointer_event, axis);
278
Peter Hutterer89b6a492016-01-18 15:58:17 +1000279 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200280 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000281 weston_event.discrete = horiz_discrete;
282 weston_event.has_discrete = (horiz_discrete != 0);
283
Peter Huttererc54f23d2015-01-13 11:55:37 +1000284 notify_axis(device->seat,
285 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000286 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000287 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000288
289 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100290}
291
292static void
293handle_touch_with_coords(struct libinput_device *libinput_device,
294 struct libinput_event_touch *touch_event,
295 int touch_type)
296{
297 struct evdev_device *device =
298 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200299 double x;
300 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100301 uint32_t width, height;
302 uint32_t time;
303 int32_t slot;
304
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300305 if (!device->output)
306 return;
307
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100308 time = libinput_event_touch_get_time(touch_event);
309 slot = libinput_event_touch_get_seat_slot(touch_event);
310
311 width = device->output->current_mode->width;
312 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200313 x = libinput_event_touch_get_x_transformed(touch_event, width);
314 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100315
316 weston_output_transform_coordinate(device->output,
317 x, y, &x, &y);
318
319 notify_touch(device->seat, time, slot, x, y, touch_type);
320}
321
322static void
323handle_touch_down(struct libinput_device *device,
324 struct libinput_event_touch *touch_event)
325{
326 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
327}
328
329static void
330handle_touch_motion(struct libinput_device *device,
331 struct libinput_event_touch *touch_event)
332{
333 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
334}
335
336static void
337handle_touch_up(struct libinput_device *libinput_device,
338 struct libinput_event_touch *touch_event)
339{
340 struct evdev_device *device =
341 libinput_device_get_user_data(libinput_device);
342 uint32_t time = libinput_event_touch_get_time(touch_event);
343 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
344
345 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
346}
347
Jonas Ådahl1679f232014-04-12 09:39:51 +0200348static void
349handle_touch_frame(struct libinput_device *libinput_device,
350 struct libinput_event_touch *touch_event)
351{
352 struct evdev_device *device =
353 libinput_device_get_user_data(libinput_device);
354 struct weston_seat *seat = device->seat;
355
356 notify_touch_frame(seat);
357}
358
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100359int
360evdev_device_process_event(struct libinput_event *event)
361{
362 struct libinput_device *libinput_device =
363 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000364 struct evdev_device *device =
365 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100366 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000367 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100368
369 switch (libinput_event_get_type(event)) {
370 case LIBINPUT_EVENT_KEYBOARD_KEY:
371 handle_keyboard_key(libinput_device,
372 libinput_event_get_keyboard_event(event));
373 break;
374 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000375 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100376 libinput_event_get_pointer_event(event));
377 break;
378 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000379 need_frame = handle_pointer_motion_absolute(
380 libinput_device,
381 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100382 break;
383 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000384 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100385 libinput_event_get_pointer_event(event));
386 break;
387 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000388 need_frame = handle_pointer_axis(
389 libinput_device,
390 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100391 break;
392 case LIBINPUT_EVENT_TOUCH_DOWN:
393 handle_touch_down(libinput_device,
394 libinput_event_get_touch_event(event));
395 break;
396 case LIBINPUT_EVENT_TOUCH_MOTION:
397 handle_touch_motion(libinput_device,
398 libinput_event_get_touch_event(event));
399 break;
400 case LIBINPUT_EVENT_TOUCH_UP:
401 handle_touch_up(libinput_device,
402 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700403 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200404 case LIBINPUT_EVENT_TOUCH_FRAME:
405 handle_touch_frame(libinput_device,
406 libinput_event_get_touch_event(event));
407 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100408 default:
409 handled = 0;
410 weston_log("unknown libinput event %d\n",
411 libinput_event_get_type(event));
412 }
413
Peter Hutterer87743e92016-01-18 16:38:22 +1000414 if (need_frame)
415 notify_pointer_frame(device->seat);
416
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100417 return handled;
418}
419
420static void
421notify_output_destroy(struct wl_listener *listener, void *data)
422{
423 struct evdev_device *device =
424 container_of(listener,
425 struct evdev_device, output_destroy_listener);
426 struct weston_compositor *c = device->seat->compositor;
427 struct weston_output *output;
428
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300429 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100430 output = container_of(c->output_list.next,
431 struct weston_output, link);
432 evdev_device_set_output(device, output);
433 } else {
434 device->output = NULL;
435 }
436}
437
Peter Hutterer3fbba492014-09-09 13:02:25 +1000438/**
439 * The WL_CALIBRATION property requires a pixel-specific matrix to be
440 * applied after scaling device coordinates to screen coordinates. libinput
441 * can't do that, so we need to convert the calibration to the normalized
442 * format libinput expects.
443 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300444void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000445evdev_device_set_calibration(struct evdev_device *device)
446{
447 struct udev *udev;
448 struct udev_device *udev_device = NULL;
449 const char *sysname = libinput_device_get_sysname(device->device);
450 const char *calibration_values;
451 uint32_t width, height;
452 float calibration[6];
453 enum libinput_config_status status;
454
455 if (!device->output)
456 return;
457
458 width = device->output->width;
459 height = device->output->height;
460 if (width == 0 || height == 0)
461 return;
462
463 /* If libinput has a pre-set calibration matrix, don't override it */
464 if (!libinput_device_config_calibration_has_matrix(device->device) ||
465 libinput_device_config_calibration_get_default_matrix(
466 device->device,
467 calibration) != 0)
468 return;
469
470 udev = udev_new();
471 if (!udev)
472 return;
473
474 udev_device = udev_device_new_from_subsystem_sysname(udev,
475 "input",
476 sysname);
477 if (!udev_device)
478 goto out;
479
480 calibration_values =
481 udev_device_get_property_value(udev_device,
482 "WL_CALIBRATION");
483
484 if (!calibration_values || sscanf(calibration_values,
485 "%f %f %f %f %f %f",
486 &calibration[0],
487 &calibration[1],
488 &calibration[2],
489 &calibration[3],
490 &calibration[4],
491 &calibration[5]) != 6)
492 goto out;
493
494 weston_log("Applying calibration: %f %f %f %f %f %f "
495 "(normalized %f %f)\n",
496 calibration[0],
497 calibration[1],
498 calibration[2],
499 calibration[3],
500 calibration[4],
501 calibration[5],
502 calibration[2] / width,
503 calibration[5] / height);
504
505 /* normalize to a format libinput can use. There is a chance of
506 this being wrong if the width/height don't match the device
507 width/height but I'm not sure how to fix that */
508 calibration[2] /= width;
509 calibration[5] /= height;
510
511 status = libinput_device_config_calibration_set_matrix(device->device,
512 calibration);
513 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
514 weston_log("Failed to apply calibration.\n");
515
516out:
517 if (udev_device)
518 udev_device_unref(udev_device);
519 udev_unref(udev);
520}
521
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100522void
523evdev_device_set_output(struct evdev_device *device,
524 struct weston_output *output)
525{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700526 if (device->output_destroy_listener.notify) {
527 wl_list_remove(&device->output_destroy_listener.link);
528 device->output_destroy_listener.notify = NULL;
529 }
530
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100531 device->output = output;
532 device->output_destroy_listener.notify = notify_output_destroy;
533 wl_signal_add(&output->destroy_signal,
534 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000535 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100536}
537
538struct evdev_device *
539evdev_device_create(struct libinput_device *libinput_device,
540 struct weston_seat *seat)
541{
542 struct evdev_device *device;
543
544 device = zalloc(sizeof *device);
545 if (device == NULL)
546 return NULL;
547
548 device->seat = seat;
549 wl_list_init(&device->link);
550 device->device = libinput_device;
551
552 if (libinput_device_has_capability(libinput_device,
553 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
554 weston_seat_init_keyboard(seat, NULL);
555 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
556 }
557 if (libinput_device_has_capability(libinput_device,
558 LIBINPUT_DEVICE_CAP_POINTER)) {
559 weston_seat_init_pointer(seat);
560 device->seat_caps |= EVDEV_SEAT_POINTER;
561 }
562 if (libinput_device_has_capability(libinput_device,
563 LIBINPUT_DEVICE_CAP_TOUCH)) {
564 weston_seat_init_touch(seat);
565 device->seat_caps |= EVDEV_SEAT_TOUCH;
566 }
567
568 libinput_device_set_user_data(libinput_device, device);
569 libinput_device_ref(libinput_device);
570
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100571 return device;
572}
573
574void
575evdev_device_destroy(struct evdev_device *device)
576{
577 if (device->seat_caps & EVDEV_SEAT_POINTER)
578 weston_seat_release_pointer(device->seat);
579 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
580 weston_seat_release_keyboard(device->seat);
581 if (device->seat_caps & EVDEV_SEAT_TOUCH)
582 weston_seat_release_touch(device->seat);
583
584 if (device->output)
585 wl_list_remove(&device->output_destroy_listener.link);
586 wl_list_remove(&device->link);
587 libinput_device_unref(device->device);
588 free(device->devnode);
589 free(device->output_name);
590 free(device);
591}
592
593void
594evdev_notify_keyboard_focus(struct weston_seat *seat,
595 struct wl_list *evdev_devices)
596{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100597 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100598
Derek Foremand621df22014-11-19 11:04:12 -0600599 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100600 return;
601
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100602 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100603 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100604 wl_array_release(&keys);
605}