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 <map> 6 #include <set> 7 8 #include <gtest/gtest.h> // for FRIEND_TEST 9 10 #include "include/filter_interpreter.h" 11 #include "include/finger_metrics.h" 12 #include "include/gestures.h" 13 #include "include/macros.h" 14 #include "include/tracer.h" 15 16 #ifndef GESTURES_PALM_CLASSIFYING_FILTER_INTERPRETER_H_ 17 #define GESTURES_PALM_CLASSIFYING_FILTER_INTERPRETER_H_ 18 19 namespace gestures { 20 21 // This interpreter looks at the location and pressure of contacts and may 22 // optionally classify each as a palm. Sometimes a contact is classified as 23 // a palm in some frames, and then in subsequent frames it is not. 24 // This interpreter is also used to suppress motion from thumbs in the 25 // bottom area of the pad. 26 27 class PalmClassifyingFilterInterpreter : public FilterInterpreter { 28 FRIEND_TEST(PalmClassifyingFilterInterpreterTest, PalmAtEdgeTest); 29 FRIEND_TEST(PalmClassifyingFilterInterpreterTest, PalmReevaluateTest); 30 FRIEND_TEST(PalmClassifyingFilterInterpreterTest, PalmTest); 31 FRIEND_TEST(PalmClassifyingFilterInterpreterTest, StationaryPalmTest); 32 public: 33 // Takes ownership of |next|: 34 PalmClassifyingFilterInterpreter(PropRegistry* prop_reg, Interpreter* next, 35 Tracer* tracer); ~PalmClassifyingFilterInterpreter()36 virtual ~PalmClassifyingFilterInterpreter() {} 37 38 protected: 39 virtual void SyncInterpretImpl(HardwareState* hwstate, stime_t* timeout); 40 41 private: 42 void FillOriginInfo(const HardwareState& hwstate); 43 void FillPrevInfo(const HardwareState& hwstate); 44 void FillMaxPressureWidthInfo(const HardwareState& hwstate); 45 46 // Part of palm detection. Returns true if the finger indicated by 47 // |finger_idx| is near another finger, which must not be a palm, in the 48 // hwstate. 49 bool FingerNearOtherFinger(const HardwareState& hwstate, 50 size_t finger_idx); 51 52 // Returns true iff fs represents a contact that may be a palm. It's a palm 53 // if it's in the side edge (or top edge if filter_top_edge_ is set) with 54 // sufficiently large pressure. The pressure required depends on exactly how 55 // close to the edge the contact is. 56 bool FingerInPalmEnvelope(const FingerState& fs); 57 58 // Returns true iff fs represents a contact that is in the bottom area. 59 bool FingerInBottomArea(const FingerState& fs); 60 61 // Updates *palm_, pointing_ below. 62 void UpdatePalmState(const HardwareState& hwstate); 63 64 // Updates the hwstate based on the local state. 65 void UpdatePalmFlags(HardwareState* hwstate); 66 67 // Updates the distance travelled for each finger along +- x/y direction. 68 void UpdateDistanceInfo(const HardwareState& hwstate); 69 70 // Returns the length of time the contact has been on the pad. Returns -1 71 // on error. 72 stime_t FingerAge(short finger_id, stime_t now) const; 73 74 // Time when a contact arrived. Persists even when fingers change. 75 std::map<short, stime_t> origin_timestamps_; 76 // FingerStates from when a contact first arrived. Persists even when fingers 77 // change. 78 std::map<short, FingerState> origin_fingerstates_; 79 80 // FingerStates from the previous HardwareState. 81 std::map<short, FingerState> prev_fingerstates_; 82 83 // Max reported pressure for present fingers. 84 std::map<short, float> max_pressure_; 85 86 // Max reported width for present fingers. 87 std::map<short, float> max_width_; 88 89 // Accumulated distance travelled by each finger. 90 // _positive[0] --> positive direction along x axis 91 // _positive[1] --> positive direction along y axis 92 // _negative[0] --> negative direction along x axis 93 // _negative[1] --> negative direction along y axis 94 std::map<short, float> distance_positive_[2]; 95 std::map<short, float> distance_negative_[2]; 96 97 // Same fingers state. This state is accumulated as fingers remain the same 98 // and it's reset when fingers change. 99 std::set<short> palm_; // tracking ids of known palms 100 // These contacts are a subset of palms_ which are marked as palms because 101 // they have a large contact size. 102 std::set<short> large_palm_; 103 // These contacts have moved significantly and shouldn't be considered 104 // stationary palms: 105 std::set<short> non_stationary_palm_; 106 107 static const unsigned kPointCloseToFinger = 1; 108 static const unsigned kPointNotInEdge = 2; 109 static const unsigned kPointMoving = 4; 110 // tracking ids of known fingers that are not palms, along with the reason(s) 111 std::map<short, unsigned> pointing_; 112 113 114 // tracking ids that were ever close to other fingers. 115 std::set<short> was_near_other_fingers_; 116 117 // tracking ids that have ever travelled out of the palm envelope or bottom 118 // area. 119 std::set<short> fingers_not_in_edge_; 120 121 // Previously input timestamp 122 stime_t prev_time_; 123 124 // Maximum pressure above which a finger is considered a palm 125 DoubleProperty palm_pressure_; 126 // Maximum width_major above which a finger is considered a palm 127 DoubleProperty palm_width_; 128 // Maximum width_major above which a finger is considered a palm if there are 129 // other contacts 130 DoubleProperty multi_palm_width_; 131 // If a finger was previously classified as palm, but its lifetime max 132 // pressure is less than palm_pressure_ * fat_finger_pressure_ratio_, 133 // lifetime max width is less than palm_width_ * fat_finger_width_ratio_, 134 // and has moved more than fat_finger_min_dist_, we consider it as a fat 135 // finger. 136 DoubleProperty fat_finger_pressure_ratio_; 137 DoubleProperty fat_finger_width_ratio_; 138 DoubleProperty fat_finger_min_dist_; 139 // The smaller of two widths around the edge for palm detection. Any contact 140 // in this edge zone may be a palm, regardless of pressure 141 DoubleProperty palm_edge_min_width_; 142 // The larger of the two widths. Palms between this and the previous are 143 // expected to have pressure linearly increase from 0 to palm_pressure_ 144 // as they approach this border. 145 DoubleProperty palm_edge_width_; 146 // If filter_top_edge_ is set, any contact this close to the top edge may be 147 // a palm. 148 DoubleProperty palm_top_edge_min_width_; 149 // Palms in edge are allowed to point if they move fast enough 150 DoubleProperty palm_edge_point_speed_; 151 // A finger can be added to the palm envelope (and thus not point) after 152 // it touches down and until palm_eval_timeout_ [s] time. 153 DoubleProperty palm_eval_timeout_; 154 // A potential palm (ambiguous contact in the edge of the pad) will be marked 155 // a palm if it travels less than palm_stationary_distance_ mm after it's 156 // been on the pad for palm_stationary_time_ seconds. 157 DoubleProperty palm_stationary_time_; 158 DoubleProperty palm_stationary_distance_; 159 // For a finger in the palm envelope to be qualified as pointing, it has to 160 // move in one direction at least palm_pointing_min_dist_ and no more 161 // than palm_pointing_max_reverse_dist_ in the opposite direction. 162 DoubleProperty palm_pointing_min_dist_; 163 DoubleProperty palm_pointing_max_reverse_dist_; 164 // If a palm splits right on the edge, sometimes the pressure readings aren't 165 // high enough to identify the palm. If two fingers on the edge of the pad 166 // are closer together than this value, then they are likely a split palm. 167 DoubleProperty palm_split_max_distance_; 168 // Determines whether we look for palms at the top edge of the touchpad. 169 BoolProperty filter_top_edge_; 170 171 DISALLOW_COPY_AND_ASSIGN(PalmClassifyingFilterInterpreter); 172 }; 173 174 } // namespace gestures 175 176 #endif // GESTURES_PALM_CLASSIFYING_FILTER_INTERPRETER_H_ 177