[PATCH libinput 11/23] filter: make the simpsons accel calculation available through the header

Peter Hutterer peter.hutterer at who-t.net
Thu Apr 12 06:34:37 UTC 2018


This is the standard approach for mice and touchpads to calculate the
acceleration based on the last two deltas, let's make that code shareable.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/filter-private.h |  8 ++++++++
 src/filter.c         | 52 ++++++++++++++++++----------------------------------
 2 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/src/filter-private.h b/src/filter-private.h
index 4779e8f2..554b7f64 100644
--- a/src/filter-private.h
+++ b/src/filter-private.h
@@ -106,6 +106,14 @@ tracker_by_offset(struct pointer_trackers *trackers, unsigned int offset);
 double
 calculate_velocity(struct pointer_trackers *trackers, uint64_t time);
 
+double
+calculate_acceleration_simpsons(struct motion_filter *filter,
+				accel_profile_func_t profile,
+				void *data,
+				double velocity,
+				double last_velocity,
+				uint64_t time);
+
 /* Convert speed/velocity from units/us to units/ms */
 static inline double
 v_us2ms(double units_per_us)
diff --git a/src/filter.c b/src/filter.c
index 5a593e17..291013a1 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -295,23 +295,6 @@ calculate_velocity(struct pointer_trackers *trackers, uint64_t time)
 	return result; /* units/us */
 }
 
-/**
- * Apply the acceleration profile to the given velocity.
- *
- * @param accel The acceleration filter
- * @param data Caller-specific data
- * @param velocity Velocity in device-units per µs
- * @param time Current time in µs
- *
- * @return A unitless acceleration factor, to be applied to the delta
- */
-static double
-acceleration_profile(struct pointer_accelerator *accel,
-		     void *data, double velocity, uint64_t time)
-{
-	return accel->profile(&accel->base, data, velocity, time);
-}
-
 /**
  * Calculate the acceleration factor for our current velocity, averaging
  * between our current and the most recent velocity to smoothen out changes.
@@ -324,23 +307,23 @@ acceleration_profile(struct pointer_accelerator *accel,
  *
  * @return A unitless acceleration factor, to be applied to the delta
  */
-static double
-calculate_acceleration(struct pointer_accelerator *accel,
-		       void *data,
-		       double velocity,
-		       double last_velocity,
-		       uint64_t time)
+double
+calculate_acceleration_simpsons(struct motion_filter *filter,
+				accel_profile_func_t profile,
+				void *data,
+				double velocity,
+				double last_velocity,
+				uint64_t time)
 {
 	double factor;
 
 	/* Use Simpson's rule to calculate the avarage acceleration between
 	 * the previous motion and the most recent. */
-	factor = acceleration_profile(accel, data, velocity, time);
-	factor += acceleration_profile(accel, data, last_velocity, time);
-	factor += 4.0 *
-		acceleration_profile(accel, data,
-				     (last_velocity + velocity) / 2,
-				     time);
+	factor = profile(filter, data, velocity, time);
+	factor += profile(filter, data, last_velocity, time);
+	factor += 4.0 * profile(filter, data,
+				(last_velocity + velocity) / 2,
+				time);
 
 	factor = factor / 6.0;
 
@@ -368,11 +351,12 @@ calculate_acceleration_factor(struct pointer_accelerator *accel,
 
 	feed_trackers(&accel->trackers, unaccelerated, time);
 	velocity = calculate_velocity(&accel->trackers, time);
-	accel_factor = calculate_acceleration(accel,
-					      data,
-					      velocity,
-					      accel->last_velocity,
-					      time);
+	accel_factor = calculate_acceleration_simpsons(&accel->base,
+						       accel->profile,
+						       data,
+						       velocity,
+						       accel->last_velocity,
+						       time);
 	accel->last_velocity = velocity;
 
 	return accel_factor;
-- 
2.14.3



More information about the wayland-devel mailing list