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_EVENT_TARGET_H
17 #define JS_EVENT_TARGET_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "util_napi.h"
22 #include "utils/log.h"
23 
24 #include "define_multimodal.h"
25 #include "error_multimodal.h"
26 #include "input_manager.h"
27 #include "js_util.h"
28 
29 namespace OHOS {
30 namespace MMI {
31 class JsEventTarget : public IInputDeviceListener, public std::enable_shared_from_this<JsEventTarget> {
32 public:
33     JsEventTarget();
34     virtual ~JsEventTarget() = default;
35     DISALLOW_COPY_AND_MOVE(JsEventTarget);
36     static void EmitJsIds(sptr<JsUtil::CallbackInfo> cb, std::vector<int32_t> &ids);
37     static void EmitJsDev(sptr<JsUtil::CallbackInfo> cb, std::shared_ptr<InputDevice> device);
38     static void EmitSupportKeys(sptr<JsUtil::CallbackInfo> cb, std::vector<bool> &keystrokeAbility);
39     static void EmitJsKeyboardType(sptr<JsUtil::CallbackInfo> cb, int32_t keyboardType);
40     static void EmitJsKeyboardRepeatDelay(sptr<JsUtil::CallbackInfo> cb, int32_t delay);
41     static void EmitJsKeyboardRepeatRate(sptr<JsUtil::CallbackInfo> cb, int32_t rate);
42     static void EmitJsSetKeyboardRepeatDelay(sptr<JsUtil::CallbackInfo> cb, int32_t errCode);
43     static void EmitJsSetKeyboardRepeatRate(sptr<JsUtil::CallbackInfo> cb, int32_t errCode);
44     static void EmitJsGetIntervalSinceLastInput(sptr<JsUtil::CallbackInfo> cb, int64_t timeInterval);
45     void AddListener(napi_env env, const std::string &type, napi_value handle);
46     void RemoveListener(napi_env env, const std::string &type, napi_value handle);
47     napi_value CreateCallbackInfo(napi_env, napi_value handle, sptr<JsUtil::CallbackInfo> cb);
48     void ResetEnv();
49     void OnDeviceAdded(int32_t deviceId, const std::string &type) override;
50     void OnDeviceRemoved(int32_t deviceId, const std::string &type) override;
51 
52 private:
53     static void CallIdsPromiseWork(uv_work_t *work, int32_t status);
54     static void CallIdsAsyncWork(uv_work_t *work, int32_t status);
55     static void CallDevAsyncWork(uv_work_t *work, int32_t status);
56     static void CallDevPromiseWork(uv_work_t *work, int32_t status);
57     static void CallKeystrokeAbilityPromise(uv_work_t *work, int32_t status);
58     static void CallKeystrokeAbilityAsync(uv_work_t *work, int32_t status);
59     static void CallKeyboardTypeAsync(uv_work_t *work, int32_t status);
60     static void CallKeyboardTypePromise(uv_work_t *work, int32_t status);
61     static void CallDevListPromiseWork(uv_work_t *work, int32_t status);
62     static void CallDevListAsyncWork(uv_work_t *work, int32_t status);
63     static void CallDevInfoAsyncWork(uv_work_t *work, int32_t status);
64     static void CallDevInfoPromiseWork(uv_work_t *work, int32_t status);
65     static void CallKeyboardRepeatDelayAsync(uv_work_t *work, int32_t status);
66     static void CallKeyboardRepeatDelayPromise(uv_work_t *work, int32_t status);
67     static void CallKeyboardRepeatRateAsync(uv_work_t *work, int32_t status);
68     static void CallKeyboardRepeatRatePromise(uv_work_t *work, int32_t status);
69     static void EmitAddedDeviceEvent(sptr<JsUtil::ReportData> reportData);
70     static void EmitRemoveDeviceEvent(sptr<JsUtil::ReportData> reportData);
71     static void CallIntervalSinceLastInputPromise(uv_work_t *work, int32_t status);
72     static napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage);
73 private:
74     inline static std::map<std::string, std::vector<std::unique_ptr<JsUtil::CallbackInfo>>> devListener_ {};
75     bool isListeningProcess_ { false };
76 };
77 } // namespace MMI
78 } // namespace OHOS
79 #endif // JS_EVENT_TARGET_H