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 JS_REGISTER_MODULE_H 17 #define JS_REGISTER_MODULE_H 18 19 #include <list> 20 21 #include "napi/native_node_api.h" 22 23 #include "define_multimodal.h" 24 #include "key_event.h" 25 #include "key_option.h" 26 27 #define SUCCESS_CODE 0 28 #define ERROR_CODE (-1) 29 #define UNREGISTERED_CODE (-2) 30 #define PRE_KEY_MAX_COUNT 4 31 32 enum JS_CALLBACK_EVENT { 33 JS_CALLBACK_EVENT_FAILED = -1, 34 JS_CALLBACK_EVENT_SUCCESS = 1, 35 JS_CALLBACK_EVENT_EXIST = 2, 36 JS_CALLBACK_EVENT_NOT_EXIST = 3, 37 }; 38 39 namespace OHOS { 40 namespace MMI { 41 extern std::mutex sCallBacksMutex; 42 class JsCommon { 43 public: 44 static bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 45 static void ThrowError(napi_env env, int32_t code); 46 }; 47 48 struct KeyEventMonitorInfo : RefBase { 49 napi_env env{ nullptr }; 50 std::mutex envMutex_; 51 napi_async_work asyncWork{ nullptr }; 52 std::string eventType; 53 std::string name; 54 napi_ref callback{ nullptr }; 55 int32_t subscribeId{ 0 }; 56 std::shared_ptr<KeyOption> keyOption{ nullptr }; 57 KeyEventMonitorInfo(napi_env env); 58 ~KeyEventMonitorInfo(); 59 }; 60 typedef std::map<std::string, std::list<sptr<KeyEventMonitorInfo>>> Callbacks; 61 62 class JsInputConsumer final { 63 enum JsKeyAction { 64 JS_KEY_ACTION_CANCEL, 65 JS_KEY_ACTION_DOWN, 66 JS_KEY_ACTION_UP, 67 }; 68 69 struct KeyMonitor { 70 int32_t subscriberId_ { -1 }; 71 KeyMonitorOption keyOption_ {}; 72 napi_env env_ { nullptr }; 73 napi_ref callback_ { nullptr }; 74 75 bool Parse(napi_env env, napi_callback_info info); 76 bool ParseKeyMonitorOption(napi_env env, napi_value keyOption); 77 bool ParseUnsubscription(napi_env env, napi_callback_info info); 78 }; 79 80 struct Work { 81 size_t keyMonitorId_ {}; 82 uv_work_t work_ {}; 83 std::shared_ptr<KeyEvent> keyEvent_ {}; 84 }; 85 86 public: 87 JsInputConsumer() = default; 88 ~JsInputConsumer() = default; 89 DISALLOW_COPY_AND_MOVE(JsInputConsumer); 90 91 void SubscribeKeyMonitor(napi_env env, napi_callback_info info); 92 void UnsubscribeKeyMonitor(napi_env env, napi_callback_info info); 93 94 static std::shared_ptr<JsInputConsumer> GetInstance(); 95 96 private: 97 size_t GenerateId(); 98 void CleanupKeyMonitor(napi_env env, KeyMonitor &tMonitor) const; 99 int32_t IsIdentical(napi_env env, const KeyMonitor &sMonitor, const KeyMonitor &tMonitor) const; 100 int32_t HasSubscribed(napi_env env, const KeyMonitor &keyMonitor) const; 101 bool SubscribeKeyMonitor(napi_env env, KeyMonitor &keyMonitor); 102 void UnsubscribeKeyMonitor(napi_env env, const KeyMonitor &keyMonitor); 103 void UnsubscribeKeyMonitors(napi_env env); 104 void OnSubscribeKeyMonitor(size_t keyMonitorId, std::shared_ptr<KeyEvent> keyEvent); 105 void NotifyKeyMonitor(uv_work_t *work, int32_t status); 106 void NotifyKeyMonitor(const KeyMonitor &keyMonitor, std::shared_ptr<KeyEvent> keyEvent); 107 void NotifyKeyMonitorScoped(const KeyMonitor &keyMonitor, std::shared_ptr<KeyEvent> keyEvent); 108 static bool CheckKeyMonitorOption(const KeyMonitorOption &keyOption); 109 static napi_value KeyEvent2JsKeyEvent(napi_env env, std::shared_ptr<KeyEvent> keyEvent); 110 static napi_value KeyItem2JsKey(napi_env env, const KeyEvent::KeyItem &keyItem); 111 static void HandleKeyMonitor(uv_work_t *work, int32_t status); 112 static int32_t JsKeyAction2KeyAction(int32_t action); 113 static int32_t KeyAction2JsKeyAction(int32_t action); 114 115 std::mutex mutex_; 116 size_t baseId_ { 0 }; 117 std::map<size_t, KeyMonitor> monitors_; 118 std::map<uv_work_t*, std::shared_ptr<Work>> pendingWorks_; 119 static const std::set<int32_t> allowedKeys_; 120 }; 121 } // namespace MMI 122 } // namespace OHOS 123 #endif // JS_REGISTER_MODULE_H 124