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 * Default parameters for pointer acceleration profiles.
45 #define DEFAULT_CONSTANT_ACCELERATION 10.0
46 #define DEFAULT_THRESHOLD 4.0
47 #define DEFAULT_ACCELERATION 2.0
50 * Pointer acceleration filter constants
53 #define MAX_VELOCITY_DIFF 1.0
54 #define MOTION_TIMEOUT 300 /* (ms) */
55 #define NUM_POINTER_TRACKERS 16
57 struct pointer_tracker {
64 struct pointer_accelerator;
65 struct pointer_accelerator {
66 struct motion_filter base;
68 accel_profile_func_t profile;
75 struct pointer_tracker *trackers;
88 UNDEFINED_DIRECTION = 0xff
92 get_direction(int dx, int dy)
94 int dir = UNDEFINED_DIRECTION;
98 if (abs(dx) < 2 && abs(dy) < 2) {
101 else if (dx > 0 && dy < 0)
103 else if (dx < 0 && dy > 0)
105 else if (dx < 0 && dy < 0)
116 /* Calculate r within the interval [0 to 8)
118 * r = [0 .. 2π] where 0 is North
119 * d_f = r / 2π ([0 .. 1))
123 r = fmod(r + 2.5*M_PI, 2*M_PI);
126 /* Mark one or two close enough octants */
127 d1 = (int)(r + 0.9) % 8;
128 d2 = (int)(r + 0.1) % 8;
130 dir = (1 << d1) | (1 << d2);
137 feed_trackers(struct pointer_accelerator *accel,
138 double dx, double dy,
142 struct pointer_tracker *trackers = accel->trackers;
144 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
145 trackers[i].dx += dx;
146 trackers[i].dy += dy;
149 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
150 accel->cur_tracker = current;
152 trackers[current].dx = 0.0;
153 trackers[current].dy = 0.0;
154 trackers[current].time = time;
155 trackers[current].dir = get_direction(dx, dy);
158 static struct pointer_tracker *
159 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
162 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
163 % NUM_POINTER_TRACKERS;
164 return &accel->trackers[index];
168 calculate_tracker_velocity(struct pointer_tracker *tracker, uint64_t time)
176 distance = sqrt(dx*dx + dy*dy);
177 return distance / (double)(time - tracker->time);
181 calculate_velocity(struct pointer_accelerator *accel, uint64_t time)
183 struct pointer_tracker *tracker;
186 double initial_velocity = 0.0;
187 double velocity_diff;
190 unsigned int dir = tracker_by_offset(accel, 0)->dir;
192 /* Find least recent vector within a timelimit, maximum velocity diff
193 * and direction threshold. */
194 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
195 tracker = tracker_by_offset(accel, offset);
197 /* Stop if too far away in time */
198 if (time - tracker->time > MOTION_TIMEOUT ||
199 tracker->time > time)
202 /* Stop if direction changed */
207 velocity = calculate_tracker_velocity(tracker, time);
209 if (initial_velocity == 0.0) {
210 result = initial_velocity = velocity;
212 /* Stop if velocity differs too much from initial */
213 velocity_diff = fabs(initial_velocity - velocity);
214 if (velocity_diff > MAX_VELOCITY_DIFF)
225 acceleration_profile(struct pointer_accelerator *accel,
226 void *data, double velocity, uint64_t time)
228 return accel->profile(&accel->base, data, velocity, time);
232 calculate_acceleration(struct pointer_accelerator *accel,
233 void *data, double velocity, uint64_t time)
237 /* Use Simpson's rule to calculate the avarage acceleration between
238 * the previous motion and the most recent. */
239 factor = acceleration_profile(accel, data, velocity, time);
240 factor += acceleration_profile(accel, data, accel->last_velocity, time);
242 acceleration_profile(accel, data,
243 (accel->last_velocity + velocity) / 2,
246 factor = factor / 6.0;
252 soften_delta(double last_delta, double delta)
254 if (delta < -1.0 || delta > 1.0) {
255 if (delta > last_delta)
257 else if (delta < last_delta)
265 apply_softening(struct pointer_accelerator *accel,
266 struct motion_params *motion)
268 motion->dx = soften_delta(accel->last_dx, motion->dx);
269 motion->dy = soften_delta(accel->last_dy, motion->dy);
273 accelerator_filter(struct motion_filter *filter,
274 struct motion_params *motion,
275 void *data, uint64_t time)
277 struct pointer_accelerator *accel =
278 (struct pointer_accelerator *) filter;
282 feed_trackers(accel, motion->dx, motion->dy, time);
283 velocity = calculate_velocity(accel, time);
284 accel_value = calculate_acceleration(accel, data, velocity, time);
286 motion->dx = accel_value * motion->dx;
287 motion->dy = accel_value * motion->dy;
289 apply_softening(accel, motion);
291 accel->last_dx = motion->dx;
292 accel->last_dy = motion->dy;
294 accel->last_velocity = velocity;
298 accelerator_destroy(struct motion_filter *filter)
300 struct pointer_accelerator *accel =
301 (struct pointer_accelerator *) filter;
303 free(accel->trackers);
307 struct motion_filter_interface accelerator_interface = {
312 struct motion_filter *
313 create_pointer_accelator_filter(accel_profile_func_t profile)
315 struct pointer_accelerator *filter;
317 filter = malloc(sizeof *filter);
321 filter->base.interface = &accelerator_interface;
323 filter->profile = profile;
324 filter->last_velocity = 0.0;
329 calloc(NUM_POINTER_TRACKERS, sizeof *filter->trackers);
330 filter->cur_tracker = 0;
332 return &filter->base;
336 motion_filter_destroy(struct motion_filter *filter)
341 filter->interface->destroy(filter);
345 calc_penumbral_gradient(double x)
349 return 0.5 + (x * sqrt(1.0 - x * x) + asin(x)) / M_PI;
353 pointer_accel_profile_smooth_simple(struct motion_filter *filter,
358 double threshold = DEFAULT_THRESHOLD;
359 double accel = DEFAULT_ACCELERATION;
360 double smooth_accel_coefficient;
362 velocity *= DEFAULT_CONSTANT_ACCELERATION;
365 return calc_penumbral_gradient(0.5 + velocity * 0.5) * 2.0 - 1.0;
368 if (velocity <= threshold)
370 velocity /= threshold;
371 if (velocity >= accel) {
374 smooth_accel_coefficient =
375 calc_penumbral_gradient(velocity / accel);
376 return 1.0 + (smooth_accel_coefficient * (accel - 1.0));