• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2006-2009 Simon Thum
3  * Copyright © 2012 Jonas Ådahl
4  * Copyright © 2014-2015 Red Hat, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <math.h>
32 
33 #include "filter.h"
34 #include "libinput-util.h"
35 #include "filter-private.h"
36 
37 /* Once normalized, touchpads see the same acceleration as mice. that is
38  * technically correct but subjectively wrong, we expect a touchpad to be a
39  * lot slower than a mouse. Apply a magic factor to slow down all movements
40  */
41 #define TP_MAGIC_SLOWDOWN 0.2968 /* unitless factor */
42 
43 struct touchpad_accelerator {
44 	struct motion_filter base;
45 
46 	accel_profile_func_t profile;
47 
48 	double velocity;	/* units/us */
49 	double last_velocity;	/* units/us */
50 
51 	struct pointer_trackers trackers;
52 
53 	double threshold;	/* mm/s */
54 	double accel;		/* unitless factor */
55 
56 	int dpi;
57 
58 	double speed_factor;    /* factor based on speed setting */
59 };
60 
61 /**
62  * Calculate the acceleration factor for the given delta with the timestamp.
63  *
64  * @param accel The acceleration filter
65  * @param unaccelerated The raw delta in the device's dpi
66  * @param data Caller-specific data
67  * @param time Current time in µs
68  *
69  * @return A unitless acceleration factor, to be applied to the delta
70  */
71 static inline double
calculate_acceleration_factor(struct touchpad_accelerator * accel,const struct device_float_coords * unaccelerated,void * data,uint64_t time)72 calculate_acceleration_factor(struct touchpad_accelerator *accel,
73 			      const struct device_float_coords *unaccelerated,
74 			      void *data,
75 			      uint64_t time)
76 {
77 	double velocity; /* units/us in device-native dpi*/
78 	double accel_factor;
79 
80 	trackers_feed(&accel->trackers, unaccelerated, time);
81 	velocity = trackers_velocity(&accel->trackers, time);
82 	accel_factor = calculate_acceleration_simpsons(&accel->base,
83 						       accel->profile,
84 						       data,
85 						       velocity,
86 						       accel->last_velocity,
87 						       time);
88 	accel->last_velocity = velocity;
89 
90 	return accel_factor;
91 }
92 
93 static struct normalized_coords
accelerator_filter_touchpad(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)94 accelerator_filter_touchpad(struct motion_filter *filter,
95 			    const struct device_float_coords *unaccelerated,
96 			    void *data, uint64_t time)
97 {
98 	struct touchpad_accelerator *accel =
99 		(struct touchpad_accelerator *) filter;
100 
101 	/* Accelerate for device units, normalize afterwards */
102 	double accel_factor = calculate_acceleration_factor(accel,
103 							    unaccelerated,
104 							    data,
105 							    time);
106 	const struct device_float_coords accelerated =  {
107 		.x = unaccelerated->x * accel_factor,
108 		.y = unaccelerated->y * accel_factor,
109 	};
110 
111 	return normalize_for_dpi(&accelerated, accel->dpi);
112 }
113 
114 /* Maps the [-1, 1] speed setting into a constant acceleration
115  * range. This isn't a linear scale, we keep 0 as the 'optimized'
116  * mid-point and scale down to 0.05 for setting -1 and up to 5 for
117  * setting 1. On the premise that if you want a faster cursor, it
118  * doesn't matter as much whether you have 0.56789 or 0.56790,
119  * but for lower settings it does because you may lose movements.
120  * *shrug*.
121  */
122 static inline double
speed_factor(double s)123 speed_factor(double s)
124 {
125 	return pow(s + 1, 2.38) * 0.95 + 0.05;
126 }
127 
128 static bool
touchpad_accelerator_set_speed(struct motion_filter * filter,double speed_adjustment)129 touchpad_accelerator_set_speed(struct motion_filter *filter,
130 		      double speed_adjustment)
131 {
132 	struct touchpad_accelerator *accel_filter =
133 		(struct touchpad_accelerator *)filter;
134 
135 	assert(speed_adjustment >= -1.0 && speed_adjustment <= 1.0);
136 
137 	filter->speed_adjustment = speed_adjustment;
138 	accel_filter->speed_factor = speed_factor(speed_adjustment);
139 
140 	return true;
141 }
142 
143 static struct normalized_coords
touchpad_constant_filter(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)144 touchpad_constant_filter(struct motion_filter *filter,
145 			 const struct device_float_coords *unaccelerated,
146 			 void *data, uint64_t time)
147 {
148 	struct touchpad_accelerator *accel =
149 		(struct touchpad_accelerator *)filter;
150 	struct normalized_coords normalized;
151 	/* We need to use the same baseline here as the accelerated code,
152 	 * otherwise our unaccelerated speed is different to the accelerated
153 	 * speed on the plateau.
154 	 *
155 	 * This is a hack, the baseline should be incorporated into the
156 	 * TP_MAGIC_SLOWDOWN so we only have one number here but meanwhile
157 	 * this will do.
158 	 */
159 	const double baseline = 0.9;
160 
161 	normalized = normalize_for_dpi(unaccelerated, accel->dpi);
162 	normalized.x = baseline * TP_MAGIC_SLOWDOWN * normalized.x;
163 	normalized.y = baseline * TP_MAGIC_SLOWDOWN * normalized.y;
164 
165 	return normalized;
166 }
167 
168 static void
touchpad_accelerator_restart(struct motion_filter * filter,void * data,uint64_t time)169 touchpad_accelerator_restart(struct motion_filter *filter,
170 			     void *data,
171 			     uint64_t time)
172 {
173 	struct touchpad_accelerator *accel =
174 		(struct touchpad_accelerator *) filter;
175 
176 	trackers_reset(&accel->trackers, time);
177 }
178 
179 static void
touchpad_accelerator_destroy(struct motion_filter * filter)180 touchpad_accelerator_destroy(struct motion_filter *filter)
181 {
182 	struct touchpad_accelerator *accel =
183 		(struct touchpad_accelerator *) filter;
184 
185 	trackers_free(&accel->trackers);
186 	free(accel);
187 }
188 
189 double
touchpad_accel_profile_linear(struct motion_filter * filter,void * data,double speed_in,uint64_t time)190 touchpad_accel_profile_linear(struct motion_filter *filter,
191 			      void *data,
192 			      double speed_in, /* in device units/µs */
193 			      uint64_t time)
194 {
195 	struct touchpad_accelerator *accel_filter =
196 		(struct touchpad_accelerator *)filter;
197 	const double threshold = accel_filter->threshold; /* mm/s */
198 	const double baseline = 0.9;
199 	double factor; /* unitless */
200 
201 	/* Convert to mm/s because that's something one can understand */
202 	speed_in = v_us2s(speed_in) * 25.4/accel_filter->dpi;
203 
204 	/*
205 	   Our acceleration function calculates a factor to accelerate input
206 	   deltas with. The function is a double incline with a plateau,
207 	   with a rough shape like this:
208 
209 	  accel
210 	 factor
211 	   ^         ______
212 	   |        )
213 	   |  _____)
214 	   | /
215 	   |/
216 	   +-------------> speed in
217 
218 	   Except the second incline is a curve, but well, asciiart.
219 
220 	   The first incline is a linear function in the form
221 		   y = ax + b
222 		   where y is speed_out
223 		         x is speed_in
224 			 a is the incline of acceleration
225 			 b is minimum acceleration factor
226 	   for speeds up to the lower threshold, we decelerate, down to 30%
227 	   of input speed.
228 		   hence 1 = a * 7 + 0.3
229 		       0.7 = a * 7  => a := 0.1
230 		   deceleration function is thus:
231 			y = 0.1x + 0.3
232 
233 	   The first plateau is the baseline.
234 
235 	   The second incline is a curve up, based on magic numbers
236 	   obtained by trial-and-error.
237 
238 	   Above the second incline we have another plateau because
239 	   by then you're moving so fast that extra acceleration doesn't
240 	   help.
241 
242 	  Note:
243 	  * The minimum threshold is a result of trial-and-error and
244 	    has no other special meaning.
245 	  * 0.3 is chosen simply because it is above the Nyquist frequency
246 	    for subpixel motion within a pixel.
247 	*/
248 
249 	if (speed_in < 7.0) {
250 		factor = min(baseline, 0.1 * speed_in + 0.3);
251 	/* up to the threshold, we keep factor 1, i.e. 1:1 movement */
252 	} else if (speed_in < threshold) {
253 		factor = baseline;
254 	} else {
255 
256 	/* Acceleration function above the threshold is a curve up
257 	   to four times the threshold, because why not.
258 
259 	   Don't assume anything about the specific numbers though, this was
260 	   all just trial and error by tweaking numbers here and there, then
261 	   the formula was optimized doing basic maths.
262 
263 	   You could replace this with some other random formula that gives
264 	   the same numbers and it would be just as correct.
265 
266 	 */
267 		const double upper_threshold = threshold * 4.0;
268 		speed_in = min(speed_in, upper_threshold);
269 
270 		factor = 0.0025 * (speed_in/threshold) * (speed_in - threshold) + baseline;
271 	}
272 
273 	factor *= accel_filter->speed_factor;
274 	return factor * TP_MAGIC_SLOWDOWN;
275 }
276 
277 static const struct motion_filter_interface accelerator_interface_touchpad = {
278 	.type = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE,
279 	.filter = accelerator_filter_touchpad,
280 	.filter_constant = touchpad_constant_filter,
281 	.filter_scroll = touchpad_constant_filter,
282 	.restart = touchpad_accelerator_restart,
283 	.destroy = touchpad_accelerator_destroy,
284 	.set_speed = touchpad_accelerator_set_speed,
285 };
286 
287 struct motion_filter *
create_pointer_accelerator_filter_touchpad(int dpi,uint64_t event_delta_smooth_threshold,uint64_t event_delta_smooth_value,bool use_velocity_averaging)288 create_pointer_accelerator_filter_touchpad(int dpi,
289 	uint64_t event_delta_smooth_threshold,
290 	uint64_t event_delta_smooth_value,
291 	bool use_velocity_averaging)
292 {
293 	struct touchpad_accelerator *filter;
294 
295 	filter = zalloc(sizeof *filter);
296 	filter->last_velocity = 0.0;
297 
298 	trackers_init(&filter->trackers, use_velocity_averaging ? 16 : 2);
299 
300 	filter->threshold = 130;
301 	filter->dpi = dpi;
302 
303 	filter->base.interface = &accelerator_interface_touchpad;
304 	filter->profile = touchpad_accel_profile_linear;
305 	filter->trackers.smoothener = pointer_delta_smoothener_create(event_delta_smooth_threshold, event_delta_smooth_value);
306 
307 	return &filter->base;
308 }
309