• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ARKUI_INPUT_RAW_INPUT_INJECTOR_RAW_INPUT_COMMAND_H
17 #define ARKUI_INPUT_RAW_INPUT_INJECTOR_RAW_INPUT_COMMAND_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "injector_utils.h"
23 
24 namespace OHOS {
25 namespace Ace {
26 // command types
27 enum class CommandType : size_t { TOUCH_DOWN = 0, TOUCH_UP, TOUCH_MOVE, TOUCH_CANCEL, COMMAND_WAIT, UNKNOWN };
28 
29 struct ConsumeActionInfo {
30     CommandType type = CommandType::UNKNOWN;
31     CoordinateCurve curve = CoordinateCurve::EASE_IN_OUT;
32     int32_t finger = 0;
33     int32_t targetDisplayX = 0;
34     int32_t targetDisplayY = 0;
35     int32_t consumeIndex = 0;
36     int32_t totalCount = 1;
37 };
38 
39 class BaseCommand {
40 public:
41     BaseCommand() = default;
42     virtual ~BaseCommand() = default;
43     virtual bool Feed(const std::vector<std::string>& data);
44     virtual std::string ToString() const = 0;
45     bool ConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo);
46     // implemented by child for raw event injecting
47     virtual bool DoConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo) = 0;
48 
49     static std::string GetCommandTypeName(CommandType type);
50     static std::string GetReadableCommandTypeName(CommandType type);
51 
IsConsumedOut()52     bool IsConsumedOut() const
53     {
54         return isConsumedOut_;
55     }
56 
SetDuration(int32_t duration)57     void SetDuration(int32_t duration)
58     {
59         duration_ = duration;
60         auto total = (duration / eventInjectingInterval_);
61         totalCount_ = (duration % eventInjectingInterval_ != 0) ? (total + 1) : total;
62     }
63 
GetDuration()64     int32_t GetDuration() const
65     {
66         return duration_;
67     }
68 
SetCommandType(CommandType type)69     void SetCommandType(CommandType type)
70     {
71         commandType_ = type;
72     }
73 
GetCommandType()74     CommandType GetCommandType() const
75     {
76         return commandType_;
77     }
78 
SetFingerId(int32_t finger)79     void SetFingerId(int32_t finger)
80     {
81         fingerId_ = finger;
82     }
83 
GetFingerId()84     int32_t GetFingerId() const
85     {
86         return fingerId_;
87     }
88 
GetConsumedCount()89     int32_t GetConsumedCount() const
90     {
91         return consumedCount_;
92     }
93 
GetTotalCount()94     int32_t GetTotalCount() const
95     {
96         return totalCount_;
97     }
98 
SetCoordinateCurve(CoordinateCurve curve)99     void SetCoordinateCurve(CoordinateCurve curve)
100     {
101         coordinateCurve_ = curve;
102     }
103 
GetCoordinateCurve()104     CoordinateCurve GetCoordinateCurve() const
105     {
106         return coordinateCurve_;
107     }
108 
MarkConsumedOnce()109     void MarkConsumedOnce()
110     {
111         consumedCount_++;
112     }
113 
114     // inject event every 5ms, not use 7ms as we need consider the IPC consumed time
115     static const int32_t EVENT_INJECTING_INTERVAL = 5; // inject event every 5 ms
116 
117 private:
MarkConsumeBaseTimeLine(int64_t currentTime)118     void MarkConsumeBaseTimeLine(int64_t currentTime)
119     {
120         if (consumeBaseTimeLine_ < 0) {
121             consumeBaseTimeLine_ = currentTime;
122         }
123     }
124 
125     int32_t eventInjectingInterval_ = EVENT_INJECTING_INTERVAL;
126     int32_t fingerId_ = 0;
127     int32_t duration_ = 0;             // the total time for this command can be executed
128     int64_t consumeBaseTimeLine_ = -1; // the base time line for consume this command
129     bool isConsumedOut_ = false;
130     int32_t consumedCount_ = 0;
131     int32_t totalCount_ = 1;
132     CoordinateCurve coordinateCurve_ = CoordinateCurve::EASE_IN_OUT;
133     CommandType commandType_ = CommandType::UNKNOWN;
134 };
135 
136 class WaitCommand : public BaseCommand {
137 public:
138     WaitCommand() = default;
139     ~WaitCommand() override = default;
140     bool DoConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo) override;
141     std::string ToString() const override;
142 };
143 
144 class BaseRawEventCommand : public BaseCommand {
145 public:
146     BaseRawEventCommand() = default;
147     ~BaseRawEventCommand() override = default;
148     bool Feed(const std::vector<std::string>& data) override;
OnFeeded(bool durationSet,const std::vector<std::string> & data)149     virtual void OnFeeded(bool durationSet, const std::vector<std::string>& data) {}
150     bool DoConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo) override;
151     std::string ToString() const override;
152 
SetTargetDisplayX(int32_t x)153     void SetTargetDisplayX(int32_t x)
154     {
155         targetDisplayX_ = x;
156     }
157 
GetTargetDisplayX()158     int32_t GetTargetDisplayX() const
159     {
160         return targetDisplayX_;
161     }
162 
SetTargetDisplayY(int32_t y)163     void SetTargetDisplayY(int32_t y)
164     {
165         targetDisplayY_ = y;
166     }
167 
GetTargetDisplayY()168     int32_t GetTargetDisplayY() const
169     {
170         return targetDisplayY_;
171     }
172 
173 private:
174     int32_t targetDisplayX_ = 0;
175     int32_t targetDisplayY_ = 0;
176 };
177 
178 class TouchDownCommand : public BaseRawEventCommand {
179 public:
180     TouchDownCommand() = default;
181     ~TouchDownCommand() override = default;
182 };
183 
184 class TouchUpCommand : public BaseRawEventCommand {
185 public:
186     TouchUpCommand() = default;
187     ~TouchUpCommand() override = default;
188 };
189 
190 class TouchMoveCommand : public BaseRawEventCommand {
191 public:
192     TouchMoveCommand() = default;
193     ~TouchMoveCommand() override = default;
194     void OnFeeded(bool durationSet, const std::vector<std::string>& data) override;
195     static const int32_t DEFAULT_MOVE_DURATION = 1000;
196 };
197 
198 class TouchCancelCommand : public BaseRawEventCommand {
199 public:
200     TouchCancelCommand() = default;
201     ~TouchCancelCommand() override = default;
202 };
203 
204 class CommandList final {
205 public:
206     CommandList() = default;
207     ~CommandList() = default;
208 
209     std::string ToString() const;
210     void AddCommand(std::shared_ptr<BaseCommand> command);
211     void Clear();
212     const std::vector<std::shared_ptr<BaseCommand>>& GetCommands() const;
213     bool ConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo);
214 
SetPointerID(int32_t pointerID)215     void SetPointerID(int32_t pointerID)
216     {
217         pointerID_ = pointerID;
218     }
219 
GetPointerID()220     int32_t GetPointerID() const
221     {
222         return pointerID_;
223     }
224 
225     bool IsAllConsumed() const;
226 
227 private:
228     std::vector<std::shared_ptr<BaseCommand>> commands_;
229     int32_t pointerID_ = 0;
230     size_t currentConsumingIndex_ = 0;
231 };
232 
233 } // namespace Ace
234 } // namespace OHOS
235 #endif // ARKUI_INPUT_RAW_INPUT_INJECTOR_RAW_INPUT_COMMAND_H
236