• 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 "injecting_executor.h"
17 
18 #include <thread>
19 
20 #include "input_manager.h"
21 
22 namespace OHOS {
23 namespace Ace {
24 
GetPointerActionFromCommandType(CommandType type)25 int32_t InjectingExecutor::GetPointerActionFromCommandType(CommandType type)
26 {
27     switch (type) {
28         case CommandType::TOUCH_DOWN:
29             return MMI::PointerEvent::POINTER_ACTION_DOWN;
30         case CommandType::TOUCH_MOVE:
31             return MMI::PointerEvent::POINTER_ACTION_MOVE;
32         case CommandType::TOUCH_UP:
33             return MMI::PointerEvent::POINTER_ACTION_UP;
34         case CommandType::TOUCH_CANCEL:
35             return MMI::PointerEvent::POINTER_ACTION_CANCEL;
36         default:
37             return MMI::PointerEvent::POINTER_ACTION_MOVE;
38     }
39 }
40 
GetCommandTypeFromPointerAction(int32_t pointerActionType)41 CommandType InjectingExecutor::GetCommandTypeFromPointerAction(int32_t pointerActionType)
42 {
43     switch (pointerActionType) {
44         case MMI::PointerEvent::POINTER_ACTION_DOWN:
45             return CommandType::TOUCH_DOWN;
46         case MMI::PointerEvent::POINTER_ACTION_MOVE:
47             return CommandType::TOUCH_MOVE;
48         case MMI::PointerEvent::POINTER_ACTION_UP:
49             return CommandType::TOUCH_UP;
50         case MMI::PointerEvent::POINTER_ACTION_CANCEL:
51             return CommandType::TOUCH_CANCEL;
52         default:
53             return CommandType::UNKNOWN;
54     }
55 
56     return CommandType::UNKNOWN;
57 }
58 
GetPointerActionName(int32_t pointerActionType)59 std::string InjectingExecutor::GetPointerActionName(int32_t pointerActionType)
60 {
61     switch (pointerActionType) {
62         case MMI::PointerEvent::POINTER_ACTION_DOWN:
63             return "down";
64         case MMI::PointerEvent::POINTER_ACTION_MOVE:
65             return "move";
66         case MMI::PointerEvent::POINTER_ACTION_UP:
67             return "up";
68         case MMI::PointerEvent::POINTER_ACTION_CANCEL:
69             return "cancel";
70         default:
71             return "unkown";
72     }
73 
74     return "unkown";
75 }
76 
IsEventNeedTriggerImmediatly(int32_t pointerActionType)77 bool InjectingExecutor::IsEventNeedTriggerImmediatly(int32_t pointerActionType)
78 {
79     if (pointerActionType == MMI::PointerEvent::POINTER_ACTION_DOWN ||
80         pointerActionType == MMI::PointerEvent::POINTER_ACTION_UP ||
81         pointerActionType == MMI::PointerEvent::POINTER_ACTION_CANCEL) {
82         return true;
83     }
84     return false;
85 }
86 
InjectOnePonterEvent(std::vector<InjectingInfo> & activingInjecting,std::vector<InjectingInfo> & allOtherInjectings)87 bool InjectingExecutor::InjectOnePonterEvent(
88     std::vector<InjectingInfo>& activingInjecting, std::vector<InjectingInfo>& allOtherInjectings)
89 {
90     // must have one activing action at least
91     if (activingInjecting.empty()) {
92         return false;
93     }
94 
95     bool ret = false;
96     for (auto& activingPointer : activingInjecting) {
97         if (ret) {
98             // give a little break 1ms between two actions
99             std::cout << "give a little break between two continus injecting" << std::endl;
100             std::this_thread::sleep_for(std::chrono::milliseconds(1));
101         }
102         // 1. prepare the pointer event
103         auto pointerEvent = MMI::PointerEvent::Create();
104         if (!pointerEvent) {
105             continue;
106         }
107         pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
108         pointerEvent->SetPointerAction(activingPointer.actionType);
109         pointerEvent->SetPointerId(activingPointer.finger);
110         // 2. add the activing pointer self as the first item
111         MMI::PointerEvent::PointerItem activingItem;
112         activingItem.SetDisplayX(activingPointer.x);
113         activingItem.SetDisplayY(activingPointer.y);
114         activingItem.SetPointerId(activingPointer.finger);
115         activingItem.SetPressed(true);
116         activingItem.SetOriginPointerId(activingPointer.finger);
117         pointerEvent->AddPointerItem(activingItem);
118         pointerEvent->SetActionTime(activingPointer.actionTime);
119         pointerEvent->SetSensorInputTime(activingPointer.actionTime - 2000);
120         // 3. pack all other pointers into items
121         for (auto& otherPointer : allOtherInjectings) {
122             MMI::PointerEvent::PointerItem item;
123             item.SetDisplayX(otherPointer.x);
124             item.SetDisplayY(otherPointer.y);
125             item.SetPointerId(otherPointer.finger);
126             item.SetPressed(true);
127             item.SetOriginPointerId(otherPointer.finger);
128             pointerEvent->AddPointerItem(item);
129         }
130         // 4. inject
131         MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
132         ret = true;
133     }
134     return ret;
135 }
136 
137 } // namespace Ace
138 } // namespace OHOS
139