2 * Copyright © 2011, 2012 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
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.
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.
30 #include "linux/input.h"
31 #include <libevdev/libevdev.h>
33 #include "libinput-private.h"
37 /* The HW DPI rate we normalize to before calculating pointer acceleration */
38 #define DEFAULT_MOUSE_DPI 1000
41 * The constant (linear) acceleration factor we use to normalize trackpoint
42 * deltas before calculating pointer acceleration.
44 #define DEFAULT_TRACKPOINT_ACCEL 1.0
46 /* The fake resolution value for abs devices without resolution */
47 #define EVDEV_FAKE_RESOLUTION 1
49 enum evdev_event_type {
51 EVDEV_ABSOLUTE_TOUCH_DOWN,
52 EVDEV_ABSOLUTE_MOTION,
53 EVDEV_ABSOLUTE_TOUCH_UP,
54 EVDEV_ABSOLUTE_MT_DOWN,
55 EVDEV_ABSOLUTE_MT_MOTION,
57 EVDEV_RELATIVE_MOTION,
60 enum evdev_device_seat_capability {
61 EVDEV_DEVICE_POINTER = (1 << 0),
62 EVDEV_DEVICE_KEYBOARD = (1 << 1),
63 EVDEV_DEVICE_TOUCH = (1 << 2)
66 enum evdev_device_tags {
67 EVDEV_TAG_EXTERNAL_MOUSE = (1 << 0),
68 EVDEV_TAG_INTERNAL_TOUCHPAD = (1 << 1),
69 EVDEV_TAG_TRACKPOINT = (1 << 2),
70 EVDEV_TAG_TOUCHPAD_TRACKPOINT = (1 << 3),
73 enum evdev_middlebutton_state {
75 MIDDLEBUTTON_LEFT_DOWN,
76 MIDDLEBUTTON_RIGHT_DOWN,
78 MIDDLEBUTTON_LEFT_UP_PENDING,
79 MIDDLEBUTTON_RIGHT_UP_PENDING,
80 MIDDLEBUTTON_IGNORE_LR,
81 MIDDLEBUTTON_IGNORE_L,
82 MIDDLEBUTTON_IGNORE_R,
83 MIDDLEBUTTON_PASSTHROUGH,
86 enum evdev_middlebutton_event {
87 MIDDLEBUTTON_EVENT_L_DOWN,
88 MIDDLEBUTTON_EVENT_R_DOWN,
89 MIDDLEBUTTON_EVENT_OTHER,
90 MIDDLEBUTTON_EVENT_L_UP,
91 MIDDLEBUTTON_EVENT_R_UP,
92 MIDDLEBUTTON_EVENT_TIMEOUT,
93 MIDDLEBUTTON_EVENT_ALL_UP,
96 enum evdev_device_model {
102 struct device_coords point;
105 struct evdev_device {
106 struct libinput_device base;
108 struct libinput_source *source;
110 struct evdev_dispatch *dispatch;
111 struct libevdev *evdev;
112 struct udev_device *udev_device;
118 const struct input_absinfo *absinfo_x, *absinfo_y;
121 struct device_coords point;
124 int apply_calibration;
125 struct matrix calibration;
126 struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
127 struct matrix usermatrix; /* as supplied by the caller */
132 struct mt_slot *slots;
137 struct device_coords rel;
140 struct libinput_timer timer;
141 struct libinput_device_config_scroll_method config;
142 /* Currently enabled method, button */
143 enum libinput_config_scroll_method method;
145 /* set during device init, used at runtime to delay changes
146 * until all buttons are up */
147 enum libinput_config_scroll_method want_method;
148 uint32_t want_button;
149 /* Checks if buttons are down and commits the setting */
150 void (*change_scroll_method)(struct evdev_device *device);
151 bool button_scroll_active;
154 struct normalized_coords buildup;
156 struct libinput_device_config_natural_scroll config_natural;
157 /* set during device init if we want natural scrolling,
158 * used at runtime to enable/disable the feature */
159 bool natural_scrolling_enabled;
161 /* angle per REL_WHEEL click in degrees */
162 int wheel_click_angle;
165 enum evdev_event_type pending_event;
166 enum evdev_device_seat_capability seat_caps;
167 enum evdev_device_tags tags;
173 struct libinput_device_config_accel config;
174 struct motion_filter *filter;
177 /* Bitmask of pressed keys used to ignore initial release events from
179 unsigned long hw_key_mask[NLONGS(KEY_CNT)];
180 /* Key counter used for multiplexing button events internally in
182 uint8_t key_count[KEY_CNT];
185 struct libinput_device_config_left_handed config;
186 /* left-handed currently enabled */
188 /* set during device init if we want left_handed config,
189 * used at runtime to delay the effect until buttons are up */
191 /* Checks if buttons are down and commits the setting */
192 void (*change_to_enabled)(struct evdev_device *device);
196 struct libinput_device_config_middle_emulation config;
197 /* middle-button emulation enabled */
199 bool enabled_default;
201 enum evdev_middlebutton_state state;
202 struct libinput_timer timer;
203 uint32_t button_mask;
204 uint64_t first_event_time;
207 int dpi; /* HW resolution */
208 struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
210 enum evdev_device_model model;
213 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
215 struct evdev_dispatch;
217 struct evdev_dispatch_interface {
218 /* Process an evdev input event. */
219 void (*process)(struct evdev_dispatch *dispatch,
220 struct evdev_device *device,
221 struct input_event *event,
224 /* Device is being removed (may be NULL) */
225 void (*remove)(struct evdev_dispatch *dispatch);
227 /* Destroy an event dispatch handler and free all its resources. */
228 void (*destroy)(struct evdev_dispatch *dispatch);
230 /* A new device was added */
231 void (*device_added)(struct evdev_device *device,
232 struct evdev_device *added_device);
234 /* A device was removed */
235 void (*device_removed)(struct evdev_device *device,
236 struct evdev_device *removed_device);
238 /* A device was suspended */
239 void (*device_suspended)(struct evdev_device *device,
240 struct evdev_device *suspended_device);
242 /* A device was resumed */
243 void (*device_resumed)(struct evdev_device *device,
244 struct evdev_device *resumed_device);
246 /* Tag device with one of EVDEV_TAG */
247 void (*tag_device)(struct evdev_device *device,
248 struct udev_device *udev_device);
251 struct evdev_dispatch {
252 struct evdev_dispatch_interface *interface;
253 struct libinput_device_config_calibration calibration;
256 struct libinput_device_config_send_events config;
257 enum libinput_config_send_events_mode current_mode;
261 struct evdev_device *
262 evdev_device_create(struct libinput_seat *seat,
263 struct udev_device *device);
266 evdev_fix_abs_resolution(struct evdev_device *device,
273 evdev_device_init_pointer_acceleration(struct evdev_device *device,
274 accel_profile_func_t profile);
276 struct evdev_dispatch *
277 evdev_touchpad_create(struct evdev_device *device);
279 struct evdev_dispatch *
280 evdev_mt_touchpad_create(struct evdev_device *device);
283 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
286 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
289 evdev_device_get_output(struct evdev_device *device);
292 evdev_device_get_sysname(struct evdev_device *device);
295 evdev_device_get_name(struct evdev_device *device);
298 evdev_device_get_id_product(struct evdev_device *device);
301 evdev_device_get_id_vendor(struct evdev_device *device);
304 evdev_device_get_udev_device(struct evdev_device *device);
307 evdev_device_set_default_calibration(struct evdev_device *device,
308 const float calibration[6]);
310 evdev_device_calibrate(struct evdev_device *device,
311 const float calibration[6]);
314 evdev_device_has_capability(struct evdev_device *device,
315 enum libinput_device_capability capability);
318 evdev_device_get_size(struct evdev_device *device,
323 evdev_device_has_button(struct evdev_device *device, uint32_t code);
326 evdev_device_has_key(struct evdev_device *device, uint32_t code);
329 evdev_device_transform_x(struct evdev_device *device,
334 evdev_device_transform_y(struct evdev_device *device,
338 evdev_device_suspend(struct evdev_device *device);
341 evdev_device_resume(struct evdev_device *device);
344 evdev_notify_suspended_device(struct evdev_device *device);
347 evdev_notify_resumed_device(struct evdev_device *device);
350 evdev_keyboard_notify_key(struct evdev_device *device,
353 enum libinput_key_state state);
356 evdev_pointer_notify_button(struct evdev_device *device,
359 enum libinput_button_state state);
361 evdev_pointer_notify_physical_button(struct evdev_device *device,
364 enum libinput_button_state state);
367 evdev_init_natural_scroll(struct evdev_device *device);
370 evdev_post_scroll(struct evdev_device *device,
372 enum libinput_pointer_axis_source source,
373 const struct normalized_coords *delta);
376 evdev_stop_scroll(struct evdev_device *device,
378 enum libinput_pointer_axis_source source);
381 evdev_device_remove(struct evdev_device *device);
384 evdev_device_destroy(struct evdev_device *device);
387 evdev_middlebutton_filter_button(struct evdev_device *device,
390 enum libinput_button_state state);
393 evdev_init_middlebutton(struct evdev_device *device,
398 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
400 double value = v - absinfo->minimum;
401 return value/absinfo->resolution;
405 evdev_init_left_handed(struct evdev_device *device,
406 void (*change_to_left_handed)(struct evdev_device *));
408 static inline uint32_t
409 evdev_to_left_handed(struct evdev_device *device,
412 if (device->left_handed.enabled) {
413 if (button == BTN_LEFT)
415 else if (button == BTN_RIGHT)