1 /*
2 * Copyright © 2012 Jonas Ådahl
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef FILTER_PRIVATE_H
25 #define FILTER_PRIVATE_H
26
27 #include "config.h"
28
29 #include "filter.h"
30
31 struct motion_filter_interface {
32 enum libinput_config_accel_profile type;
33 struct normalized_coords (*filter)(
34 struct motion_filter *filter,
35 const struct device_float_coords *unaccelerated,
36 void *data, uint64_t time);
37 struct normalized_coords (*filter_constant)(
38 struct motion_filter *filter,
39 const struct device_float_coords *unaccelerated,
40 void *data, uint64_t time);
41 struct normalized_coords (*filter_scroll)(
42 struct motion_filter *filter,
43 const struct device_float_coords *unaccelerated,
44 void *data, uint64_t time);
45 void (*restart)(struct motion_filter *filter,
46 void *data,
47 uint64_t time);
48 void (*destroy)(struct motion_filter *filter);
49 bool (*set_speed)(struct motion_filter *filter,
50 double speed_adjustment);
51 bool (*set_accel_config)(struct motion_filter *filter,
52 struct libinput_config_accel *accel_config);
53 };
54
55 struct motion_filter {
56 double speed_adjustment; /* normalized [-1, 1] */
57 const struct motion_filter_interface *interface;
58 };
59
60 struct pointer_tracker {
61 struct device_float_coords delta; /* delta to most recent event */
62 uint64_t time; /* us */
63 uint32_t dir;
64 };
65
66 /* For smoothing timestamps from devices with unreliable timing */
67 struct pointer_delta_smoothener {
68 uint64_t threshold;
69 uint64_t value;
70 };
71
72 static inline struct pointer_delta_smoothener *
pointer_delta_smoothener_create(uint64_t event_delta_smooth_threshold,uint64_t event_delta_smooth_value)73 pointer_delta_smoothener_create(uint64_t event_delta_smooth_threshold,
74 uint64_t event_delta_smooth_value)
75 {
76 struct pointer_delta_smoothener *s = zalloc(sizeof(*s));
77 s->threshold = event_delta_smooth_threshold;
78 s->value = event_delta_smooth_value;
79 return s;
80 }
81
82 static inline void
pointer_delta_smoothener_destroy(struct pointer_delta_smoothener * smoothener)83 pointer_delta_smoothener_destroy(struct pointer_delta_smoothener *smoothener)
84 {
85 free(smoothener);
86 }
87
88 struct pointer_trackers {
89 struct pointer_tracker *trackers;
90 size_t ntrackers;
91 unsigned int cur_tracker;
92
93 struct pointer_delta_smoothener *smoothener;
94 };
95
96 void trackers_init(struct pointer_trackers *trackers, int ntrackers);
97 void trackers_free(struct pointer_trackers *trackers);
98
99 void
100 trackers_reset(struct pointer_trackers *trackers,
101 uint64_t time);
102 void
103 trackers_feed(struct pointer_trackers *trackers,
104 const struct device_float_coords *delta,
105 uint64_t time);
106
107 struct pointer_tracker *
108 trackers_by_offset(struct pointer_trackers *trackers, unsigned int offset);
109
110 double
111 trackers_velocity(struct pointer_trackers *trackers, uint64_t time);
112
113 double
114 calculate_acceleration_simpsons(struct motion_filter *filter,
115 accel_profile_func_t profile,
116 void *data,
117 double velocity,
118 double last_velocity,
119 uint64_t time);
120
121 /* Convert speed/velocity from units/us to units/ms */
122 static inline double
v_us2ms(double units_per_us)123 v_us2ms(double units_per_us)
124 {
125 return units_per_us * 1000.0;
126 }
127
128 static inline double
v_us2s(double units_per_us)129 v_us2s(double units_per_us)
130 {
131 return units_per_us * 1000000.0;
132 }
133
134 /* Convert speed/velocity from units/ms to units/us */
135 static inline double
v_ms2us(double units_per_ms)136 v_ms2us(double units_per_ms)
137 {
138 return units_per_ms/1000.0;
139 }
140
141 static inline struct normalized_coords
normalize_for_dpi(const struct device_float_coords * coords,int dpi)142 normalize_for_dpi(const struct device_float_coords *coords, int dpi)
143 {
144 struct normalized_coords norm;
145
146 norm.x = coords->x * DEFAULT_MOUSE_DPI/dpi;
147 norm.y = coords->y * DEFAULT_MOUSE_DPI/dpi;
148
149 return norm;
150 }
151
152 #endif
153