blob: 0ca6c4ba1eb015a85cf4291e90b2338ccc0b9d72 [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
150 time = libinput_event_touch_get_time(touch_event);
151 slot = libinput_event_touch_get_seat_slot(touch_event);
152
153 width = device->output->current_mode->width;
154 height = device->output->current_mode->height;
155 x = libinput_event_touch_get_x_transformed(touch_event, width);
156 y = libinput_event_touch_get_y_transformed(touch_event, height);
157
158 weston_output_transform_coordinate(device->output,
159 x, y, &x, &y);
160
161 notify_touch(device->seat, time, slot, x, y, touch_type);
162}
163
164static void
165handle_touch_down(struct libinput_device *device,
166 struct libinput_event_touch *touch_event)
167{
168 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
169}
170
171static void
172handle_touch_motion(struct libinput_device *device,
173 struct libinput_event_touch *touch_event)
174{
175 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
176}
177
178static void
179handle_touch_up(struct libinput_device *libinput_device,
180 struct libinput_event_touch *touch_event)
181{
182 struct evdev_device *device =
183 libinput_device_get_user_data(libinput_device);
184 uint32_t time = libinput_event_touch_get_time(touch_event);
185 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
186
187 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
188}
189
Jonas Ådahl1679f232014-04-12 09:39:51 +0200190static void
191handle_touch_frame(struct libinput_device *libinput_device,
192 struct libinput_event_touch *touch_event)
193{
194 struct evdev_device *device =
195 libinput_device_get_user_data(libinput_device);
196 struct weston_seat *seat = device->seat;
197
198 notify_touch_frame(seat);
199}
200
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100201int
202evdev_device_process_event(struct libinput_event *event)
203{
204 struct libinput_device *libinput_device =
205 libinput_event_get_device(event);
206 int handled = 1;
207
208 switch (libinput_event_get_type(event)) {
209 case LIBINPUT_EVENT_KEYBOARD_KEY:
210 handle_keyboard_key(libinput_device,
211 libinput_event_get_keyboard_event(event));
212 break;
213 case LIBINPUT_EVENT_POINTER_MOTION:
214 handle_pointer_motion(libinput_device,
215 libinput_event_get_pointer_event(event));
216 break;
217 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
218 handle_pointer_motion_absolute(
219 libinput_device,
220 libinput_event_get_pointer_event(event));
221 break;
222 case LIBINPUT_EVENT_POINTER_BUTTON:
223 handle_pointer_button(libinput_device,
224 libinput_event_get_pointer_event(event));
225 break;
226 case LIBINPUT_EVENT_POINTER_AXIS:
227 handle_pointer_axis(libinput_device,
228 libinput_event_get_pointer_event(event));
229 break;
230 case LIBINPUT_EVENT_TOUCH_DOWN:
231 handle_touch_down(libinput_device,
232 libinput_event_get_touch_event(event));
233 break;
234 case LIBINPUT_EVENT_TOUCH_MOTION:
235 handle_touch_motion(libinput_device,
236 libinput_event_get_touch_event(event));
237 break;
238 case LIBINPUT_EVENT_TOUCH_UP:
239 handle_touch_up(libinput_device,
240 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700241 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200242 case LIBINPUT_EVENT_TOUCH_FRAME:
243 handle_touch_frame(libinput_device,
244 libinput_event_get_touch_event(event));
245 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100246 default:
247 handled = 0;
248 weston_log("unknown libinput event %d\n",
249 libinput_event_get_type(event));
250 }
251
252 return handled;
253}
254
255static void
256notify_output_destroy(struct wl_listener *listener, void *data)
257{
258 struct evdev_device *device =
259 container_of(listener,
260 struct evdev_device, output_destroy_listener);
261 struct weston_compositor *c = device->seat->compositor;
262 struct weston_output *output;
263
264 if (device->output_name) {
265 output = container_of(c->output_list.next,
266 struct weston_output, link);
267 evdev_device_set_output(device, output);
268 } else {
269 device->output = NULL;
270 }
271}
272
273void
274evdev_device_set_output(struct evdev_device *device,
275 struct weston_output *output)
276{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700277 if (device->output_destroy_listener.notify) {
278 wl_list_remove(&device->output_destroy_listener.link);
279 device->output_destroy_listener.notify = NULL;
280 }
281
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100282 device->output = output;
283 device->output_destroy_listener.notify = notify_output_destroy;
284 wl_signal_add(&output->destroy_signal,
285 &device->output_destroy_listener);
286}
287
288struct evdev_device *
289evdev_device_create(struct libinput_device *libinput_device,
290 struct weston_seat *seat)
291{
292 struct evdev_device *device;
293
294 device = zalloc(sizeof *device);
295 if (device == NULL)
296 return NULL;
297
298 device->seat = seat;
299 wl_list_init(&device->link);
300 device->device = libinput_device;
301
302 if (libinput_device_has_capability(libinput_device,
303 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
304 weston_seat_init_keyboard(seat, NULL);
305 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
306 }
307 if (libinput_device_has_capability(libinput_device,
308 LIBINPUT_DEVICE_CAP_POINTER)) {
309 weston_seat_init_pointer(seat);
310 device->seat_caps |= EVDEV_SEAT_POINTER;
311 }
312 if (libinput_device_has_capability(libinput_device,
313 LIBINPUT_DEVICE_CAP_TOUCH)) {
314 weston_seat_init_touch(seat);
315 device->seat_caps |= EVDEV_SEAT_TOUCH;
316 }
317
318 libinput_device_set_user_data(libinput_device, device);
319 libinput_device_ref(libinput_device);
320
321 return device;
322}
323
324void
325evdev_device_destroy(struct evdev_device *device)
326{
327 if (device->seat_caps & EVDEV_SEAT_POINTER)
328 weston_seat_release_pointer(device->seat);
329 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
330 weston_seat_release_keyboard(device->seat);
331 if (device->seat_caps & EVDEV_SEAT_TOUCH)
332 weston_seat_release_touch(device->seat);
333
334 if (device->output)
335 wl_list_remove(&device->output_destroy_listener.link);
336 wl_list_remove(&device->link);
337 libinput_device_unref(device->device);
338 free(device->devnode);
339 free(device->output_name);
340 free(device);
341}
342
343void
344evdev_notify_keyboard_focus(struct weston_seat *seat,
345 struct wl_list *evdev_devices)
346{
347 struct evdev_device *device;
348 struct wl_array keys;
349 unsigned int i, set;
350 char evdev_keys[(KEY_CNT + 7) / 8];
351 char all_keys[(KEY_CNT + 7) / 8];
352 uint32_t *k;
353 int ret;
354
355 if (!seat->keyboard_device_count > 0)
356 return;
357
358 memset(all_keys, 0, sizeof all_keys);
359 wl_list_for_each(device, evdev_devices, link) {
360 memset(evdev_keys, 0, sizeof evdev_keys);
361 ret = libinput_device_get_keys(device->device,
362 evdev_keys,
363 sizeof evdev_keys);
364 if (ret < 0) {
365 weston_log("failed to get keys for device %s\n",
366 device->devnode);
367 continue;
368 }
369 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
370 all_keys[i] |= evdev_keys[i];
371 }
372
373 wl_array_init(&keys);
374 for (i = 0; i < KEY_CNT; i++) {
375 set = all_keys[i >> 3] & (1 << (i & 7));
376 if (set) {
377 k = wl_array_add(&keys, sizeof *k);
378 *k = i;
379 }
380 }
381
382 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
383
384 wl_array_release(&keys);
385}