1 /* 2 * Copyright (c) 2021-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 JS_INPUT_MONITOR_MANAGER_H 17 #define JS_INPUT_MONITOR_MANAGER_H 18 19 #include "js_input_monitor.h" 20 21 namespace OHOS { 22 namespace MMI { 23 class JsInputMonitorManager final { 24 public: 25 static JsInputMonitorManager& GetInstance(); 26 DISALLOW_COPY_AND_MOVE(JsInputMonitorManager); 27 ~JsInputMonitorManager() = default; 28 29 void AddMonitor(napi_env jsEnv, const std::string &typeName, 30 std::vector<Rect> hotRectArea, int32_t rectTotal, napi_value callback, const int32_t fingers = 0); 31 void AddMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers = 0); 32 void RemoveMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers = 0); 33 void RemoveMonitor(napi_env jsEnv, const std::string &typeName, const int32_t fingers = 0); 34 void RemoveMonitor(napi_env jsEnv); 35 void AddPreMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, std::vector<int32_t> keys); 36 void OnPointerEventByMonitorId(int32_t id, int32_t fingers, std::shared_ptr<PointerEvent> pointEvent); 37 void OnKeyEventByMonitorId(int32_t id, std::shared_ptr<KeyEvent> keyEvent); 38 const std::shared_ptr<JsInputMonitor> GetMonitor(int32_t id, int32_t fingers); 39 const std::shared_ptr<JsInputMonitor> GetPreMonitor(int32_t id); 40 std::string GetMonitorTypeName(int32_t id, int32_t fingers); 41 std::string GetPreMonitorTypeName(int32_t id); 42 bool AddEnv(napi_env env, napi_callback_info cbInfo); 43 void RemoveEnv(napi_env env); 44 void ThrowError(napi_env env, int32_t code); 45 std::vector<Rect> GetHotRectAreaList(napi_env env, napi_value rectNapiValue, uint32_t rectListLength); 46 bool GetKeysArray(napi_env env, napi_value keysNapiValue, uint32_t keysLength, std::vector<int32_t>& keys); 47 48 private: 49 JsInputMonitorManager() = default; 50 bool IsExisting(napi_env env); 51 void RemoveEnv(std::map<napi_env, napi_ref>::iterator it); 52 void RemoveAllEnv(); 53 bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor, 54 napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers); 55 bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor, 56 napi_env jsEnv, const std::string &typeName, const int32_t fingers); 57 58 private: 59 std::list<std::shared_ptr<JsInputMonitor>> monitors_; 60 std::map<napi_env, napi_ref> envManager_; 61 int32_t nextId_ { 0 }; 62 std::mutex mutex_; 63 std::mutex envMutex_; 64 }; 65 66 #define JS_INPUT_MONITOR_MGR JsInputMonitorManager::GetInstance() 67 } // namespace MMI 68 } // namespace OHOS 69 #endif // JS_INPUT_MONITOR_MANAGER_H 70