blob: c8a64431dab5103d4613b99824baf8929ef5ae1c [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));
230 default:
231 handled = 0;
232 weston_log("unknown libinput event %d\n",
233 libinput_event_get_type(event));
234 }
235
236 return handled;
237}
238
239static void
240notify_output_destroy(struct wl_listener *listener, void *data)
241{
242 struct evdev_device *device =
243 container_of(listener,
244 struct evdev_device, output_destroy_listener);
245 struct weston_compositor *c = device->seat->compositor;
246 struct weston_output *output;
247
248 if (device->output_name) {
249 output = container_of(c->output_list.next,
250 struct weston_output, link);
251 evdev_device_set_output(device, output);
252 } else {
253 device->output = NULL;
254 }
255}
256
257void
258evdev_device_set_output(struct evdev_device *device,
259 struct weston_output *output)
260{
261 device->output = output;
262 device->output_destroy_listener.notify = notify_output_destroy;
263 wl_signal_add(&output->destroy_signal,
264 &device->output_destroy_listener);
265}
266
267struct evdev_device *
268evdev_device_create(struct libinput_device *libinput_device,
269 struct weston_seat *seat)
270{
271 struct evdev_device *device;
272
273 device = zalloc(sizeof *device);
274 if (device == NULL)
275 return NULL;
276
277 device->seat = seat;
278 wl_list_init(&device->link);
279 device->device = libinput_device;
280
281 if (libinput_device_has_capability(libinput_device,
282 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
283 weston_seat_init_keyboard(seat, NULL);
284 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
285 }
286 if (libinput_device_has_capability(libinput_device,
287 LIBINPUT_DEVICE_CAP_POINTER)) {
288 weston_seat_init_pointer(seat);
289 device->seat_caps |= EVDEV_SEAT_POINTER;
290 }
291 if (libinput_device_has_capability(libinput_device,
292 LIBINPUT_DEVICE_CAP_TOUCH)) {
293 weston_seat_init_touch(seat);
294 device->seat_caps |= EVDEV_SEAT_TOUCH;
295 }
296
297 libinput_device_set_user_data(libinput_device, device);
298 libinput_device_ref(libinput_device);
299
300 return device;
301}
302
303void
304evdev_device_destroy(struct evdev_device *device)
305{
306 if (device->seat_caps & EVDEV_SEAT_POINTER)
307 weston_seat_release_pointer(device->seat);
308 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
309 weston_seat_release_keyboard(device->seat);
310 if (device->seat_caps & EVDEV_SEAT_TOUCH)
311 weston_seat_release_touch(device->seat);
312
313 if (device->output)
314 wl_list_remove(&device->output_destroy_listener.link);
315 wl_list_remove(&device->link);
316 libinput_device_unref(device->device);
317 free(device->devnode);
318 free(device->output_name);
319 free(device);
320}
321
322void
323evdev_notify_keyboard_focus(struct weston_seat *seat,
324 struct wl_list *evdev_devices)
325{
326 struct evdev_device *device;
327 struct wl_array keys;
328 unsigned int i, set;
329 char evdev_keys[(KEY_CNT + 7) / 8];
330 char all_keys[(KEY_CNT + 7) / 8];
331 uint32_t *k;
332 int ret;
333
334 if (!seat->keyboard_device_count > 0)
335 return;
336
337 memset(all_keys, 0, sizeof all_keys);
338 wl_list_for_each(device, evdev_devices, link) {
339 memset(evdev_keys, 0, sizeof evdev_keys);
340 ret = libinput_device_get_keys(device->device,
341 evdev_keys,
342 sizeof evdev_keys);
343 if (ret < 0) {
344 weston_log("failed to get keys for device %s\n",
345 device->devnode);
346 continue;
347 }
348 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
349 all_keys[i] |= evdev_keys[i];
350 }
351
352 wl_array_init(&keys);
353 for (i = 0; i < KEY_CNT; i++) {
354 set = all_keys[i >> 3] & (1 << (i & 7));
355 if (set) {
356 k = wl_array_add(&keys, sizeof *k);
357 *k = i;
358 }
359 }
360
361 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
362
363 wl_array_release(&keys);
364}