summaryrefslogtreecommitdiff
path: root/src/filter-custom.c
blob: b2eb75c8ccecdd962dcfae12775d3844b1ef1c12 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * 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 "filter.h"
#include "filter-private.h"

#define MOTION_TIMEOUT ms2us(1000)
#define FIRST_MOTION_TIME_INTERVAL ms2us(7) /* random but good enough interval for very first event */

struct custom_accel_function {
	uint64_t last_time;
	double step;
	size_t npoints;
	double points[];
};

static struct custom_accel_function *
create_custom_accel_function(double step, const double *points, size_t npoints)
{
	if (npoints < LIBINPUT_ACCEL_NPOINTS_MIN ||
	    npoints > LIBINPUT_ACCEL_NPOINTS_MAX)
		return NULL;

	if (step <= 0 || step > LIBINPUT_ACCEL_STEP_MAX)
		return NULL;

	for (size_t idx = 0; idx < npoints; idx++) {
		if (points[idx] < LIBINPUT_ACCEL_POINT_MIN_VALUE ||
		    points[idx] > LIBINPUT_ACCEL_POINT_MAX_VALUE)
			return NULL;
	}

	struct custom_accel_function *cf = zalloc(sizeof(*cf) + npoints * sizeof(*points));
	cf->last_time = 0;
	cf->step = step;
	cf->npoints = npoints;
	memcpy(cf->points, points, sizeof(*points) * npoints);

	return cf;
}

static void
custom_accel_function_destroy(struct custom_accel_function *cf)
{
	if (cf == NULL)
		return;

	free(cf);
}

static double
custom_accel_function_calculate_speed(struct custom_accel_function *cf,
				      const struct device_float_coords *unaccelerated,
				      uint64_t time)
{
	/* Although most devices have a constant polling rate, and for fast
	 * movements these distances do represent the actual speed,
	 * for slow movements it is not the case.
	 *
	 * Since all devices have a finite resolution, real world events
	 * for a slow smooth movement could look like:
	 *   Event 1 - (0,  1) - time 0
	 *   Event 2 - (0,  0) - time 7  - filtered (zero event)
	 *   Event 3 - (1,  0) - time 14
	 *   Event 4 - (0,  0) - time 21 - filtered (zero event)
	 *   Event 5 - (0,  0) - time 28 - filtered (zero event)
	 *   Event 6 - (0,  1) - time 35
	 *
	 * Not taking the time into account would mean interpreting those events as:
	 *   Move 1 unit over 7 ms
	 *   Pause for 7 ms
	 *   Move 1 unit over 7 ms
	 *   Pause for 14 ms
	 *   Move 1 unit over 7ms
	 *
	 * Where in reality this was one smooth movement without pauses,
	 * so after normalizing for time we get:
	 *   Move 1 unit over 7 ms
	 *   Move 1 unit over 14 ms
	 *   Move 1 unit over 21ms
	 *
	 * which should give us better speed estimation.
	 */

	/* calculate speed based on time passed since last event */
	double distance = hypot(unaccelerated->x, unaccelerated->y);
	/* handle first event in a motion */
	if (time - cf->last_time > MOTION_TIMEOUT)
		cf->last_time = time - FIRST_MOTION_TIME_INTERVAL;

	double dt = us2ms_f(time - cf->last_time);
	double speed = distance / dt; /* speed is in device-units per ms */
	cf->last_time = time;

	return speed;
}

