• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <math.h>
6 
7 #include <memory>
8 #include <gtest/gtest.h>  // for FRIEND_TEST
9 
10 #include "include/filter_interpreter.h"
11 #include "include/gestures.h"
12 #include "include/prop_registry.h"
13 #include "include/tracer.h"
14 
15 #ifndef GESTURES_ACCEL_FILTER_INTERPRETER_H_
16 #define GESTURES_ACCEL_FILTER_INTERPRETER_H_
17 
18 namespace gestures {
19 
20 // This interpreter provides pointer and scroll acceleration based on
21 // an acceleration curve and the user's sensitivity setting.
22 //
23 // For additional documentation, see ../docs/accel_filter_interpreter.md.
24 
25 class AccelFilterInterpreter : public FilterInterpreter {
26   FRIEND_TEST(AccelFilterInterpreterTest, CurveSegmentInitializerTest);
27   FRIEND_TEST(AccelFilterInterpreterTest, CustomAccelTest);
28   FRIEND_TEST(AccelFilterInterpreterTest, SimpleTest);
29   FRIEND_TEST(AccelFilterInterpreterTest, TimingTest);
30   FRIEND_TEST(AccelFilterInterpreterTest, TinyMoveTest);
31   FRIEND_TEST(AccelFilterInterpreterTest, UnacceleratedMouseTest);
32   FRIEND_TEST(AccelFilterInterpreterTest, UnacceleratedTouchpadTest);
33  public:
34   // Takes ownership of |next|:
35   AccelFilterInterpreter(PropRegistry* prop_reg, Interpreter* next,
36                          Tracer* tracer);
~AccelFilterInterpreter()37   virtual ~AccelFilterInterpreter() {}
38 
39   virtual void ConsumeGesture(const Gesture& gs);
40 
41  private:
42   struct CurveSegment {
CurveSegmentCurveSegment43     CurveSegment() : x_(INFINITY), sqr_(0.0), mul_(1.0), int_(0.0) {}
CurveSegmentCurveSegment44     CurveSegment(float x, float s, float m, float b)
45         : x_(x), sqr_(s), mul_(m), int_(b) {}
CurveSegmentCurveSegment46     CurveSegment(const CurveSegment& that)
47         : x_(that.x_), sqr_(that.sqr_), mul_(that.mul_), int_(that.int_) {}
48     // Be careful adding new members: We currently cast arrays of CurveSegment
49     // to arrays of float (to expose to the properties system)
50     double x_;  // Max X value of segment. User's point will be less than this.
51     double sqr_;  // x^2 multiplier
52     double mul_;  // Slope of line (x multiplier)
53     double int_;  // Intercept of line
54   };
55 
56   static const size_t kMaxCurveSegs = 3;
57   static const size_t kMaxCustomCurveSegs = 20;
58   static const size_t kMaxAccelCurves = 5;
59 
60   // curves for sensitivity 1..5
61   CurveSegment point_curves_[kMaxAccelCurves][kMaxCurveSegs];
62   CurveSegment old_mouse_point_curves_[kMaxAccelCurves][kMaxCurveSegs];
63   CurveSegment mouse_point_curves_[kMaxAccelCurves][kMaxCurveSegs];
64   CurveSegment scroll_curves_[kMaxAccelCurves][kMaxCurveSegs];
65 
66   // curves when acceleration is disabled.
67   CurveSegment unaccel_point_curves_[kMaxAccelCurves];
68   CurveSegment unaccel_mouse_curves_[kMaxAccelCurves];
69   // TODO(zentaro): Add unaccelerated scroll curves.
70 
71   // Custom curves
72   CurveSegment tp_custom_point_[kMaxCustomCurveSegs];
73   CurveSegment tp_custom_scroll_[kMaxCustomCurveSegs];
74   CurveSegment mouse_custom_point_[kMaxCustomCurveSegs];
75   // Note: there is no mouse_custom_scroll_ b/c mouse wheel accel is
76   // handled in the MouseInterpreter class.
77 
78   // See max* and min_reasonable_dt_ properties
79   stime_t last_reasonable_dt_;
80 
81   // These are used to calculate acceleration, see smooth_accel_
82   stime_t last_end_time_;
83   float last_mags_[2];
84   size_t last_mags_size_;
85 
86   // These properties expose the custom curves (just above) to the
87   // property system.
88   DoubleArrayProperty tp_custom_point_prop_;
89   DoubleArrayProperty tp_custom_scroll_prop_;
90   DoubleArrayProperty mouse_custom_point_prop_;
91 
92   // These properties enable use of custom curves (just above).
93   BoolProperty use_custom_tp_point_curve_;
94   BoolProperty use_custom_tp_scroll_curve_;
95   BoolProperty use_custom_mouse_curve_;
96 
97   IntProperty pointer_sensitivity_;  // [1..5]
98   IntProperty scroll_sensitivity_;  // [1..5]
99 
100   DoubleProperty point_x_out_scale_;
101   DoubleProperty point_y_out_scale_;
102   DoubleProperty scroll_x_out_scale_;
103   DoubleProperty scroll_y_out_scale_;
104   // These properties are automatically set on mice-like devices:
105   BoolProperty use_mouse_point_curves_;  // set on {touch,nontouch} mice
106   BoolProperty use_mouse_scroll_curves_;  // set on nontouch mice
107   // If use_mouse_point_curves_ is true, this is consulted to see which
108   // curves to use:
109   BoolProperty use_old_mouse_point_curves_;
110   // Flag for disabling mouse/touchpad acceleration.
111   BoolProperty pointer_acceleration_;
112 
113   // Sometimes on wireless hardware (e.g. Bluetooth), patckets need to be
114   // resent. This can lead to a time between packets that very large followed
115   // by a very small one. Very small periods especially cause problems b/c they
116   // make the velocity seem very fast, which leads to an exaggeration of
117   // movement.
118   // To compensate, we have bounds on what we expect a reasonable period to be.
119   // Events that have too large or small a period get reassigned the last
120   // reasonable period.
121   DoubleProperty min_reasonable_dt_;
122   DoubleProperty max_reasonable_dt_;
123 
124   // If we enable smooth accel, the past few magnitudes are used to compute the
125   // multiplication factor.
126   BoolProperty smooth_accel_;
127 };
128 
129 }  // namespace gestures
130 
131 #endif  // GESTURES_SCALING_FILTER_INTERPRETER_H_
132