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_THRESHOLD 0.4 /* in units/ms */
55 #define DEFAULT_ACCELERATION 2.0 /* unitless factor */
58 * Pointer acceleration filter constants
61 #define MAX_VELOCITY_DIFF 1.0 /* units/ms */
62 #define MOTION_TIMEOUT 300 /* (ms) */
63 #define NUM_POINTER_TRACKERS 16
65 struct pointer_tracker {
66 double dx; /* delta to most recent event, in device units */
67 double dy; /* delta to most recent event, in device units */
68 uint64_t time; /* ms */
72 struct pointer_accelerator;
73 struct pointer_accelerator {
74 struct motion_filter base;
76 accel_profile_func_t profile;
78 double velocity; /* units/ms */
79 double last_velocity; /* units/ms */
80 int last_dx; /* device units */
81 int last_dy; /* device units */
83 struct pointer_tracker *trackers;
96 UNDEFINED_DIRECTION = 0xff
100 get_direction(int dx, int dy)
102 int dir = UNDEFINED_DIRECTION;
106 if (abs(dx) < 2 && abs(dy) < 2) {
107 if (dx > 0 && dy > 0)
109 else if (dx > 0 && dy < 0)
111 else if (dx < 0 && dy > 0)
113 else if (dx < 0 && dy < 0)
124 /* Calculate r within the interval [0 to 8)
126 * r = [0 .. 2π] where 0 is North
127 * d_f = r / 2π ([0 .. 1))
131 r = fmod(r + 2.5*M_PI, 2*M_PI);
134 /* Mark one or two close enough octants */
135 d1 = (int)(r + 0.9) % 8;
136 d2 = (int)(r + 0.1) % 8;
138 dir = (1 << d1) | (1 << d2);
145 feed_trackers(struct pointer_accelerator *accel,
146 double dx, double dy,
150 struct pointer_tracker *trackers = accel->trackers;
152 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
153 trackers[i].dx += dx;
154 trackers[i].dy += dy;
157 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
158 accel->cur_tracker = current;
160 trackers[current].dx = 0.0;
161 trackers[current].dy = 0.0;
162 trackers[current].time = time;
163 trackers[current].dir = get_direction(dx, dy);
166 static struct pointer_tracker *
167 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
170 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
171 % NUM_POINTER_TRACKERS;
172 return &accel->trackers[index];
176 calculate_tracker_velocity(struct pointer_tracker *tracker, uint64_t time)
184 distance = sqrt(dx*dx + dy*dy);
185 return distance / (double)(time - tracker->time); /* units/ms */
189 calculate_velocity(struct pointer_accelerator *accel, uint64_t time)
191 struct pointer_tracker *tracker;
194 double initial_velocity = 0.0;
195 double velocity_diff;
198 unsigned int dir = tracker_by_offset(accel, 0)->dir;
200 /* Find least recent vector within a timelimit, maximum velocity diff
201 * and direction threshold. */
202 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
203 tracker = tracker_by_offset(accel, offset);
205 /* Stop if too far away in time */
206 if (time - tracker->time > MOTION_TIMEOUT ||
207 tracker->time > time)
210 /* Stop if direction changed */
215 velocity = calculate_tracker_velocity(tracker, time);
217 if (initial_velocity == 0.0) {
218 result = initial_velocity = velocity;
220 /* Stop if velocity differs too much from initial */
221 velocity_diff = fabs(initial_velocity - velocity);
222 if (velocity_diff > MAX_VELOCITY_DIFF)
229 return result; /* units/ms */
233 acceleration_profile(struct pointer_accelerator *accel,
234 void *data, double velocity, uint64_t time)
236 return accel->profile(&accel->base, data, velocity, time);
240 calculate_acceleration(struct pointer_accelerator *accel,
241 void *data, double velocity, uint64_t time)
245 /* Use Simpson's rule to calculate the avarage acceleration between
246 * the previous motion and the most recent. */
247 factor = acceleration_profile(accel, data, velocity, time);
248 factor += acceleration_profile(accel, data, accel->last_velocity, time);
250 acceleration_profile(accel, data,
251 (accel->last_velocity + velocity) / 2,
254 factor = factor / 6.0;
256 return factor; /* unitless factor */
260 accelerator_filter(struct motion_filter *filter,
261 struct motion_params *motion,
262 void *data, uint64_t time)
264 struct pointer_accelerator *accel =
265 (struct pointer_accelerator *) filter;
266 double velocity; /* units/ms */
267 double accel_value; /* unitless factor */
269 feed_trackers(accel, motion->dx, motion->dy, time);
270 velocity = calculate_velocity(accel, time);
271 accel_value = calculate_acceleration(accel, data, velocity, time);
273 motion->dx = accel_value * motion->dx;
274 motion->dy = accel_value * motion->dy;
276 accel->last_dx = motion->dx;
277 accel->last_dy = motion->dy;
279 accel->last_velocity = velocity;
283 accelerator_destroy(struct motion_filter *filter)
285 struct pointer_accelerator *accel =
286 (struct pointer_accelerator *) filter;
288 free(accel->trackers);
292 struct motion_filter_interface accelerator_interface = {
297 struct motion_filter *
298 create_pointer_accelator_filter(accel_profile_func_t profile)
300 struct pointer_accelerator *filter;
302 filter = malloc(sizeof *filter);
306 filter->base.interface = &accelerator_interface;
308 filter->profile = profile;
309 filter->last_velocity = 0.0;
314 calloc(NUM_POINTER_TRACKERS, sizeof *filter->trackers);
315 filter->cur_tracker = 0;
317 return &filter->base;
321 calc_penumbral_gradient(double x)
325 return 0.5 + (x * sqrt(1.0 - x * x) + asin(x)) / M_PI;
329 pointer_accel_profile_smooth_simple(struct motion_filter *filter,
331 double velocity, /* units/ms */
334 double threshold = DEFAULT_THRESHOLD; /* units/ms */
335 double accel = DEFAULT_ACCELERATION; /* unitless factor */
336 double smooth_accel_coefficient; /* unitless factor */
337 double factor; /* unitless factor */
345 if (velocity < (threshold / 2.0))
346 return calc_penumbral_gradient(0.5 + velocity / threshold) * 2.0 - 1.0;
348 if (velocity <= threshold)
351 factor = velocity/threshold;
355 /* factor is between 1.0 and accel, scale this to 0.0 - 1.0 */
356 factor = (factor - 1.0) / (accel - 1.0);
357 smooth_accel_coefficient = calc_penumbral_gradient(factor);
358 return 1.0 + (smooth_accel_coefficient * (accel - 1.0));