1 /*
2 * Copyright (c) 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 #include "js_input_device_cooperate_manager.h"
17
18 #include <functional>
19
20 #include "define_multimodal.h"
21 #include "input_device_cooperate_impl.h"
22 #include "input_manager.h"
23 #include "mmi_log.h"
24 #include "napi_constants.h"
25 #include "util_napi.h"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsInputDeviceCooperateManager" };
31 } // namespace
32
Enable(napi_env env,bool enable,napi_value handle)33 napi_value JsInputDeviceCooperateManager::Enable(napi_env env, bool enable, napi_value handle)
34 {
35 CALL_INFO_TRACE;
36 std::lock_guard<std::mutex> guard(mutex_);
37 int32_t userData = InputDevCooperateImpl.GetUserData();
38 napi_value result = CreateCallbackInfo(env, handle, userData);
39 auto callback = std::bind(EmitJsEnable, userData, std::placeholders::_1, std::placeholders::_2);
40 int32_t errCode = InputMgr->EnableInputDeviceCooperate(enable, callback);
41 HandleExecuteResult(env, errCode);
42 if (errCode != RET_OK) {
43 RemoveCallbackInfo(userData);
44 }
45 return result;
46 }
47
Start(napi_env env,const std::string & sinkDeviceDescriptor,int32_t srcInputDeviceId,napi_value handle)48 napi_value JsInputDeviceCooperateManager::Start(napi_env env, const std::string &sinkDeviceDescriptor,
49 int32_t srcInputDeviceId, napi_value handle)
50 {
51 CALL_INFO_TRACE;
52 std::lock_guard<std::mutex> guard(mutex_);
53 int32_t userData = InputDevCooperateImpl.GetUserData();
54 napi_value result = CreateCallbackInfo(env, handle, userData);
55 auto callback = std::bind(EmitJsStart, userData, std::placeholders::_1, std::placeholders::_2);
56 int32_t errCode = InputMgr->StartInputDeviceCooperate(sinkDeviceDescriptor, srcInputDeviceId, callback);
57 HandleExecuteResult(env, errCode);
58 if (errCode != RET_OK) {
59 RemoveCallbackInfo(userData);
60 }
61 return result;
62 }
63
Stop(napi_env env,napi_value handle)64 napi_value JsInputDeviceCooperateManager::Stop(napi_env env, napi_value handle)
65 {
66 CALL_INFO_TRACE;
67 std::lock_guard<std::mutex> guard(mutex_);
68 int32_t userData = InputDevCooperateImpl.GetUserData();
69 napi_value result = CreateCallbackInfo(env, handle, userData);
70 auto callback = std::bind(EmitJsStop, userData, std::placeholders::_1, std::placeholders::_2);
71 int32_t errCode = InputMgr->StopDeviceCooperate(callback);
72 HandleExecuteResult(env, errCode);
73 if (errCode != RET_OK) {
74 RemoveCallbackInfo(userData);
75 }
76 return result;
77 }
78
GetState(napi_env env,const std::string & deviceDescriptor,napi_value handle)79 napi_value JsInputDeviceCooperateManager::GetState(napi_env env, const std::string &deviceDescriptor, napi_value handle)
80 {
81 CALL_INFO_TRACE;
82 std::lock_guard<std::mutex> guard(mutex_);
83 int32_t userData = InputDevCooperateImpl.GetUserData();
84 napi_value result = CreateCallbackInfo(env, handle, userData);
85 auto callback = std::bind(EmitJsGetState, userData, std::placeholders::_1);
86 int32_t errCode = InputMgr->GetInputDeviceCooperateState(deviceDescriptor, callback);
87 HandleExecuteResult(env, errCode);
88 if (errCode != RET_OK) {
89 RemoveCallbackInfo(userData);
90 }
91 return result;
92 }
93
RegisterListener(napi_env env,const std::string & type,napi_value handle)94 void JsInputDeviceCooperateManager::RegisterListener(napi_env env, const std::string &type, napi_value handle)
95 {
96 CALL_INFO_TRACE;
97 AddListener(env, type, handle);
98 }
99
UnregisterListener(napi_env env,const std::string & type,napi_value handle)100 void JsInputDeviceCooperateManager::UnregisterListener(napi_env env, const std::string &type, napi_value handle)
101 {
102 CALL_INFO_TRACE;
103 RemoveListener(env, type, handle);
104 }
105
ResetEnv()106 void JsInputDeviceCooperateManager::ResetEnv()
107 {
108 CALL_INFO_TRACE;
109 JsEventTarget::ResetEnv();
110 }
111 } // namespace MMI
112 } // namespace OHOS
113