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 <cstdio> 20 #include <cstring> 21 #include <iostream> 22 #include <list> 23 #include <map> 24 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "utils/log.h" 28 29 #include "define_multimodal.h" 30 #include "key_event.h" 31 #include "key_option.h" 32 33 #define SUCCESS_CODE 0 34 #define ERROR_CODE (-1) 35 #define UNREGISTERED_CODE (-2) 36 #define PRE_KEY_MAX_COUNT 4 37 38 enum JS_CALLBACK_EVENT { 39 JS_CALLBACK_EVENT_FAILED = -1, 40 JS_CALLBACK_EVENT_SUCCESS = 1, 41 JS_CALLBACK_EVENT_EXIST = 2, 42 JS_CALLBACK_EVENT_NOT_EXIST = 3, 43 }; 44 45 namespace OHOS { 46 namespace MMI { 47 class JsCommon { 48 public: 49 static bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 50 static void ThrowError(napi_env env, int32_t code); 51 }; 52 struct KeyEventMonitorInfo { 53 napi_env env{ nullptr }; 54 napi_async_work asyncWork{ nullptr }; 55 std::string eventType; 56 std::string name; 57 napi_value handle{ nullptr }; 58 std::shared_ptr<KeyEvent> keyEvent{ nullptr }; 59 napi_ref callback[1]{ nullptr }; 60 int32_t subscribeId{ 0 }; 61 std::shared_ptr<KeyOption> keyOption{ nullptr }; 62 std::function<void()> delCallback; ~KeyEventMonitorInfoKeyEventMonitorInfo63 ~KeyEventMonitorInfo() 64 { 65 if (delCallback) { 66 delCallback(); 67 } 68 } 69 }; 70 typedef std::map<std::string, std::list<KeyEventMonitorInfo *>> Callbacks; 71 } // namespace MMI 72 } // namespace OHOS 73 #endif // JS_REGISTER_MODULE_H