2 * Copyright © 2012 Jonas Ådahl
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 filter_dispatch(struct motion_filter *filter,
35 struct motion_params *motion,
36 void *data, uint64_t time)
38 filter->interface->filter(filter, motion, data, time);
42 filter_destroy(struct motion_filter *filter)
47 filter->interface->destroy(filter);
51 * Default parameters for pointer acceleration profiles.
54 #define DEFAULT_CONSTANT_ACCELERATION 10.0 /* unitless factor */
55 #define DEFAULT_THRESHOLD 4.0 /* in units/ms */
56 #define DEFAULT_ACCELERATION 2.0 /* unitless factor */
59 * Pointer acceleration filter constants
62 #define MAX_VELOCITY_DIFF 1.0 /* units/ms */
63 #define MOTION_TIMEOUT 300 /* (ms) */
64 #define NUM_POINTER_TRACKERS 16
66 struct pointer_tracker {
67 double dx; /* delta to most recent event, in device units */
68 double dy; /* delta to most recent event, in device units */
69 uint64_t time; /* ms */
73 struct pointer_accelerator;
74 struct pointer_accelerator {
75 struct motion_filter base;
77 accel_profile_func_t profile;
79 double velocity; /* units/ms */
80 double last_velocity; /* units/ms */
81 int last_dx; /* device units */
82 int last_dy; /* device units */
84 struct pointer_tracker *trackers;
97 UNDEFINED_DIRECTION = 0xff
101 get_direction(int dx, int dy)
103 int dir = UNDEFINED_DIRECTION;
107 if (abs(dx) < 2 && abs(dy) < 2) {
108 if (dx > 0 && dy > 0)
110 else if (dx > 0 && dy < 0)
112 else if (dx < 0 && dy > 0)
114 else if (dx < 0 && dy < 0)
125 /* Calculate r within the interval [0 to 8)
127 * r = [0 .. 2π] where 0 is North
128 * d_f = r / 2π ([0 .. 1))
132 r = fmod(r + 2.5*M_PI, 2*M_PI);
135 /* Mark one or two close enough octants */
136 d1 = (int)(r + 0.9) % 8;
137 d2 = (int)(r + 0.1) % 8;
139 dir = (1 << d1) | (1 << d2);
146 feed_trackers(struct pointer_accelerator *accel,
147 double dx, double dy,
151 struct pointer_tracker *trackers = accel->trackers;
153 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
154 trackers[i].dx += dx;
155 trackers[i].dy += dy;
158 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
159 accel->cur_tracker = current;
161 trackers[current].dx = 0.0;
162 trackers[current].dy = 0.0;
163 trackers[current].time = time;
164 trackers[current].dir = get_direction(dx, dy);
167 static struct pointer_tracker *
168 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
171 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
172 % NUM_POINTER_TRACKERS;
173 return &accel->trackers[index];
177 calculate_tracker_velocity(struct pointer_tracker *tracker, uint64_t time)
185 distance = sqrt(dx*dx + dy*dy);
186 return distance / (double)(time - tracker->time); /* units/ms */
190 calculate_velocity(struct pointer_accelerator *accel, uint64_t time)
192 struct pointer_tracker *tracker;
195 double initial_velocity = 0.0;
196 double velocity_diff;
199 unsigned int dir = tracker_by_offset(accel, 0)->dir;
201 /* Find least recent vector within a timelimit, maximum velocity diff
202 * and direction threshold. */
203 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
204 tracker = tracker_by_offset(accel, offset);
206 /* Stop if too far away in time */
207 if (time - tracker->time > MOTION_TIMEOUT ||
208 tracker->time > time)
211 /* Stop if direction changed */
216 velocity = calculate_tracker_velocity(tracker, time);
218 if (initial_velocity == 0.0) {
219 result = initial_velocity = velocity;
221 /* Stop if velocity differs too much from initial */
222 velocity_diff = fabs(initial_velocity - velocity);
223 if (velocity_diff > MAX_VELOCITY_DIFF)
230 return result; /* units/ms */
234 acceleration_profile(struct pointer_accelerator *accel,
235 void *data, double velocity, uint64_t time)
237 return accel->profile(&accel->base, data, velocity, time);
241 calculate_acceleration(struct pointer_accelerator *accel,
242 void *data, double velocity, uint64_t time)
246 /* Use Simpson's rule to calculate the avarage acceleration between
247 * the previous motion and the most recent. */
248 factor = acceleration_profile(accel, data, velocity, time);
249 factor += acceleration_profile(accel, data, accel->last_velocity, time);
251 acceleration_profile(accel, data,
252 (accel->last_velocity + velocity) / 2,
255 factor = factor / 6.0;
257 return factor; /* unitless factor */
261 accelerator_filter(struct motion_filter *filter,
262 struct motion_params *motion,
263 void *data, uint64_t time)
265 struct pointer_accelerator *accel =
266 (struct pointer_accelerator *) filter;
267 double velocity; /* units/ms */
268 double accel_value; /* unitless factor */
270 feed_trackers(accel, motion->dx, motion->dy, time);
271 velocity = calculate_velocity(accel, time);
272 accel_value = calculate_acceleration(accel, data, velocity, time);
274 motion->dx = accel_value * motion->dx;
275 motion->dy = accel_value * motion->dy;
277 accel->last_dx = motion->dx;
278 accel->last_dy = motion->dy;
280 accel->last_velocity = velocity;
284 accelerator_destroy(struct motion_filter *filter)
286 struct pointer_accelerator *accel =
287 (struct pointer_accelerator *) filter;
289 free(accel->trackers);
293 struct motion_filter_interface accelerator_interface = {
298 struct motion_filter *
299 create_pointer_accelator_filter(accel_profile_func_t profile)
301 struct pointer_accelerator *filter;
303 filter = malloc(sizeof *filter);
307 filter->base.interface = &accelerator_interface;
309 filter->profile = profile;
310 filter->last_velocity = 0.0;
315 calloc(NUM_POINTER_TRACKERS, sizeof *filter->trackers);
316 filter->cur_tracker = 0;
318 return &filter->base;
322 calc_penumbral_gradient(double x)
326 return 0.5 + (x * sqrt(1.0 - x * x) + asin(x)) / M_PI;
330 pointer_accel_profile_smooth_simple(struct motion_filter *filter,
332 double velocity, /* units/ms */
335 double threshold = DEFAULT_THRESHOLD; /* units/ms */
336 double accel = DEFAULT_ACCELERATION; /* unitless factor */
337 double smooth_accel_coefficient; /* unitless factor */
338 double factor; /* unitless factor */
345 velocity *= DEFAULT_CONSTANT_ACCELERATION;
347 if (velocity < (threshold / 2.0))
348 return calc_penumbral_gradient(0.5 + velocity / threshold) * 2.0 - 1.0;
350 if (velocity <= threshold)
353 factor = velocity/threshold;
357 /* factor is between 1.0 and accel, scale this to 0.0 - 1.0 */
358 factor = (factor - 1.0) / (accel - 1.0);
359 smooth_accel_coefficient = calc_penumbral_gradient(factor);
360 return 1.0 + (smooth_accel_coefficient * (accel - 1.0));