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
22 #include "ace_ability.h"
23 #include "event_dispatcher.h"
24
25 #include "PreviewerEngineLog.h"
26
27 using namespace std;
28
29 using namespace OHOS::Ace;
30
MouseInputImpl()31 MouseInputImpl::MouseInputImpl()
32 {
33 }
34
ConvertToOsType(MouseInput::MouseStatus status) const35 TouchType MouseInputImpl::ConvertToOsType(MouseInput::MouseStatus status) const
36 {
37 TouchType type;
38 switch (status) {
39 case INDEV_STATE_RELEASE:
40 type = TouchType::UP;
41 break;
42 case INDEV_STATE_PRESS:
43 type = TouchType::DOWN;
44 break;
45 case INDEV_STATE_MOVE:
46 type = TouchType::MOVE;
47 break;
48 default:
49 break;
50 }
51 return type;
52 }
DispatchOsTouchEvent() const53 void MouseInputImpl::DispatchOsTouchEvent() const
54 {
55 TouchEvent touchEvent;
56 touchEvent.time = std::chrono::high_resolution_clock::now();
57 touchEvent.id = 1;
58 touchEvent.x = mouseXPosition;
59 touchEvent.y = mouseYPosition;
60 touchEvent.type = ConvertToOsType(mouseStatus);
61 touchEvent.size = sizeof (TouchEvent);
62 ILOG("MouseInputImpl::DispatchEvent x: %f y:%f", mouseXPosition, mouseYPosition);
63 ILOG("current thread: %d", this_thread::get_id());
64 if (!Platform::EventDispatcher::GetInstance().DispatchTouchEvent(touchEvent)) {
65 ILOG("MouseInputImpl::DispatchEvent failed, x: %f y:%f", mouseXPosition, mouseYPosition);
66 }
67 }
68
DispatchOsBackEvent() const69 void MouseInputImpl::DispatchOsBackEvent() const
70 {
71 ILOG("DispatchBackPressedEvent run.");
72 ILOG("current thread: %d", this_thread::get_id());
73 if (!Platform::EventDispatcher::GetInstance().DispatchBackPressedEvent()) {
74 ELOG("DispatchBackPressedEvent failed.");
75 }
76 }
77
GetInstance()78 MouseInputImpl& MouseInputImpl::GetInstance()
79 {
80 static MouseInputImpl instance;
81 return instance;
82 }
83
SetMouseStatus(MouseStatus status)84 void MouseInputImpl::SetMouseStatus(MouseStatus status)
85 {
86 mouseStatus = status;
87 }
88
SetMousePosition(double xPosition,double yPosition)89 void MouseInputImpl::SetMousePosition(double xPosition, double yPosition)
90 {
91 mouseXPosition = xPosition;
92 mouseYPosition = yPosition;
93 }
94