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 <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32
33 #include "filter.h"
34 #include "libinput-util.h"
35 #include "filter-private.h"
36
37 struct trackpoint_flat_accelerator {
38 struct motion_filter base;
39
40 double speed_factor;
41 double multiplier;
42 };
43
44 static struct normalized_coords
trackpoint_flat_filter(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)45 trackpoint_flat_filter(struct motion_filter *filter,
46 const struct device_float_coords *unaccelerated,
47 void *data, uint64_t time)
48 {
49 struct trackpoint_flat_accelerator *accel_filter =
50 (struct trackpoint_flat_accelerator *) filter;
51 struct normalized_coords accelerated;
52
53 double factor = accel_filter->speed_factor;
54 double multiplier = accel_filter->multiplier;
55 accelerated.x = factor * multiplier * unaccelerated->x;
56 accelerated.y = factor * multiplier * unaccelerated->y;
57
58 return accelerated;
59 }
60
61 static struct normalized_coords
trackpoint_flat_filter_noop(struct motion_filter * filter,const struct device_float_coords * unaccelerated,void * data,uint64_t time)62 trackpoint_flat_filter_noop(struct motion_filter *filter,
63 const struct device_float_coords *unaccelerated,
64 void *data, uint64_t time)
65 {
66 /* We map the unaccelerated flat filter to have the same behavior as
67 * the "accelerated" flat filter.
68 * The filter by definition is flat, i.e. it does not actually
69 * apply any acceleration (merely a constant factor) and we can assume
70 * that a user wants all mouse movement to have the same speed, mapped
71 * 1:1 to the input speed.
72 *
73 * Thus we apply the same factor to our non-accelerated motion - this way
74 * things like button scrolling end up having the same movement as
75 * pointer motion.
76 */
77 return trackpoint_flat_filter(filter, unaccelerated, data, time);
78 }
79
80 /* Maps the [-1, 1] speed setting into a constant acceleration
81 * range. This isn't a linear scale, we keep 0 as the 'optimized'
82 * mid-point and scale down to 0 for setting -1 and up to 5 for
83 * setting 1. On the premise that if you want a faster cursor, it
84 * doesn't matter as much whether you have 0.56789 or 0.56790,
85 * but for lower settings it does because you may lose movements.
86 * *shrug*.
87 *
88 * Magic numbers calculated by MyCurveFit.com, data points were
89 * 0.0 0.0
90 * 0.1 0.1 (because we need 4 points)
91 * 1 1
92 * 2 5
93 *
94 * This curve fits nicely into the range necessary.
95 */
96 static inline double
speed_factor(double s)97 speed_factor(double s)
98 {
99 s += 1; /* map to [0, 2] */
100 return 435837.2 + (0.04762636 - 435837.2)/(1 + pow(s/240.4549,
101 2.377168));
102 }
103
104 static bool
trackpoint_flat_set_speed(struct motion_filter * filter,double speed_adjustment)105 trackpoint_flat_set_speed(struct motion_filter *filter,
106 double speed_adjustment)
107 {
108 struct trackpoint_flat_accelerator *accel_filter =
109 (struct trackpoint_flat_accelerator *) filter;
110
111 assert(speed_adjustment >= -1.0 && speed_adjustment <= 1.0);
112
113 filter->speed_adjustment = speed_adjustment;
114 accel_filter->speed_factor = speed_factor(speed_adjustment);
115
116 return true;
117 }
118
119 static void
trackpoint_flat_destroy(struct motion_filter * filter)120 trackpoint_flat_destroy(struct motion_filter *filter)
121 {
122 struct trackpoint_flat_accelerator *accel_filter =
123 (struct trackpoint_flat_accelerator *) filter;
124
125 free(accel_filter);
126 }
127
128 static struct motion_filter_interface accelerator_interface_flat = {
129 .type = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT,
130 .filter = trackpoint_flat_filter,
131 .filter_constant = trackpoint_flat_filter_noop,
132 .filter_scroll = trackpoint_flat_filter_noop,
133 .restart = NULL,
134 .destroy = trackpoint_flat_destroy,
135 .set_speed = trackpoint_flat_set_speed,
136 };
137
138 struct motion_filter *
create_pointer_accelerator_filter_trackpoint_flat(double multiplier)139 create_pointer_accelerator_filter_trackpoint_flat(double multiplier)
140 {
141 struct trackpoint_flat_accelerator *filter;
142
143 filter = zalloc(sizeof *filter);
144 filter->base.interface = &accelerator_interface_flat;
145 filter->multiplier = multiplier;
146
147 return &filter->base;
148 }
149