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 16 #include "wukong_event_manager.h" 17 #include <unistd.h> 18 #include "input_manager.h" 19 20 namespace OHOS { 21 namespace AppExecFwk { TouchEvent(int xPosition,int yPosition,int touchTime,int pressure)22 ErrCode WuKongEventManager::TouchEvent(int xPosition, int yPosition, int touchTime, int pressure) 23 { 24 auto pointerEvent = MMI::PointerEvent::Create(); 25 MMI::PointerEvent::PointerItem item; 26 27 item.SetPointerId(0); 28 item.SetGlobalX(xPosition); 29 item.SetGlobalY(yPosition); 30 item.SetPressure(pressure); 31 pointerEvent->AddPointerItem(item); 32 33 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN); 34 pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 35 pointerEvent->SetPointerId(0); 36 37 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent); 38 39 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_UP); 40 pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 41 pointerEvent->SetPointerId(0); 42 43 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent); 44 45 return OHOS::ERR_OK; 46 } 47 MotionEvent(int xSrcPosition,int ySrcPosition,int xDstPosition,int yDstPosition,int pressure)48 ErrCode WuKongEventManager::MotionEvent(int xSrcPosition, int ySrcPosition, 49 int xDstPosition, int yDstPosition, 50 int pressure) 51 { 52 int sleepTime = 16000; 53 54 auto downEvent = MMI::PointerEvent::Create(); 55 MMI::PointerEvent::PointerItem downItem; 56 57 downItem.SetPointerId(0); 58 downItem.SetGlobalX(xSrcPosition); 59 downItem.SetGlobalY(ySrcPosition); 60 downItem.SetPressure(pressure); 61 downEvent->SetPointerId(0); 62 downEvent->AddPointerItem(downItem); 63 downEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN); 64 downEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 65 66 MMI::InputManager::GetInstance()->SimulateInputEvent(downEvent); 67 usleep(sleepTime); 68 69 auto moveEvent = MMI::PointerEvent::Create(); 70 MMI::PointerEvent::PointerItem moveItem; 71 72 int segment = 50; 73 float secX = (xDstPosition - xSrcPosition) / (float) segment; 74 float secY = (yDstPosition - ySrcPosition) / (float) segment; 75 76 for (int i = 1; i < segment; ++i) { 77 moveItem.SetPointerId(0); 78 moveItem.SetGlobalX(int(xSrcPosition + secX * i)); 79 moveItem.SetGlobalY(int(ySrcPosition + secY * i)); 80 moveItem.SetPressure(pressure); 81 moveEvent->SetPointerId(0); 82 moveEvent->AddPointerItem(moveItem); 83 moveEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_MOVE); 84 moveEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 85 86 MMI::InputManager::GetInstance()->SimulateInputEvent(moveEvent); 87 moveEvent->RemovePointerItem(0); 88 usleep(sleepTime); 89 } 90 91 moveItem.SetPointerId(0); 92 moveItem.SetGlobalX(xDstPosition); 93 moveItem.SetGlobalY(yDstPosition); 94 moveItem.SetPressure(pressure); 95 moveEvent->SetPointerId(0); 96 moveEvent->AddPointerItem(moveItem); 97 moveEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_MOVE); 98 moveEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 99 100 MMI::InputManager::GetInstance()->SimulateInputEvent(moveEvent); 101 usleep(sleepTime); 102 103 return OHOS::ERR_OK; 104 } 105 PowerOnAndOffEvent()106 ErrCode WuKongEventManager::PowerOnAndOffEvent() 107 { 108 auto keyPowerEvent = OHOS::MMI::KeyEvent::Create(); 109 110 int32_t downTime = 0; 111 MMI::KeyEvent::KeyItem item; 112 item.SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); 113 item.SetPressed(true); 114 item.SetDownTime(downTime); 115 keyPowerEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); 116 keyPowerEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN); 117 keyPowerEvent->AddPressedKeyItems(item); 118 119 MMI::InputManager::GetInstance()->SimulateInputEvent(keyPowerEvent); 120 121 keyPowerEvent->RemoveReleasedKeyItems(item); 122 item.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_POWER); 123 item.SetPressed(false); 124 item.SetDownTime(downTime); 125 keyPowerEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); 126 keyPowerEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP); 127 keyPowerEvent->RemoveReleasedKeyItems(item); 128 MMI::InputManager::GetInstance()->SimulateInputEvent(keyPowerEvent); 129 keyPowerEvent->RemoveReleasedKeyItems(item); 130 131 return OHOS::ERR_OK; 132 } 133 } 134 } 135