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 <functional> 20 #include <string> 21 #include <unistd.h> 22 #include <vector> 23 24 #include "interfaces/native/native_interface_xcomponent.h" 25 26 #include "base/memory/ace_type.h" 27 #include "base/utils/utils.h" 28 29 struct XComponentTouchPoint { 30 float tiltX = 0.0f; 31 float tiltY = 0.0f; 32 OH_NativeXComponent_TouchPointToolType sourceToolType = 33 OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 34 }; 35 36 struct OH_NativeXComponent_KeyEvent { 37 OH_NativeXComponent_KeyAction action = OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN; 38 OH_NativeXComponent_KeyCode code = OH_NativeXComponent_KeyCode::KEY_UNKNOWN; 39 OH_NativeXComponent_EventSourceType sourceType = 40 OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN; 41 int64_t deviceId {}; 42 int64_t timestamp {}; 43 }; 44 45 namespace OHOS::Ace { 46 class NativeXComponentImpl : public virtual AceType { 47 DECLARE_ACE_TYPE(NativeXComponentImpl, AceType); 48 using NativeXComponent_Callback = void (*)(OH_NativeXComponent*, void*); 49 using SetExpectedRateRangeEvent_Callback = std::function<void()>; 50 using SetOnFrameEvent_Callback = std::function<void()>; 51 using SetUnregisterOnFrameEvent_Callback = std::function<void()>; 52 using OnFrame_Callback = void (*)(OH_NativeXComponent*, uint64_t, uint64_t); 53 using NativeNode_Callback = void (*)(void*, void*); 54 55 public: NativeXComponentImpl()56 NativeXComponentImpl() {} 57 ~NativeXComponentImpl()58 ~NativeXComponentImpl() {} 59 SetXComponentId(const std::string & id)60 void SetXComponentId(const std::string& id) 61 { 62 xcomponentId_ = id; 63 } 64 GetXComponentId()65 const std::string& GetXComponentId() const 66 { 67 return xcomponentId_; 68 } 69 SetXComponentWidth(const int width)70 void SetXComponentWidth(const int width) 71 { 72 width_ = width; 73 } 74 GetXComponentWidth()75 int GetXComponentWidth() const 76 { 77 return width_; 78 } 79 SetXComponentHeight(const int height)80 void SetXComponentHeight(const int height) 81 { 82 height_ = height; 83 } 84 GetXComponentHeight()85 int GetXComponentHeight() const 86 { 87 return height_; 88 } 89 SetXComponentOffsetX(const double x)90 void SetXComponentOffsetX(const double x) 91 { 92 x_ = x; 93 } 94 GetXComponentOffsetX()95 double GetXComponentOffsetX() const 96 { 97 return x_; 98 } 99 SetXComponentOffsetY(const double y)100 void SetXComponentOffsetY(const double y) 101 { 102 y_ = y; 103 } 104 GetXComponentOffsetY()105 double GetXComponentOffsetY() const 106 { 107 return y_; 108 } 109 SetSurface(void * window)110 void SetSurface(void* window) 111 { 112 window_ = window; 113 } 114 GetSurface()115 const void* GetSurface() const 116 { 117 return window_; 118 } 119 SetCallback(OH_NativeXComponent_Callback * callback)120 void SetCallback(OH_NativeXComponent_Callback* callback) 121 { 122 callback_ = callback; 123 } 124 GetCallback()125 const OH_NativeXComponent_Callback* GetCallback() const 126 { 127 return callback_; 128 } 129 SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)130 void SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback) 131 { 132 mouseEventCallback_ = callback; 133 } 134 GetMouseEventCallback()135 const OH_NativeXComponent_MouseEvent_Callback* GetMouseEventCallback() 136 { 137 return mouseEventCallback_; 138 } 139 SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent)140 void SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent) 141 { 142 touchEvent_ = touchEvent; 143 } 144 SetTouchPoint(const std::vector<XComponentTouchPoint> & xComponentTouchPoints)145 void SetTouchPoint(const std::vector<XComponentTouchPoint>& xComponentTouchPoints) 146 { 147 touchPoints_ = xComponentTouchPoints; 148 } 149 SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint> & historicalPoints)150 void SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint>& historicalPoints) 151 { 152 historicalPoints_ = historicalPoints; 153 } 154 SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent)155 void SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent) 156 { 157 keyEvent_ = keyEvent; 158 } 159 SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent)160 void SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent) 161 { 162 mouseEvent_ = mouseEvent; 163 } 164 GetTouchEvent()165 const OH_NativeXComponent_TouchEvent GetTouchEvent() const 166 { 167 return touchEvent_; 168 } 169 GetHistoryPoints(int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)170 void GetHistoryPoints(int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints) 171 { 172 int32_t historicalPointsSize = (int32_t)(historicalPoints_.size()); 173 *size = historicalPointsSize; 174 *historicalPoints = &(historicalPoints_[0]); 175 } 176 GetHistoryPointsSize(int32_t * size)177 void GetHistoryPointsSize(int32_t* size) const 178 { 179 *size = (int32_t)(historicalPoints_.size()); 180 } 181 GetMouseEvent()182 const OH_NativeXComponent_MouseEvent GetMouseEvent() const 183 { 184 return mouseEvent_; 185 } 186 SetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType toolType)187 void SetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType toolType) 188 { 189 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 190 return; 191 } 192 touchPoints_[pointIndex].sourceToolType = toolType; 193 } 194 GetToolType(size_t pointIndex)195 OH_NativeXComponent_TouchPointToolType GetToolType(size_t pointIndex) const 196 { 197 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 198 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 199 } 200 return touchPoints_[pointIndex].sourceToolType; 201 } 202 GetTiltX(size_t pointIndex)203 float GetTiltX(size_t pointIndex) const 204 { 205 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 206 return 0.0f; 207 } 208 return touchPoints_[pointIndex].tiltX; 209 } 210 GetTiltY(size_t pointIndex)211 float GetTiltY(size_t pointIndex) const 212 { 213 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 214 return 0.0f; 215 } 216 return touchPoints_[pointIndex].tiltY; 217 } 218 GetKeyEvent()219 OH_NativeXComponent_KeyEvent* GetKeyEvent() 220 { 221 return &keyEvent_; 222 } 223 GetFocusEventCallback()224 NativeXComponent_Callback GetFocusEventCallback() const 225 { 226 return focusEventCallback_; 227 } 228 GetKeyEventCallback()229 NativeXComponent_Callback GetKeyEventCallback() const 230 { 231 return keyEventCallback_; 232 } 233 GetBlurEventCallback()234 NativeXComponent_Callback GetBlurEventCallback() const 235 { 236 return blurEventCallback_; 237 } 238 SetFocusEventCallback(NativeXComponent_Callback callback)239 void SetFocusEventCallback(NativeXComponent_Callback callback) 240 { 241 focusEventCallback_ = callback; 242 } 243 SetKeyEventCallback(NativeXComponent_Callback callback)244 void SetKeyEventCallback(NativeXComponent_Callback callback) 245 { 246 keyEventCallback_ = callback; 247 } 248 SetBlurEventCallback(NativeXComponent_Callback callback)249 void SetBlurEventCallback(NativeXComponent_Callback callback) 250 { 251 blurEventCallback_ = callback; 252 } 253 SetOnFrameCallback(OnFrame_Callback callback)254 void SetOnFrameCallback(OnFrame_Callback callback) 255 { 256 onFrameCallback_ = callback; 257 } 258 GetOnFrameCallback()259 OnFrame_Callback GetOnFrameCallback() 260 { 261 return onFrameCallback_; 262 } 263 GetRateRange()264 OH_NativeXComponent_ExpectedRateRange* GetRateRange() 265 { 266 return rateRange_; 267 } 268 SetRateRange(OH_NativeXComponent_ExpectedRateRange * rateRange)269 void SetRateRange(OH_NativeXComponent_ExpectedRateRange* rateRange) 270 { 271 rateRange_ = rateRange; 272 } 273 SetExpectedRateRangeEventCallback(SetExpectedRateRangeEvent_Callback callback)274 void SetExpectedRateRangeEventCallback(SetExpectedRateRangeEvent_Callback callback) 275 { 276 setExpectedRateRangeCallback_ = callback; 277 } 278 SetOnFrameEventCallback(SetOnFrameEvent_Callback callback)279 void SetOnFrameEventCallback(SetOnFrameEvent_Callback callback) 280 { 281 setOnFrameEventCallback_ = callback; 282 } 283 SetUnregisterOnFrameEventCallback(SetUnregisterOnFrameEvent_Callback callback)284 void SetUnregisterOnFrameEventCallback(SetUnregisterOnFrameEvent_Callback callback) 285 { 286 setUnregisterOnFrameEventCallback_ = callback; 287 } 288 289 SetExpectedRateRangeEvent_Callback setExpectedRateRangeCallback_; 290 291 SetOnFrameEvent_Callback setOnFrameEventCallback_; 292 293 SetUnregisterOnFrameEvent_Callback setUnregisterOnFrameEventCallback_; 294 registerNativeNodeCallbacks(NativeNode_Callback attach,NativeNode_Callback detach)295 void registerNativeNodeCallbacks(NativeNode_Callback attach, NativeNode_Callback detach) 296 { 297 attachNativeNodeCallback_ = attach; 298 detachNativeNodeCallback_ = detach; 299 } 300 registerContaner(void * container)301 void registerContaner(void* container) 302 { 303 container_ = container; 304 } 305 AttachContainer(void * root)306 void AttachContainer(void* root) 307 { 308 CHECK_NULL_VOID(attachNativeNodeCallback_); 309 attachNativeNodeCallback_(container_, root); 310 } 311 DetachContainer(void * root)312 void DetachContainer(void* root) 313 { 314 CHECK_NULL_VOID(detachNativeNodeCallback_); 315 detachNativeNodeCallback_(container_, root); 316 } 317 318 private: 319 std::string xcomponentId_; 320 void* window_ = nullptr; 321 int width_ = 0; 322 int height_ = 0; 323 double x_ = 0.0; 324 double y_ = 0.0; 325 OH_NativeXComponent_TouchEvent touchEvent_; 326 OH_NativeXComponent_MouseEvent mouseEvent_; 327 OH_NativeXComponent_KeyEvent keyEvent_; 328 OH_NativeXComponent_Callback* callback_ = nullptr; 329 OH_NativeXComponent_MouseEvent_Callback* mouseEventCallback_ = nullptr; 330 NativeXComponent_Callback focusEventCallback_ = nullptr; 331 NativeXComponent_Callback keyEventCallback_ = nullptr; 332 NativeXComponent_Callback blurEventCallback_ = nullptr; 333 std::vector<XComponentTouchPoint> touchPoints_; 334 std::vector<OH_NativeXComponent_HistoricalPoint> historicalPoints_; 335 OnFrame_Callback onFrameCallback_ = nullptr; 336 OH_NativeXComponent_ExpectedRateRange* rateRange_ = nullptr; 337 NativeNode_Callback attachNativeNodeCallback_ = nullptr; 338 NativeNode_Callback detachNativeNodeCallback_ = nullptr; 339 void* container_; 340 }; 341 } // namespace OHOS::Ace 342 343 struct OH_NativeXComponent { OH_NativeXComponentOH_NativeXComponent344 explicit OH_NativeXComponent(OHOS::Ace::NativeXComponentImpl* xComponentImpl) : xcomponentImpl_(xComponentImpl) {} ~OH_NativeXComponentOH_NativeXComponent345 ~OH_NativeXComponent() {} 346 int32_t GetXComponentId(char* id, uint64_t* size); 347 int32_t GetNativeWindow(void** window); 348 int32_t GetXComponentSize(const void* window, uint64_t* width, uint64_t* height); 349 int32_t GetXComponentOffset(const void* window, double* x, double* y); 350 int32_t GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent); 351 int32_t GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent); 352 int32_t GetHistoryPoints(const void* window, int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints); 353 int32_t RegisterCallback(OH_NativeXComponent_Callback* callback); 354 int32_t RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback); 355 int32_t GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType); 356 int32_t GetTiltX(size_t pointIndex, float* tiltX); 357 int32_t GetTiltY(size_t pointIndex, float* tiltY); 358 int32_t RegisterFocusEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 359 int32_t RegisterKeyEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 360 int32_t RegisterBlurEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 361 int32_t GetKeyEvent(OH_NativeXComponent_KeyEvent** keyEvent); 362 int32_t SetExpectedFrameRateRange(OH_NativeXComponent_ExpectedRateRange* range); 363 int32_t RegisterOnFrameCallback( 364 void (*callback)(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targetTimestamp)); 365 int32_t UnregisterOnFrameCallback(); 366 int32_t AttachNativeRootNode(void* root); 367 int32_t DetachNativeRootNode(void* root); 368 369 private: 370 OHOS::Ace::NativeXComponentImpl* xcomponentImpl_ = nullptr; 371 }; 372 373 #endif // _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 374