blob: 4d8cf2e6ec07cd46da02bb1ae1e2db8541921b8e [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"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020042#include "shared/timespec-util.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 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020090 struct timespec time;
Jonas Ådahl3845b322014-10-21 23:51:29 +020091 double dx_unaccel, dy_unaccel;
92
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020093 timespec_from_usec(&time,
94 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahl3845b322014-10-21 23:51:29 +020095 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
96 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010097
Jonas Ådahld2510102014-10-05 21:39:14 +020098 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +020099 .mask = WESTON_POINTER_MOTION_REL |
100 WESTON_POINTER_MOTION_REL_UNACCEL,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200101 .time = time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200102 .dx = libinput_event_pointer_get_dx(pointer_event),
103 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200104 .dx_unaccel = dx_unaccel,
105 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200106 };
107
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200108 notify_motion(device->seat, &time, &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;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200121 struct timespec 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
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200128 timespec_from_usec(&time,
129 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100130 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);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200139 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);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200154 struct timespec time;
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800155
156 /* Ignore button events that are not seat wide state changes. */
157 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
158 seat_button_count != 1) ||
159 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
160 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000161 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100162
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200163 timespec_from_usec(&time,
164 libinput_event_pointer_get_time_usec(pointer_event));
165
166 notify_button(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100167 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500168 button_state);
169
Peter Hutterer87743e92016-01-18 16:38:22 +1000170 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100171}
172
Peter Hutterer5dddd412015-01-15 13:14:43 +1000173static double
174normalize_scroll(struct libinput_event_pointer *pointer_event,
175 enum libinput_pointer_axis axis)
176{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000177 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000178 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000179
180 source = libinput_event_pointer_get_axis_source(pointer_event);
181 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
182 the value is the angle of the click in degrees. To keep
183 backwards-compat with existing clients, we just send multiples of
184 the click count.
185 */
186 switch (source) {
187 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
188 value = 10 * libinput_event_pointer_get_axis_value_discrete(
189 pointer_event,
190 axis);
191 break;
192 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
193 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
194 value = libinput_event_pointer_get_axis_value(pointer_event,
195 axis);
196 break;
Daniel Stone4933ca52017-03-14 17:24:04 +0000197 default:
198 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000199 }
200
201 return value;
202}
203
Peter Hutterer87743e92016-01-18 16:38:22 +1000204static int32_t
205get_axis_discrete(struct libinput_event_pointer *pointer_event,
206 enum libinput_pointer_axis axis)
207{
208 enum libinput_pointer_axis_source source;
209
210 source = libinput_event_pointer_get_axis_source(pointer_event);
211
212 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
213 return 0;
214
215 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
216 axis);
217}
218
219static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100220handle_pointer_axis(struct libinput_device *libinput_device,
221 struct libinput_event_pointer *pointer_event)
222{
Peter Hutterer87743e92016-01-18 16:38:22 +1000223 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100224 struct evdev_device *device =
225 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000226 double vert, horiz;
227 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000228 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000229 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000230 enum libinput_pointer_axis_source source;
231 uint32_t wl_axis_source;
232 bool has_vert, has_horiz;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200233 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100234
Peter Hutterer87743e92016-01-18 16:38:22 +1000235 has_vert = libinput_event_pointer_has_axis(pointer_event,
236 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
237 has_horiz = libinput_event_pointer_has_axis(pointer_event,
238 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
239
240 if (!has_vert && !has_horiz)
241 return false;
242
243 source = libinput_event_pointer_get_axis_source(pointer_event);
244 switch (source) {
245 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
246 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
247 break;
248 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
249 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
250 break;
251 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
252 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
253 break;
254 default:
255 if (warned < 5) {
256 weston_log("Unknown scroll source %d.\n", source);
257 warned++;
258 }
259 return false;
260 }
261
262 notify_axis_source(device->seat, wl_axis_source);
263
Alexandros Frantzis80321942017-11-16 18:20:56 +0200264 timespec_from_usec(&time,
265 libinput_event_pointer_get_time_usec(pointer_event));
266
Peter Hutterer87743e92016-01-18 16:38:22 +1000267 if (has_vert) {
268 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
269 vert_discrete = get_axis_discrete(pointer_event, axis);
270 vert = normalize_scroll(pointer_event, axis);
271
Peter Hutterer89b6a492016-01-18 15:58:17 +1000272 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200273 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000274 weston_event.discrete = vert_discrete;
275 weston_event.has_discrete = (vert_discrete != 0);
276
Alexandros Frantzis80321942017-11-16 18:20:56 +0200277 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000278 }
279
Peter Hutterer87743e92016-01-18 16:38:22 +1000280 if (has_horiz) {
281 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
282 horiz_discrete = get_axis_discrete(pointer_event, axis);
283 horiz = normalize_scroll(pointer_event, axis);
284
Peter Hutterer89b6a492016-01-18 15:58:17 +1000285 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200286 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000287 weston_event.discrete = horiz_discrete;
288 weston_event.has_discrete = (horiz_discrete != 0);
289
Alexandros Frantzis80321942017-11-16 18:20:56 +0200290 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000291 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000292
293 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100294}
295
296static void
297handle_touch_with_coords(struct libinput_device *libinput_device,
298 struct libinput_event_touch *touch_event,
299 int touch_type)
300{
301 struct evdev_device *device =
302 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200303 double x;
304 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100305 uint32_t width, height;
306 uint32_t time;
307 int32_t slot;
308
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300309 if (!device->output)
310 return;
311
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100312 time = libinput_event_touch_get_time(touch_event);
313 slot = libinput_event_touch_get_seat_slot(touch_event);
314
315 width = device->output->current_mode->width;
316 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200317 x = libinput_event_touch_get_x_transformed(touch_event, width);
318 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100319
320 weston_output_transform_coordinate(device->output,
321 x, y, &x, &y);
322
323 notify_touch(device->seat, time, slot, x, y, touch_type);
324}
325
326static void
327handle_touch_down(struct libinput_device *device,
328 struct libinput_event_touch *touch_event)
329{
330 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
331}
332
333static void
334handle_touch_motion(struct libinput_device *device,
335 struct libinput_event_touch *touch_event)
336{
337 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
338}
339
340static void
341handle_touch_up(struct libinput_device *libinput_device,
342 struct libinput_event_touch *touch_event)
343{
344 struct evdev_device *device =
345 libinput_device_get_user_data(libinput_device);
346 uint32_t time = libinput_event_touch_get_time(touch_event);
347 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
348
349 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
350}
351
Jonas Ådahl1679f232014-04-12 09:39:51 +0200352static void
353handle_touch_frame(struct libinput_device *libinput_device,
354 struct libinput_event_touch *touch_event)
355{
356 struct evdev_device *device =
357 libinput_device_get_user_data(libinput_device);
358 struct weston_seat *seat = device->seat;
359
360 notify_touch_frame(seat);
361}
362
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100363int
364evdev_device_process_event(struct libinput_event *event)
365{
366 struct libinput_device *libinput_device =
367 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000368 struct evdev_device *device =
369 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100370 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000371 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100372
373 switch (libinput_event_get_type(event)) {
374 case LIBINPUT_EVENT_KEYBOARD_KEY:
375 handle_keyboard_key(libinput_device,
376 libinput_event_get_keyboard_event(event));
377 break;
378 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000379 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100380 libinput_event_get_pointer_event(event));
381 break;
382 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000383 need_frame = handle_pointer_motion_absolute(
384 libinput_device,
385 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100386 break;
387 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000388 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100389 libinput_event_get_pointer_event(event));
390 break;
391 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000392 need_frame = handle_pointer_axis(
393 libinput_device,
394 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100395 break;
396 case LIBINPUT_EVENT_TOUCH_DOWN:
397 handle_touch_down(libinput_device,
398 libinput_event_get_touch_event(event));
399 break;
400 case LIBINPUT_EVENT_TOUCH_MOTION:
401 handle_touch_motion(libinput_device,
402 libinput_event_get_touch_event(event));
403 break;
404 case LIBINPUT_EVENT_TOUCH_UP:
405 handle_touch_up(libinput_device,
406 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700407 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200408 case LIBINPUT_EVENT_TOUCH_FRAME:
409 handle_touch_frame(libinput_device,
410 libinput_event_get_touch_event(event));
411 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100412 default:
413 handled = 0;
414 weston_log("unknown libinput event %d\n",
415 libinput_event_get_type(event));
416 }
417
Peter Hutterer87743e92016-01-18 16:38:22 +1000418 if (need_frame)
419 notify_pointer_frame(device->seat);
420
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100421 return handled;
422}
423
424static void
425notify_output_destroy(struct wl_listener *listener, void *data)
426{
427 struct evdev_device *device =
428 container_of(listener,
429 struct evdev_device, output_destroy_listener);
430 struct weston_compositor *c = device->seat->compositor;
431 struct weston_output *output;
432
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300433 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100434 output = container_of(c->output_list.next,
435 struct weston_output, link);
436 evdev_device_set_output(device, output);
437 } else {
438 device->output = NULL;
439 }
440}
441
Peter Hutterer3fbba492014-09-09 13:02:25 +1000442/**
443 * The WL_CALIBRATION property requires a pixel-specific matrix to be
444 * applied after scaling device coordinates to screen coordinates. libinput
445 * can't do that, so we need to convert the calibration to the normalized
446 * format libinput expects.
447 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300448void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000449evdev_device_set_calibration(struct evdev_device *device)
450{
451 struct udev *udev;
452 struct udev_device *udev_device = NULL;
453 const char *sysname = libinput_device_get_sysname(device->device);
454 const char *calibration_values;
455 uint32_t width, height;
456 float calibration[6];
457 enum libinput_config_status status;
458
459 if (!device->output)
460 return;
461
462 width = device->output->width;
463 height = device->output->height;
464 if (width == 0 || height == 0)
465 return;
466
467 /* If libinput has a pre-set calibration matrix, don't override it */
468 if (!libinput_device_config_calibration_has_matrix(device->device) ||
469 libinput_device_config_calibration_get_default_matrix(
470 device->device,
471 calibration) != 0)
472 return;
473
474 udev = udev_new();
475 if (!udev)
476 return;
477
478 udev_device = udev_device_new_from_subsystem_sysname(udev,
479 "input",
480 sysname);
481 if (!udev_device)
482 goto out;
483
484 calibration_values =
485 udev_device_get_property_value(udev_device,
486 "WL_CALIBRATION");
487
488 if (!calibration_values || sscanf(calibration_values,
489 "%f %f %f %f %f %f",
490 &calibration[0],
491 &calibration[1],
492 &calibration[2],
493 &calibration[3],
494 &calibration[4],
495 &calibration[5]) != 6)
496 goto out;
497
498 weston_log("Applying calibration: %f %f %f %f %f %f "
499 "(normalized %f %f)\n",
500 calibration[0],
501 calibration[1],
502 calibration[2],
503 calibration[3],
504 calibration[4],
505 calibration[5],
506 calibration[2] / width,
507 calibration[5] / height);
508
509 /* normalize to a format libinput can use. There is a chance of
510 this being wrong if the width/height don't match the device
511 width/height but I'm not sure how to fix that */
512 calibration[2] /= width;
513 calibration[5] /= height;
514
515 status = libinput_device_config_calibration_set_matrix(device->device,
516 calibration);
517 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
518 weston_log("Failed to apply calibration.\n");
519
520out:
521 if (udev_device)
522 udev_device_unref(udev_device);
523 udev_unref(udev);
524}
525
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100526void
527evdev_device_set_output(struct evdev_device *device,
528 struct weston_output *output)
529{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700530 if (device->output_destroy_listener.notify) {
531 wl_list_remove(&device->output_destroy_listener.link);
532 device->output_destroy_listener.notify = NULL;
533 }
534
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100535 device->output = output;
536 device->output_destroy_listener.notify = notify_output_destroy;
537 wl_signal_add(&output->destroy_signal,
538 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000539 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100540}
541
542struct evdev_device *
543evdev_device_create(struct libinput_device *libinput_device,
544 struct weston_seat *seat)
545{
546 struct evdev_device *device;
547
548 device = zalloc(sizeof *device);
549 if (device == NULL)
550 return NULL;
551
552 device->seat = seat;
553 wl_list_init(&device->link);
554 device->device = libinput_device;
555
556 if (libinput_device_has_capability(libinput_device,
557 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
558 weston_seat_init_keyboard(seat, NULL);
559 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
560 }
561 if (libinput_device_has_capability(libinput_device,
562 LIBINPUT_DEVICE_CAP_POINTER)) {
563 weston_seat_init_pointer(seat);
564 device->seat_caps |= EVDEV_SEAT_POINTER;
565 }
566 if (libinput_device_has_capability(libinput_device,
567 LIBINPUT_DEVICE_CAP_TOUCH)) {
568 weston_seat_init_touch(seat);
569 device->seat_caps |= EVDEV_SEAT_TOUCH;
570 }
571
572 libinput_device_set_user_data(libinput_device, device);
573 libinput_device_ref(libinput_device);
574
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100575 return device;
576}
577
578void
579evdev_device_destroy(struct evdev_device *device)
580{
581 if (device->seat_caps & EVDEV_SEAT_POINTER)
582 weston_seat_release_pointer(device->seat);
583 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
584 weston_seat_release_keyboard(device->seat);
585 if (device->seat_caps & EVDEV_SEAT_TOUCH)
586 weston_seat_release_touch(device->seat);
587
588 if (device->output)
589 wl_list_remove(&device->output_destroy_listener.link);
590 wl_list_remove(&device->link);
591 libinput_device_unref(device->device);
592 free(device->devnode);
593 free(device->output_name);
594 free(device);
595}
596
597void
598evdev_notify_keyboard_focus(struct weston_seat *seat,
599 struct wl_list *evdev_devices)
600{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100601 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100602
Derek Foremand621df22014-11-19 11:04:12 -0600603 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100604 return;
605
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100606 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100607 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100608 wl_array_release(&keys);
609}