• 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;	/* units/us */
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 /**
94  * Generic filter that calculates the acceleration factor and applies it to
95  * the coordinates.
96  *
97  * @param filter The acceleration filter
98  * @param unaccelerated The raw delta in the device's dpi
99  * @param data Caller-specific data
100  * @param time Current time in µs
101  *
102  * @return An accelerated tuple of coordinates representing accelerated
103  * motion, still in device units.
104  */
105 static struct device_float_coords
accelerator_filter_generic(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)106 accelerator_filter_generic(struct motion_filter *filter,
107 			   const struct device_float_coords *unaccelerated,
108 			   void *data, uint64_t time)
109 {
110 	struct touchpad_accelerator *accel =
111 		(struct touchpad_accelerator *) filter;
112 	double accel_value; /* unitless factor */
113 	struct device_float_coords accelerated;
114 
115 	accel_value = calculate_acceleration_factor(accel,
116 						    unaccelerated,
117 						    data,
118 						    time);
119 
120 	accelerated.x = accel_value * unaccelerated->x;
121 	accelerated.y = accel_value * unaccelerated->y;
122 
123 	return accelerated;
124 }
125 
126 static struct normalized_coords
accelerator_filter_post_normalized(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)127 accelerator_filter_post_normalized(struct motion_filter *filter,
128 				   const struct device_float_coords *unaccelerated,
129 				   void *data, uint64_t time)
130 {
131 	struct touchpad_accelerator *accel =
132 		(struct touchpad_accelerator *) filter;
133 	struct device_float_coords accelerated;
134 
135 	/* Accelerate for device units, normalize afterwards */
136 	accelerated = accelerator_filter_generic(filter,
137 						 unaccelerated,
138 						 data,
139 						 time);
140 	return normalize_for_dpi(&accelerated, accel->dpi);
141 }
142 
143 /* Maps the [-1, 1] speed setting into a constant acceleration
144  * range. This isn't a linear scale, we keep 0 as the 'optimized'
145  * mid-point and scale down to 0 for setting -1 and up to 5 for
146  * setting 1. On the premise that if you want a faster cursor, it
147  * doesn't matter as much whether you have 0.56789 or 0.56790,
148  * but for lower settings it does because you may lose movements.
149  * *shrug*.
150  *
151  * Magic numbers calculated by MyCurveFit.com, data points were
152  *  0.0 0.0
153  *  0.1 0.1 (because we need 4 points)
154  *  1   1
155  *  2   5
156  *
157  *  This curve fits nicely into the range necessary.
158  */
159 static inline double
speed_factor(double s)160 speed_factor(double s)
161 {
162 	s += 1; /* map to [0, 2] */
163 	return 435837.2 + (0.04762636 - 435837.2)/(1 + pow(s/240.4549,
164 							   2.377168));
165 }
166 
167 static bool
touchpad_accelerator_set_speed(struct motion_filter * filter,double speed_adjustment)168 touchpad_accelerator_set_speed(struct motion_filter *filter,
169 		      double speed_adjustment)
170 {
171 	struct touchpad_accelerator *accel_filter =
172 		(struct touchpad_accelerator *)filter;
173 
174 	assert(speed_adjustment >= -1.0 && speed_adjustment <= 1.0);
175 
176 	filter->speed_adjustment = speed_adjustment;
177 	accel_filter->speed_factor = speed_factor(speed_adjustment);
178 
179 	return true;
180 }
181 
182 static struct normalized_coords
touchpad_constant_filter(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)183 touchpad_constant_filter(struct motion_filter *filter,
184 			 const struct device_float_coords *unaccelerated,
185 			 void *data, uint64_t time)
186 {
187 	struct touchpad_accelerator *accel =
188 		(struct touchpad_accelerator *)filter;
189 	struct normalized_coords normalized;
190 	/* We need to use the same baseline here as the accelerated code,
191 	 * otherwise our unaccelerated speed is different to the accelerated
192 	 * speed on the plateau.
193 	 *
194 	 * This is a hack, the baseline should be incorporated into the
195 	 * TP_MAGIC_SLOWDOWN so we only have one number here but meanwhile
196 	 * this will do.
197 	 */
198 	const double baseline = 0.9;
199 
200 	normalized = normalize_for_dpi(unaccelerated, accel->dpi);
201 	normalized.x = baseline * TP_MAGIC_SLOWDOWN * normalized.x;
202 	normalized.y = baseline * TP_MAGIC_SLOWDOWN * normalized.y;
203 
204 	return normalized;
205 }
206 
207 static void
touchpad_accelerator_restart(struct motion_filter * filter,void * data,uint64_t time)208 touchpad_accelerator_restart(struct motion_filter *filter,
209 			     void *data,
210 			     uint64_t time)
211 {
212 	struct touchpad_accelerator *accel =
213 		(struct touchpad_accelerator *) filter;
214 
215 	trackers_reset(&accel->trackers, time);
216 }
217 
218 static void
touchpad_accelerator_destroy(struct motion_filter * filter)219 touchpad_accelerator_destroy(struct motion_filter *filter)
220 {
221 	struct touchpad_accelerator *accel =
222 		(struct touchpad_accelerator *) filter;
223 
224 	trackers_free(&accel->trackers);
225 	free(accel);
226 }
227 
228 double
touchpad_accel_profile_linear(struct motion_filter * filter,void * data,double speed_in,uint64_t time)229 touchpad_accel_profile_linear(struct motion_filter *filter,
230 			      void *data,
231 			      double speed_in, /* in device units/µs */
232 			      uint64_t time)
233 {
234 	struct touchpad_accelerator *accel_filter =
235 		(struct touchpad_accelerator *)filter;
236 	const double threshold = accel_filter->threshold; /* units/us */
237 	const double baseline = 0.9;
238 	double factor; /* unitless */
239 
240 	/* Convert to mm/s because that's something one can understand */
241 	speed_in = v_us2s(speed_in) * 25.4/accel_filter->dpi;
242 
243 	/*
244 	   Our acceleration function calculates a factor to accelerate input
245 	   deltas with. The function is a double incline with a plateau,
246 	   with a rough shape like this:
247 
248 	  accel
249 	 factor
250 	   ^         ______
251 	   |        )
252 	   |  _____)
253 	   | /
254 	   |/
255 	   +-------------> speed in
256 
257 	   Except the second incline is a curve, but well, asciiart.
258 
259 	   The first incline is a linear function in the form
260 		   y = ax + b
261 		   where y is speed_out
262 		         x is speed_in
263 			 a is the incline of acceleration
264 			 b is minimum acceleration factor
265 	   for speeds up to the lower threshold, we decelerate, down to 30%
266 	   of input speed.
267 		   hence 1 = a * 7 + 0.3
268 		       0.7 = a * 7  => a := 0.1
269 		   deceleration function is thus:
270 			y = 0.1x + 0.3
271 
272 	   The first plateau is the baseline.
273 
274 	   The second incline is a curve up, based on magic numbers
275 	   obtained by trial-and-error.
276 
277 	   Above the second incline we have another plateau because
278 	   by then you're moving so fast that extra acceleration doesn't
279 	   help.
280 
281 	  Note:
282 	  * The minimum threshold is a result of trial-and-error and
283 	    has no other special meaning.
284 	  * 0.3 is chosen simply because it is above the Nyquist frequency
285 	    for subpixel motion within a pixel.
286 	*/
287 
288 	if (speed_in < 7.0) {
289 		factor = min(baseline, 0.1 * speed_in + 0.3);
290 	/* up to the threshold, we keep factor 1, i.e. 1:1 movement */
291 	} else if (speed_in < threshold) {
292 		factor = baseline;
293 	} else {
294 
295 	/* Acceleration function above the threshold is a curve up
296 	   to four times the threshold, because why not.
297 
298 	   Don't assume anything about the specific numbers though, this was
299 	   all just trial and error by tweaking numbers here and there, then
300 	   the formula was optimized doing basic maths.
301 
302 	   You could replace this with some other random formula that gives
303 	   the same numbers and it would be just as correct.
304 
305 	 */
306 		const double upper_threshold = threshold * 4.0;
307 		speed_in = min(speed_in, upper_threshold);
308 
309 		factor = 0.0025 * (speed_in/threshold) * (speed_in - threshold) + baseline;
310 	}
311 
312 	factor *= accel_filter->speed_factor;
313 	return factor * TP_MAGIC_SLOWDOWN;
314 }
315 
316 struct motion_filter_interface accelerator_interface_touchpad = {
317 	.type = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE,
318 	.filter = accelerator_filter_post_normalized,
319 	.filter_constant = touchpad_constant_filter,
320 	.restart = touchpad_accelerator_restart,
321 	.destroy = touchpad_accelerator_destroy,
322 	.set_speed = touchpad_accelerator_set_speed,
323 };
324 
325 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)326 create_pointer_accelerator_filter_touchpad(int dpi,
327 	uint64_t event_delta_smooth_threshold,
328 	uint64_t event_delta_smooth_value,
329 	bool use_velocity_averaging)
330 {
331 	struct touchpad_accelerator *filter;
332 	struct pointer_delta_smoothener *smoothener;
333 
334 	filter = zalloc(sizeof *filter);
335 	filter->last_velocity = 0.0;
336 
337 	trackers_init(&filter->trackers, use_velocity_averaging ? 16 : 2);
338 
339 	filter->threshold = 130;
340 	filter->dpi = dpi;
341 
342 	filter->base.interface = &accelerator_interface_touchpad;
343 	filter->profile = touchpad_accel_profile_linear;
344 
345 	smoothener = zalloc(sizeof(*smoothener));
346 	smoothener->threshold = event_delta_smooth_threshold,
347 	smoothener->value = event_delta_smooth_value,
348 	filter->trackers.smoothener = smoothener;
349 
350 	return &filter->base;
351 }
352