blob: a67c1199847aa1a6035477644d87892dfb748c54 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <errno.h>
27#include <stdlib.h>
28#include <string.h>
29#include <linux/input.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <mtdev.h>
33#include <assert.h>
34#include <libinput.h>
35
36#include "compositor.h"
37#include "libinput-device.h"
38
39#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
40
41void
42evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
43{
44 enum libinput_led leds = 0;
45
46 if (weston_leds & LED_NUM_LOCK)
47 leds |= LIBINPUT_LED_NUM_LOCK;
48 if (weston_leds & LED_CAPS_LOCK)
49 leds |= LIBINPUT_LED_CAPS_LOCK;
50 if (weston_leds & LED_SCROLL_LOCK)
51 leds |= LIBINPUT_LED_SCROLL_LOCK;
52
53 libinput_device_led_update(device->device, leds);
54}
55
56static void
57handle_keyboard_key(struct libinput_device *libinput_device,
58 struct libinput_event_keyboard *keyboard_event)
59{
60 struct evdev_device *device =
61 libinput_device_get_user_data(libinput_device);
62
63 notify_key(device->seat,
64 libinput_event_keyboard_get_time(keyboard_event),
65 libinput_event_keyboard_get_key(keyboard_event),
66 libinput_event_keyboard_get_key_state(keyboard_event),
67 STATE_UPDATE_AUTOMATIC);
68}
69
70static void
71handle_pointer_motion(struct libinput_device *libinput_device,
72 struct libinput_event_pointer *pointer_event)
73{
74 struct evdev_device *device =
75 libinput_device_get_user_data(libinput_device);
76
77 notify_motion(device->seat,
78 libinput_event_pointer_get_time(pointer_event),
79 libinput_event_pointer_get_dx(pointer_event),
80 libinput_event_pointer_get_dy(pointer_event));
81}
82
83static void
84handle_pointer_motion_absolute(
85 struct libinput_device *libinput_device,
86 struct libinput_event_pointer *pointer_event)
87{
88 struct evdev_device *device =
89 libinput_device_get_user_data(libinput_device);
90 struct weston_output *output = device->output;
91 uint32_t time;
92 wl_fixed_t x, y;
93 uint32_t width, height;
94
95 if (!output)
96 return;
97
98 time = libinput_event_pointer_get_time(pointer_event);
99 width = device->output->current_mode->width;
100 height = device->output->current_mode->height;
101
102 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
103 width);
104 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
105 height);
106
107 weston_output_transform_coordinate(device->output, x, y, &x, &y);
108 notify_motion_absolute(device->seat, time, x, y);
109}
110
111static void
112handle_pointer_button(struct libinput_device *libinput_device,
113 struct libinput_event_pointer *pointer_event)
114{
115 struct evdev_device *device =
116 libinput_device_get_user_data(libinput_device);
117
118 notify_button(device->seat,
119 libinput_event_pointer_get_time(pointer_event),
120 libinput_event_pointer_get_button(pointer_event),
121 libinput_event_pointer_get_button_state(pointer_event));
122}
123
124static void
125handle_pointer_axis(struct libinput_device *libinput_device,
126 struct libinput_event_pointer *pointer_event)
127{
128 struct evdev_device *device =
129 libinput_device_get_user_data(libinput_device);
130
131 notify_axis(device->seat,
132 libinput_event_pointer_get_time(pointer_event),
133 libinput_event_pointer_get_axis(pointer_event),
134 libinput_event_pointer_get_axis_value(pointer_event));
135}
136
137static void
138handle_touch_with_coords(struct libinput_device *libinput_device,
139 struct libinput_event_touch *touch_event,
140 int touch_type)
141{
142 struct evdev_device *device =
143 libinput_device_get_user_data(libinput_device);
144 wl_fixed_t x;
145 wl_fixed_t y;
146 uint32_t width, height;
147 uint32_t time;
148 int32_t slot;
149
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300150 if (!device->output)
151 return;
152
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100153 time = libinput_event_touch_get_time(touch_event);
154 slot = libinput_event_touch_get_seat_slot(touch_event);
155
156 width = device->output->current_mode->width;
157 height = device->output->current_mode->height;
158 x = libinput_event_touch_get_x_transformed(touch_event, width);
159 y = libinput_event_touch_get_y_transformed(touch_event, height);
160
161 weston_output_transform_coordinate(device->output,
162 x, y, &x, &y);
163
164 notify_touch(device->seat, time, slot, x, y, touch_type);
165}
166
167static void
168handle_touch_down(struct libinput_device *device,
169 struct libinput_event_touch *touch_event)
170{
171 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
172}
173
174static void
175handle_touch_motion(struct libinput_device *device,
176 struct libinput_event_touch *touch_event)
177{
178 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
179}
180
181static void
182handle_touch_up(struct libinput_device *libinput_device,
183 struct libinput_event_touch *touch_event)
184{
185 struct evdev_device *device =
186 libinput_device_get_user_data(libinput_device);
187 uint32_t time = libinput_event_touch_get_time(touch_event);
188 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
189
190 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
191}
192
Jonas Ådahl1679f232014-04-12 09:39:51 +0200193static void
194handle_touch_frame(struct libinput_device *libinput_device,
195 struct libinput_event_touch *touch_event)
196{
197 struct evdev_device *device =
198 libinput_device_get_user_data(libinput_device);
199 struct weston_seat *seat = device->seat;
200
201 notify_touch_frame(seat);
202}
203
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100204int
205evdev_device_process_event(struct libinput_event *event)
206{
207 struct libinput_device *libinput_device =
208 libinput_event_get_device(event);
209 int handled = 1;
210
211 switch (libinput_event_get_type(event)) {
212 case LIBINPUT_EVENT_KEYBOARD_KEY:
213 handle_keyboard_key(libinput_device,
214 libinput_event_get_keyboard_event(event));
215 break;
216 case LIBINPUT_EVENT_POINTER_MOTION:
217 handle_pointer_motion(libinput_device,
218 libinput_event_get_pointer_event(event));
219 break;
220 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
221 handle_pointer_motion_absolute(
222 libinput_device,
223 libinput_event_get_pointer_event(event));
224 break;
225 case LIBINPUT_EVENT_POINTER_BUTTON:
226 handle_pointer_button(libinput_device,
227 libinput_event_get_pointer_event(event));
228 break;
229 case LIBINPUT_EVENT_POINTER_AXIS:
230 handle_pointer_axis(libinput_device,
231 libinput_event_get_pointer_event(event));
232 break;
233 case LIBINPUT_EVENT_TOUCH_DOWN:
234 handle_touch_down(libinput_device,
235 libinput_event_get_touch_event(event));
236 break;
237 case LIBINPUT_EVENT_TOUCH_MOTION:
238 handle_touch_motion(libinput_device,
239 libinput_event_get_touch_event(event));
240 break;
241 case LIBINPUT_EVENT_TOUCH_UP:
242 handle_touch_up(libinput_device,
243 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700244 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200245 case LIBINPUT_EVENT_TOUCH_FRAME:
246 handle_touch_frame(libinput_device,
247 libinput_event_get_touch_event(event));
248 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100249 default:
250 handled = 0;
251 weston_log("unknown libinput event %d\n",
252 libinput_event_get_type(event));
253 }
254
255 return handled;
256}
257
258static void
259notify_output_destroy(struct wl_listener *listener, void *data)
260{
261 struct evdev_device *device =
262 container_of(listener,
263 struct evdev_device, output_destroy_listener);
264 struct weston_compositor *c = device->seat->compositor;
265 struct weston_output *output;
266
267 if (device->output_name) {
268 output = container_of(c->output_list.next,
269 struct weston_output, link);
270 evdev_device_set_output(device, output);
271 } else {
272 device->output = NULL;
273 }
274}
275
276void
277evdev_device_set_output(struct evdev_device *device,
278 struct weston_output *output)
279{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700280 if (device->output_destroy_listener.notify) {
281 wl_list_remove(&device->output_destroy_listener.link);
282 device->output_destroy_listener.notify = NULL;
283 }
284
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100285 device->output = output;
286 device->output_destroy_listener.notify = notify_output_destroy;
287 wl_signal_add(&output->destroy_signal,
288 &device->output_destroy_listener);
289}
290
291struct evdev_device *
292evdev_device_create(struct libinput_device *libinput_device,
293 struct weston_seat *seat)
294{
295 struct evdev_device *device;
296
297 device = zalloc(sizeof *device);
298 if (device == NULL)
299 return NULL;
300
301 device->seat = seat;
302 wl_list_init(&device->link);
303 device->device = libinput_device;
304
305 if (libinput_device_has_capability(libinput_device,
306 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
307 weston_seat_init_keyboard(seat, NULL);
308 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
309 }
310 if (libinput_device_has_capability(libinput_device,
311 LIBINPUT_DEVICE_CAP_POINTER)) {
312 weston_seat_init_pointer(seat);
313 device->seat_caps |= EVDEV_SEAT_POINTER;
314 }
315 if (libinput_device_has_capability(libinput_device,
316 LIBINPUT_DEVICE_CAP_TOUCH)) {
317 weston_seat_init_touch(seat);
318 device->seat_caps |= EVDEV_SEAT_TOUCH;
319 }
320
321 libinput_device_set_user_data(libinput_device, device);
322 libinput_device_ref(libinput_device);
323
324 return device;
325}
326
327void
328evdev_device_destroy(struct evdev_device *device)
329{
330 if (device->seat_caps & EVDEV_SEAT_POINTER)
331 weston_seat_release_pointer(device->seat);
332 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
333 weston_seat_release_keyboard(device->seat);
334 if (device->seat_caps & EVDEV_SEAT_TOUCH)
335 weston_seat_release_touch(device->seat);
336
337 if (device->output)
338 wl_list_remove(&device->output_destroy_listener.link);
339 wl_list_remove(&device->link);
340 libinput_device_unref(device->device);
341 free(device->devnode);
342 free(device->output_name);
343 free(device);
344}
345
346void
347evdev_notify_keyboard_focus(struct weston_seat *seat,
348 struct wl_list *evdev_devices)
349{
350 struct evdev_device *device;
351 struct wl_array keys;
352 unsigned int i, set;
353 char evdev_keys[(KEY_CNT + 7) / 8];
354 char all_keys[(KEY_CNT + 7) / 8];
355 uint32_t *k;
356 int ret;
357
358 if (!seat->keyboard_device_count > 0)
359 return;
360
361 memset(all_keys, 0, sizeof all_keys);
362 wl_list_for_each(device, evdev_devices, link) {
363 memset(evdev_keys, 0, sizeof evdev_keys);
364 ret = libinput_device_get_keys(device->device,
365 evdev_keys,
366 sizeof evdev_keys);
367 if (ret < 0) {
368 weston_log("failed to get keys for device %s\n",
369 device->devnode);
370 continue;
371 }
372 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
373 all_keys[i] |= evdev_keys[i];
374 }
375
376 wl_array_init(&keys);
377 for (i = 0; i < KEY_CNT; i++) {
378 set = all_keys[i >> 3] & (1 << (i & 7));
379 if (set) {
380 k = wl_array_add(&keys, sizeof *k);
381 *k = i;
382 }
383 }
384
385 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
386
387 wl_array_release(&keys);
388}