blob: 99b2916f136e8a06c92d562325fffacd8427493b [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),
157 libinput_event_pointer_get_button_state(pointer_event));
Peter Hutterer87743e92016-01-18 16:38:22 +1000158 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100159}
160
Peter Hutterer5dddd412015-01-15 13:14:43 +1000161static double
162normalize_scroll(struct libinput_event_pointer *pointer_event,
163 enum libinput_pointer_axis axis)
164{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000165 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000166 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000167
168 source = libinput_event_pointer_get_axis_source(pointer_event);
169 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
170 the value is the angle of the click in degrees. To keep
171 backwards-compat with existing clients, we just send multiples of
172 the click count.
173 */
174 switch (source) {
175 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
176 value = 10 * libinput_event_pointer_get_axis_value_discrete(
177 pointer_event,
178 axis);
179 break;
180 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
181 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
182 value = libinput_event_pointer_get_axis_value(pointer_event,
183 axis);
184 break;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000185 }
186
187 return value;
188}
189
Peter Hutterer87743e92016-01-18 16:38:22 +1000190static int32_t
191get_axis_discrete(struct libinput_event_pointer *pointer_event,
192 enum libinput_pointer_axis axis)
193{
194 enum libinput_pointer_axis_source source;
195
196 source = libinput_event_pointer_get_axis_source(pointer_event);
197
198 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
199 return 0;
200
201 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
202 axis);
203}
204
205static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100206handle_pointer_axis(struct libinput_device *libinput_device,
207 struct libinput_event_pointer *pointer_event)
208{
Peter Hutterer87743e92016-01-18 16:38:22 +1000209 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100210 struct evdev_device *device =
211 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000212 double vert, horiz;
213 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000214 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000215 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000216 enum libinput_pointer_axis_source source;
217 uint32_t wl_axis_source;
218 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100219
Peter Hutterer87743e92016-01-18 16:38:22 +1000220 has_vert = libinput_event_pointer_has_axis(pointer_event,
221 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
222 has_horiz = libinput_event_pointer_has_axis(pointer_event,
223 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
224
225 if (!has_vert && !has_horiz)
226 return false;
227
228 source = libinput_event_pointer_get_axis_source(pointer_event);
229 switch (source) {
230 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
231 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
232 break;
233 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
234 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
235 break;
236 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
237 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
238 break;
239 default:
240 if (warned < 5) {
241 weston_log("Unknown scroll source %d.\n", source);
242 warned++;
243 }
244 return false;
245 }
246
247 notify_axis_source(device->seat, wl_axis_source);
248
249 if (has_vert) {
250 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
251 vert_discrete = get_axis_discrete(pointer_event, axis);
252 vert = normalize_scroll(pointer_event, axis);
253
Peter Hutterer89b6a492016-01-18 15:58:17 +1000254 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Peter Hutterer87743e92016-01-18 16:38:22 +1000255 weston_event.value = wl_fixed_from_double(vert);
256 weston_event.discrete = vert_discrete;
257 weston_event.has_discrete = (vert_discrete != 0);
258
Peter Huttererc54f23d2015-01-13 11:55:37 +1000259 notify_axis(device->seat,
260 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000261 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000262 }
263
Peter Hutterer87743e92016-01-18 16:38:22 +1000264 if (has_horiz) {
265 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
266 horiz_discrete = get_axis_discrete(pointer_event, axis);
267 horiz = normalize_scroll(pointer_event, axis);
268
Peter Hutterer89b6a492016-01-18 15:58:17 +1000269 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Peter Hutterer87743e92016-01-18 16:38:22 +1000270 weston_event.value = wl_fixed_from_double(horiz);
271 weston_event.discrete = horiz_discrete;
272 weston_event.has_discrete = (horiz_discrete != 0);
273
Peter Huttererc54f23d2015-01-13 11:55:37 +1000274 notify_axis(device->seat,
275 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000276 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000277 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000278
279 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100280}
281
282static void
283handle_touch_with_coords(struct libinput_device *libinput_device,
284 struct libinput_event_touch *touch_event,
285 int touch_type)
286{
287 struct evdev_device *device =
288 libinput_device_get_user_data(libinput_device);
289 wl_fixed_t x;
290 wl_fixed_t y;
291 uint32_t width, height;
292 uint32_t time;
293 int32_t slot;
294
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300295 if (!device->output)
296 return;
297
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100298 time = libinput_event_touch_get_time(touch_event);
299 slot = libinput_event_touch_get_seat_slot(touch_event);
300
301 width = device->output->current_mode->width;
302 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200303 x = wl_fixed_from_double(
304 libinput_event_touch_get_x_transformed(touch_event, width));
305 y = wl_fixed_from_double(
306 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100307
308 weston_output_transform_coordinate(device->output,
309 x, y, &x, &y);
310
311 notify_touch(device->seat, time, slot, x, y, touch_type);
312}
313
314static void
315handle_touch_down(struct libinput_device *device,
316 struct libinput_event_touch *touch_event)
317{
318 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
319}
320
321static void
322handle_touch_motion(struct libinput_device *device,
323 struct libinput_event_touch *touch_event)
324{
325 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
326}
327
328static void
329handle_touch_up(struct libinput_device *libinput_device,
330 struct libinput_event_touch *touch_event)
331{
332 struct evdev_device *device =
333 libinput_device_get_user_data(libinput_device);
334 uint32_t time = libinput_event_touch_get_time(touch_event);
335 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
336
337 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
338}
339
Jonas Ådahl1679f232014-04-12 09:39:51 +0200340static void
341handle_touch_frame(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 struct weston_seat *seat = device->seat;
347
348 notify_touch_frame(seat);
349}
350
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100351int
352evdev_device_process_event(struct libinput_event *event)
353{
354 struct libinput_device *libinput_device =
355 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000356 struct evdev_device *device =
357 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100358 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000359 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100360
361 switch (libinput_event_get_type(event)) {
362 case LIBINPUT_EVENT_KEYBOARD_KEY:
363 handle_keyboard_key(libinput_device,
364 libinput_event_get_keyboard_event(event));
365 break;
366 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000367 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100368 libinput_event_get_pointer_event(event));
369 break;
370 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000371 need_frame = handle_pointer_motion_absolute(
372 libinput_device,
373 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100374 break;
375 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000376 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100377 libinput_event_get_pointer_event(event));
378 break;
379 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000380 need_frame = handle_pointer_axis(
381 libinput_device,
382 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100383 break;
384 case LIBINPUT_EVENT_TOUCH_DOWN:
385 handle_touch_down(libinput_device,
386 libinput_event_get_touch_event(event));
387 break;
388 case LIBINPUT_EVENT_TOUCH_MOTION:
389 handle_touch_motion(libinput_device,
390 libinput_event_get_touch_event(event));
391 break;
392 case LIBINPUT_EVENT_TOUCH_UP:
393 handle_touch_up(libinput_device,
394 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700395 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200396 case LIBINPUT_EVENT_TOUCH_FRAME:
397 handle_touch_frame(libinput_device,
398 libinput_event_get_touch_event(event));
399 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100400 default:
401 handled = 0;
402 weston_log("unknown libinput event %d\n",
403 libinput_event_get_type(event));
404 }
405
Peter Hutterer87743e92016-01-18 16:38:22 +1000406 if (need_frame)
407 notify_pointer_frame(device->seat);
408
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100409 return handled;
410}
411
412static void
413notify_output_destroy(struct wl_listener *listener, void *data)
414{
415 struct evdev_device *device =
416 container_of(listener,
417 struct evdev_device, output_destroy_listener);
418 struct weston_compositor *c = device->seat->compositor;
419 struct weston_output *output;
420
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300421 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100422 output = container_of(c->output_list.next,
423 struct weston_output, link);
424 evdev_device_set_output(device, output);
425 } else {
426 device->output = NULL;
427 }
428}
429
Peter Hutterer3fbba492014-09-09 13:02:25 +1000430/**
431 * The WL_CALIBRATION property requires a pixel-specific matrix to be
432 * applied after scaling device coordinates to screen coordinates. libinput
433 * can't do that, so we need to convert the calibration to the normalized
434 * format libinput expects.
435 */
436static void
437evdev_device_set_calibration(struct evdev_device *device)
438{
439 struct udev *udev;
440 struct udev_device *udev_device = NULL;
441 const char *sysname = libinput_device_get_sysname(device->device);
442 const char *calibration_values;
443 uint32_t width, height;
444 float calibration[6];
445 enum libinput_config_status status;
446
447 if (!device->output)
448 return;
449
450 width = device->output->width;
451 height = device->output->height;
452 if (width == 0 || height == 0)
453 return;
454
455 /* If libinput has a pre-set calibration matrix, don't override it */
456 if (!libinput_device_config_calibration_has_matrix(device->device) ||
457 libinput_device_config_calibration_get_default_matrix(
458 device->device,
459 calibration) != 0)
460 return;
461
462 udev = udev_new();
463 if (!udev)
464 return;
465
466 udev_device = udev_device_new_from_subsystem_sysname(udev,
467 "input",
468 sysname);
469 if (!udev_device)
470 goto out;
471
472 calibration_values =
473 udev_device_get_property_value(udev_device,
474 "WL_CALIBRATION");
475
476 if (!calibration_values || sscanf(calibration_values,
477 "%f %f %f %f %f %f",
478 &calibration[0],
479 &calibration[1],
480 &calibration[2],
481 &calibration[3],
482 &calibration[4],
483 &calibration[5]) != 6)
484 goto out;
485
486 weston_log("Applying calibration: %f %f %f %f %f %f "
487 "(normalized %f %f)\n",
488 calibration[0],
489 calibration[1],
490 calibration[2],
491 calibration[3],
492 calibration[4],
493 calibration[5],
494 calibration[2] / width,
495 calibration[5] / height);
496
497 /* normalize to a format libinput can use. There is a chance of
498 this being wrong if the width/height don't match the device
499 width/height but I'm not sure how to fix that */
500 calibration[2] /= width;
501 calibration[5] /= height;
502
503 status = libinput_device_config_calibration_set_matrix(device->device,
504 calibration);
505 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
506 weston_log("Failed to apply calibration.\n");
507
508out:
509 if (udev_device)
510 udev_device_unref(udev_device);
511 udev_unref(udev);
512}
513
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100514void
515evdev_device_set_output(struct evdev_device *device,
516 struct weston_output *output)
517{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700518 if (device->output_destroy_listener.notify) {
519 wl_list_remove(&device->output_destroy_listener.link);
520 device->output_destroy_listener.notify = NULL;
521 }
522
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100523 device->output = output;
524 device->output_destroy_listener.notify = notify_output_destroy;
525 wl_signal_add(&output->destroy_signal,
526 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000527 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100528}
529
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200530static void
531configure_device(struct evdev_device *device)
532{
533 struct weston_compositor *compositor = device->seat->compositor;
534 struct weston_config_section *s;
535 int enable_tap;
536 int enable_tap_default;
537
538 s = weston_config_get_section(compositor->config,
539 "libinput", NULL, NULL);
540
541 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
542 enable_tap_default =
543 libinput_device_config_tap_get_default_enabled(
544 device->device);
545 weston_config_section_get_bool(s, "enable_tap",
546 &enable_tap,
547 enable_tap_default);
548 libinput_device_config_tap_set_enabled(device->device,
549 enable_tap);
550 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000551
552 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200553}
554
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100555struct evdev_device *
556evdev_device_create(struct libinput_device *libinput_device,
557 struct weston_seat *seat)
558{
559 struct evdev_device *device;
560
561 device = zalloc(sizeof *device);
562 if (device == NULL)
563 return NULL;
564
565 device->seat = seat;
566 wl_list_init(&device->link);
567 device->device = libinput_device;
568
569 if (libinput_device_has_capability(libinput_device,
570 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
571 weston_seat_init_keyboard(seat, NULL);
572 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
573 }
574 if (libinput_device_has_capability(libinput_device,
575 LIBINPUT_DEVICE_CAP_POINTER)) {
576 weston_seat_init_pointer(seat);
577 device->seat_caps |= EVDEV_SEAT_POINTER;
578 }
579 if (libinput_device_has_capability(libinput_device,
580 LIBINPUT_DEVICE_CAP_TOUCH)) {
581 weston_seat_init_touch(seat);
582 device->seat_caps |= EVDEV_SEAT_TOUCH;
583 }
584
585 libinput_device_set_user_data(libinput_device, device);
586 libinput_device_ref(libinput_device);
587
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200588 configure_device(device);
589
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100590 return device;
591}
592
593void
594evdev_device_destroy(struct evdev_device *device)
595{
596 if (device->seat_caps & EVDEV_SEAT_POINTER)
597 weston_seat_release_pointer(device->seat);
598 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
599 weston_seat_release_keyboard(device->seat);
600 if (device->seat_caps & EVDEV_SEAT_TOUCH)
601 weston_seat_release_touch(device->seat);
602
603 if (device->output)
604 wl_list_remove(&device->output_destroy_listener.link);
605 wl_list_remove(&device->link);
606 libinput_device_unref(device->device);
607 free(device->devnode);
608 free(device->output_name);
609 free(device);
610}
611
612void
613evdev_notify_keyboard_focus(struct weston_seat *seat,
614 struct wl_list *evdev_devices)
615{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100616 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100617
Derek Foremand621df22014-11-19 11:04:12 -0600618 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100619 return;
620
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100621 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100622 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100623 wl_array_release(&keys);
624}