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 "mouse_input.h"
17#include "multimode_manager.h"
18#include "wukong_define.h"
19#include "report.h"
20
21namespace OHOS {
22namespace WuKong {
23namespace {
24const int ONEHUNDRED = 100;
25const int MOUSE_RIGHT_PERCENT = 20;
26const int MOUSE_MID_PERCENT = 10;
27}  // namespace
28MouseInput::MouseInput() : InputAction()
29{
30    std::shared_ptr<MultimodeInputMsg> multimodeInputMsg = std::make_shared<MultimodeInputMsg>();
31    multimodeInputMsg->inputType_ = INPUTTYPE_MOUSEINPUT;
32    inputedMsgObject_ = multimodeInputMsg;
33}
34
35MouseInput::~MouseInput()
36{
37}
38
39ErrCode MouseInput::RandomInput()
40{
41    int32_t screenWidth = -1;
42    int32_t screenHeight = -1;
43    int mouseType;
44    // get the size of screen
45    ErrCode result = WuKongUtil::GetInstance()->GetScreenSize(screenWidth, screenHeight);
46    if (result != OHOS::ERR_OK) {
47        return result;
48    }
49    // generate random point on the screen
50    int xClickPosition = rand() % screenWidth;
51    int yClickPosition = rand() % screenHeight;
52    // distrbute type accord to percentage
53    int randomInt = rand() % ONEHUNDRED;
54    if (randomInt < MOUSE_MID_PERCENT) {
55        mouseType = MMI::PointerEvent::MOUSE_BUTTON_MIDDLE;
56    } else if (randomInt < (MOUSE_RIGHT_PERCENT + MOUSE_MID_PERCENT)) {
57        mouseType = MMI::PointerEvent::MOUSE_BUTTON_RIGHT;
58    } else {
59        mouseType = MMI::PointerEvent::MOUSE_BUTTON_LEFT;
60    }
61    INFO_LOG_STR("Mouse: (%d, %d) Mouse Type: (%s)", xClickPosition, yClickPosition,
62                 MouseTypeToString(mouseType).c_str());
63    auto multiinput = MultimodeManager::GetInstance();
64    result =
65        multiinput->PointerInput(xClickPosition, yClickPosition, mouseType, MMI::PointerEvent::POINTER_ACTION_DOWN);
66    if (result != OHOS::ERR_OK) {
67        return result;
68    }
69    result = multiinput->PointerInput(xClickPosition, yClickPosition, mouseType, MMI::PointerEvent::POINTER_ACTION_UP);
70    if (result != OHOS::ERR_OK) {
71        return result;
72    }
73    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
74    return result;
75}
76
77InputType MouseInput::GetInputInfo()
78{
79    return INPUTTYPE_MOUSEINPUT;
80}
81
82std::string MouseInput::MouseTypeToString(int mousetype)
83{
84    switch (mousetype) {
85        case MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
86            return "MOUSE_MIDDLE";
87            break;
88        case MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
89            return "MOUSE_RIGHT";
90            break;
91        case MMI::PointerEvent::MOUSE_BUTTON_LEFT:
92            return "MOUSE_LEFT";
93            break;
94        default:
95            return "BUTTON_NONE";
96            break;
97    }
98}
99}  // namespace WuKong
100}  // namespace OHOS
101