blob: 78b0ac922a4e0eb696eabbdbcb9600d0851a99e6 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Jonas Ådahle0de3c22014-03-12 22:08:42 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Jonas Ådahle0de3c22014-03-12 22:08:42 +010025 */
26
27#include "config.h"
28
29#include <errno.h>
30#include <stdlib.h>
31#include <string.h>
32#include <linux/input.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <mtdev.h>
36#include <assert.h>
37#include <libinput.h>
38
39#include "compositor.h"
40#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010042
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043void
44evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
45{
46 enum libinput_led leds = 0;
47
48 if (weston_leds & LED_NUM_LOCK)
49 leds |= LIBINPUT_LED_NUM_LOCK;
50 if (weston_leds & LED_CAPS_LOCK)
51 leds |= LIBINPUT_LED_CAPS_LOCK;
52 if (weston_leds & LED_SCROLL_LOCK)
53 leds |= LIBINPUT_LED_SCROLL_LOCK;
54
55 libinput_device_led_update(device->device, leds);
56}
57
58static void
59handle_keyboard_key(struct libinput_device *libinput_device,
60 struct libinput_event_keyboard *keyboard_event)
61{
62 struct evdev_device *device =
63 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080064 int key_state =
65 libinput_event_keyboard_get_key_state(keyboard_event);
66 int seat_key_count =
67 libinput_event_keyboard_get_seat_key_count(keyboard_event);
68
69 /* Ignore key events that are not seat wide state changes. */
70 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
71 seat_key_count != 1) ||
72 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
73 seat_key_count != 0))
74 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010075
76 notify_key(device->seat,
77 libinput_event_keyboard_get_time(keyboard_event),
78 libinput_event_keyboard_get_key(keyboard_event),
79 libinput_event_keyboard_get_key_state(keyboard_event),
80 STATE_UPDATE_AUTOMATIC);
81}
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 Ådahle0de3c22014-03-12 22:08:42 +010090
Jonas Ådahld2510102014-10-05 21:39:14 +020091 event = (struct weston_pointer_motion_event) {
92 .mask = WESTON_POINTER_MOTION_REL,
93 .dx = libinput_event_pointer_get_dx(pointer_event),
94 .dy = libinput_event_pointer_get_dy(pointer_event),
95 };
96
Jonas Ådahle0de3c22014-03-12 22:08:42 +010097 notify_motion(device->seat,
98 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahld2510102014-10-05 21:39:14 +020099 &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000100
101 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100102}
103
Peter Hutterer87743e92016-01-18 16:38:22 +1000104static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100105handle_pointer_motion_absolute(
106 struct libinput_device *libinput_device,
107 struct libinput_event_pointer *pointer_event)
108{
109 struct evdev_device *device =
110 libinput_device_get_user_data(libinput_device);
111 struct weston_output *output = device->output;
112 uint32_t time;
113 wl_fixed_t x, y;
114 uint32_t width, height;
115
116 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000117 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100118
119 time = libinput_event_pointer_get_time(pointer_event);
120 width = device->output->current_mode->width;
121 height = device->output->current_mode->height;
122
Jonas Ådahl26714b42014-06-02 23:15:48 +0200123 x = wl_fixed_from_double(
124 libinput_event_pointer_get_absolute_x_transformed(pointer_event,
125 width));
126 y = wl_fixed_from_double(
127 libinput_event_pointer_get_absolute_y_transformed(pointer_event,
128 height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100129
130 weston_output_transform_coordinate(device->output, x, y, &x, &y);
131 notify_motion_absolute(device->seat, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000132
133 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100134}
135
Peter Hutterer87743e92016-01-18 16:38:22 +1000136static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100137handle_pointer_button(struct libinput_device *libinput_device,
138 struct libinput_event_pointer *pointer_event)
139{
140 struct evdev_device *device =
141 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800142 int button_state =
143 libinput_event_pointer_get_button_state(pointer_event);
144 int seat_button_count =
145 libinput_event_pointer_get_seat_button_count(pointer_event);
146
147 /* Ignore button events that are not seat wide state changes. */
148 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
149 seat_button_count != 1) ||
150 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
151 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000152 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100153
154 notify_button(device->seat,
155 libinput_event_pointer_get_time(pointer_event),
156 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500157 button_state);
158
Peter Hutterer87743e92016-01-18 16:38:22 +1000159 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100160}
161
Peter Hutterer5dddd412015-01-15 13:14:43 +1000162static double
163normalize_scroll(struct libinput_event_pointer *pointer_event,
164 enum libinput_pointer_axis axis)
165{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000166 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000167 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000168
169 source = libinput_event_pointer_get_axis_source(pointer_event);
170 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
171 the value is the angle of the click in degrees. To keep
172 backwards-compat with existing clients, we just send multiples of
173 the click count.
174 */
175 switch (source) {
176 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
177 value = 10 * libinput_event_pointer_get_axis_value_discrete(
178 pointer_event,
179 axis);
180 break;
181 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
182 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
183 value = libinput_event_pointer_get_axis_value(pointer_event,
184 axis);
185 break;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000186 }
187
188 return value;
189}
190
Peter Hutterer87743e92016-01-18 16:38:22 +1000191static int32_t
192get_axis_discrete(struct libinput_event_pointer *pointer_event,
193 enum libinput_pointer_axis axis)
194{
195 enum libinput_pointer_axis_source source;
196
197 source = libinput_event_pointer_get_axis_source(pointer_event);
198
199 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
200 return 0;
201
202 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
203 axis);
204}
205
206static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100207handle_pointer_axis(struct libinput_device *libinput_device,
208 struct libinput_event_pointer *pointer_event)
209{
Peter Hutterer87743e92016-01-18 16:38:22 +1000210 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100211 struct evdev_device *device =
212 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000213 double vert, horiz;
214 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000215 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000216 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000217 enum libinput_pointer_axis_source source;
218 uint32_t wl_axis_source;
219 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100220
Peter Hutterer87743e92016-01-18 16:38:22 +1000221 has_vert = libinput_event_pointer_has_axis(pointer_event,
222 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
223 has_horiz = libinput_event_pointer_has_axis(pointer_event,
224 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
225
226 if (!has_vert && !has_horiz)
227 return false;
228
229 source = libinput_event_pointer_get_axis_source(pointer_event);
230 switch (source) {
231 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
232 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
233 break;
234 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
235 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
236 break;
237 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
238 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
239 break;
240 default:
241 if (warned < 5) {
242 weston_log("Unknown scroll source %d.\n", source);
243 warned++;
244 }
245 return false;
246 }
247
248 notify_axis_source(device->seat, wl_axis_source);
249
250 if (has_vert) {
251 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
252 vert_discrete = get_axis_discrete(pointer_event, axis);
253 vert = normalize_scroll(pointer_event, axis);
254
Peter Hutterer89b6a492016-01-18 15:58:17 +1000255 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Peter Hutterer87743e92016-01-18 16:38:22 +1000256 weston_event.value = wl_fixed_from_double(vert);
257 weston_event.discrete = vert_discrete;
258 weston_event.has_discrete = (vert_discrete != 0);
259
Peter Huttererc54f23d2015-01-13 11:55:37 +1000260 notify_axis(device->seat,
261 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000262 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000263 }
264
Peter Hutterer87743e92016-01-18 16:38:22 +1000265 if (has_horiz) {
266 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
267 horiz_discrete = get_axis_discrete(pointer_event, axis);
268 horiz = normalize_scroll(pointer_event, axis);
269
Peter Hutterer89b6a492016-01-18 15:58:17 +1000270 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Peter Hutterer87743e92016-01-18 16:38:22 +1000271 weston_event.value = wl_fixed_from_double(horiz);
272 weston_event.discrete = horiz_discrete;
273 weston_event.has_discrete = (horiz_discrete != 0);
274
Peter Huttererc54f23d2015-01-13 11:55:37 +1000275 notify_axis(device->seat,
276 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000277 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000278 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000279
280 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100281}
282
283static void
284handle_touch_with_coords(struct libinput_device *libinput_device,
285 struct libinput_event_touch *touch_event,
286 int touch_type)
287{
288 struct evdev_device *device =
289 libinput_device_get_user_data(libinput_device);
290 wl_fixed_t x;
291 wl_fixed_t y;
292 uint32_t width, height;
293 uint32_t time;
294 int32_t slot;
295
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300296 if (!device->output)
297 return;
298
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100299 time = libinput_event_touch_get_time(touch_event);
300 slot = libinput_event_touch_get_seat_slot(touch_event);
301
302 width = device->output->current_mode->width;
303 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200304 x = wl_fixed_from_double(
305 libinput_event_touch_get_x_transformed(touch_event, width));
306 y = wl_fixed_from_double(
307 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100308
309 weston_output_transform_coordinate(device->output,
310 x, y, &x, &y);
311
312 notify_touch(device->seat, time, slot, x, y, touch_type);
313}
314
315static void
316handle_touch_down(struct libinput_device *device,
317 struct libinput_event_touch *touch_event)
318{
319 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
320}
321
322static void
323handle_touch_motion(struct libinput_device *device,
324 struct libinput_event_touch *touch_event)
325{
326 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
327}
328
329static void
330handle_touch_up(struct libinput_device *libinput_device,
331 struct libinput_event_touch *touch_event)
332{
333 struct evdev_device *device =
334 libinput_device_get_user_data(libinput_device);
335 uint32_t time = libinput_event_touch_get_time(touch_event);
336 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
337
338 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
339}
340
Jonas Ådahl1679f232014-04-12 09:39:51 +0200341static void
342handle_touch_frame(struct libinput_device *libinput_device,
343 struct libinput_event_touch *touch_event)
344{
345 struct evdev_device *device =
346 libinput_device_get_user_data(libinput_device);
347 struct weston_seat *seat = device->seat;
348
349 notify_touch_frame(seat);
350}
351
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100352int
353evdev_device_process_event(struct libinput_event *event)
354{
355 struct libinput_device *libinput_device =
356 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000357 struct evdev_device *device =
358 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100359 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000360 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100361
362 switch (libinput_event_get_type(event)) {
363 case LIBINPUT_EVENT_KEYBOARD_KEY:
364 handle_keyboard_key(libinput_device,
365 libinput_event_get_keyboard_event(event));
366 break;
367 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000368 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100369 libinput_event_get_pointer_event(event));
370 break;
371 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000372 need_frame = handle_pointer_motion_absolute(
373 libinput_device,
374 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100375 break;
376 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000377 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100378 libinput_event_get_pointer_event(event));
379 break;
380 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000381 need_frame = handle_pointer_axis(
382 libinput_device,
383 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100384 break;
385 case LIBINPUT_EVENT_TOUCH_DOWN:
386 handle_touch_down(libinput_device,
387 libinput_event_get_touch_event(event));
388 break;
389 case LIBINPUT_EVENT_TOUCH_MOTION:
390 handle_touch_motion(libinput_device,
391 libinput_event_get_touch_event(event));
392 break;
393 case LIBINPUT_EVENT_TOUCH_UP:
394 handle_touch_up(libinput_device,
395 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700396 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200397 case LIBINPUT_EVENT_TOUCH_FRAME:
398 handle_touch_frame(libinput_device,
399 libinput_event_get_touch_event(event));
400 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100401 default:
402 handled = 0;
403 weston_log("unknown libinput event %d\n",
404 libinput_event_get_type(event));
405 }
406
Peter Hutterer87743e92016-01-18 16:38:22 +1000407 if (need_frame)
408 notify_pointer_frame(device->seat);
409
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100410 return handled;
411}
412
413static void
414notify_output_destroy(struct wl_listener *listener, void *data)
415{
416 struct evdev_device *device =
417 container_of(listener,
418 struct evdev_device, output_destroy_listener);
419 struct weston_compositor *c = device->seat->compositor;
420 struct weston_output *output;
421
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300422 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100423 output = container_of(c->output_list.next,
424 struct weston_output, link);
425 evdev_device_set_output(device, output);
426 } else {
427 device->output = NULL;
428 }
429}
430
Peter Hutterer3fbba492014-09-09 13:02:25 +1000431/**
432 * The WL_CALIBRATION property requires a pixel-specific matrix to be
433 * applied after scaling device coordinates to screen coordinates. libinput
434 * can't do that, so we need to convert the calibration to the normalized
435 * format libinput expects.
436 */
437static void
438evdev_device_set_calibration(struct evdev_device *device)
439{
440 struct udev *udev;
441 struct udev_device *udev_device = NULL;
442 const char *sysname = libinput_device_get_sysname(device->device);
443 const char *calibration_values;
444 uint32_t width, height;
445 float calibration[6];
446 enum libinput_config_status status;
447
448 if (!device->output)
449 return;
450
451 width = device->output->width;
452 height = device->output->height;
453 if (width == 0 || height == 0)
454 return;
455
456 /* If libinput has a pre-set calibration matrix, don't override it */
457 if (!libinput_device_config_calibration_has_matrix(device->device) ||
458 libinput_device_config_calibration_get_default_matrix(
459 device->device,
460 calibration) != 0)
461 return;
462
463 udev = udev_new();
464 if (!udev)
465 return;
466
467 udev_device = udev_device_new_from_subsystem_sysname(udev,
468 "input",
469 sysname);
470 if (!udev_device)
471 goto out;
472
473 calibration_values =
474 udev_device_get_property_value(udev_device,
475 "WL_CALIBRATION");
476
477 if (!calibration_values || sscanf(calibration_values,
478 "%f %f %f %f %f %f",
479 &calibration[0],
480 &calibration[1],
481 &calibration[2],
482 &calibration[3],
483 &calibration[4],
484 &calibration[5]) != 6)
485 goto out;
486
487 weston_log("Applying calibration: %f %f %f %f %f %f "
488 "(normalized %f %f)\n",
489 calibration[0],
490 calibration[1],
491 calibration[2],
492 calibration[3],
493 calibration[4],
494 calibration[5],
495 calibration[2] / width,
496 calibration[5] / height);
497
498 /* normalize to a format libinput can use. There is a chance of
499 this being wrong if the width/height don't match the device
500 width/height but I'm not sure how to fix that */
501 calibration[2] /= width;
502 calibration[5] /= height;
503
504 status = libinput_device_config_calibration_set_matrix(device->device,
505 calibration);
506 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
507 weston_log("Failed to apply calibration.\n");
508
509out:
510 if (udev_device)
511 udev_device_unref(udev_device);
512 udev_unref(udev);
513}
514
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100515void
516evdev_device_set_output(struct evdev_device *device,
517 struct weston_output *output)
518{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700519 if (device->output_destroy_listener.notify) {
520 wl_list_remove(&device->output_destroy_listener.link);
521 device->output_destroy_listener.notify = NULL;
522 }
523
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100524 device->output = output;
525 device->output_destroy_listener.notify = notify_output_destroy;
526 wl_signal_add(&output->destroy_signal,
527 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000528 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100529}
530
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200531static void
532configure_device(struct evdev_device *device)
533{
534 struct weston_compositor *compositor = device->seat->compositor;
535 struct weston_config_section *s;
536 int enable_tap;
537 int enable_tap_default;
538
539 s = weston_config_get_section(compositor->config,
540 "libinput", NULL, NULL);
541
542 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
543 enable_tap_default =
544 libinput_device_config_tap_get_default_enabled(
545 device->device);
546 weston_config_section_get_bool(s, "enable_tap",
547 &enable_tap,
548 enable_tap_default);
549 libinput_device_config_tap_set_enabled(device->device,
550 enable_tap);
551 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000552
553 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200554}
555
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100556struct evdev_device *
557evdev_device_create(struct libinput_device *libinput_device,
558 struct weston_seat *seat)
559{
560 struct evdev_device *device;
561
562 device = zalloc(sizeof *device);
563 if (device == NULL)
564 return NULL;
565
566 device->seat = seat;
567 wl_list_init(&device->link);
568 device->device = libinput_device;
569
570 if (libinput_device_has_capability(libinput_device,
571 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
572 weston_seat_init_keyboard(seat, NULL);
573 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
574 }
575 if (libinput_device_has_capability(libinput_device,
576 LIBINPUT_DEVICE_CAP_POINTER)) {
577 weston_seat_init_pointer(seat);
578 device->seat_caps |= EVDEV_SEAT_POINTER;
579 }
580 if (libinput_device_has_capability(libinput_device,
581 LIBINPUT_DEVICE_CAP_TOUCH)) {
582 weston_seat_init_touch(seat);
583 device->seat_caps |= EVDEV_SEAT_TOUCH;
584 }
585
586 libinput_device_set_user_data(libinput_device, device);
587 libinput_device_ref(libinput_device);
588
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200589 configure_device(device);
590
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100591 return device;
592}
593
594void
595evdev_device_destroy(struct evdev_device *device)
596{
597 if (device->seat_caps & EVDEV_SEAT_POINTER)
598 weston_seat_release_pointer(device->seat);
599 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
600 weston_seat_release_keyboard(device->seat);
601 if (device->seat_caps & EVDEV_SEAT_TOUCH)
602 weston_seat_release_touch(device->seat);
603
604 if (device->output)
605 wl_list_remove(&device->output_destroy_listener.link);
606 wl_list_remove(&device->link);
607 libinput_device_unref(device->device);
608 free(device->devnode);
609 free(device->output_name);
610 free(device);
611}
612
613void
614evdev_notify_keyboard_focus(struct weston_seat *seat,
615 struct wl_list *evdev_devices)
616{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100617 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100618
Derek Foremand621df22014-11-19 11:04:12 -0600619 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100620 return;
621
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100622 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100623 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100624 wl_array_release(&keys);
625}