• 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 #include "raw_input_command.h"
17 
18 #include <iostream>
19 
20 #include "string_ex.h"
21 
22 namespace OHOS {
23 namespace Ace {
24 namespace {
25 constexpr int32_t MIN_DATA_COUNT_FOR_RAW_EVENT = 2;
26 constexpr int32_t MAX_DATA_COUNT_FOR_RAW_EVENT = 4;
27 constexpr int32_t RAW_EVENT_X_INDEX = 0;
28 constexpr int32_t RAW_EVENT_Y_INDEX = 1;
29 constexpr int32_t RAW_EVENT_DURATION_INDEX = 2;
30 } // namespace
31 
GetCommandTypeName(CommandType type)32 std::string BaseCommand::GetCommandTypeName(CommandType type)
33 {
34     switch (type) {
35         case CommandType::TOUCH_DOWN:
36             return "d";
37         case CommandType::TOUCH_MOVE:
38             return "m";
39         case CommandType::TOUCH_UP:
40             return "u";
41         case CommandType::TOUCH_CANCEL:
42             return "c";
43         case CommandType::COMMAND_WAIT:
44             return "w";
45         default:
46             return "";
47     }
48 }
49 
GetReadableCommandTypeName(CommandType type)50 std::string BaseCommand::GetReadableCommandTypeName(CommandType type)
51 {
52     switch (type) {
53         case CommandType::TOUCH_DOWN:
54             return "down";
55         case CommandType::TOUCH_MOVE:
56             return "move";
57         case CommandType::TOUCH_UP:
58             return "up";
59         case CommandType::TOUCH_CANCEL:
60             return "cancel";
61         case CommandType::COMMAND_WAIT:
62             return "wait";
63         default:
64             return "";
65     }
66 }
67 
Feed(const std::vector<std::string> & data)68 bool BaseCommand::Feed(const std::vector<std::string>& data)
69 {
70     if (data.empty()) {
71         return true;
72     }
73     for (const auto& str : data) {
74         int32_t duration;
75         if (!StrToInt(str, duration)) {
76             return false;
77         }
78         SetDuration(duration);
79         break;
80     }
81 
82     return true;
83 }
84 
85 // -d/m/u/c x y duration finger
Feed(const std::vector<std::string> & data)86 bool BaseRawEventCommand::Feed(const std::vector<std::string>& data)
87 {
88     if (data.size() < MIN_DATA_COUNT_FOR_RAW_EVENT || data.size() > MAX_DATA_COUNT_FOR_RAW_EVENT) {
89         std::cout << "error data count for -" << GetCommandTypeName(GetCommandType()) << std::endl;
90         return false;
91     }
92 
93     bool durationSet = false;
94     int32_t paramInd = 0;
95     for (const auto& str : data) {
96         int32_t value;
97         if (!StrToInt(str, value)) {
98             std::cout << "parse error for " << str << std::endl;
99             return false;
100         }
101         switch (paramInd) {
102             case RAW_EVENT_X_INDEX:
103                 SetTargetDisplayX(value);
104                 break;
105             case RAW_EVENT_Y_INDEX:
106                 SetTargetDisplayY(value);
107                 break;
108             case RAW_EVENT_DURATION_INDEX:
109                 durationSet = true;
110                 SetDuration(value);
111                 break;
112             default:
113                 break;
114         }
115         paramInd++;
116     }
117 
118     OnFeeded(durationSet, data);
119 
120     return true;
121 }
122 
ToString() const123 std::string BaseRawEventCommand::ToString() const
124 {
125     std::string typeName = GetReadableCommandTypeName(GetCommandType());
126     std::string result = "";
127     if (GetDuration() != 0) {
128         result = typeName + "(" + std::to_string(GetTargetDisplayX()) + ", " + std::to_string(GetTargetDisplayY()) +
129                  ")-[" + std::to_string(GetDuration()) + "ms]";
130     } else {
131         result =
132             typeName + "(" + std::to_string(GetTargetDisplayX()) + ", " + std::to_string(GetTargetDisplayY()) + ")";
133     }
134     return result;
135 }
136 
ToString() const137 std::string WaitCommand::ToString() const
138 {
139     return "wait-[" + std::to_string(GetDuration()) + "]ms";
140 }
141 
OnFeeded(bool durationSet,const std::vector<std::string> & data)142 void TouchMoveCommand::OnFeeded(bool durationSet, const std::vector<std::string>& data)
143 {
144     (void)(data); // not used for now
145     if (!durationSet) {
146         // for touch move, the default duration is 1000
147         SetDuration(DEFAULT_MOVE_DURATION);
148     }
149 }
150 
AddCommand(std::shared_ptr<BaseCommand> command)151 void CommandList::AddCommand(std::shared_ptr<BaseCommand> command)
152 {
153     commands_.push_back(command);
154 }
155 
Clear()156 void CommandList::Clear()
157 {
158     commands_.clear();
159 }
160 
GetCommands() const161 const std::vector<std::shared_ptr<BaseCommand>>& CommandList::GetCommands() const
162 {
163     return commands_;
164 }
165 
ToString() const166 std::string CommandList::ToString() const
167 {
168     std::string dumpStr = "";
169     for (auto& command : commands_) {
170         dumpStr += ("  " + command->ToString());
171     }
172     return dumpStr;
173 }
174 
ConsumeOnce(int64_t currentTime,ConsumeActionInfo & actionInfo)175 bool BaseCommand::ConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo)
176 {
177     // mark the base time line first if not set before this consuming try
178     MarkConsumeBaseTimeLine(currentTime);
179     // check if already consumed out
180     if (IsConsumedOut()) {
181         return false; // running out, can not be consumed any more
182     }
183 
184     if (currentTime >= (consumeBaseTimeLine_ + duration_)) {
185         // give the current command at least one chance to be executed
186         isConsumedOut_ = true;
187     }
188 
189     // call the func implemented by child class
190     bool ret = DoConsumeOnce(currentTime, actionInfo);
191     // increase consumed count
192     MarkConsumedOnce();
193     return ret;
194 }
195 
DoConsumeOnce(int64_t currentTime,ConsumeActionInfo & actionInfo)196 bool WaitCommand::DoConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo)
197 {
198     actionInfo.type = GetCommandType();
199     actionInfo.curve = GetCoordinateCurve();
200     actionInfo.finger = GetFingerId();
201     actionInfo.targetDisplayX = 0;
202     actionInfo.targetDisplayY = 0;
203     actionInfo.consumeIndex = GetConsumedCount();
204     actionInfo.totalCount = GetTotalCount();
205     return true;
206 }
207 
DoConsumeOnce(int64_t currentTime,ConsumeActionInfo & actionInfo)208 bool BaseRawEventCommand::DoConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo)
209 {
210     actionInfo.type = GetCommandType();
211     actionInfo.curve = GetCoordinateCurve();
212     actionInfo.finger = GetFingerId();
213     actionInfo.targetDisplayX = GetTargetDisplayX();
214     actionInfo.targetDisplayY = GetTargetDisplayY();
215     actionInfo.consumeIndex = GetConsumedCount();
216     actionInfo.totalCount = GetTotalCount();
217     return true;
218 }
219 
IsAllConsumed() const220 bool CommandList::IsAllConsumed() const
221 {
222     for (const auto& command : commands_) {
223         if (!(command->IsConsumedOut())) {
224             return false;
225         }
226     }
227     return true;
228 }
229 
ConsumeOnce(int64_t currentTime,ConsumeActionInfo & actionInfo)230 bool CommandList::ConsumeOnce(int64_t currentTime, ConsumeActionInfo& actionInfo)
231 {
232     if (currentConsumingIndex_ >= commands_.size()) {
233         return false;
234     }
235 
236     auto& command = commands_[currentConsumingIndex_];
237     command->ConsumeOnce(currentTime, actionInfo);
238     if (command->IsConsumedOut()) {
239         currentConsumingIndex_++;
240     }
241 
242     return true;
243 }
244 
245 } // namespace Ace
246 } // namespace OHOS
247