1 /* 2 * Copyright (c) 2021-2022 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 #ifndef _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 17 #define _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 18 19 #include <string> 20 #include <unistd.h> 21 #include <vector> 22 23 #include "interfaces/native/native_interface_xcomponent.h" 24 25 #include "base/memory/ace_type.h" 26 27 struct XComponentTouchPoint { 28 float tiltX = 0.0f; 29 float tiltY = 0.0f; 30 OH_NativeXComponent_TouchPointToolType sourceToolType = 31 OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 32 }; 33 34 namespace OHOS::Ace { 35 class NativeXComponentImpl : public virtual AceType { 36 DECLARE_ACE_TYPE(NativeXComponentImpl, AceType); 37 38 public: NativeXComponentImpl()39 NativeXComponentImpl() {} 40 ~NativeXComponentImpl()41 ~NativeXComponentImpl() {} 42 SetXComponentId(const std::string & id)43 void SetXComponentId(const std::string& id) 44 { 45 xcomponentId_ = id; 46 } 47 GetXComponentId()48 const std::string& GetXComponentId() const 49 { 50 return xcomponentId_; 51 } 52 SetXComponentWidth(const int width)53 void SetXComponentWidth(const int width) 54 { 55 width_ = width; 56 } 57 GetXComponentWidth()58 int GetXComponentWidth() const 59 { 60 return width_; 61 } 62 SetXComponentHeight(const int height)63 void SetXComponentHeight(const int height) 64 { 65 height_ = height; 66 } 67 GetXComponentHeight()68 int GetXComponentHeight() const 69 { 70 return height_; 71 } 72 SetXComponentOffsetX(const double x)73 void SetXComponentOffsetX(const double x) 74 { 75 x_ = x; 76 } 77 GetXComponentOffsetX()78 double GetXComponentOffsetX() const 79 { 80 return x_; 81 } 82 SetXComponentOffsetY(const double y)83 void SetXComponentOffsetY(const double y) 84 { 85 y_ = y; 86 } 87 GetXComponentOffsetY()88 double GetXComponentOffsetY() const 89 { 90 return y_; 91 } 92 SetSurface(void * window)93 void SetSurface(void* window) 94 { 95 window_ = window; 96 } 97 GetSurface()98 const void* GetSurface() const 99 { 100 return window_; 101 } 102 SetCallback(OH_NativeXComponent_Callback * callback)103 void SetCallback(OH_NativeXComponent_Callback* callback) 104 { 105 callback_ = callback; 106 } 107 GetCallback()108 const OH_NativeXComponent_Callback* GetCallback() const 109 { 110 return callback_; 111 } 112 SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)113 void SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback) 114 { 115 mouseEventCallback_ = callback; 116 } 117 GetMouseEventCallback()118 const OH_NativeXComponent_MouseEvent_Callback* GetMouseEventCallback() 119 { 120 return mouseEventCallback_; 121 } 122 SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent)123 void SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent) 124 { 125 touchEvent_ = touchEvent; 126 } 127 SetTouchPoint(const std::vector<XComponentTouchPoint> & xComponentTouchPoints)128 void SetTouchPoint(const std::vector<XComponentTouchPoint>& xComponentTouchPoints) 129 { 130 touchPoints_ = xComponentTouchPoints; 131 } 132 SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent)133 void SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent) 134 { 135 mouseEvent_ = mouseEvent; 136 } 137 GetTouchEvent()138 const OH_NativeXComponent_TouchEvent GetTouchEvent() const 139 { 140 return touchEvent_; 141 } 142 GetMouseEvent()143 const OH_NativeXComponent_MouseEvent GetMouseEvent() const 144 { 145 return mouseEvent_; 146 } 147 SetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType toolType)148 void SetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType toolType) 149 { 150 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 151 return; 152 } 153 touchPoints_[pointIndex].sourceToolType = toolType; 154 } 155 GetToolType(size_t pointIndex)156 OH_NativeXComponent_TouchPointToolType GetToolType(size_t pointIndex) const 157 { 158 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 159 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 160 } 161 return touchPoints_[pointIndex].sourceToolType; 162 } 163 GetTiltX(size_t pointIndex)164 float GetTiltX(size_t pointIndex) const 165 { 166 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 167 return 0.0f; 168 } 169 return touchPoints_[pointIndex].tiltX; 170 } 171 GetTiltY(size_t pointIndex)172 float GetTiltY(size_t pointIndex) const 173 { 174 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 175 return 0.0f; 176 } 177 return touchPoints_[pointIndex].tiltY; 178 } 179 180 private: 181 std::string xcomponentId_; 182 void* window_ = nullptr; 183 int width_ = 0; 184 int height_ = 0; 185 double x_ = 0.0; 186 double y_ = 0.0; 187 OH_NativeXComponent_TouchEvent touchEvent_; 188 OH_NativeXComponent_MouseEvent mouseEvent_; 189 OH_NativeXComponent_Callback* callback_ = nullptr; 190 OH_NativeXComponent_MouseEvent_Callback* mouseEventCallback_ = nullptr; 191 std::vector<XComponentTouchPoint> touchPoints_; 192 }; 193 } // namespace OHOS::Ace 194 195 struct OH_NativeXComponent { OH_NativeXComponentOH_NativeXComponent196 explicit OH_NativeXComponent(OHOS::Ace::NativeXComponentImpl* xComponentImpl) : xcomponentImpl_(xComponentImpl) {} ~OH_NativeXComponentOH_NativeXComponent197 ~OH_NativeXComponent() {} 198 int32_t GetXComponentId(char* id, uint64_t* size); 199 int32_t GetNativeWindow(void** window); 200 int32_t GetXComponentSize(const void* window, uint64_t* width, uint64_t* height); 201 int32_t GetXComponentOffset(const void* window, double* x, double* y); 202 int32_t GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent); 203 int32_t GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent); 204 int32_t RegisterCallback(OH_NativeXComponent_Callback* callback); 205 int32_t RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback); 206 int32_t GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType); 207 int32_t GetTiltX(size_t pointIndex, float* tiltX); 208 int32_t GetTiltY(size_t pointIndex, float* tiltY); 209 210 private: 211 OHOS::Ace::NativeXComponentImpl* xcomponentImpl_ = nullptr; 212 }; 213 214 #endif // _NATIVE_INTERFACE_XCOMPONENT_IMPL_