static double
custom_accel_function_profile(struct custom_accel_function *cf,
			      double speed_in)
{
	size_t npoints = cf->npoints;
	double step = cf->step;
	double *points = cf->points;

	/* calculate the index of the first point used for interpolation */
	size_t i = speed_in / step;

	/* if speed is greater than custom curve's max speed,
	   use last 2 points for linear extrapolation
	   (same calculation as linear interpolation) */
	i = min(i, npoints - 2);

	/* the 2 points used for linear interpolation */
	double x0 = step * i;
	double x1 = step * (i + 1);
	double y0 = points[i];
	double y1 = points[i + 1];

	/* linear interpolation */
	double speed_out = (y0 * (x1 - speed_in) + y1 * (speed_in - x0)) / step;

	/* We moved (dx, dy) device units within the last N ms. This gives us a
	 * given speed S in units/ms, that's our accel input. Our curve says map
	 * that speed S to some other speed S'.
	 *
	 * Our device delta is represented by the vector, that vector needs to
	 * be modified to represent our intended speed.
	 *
	 * Example: we moved a delta of 7 over the last 7ms. Our speed is
	 * thus 1 u/ms, our out speed is 2 u/ms because we want to double our
	 * speed (points: [0.0, 2.0]). Our delta must thus be 14 - factor of 2,
	 * or out-speed/in-speed.
	 *
	 * Example: we moved a delta of 1 over the last 7ms. Our input speed is
	 * 1/7 u/ms, our out speed is 1/7ms because we set up a flat accel
	 * curve (points: [0.0, 1.0]). Our delta must thus be 1 - factor of 1,
	 * or out-speed/in-speed.
	 *
	 * Example: we moved a delta of 1 over the last 21ms. Our input speed is
	 * 1/21 u/ms, our out speed is 1u/ms because we set up a fixed-speed
	 * curve (points: [1.0, 1.0]). Our delta must thus be 21 - factor of 21,
	 * or out-speed/in-speed.
	 *
	 * Example: we moved a delta of 21 over the last 7ms. Our input speed is
	 * 3 u/ms, our out speed is 1u/ms because we set up a fixed-speed
	 * curve (points: [1.0, 1.0]). Our delta must thus be 7 - factor of 1/3,
	 * or out-speed/in-speed.
	 */

	/* calculate the acceleration factor based on the user desired speed out */
	double accel_factor = speed_out / speed_in;

	return accel_factor;
}

static struct normalized_coords
custom_accel_function_filter(struct custom_accel_function *cf,
			     const struct device_float_coords *unaccelerated,
			     uint64_t time)
{
	double speed = custom_accel_function_calculate_speed(cf, unaccelerated, time);

	double accel_factor = custom_accel_function_profile(cf, speed);

	struct normalized_coords accelerated = {
		.x = unaccelerated->x * accel_factor,
		.y = unaccelerated->y * accel_factor,
	};

	return accelerated;
}

struct custom_accelerator {
	struct motion_filter base;
	struct {
		struct custom_accel_function *fallback;
		struct custom_accel_function *motion;
		struct custom_accel_function *scroll;
	} funcs;
};

static struct custom_accel_function *
custom_accelerator_get_custom_function(struct custom_accelerator *f,
				       enum libinput_config_accel_type accel_type)
{
	switch (accel_type) {
	case LIBINPUT_ACCEL_TYPE_FALLBACK:
		return f->funcs.fallback;
	case LIBINPUT_ACCEL_TYPE_MOTION:
		return f->funcs.motion ? f->funcs.motion : f->funcs.fallback;
	case LIBINPUT_ACCEL_TYPE_SCROLL:
		return f->funcs.scroll ? f->funcs.scroll : f->funcs.fallback;
	}

	return f->funcs.fallback;
}

static double
custom_accelerator_profile(enum libinput_config_accel_type accel_type,
			   struct motion_filter *filter,
			   double speed_in)
{
	struct custom_accelerator *f = (struct custom_accelerator *)filter;
	struct custom_accel_function *cf;

	cf = custom_accelerator_get_custom_function(f, accel_type);

	return custom_accel_function_profile(cf, speed_in);
}

static struct normalized_coords
custom_accelerator_filter(enum libinput_config_accel_type accel_type,
			  struct motion_filter *filter,
			  const struct device_float_coords *unaccelerated,
			  uint64_t time)
{
	struct custom_accelerator *f = (struct custom_accelerator *)filter;
	struct custom_accel_function *cf;

	cf = custom_accelerator_get_custom_function(f, accel_type);

	return custom_accel_function_filter(cf, unaccelerated, time);
}

static void
custom_accelerator_restart(struct motion_filter *filter,
			   void *data,
			   uint64_t time)
{
	/* noop, this function has no effect in the custom interface */
}

static void
custom_accelerator_destroy(struct motion_filter *filter)
{
	struct custom_accelerator *f =
		(struct custom_accelerator *)filter;

	/* destroy all custom movement functions */
	custom_accel_function_destroy(f->funcs.fallback);
	custom_accel_function_destroy(f->funcs.motion);
	custom_accel_function_destroy(f->funcs.scroll);
	free(f);
}

static bool
custom_accelerator_set_speed(struct motion_filter *filter,
			     double speed_adjustment)
{
	assert(speed_adjustment >= -1.0 && speed_adjustment <= 1.0);

	/* noop, this function has no effect in the custom interface */

	return true;
}

