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 struct OH_NativeXComponent_KeyEvent { 35 OH_NativeXComponent_KeyAction action = OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN; 36 OH_NativeXComponent_KeyCode code = OH_NativeXComponent_KeyCode::KEY_UNKNOWN; 37 OH_NativeXComponent_EventSourceType sourceType = 38 OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN; 39 int64_t deviceId {}; 40 int64_t timestamp {}; 41 }; 42 43 namespace OHOS::Ace { 44 class NativeXComponentImpl : public virtual AceType { 45 DECLARE_ACE_TYPE(NativeXComponentImpl, AceType); 46 using NativeXComponent_Callback = void (*)(OH_NativeXComponent*, void*); 47 48 public: NativeXComponentImpl()49 NativeXComponentImpl() {} 50 ~NativeXComponentImpl()51 ~NativeXComponentImpl() {} 52 SetXComponentId(const std::string & id)53 void SetXComponentId(const std::string& id) 54 { 55 xcomponentId_ = id; 56 } 57 GetXComponentId()58 const std::string& GetXComponentId() const 59 { 60 return xcomponentId_; 61 } 62 SetXComponentWidth(const int width)63 void SetXComponentWidth(const int width) 64 { 65 width_ = width; 66 } 67 GetXComponentWidth()68 int GetXComponentWidth() const 69 { 70 return width_; 71 } 72 SetXComponentHeight(const int height)73 void SetXComponentHeight(const int height) 74 { 75 height_ = height; 76 } 77 GetXComponentHeight()78 int GetXComponentHeight() const 79 { 80 return height_; 81 } 82 SetXComponentOffsetX(const double x)83 void SetXComponentOffsetX(const double x) 84 { 85 x_ = x; 86 } 87 GetXComponentOffsetX()88 double GetXComponentOffsetX() const 89 { 90 return x_; 91 } 92 SetXComponentOffsetY(const double y)93 void SetXComponentOffsetY(const double y) 94 { 95 y_ = y; 96 } 97 GetXComponentOffsetY()98 double GetXComponentOffsetY() const 99 { 100 return y_; 101 } 102 SetSurface(void * window)103 void SetSurface(void* window) 104 { 105 window_ = window; 106 } 107 GetSurface()108 const void* GetSurface() const 109 { 110 return window_; 111 } 112 SetCallback(OH_NativeXComponent_Callback * callback)113 void SetCallback(OH_NativeXComponent_Callback* callback) 114 { 115 callback_ = callback; 116 } 117 GetCallback()118 const OH_NativeXComponent_Callback* GetCallback() const 119 { 120 return callback_; 121 } 122 SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)123 void SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback) 124 { 125 mouseEventCallback_ = callback; 126 } 127 GetMouseEventCallback()128 const OH_NativeXComponent_MouseEvent_Callback* GetMouseEventCallback() 129 { 130 return mouseEventCallback_; 131 } 132 SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent)133 void SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent) 134 { 135 touchEvent_ = touchEvent; 136 } 137 SetTouchPoint(const std::vector<XComponentTouchPoint> & xComponentTouchPoints)138 void SetTouchPoint(const std::vector<XComponentTouchPoint>& xComponentTouchPoints) 139 { 140 touchPoints_ = xComponentTouchPoints; 141 } 142 SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint> & historicalPoints)143 void SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint>& historicalPoints) 144 { 145 historicalPoints_ = historicalPoints; 146 } 147 SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent)148 void SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent) 149 { 150 keyEvent_ = keyEvent; 151 } 152 SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent)153 void SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent) 154 { 155 mouseEvent_ = mouseEvent; 156 } 157 GetTouchEvent()158 const OH_NativeXComponent_TouchEvent GetTouchEvent() const 159 { 160 return touchEvent_; 161 } 162 GetHistoryPoints(int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)163 void GetHistoryPoints(int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints) 164 { 165 int32_t historicalPointsSize = (int32_t)(historicalPoints_.size()); 166 *size = historicalPointsSize; 167 *historicalPoints = &(historicalPoints_[0]); 168 } 169 GetHistoryPointsSize(int32_t * size)170 void GetHistoryPointsSize(int32_t* size) const 171 { 172 *size = (int32_t)(historicalPoints_.size()); 173 } 174 GetMouseEvent()175 const OH_NativeXComponent_MouseEvent GetMouseEvent() const 176 { 177 return mouseEvent_; 178 } 179 SetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType toolType)180 void SetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType toolType) 181 { 182 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 183 return; 184 } 185 touchPoints_[pointIndex].sourceToolType = toolType; 186 } 187 GetToolType(size_t pointIndex)188 OH_NativeXComponent_TouchPointToolType GetToolType(size_t pointIndex) const 189 { 190 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 191 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 192 } 193 return touchPoints_[pointIndex].sourceToolType; 194 } 195 GetTiltX(size_t pointIndex)196 float GetTiltX(size_t pointIndex) const 197 { 198 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 199 return 0.0f; 200 } 201 return touchPoints_[pointIndex].tiltX; 202 } 203 GetTiltY(size_t pointIndex)204 float GetTiltY(size_t pointIndex) const 205 { 206 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 207 return 0.0f; 208 } 209 return touchPoints_[pointIndex].tiltY; 210 } 211 GetKeyEvent()212 OH_NativeXComponent_KeyEvent* GetKeyEvent() 213 { 214 return &keyEvent_; 215 } 216 GetFocusEventCallback()217 NativeXComponent_Callback GetFocusEventCallback() const 218 { 219 return focusEventCallback_; 220 } 221 GetKeyEventCallback()222 NativeXComponent_Callback GetKeyEventCallback() const 223 { 224 return keyEventCallback_; 225 } 226 GetBlurEventCallback()227 NativeXComponent_Callback GetBlurEventCallback() const 228 { 229 return blurEventCallback_; 230 } 231 SetFocusEventCallback(NativeXComponent_Callback callback)232 void SetFocusEventCallback(NativeXComponent_Callback callback) 233 { 234 focusEventCallback_ = callback; 235 } 236 SetKeyEventCallback(NativeXComponent_Callback callback)237 void SetKeyEventCallback(NativeXComponent_Callback callback) 238 { 239 keyEventCallback_ = callback; 240 } 241 SetBlurEventCallback(NativeXComponent_Callback callback)242 void SetBlurEventCallback(NativeXComponent_Callback callback) 243 { 244 blurEventCallback_ = callback; 245 } 246 247 private: 248 std::string xcomponentId_; 249 void* window_ = nullptr; 250 int width_ = 0; 251 int height_ = 0; 252 double x_ = 0.0; 253 double y_ = 0.0; 254 OH_NativeXComponent_TouchEvent touchEvent_; 255 OH_NativeXComponent_MouseEvent mouseEvent_; 256 OH_NativeXComponent_KeyEvent keyEvent_; 257 OH_NativeXComponent_Callback* callback_ = nullptr; 258 OH_NativeXComponent_MouseEvent_Callback* mouseEventCallback_ = nullptr; 259 NativeXComponent_Callback focusEventCallback_ = nullptr; 260 NativeXComponent_Callback keyEventCallback_ = nullptr; 261 NativeXComponent_Callback blurEventCallback_ = nullptr; 262 std::vector<XComponentTouchPoint> touchPoints_; 263 std::vector<OH_NativeXComponent_HistoricalPoint> historicalPoints_; 264 }; 265 } // namespace OHOS::Ace 266 267 struct OH_NativeXComponent { OH_NativeXComponentOH_NativeXComponent268 explicit OH_NativeXComponent(OHOS::Ace::NativeXComponentImpl* xComponentImpl) : xcomponentImpl_(xComponentImpl) {} ~OH_NativeXComponentOH_NativeXComponent269 ~OH_NativeXComponent() {} 270 int32_t GetXComponentId(char* id, uint64_t* size); 271 int32_t GetNativeWindow(void** window); 272 int32_t GetXComponentSize(const void* window, uint64_t* width, uint64_t* height); 273 int32_t GetXComponentOffset(const void* window, double* x, double* y); 274 int32_t GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent); 275 int32_t GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent); 276 int32_t GetHistoryPoints(const void* window, int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints); 277 int32_t RegisterCallback(OH_NativeXComponent_Callback* callback); 278 int32_t RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback); 279 int32_t GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType); 280 int32_t GetTiltX(size_t pointIndex, float* tiltX); 281 int32_t GetTiltY(size_t pointIndex, float* tiltY); 282 int32_t RegisterFocusEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 283 int32_t RegisterKeyEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 284 int32_t RegisterBlurEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 285 int32_t GetKeyEvent(OH_NativeXComponent_KeyEvent** keyEvent); 286 287 private: 288 OHOS::Ace::NativeXComponentImpl* xcomponentImpl_ = nullptr; 289 }; 290 291 #endif // _NATIVE_INTERFACE_XCOMPONENT_IMPL_