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 #ifndef GESTURES_ACTIVITY_LOG_H_ 6 #define GESTURES_ACTIVITY_LOG_H_ 7 8 #include "include/gestures.h" 9 10 #include <string> 11 12 #include <gtest/gtest.h> // For FRIEND_TEST 13 #include <json/value.h> 14 15 // This is a class that circularly buffers all incoming and outgoing activity 16 // so that end users can report issues and engineers can reproduce them. 17 18 namespace gestures { 19 20 class PropRegistry; 21 22 class ActivityLog { 23 FRIEND_TEST(ActivityLogTest, SimpleTest); 24 FRIEND_TEST(ActivityLogTest, WrapAroundTest); 25 FRIEND_TEST(ActivityLogTest, VersionTest); 26 FRIEND_TEST(LoggingFilterInterpreterTest, SimpleTest); 27 FRIEND_TEST(PropRegistryTest, PropChangeTest); 28 public: 29 enum EntryType { 30 kHardwareState = 0, 31 kTimerCallback, 32 kCallbackRequest, 33 kGesture, 34 kPropChange 35 }; 36 struct PropChangeEntry { 37 const char* name; 38 enum { 39 kBoolProp = 0, 40 kDoubleProp, 41 kIntProp, 42 kShortProp 43 } type; 44 union { 45 GesturesPropBool bool_val; 46 double double_val; 47 int int_val; 48 short short_val; 49 // No string because string values can't change 50 } value; 51 }; 52 struct Entry { 53 EntryType type; 54 struct details { 55 HardwareState hwstate; // kHardwareState 56 stime_t timestamp; // kTimerCallback, kCallbackRequest 57 Gesture gesture; // kGesture 58 PropChangeEntry prop_change; // kPropChange 59 } details; 60 }; 61 62 explicit ActivityLog(PropRegistry* prop_reg); 63 void SetHardwareProperties(const HardwareProperties& hwprops); 64 65 // Log*() functions record an argument into the buffer 66 void LogHardwareState(const HardwareState& hwstate); 67 void LogTimerCallback(stime_t now); 68 void LogCallbackRequest(stime_t when); 69 void LogGesture(const Gesture& gesture); 70 void LogPropChange(const PropChangeEntry& prop_change); 71 72 // Dump allocates, and thus must not be called on a signal handler. 73 void Dump(const char* filename); Clear()74 void Clear() { head_idx_ = size_ = 0; } 75 76 // Returns a JSON string representing all the state in the buffer 77 std::string Encode(); 78 void AddEncodeInfo(Json::Value* root); 79 Json::Value EncodeCommonInfo(); size()80 size_t size() const { return size_; } MaxSize()81 size_t MaxSize() const { return kBufferSize; } GetEntry(size_t idx)82 Entry* GetEntry(size_t idx) { 83 return &buffer_[(head_idx_ + idx) % kBufferSize]; 84 } 85 86 static const char kKeyInterpreterName[]; 87 static const char kKeyNext[]; 88 static const char kKeyRoot[]; 89 static const char kKeyType[]; 90 static const char kKeyHardwareState[]; 91 static const char kKeyTimerCallback[]; 92 static const char kKeyCallbackRequest[]; 93 static const char kKeyGesture[]; 94 static const char kKeyPropChange[]; 95 // HardwareState keys: 96 static const char kKeyHardwareStateTimestamp[]; 97 static const char kKeyHardwareStateButtonsDown[]; 98 static const char kKeyHardwareStateTouchCnt[]; 99 static const char kKeyHardwareStateFingers[]; 100 static const char kKeyHardwareStateRelX[]; 101 static const char kKeyHardwareStateRelY[]; 102 static const char kKeyHardwareStateRelWheel[]; 103 static const char kKeyHardwareStateRelHWheel[]; 104 // FingerState keys (part of HardwareState): 105 static const char kKeyFingerStateTouchMajor[]; 106 static const char kKeyFingerStateTouchMinor[]; 107 static const char kKeyFingerStateWidthMajor[]; 108 static const char kKeyFingerStateWidthMinor[]; 109 static const char kKeyFingerStatePressure[]; 110 static const char kKeyFingerStateOrientation[]; 111 static const char kKeyFingerStatePositionX[]; 112 static const char kKeyFingerStatePositionY[]; 113 static const char kKeyFingerStateTrackingId[]; 114 static const char kKeyFingerStateFlags[]; 115 // TimerCallback keys: 116 static const char kKeyTimerCallbackNow[]; 117 // CallbackRequest keys: 118 static const char kKeyCallbackRequestWhen[]; 119 // Gesture keys: 120 static const char kKeyGestureType[]; 121 static const char kValueGestureTypeContactInitiated[]; 122 static const char kValueGestureTypeMove[]; 123 static const char kValueGestureTypeScroll[]; 124 static const char kValueGestureTypeMouseWheel[]; 125 static const char kValueGestureTypePinch[]; 126 static const char kValueGestureTypeButtonsChange[]; 127 static const char kValueGestureTypeFling[]; 128 static const char kValueGestureTypeSwipe[]; 129 static const char kValueGestureTypeSwipeLift[]; 130 static const char kValueGestureTypeFourFingerSwipe[]; 131 static const char kValueGestureTypeFourFingerSwipeLift[]; 132 static const char kValueGestureTypeMetrics[]; 133 static const char kKeyGestureStartTime[]; 134 static const char kKeyGestureEndTime[]; 135 static const char kKeyGestureMoveDX[]; 136 static const char kKeyGestureMoveDY[]; 137 static const char kKeyGestureMoveOrdinalDX[]; 138 static const char kKeyGestureMoveOrdinalDY[]; 139 static const char kKeyGestureScrollDX[]; 140 static const char kKeyGestureScrollDY[]; 141 static const char kKeyGestureScrollOrdinalDX[]; 142 static const char kKeyGestureScrollOrdinalDY[]; 143 static const char kKeyGestureMouseWheelDX[]; 144 static const char kKeyGestureMouseWheelDY[]; 145 static const char kKeyGestureMouseWheelTicksDX[]; 146 static const char kKeyGestureMouseWheelTicksDY[]; 147 static const char kKeyGesturePinchDZ[]; 148 static const char kKeyGesturePinchOrdinalDZ[]; 149 static const char kKeyGesturePinchZoomState[]; 150 static const char kKeyGestureButtonsChangeDown[]; 151 static const char kKeyGestureButtonsChangeUp[]; 152 static const char kKeyGestureFlingVX[]; 153 static const char kKeyGestureFlingVY[]; 154 static const char kKeyGestureFlingOrdinalVX[]; 155 static const char kKeyGestureFlingOrdinalVY[]; 156 static const char kKeyGestureFlingState[]; 157 static const char kKeyGestureSwipeDX[]; 158 static const char kKeyGestureSwipeDY[]; 159 static const char kKeyGestureSwipeOrdinalDX[]; 160 static const char kKeyGestureSwipeOrdinalDY[]; 161 static const char kKeyGestureFourFingerSwipeDX[]; 162 static const char kKeyGestureFourFingerSwipeDY[]; 163 static const char kKeyGestureFourFingerSwipeOrdinalDX[]; 164 static const char kKeyGestureFourFingerSwipeOrdinalDY[]; 165 static const char kKeyGestureMetricsType[]; 166 static const char kKeyGestureMetricsData1[]; 167 static const char kKeyGestureMetricsData2[]; 168 // PropChange keys: 169 static const char kKeyPropChangeType[]; 170 static const char kKeyPropChangeName[]; 171 static const char kKeyPropChangeValue[]; 172 static const char kValuePropChangeTypeBool[]; 173 static const char kValuePropChangeTypeDouble[]; 174 static const char kValuePropChangeTypeInt[]; 175 static const char kValuePropChangeTypeShort[]; 176 177 // Hardware Properties keys: 178 static const char kKeyHardwarePropRoot[]; 179 static const char kKeyHardwarePropLeft[]; 180 static const char kKeyHardwarePropTop[]; 181 static const char kKeyHardwarePropRight[]; 182 static const char kKeyHardwarePropBottom[]; 183 static const char kKeyHardwarePropXResolution[]; 184 static const char kKeyHardwarePropYResolution[]; 185 static const char kKeyHardwarePropXDpi[]; 186 static const char kKeyHardwarePropYDpi[]; 187 static const char kKeyHardwarePropOrientationMinimum[]; 188 static const char kKeyHardwarePropOrientationMaximum[]; 189 static const char kKeyHardwarePropMaxFingerCount[]; 190 static const char kKeyHardwarePropMaxTouchCount[]; 191 static const char kKeyHardwarePropSupportsT5R2[]; 192 static const char kKeyHardwarePropSemiMt[]; 193 static const char kKeyHardwarePropIsButtonPad[]; 194 static const char kKeyHardwarePropHasWheel[]; 195 196 static const char kKeyProperties[]; 197 198 private: 199 // Extends the tail of the buffer by one element and returns that new element. 200 // This may cause an older element to be overwritten if the buffer is full. 201 Entry* PushBack(); 202 TailIdx()203 size_t TailIdx() const { return (head_idx_ + size_ - 1) % kBufferSize; } 204 205 // JSON-encoders for various types 206 Json::Value EncodeHardwareProperties() const; 207 Json::Value EncodeHardwareState(const HardwareState& hwstate); 208 Json::Value EncodeTimerCallback(stime_t timestamp); 209 Json::Value EncodeCallbackRequest(stime_t timestamp); 210 Json::Value EncodeGesture(const Gesture& gesture); 211 Json::Value EncodePropChange(const PropChangeEntry& prop_change); 212 213 // Encode user-configurable properties 214 Json::Value EncodePropRegistry(); 215 216 #ifdef GESTURES_LARGE_LOGGING_BUFFER 217 static const size_t kBufferSize = 65536; 218 #else 219 static const size_t kBufferSize = 8192; 220 #endif 221 222 Entry buffer_[kBufferSize]; 223 size_t head_idx_; 224 size_t size_; 225 226 // We allocate this to be number of entries * max fingers/entry, and 227 // if buffer_[i] is a kHardwareState type, then the fingers for it are 228 // at finger_states_[i * (max fingers/entry)]. 229 std::unique_ptr<FingerState[]> finger_states_; 230 size_t max_fingers_; 231 232 HardwareProperties hwprops_; 233 PropRegistry* prop_reg_; 234 }; 235 236 } // namespace gestures 237 238 #endif // GESTURES_ACTIVITY_LOG_H_ 239