filter-touchpad.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Copyright © 2006-2009 Simon Thum
 * Copyright © 2012 Jonas Ådahl
 * Copyright © 2014-2015 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>

#include "filter.h"
#include "libinput-util.h"
#include "filter-private.h"

/* Once normalized, touchpads see the same acceleration as mice. that is
 * technically correct but subjectively wrong, we expect a touchpad to be a
 * lot slower than a mouse. Apply a magic factor to slow down all movements
 */
43
#define TP_MAGIC_SLOWDOWN 0.2968 /* unitless factor */
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
struct touchpad_accelerator {
	struct motion_filter base;

	accel_profile_func_t profile;

	double velocity;	/* units/us */
	double last_velocity;	/* units/us */

	struct pointer_trackers trackers;

	double threshold;	/* units/us */
	double accel;		/* unitless factor */

	int dpi;
59 60

	double speed_factor;    /* factor based on speed setting */
61 62
};

63 64 65 66 67 68 69 70 71 72 73
/**
 * Calculate the acceleration factor for the given delta with the timestamp.
 *
 * @param accel The acceleration filter
 * @param unaccelerated The raw delta in the device's dpi
 * @param data Caller-specific data
 * @param time Current time in µs
 *
 * @return A unitless acceleration factor, to be applied to the delta
 */
static inline double
74
calculate_acceleration_factor(struct touchpad_accelerator *accel,
75 76 77 78 79 80 81
			      const struct device_float_coords *unaccelerated,
			      void *data,
			      uint64_t time)
{
	double velocity; /* units/us in device-native dpi*/
	double accel_factor;

82 83
	trackers_feed(&accel->trackers, unaccelerated, time);
	velocity = trackers_velocity(&accel->trackers, time);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	accel_factor = calculate_acceleration_simpsons(&accel->base,
						       accel->profile,
						       data,
						       velocity,
						       accel->last_velocity,
						       time);
	accel->last_velocity = velocity;

	return accel_factor;
}

/**
 * Generic filter that calculates the acceleration factor and applies it to
 * the coordinates.
 *
 * @param filter The acceleration filter
 * @param unaccelerated The raw delta in the device's dpi
 * @param data Caller-specific data
 * @param time Current time in µs
 *
 * @return An accelerated tuple of coordinates representing accelerated
 * motion, still in device units.
 */
static struct device_float_coords
accelerator_filter_generic(struct motion_filter *filter,
			   const struct device_float_coords *unaccelerated,
			   void *data, uint64_t time)
{
112 113
	struct touchpad_accelerator *accel =
		(struct touchpad_accelerator *) filter;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	double accel_value; /* unitless factor */
	struct device_float_coords accelerated;

	accel_value = calculate_acceleration_factor(accel,
						    unaccelerated,
						    data,
						    time);

	accelerated.x = accel_value * unaccelerated->x;
	accelerated.y = accel_value * unaccelerated->y;

	return accelerated;
}

