• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     napi_value JsQueryTouchEvents(napi_env env, int32_t count);
48     void EmitJsQueryTouchEvents(sptr<JsInputMonitor::CallbackInfo> cb, int32_t count);
49 
50 private:
51     JsInputMonitorManager() = default;
52     bool IsExisting(napi_env env);
53     void RemoveEnv(std::map<napi_env, napi_ref>::iterator it);
54     void RemoveAllEnv();
55     bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor,
56         napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers);
57     bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor,
58         napi_env jsEnv, const std::string &typeName, const int32_t fingers);
59 
60 private:
61     std::list<std::shared_ptr<JsInputMonitor>> monitors_;
62     std::map<napi_env, napi_ref> envManager_;
63     int32_t nextId_ { 0 };
64     std::mutex mutex_;
65     std::mutex envMutex_;
66 };
67 
68 #define JS_INPUT_MONITOR_MGR JsInputMonitorManager::GetInstance()
69 } // namespace MMI
70 } // namespace OHOS
71 #endif // JS_INPUT_MONITOR_MANAGER_H
72