xref: /aosp_15_r20/external/libchrome-gestures/include/interpreter.h (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1*aed3e508SAndroid Build Coastguard Worker // Copyright 2012 The ChromiumOS Authors
2*aed3e508SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*aed3e508SAndroid Build Coastguard Worker // found in the LICENSE file.
4*aed3e508SAndroid Build Coastguard Worker 
5*aed3e508SAndroid Build Coastguard Worker #include <string>
6*aed3e508SAndroid Build Coastguard Worker 
7*aed3e508SAndroid Build Coastguard Worker #include <gtest/gtest.h>
8*aed3e508SAndroid Build Coastguard Worker 
9*aed3e508SAndroid Build Coastguard Worker #include "include/activity_log.h"
10*aed3e508SAndroid Build Coastguard Worker #include "include/gestures.h"
11*aed3e508SAndroid Build Coastguard Worker #include "include/prop_registry.h"
12*aed3e508SAndroid Build Coastguard Worker #include "include/tracer.h"
13*aed3e508SAndroid Build Coastguard Worker 
14*aed3e508SAndroid Build Coastguard Worker #ifndef GESTURES_INTERPRETER_H__
15*aed3e508SAndroid Build Coastguard Worker #define GESTURES_INTERPRETER_H__
16*aed3e508SAndroid Build Coastguard Worker 
17*aed3e508SAndroid Build Coastguard Worker // This is a collection of supporting structs and an interface for
18*aed3e508SAndroid Build Coastguard Worker // Interpreters.
19*aed3e508SAndroid Build Coastguard Worker 
20*aed3e508SAndroid Build Coastguard Worker struct HardwareState;
21*aed3e508SAndroid Build Coastguard Worker 
22*aed3e508SAndroid Build Coastguard Worker namespace gestures {
23*aed3e508SAndroid Build Coastguard Worker 
24*aed3e508SAndroid Build Coastguard Worker class GestureConsumer {
25*aed3e508SAndroid Build Coastguard Worker  public:
~GestureConsumer()26*aed3e508SAndroid Build Coastguard Worker   virtual ~GestureConsumer() {}
27*aed3e508SAndroid Build Coastguard Worker   virtual void ConsumeGesture(const Gesture& gesture) = 0;
28*aed3e508SAndroid Build Coastguard Worker };
29*aed3e508SAndroid Build Coastguard Worker 
30*aed3e508SAndroid Build Coastguard Worker class Metrics;
31*aed3e508SAndroid Build Coastguard Worker class MetricsProperties;
32*aed3e508SAndroid Build Coastguard Worker 
33*aed3e508SAndroid Build Coastguard Worker // Interface for all interpreters. Interpreters currently are synchronous.
34*aed3e508SAndroid Build Coastguard Worker // A synchronous interpreter will return  0 or 1 Gestures for each passed in
35*aed3e508SAndroid Build Coastguard Worker // HardwareState.
36*aed3e508SAndroid Build Coastguard Worker class Interpreter {
37*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, SimpleTest);
38*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, ResetLogTest);
39*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, LoggingDisabledByDefault);
40*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(LoggingFilterInterpreterTest, LogResetHandlerTest);
41*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, EventDebugLoggingEnableTest);
42*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, LogHardwareStateTest);
43*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, LogGestureTest);
44*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(InterpreterTest, LogHandleTimerTest);
45*aed3e508SAndroid Build Coastguard Worker   FRIEND_TEST(TimestampFilterInterpreterParmTest, TimestampDebugLoggingTest);
46*aed3e508SAndroid Build Coastguard Worker  public:
47*aed3e508SAndroid Build Coastguard Worker   Interpreter(PropRegistry* prop_reg, Tracer* tracer, bool force_log_creation);
48*aed3e508SAndroid Build Coastguard Worker   virtual ~Interpreter();
49*aed3e508SAndroid Build Coastguard Worker 
50*aed3e508SAndroid Build Coastguard Worker   // Called to interpret the current state.
51*aed3e508SAndroid Build Coastguard Worker   // The passed |hwstate| may be modified.
52*aed3e508SAndroid Build Coastguard Worker   // If *timeout is set to >0.0, a timer will be setup to call
53*aed3e508SAndroid Build Coastguard Worker   // HandleTimer after *timeout time passes. An interpreter can only
54*aed3e508SAndroid Build Coastguard Worker   // have up to 1 outstanding timer, so if a timeout is requested by
55*aed3e508SAndroid Build Coastguard Worker   // setting *timeout and one already exists, the old one will be cancelled
56*aed3e508SAndroid Build Coastguard Worker   // and reused for this timeout.
57*aed3e508SAndroid Build Coastguard Worker   virtual void SyncInterpret(HardwareState& hwstate, stime_t* timeout);
58*aed3e508SAndroid Build Coastguard Worker 
59*aed3e508SAndroid Build Coastguard Worker   // Called to handle a timeout.
60*aed3e508SAndroid Build Coastguard Worker   // If *timeout is set to >0.0, a timer will be setup to call
61*aed3e508SAndroid Build Coastguard Worker   // HandleTimer after *timeout time passes. An interpreter can only
62*aed3e508SAndroid Build Coastguard Worker   // have up to 1 outstanding timer, so if a timeout is requested by
63*aed3e508SAndroid Build Coastguard Worker   // setting *timeout and one already exists, the old one will be cancelled
64*aed3e508SAndroid Build Coastguard Worker   // and reused for this timeout.
65*aed3e508SAndroid Build Coastguard Worker   virtual void HandleTimer(stime_t now, stime_t* timeout);
66*aed3e508SAndroid Build Coastguard Worker 
67*aed3e508SAndroid Build Coastguard Worker   virtual void Initialize(const HardwareProperties* hwprops,
68*aed3e508SAndroid Build Coastguard Worker                           Metrics* metrics, MetricsProperties* mprops,
69*aed3e508SAndroid Build Coastguard Worker                           GestureConsumer* consumer);
70*aed3e508SAndroid Build Coastguard Worker 
71*aed3e508SAndroid Build Coastguard Worker   virtual Json::Value EncodeCommonInfo();
72*aed3e508SAndroid Build Coastguard Worker   std::string Encode();
73*aed3e508SAndroid Build Coastguard Worker 
Clear()74*aed3e508SAndroid Build Coastguard Worker   virtual void Clear() {
75*aed3e508SAndroid Build Coastguard Worker     if (log_.get())
76*aed3e508SAndroid Build Coastguard Worker       log_->Clear();
77*aed3e508SAndroid Build Coastguard Worker   }
78*aed3e508SAndroid Build Coastguard Worker 
79*aed3e508SAndroid Build Coastguard Worker   virtual void ProduceGesture(const Gesture& gesture);
name()80*aed3e508SAndroid Build Coastguard Worker   const char* name() const { return name_; }
81*aed3e508SAndroid Build Coastguard Worker 
82*aed3e508SAndroid Build Coastguard Worker  protected:
83*aed3e508SAndroid Build Coastguard Worker   std::unique_ptr<ActivityLog> log_;
84*aed3e508SAndroid Build Coastguard Worker   GestureConsumer* consumer_;
85*aed3e508SAndroid Build Coastguard Worker   const HardwareProperties* hwprops_;
86*aed3e508SAndroid Build Coastguard Worker   Metrics* metrics_;
87*aed3e508SAndroid Build Coastguard Worker   std::unique_ptr<Metrics> own_metrics_;
88*aed3e508SAndroid Build Coastguard Worker   bool requires_metrics_;
89*aed3e508SAndroid Build Coastguard Worker   bool initialized_;
90*aed3e508SAndroid Build Coastguard Worker 
91*aed3e508SAndroid Build Coastguard Worker   void InitName();
92*aed3e508SAndroid Build Coastguard Worker   void Trace(const char* message, const char* name);
93*aed3e508SAndroid Build Coastguard Worker 
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)94*aed3e508SAndroid Build Coastguard Worker   virtual void SyncInterpretImpl(HardwareState& hwstate,
95*aed3e508SAndroid Build Coastguard Worker                                  stime_t* timeout) {}
HandleTimerImpl(stime_t now,stime_t * timeout)96*aed3e508SAndroid Build Coastguard Worker   virtual void HandleTimerImpl(stime_t now, stime_t* timeout) {}
97*aed3e508SAndroid Build Coastguard Worker 
98*aed3e508SAndroid Build Coastguard Worker   bool EventLoggingIsEnabled();
99*aed3e508SAndroid Build Coastguard Worker   void SetEventLoggingEnabled(bool enabled);
100*aed3e508SAndroid Build Coastguard Worker 
101*aed3e508SAndroid Build Coastguard Worker   bool EventDebugLoggingIsEnabled(ActivityLog::EventDebug event);
102*aed3e508SAndroid Build Coastguard Worker   uint32_t GetEventDebugLoggingEnabled();
103*aed3e508SAndroid Build Coastguard Worker   void SetEventDebugLoggingEnabled(uint32_t enabled);
104*aed3e508SAndroid Build Coastguard Worker   void EventDebugLoggingDisable(ActivityLog::EventDebug event);
105*aed3e508SAndroid Build Coastguard Worker   void EventDebugLoggingEnable(ActivityLog::EventDebug event);
106*aed3e508SAndroid Build Coastguard Worker 
107*aed3e508SAndroid Build Coastguard Worker   void LogGestureConsume(const std::string& name, const Gesture& gesture);
108*aed3e508SAndroid Build Coastguard Worker   void LogGestureProduce(const std::string& name, const Gesture& gesture);
109*aed3e508SAndroid Build Coastguard Worker   void LogHardwareStatePre(const std::string& name,
110*aed3e508SAndroid Build Coastguard Worker                            const HardwareState& hwstate);
111*aed3e508SAndroid Build Coastguard Worker   void LogHardwareStatePost(const std::string& name,
112*aed3e508SAndroid Build Coastguard Worker                             const HardwareState& hwstate);
113*aed3e508SAndroid Build Coastguard Worker   void LogHandleTimerPre(const std::string& name,
114*aed3e508SAndroid Build Coastguard Worker                          stime_t now, const stime_t* timeout);
115*aed3e508SAndroid Build Coastguard Worker   void LogHandleTimerPost(const std::string& name,
116*aed3e508SAndroid Build Coastguard Worker                           stime_t now, const stime_t* timeout);
117*aed3e508SAndroid Build Coastguard Worker 
118*aed3e508SAndroid Build Coastguard Worker  private:
119*aed3e508SAndroid Build Coastguard Worker   const char* name_;
120*aed3e508SAndroid Build Coastguard Worker   Tracer* tracer_;
121*aed3e508SAndroid Build Coastguard Worker 
122*aed3e508SAndroid Build Coastguard Worker   bool enable_event_logging_ = false;
123*aed3e508SAndroid Build Coastguard Worker   uint32_t enable_event_debug_logging_ = 0;
124*aed3e508SAndroid Build Coastguard Worker 
125*aed3e508SAndroid Build Coastguard Worker   void LogOutputs(const Gesture* result, stime_t* timeout, const char* action);
126*aed3e508SAndroid Build Coastguard Worker };
127*aed3e508SAndroid Build Coastguard Worker }  // namespace gestures
128*aed3e508SAndroid Build Coastguard Worker 
129*aed3e508SAndroid Build Coastguard Worker #endif  // GESTURES_INTERPRETER_H__
130