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 "bridge/cj_frontend/interfaces/cj_ffi/cj_common_ffi.h"
17 
18 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
19 #include "bridge/cj_frontend/runtime/cj_runtime_delegate.h"
20 #include "core/common/container.h"
21 
22 using namespace OHOS::Ace;
23 using namespace OHOS::Ace::Framework;
24 
25 extern "C" {
FfiOHOSAceFrameworkRegisterCJFuncs(AtCPackage cjFuncs)26 void FfiOHOSAceFrameworkRegisterCJFuncs(AtCPackage cjFuncs)
27 {
28     CJRuntimeDelegate::GetInstance()->RegisterCJFuncs(cjFuncs);
29 }
30 
FfiGeneralSizeOfPointer()31 int64_t FfiGeneralSizeOfPointer()
32 {
33     return sizeof(void*);
34 }
35 }
36 
37 namespace OHOS::Ace {
TransformNativeTouchLocationInfo(CJTouchInfo * sources,const std::list<OHOS::Ace::TouchLocationInfo> & touchLocationInfoList)38 void TransformNativeTouchLocationInfo(
39     CJTouchInfo* sources, const std::list<OHOS::Ace::TouchLocationInfo>& touchLocationInfoList)
40 {
41     int id = 0;
42     for (const OHOS::Ace::TouchLocationInfo& touchLocationInfo : touchLocationInfoList) {
43         auto& ffiTouches = sources[id];
44         ffiTouches.type = static_cast<uint8_t>(touchLocationInfo.GetTouchType());
45         ffiTouches.fingerId = touchLocationInfo.GetFingerId();
46         auto& globalLoc = touchLocationInfo.GetGlobalLocation();
47         ffiTouches.globalX = PipelineBase::Px2VpWithCurrentDensity(globalLoc.GetX());
48         ffiTouches.globalY = PipelineBase::Px2VpWithCurrentDensity(globalLoc.GetY());
49         auto& localLoc = touchLocationInfo.GetLocalLocation();
50         ffiTouches.localX = PipelineBase::Px2VpWithCurrentDensity(localLoc.GetX());
51         ffiTouches.localY = PipelineBase::Px2VpWithCurrentDensity(localLoc.GetY());
52         id++;
53     }
54 }
55 
TransformNativeCJFingerInfo(CJFingerInfo * sources,const std::list<OHOS::Ace::FingerInfo> & fingerInfoList)56 void TransformNativeCJFingerInfo(CJFingerInfo* sources, const std::list<OHOS::Ace::FingerInfo>& fingerInfoList)
57 {
58     int id = 0;
59     for (const OHOS::Ace::FingerInfo& fingerInfo : fingerInfoList) {
60         auto& ffiFingers = sources[id];
61         ffiFingers.id = fingerInfo.fingerId_;
62         auto& globalLoc = fingerInfo.globalLocation_;
63         ffiFingers.globalX = PipelineBase::Px2VpWithCurrentDensity(globalLoc.GetX());
64         ffiFingers.globalY = PipelineBase::Px2VpWithCurrentDensity(globalLoc.GetY());
65         auto& localLoc = fingerInfo.localLocation_;
66         ffiFingers.localX = PipelineBase::Px2VpWithCurrentDensity(localLoc.GetX());
67         ffiFingers.localY = PipelineBase::Px2VpWithCurrentDensity(localLoc.GetY());
68         id++;
69     }
70 }
71 
AssambleCJEventTarget(const OHOS::Ace::EventTarget & eventTarget,CJArea & area,CJPosition & position,CJPosition & globalPosition)72 void AssambleCJEventTarget(
73     const OHOS::Ace::EventTarget& eventTarget, CJArea& area, CJPosition& position, CJPosition& globalPosition)
74 {
75     area.width = eventTarget.area.GetWidth().ConvertToVp();
76     area.height = eventTarget.area.GetHeight().ConvertToVp();
77     const auto& localOffset = eventTarget.area.GetOffset();
78     const auto& origin = eventTarget.origin;
79     position.x = localOffset.GetX().ConvertToVp();
80     position.y = localOffset.GetY().ConvertToVp();
81     globalPosition.x = origin.GetX().ConvertToVp() + localOffset.GetX().ConvertToVp();
82     globalPosition.y = origin.GetX().ConvertToVp() + localOffset.GetY().ConvertToVp();
83 }
84 
AssambleCJClickInfo(const OHOS::Ace::GestureEvent & event,CJClickInfo & clickInfo,CJEventTarget & eventTarget,CJArea & area,CJPosition & position,CJPosition & globalPosition)85 void AssambleCJClickInfo(const OHOS::Ace::GestureEvent& event, CJClickInfo& clickInfo, CJEventTarget& eventTarget,
86     CJArea& area, CJPosition& position, CJPosition& globalPosition)
87 {
88     AssambleCJEventTarget(event.GetTarget(), area, position, globalPosition);
89     area.position = &position;
90     area.globalPosition = &globalPosition;
91     eventTarget.area = &area;
92     clickInfo.target = &eventTarget;
93     clickInfo.timestamp = event.GetTimeStamp().time_since_epoch().count();
94     Offset globalOffset = event.GetGlobalLocation();
95     Offset localOffset = event.GetLocalLocation();
96     Offset screenOffset = event.GetScreenLocation();
97     double currtDensity = PipelineBase::GetCurrentDensity();
98     clickInfo.x = localOffset.GetX() / currtDensity;
99     clickInfo.y = localOffset.GetY() / currtDensity;
100     clickInfo.windowX = globalOffset.GetX() / currtDensity;
101     clickInfo.windowY = globalOffset.GetY() / currtDensity;
102     clickInfo.displayX = screenOffset.GetX() / currtDensity;
103     clickInfo.displayY = screenOffset.GetY() / currtDensity;
104     clickInfo.source = static_cast<int32_t>(event.GetSourceDevice());
105 }
106 } // namespace OHOS::Ace
107