1 /*
2 * Copyright (c) 2023 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 "MouseInputImpl.h"
17
18 #include <thread>
19 #include <vector>
20 #include <chrono>
21 #include <sstream>
22
23 #include "PreviewerEngineLog.h"
24
25 using namespace std;
26
27 using namespace OHOS::MMI;
28
MouseInputImpl()29 MouseInputImpl::MouseInputImpl() noexcept
30 {
31 }
32
ConvertToOsType(int status) const33 TouchType MouseInputImpl::ConvertToOsType(int status) const
34 {
35 if (status < static_cast<int>(TouchType::DOWN) || status > static_cast<int>(TouchType::UNKNOWN)) {
36 return TouchType::UNKNOWN;
37 }
38 return static_cast<TouchType>(status);
39 }
40
ConvertToOsTool(int tools) const41 SourceTool MouseInputImpl::ConvertToOsTool(int tools) const
42 {
43 if (tools < static_cast<int>(SourceTool::UNKNOWN) || tools > static_cast<int>(SourceTool::LENS)) {
44 return SourceTool::UNKNOWN;
45 }
46 return static_cast<SourceTool>(tools);
47 }
48
DispatchOsTouchEvent() const49 void MouseInputImpl::DispatchOsTouchEvent() const
50 {
51 auto pointerEvent = std::make_shared<PointerEvent>();
52 pointerEvent->time = std::chrono::high_resolution_clock::now();
53 pointerEvent->id = 1;
54 pointerEvent->x = mouseXPosition;
55 pointerEvent->y = mouseYPosition;
56 pointerEvent->type = ConvertToOsType(touchAction);
57 pointerEvent->buttonId_ = pointButton;
58 pointerEvent->pointerAction_ = pointAction;
59 pointerEvent->sourceType = sourceType;
60 pointerEvent->sourceTool = ConvertToOsTool(sourceTool);
61 pointerEvent->pressedButtons_ = pressedBtnsVec;
62 std::copy(axisValuesArr.begin(), axisValuesArr.end(), pointerEvent->axisValues_.begin());
63 pointerEvent->size = sizeof (PointerEvent);
64 std::stringstream ss;
65 ss << "[";
66 for (double val : axisValuesArr) {
67 ss << " " << val << " ";
68 }
69 ss << "]" << std::endl;
70 ILOG("MouseInputImpl::DispatchEvent x: %f y:%f type:%d buttonId_:%d pointerAction_:%d sourceType:%d \
71 sourceTool:%d pressedButtonsSize:%d axisValuesArr:%s", pointerEvent->x, pointerEvent->y,
72 pointerEvent->type, pointerEvent->buttonId_, pointerEvent->pointerAction_, pointerEvent->sourceType,
73 pointerEvent->sourceTool, pointerEvent->pressedButtons_.size(), ss.str().c_str());
74 ILOG("current thread: %d", this_thread::get_id());
75 JsAppImpl::GetInstance().DispatchPointerEvent(pointerEvent);
76 }
77
DispatchOsBackEvent() const78 void MouseInputImpl::DispatchOsBackEvent() const
79 {
80 ILOG("DispatchBackPressedEvent run.");
81 ILOG("current thread: %d", this_thread::get_id());
82 JsAppImpl::GetInstance().DispatchBackPressedEvent();
83 }
84
GetInstance()85 MouseInputImpl& MouseInputImpl::GetInstance()
86 {
87 static MouseInputImpl instance;
88 return instance;
89 }
90
SetMouseStatus(int status)91 void MouseInputImpl::SetMouseStatus(int status)
92 {
93 touchAction = status;
94 }
95
SetMousePosition(double xPosition,double yPosition)96 void MouseInputImpl::SetMousePosition(double xPosition, double yPosition)
97 {
98 mouseXPosition = xPosition;
99 mouseYPosition = yPosition;
100 }
101