• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     struct timespec ts;
53     clock_gettime(CLOCK_MONOTONIC, &ts);
54     std::chrono::duration<int64_t, std::nano> duration(ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec);
55     pointerEvent->time = std::chrono::high_resolution_clock::time_point(duration);
56     pointerEvent->id = 1;
57     pointerEvent->x = mouseXPosition;
58     pointerEvent->y = mouseYPosition;
59     pointerEvent->type = ConvertToOsType(touchAction);
60     pointerEvent->buttonId_ = pointButton;
61     pointerEvent->pointerAction_ = pointAction;
62     pointerEvent->sourceType = sourceType;
63     pointerEvent->sourceTool = ConvertToOsTool(sourceTool);
64     pointerEvent->pressedButtons_ = pressedBtnsVec;
65     std::copy(axisValuesArr.begin(), axisValuesArr.end(), pointerEvent->axisValues_.begin());
66     pointerEvent->size = sizeof (PointerEvent);
67     std::stringstream ss;
68     ss << "[";
69     for (double val : axisValuesArr) {
70         ss << " " << val << " ";
71     }
72     ss << "]" << std::endl;
73     ILOG("MouseInputImpl::DispatchEvent x: %f y:%f type:%d buttonId_:%d pointerAction_:%d sourceType:%d \
74         sourceTool:%d pressedButtonsSize:%d axisValuesArr:%s", pointerEvent->x, pointerEvent->y,
75         pointerEvent->type, pointerEvent->buttonId_, pointerEvent->pointerAction_, pointerEvent->sourceType,
76         pointerEvent->sourceTool, pointerEvent->pressedButtons_.size(), ss.str().c_str());
77     ILOG("current thread: %d", this_thread::get_id());
78     JsAppImpl::GetInstance().DispatchPointerEvent(pointerEvent);
79 }
80 
DispatchOsBackEvent() const81 void MouseInputImpl::DispatchOsBackEvent() const
82 {
83     ILOG("DispatchBackPressedEvent run.");
84     ILOG("current thread: %d", this_thread::get_id());
85     JsAppImpl::GetInstance().DispatchBackPressedEvent();
86 }
87 
GetInstance()88 MouseInputImpl& MouseInputImpl::GetInstance()
89 {
90     static MouseInputImpl instance;
91     return instance;
92 }
93 
SetMouseStatus(int status)94 void MouseInputImpl::SetMouseStatus(int status)
95 {
96     touchAction = status;
97 }
98 
SetMousePosition(double xPosition,double yPosition)99 void MouseInputImpl::SetMousePosition(double xPosition, double yPosition)
100 {
101     mouseXPosition = xPosition;
102     mouseYPosition = yPosition;
103 }
104