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.
29 #include <wayland-util.h>
31 #include "compositor.h"
35 weston_filter_dispatch(struct weston_motion_filter *filter,
36 struct weston_motion_params *motion,
37 void *data, uint32_t time)
39 filter->interface->filter(filter, motion, data, time);
43 * Pointer acceleration filter
46 #define MAX_VELOCITY_DIFF 1.0
47 #define MOTION_TIMEOUT 300 /* (ms) */
48 #define NUM_POINTER_TRACKERS 16
50 struct pointer_tracker {
57 struct pointer_accelerator;
58 struct pointer_accelerator {
59 struct weston_motion_filter base;
61 accel_profile_func_t profile;
68 struct pointer_tracker *trackers;
81 UNDEFINED_DIRECTION = 0xff
85 get_direction(int dx, int dy)
87 int dir = UNDEFINED_DIRECTION;
91 if (abs(dx) < 2 && abs(dy) < 2) {
94 else if (dx > 0 && dy < 0)
96 else if (dx < 0 && dy > 0)
98 else if (dx < 0 && dy < 0)
110 /* Calculate r within the interval [0 to 8)
112 * r = [0 .. 2π] where 0 is North
113 * d_f = r / 2π ([0 .. 1))
117 r = fmod(r + 2.5*M_PI, 2*M_PI);
120 /* Mark one or two close enough octants */
121 d1 = (int)(r + 0.9) % 8;
122 d2 = (int)(r + 0.1) % 8;
124 dir = (1 << d1) | (1 << d2);
131 feed_trackers(struct pointer_accelerator *accel,
132 double dx, double dy,
136 struct pointer_tracker *trackers = accel->trackers;
138 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
139 trackers[i].dx += dx;
140 trackers[i].dy += dy;
143 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
144 accel->cur_tracker = current;
146 trackers[current].dx = 0.0;
147 trackers[current].dy = 0.0;
148 trackers[current].time = time;
149 trackers[current].dir = get_direction(dx, dy);
152 static struct pointer_tracker *
153 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
156 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
157 % NUM_POINTER_TRACKERS;
158 return &accel->trackers[index];
162 calculate_tracker_velocity(struct pointer_tracker *tracker, uint32_t time)
170 distance = sqrt(dx*dx + dy*dy);
171 return distance / (double)(time - tracker->time);
175 calculate_velocity(struct pointer_accelerator *accel, uint32_t time)
177 struct pointer_tracker *tracker;
180 double initial_velocity;
181 double velocity_diff;
184 unsigned int dir = tracker_by_offset(accel, 0)->dir;
186 /* Find first velocity */
187 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
188 tracker = tracker_by_offset(accel, offset);
190 if (time <= tracker->time)
193 result = initial_velocity =
194 calculate_tracker_velocity(tracker, time);
195 if (initial_velocity > 0.0)
199 /* Find least recent vector within a timelimit, maximum velocity diff
200 * and direction threshold. */
201 for (; offset < NUM_POINTER_TRACKERS; offset++) {
202 tracker = tracker_by_offset(accel, offset);
204 /* Stop if too far away in time */
205 if (time - tracker->time > MOTION_TIMEOUT ||
206 tracker->time > time)
209 /* Stop if direction changed */
214 velocity = calculate_tracker_velocity(tracker, time);
216 /* Stop if velocity differs too much from initial */
217 velocity_diff = fabs(initial_velocity - velocity);
218 if (velocity_diff > MAX_VELOCITY_DIFF)
228 acceleration_profile(struct pointer_accelerator *accel,
229 void *data, double velocity, uint32_t time)
231 return accel->profile(&accel->base, data, velocity, time);
235 calculate_acceleration(struct pointer_accelerator *accel,
236 void *data, double velocity, uint32_t time)
240 /* Use Simpson's rule to calculate the avarage acceleration between
241 * the previous motion and the most recent. */
242 factor = acceleration_profile(accel, data, velocity, time);
243 factor += acceleration_profile(accel, data, accel->last_velocity, time);
245 acceleration_profile(accel, data,
246 (accel->last_velocity + velocity) / 2,
249 factor = factor / 6.0;
255 soften_delta(double last_delta, double delta)
257 if (delta < -1.0 || delta > 1.0) {
258 if (delta > last_delta)
260 else if (delta < last_delta)
268 apply_softening(struct pointer_accelerator *accel,
269 struct weston_motion_params *motion)
271 motion->dx = soften_delta(accel->last_dx, motion->dx);
272 motion->dy = soften_delta(accel->last_dy, motion->dy);
276 accelerator_filter(struct weston_motion_filter *filter,
277 struct weston_motion_params *motion,
278 void *data, uint32_t time)
280 struct pointer_accelerator *accel =
281 (struct pointer_accelerator *) filter;
285 feed_trackers(accel, motion->dx, motion->dy, time);
286 velocity = calculate_velocity(accel, time);
287 accel_value = calculate_acceleration(accel, data, velocity, time);
289 motion->dx = accel_value * motion->dx;
290 motion->dy = accel_value * motion->dy;
292 apply_softening(accel, motion);
294 accel->last_dx = motion->dx;
295 accel->last_dy = motion->dy;
297 accel->last_velocity = velocity;
301 accelerator_destroy(struct weston_motion_filter *filter)
303 struct pointer_accelerator *accel =
304 (struct pointer_accelerator *) filter;
306 free(accel->trackers);
310 struct weston_motion_filter_interface accelerator_interface = {
315 struct weston_motion_filter *
316 create_pointer_accelator_filter(accel_profile_func_t profile)
318 struct pointer_accelerator *filter;
320 filter = malloc(sizeof *filter);
324 filter->base.interface = &accelerator_interface;
325 wl_list_init(&filter->base.link);
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;