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