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 "include/activity_log.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <set>
10 #include <string>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 #include <json/value.h>
15 #include <json/writer.h>
16
17 #include "include/file_util.h"
18 #include "include/logging.h"
19 #include "include/prop_registry.h"
20 #include "include/string_util.h"
21
22 #define QUINTTAP_COUNT 5 /* BTN_TOOL_QUINTTAP - Five fingers on trackpad */
23
24 using std::set;
25 using std::string;
26
27 namespace {
28
29 // Helper to std::visit with lambdas.
30 template <typename... V>
31 struct Visitor : V... {
32 using V::operator()...;
33 };
34 // Explicit deduction guide (not needed as of C++20).
35 template <typename... V>
36 Visitor(V...) -> Visitor<V...>;
37
38 } // namespace
39
40 namespace gestures {
41
ActivityLog(PropRegistry * prop_reg)42 ActivityLog::ActivityLog(PropRegistry* prop_reg)
43 : head_idx_(0), size_(0), max_fingers_(0), hwprops_(),
44 prop_reg_(prop_reg) {}
45
SetHardwareProperties(const HardwareProperties & hwprops)46 void ActivityLog::SetHardwareProperties(const HardwareProperties& hwprops) {
47 hwprops_ = hwprops;
48
49 // For old devices(such as mario, alex, zgb..), the reporting touch count
50 // or 'max_touch_cnt' will be less than number of slots or 'max_finger_cnt'
51 // they support. As kernel evdev drivers do not have a bitset to report
52 // touch count greater than five (bitset for five-fingers gesture is
53 // BTN_TOOL_QUINTAP), we respect 'max_finger_cnt' than 'max_touch_cnt'
54 // reported from kernel driver as the 'max_fingers_' instead.
55 if (hwprops.max_touch_cnt < QUINTTAP_COUNT) {
56 max_fingers_ = std::min<size_t>(hwprops.max_finger_cnt,
57 hwprops.max_touch_cnt);
58 } else {
59 max_fingers_ = std::max<size_t>(hwprops.max_finger_cnt,
60 hwprops.max_touch_cnt);
61 }
62
63 finger_states_.reset(new FingerState[kBufferSize * max_fingers_]);
64 }
65
LogHardwareState(const HardwareState & hwstate)66 void ActivityLog::LogHardwareState(const HardwareState& hwstate) {
67 Entry* entry = PushBack();
68 entry->details = hwstate;
69 HardwareState& entry_hwstate = std::get<HardwareState>(entry->details);
70 if (hwstate.finger_cnt > max_fingers_) {
71 Err("Too many fingers! Max is %zu, but I got %d",
72 max_fingers_, hwstate.finger_cnt);
73 entry_hwstate.fingers = nullptr;
74 entry_hwstate.finger_cnt = 0;
75 return;
76 }
77 if (!finger_states_.get())
78 return;
79 entry_hwstate.fingers = &finger_states_[TailIdx() * max_fingers_];
80 std::copy(&hwstate.fingers[0], &hwstate.fingers[hwstate.finger_cnt],
81 entry_hwstate.fingers);
82 }
83
LogTimerCallback(stime_t now)84 void ActivityLog::LogTimerCallback(stime_t now) {
85 Entry* entry = PushBack();
86 entry->details = TimerCallbackEntry{now};
87 }
88
LogCallbackRequest(stime_t when)89 void ActivityLog::LogCallbackRequest(stime_t when) {
90 Entry* entry = PushBack();
91 entry->details = CallbackRequestEntry{when};
92 }
93
LogGesture(const Gesture & gesture)94 void ActivityLog::LogGesture(const Gesture& gesture) {
95 Entry* entry = PushBack();
96 entry->details = gesture;
97 }
98
LogPropChange(const PropChangeEntry & prop_change)99 void ActivityLog::LogPropChange(const PropChangeEntry& prop_change) {
100 Entry* entry = PushBack();
101 entry->details = prop_change;
102 }
103
LogGestureConsume(const std::string & name,const Gesture & gesture)104 void ActivityLog::LogGestureConsume(
105 const std::string& name, const Gesture& gesture) {
106 GestureConsume gesture_consume { name, gesture };
107 Entry* entry = PushBack();
108 entry->details = gesture_consume;
109 }
110
LogGestureProduce(const std::string & name,const Gesture & gesture)111 void ActivityLog::LogGestureProduce(
112 const std::string& name, const Gesture& gesture) {
113 GestureProduce gesture_produce { name, gesture };
114 Entry* entry = PushBack();
115 entry->details = gesture_produce;
116 }
117
LogHardwareStatePre(const std::string & name,const HardwareState & hwstate)118 void ActivityLog::LogHardwareStatePre(const std::string& name,
119 const HardwareState& hwstate) {
120 HardwareStatePre hwstate_pre { name, hwstate };
121 Entry* entry = PushBack();
122 entry->details = hwstate_pre;
123 }
124
LogHardwareStatePost(const std::string & name,const HardwareState & hwstate)125 void ActivityLog::LogHardwareStatePost(const std::string& name,
126 const HardwareState& hwstate) {
127 HardwareStatePost hwstate_post { name, hwstate };
128 Entry* entry = PushBack();
129 entry->details = hwstate_post;
130 }
131
LogHandleTimerPre(const std::string & name,stime_t now,const stime_t * timeout)132 void ActivityLog::LogHandleTimerPre(const std::string& name,
133 stime_t now, const stime_t* timeout) {
134 HandleTimerPre handle;
135 handle.name = name;
136 handle.now = now;
137 handle.timeout_is_present = (timeout != nullptr);
138 handle.timeout = (timeout == nullptr) ? 0 : *timeout;
139 Entry* entry = PushBack();
140 entry->details = handle;
141 }
142
LogHandleTimerPost(const std::string & name,stime_t now,const stime_t * timeout)143 void ActivityLog::LogHandleTimerPost(const std::string& name,
144 stime_t now, const stime_t* timeout) {
145 HandleTimerPost handle;
146 handle.name = name;
147 handle.now = now;
148 handle.timeout_is_present = (timeout != nullptr);
149 handle.timeout = (timeout == nullptr) ? 0 : *timeout;
150 Entry* entry = PushBack();
151 entry->details = handle;
152 }
153
Dump(const char * filename)154 void ActivityLog::Dump(const char* filename) {
155 string data = Encode();
156 WriteFile(filename, data.c_str(), data.size());
157 }
158
PushBack()159 ActivityLog::Entry* ActivityLog::PushBack() {
160 if (size_ == kBufferSize) {
161 Entry* ret = &buffer_[head_idx_];
162 head_idx_ = (head_idx_ + 1) % kBufferSize;
163 return ret;
164 }
165 ++size_;
166 return &buffer_[TailIdx()];
167 }
168
EncodeHardwareProperties() const169 Json::Value ActivityLog::EncodeHardwareProperties() const {
170 Json::Value ret(Json::objectValue);
171 ret[kKeyHardwarePropLeft] = Json::Value(hwprops_.left);
172 ret[kKeyHardwarePropTop] = Json::Value(hwprops_.top);
173 ret[kKeyHardwarePropRight] = Json::Value(hwprops_.right);
174 ret[kKeyHardwarePropBottom] = Json::Value(hwprops_.bottom);
175 ret[kKeyHardwarePropXResolution] = Json::Value(hwprops_.res_x);
176 ret[kKeyHardwarePropYResolution] = Json::Value(hwprops_.res_y);
177 ret[kKeyHardwarePropOrientationMinimum] =
178 Json::Value(hwprops_.orientation_minimum);
179 ret[kKeyHardwarePropOrientationMaximum] =
180 Json::Value(hwprops_.orientation_maximum);
181 ret[kKeyHardwarePropMaxFingerCount] = Json::Value(hwprops_.max_finger_cnt);
182 ret[kKeyHardwarePropMaxTouchCount] = Json::Value(hwprops_.max_touch_cnt);
183
184 ret[kKeyHardwarePropSupportsT5R2] = Json::Value(hwprops_.supports_t5r2 != 0);
185 ret[kKeyHardwarePropSemiMt] = Json::Value(hwprops_.support_semi_mt != 0);
186 ret[kKeyHardwarePropIsButtonPad] = Json::Value(hwprops_.is_button_pad != 0);
187 ret[kKeyHardwarePropHasWheel] = Json::Value(hwprops_.has_wheel != 0);
188 return ret;
189 }
190
EncodeHardwareStateCommon(const HardwareState & hwstate)191 Json::Value ActivityLog::EncodeHardwareStateCommon(
192 const HardwareState& hwstate) {
193 Json::Value ret(Json::objectValue);
194 ret[kKeyHardwareStateButtonsDown] = Json::Value(hwstate.buttons_down);
195 ret[kKeyHardwareStateTouchCnt] = Json::Value(hwstate.touch_cnt);
196 ret[kKeyHardwareStateTimestamp] = Json::Value(hwstate.timestamp);
197 Json::Value fingers(Json::arrayValue);
198 for (size_t i = 0; i < hwstate.finger_cnt; ++i) {
199 if (hwstate.fingers == nullptr) {
200 Err("Have finger_cnt %d but fingers is null!", hwstate.finger_cnt);
201 break;
202 }
203 const FingerState& fs = hwstate.fingers[i];
204 Json::Value finger(Json::objectValue);
205 finger[kKeyFingerStateTouchMajor] = Json::Value(fs.touch_major);
206 finger[kKeyFingerStateTouchMinor] = Json::Value(fs.touch_minor);
207 finger[kKeyFingerStateWidthMajor] = Json::Value(fs.width_major);
208 finger[kKeyFingerStateWidthMinor] = Json::Value(fs.width_minor);
209 finger[kKeyFingerStatePressure] = Json::Value(fs.pressure);
210 finger[kKeyFingerStateOrientation] = Json::Value(fs.orientation);
211 finger[kKeyFingerStatePositionX] = Json::Value(fs.position_x);
212 finger[kKeyFingerStatePositionY] = Json::Value(fs.position_y);
213 finger[kKeyFingerStateTrackingId] = Json::Value(fs.tracking_id);
214 finger[kKeyFingerStateFlags] = Json::Value(static_cast<int>(fs.flags));
215 fingers.append(finger);
216 }
217 ret[kKeyHardwareStateFingers] = fingers;
218 ret[kKeyHardwareStateRelX] = Json::Value(hwstate.rel_x);
219 ret[kKeyHardwareStateRelY] = Json::Value(hwstate.rel_y);
220 ret[kKeyHardwareStateRelWheel] = Json::Value(hwstate.rel_wheel);
221 ret[kKeyHardwareStateRelHWheel] = Json::Value(hwstate.rel_hwheel);
222 return ret;
223 }
224
EncodeHardwareState(const HardwareState & hwstate)225 Json::Value ActivityLog::EncodeHardwareState(const HardwareState& hwstate) {
226 auto ret = EncodeHardwareStateCommon(hwstate);
227 ret[kKeyType] = Json::Value(kKeyHardwareState);
228 return ret;
229 }
230
EncodeHardwareState(const HardwareStatePre & pre_hwstate)231 Json::Value ActivityLog::EncodeHardwareState(
232 const HardwareStatePre& pre_hwstate) {
233 auto ret = EncodeHardwareStateCommon(pre_hwstate.hwstate);
234 ret[kKeyType] = Json::Value(kKeyHardwareStatePre);
235 ret[kKeyMethodName] = Json::Value(pre_hwstate.name);
236 return ret;
237 }
238
EncodeHardwareState(const HardwareStatePost & post_hwstate)239 Json::Value ActivityLog::EncodeHardwareState(
240 const HardwareStatePost& post_hwstate) {
241 auto ret = EncodeHardwareStateCommon(post_hwstate.hwstate);
242 ret[kKeyType] = Json::Value(kKeyHardwareStatePost);
243 ret[kKeyMethodName] = Json::Value(post_hwstate.name);
244 return ret;
245 }
246
EncodeHandleTimer(const HandleTimerPre & handle)247 Json::Value ActivityLog::EncodeHandleTimer(const HandleTimerPre& handle) {
248 Json::Value ret(Json::objectValue);
249 ret[kKeyType] = Json::Value(kKeyHandleTimerPre);
250 ret[kKeyMethodName] = Json::Value(handle.name);
251 ret[kKeyTimerNow] = Json::Value(handle.now);
252 if (handle.timeout_is_present)
253 ret[kKeyHandleTimerTimeout] = Json::Value(handle.timeout);
254 return ret;
255 }
256
EncodeHandleTimer(const HandleTimerPost & handle)257 Json::Value ActivityLog::EncodeHandleTimer(const HandleTimerPost& handle) {
258 Json::Value ret(Json::objectValue);
259 ret[kKeyType] = Json::Value(kKeyHandleTimerPost);
260 ret[kKeyMethodName] = Json::Value(handle.name);
261 ret[kKeyTimerNow] = Json::Value(handle.now);
262 if (handle.timeout_is_present)
263 ret[kKeyHandleTimerTimeout] = Json::Value(handle.timeout);
264 return ret;
265 }
266
EncodeTimerCallback(stime_t timestamp)267 Json::Value ActivityLog::EncodeTimerCallback(stime_t timestamp) {
268 Json::Value ret(Json::objectValue);
269 ret[kKeyType] = Json::Value(kKeyTimerCallback);
270 ret[kKeyTimerNow] = Json::Value(timestamp);
271 return ret;
272 }
273
EncodeCallbackRequest(stime_t timestamp)274 Json::Value ActivityLog::EncodeCallbackRequest(stime_t timestamp) {
275 Json::Value ret(Json::objectValue);
276 ret[kKeyType] = Json::Value(kKeyCallbackRequest);
277 ret[kKeyCallbackRequestWhen] = Json::Value(timestamp);
278 return ret;
279 }
280
EncodeGestureCommon(const Gesture & gesture)281 Json::Value ActivityLog::EncodeGestureCommon(const Gesture& gesture) {
282 Json::Value ret(Json::objectValue);
283 ret[kKeyGestureStartTime] = Json::Value(gesture.start_time);
284 ret[kKeyGestureEndTime] = Json::Value(gesture.end_time);
285
286 switch (gesture.type) {
287 case kGestureTypeNull:
288 ret[kKeyGestureType] = Json::Value("null");
289 break;
290 case kGestureTypeContactInitiated:
291 ret[kKeyGestureType] = Json::Value(kValueGestureTypeContactInitiated);
292 break;
293 case kGestureTypeMove:
294 ret[kKeyGestureType] = Json::Value(kValueGestureTypeMove);
295 ret[kKeyGestureDX] = Json::Value(gesture.details.move.dx);
296 ret[kKeyGestureDY] = Json::Value(gesture.details.move.dy);
297 ret[kKeyGestureOrdinalDX] = Json::Value(gesture.details.move.ordinal_dx);
298 ret[kKeyGestureOrdinalDY] = Json::Value(gesture.details.move.ordinal_dy);
299 break;
300 case kGestureTypeScroll:
301 ret[kKeyGestureType] = Json::Value(kValueGestureTypeScroll);
302 ret[kKeyGestureDX] = Json::Value(gesture.details.scroll.dx);
303 ret[kKeyGestureDY] = Json::Value(gesture.details.scroll.dy);
304 ret[kKeyGestureOrdinalDX] =
305 Json::Value(gesture.details.scroll.ordinal_dx);
306 ret[kKeyGestureOrdinalDY] =
307 Json::Value(gesture.details.scroll.ordinal_dy);
308 break;
309 case kGestureTypeMouseWheel:
310 ret[kKeyGestureType] = Json::Value(kValueGestureTypeMouseWheel);
311 ret[kKeyGestureDX] = Json::Value(gesture.details.wheel.dx);
312 ret[kKeyGestureDY] = Json::Value(gesture.details.wheel.dy);
313 ret[kKeyGestureMouseWheelTicksDX] =
314 Json::Value(gesture.details.wheel.tick_120ths_dx);
315 ret[kKeyGestureMouseWheelTicksDY] =
316 Json::Value(gesture.details.wheel.tick_120ths_dy);
317 break;
318 case kGestureTypePinch:
319 ret[kKeyGestureType] = Json::Value(kValueGestureTypePinch);
320 ret[kKeyGesturePinchDZ] = Json::Value(gesture.details.pinch.dz);
321 ret[kKeyGesturePinchOrdinalDZ] =
322 Json::Value(gesture.details.pinch.ordinal_dz);
323 ret[kKeyGesturePinchZoomState] =
324 Json::Value(gesture.details.pinch.zoom_state);
325 break;
326 case kGestureTypeButtonsChange:
327 ret[kKeyGestureType] = Json::Value(kValueGestureTypeButtonsChange);
328 ret[kKeyGestureButtonsChangeDown] =
329 Json::Value(static_cast<int>(gesture.details.buttons.down));
330 ret[kKeyGestureButtonsChangeUp] =
331 Json::Value(static_cast<int>(gesture.details.buttons.up));
332 break;
333 case kGestureTypeFling:
334 ret[kKeyGestureType] = Json::Value(kValueGestureTypeFling);
335 ret[kKeyGestureFlingVX] = Json::Value(gesture.details.fling.vx);
336 ret[kKeyGestureFlingVY] = Json::Value(gesture.details.fling.vy);
337 ret[kKeyGestureFlingOrdinalVX] =
338 Json::Value(gesture.details.fling.ordinal_vx);
339 ret[kKeyGestureFlingOrdinalVY] =
340 Json::Value(gesture.details.fling.ordinal_vy);
341 ret[kKeyGestureFlingState] =
342 Json::Value(static_cast<int>(gesture.details.fling.fling_state));
343 break;
344 case kGestureTypeSwipe:
345 ret[kKeyGestureType] = Json::Value(kValueGestureTypeSwipe);
346 ret[kKeyGestureDX] = Json::Value(gesture.details.swipe.dx);
347 ret[kKeyGestureDY] = Json::Value(gesture.details.swipe.dy);
348 ret[kKeyGestureOrdinalDX] =
349 Json::Value(gesture.details.swipe.ordinal_dx);
350 ret[kKeyGestureOrdinalDY] =
351 Json::Value(gesture.details.swipe.ordinal_dy);
352 break;
353 case kGestureTypeSwipeLift:
354 ret[kKeyGestureType] = Json::Value(kValueGestureTypeSwipeLift);
355 break;
356 case kGestureTypeFourFingerSwipe:
357 ret[kKeyGestureType] = Json::Value(kValueGestureTypeFourFingerSwipe);
358 ret[kKeyGestureDX] =
359 Json::Value(gesture.details.four_finger_swipe.dx);
360 ret[kKeyGestureDY] =
361 Json::Value(gesture.details.four_finger_swipe.dy);
362 ret[kKeyGestureOrdinalDX] =
363 Json::Value(gesture.details.four_finger_swipe.ordinal_dx);
364 ret[kKeyGestureOrdinalDY] =
365 Json::Value(gesture.details.four_finger_swipe.ordinal_dy);
366 break;
367 case kGestureTypeFourFingerSwipeLift:
368 ret[kKeyGestureType] = Json::Value(kValueGestureTypeFourFingerSwipeLift);
369 break;
370 case kGestureTypeMetrics:
371 ret[kKeyGestureType] = Json::Value(kValueGestureTypeMetrics);
372 ret[kKeyGestureMetricsType] =
373 Json::Value(static_cast<int>(gesture.details.metrics.type));
374 ret[kKeyGestureMetricsData1] =
375 Json::Value(gesture.details.metrics.data[0]);
376 ret[kKeyGestureMetricsData2] =
377 Json::Value(gesture.details.metrics.data[1]);
378 break;
379 default:
380 ret[kKeyGestureType] =
381 Json::Value(StringPrintf("Unhandled %d", gesture.type));
382 }
383 return ret;
384 }
385
EncodeGesture(const Gesture & gesture)386 Json::Value ActivityLog::EncodeGesture(const Gesture& gesture) {
387 auto ret = EncodeGestureCommon(gesture);
388 ret[kKeyType] = Json::Value(kKeyGesture);
389 return ret;
390 }
391
EncodeGesture(const GestureConsume & gesture_consume)392 Json::Value ActivityLog::EncodeGesture(const GestureConsume& gesture_consume) {
393 auto ret = EncodeGestureCommon(gesture_consume.gesture);
394 ret[kKeyType] = Json::Value(kKeyGestureConsume);
395 ret[kKeyMethodName] = Json::Value(gesture_consume.name);
396 return ret;
397 }
398
EncodeGesture(const GestureProduce & gesture_produce)399 Json::Value ActivityLog::EncodeGesture(const GestureProduce& gesture_produce) {
400 auto ret = EncodeGestureCommon(gesture_produce.gesture);
401 ret[kKeyType] = Json::Value(kKeyGestureProduce);
402 ret[kKeyMethodName] = Json::Value(gesture_produce.name);
403 return ret;
404 }
405
EncodePropChange(const PropChangeEntry & prop_change)406 Json::Value ActivityLog::EncodePropChange(const PropChangeEntry& prop_change) {
407 Json::Value ret(Json::objectValue);
408 ret[kKeyType] = Json::Value(kKeyPropChange);
409 ret[kKeyPropChangeName] = Json::Value(prop_change.name);
410 Json::Value val;
411 Json::Value type;
412 std::visit(
413 Visitor {
414 [&val, &type](GesturesPropBool value) {
415 val = Json::Value(value);
416 type = Json::Value(kValuePropChangeTypeBool);
417 },
418 [&val, &type](double value) {
419 val = Json::Value(value);
420 type = Json::Value(kValuePropChangeTypeDouble);
421 },
422 [&val, &type](int value) {
423 val = Json::Value(value);
424 type = Json::Value(kValuePropChangeTypeInt);
425 },
426 [&val, &type](short value) {
427 val = Json::Value(value);
428 type = Json::Value(kValuePropChangeTypeShort);
429 },
430 [](auto arg) {
431 Err("Invalid value type");
432 }
433 }, prop_change.value);
434 if (!val.isNull())
435 ret[kKeyPropChangeValue] = val;
436 if (!type.isNull())
437 ret[kKeyPropChangeType] = type;
438 return ret;
439 }
440
EncodeGestureDebug(const AccelGestureDebug & debug_data)441 Json::Value ActivityLog::EncodeGestureDebug(
442 const AccelGestureDebug& debug_data) {
443 Json::Value ret(Json::objectValue);
444 ret[kKeyType] = Json::Value(kKeyAccelGestureDebug);
445 ret[kKeyAccelDebugDroppedGesture] = Json::Value(debug_data.dropped_gesture);
446 if (debug_data.no_accel_for_gesture_type)
447 ret[kKeyAccelDebugNoAccelGestureType] = Json::Value(true);
448 else if (debug_data.no_accel_for_small_dt)
449 ret[kKeyAccelDebugNoAccelSmallDt] = Json::Value(true);
450 else if (debug_data.no_accel_for_small_speed)
451 ret[kKeyAccelDebugNoAccelSmallSpeed] = Json::Value(true);
452 else if (debug_data.no_accel_for_bad_gain)
453 ret[kKeyAccelDebugNoAccelBadGain] = Json::Value(true);
454 ret[kKeyAccelDebugXYAreVelocity] = Json::Value(debug_data.x_y_are_velocity);
455 ret[kKeyAccelDebugXScale] = Json::Value(debug_data.x_scale);
456 ret[kKeyAccelDebugYScale] = Json::Value(debug_data.y_scale);
457 ret[kKeyAccelDebugDt] = Json::Value(debug_data.dt);
458 ret[kKeyAccelDebugAdjustedDt] = Json::Value(debug_data.adjusted_dt);
459 ret[kKeyAccelDebugSpeed] = Json::Value(debug_data.speed);
460 if (debug_data.speed != debug_data.smoothed_speed)
461 ret[kKeyAccelDebugSmoothSpeed] = Json::Value(debug_data.smoothed_speed);
462 ret[kKeyAccelDebugGainX] = Json::Value(debug_data.gain_x);
463 ret[kKeyAccelDebugGainY] = Json::Value(debug_data.gain_y);
464 return ret;
465 }
466
EncodeGestureDebug(const TimestampGestureDebug & debug_data)467 Json::Value ActivityLog::EncodeGestureDebug(
468 const TimestampGestureDebug& debug_data) {
469 Json::Value ret(Json::objectValue);
470 ret[kKeyType] = Json::Value(kKeyTimestampGestureDebug);
471 ret[kKeyTimestampDebugSkew] = Json::Value(debug_data.skew);
472 return ret;
473 }
474
EncodeHardwareStateDebug(const TimestampHardwareStateDebug & debug_data)475 Json::Value ActivityLog::EncodeHardwareStateDebug(
476 const TimestampHardwareStateDebug& debug_data) {
477 Json::Value ret(Json::objectValue);
478 ret[kKeyType] = Json::Value(kKeyTimestampHardwareStateDebug);
479 ret[kKeyTimestampDebugIsUsingFake] = Json::Value(debug_data.is_using_fake);
480 if (debug_data.is_using_fake) {
481 ret[kKeyTimestampDebugWasFirstOrBackward] =
482 Json::Value(debug_data.was_first_or_backward);
483 ret[kKeyTimestampDebugPrevMscTimestampIn] =
484 Json::Value(debug_data.prev_msc_timestamp_in);
485 ret[kKeyTimestampDebugPrevMscTimestampOut] =
486 Json::Value(debug_data.prev_msc_timestamp_out);
487 } else {
488 ret[kKeyTimestampDebugWasDivergenceReset] =
489 Json::Value(debug_data.was_divergence_reset);
490 ret[kKeyTimestampDebugFakeTimestampIn] =
491 Json::Value(debug_data.fake_timestamp_in);
492 ret[kKeyTimestampDebugFakeTimestampDelta] =
493 Json::Value(debug_data.fake_timestamp_delta);
494 ret[kKeyTimestampDebugFakeTimestampOut] =
495 Json::Value(debug_data.fake_timestamp_out);
496 }
497 ret[kKeyTimestampDebugSkew] = Json::Value(debug_data.skew);
498 ret[kKeyTimestampDebugMaxSkew] = Json::Value(debug_data.max_skew);
499 return ret;
500 }
501
EncodePropRegistry()502 Json::Value ActivityLog::EncodePropRegistry() {
503 Json::Value ret(Json::objectValue);
504 if (!prop_reg_)
505 return ret;
506
507 const set<Property*>& props = prop_reg_->props();
508 for (set<Property*>::const_iterator it = props.begin(), e = props.end();
509 it != e; ++it) {
510 ret[(*it)->name()] = (*it)->NewValue();
511 }
512 return ret;
513 }
514
EncodeCommonInfo()515 Json::Value ActivityLog::EncodeCommonInfo() {
516 Json::Value root(Json::objectValue);
517 Json::Value entries(Json::arrayValue);
518 for (size_t i = 0; i < size_; ++i) {
519 const Entry& entry = buffer_[(i + head_idx_) % kBufferSize];
520 std::visit(
521 Visitor {
522 [this, &entries](HardwareState hwstate) {
523 entries.append(EncodeHardwareState(hwstate));
524 },
525 [this, &entries](HardwareStatePre hwstate) {
526 entries.append(EncodeHardwareState(hwstate));
527 },
528 [this, &entries](HardwareStatePost hwstate) {
529 entries.append(EncodeHardwareState(hwstate));
530 },
531 [this, &entries](TimerCallbackEntry now) {
532 entries.append(EncodeTimerCallback(now.timestamp));
533 },
534 [this, &entries](CallbackRequestEntry when) {
535 entries.append(EncodeCallbackRequest(when.timestamp));
536 },
537 [this, &entries](Gesture gesture) {
538 entries.append(EncodeGesture(gesture));
539 },
540 [this, &entries](GestureConsume gesture) {
541 entries.append(EncodeGesture(gesture));
542 },
543 [this, &entries](GestureProduce gesture) {
544 entries.append(EncodeGesture(gesture));
545 },
546 [this, &entries](PropChangeEntry prop_change) {
547 entries.append(EncodePropChange(prop_change));
548 },
549 [this, &entries](HandleTimerPre handle) {
550 entries.append(EncodeHandleTimer(handle));
551 },
552 [this, &entries](HandleTimerPost handle) {
553 entries.append(EncodeHandleTimer(handle));
554 },
555 [this, &entries](AccelGestureDebug debug_data) {
556 entries.append(EncodeGestureDebug(debug_data));
557 },
558 [this, &entries](TimestampGestureDebug debug_data) {
559 entries.append(EncodeGestureDebug(debug_data));
560 },
561 [this, &entries](TimestampHardwareStateDebug debug_data) {
562 entries.append(EncodeHardwareStateDebug(debug_data));
563 },
564 [](auto arg) {
565 Err("Unknown entry type");
566 }
567 }, entry.details);
568 }
569 root[kKeyRoot] = entries;
570 root[kKeyHardwarePropRoot] = EncodeHardwareProperties();
571
572 return root;
573 }
574
AddEncodeInfo(Json::Value * root)575 void ActivityLog::AddEncodeInfo(Json::Value* root) {
576 (*root)["version"] = Json::Value(1);
577 string gestures_version = VCSID;
578
579 // Strip tailing whitespace.
580 gestures_version = TrimWhitespaceASCII(gestures_version);
581 (*root)["gesturesVersion"] = Json::Value(gestures_version);
582 (*root)[kKeyProperties] = EncodePropRegistry();
583 }
584
Encode()585 string ActivityLog::Encode() {
586 Json::Value root = EncodeCommonInfo();
587 AddEncodeInfo(&root);
588 return root.toStyledString();
589 }
590
591 const char ActivityLog::kKeyInterpreterName[] = "interpreterName";
592 const char ActivityLog::kKeyNext[] = "nextLayer";
593 const char ActivityLog::kKeyRoot[] = "entries";
594 const char ActivityLog::kKeyType[] = "type";
595 const char ActivityLog::kKeyMethodName[] = "methodName";
596 const char ActivityLog::kKeyHardwareState[] = "hardwareState";
597 const char ActivityLog::kKeyHardwareStatePre[] = "debugHardwareStatePre";
598 const char ActivityLog::kKeyHardwareStatePost[] = "debugHardwareStatePost";
599 const char ActivityLog::kKeyTimerCallback[] = "timerCallback";
600 const char ActivityLog::kKeyCallbackRequest[] = "callbackRequest";
601 const char ActivityLog::kKeyGesture[] = "gesture";
602 const char ActivityLog::kKeyGestureConsume[] = "debugGestureConsume";
603 const char ActivityLog::kKeyGestureProduce[] = "debugGestureProduce";
604 const char ActivityLog::kKeyHandleTimerPre[] = "debugHandleTimerPre";
605 const char ActivityLog::kKeyHandleTimerPost[] = "debugHandleTimerPost";
606 const char ActivityLog::kKeyTimerNow[] = "now";
607 const char ActivityLog::kKeyHandleTimerTimeout[] = "timeout";
608 const char ActivityLog::kKeyPropChange[] = "propertyChange";
609 const char ActivityLog::kKeyHardwareStateTimestamp[] = "timestamp";
610 const char ActivityLog::kKeyHardwareStateButtonsDown[] = "buttonsDown";
611 const char ActivityLog::kKeyHardwareStateTouchCnt[] = "touchCount";
612 const char ActivityLog::kKeyHardwareStateFingers[] = "fingers";
613 const char ActivityLog::kKeyHardwareStateRelX[] = "relX";
614 const char ActivityLog::kKeyHardwareStateRelY[] = "relY";
615 const char ActivityLog::kKeyHardwareStateRelWheel[] = "relWheel";
616 const char ActivityLog::kKeyHardwareStateRelHWheel[] = "relHWheel";
617 const char ActivityLog::kKeyFingerStateTouchMajor[] = "touchMajor";
618 const char ActivityLog::kKeyFingerStateTouchMinor[] = "touchMinor";
619 const char ActivityLog::kKeyFingerStateWidthMajor[] = "widthMajor";
620 const char ActivityLog::kKeyFingerStateWidthMinor[] = "widthMinor";
621 const char ActivityLog::kKeyFingerStatePressure[] = "pressure";
622 const char ActivityLog::kKeyFingerStateOrientation[] = "orientation";
623 const char ActivityLog::kKeyFingerStatePositionX[] = "positionX";
624 const char ActivityLog::kKeyFingerStatePositionY[] = "positionY";
625 const char ActivityLog::kKeyFingerStateTrackingId[] = "trackingId";
626 const char ActivityLog::kKeyFingerStateFlags[] = "flags";
627 const char ActivityLog::kKeyCallbackRequestWhen[] = "when";
628 const char ActivityLog::kKeyGestureType[] = "gestureType";
629 const char ActivityLog::kValueGestureTypeContactInitiated[] =
630 "contactInitiated";
631 const char ActivityLog::kValueGestureTypeMove[] = "move";
632 const char ActivityLog::kValueGestureTypeScroll[] = "scroll";
633 const char ActivityLog::kValueGestureTypeMouseWheel[] = "mouseWheel";
634 const char ActivityLog::kValueGestureTypePinch[] = "pinch";
635 const char ActivityLog::kValueGestureTypeButtonsChange[] = "buttonsChange";
636 const char ActivityLog::kValueGestureTypeFling[] = "fling";
637 const char ActivityLog::kValueGestureTypeSwipe[] = "swipe";
638 const char ActivityLog::kValueGestureTypeSwipeLift[] = "swipeLift";
639 const char ActivityLog::kValueGestureTypeFourFingerSwipe[] = "fourFingerSwipe";
640 const char ActivityLog::kValueGestureTypeFourFingerSwipeLift[] =
641 "fourFingerSwipeLift";
642 const char ActivityLog::kValueGestureTypeMetrics[] = "metrics";
643 const char ActivityLog::kKeyGestureStartTime[] = "startTime";
644 const char ActivityLog::kKeyGestureEndTime[] = "endTime";
645 const char ActivityLog::kKeyGestureDX[] = "dx";
646 const char ActivityLog::kKeyGestureDY[] = "dy";
647 const char ActivityLog::kKeyGestureOrdinalDX[] = "ordinalDx";
648 const char ActivityLog::kKeyGestureOrdinalDY[] = "ordinalDy";
649 const char ActivityLog::kKeyGestureMouseWheelTicksDX[] = "ticksDx";
650 const char ActivityLog::kKeyGestureMouseWheelTicksDY[] = "ticksDy";
651 const char ActivityLog::kKeyGesturePinchDZ[] = "dz";
652 const char ActivityLog::kKeyGesturePinchOrdinalDZ[] = "ordinalDz";
653 const char ActivityLog::kKeyGesturePinchZoomState[] = "zoomState";
654 const char ActivityLog::kKeyGestureButtonsChangeDown[] = "down";
655 const char ActivityLog::kKeyGestureButtonsChangeUp[] = "up";
656 const char ActivityLog::kKeyGestureFlingVX[] = "vx";
657 const char ActivityLog::kKeyGestureFlingVY[] = "vy";
658 const char ActivityLog::kKeyGestureFlingOrdinalVX[] = "ordinalVx";
659 const char ActivityLog::kKeyGestureFlingOrdinalVY[] = "ordinalVy";
660 const char ActivityLog::kKeyGestureFlingState[] = "flingState";
661 const char ActivityLog::kKeyGestureMetricsType[] = "metricsType";
662 const char ActivityLog::kKeyGestureMetricsData1[] = "data1";
663 const char ActivityLog::kKeyGestureMetricsData2[] = "data2";
664 const char ActivityLog::kKeyPropChangeType[] = "propChangeType";
665 const char ActivityLog::kKeyPropChangeName[] = "name";
666 const char ActivityLog::kKeyPropChangeValue[] = "value";
667 const char ActivityLog::kValuePropChangeTypeBool[] = "bool";
668 const char ActivityLog::kValuePropChangeTypeDouble[] = "double";
669 const char ActivityLog::kValuePropChangeTypeInt[] = "int";
670 const char ActivityLog::kValuePropChangeTypeShort[] = "short";
671 const char ActivityLog::kKeyHardwarePropRoot[] = "hardwareProperties";
672 const char ActivityLog::kKeyHardwarePropLeft[] = "left";
673 const char ActivityLog::kKeyHardwarePropTop[] = "top";
674 const char ActivityLog::kKeyHardwarePropRight[] = "right";
675 const char ActivityLog::kKeyHardwarePropBottom[] = "bottom";
676 const char ActivityLog::kKeyHardwarePropXResolution[] = "xResolution";
677 const char ActivityLog::kKeyHardwarePropYResolution[] = "yResolution";
678 const char ActivityLog::kKeyHardwarePropXDpi[] = "xDpi";
679 const char ActivityLog::kKeyHardwarePropYDpi[] = "yDpi";
680 const char ActivityLog::kKeyHardwarePropOrientationMinimum[] =
681 "orientationMinimum";
682 const char ActivityLog::kKeyHardwarePropOrientationMaximum[] =
683 "orientationMaximum";
684 const char ActivityLog::kKeyHardwarePropMaxFingerCount[] = "maxFingerCount";
685 const char ActivityLog::kKeyHardwarePropMaxTouchCount[] = "maxTouchCount";
686 const char ActivityLog::kKeyHardwarePropSupportsT5R2[] = "supportsT5R2";
687 const char ActivityLog::kKeyHardwarePropSemiMt[] = "semiMt";
688 const char ActivityLog::kKeyHardwarePropIsButtonPad[] = "isButtonPad";
689 const char ActivityLog::kKeyHardwarePropHasWheel[] = "hasWheel";
690
691 const char ActivityLog::kKeyProperties[] = "properties";
692
693 const char ActivityLog::kKeyAccelGestureDebug[] = "debugAccelGesture";
694 const char ActivityLog::kKeyAccelDebugNoAccelBadGain[] = "noAccelBadGain";
695 const char ActivityLog::kKeyAccelDebugNoAccelGestureType[] = "noAccelBadType";
696 const char ActivityLog::kKeyAccelDebugNoAccelSmallDt[] = "noAccelSmallDt";
697 const char ActivityLog::kKeyAccelDebugNoAccelSmallSpeed[] =
698 "noAccelSmallSpeed";
699 const char ActivityLog::kKeyAccelDebugDroppedGesture[] = "gestureDropped";
700 const char ActivityLog::kKeyAccelDebugXYAreVelocity[] = "XYAreVelocity";
701 const char ActivityLog::kKeyAccelDebugXScale[] = "XScale";
702 const char ActivityLog::kKeyAccelDebugYScale[] = "YScale";
703 const char ActivityLog::kKeyAccelDebugDt[] = "dt";
704 const char ActivityLog::kKeyAccelDebugAdjustedDt[] = "adjDt";
705 const char ActivityLog::kKeyAccelDebugSpeed[] = "speed";
706 const char ActivityLog::kKeyAccelDebugSmoothSpeed[] = "smoothSpeed";
707 const char ActivityLog::kKeyAccelDebugGainX[] = "gainX";
708 const char ActivityLog::kKeyAccelDebugGainY[] = "gainY";
709
710 const char ActivityLog::kKeyTimestampGestureDebug[] = "debugTimestampGesture";
711 const char ActivityLog::kKeyTimestampHardwareStateDebug[] =
712 "debugTimestampHardwareState";
713 const char ActivityLog::kKeyTimestampDebugIsUsingFake[] = "isUsingFake";
714 const char ActivityLog::kKeyTimestampDebugWasFirstOrBackward[] =
715 "wasFirstOrBackward";
716 const char ActivityLog::kKeyTimestampDebugPrevMscTimestampIn[] =
717 "prevMscTimestampIn";
718 const char ActivityLog::kKeyTimestampDebugPrevMscTimestampOut[] =
719 "prevMscTimestampOut";
720 const char ActivityLog::kKeyTimestampDebugWasDivergenceReset[] =
721 "wasDivergenceReset";
722 const char ActivityLog::kKeyTimestampDebugFakeTimestampIn[] =
723 "fakeTimestampIn";
724 const char ActivityLog::kKeyTimestampDebugFakeTimestampDelta[] =
725 "fakeTimestampDelta";
726 const char ActivityLog::kKeyTimestampDebugFakeTimestampOut[] =
727 "fakeTimestampOut";
728 const char ActivityLog::kKeyTimestampDebugSkew[] = "skew";
729 const char ActivityLog::kKeyTimestampDebugMaxSkew[] = "maxSkew";
730
731 } // namespace gestures
732