• 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 OHOS::MMI;
26 
MouseInputImpl()27 MouseInputImpl::MouseInputImpl() noexcept
28 {
29 }
30 
ConvertToOsType(int status) const31 TouchType MouseInputImpl::ConvertToOsType(int status) const
32 {
33     if (status < static_cast<int>(TouchType::DOWN) || status > static_cast<int>(TouchType::UNKNOWN)) {
34         return TouchType::UNKNOWN;
35     }
36     return static_cast<TouchType>(status);
37 }
38 
ConvertToOsTool(int tools) const39 SourceTool MouseInputImpl::ConvertToOsTool(int tools) const
40 {
41     if (tools < static_cast<int>(SourceTool::UNKNOWN) || tools > static_cast<int>(SourceTool::LENS)) {
42         return SourceTool::UNKNOWN;
43     }
44     return static_cast<SourceTool>(tools);
45 }
46 
DispatchOsTouchEvent() const47 void MouseInputImpl::DispatchOsTouchEvent() const
48 {
49     auto pointerEvent = std::make_shared<PointerEvent>();
50     struct timespec ts;
51     clock_gettime(CLOCK_MONOTONIC, &ts);
52     std::chrono::duration<int64_t, std::nano> duration(ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec);
53     pointerEvent->time = std::chrono::high_resolution_clock::time_point(duration);
54     pointerEvent->id = 1;
55     pointerEvent->x = mouseXPosition;
56     pointerEvent->y = mouseYPosition;
57     pointerEvent->type = ConvertToOsType(touchAction);
58     pointerEvent->buttonId_ = pointButton;
59     pointerEvent->pointerAction_ = pointAction;
60     pointerEvent->sourceType = sourceType;
61     pointerEvent->sourceTool = ConvertToOsTool(sourceTool);
62     pointerEvent->pressedButtons_ = pressedBtnsVec;
63     std::copy(axisValuesArr.begin(), axisValuesArr.end(), pointerEvent->axisValues_.begin());
64     pointerEvent->size = sizeof (PointerEvent);
65     std::stringstream ss;
66     ss << "[";
67     for (double val : axisValuesArr) {
68         ss << " " << val << " ";
69     }
70     ss << "]" << std::endl;
71     ILOG("MouseInputImpl::DispatchEvent x: %f y:%f type:%d buttonId_:%d pointerAction_:%d sourceType:%d \
72         sourceTool:%d pressedButtonsSize:%d axisValuesArr:%s", pointerEvent->x, pointerEvent->y,
73         pointerEvent->type, pointerEvent->buttonId_, pointerEvent->pointerAction_, pointerEvent->sourceType,
74         pointerEvent->sourceTool, pointerEvent->pressedButtons_.size(), ss.str().c_str());
75     ILOG("current thread: %d", std::this_thread::get_id());
76     JsAppImpl::GetInstance().DispatchPointerEvent(pointerEvent);
77 }
78 
DispatchOsBackEvent() const79 void MouseInputImpl::DispatchOsBackEvent() const
80 {
81     ILOG("DispatchBackPressedEvent run.");
82     ILOG("current thread: %d", std::this_thread::get_id());
83     JsAppImpl::GetInstance().DispatchBackPressedEvent();
84 }
85 
GetInstance()86 MouseInputImpl& MouseInputImpl::GetInstance()
87 {
88     static MouseInputImpl instance;
89     return instance;
90 }
91 
SetMouseStatus(int status)92 void MouseInputImpl::SetMouseStatus(int status)
93 {
94     touchAction = status;
95 }
96 
SetMousePosition(double xPosition,double yPosition)97 void MouseInputImpl::SetMousePosition(double xPosition, double yPosition)
98 {
99     mouseXPosition = xPosition;
100     mouseYPosition = yPosition;
101 }
102