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 "define_multimodal.h" 20 #include "input_manager.h" 21 #include "js_util.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class JsEventTarget : public IInputDeviceListener, public std::enable_shared_from_this<JsEventTarget> { 26 public: 27 JsEventTarget(); 28 virtual ~JsEventTarget() = default; 29 DISALLOW_COPY_AND_MOVE(JsEventTarget); 30 static void EmitJsIds(sptr<JsUtil::CallbackInfo> cb, std::vector<int32_t> &ids); 31 static void EmitJsDev(sptr<JsUtil::CallbackInfo> cb, int32_t deviceid); 32 static void EmitSupportKeys(sptr<JsUtil::CallbackInfo> cb, std::vector<int32_t> &keycode, int32_t id); 33 static void EmitJsKeyboardType(sptr<JsUtil::CallbackInfo> cb, int32_t deviceid); 34 static void EmitJsKeyboardRepeatDelay(sptr<JsUtil::CallbackInfo> cb, int32_t delay); 35 static void EmitJsKeyboardRepeatRate(sptr<JsUtil::CallbackInfo> cb, int32_t rate); 36 static void EmitJsSetKeyboardRepeatDelay(sptr<JsUtil::CallbackInfo> cb, int32_t delay); 37 static void EmitJsSetKeyboardRepeatRate(sptr<JsUtil::CallbackInfo> cb, int32_t rate); 38 static void EmitJsGetIntervalSinceLastInput(sptr<JsUtil::CallbackInfo> cb); 39 void AddListener(napi_env env, const std::string &type, napi_value handle); 40 void RemoveListener(napi_env env, const std::string &type, napi_value handle); 41 napi_value CreateCallbackInfo(napi_env, napi_value handle, sptr<JsUtil::CallbackInfo> cb); 42 void ResetEnv(); 43 void OnDeviceAdded(int32_t deviceId, const std::string &type) override; 44 void OnDeviceRemoved(int32_t deviceId, const std::string &type) override; 45 static void EmitJsSetInputDeviceEnabled(sptr<JsUtil::CallbackInfo> cb, int32_t errCode); 46 static void EmitJsSetFunctionKeyState(sptr<JsUtil::CallbackInfo> cb, int32_t funcKey, bool state); 47 static void EmitJsGetFunctionKeyState(sptr<JsUtil::CallbackInfo> cb, int32_t funcKey); 48 static void CallFunctionKeyStateTask(uv_work_t *work); 49 static void CallFunctionKeyState(uv_work_t *work, int32_t status); 50 static bool GetFunctionKeyStateErrCode(sptr<JsUtil::CallbackInfo> cb, 51 napi_handle_scope scope, napi_value &callResult); 52 static void EmitJsDevInternal(sptr<JsUtil::CallbackInfo> cb); 53 static void EmitJsIdsInternal(sptr<JsUtil::CallbackInfo> cb); 54 private: 55 static void CallIdsPromiseWork(uv_work_t *work, int32_t status); 56 static void CallIdsAsyncWork(uv_work_t *work, int32_t status); 57 static void CallDevAsyncWork(uv_work_t *work, int32_t status); 58 static void CallDevPromiseWork(uv_work_t *work, int32_t status); 59 static void CallKeystrokeAbilityPromise(uv_work_t *work, int32_t status); 60 static void CallKeystrokeAbilityAsync(uv_work_t *work, int32_t status); 61 static void CallKeyboardTypeAsync(uv_work_t *work, int32_t status); 62 static void CallKeyboardTypePromise(uv_work_t *work, int32_t status); 63 static void CallDevListPromiseWork(uv_work_t *work, int32_t status); 64 static void CallDevListAsyncWork(uv_work_t *work, int32_t status); 65 static void CallDevInfoAsyncWork(uv_work_t *work, int32_t status); 66 static void CallDevInfoPromiseWork(uv_work_t *work, int32_t status); 67 static void CallKeyboardRepeatDelayAsync(uv_work_t *work, int32_t status); 68 static void CallKeyboardRepeatDelayPromise(uv_work_t *work, int32_t status); 69 static void CallKeyboardRepeatRateAsync(uv_work_t *work, int32_t status); 70 static void CallKeyboardRepeatRatePromise(uv_work_t *work, int32_t status); 71 static void CallIntervalSinceLastInputPromise(uv_work_t *work, int32_t status); 72 static void EmitAddedDeviceEvent(sptr<JsUtil::ReportData> reportData); 73 static void EmitRemoveDeviceEvent(sptr<JsUtil::ReportData> reportData); 74 static napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage); 75 static void CallSetInputDeviceEnabledPromise(uv_work_t *work, int32_t status); 76 static void CallKeyboardRepeatDelayTask(uv_work_t *work, const std::string& operateType); 77 static void CallKeyboardRepeatRateTask(uv_work_t *work, const std::string& operateType); 78 static void CallGetKeyboardTypeTask(uv_work_t *work); 79 static void CallJsIdsTask(uv_work_t *work); 80 static void CallJsDevTask(uv_work_t *work); 81 static void CallSupportKeysTask(uv_work_t *work); 82 static void CallIntervalSinceLastInputTask(uv_work_t *work); 83 private: 84 inline static std::map<std::string, std::vector<std::unique_ptr<JsUtil::CallbackInfo>>> devListener_ {}; 85 bool isListeningProcess_ { false }; 86 }; 87 } // namespace MMI 88 } // namespace OHOS 89 #endif // JS_EVENT_TARGET_H