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 "refbase.h"
28 #include "utils/log.h"
29 
30 #include "define_multimodal.h"
31 #include "key_event.h"
32 #include "key_option.h"
33 
34 #define SUCCESS_CODE 0
35 #define ERROR_CODE (-1)
36 #define UNREGISTERED_CODE (-2)
37 #define PRE_KEY_MAX_COUNT 4
38 
39 enum JS_CALLBACK_EVENT {
40     JS_CALLBACK_EVENT_FAILED = -1,
41     JS_CALLBACK_EVENT_SUCCESS = 1,
42     JS_CALLBACK_EVENT_EXIST = 2,
43     JS_CALLBACK_EVENT_NOT_EXIST = 3,
44 };
45 
46 namespace OHOS {
47 namespace MMI {
48 extern std::mutex sCallBacksMutex;
49 class JsCommon {
50 public:
51     static bool TypeOf(napi_env env, napi_value value, napi_valuetype type);
52     static void ThrowError(napi_env env, int32_t code);
53 };
54 
55 struct KeyEventMonitorInfo : RefBase {
56     napi_env env{ nullptr };
57     napi_async_work asyncWork{ nullptr };
58     std::string eventType;
59     std::string name;
60     napi_ref callback{ nullptr };
61     int32_t subscribeId{ 0 };
62     std::shared_ptr<KeyOption> keyOption{ nullptr };
63     ~KeyEventMonitorInfo();
64 };
65 typedef std::map<std::string, std::list<sptr<KeyEventMonitorInfo>>> Callbacks;
66 } // namespace MMI
67 } // namespace OHOS
68 #endif // JS_REGISTER_MODULE_H