• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "multimode_manager.h"
16 
17 namespace OHOS {
18 namespace WuKong {
19 namespace {
20 const int SHORTEST_LEN = 2;
21 const int LONGEST_LEN = 20;
22 const int DEFAULT_PRESSURE = 5;
23 const int SLEEP_TIME = 16000;
24 }  // namespace
MultimodeManager()25 MultimodeManager::MultimodeManager()
26 {
27     for (int i = OHOS::MMI::KeyEvent::KEYCODE_CALL; i <= OHOS::MMI::KeyEvent::KEYCODE_ENDCALL; i++) {
28         keycodelist_.push_back(i);
29     }
30 
31     for (int j = OHOS::MMI::KeyEvent::KEYCODE_0; j <= OHOS::MMI::KeyEvent::KEYCODE_NUMPAD_RIGHT_PAREN; j++) {
32         keycodelist_.push_back(j);
33     }
34 }
35 
~MultimodeManager()36 MultimodeManager::~MultimodeManager()
37 {
38 }
39 
SingleKeyCodeInput(int keycode,int downtime)40 ErrCode MultimodeManager::SingleKeyCodeInput(int keycode, int downtime)
41 {
42     ErrCode result = ERR_OK;
43     std::string keycodeType = OHOS::MMI::KeyEvent::KeyCodeToString(keycode);
44     INFO_LOG_STR("keycodeType: %s", keycodeType.c_str());
45     auto keyKeyboardEvent = OHOS::MMI::KeyEvent::Create();
46     MMI::KeyEvent::KeyItem item;
47     item.SetKeyCode(keycode);
48     item.SetPressed(true);
49     item.SetDownTime(downtime);
50     keyKeyboardEvent->SetKeyCode(keycode);
51     keyKeyboardEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
52     keyKeyboardEvent->AddPressedKeyItems(item);
53     // check if KeyEvent is valid
54     if (keyKeyboardEvent->IsValid()) {
55         MMI::InputManager::GetInstance()->SimulateInputEvent(keyKeyboardEvent);
56     } else {
57         WARN_LOG("keyevent down is invalid");
58         return OHOS::ERR_NO_INIT;
59     }
60     keyKeyboardEvent->RemoveReleasedKeyItems(item);
61     item.SetKeyCode(keycode);
62     item.SetPressed(false);
63     item.SetDownTime(downtime);
64     keyKeyboardEvent->SetKeyCode(keycode);
65     keyKeyboardEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
66     keyKeyboardEvent->AddPressedKeyItems(item);
67     // check if KeyEvent is valid
68     if (keyKeyboardEvent->IsValid()) {
69         MMI::InputManager::GetInstance()->SimulateInputEvent(keyKeyboardEvent);
70     } else {
71         WARN_LOG("keyevent up is invalid");
72         return OHOS::ERR_NO_INIT;
73     }
74     keyKeyboardEvent->RemoveReleasedKeyItems(item);
75 
76     return result;
77 }
78 
MultiKeyCodeInput(int downtime)79 ErrCode MultimodeManager::MultiKeyCodeInput(int downtime)
80 {
81     ErrCode result = OHOS::ERR_OK;
82     // generate the length of string randomly
83     int stringLen = SHORTEST_LEN + random() % (LONGEST_LEN - 1);
84     if (keycodelist_.size() > 0) {
85         for (int i = 0; i < stringLen; i++) {
86             int keycode = keycodelist_[(uint32_t)(rand()) % keycodelist_.size()];
87             result = MultimodeManager::GetInstance()->SingleKeyCodeInput(keycode, downtime);
88         }
89     } else {
90         return OHOS::ERR_NO_INIT;
91     }
92     return result;
93 }
94 
GetKeycodeList(std::vector<int> & keycodelist)95 void MultimodeManager::GetKeycodeList(std::vector<int> &keycodelist)
96 {
97     keycodelist = keycodelist_;
98 }
99 
PointerInput(int x,int y,int pointertype,int actiontype)100 ErrCode MultimodeManager::PointerInput(int x, int y, int pointertype, int actiontype)
101 {
102     ErrCode result = OHOS::ERR_OK;
103     auto pointerEvent = MMI::PointerEvent::Create();
104     MMI::PointerEvent::PointerItem item;
105 
106     item.SetPointerId(0);
107     item.SetDisplayX(x);
108     item.SetDisplayY(y);
109     item.SetPressure(DEFAULT_PRESSURE);
110     pointerEvent->AddPointerItem(item);
111 
112     pointerEvent->SetPointerAction(actiontype);
113     pointerEvent->SetSourceType(pointertype);
114     pointerEvent->SetPointerId(0);
115 
116     MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
117 
118     return result;
119 }
120 
IntervalSwap(int xSrcPosition,int ySrcPosition,int xDstPosition,int yDstPosition)121 ErrCode MultimodeManager::IntervalSwap(int xSrcPosition, int ySrcPosition, int xDstPosition, int yDstPosition)
122 {
123     auto multiinput = MultimodeManager::GetInstance();
124     ErrCode result = multiinput->PointerInput(xSrcPosition, ySrcPosition, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN,
125                                               MMI::PointerEvent::POINTER_ACTION_DOWN);
126     if (result != OHOS::ERR_OK) {
127         return result;
128     }
129     usleep(SLEEP_TIME);
130     int segment = 50;
131     float secX = (xDstPosition - xSrcPosition) / (float)segment;
132     float secY = (yDstPosition - ySrcPosition) / (float)segment;
133 
134     for (int i = 1; i < segment; ++i) {
135         int mPosX = int(xSrcPosition + secX * i);
136         int mPosY = int(ySrcPosition + secY * i);
137         result = multiinput->PointerInput(mPosX, mPosY, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN,
138                                           MMI::PointerEvent::POINTER_ACTION_MOVE);
139         if (result != OHOS::ERR_OK) {
140             return result;
141         }
142         usleep(SLEEP_TIME);
143     }
144     result = multiinput->PointerInput(xDstPosition, yDstPosition, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN,
145                                       MMI::PointerEvent::POINTER_ACTION_UP);
146     return result;
147 }
148 }  // namespace WuKong
149 }  // namespace OHOS
150