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.
30 #include <wayland-util.h>
32 #include "compositor.h"
36 weston_filter_dispatch(struct weston_motion_filter *filter,
37 struct weston_motion_params *motion,
38 void *data, uint32_t time)
40 filter->interface->filter(filter, motion, data, time);
44 * Pointer acceleration filter
47 #define MAX_VELOCITY_DIFF 1.0
48 #define MOTION_TIMEOUT 300 /* (ms) */
49 #define NUM_POINTER_TRACKERS 16
51 struct pointer_tracker {
58 struct pointer_accelerator;
59 struct pointer_accelerator {
60 struct weston_motion_filter base;
62 accel_profile_func_t profile;
69 struct pointer_tracker *trackers;
82 UNDEFINED_DIRECTION = 0xff
86 get_direction(int dx, int dy)
88 int dir = UNDEFINED_DIRECTION;
92 if (abs(dx) < 2 && abs(dy) < 2) {
95 else if (dx > 0 && dy < 0)
97 else if (dx < 0 && dy > 0)
99 else if (dx < 0 && dy < 0)
111 /* Calculate r within the interval [0 to 8)
113 * r = [0 .. 2π] where 0 is North
114 * d_f = r / 2π ([0 .. 1))
118 r = fmod(r + 2.5*M_PI, 2*M_PI);
121 /* Mark one or two close enough octants */
122 d1 = (int)(r + 0.9) % 8;
123 d2 = (int)(r + 0.1) % 8;
125 dir = (1 << d1) | (1 << d2);
132 feed_trackers(struct pointer_accelerator *accel,
133 double dx, double dy,
137 struct pointer_tracker *trackers = accel->trackers;
139 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
140 trackers[i].dx += dx;
141 trackers[i].dy += dy;
144 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
145 accel->cur_tracker = current;
147 trackers[current].dx = 0.0;
148 trackers[current].dy = 0.0;
149 trackers[current].time = time;
150 trackers[current].dir = get_direction(dx, dy);
153 static struct pointer_tracker *
154 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
157 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
158 % NUM_POINTER_TRACKERS;
159 return &accel->trackers[index];
163 calculate_tracker_velocity(struct pointer_tracker *tracker, uint32_t time)
171 distance = sqrt(dx*dx + dy*dy);
172 return distance / (double)(time - tracker->time);
176 calculate_velocity(struct pointer_accelerator *accel, uint32_t time)
178 struct pointer_tracker *tracker;
181 double initial_velocity;
182 double velocity_diff;
185 unsigned int dir = tracker_by_offset(accel, 0)->dir;
187 /* Find first velocity */
188 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
189 tracker = tracker_by_offset(accel, offset);
191 if (time <= tracker->time)
194 result = initial_velocity =
195 calculate_tracker_velocity(tracker, time);
196 if (initial_velocity > 0.0)
200 /* Find least recent vector within a timelimit, maximum velocity diff
201 * and direction threshold. */
202 for (; 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 /* Stop if velocity differs too much from initial */
218 velocity_diff = fabs(initial_velocity - velocity);
219 if (velocity_diff > MAX_VELOCITY_DIFF)
229 acceleration_profile(struct pointer_accelerator *accel,
230 void *data, double velocity, uint32_t time)
232 return accel->profile(&accel->base, data, velocity, time);
236 calculate_acceleration(struct pointer_accelerator *accel,
237 void *data, double velocity, uint32_t time)
241 /* Use Simpson's rule to calculate the avarage acceleration between
242 * the previous motion and the most recent. */
243 factor = acceleration_profile(accel, data, velocity, time);
244 factor += acceleration_profile(accel, data, accel->last_velocity, time);
246 acceleration_profile(accel, data,
247 (accel->last_velocity + velocity) / 2,
250 factor = factor / 6.0;
256 soften_delta(double last_delta, double delta)
258 if (delta < -1.0 || delta > 1.0) {
259 if (delta > last_delta)
261 else if (delta < last_delta)
269 apply_softening(struct pointer_accelerator *accel,
270 struct weston_motion_params *motion)
272 motion->dx = soften_delta(accel->last_dx, motion->dx);
273 motion->dy = soften_delta(accel->last_dy, motion->dy);
277 accelerator_filter(struct weston_motion_filter *filter,
278 struct weston_motion_params *motion,
279 void *data, uint32_t time)
281 struct pointer_accelerator *accel =
282 (struct pointer_accelerator *) filter;
286 feed_trackers(accel, motion->dx, motion->dy, time);
287 velocity = calculate_velocity(accel, time);
288 accel_value = calculate_acceleration(accel, data, velocity, time);
290 motion->dx = accel_value * motion->dx;
291 motion->dy = accel_value * motion->dy;
293 apply_softening(accel, motion);
295 accel->last_dx = motion->dx;
296 accel->last_dy = motion->dy;
298 accel->last_velocity = velocity;
302 accelerator_destroy(struct weston_motion_filter *filter)
304 struct pointer_accelerator *accel =
305 (struct pointer_accelerator *) filter;
307 free(accel->trackers);
311 struct weston_motion_filter_interface accelerator_interface = {
316 struct weston_motion_filter *
317 create_pointer_accelator_filter(accel_profile_func_t profile)
319 struct pointer_accelerator *filter;
321 filter = malloc(sizeof *filter);
325 filter->base.interface = &accelerator_interface;
327 filter->profile = profile;
328 filter->last_velocity = 0.0;
333 calloc(NUM_POINTER_TRACKERS, sizeof *filter->trackers);
334 filter->cur_tracker = 0;
336 return &filter->base;