1 /*
2  * Copyright (c) 2024 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 "gtx_input_event_sender.h"
17 
18 namespace OHOS {
19 
GetInstance()20 GtxInputEventSender& GtxInputEventSender::GetInstance()
21 {
22     static GtxInputEventSender instance;
23     return instance;
24 }
25 
GetTouchEvent(GtxTouchEventInfo & touchEvent)26 void GtxInputEventSender::GetTouchEvent(GtxTouchEventInfo& touchEvent)
27 {
28     std::unique_lock<std::mutex> lock(mEventMutex);
29     touchEvent = mEvent;
30 }
31 
SetTouchEvent(Rosen::Rect rect,std::shared_ptr<MMI::PointerEvent> pointerEvent)32 void GtxInputEventSender::SetTouchEvent(Rosen::Rect rect,
33     std::shared_ptr<MMI::PointerEvent> pointerEvent)
34 {
35     if (!mIsEnable.load()) {
36         return;
37     }
38     if (pointerEvent == nullptr) {
39         return;
40     }
41 
42     std::unique_lock<std::mutex> lock(mEventMutex);
43     mEvent = {};
44     mEvent.windowId = static_cast<uint32_t>(pointerEvent->GetAgentWindowId());
45     mEvent.pointerId = pointerEvent->GetPointerId();
46     mEvent.extent = { rect.posX_, rect.posY_, rect.width_, rect.height_ };
47 
48     std::vector<int32_t> pointIndex = pointerEvent->GetPointerIds();
49     mEvent.numPoints = std::min(pointIndex.size(), static_cast<size_t>(GTX_MAX_TOUCH_POINTS_NUMBER));
50 
51     for (uint32_t i = 0; i < mEvent.numPoints; i++) {
52         GtxTouchPoint& dstTouchPoint = mEvent.touchPoints[i];
53         MMI::PointerEvent::PointerItem srcTouchPoint;
54         pointerEvent->GetPointerItem(pointIndex[i], srcTouchPoint);
55 
56         dstTouchPoint.id = pointIndex[i];
57         dstTouchPoint.screenX = srcTouchPoint.GetDisplayX();
58         dstTouchPoint.screenY = srcTouchPoint.GetDisplayY();
59         dstTouchPoint.x = srcTouchPoint.GetWindowX();
60         dstTouchPoint.y = srcTouchPoint.GetWindowY();
61         dstTouchPoint.isPressed = srcTouchPoint.IsPressed();
62         dstTouchPoint.pressure = srcTouchPoint.GetPressure();
63     }
64 }
65 
66 extern "C" {
SetGtxTouchEventStatus(bool isEnable)67 __attribute__((visibility("default"))) void SetGtxTouchEventStatus(bool isEnable)
68 {
69     OHOS::GtxInputEventSender::GetInstance().SetOpt(isEnable);
70 }
71 
GetGtxTouchEvent(OHOS::GtxTouchEventInfo & touchEvent)72 __attribute__((visibility("default"))) void GetGtxTouchEvent(OHOS::GtxTouchEventInfo& touchEvent)
73 {
74     OHOS::GtxInputEventSender::GetInstance().GetTouchEvent(touchEvent);
75 }
76 }
77 
78 } // namespace OHOS
79