static bool
custom_accelerator_set_accel_config(struct motion_filter *filter,
				    struct libinput_config_accel *config)
{
	struct custom_accelerator *f =
		(struct custom_accelerator *)filter;

	struct custom_accel_function *fallback = NULL,
				     *motion = NULL,
				     *scroll = NULL;

	if (config->custom.fallback) {
		fallback = create_custom_accel_function(config->custom.fallback->step,
							config->custom.fallback->points,
							config->custom.fallback->npoints);
		if (!fallback)
			goto out;
	}

	if (config->custom.motion) {
		motion = create_custom_accel_function(config->custom.motion->step,
						      config->custom.motion->points,
						      config->custom.motion->npoints);
		if (!motion)
			goto out;
	}

	if (config->custom.scroll) {
		scroll = create_custom_accel_function(config->custom.scroll->step,
						      config->custom.scroll->points,
						      config->custom.scroll->npoints);
		if (!scroll)
			goto out;
	}

	custom_accel_function_destroy(f->funcs.fallback);
	custom_accel_function_destroy(f->funcs.motion);
	custom_accel_function_destroy(f->funcs.scroll);

	f->funcs.fallback = fallback;
	f->funcs.motion = motion;
	f->funcs.scroll = scroll;

	return true;

out:
	custom_accel_function_destroy(fallback);
	custom_accel_function_destroy(motion);
	custom_accel_function_destroy(scroll);

	return false;
}

/* custom profiles and filters for the different accel types: */

double
custom_accel_profile_fallback(struct motion_filter *filter,
			      void *data,
			      double speed_in,
			      uint64_t time)
{
	return custom_accelerator_profile(LIBINPUT_ACCEL_TYPE_FALLBACK,
					  filter,
					  speed_in);
}

static struct normalized_coords
custom_accelerator_filter_fallback(struct motion_filter *filter,
				   const struct device_float_coords *unaccelerated,
				   void *data,
				   uint64_t time)
{
	return custom_accelerator_filter(LIBINPUT_ACCEL_TYPE_FALLBACK,
					 filter,
					 unaccelerated,
					 time);
}

double
custom_accel_profile_motion(struct motion_filter *filter,
			    void *data,
			    double speed_in,
			    uint64_t time)
{
	return custom_accelerator_profile(LIBINPUT_ACCEL_TYPE_MOTION,
					  filter,
					  speed_in);
}

static struct normalized_coords
custom_accelerator_filter_motion(struct motion_filter *filter,
				 const struct device_float_coords *unaccelerated,
				 void *data,
				 uint64_t time)
{
	return custom_accelerator_filter(LIBINPUT_ACCEL_TYPE_MOTION,
					 filter,
					 unaccelerated,
					 time);
}

double
custom_accel_profile_scroll(struct motion_filter *filter,
			    void *data,
			    double speed_in,
			    uint64_t time)
{
	return custom_accelerator_profile(LIBINPUT_ACCEL_TYPE_SCROLL,
					  filter,
					  speed_in);
}

static struct normalized_coords
custom_accelerator_filter_scroll(struct motion_filter *filter,
				 const struct device_float_coords *unaccelerated,
				 void *data,
				 uint64_t time)
{
	return custom_accelerator_filter(LIBINPUT_ACCEL_TYPE_SCROLL,
					 filter,
					 unaccelerated,
					 time);
}

struct motion_filter_interface custom_accelerator_interface = {
	.type = LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM,
	.filter = custom_accelerator_filter_motion,
	.filter_constant = custom_accelerator_filter_fallback,
	.filter_scroll = custom_accelerator_filter_scroll,
	.restart = custom_accelerator_restart,
	.destroy = custom_accelerator_destroy,
	.set_speed = custom_accelerator_set_speed,
	.set_accel_config = custom_accelerator_set_accel_config,
};

struct motion_filter *
create_custom_accelerator_filter(void)
{
	struct custom_accelerator *f = zalloc(sizeof(*f));

	/* the unit function by default, speed in = speed out,
	   i.e. no acceleration */
	const double default_step = 1.0;
	const double default_points[2] = {0.0, 1.0};

	/* initialize default acceleration, used as fallback */
	f->funcs.fallback = create_custom_accel_function(default_step,
							 default_points,
							 ARRAY_LENGTH(default_points));
	/* Don't initialize other acceleration functions. Those will be
	   initialized if the user sets their points, otherwise the fallback
	   acceleration function is used */

	f->base.interface = &custom_accelerator_interface;

	return &f->base;
}