static struct normalized_coords
accelerator_filter_post_normalized(struct motion_filter *filter,
				   const struct device_float_coords *unaccelerated,
				   void *data, uint64_t time)
{
133 134
	struct touchpad_accelerator *accel =
		(struct touchpad_accelerator *) filter;
135 136 137 138 139 140 141 142 143 144
	struct device_float_coords accelerated;

	/* Accelerate for device units, normalize afterwards */
	accelerated = accelerator_filter_generic(filter,
						 unaccelerated,
						 data,
						 time);
	return normalize_for_dpi(&accelerated, accel->dpi);
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/* Maps the [-1, 1] speed setting into a constant acceleration
 * range. This isn't a linear scale, we keep 0 as the 'optimized'
 * mid-point and scale down to 0 for setting -1 and up to 5 for
 * setting 1. On the premise that if you want a faster cursor, it
 * doesn't matter as much whether you have 0.56789 or 0.56790,
 * but for lower settings it does because you may lose movements.
 * *shrug*.
 *
 * Magic numbers calculated by MyCurveFit.com, data points were
 *  0.0 0.0
 *  0.1 0.1 (because we need 4 points)
 *  1   1
 *  2   5
 *
 *  This curve fits nicely into the range necessary.
 */
static inline double
speed_factor(double s)
{
	s += 1; /* map to [0, 2] */
	return 435837.2 + (0.04762636 - 435837.2)/(1 + pow(s/240.4549,
							   2.377168));
}

169 170 171 172
static bool
touchpad_accelerator_set_speed(struct motion_filter *filter,
		      double speed_adjustment)
{
173 174
	struct touchpad_accelerator *accel_filter =
		(struct touchpad_accelerator *)filter;
175 176 177 178

	assert(speed_adjustment >= -1.0 && speed_adjustment <= 1.0);

	filter->speed_adjustment = speed_adjustment;
179
	accel_filter->speed_factor = speed_factor(speed_adjustment);
180 181 182 183 184 185 186 187 188

	return true;
}

static struct normalized_coords
touchpad_constant_filter(struct motion_filter *filter,
			 const struct device_float_coords *unaccelerated,
			 void *data, uint64_t time)
{
189 190
	struct touchpad_accelerator *accel =
		(struct touchpad_accelerator *)filter;
191 192 193 194 195 196 197 198 199 200 201 202 203 204
	struct normalized_coords normalized;

	normalized = normalize_for_dpi(unaccelerated, accel->dpi);
	normalized.x = TP_MAGIC_SLOWDOWN * normalized.x;
	normalized.y = TP_MAGIC_SLOWDOWN * normalized.y;

	return normalized;
}

static void
touchpad_accelerator_restart(struct motion_filter *filter,
			     void *data,
			     uint64_t time)
{
205 206
	struct touchpad_accelerator *accel =
		(struct touchpad_accelerator *) filter;
207

208
	trackers_reset(&accel->trackers, time);
209 210 211 212 213
}

static void
touchpad_accelerator_destroy(struct motion_filter *filter)
{
214 215
	struct touchpad_accelerator *accel =
		(struct touchpad_accelerator *) filter;
216

217
	trackers_free(&accel->trackers);
218 219 220 221 222 223 224 225 226
	free(accel);
}

double
touchpad_accel_profile_linear(struct motion_filter *filter,
			      void *data,
			      double speed_in, /* in device units/µs */
			      uint64_t time)
{
227 228
	struct touchpad_accelerator *accel_filter =
		(struct touchpad_accelerator *)filter;
229
	const double threshold = accel_filter->threshold; /* units/us */
230
	const double baseline = 0.9;
231 232 233 234 235 236 237 238 239 240 241 242
	double factor; /* unitless */

	/* Convert to mm/s because that's something one can understand */
	speed_in = v_us2s(speed_in) * 25.4/accel_filter->dpi;

	/*
	   Our acceleration function calculates a factor to accelerate input
	   deltas with. The function is a double incline with a plateau,
	   with a rough shape like this:

	  accel
	 factor
243 244 245
	   ^         ______
	   |        )
	   |  _____)
246 247 248 249
	   | /
	   |/
	   +-------------> speed in

250 251 252
	   Except the second incline is a curve, but well, asciiart.

	   The first incline is a linear function in the form
253 254 255 256 257 258 259 260 261 262 263 264
		   y = ax + b
		   where y is speed_out
		         x is speed_in
			 a is the incline of acceleration
			 b is minimum acceleration factor
	   for speeds up to the lower threshold, we decelerate, down to 30%
	   of input speed.
		   hence 1 = a * 7 + 0.3
		       0.7 = a * 7  => a := 0.1
		   deceleration function is thus:
			y = 0.1x + 0.3

265 266 267 268 269 270 271 272 273
	   The first plateau is the baseline.

	   The second incline is a curve up, based on magic numbers
	   obtained by trial-and-error.

	   Above the second incline we have another plateau because
	   by then you're moving so fast that extra acceleration doesn't
	   help.

274 275
	  Note:
	  * The minimum threshold is a result of trial-and-error and
276
	    has no other special meaning.
277 278 279
	  * 0.3 is chosen simply because it is above the Nyquist frequency
	    for subpixel motion within a pixel.
	*/
280

281
	if (speed_in < 7.0) {
282
		factor = min(baseline, 0.1 * speed_in + 0.3);
283 284
	/* up to the threshold, we keep factor 1, i.e. 1:1 movement */
	} else if (speed_in < threshold) {
285
		factor = baseline;
286 287
	} else {

288 289
	/* Acceleration function above the threshold is a curve up
	   to four times the threshold, because why not.
290

291 292 293 294 295 296 297 298 299 300 301 302 303
	   Don't assume anything about the specific numbers though, this was
	   all just trial and error by tweaking numbers here and there, then
	   the formula was optimized doing basic maths.

	   You could replace this with some other random formula that gives
	   the same numbers and it would be just as correct.

	 */
		const double upper_threshold = threshold * 4.0;
		speed_in = min(speed_in, upper_threshold);

		factor = 0.0025 * (speed_in/threshold) * (speed_in - threshold) + baseline;
	}
304

305
	factor *= accel_filter->speed_factor;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	return factor * TP_MAGIC_SLOWDOWN;
}

struct motion_filter_interface accelerator_interface_touchpad = {
	.type = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE,
	.filter = accelerator_filter_post_normalized,
	.filter_constant = touchpad_constant_filter,
	.restart = touchpad_accelerator_restart,
	.destroy = touchpad_accelerator_destroy,
	.set_speed = touchpad_accelerator_set_speed,
};

struct motion_filter *
create_pointer_accelerator_filter_touchpad(int dpi,
	uint64_t event_delta_smooth_threshold,
321 322
	uint64_t event_delta_smooth_value,
	bool use_velocity_averaging)
323
{
324
	struct touchpad_accelerator *filter;
325 326
	struct pointer_delta_smoothener *smoothener;

327 328 329
	filter = zalloc(sizeof *filter);
	filter->last_velocity = 0.0;

330
	trackers_init(&filter->trackers, use_velocity_averaging ? 16 : 2);
331

332
	filter->threshold = 130;
333
	filter->dpi = dpi;
334 335 336 337 338 339 340 341 342 343 344

	filter->base.interface = &accelerator_interface_touchpad;
	filter->profile = touchpad_accel_profile_linear;

	smoothener = zalloc(sizeof(*smoothener));
	smoothener->threshold = event_delta_smooth_threshold,
	smoothener->value = event_delta_smooth_value,
	filter->trackers.smoothener = smoothener;

	return &filter->base;
}