blob: 2105aded21e3c3ccbc0286667bcfedfb0107842b [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
190int
191evdev_device_process_event(struct libinput_event *event)
192{
193 struct libinput_device *libinput_device =
194 libinput_event_get_device(event);
195 int handled = 1;
196
197 switch (libinput_event_get_type(event)) {
198 case LIBINPUT_EVENT_KEYBOARD_KEY:
199 handle_keyboard_key(libinput_device,
200 libinput_event_get_keyboard_event(event));
201 break;
202 case LIBINPUT_EVENT_POINTER_MOTION:
203 handle_pointer_motion(libinput_device,
204 libinput_event_get_pointer_event(event));
205 break;
206 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
207 handle_pointer_motion_absolute(
208 libinput_device,
209 libinput_event_get_pointer_event(event));
210 break;
211 case LIBINPUT_EVENT_POINTER_BUTTON:
212 handle_pointer_button(libinput_device,
213 libinput_event_get_pointer_event(event));
214 break;
215 case LIBINPUT_EVENT_POINTER_AXIS:
216 handle_pointer_axis(libinput_device,
217 libinput_event_get_pointer_event(event));
218 break;
219 case LIBINPUT_EVENT_TOUCH_DOWN:
220 handle_touch_down(libinput_device,
221 libinput_event_get_touch_event(event));
222 break;
223 case LIBINPUT_EVENT_TOUCH_MOTION:
224 handle_touch_motion(libinput_device,
225 libinput_event_get_touch_event(event));
226 break;
227 case LIBINPUT_EVENT_TOUCH_UP:
228 handle_touch_up(libinput_device,
229 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700230 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100231 default:
232 handled = 0;
233 weston_log("unknown libinput event %d\n",
234 libinput_event_get_type(event));
235 }
236
237 return handled;
238}
239
240static void
241notify_output_destroy(struct wl_listener *listener, void *data)
242{
243 struct evdev_device *device =
244 container_of(listener,
245 struct evdev_device, output_destroy_listener);
246 struct weston_compositor *c = device->seat->compositor;
247 struct weston_output *output;
248
249 if (device->output_name) {
250 output = container_of(c->output_list.next,
251 struct weston_output, link);
252 evdev_device_set_output(device, output);
253 } else {
254 device->output = NULL;
255 }
256}
257
258void
259evdev_device_set_output(struct evdev_device *device,
260 struct weston_output *output)
261{
262 device->output = output;
263 device->output_destroy_listener.notify = notify_output_destroy;
264 wl_signal_add(&output->destroy_signal,
265 &device->output_destroy_listener);
266}
267
268struct evdev_device *
269evdev_device_create(struct libinput_device *libinput_device,
270 struct weston_seat *seat)
271{
272 struct evdev_device *device;
273
274 device = zalloc(sizeof *device);
275 if (device == NULL)
276 return NULL;
277
278 device->seat = seat;
279 wl_list_init(&device->link);
280 device->device = libinput_device;
281
282 if (libinput_device_has_capability(libinput_device,
283 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
284 weston_seat_init_keyboard(seat, NULL);
285 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
286 }
287 if (libinput_device_has_capability(libinput_device,
288 LIBINPUT_DEVICE_CAP_POINTER)) {
289 weston_seat_init_pointer(seat);
290 device->seat_caps |= EVDEV_SEAT_POINTER;
291 }
292 if (libinput_device_has_capability(libinput_device,
293 LIBINPUT_DEVICE_CAP_TOUCH)) {
294 weston_seat_init_touch(seat);
295 device->seat_caps |= EVDEV_SEAT_TOUCH;
296 }
297
298 libinput_device_set_user_data(libinput_device, device);
299 libinput_device_ref(libinput_device);
300
301 return device;
302}
303
304void
305evdev_device_destroy(struct evdev_device *device)
306{
307 if (device->seat_caps & EVDEV_SEAT_POINTER)
308 weston_seat_release_pointer(device->seat);
309 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
310 weston_seat_release_keyboard(device->seat);
311 if (device->seat_caps & EVDEV_SEAT_TOUCH)
312 weston_seat_release_touch(device->seat);
313
314 if (device->output)
315 wl_list_remove(&device->output_destroy_listener.link);
316 wl_list_remove(&device->link);
317 libinput_device_unref(device->device);
318 free(device->devnode);
319 free(device->output_name);
320 free(device);
321}
322
323void
324evdev_notify_keyboard_focus(struct weston_seat *seat,
325 struct wl_list *evdev_devices)
326{
327 struct evdev_device *device;
328 struct wl_array keys;
329 unsigned int i, set;
330 char evdev_keys[(KEY_CNT + 7) / 8];
331 char all_keys[(KEY_CNT + 7) / 8];
332 uint32_t *k;
333 int ret;
334
335 if (!seat->keyboard_device_count > 0)
336 return;
337
338 memset(all_keys, 0, sizeof all_keys);
339 wl_list_for_each(device, evdev_devices, link) {
340 memset(evdev_keys, 0, sizeof evdev_keys);
341 ret = libinput_device_get_keys(device->device,
342 evdev_keys,
343 sizeof evdev_keys);
344 if (ret < 0) {
345 weston_log("failed to get keys for device %s\n",
346 device->devnode);
347 continue;
348 }
349 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
350 all_keys[i] |= evdev_keys[i];
351 }
352
353 wl_array_init(&keys);
354 for (i = 0; i < KEY_CNT; i++) {
355 set = all_keys[i >> 3] & (1 << (i & 7));
356 if (set) {
357 k = wl_array_add(&keys, sizeof *k);
358 *k = i;
359 }
360 }
361
362 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
363
364 wl_array_release(&keys);
365}