blob: 62350f2e41112f88e34df4d72600693b5672b9e8 [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),
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 Ådahle0de3c22014-03-12 22:08:42 +010089
Jonas Ådahld2510102014-10-05 21:39:14 +020090 event = (struct weston_pointer_motion_event) {
91 .mask = WESTON_POINTER_MOTION_REL,
92 .dx = libinput_event_pointer_get_dx(pointer_event),
93 .dy = libinput_event_pointer_get_dy(pointer_event),
94 };
95
Jonas Ådahle0de3c22014-03-12 22:08:42 +010096 notify_motion(device->seat,
97 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahld2510102014-10-05 21:39:14 +020098 &event);
Peter Hutterer87743e92016-01-18 16:38:22 +100099
100 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100101}
102
Peter Hutterer87743e92016-01-18 16:38:22 +1000103static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100104handle_pointer_motion_absolute(
105 struct libinput_device *libinput_device,
106 struct libinput_event_pointer *pointer_event)
107{
108 struct evdev_device *device =
109 libinput_device_get_user_data(libinput_device);
110 struct weston_output *output = device->output;
111 uint32_t time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200112 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100113 uint32_t width, height;
114
115 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000116 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100117
118 time = libinput_event_pointer_get_time(pointer_event);
119 width = device->output->current_mode->width;
120 height = device->output->current_mode->height;
121
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200122 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
123 width);
124 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
125 height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100126
127 weston_output_transform_coordinate(device->output, x, y, &x, &y);
128 notify_motion_absolute(device->seat, time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000129
130 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100131}
132
Peter Hutterer87743e92016-01-18 16:38:22 +1000133static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100134handle_pointer_button(struct libinput_device *libinput_device,
135 struct libinput_event_pointer *pointer_event)
136{
137 struct evdev_device *device =
138 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800139 int button_state =
140 libinput_event_pointer_get_button_state(pointer_event);
141 int seat_button_count =
142 libinput_event_pointer_get_seat_button_count(pointer_event);
143
144 /* Ignore button events that are not seat wide state changes. */
145 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
146 seat_button_count != 1) ||
147 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
148 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000149 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100150
151 notify_button(device->seat,
152 libinput_event_pointer_get_time(pointer_event),
153 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500154 button_state);
155
Peter Hutterer87743e92016-01-18 16:38:22 +1000156 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100157}
158
Peter Hutterer5dddd412015-01-15 13:14:43 +1000159static double
160normalize_scroll(struct libinput_event_pointer *pointer_event,
161 enum libinput_pointer_axis axis)
162{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000163 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000164 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000165
166 source = libinput_event_pointer_get_axis_source(pointer_event);
167 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
168 the value is the angle of the click in degrees. To keep
169 backwards-compat with existing clients, we just send multiples of
170 the click count.
171 */
172 switch (source) {
173 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
174 value = 10 * libinput_event_pointer_get_axis_value_discrete(
175 pointer_event,
176 axis);
177 break;
178 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
179 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
180 value = libinput_event_pointer_get_axis_value(pointer_event,
181 axis);
182 break;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000183 }
184
185 return value;
186}
187
Peter Hutterer87743e92016-01-18 16:38:22 +1000188static int32_t
189get_axis_discrete(struct libinput_event_pointer *pointer_event,
190 enum libinput_pointer_axis axis)
191{
192 enum libinput_pointer_axis_source source;
193
194 source = libinput_event_pointer_get_axis_source(pointer_event);
195
196 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
197 return 0;
198
199 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
200 axis);
201}
202
203static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100204handle_pointer_axis(struct libinput_device *libinput_device,
205 struct libinput_event_pointer *pointer_event)
206{
Peter Hutterer87743e92016-01-18 16:38:22 +1000207 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100208 struct evdev_device *device =
209 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000210 double vert, horiz;
211 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000212 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000213 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000214 enum libinput_pointer_axis_source source;
215 uint32_t wl_axis_source;
216 bool has_vert, has_horiz;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100217
Peter Hutterer87743e92016-01-18 16:38:22 +1000218 has_vert = libinput_event_pointer_has_axis(pointer_event,
219 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
220 has_horiz = libinput_event_pointer_has_axis(pointer_event,
221 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
222
223 if (!has_vert && !has_horiz)
224 return false;
225
226 source = libinput_event_pointer_get_axis_source(pointer_event);
227 switch (source) {
228 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
229 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
230 break;
231 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
232 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
233 break;
234 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
235 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
236 break;
237 default:
238 if (warned < 5) {
239 weston_log("Unknown scroll source %d.\n", source);
240 warned++;
241 }
242 return false;
243 }
244
245 notify_axis_source(device->seat, wl_axis_source);
246
247 if (has_vert) {
248 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
249 vert_discrete = get_axis_discrete(pointer_event, axis);
250 vert = normalize_scroll(pointer_event, axis);
251
Peter Hutterer89b6a492016-01-18 15:58:17 +1000252 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200253 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000254 weston_event.discrete = vert_discrete;
255 weston_event.has_discrete = (vert_discrete != 0);
256
Peter Huttererc54f23d2015-01-13 11:55:37 +1000257 notify_axis(device->seat,
258 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000259 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000260 }
261
Peter Hutterer87743e92016-01-18 16:38:22 +1000262 if (has_horiz) {
263 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
264 horiz_discrete = get_axis_discrete(pointer_event, axis);
265 horiz = normalize_scroll(pointer_event, axis);
266
Peter Hutterer89b6a492016-01-18 15:58:17 +1000267 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200268 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000269 weston_event.discrete = horiz_discrete;
270 weston_event.has_discrete = (horiz_discrete != 0);
271
Peter Huttererc54f23d2015-01-13 11:55:37 +1000272 notify_axis(device->seat,
273 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000274 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000275 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000276
277 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100278}
279
280static void
281handle_touch_with_coords(struct libinput_device *libinput_device,
282 struct libinput_event_touch *touch_event,
283 int touch_type)
284{
285 struct evdev_device *device =
286 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200287 double x;
288 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100289 uint32_t width, height;
290 uint32_t time;
291 int32_t slot;
292
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300293 if (!device->output)
294 return;
295
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100296 time = libinput_event_touch_get_time(touch_event);
297 slot = libinput_event_touch_get_seat_slot(touch_event);
298
299 width = device->output->current_mode->width;
300 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200301 x = libinput_event_touch_get_x_transformed(touch_event, width);
302 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100303
304 weston_output_transform_coordinate(device->output,
305 x, y, &x, &y);
306
307 notify_touch(device->seat, time, slot, x, y, touch_type);
308}
309
310static void
311handle_touch_down(struct libinput_device *device,
312 struct libinput_event_touch *touch_event)
313{
314 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
315}
316
317static void
318handle_touch_motion(struct libinput_device *device,
319 struct libinput_event_touch *touch_event)
320{
321 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
322}
323
324static void
325handle_touch_up(struct libinput_device *libinput_device,
326 struct libinput_event_touch *touch_event)
327{
328 struct evdev_device *device =
329 libinput_device_get_user_data(libinput_device);
330 uint32_t time = libinput_event_touch_get_time(touch_event);
331 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
332
333 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
334}
335
Jonas Ådahl1679f232014-04-12 09:39:51 +0200336static void
337handle_touch_frame(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 struct weston_seat *seat = device->seat;
343
344 notify_touch_frame(seat);
345}
346
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100347int
348evdev_device_process_event(struct libinput_event *event)
349{
350 struct libinput_device *libinput_device =
351 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000352 struct evdev_device *device =
353 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100354 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000355 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100356
357 switch (libinput_event_get_type(event)) {
358 case LIBINPUT_EVENT_KEYBOARD_KEY:
359 handle_keyboard_key(libinput_device,
360 libinput_event_get_keyboard_event(event));
361 break;
362 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000363 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100364 libinput_event_get_pointer_event(event));
365 break;
366 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000367 need_frame = handle_pointer_motion_absolute(
368 libinput_device,
369 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100370 break;
371 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000372 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100373 libinput_event_get_pointer_event(event));
374 break;
375 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000376 need_frame = handle_pointer_axis(
377 libinput_device,
378 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100379 break;
380 case LIBINPUT_EVENT_TOUCH_DOWN:
381 handle_touch_down(libinput_device,
382 libinput_event_get_touch_event(event));
383 break;
384 case LIBINPUT_EVENT_TOUCH_MOTION:
385 handle_touch_motion(libinput_device,
386 libinput_event_get_touch_event(event));
387 break;
388 case LIBINPUT_EVENT_TOUCH_UP:
389 handle_touch_up(libinput_device,
390 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700391 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200392 case LIBINPUT_EVENT_TOUCH_FRAME:
393 handle_touch_frame(libinput_device,
394 libinput_event_get_touch_event(event));
395 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100396 default:
397 handled = 0;
398 weston_log("unknown libinput event %d\n",
399 libinput_event_get_type(event));
400 }
401
Peter Hutterer87743e92016-01-18 16:38:22 +1000402 if (need_frame)
403 notify_pointer_frame(device->seat);
404
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100405 return handled;
406}
407
408static void
409notify_output_destroy(struct wl_listener *listener, void *data)
410{
411 struct evdev_device *device =
412 container_of(listener,
413 struct evdev_device, output_destroy_listener);
414 struct weston_compositor *c = device->seat->compositor;
415 struct weston_output *output;
416
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300417 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100418 output = container_of(c->output_list.next,
419 struct weston_output, link);
420 evdev_device_set_output(device, output);
421 } else {
422 device->output = NULL;
423 }
424}
425
Peter Hutterer3fbba492014-09-09 13:02:25 +1000426/**
427 * The WL_CALIBRATION property requires a pixel-specific matrix to be
428 * applied after scaling device coordinates to screen coordinates. libinput
429 * can't do that, so we need to convert the calibration to the normalized
430 * format libinput expects.
431 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300432void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000433evdev_device_set_calibration(struct evdev_device *device)
434{
435 struct udev *udev;
436 struct udev_device *udev_device = NULL;
437 const char *sysname = libinput_device_get_sysname(device->device);
438 const char *calibration_values;
439 uint32_t width, height;
440 float calibration[6];
441 enum libinput_config_status status;
442
443 if (!device->output)
444 return;
445
446 width = device->output->width;
447 height = device->output->height;
448 if (width == 0 || height == 0)
449 return;
450
451 /* If libinput has a pre-set calibration matrix, don't override it */
452 if (!libinput_device_config_calibration_has_matrix(device->device) ||
453 libinput_device_config_calibration_get_default_matrix(
454 device->device,
455 calibration) != 0)
456 return;
457
458 udev = udev_new();
459 if (!udev)
460 return;
461
462 udev_device = udev_device_new_from_subsystem_sysname(udev,
463 "input",
464 sysname);
465 if (!udev_device)
466 goto out;
467
468 calibration_values =
469 udev_device_get_property_value(udev_device,
470 "WL_CALIBRATION");
471
472 if (!calibration_values || sscanf(calibration_values,
473 "%f %f %f %f %f %f",
474 &calibration[0],
475 &calibration[1],
476 &calibration[2],
477 &calibration[3],
478 &calibration[4],
479 &calibration[5]) != 6)
480 goto out;
481
482 weston_log("Applying calibration: %f %f %f %f %f %f "
483 "(normalized %f %f)\n",
484 calibration[0],
485 calibration[1],
486 calibration[2],
487 calibration[3],
488 calibration[4],
489 calibration[5],
490 calibration[2] / width,
491 calibration[5] / height);
492
493 /* normalize to a format libinput can use. There is a chance of
494 this being wrong if the width/height don't match the device
495 width/height but I'm not sure how to fix that */
496 calibration[2] /= width;
497 calibration[5] /= height;
498
499 status = libinput_device_config_calibration_set_matrix(device->device,
500 calibration);
501 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
502 weston_log("Failed to apply calibration.\n");
503
504out:
505 if (udev_device)
506 udev_device_unref(udev_device);
507 udev_unref(udev);
508}
509
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100510void
511evdev_device_set_output(struct evdev_device *device,
512 struct weston_output *output)
513{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700514 if (device->output_destroy_listener.notify) {
515 wl_list_remove(&device->output_destroy_listener.link);
516 device->output_destroy_listener.notify = NULL;
517 }
518
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100519 device->output = output;
520 device->output_destroy_listener.notify = notify_output_destroy;
521 wl_signal_add(&output->destroy_signal,
522 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000523 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100524}
525
526struct evdev_device *
527evdev_device_create(struct libinput_device *libinput_device,
528 struct weston_seat *seat)
529{
530 struct evdev_device *device;
531
532 device = zalloc(sizeof *device);
533 if (device == NULL)
534 return NULL;
535
536 device->seat = seat;
537 wl_list_init(&device->link);
538 device->device = libinput_device;
539
540 if (libinput_device_has_capability(libinput_device,
541 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
542 weston_seat_init_keyboard(seat, NULL);
543 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
544 }
545 if (libinput_device_has_capability(libinput_device,
546 LIBINPUT_DEVICE_CAP_POINTER)) {
547 weston_seat_init_pointer(seat);
548 device->seat_caps |= EVDEV_SEAT_POINTER;
549 }
550 if (libinput_device_has_capability(libinput_device,
551 LIBINPUT_DEVICE_CAP_TOUCH)) {
552 weston_seat_init_touch(seat);
553 device->seat_caps |= EVDEV_SEAT_TOUCH;
554 }
555
556 libinput_device_set_user_data(libinput_device, device);
557 libinput_device_ref(libinput_device);
558
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100559 return device;
560}
561
562void
563evdev_device_destroy(struct evdev_device *device)
564{
565 if (device->seat_caps & EVDEV_SEAT_POINTER)
566 weston_seat_release_pointer(device->seat);
567 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
568 weston_seat_release_keyboard(device->seat);
569 if (device->seat_caps & EVDEV_SEAT_TOUCH)
570 weston_seat_release_touch(device->seat);
571
572 if (device->output)
573 wl_list_remove(&device->output_destroy_listener.link);
574 wl_list_remove(&device->link);
575 libinput_device_unref(device->device);
576 free(device->devnode);
577 free(device->output_name);
578 free(device);
579}
580
581void
582evdev_notify_keyboard_focus(struct weston_seat *seat,
583 struct wl_list *evdev_devices)
584{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100585 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100586
Derek Foremand621df22014-11-19 11:04:12 -0600587 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100588 return;
589
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100590 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100591 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100592 wl_array_release(&keys);
593}