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 #ifndef MOTION_EVENT_NAPI_H 17 #define MOTION_EVENT_NAPI_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <set> 23 #include <string> 24 25 #include "napi/native_api.h" 26 #ifdef MOTION_ENABLE 27 #include "motion_agent_type.h" 28 #endif 29 30 namespace OHOS { 31 namespace Msdp { 32 struct MotionEventListener { 33 std::set<napi_ref> onRefSets; 34 }; 35 36 class MotionEventNapi { 37 public: 38 MotionEventNapi(napi_env env, napi_value thisVar); 39 MotionEventNapi() = default; 40 virtual ~MotionEventNapi(); 41 #ifdef MOTION_ENABLE 42 bool AddCallback(int32_t eventType, napi_value handler); 43 bool CheckEvents(int32_t eventType); 44 bool RemoveAllCallback(int32_t eventType); 45 bool RemoveCallback(int32_t eventType, napi_value handler); 46 virtual void OnEventOperatingHand(int32_t eventType, size_t argc, const MotionEvent &event); 47 void CreateIntData(napi_env env, napi_value motionValue, napi_value result, std::string name, int32_t value); 48 49 protected: 50 bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs); 51 bool InsertRef(std::shared_ptr<MotionEventListener> listener, const napi_value &handler); 52 void ConvertOperatingHandData(napi_value handler, size_t argc, const MotionEvent &event); 53 #endif 54 55 protected: 56 napi_env env_; 57 napi_ref thisVarRef_; 58 std::map<int32_t, std::shared_ptr<MotionEventListener>> events_; 59 int32_t count_ {0}; 60 }; 61 } // namespace Msdp 62 } // namespace OHOS 63 #endif // MOTION_EVENT_NAPI_H 64