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 "input_device_cooperate_state_in.h"
17
18 #include "cooperation_message.h"
19 #include "device_cooperate_softbus_adapter.h"
20 #include "distributed_input_adapter.h"
21 #include "input_device_cooperate_sm.h"
22 #include "input_device_cooperate_util.h"
23 #include "input_device_manager.h"
24 #include "mouse_event_normalize.h"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceCooperateStateIn"};
30 } // namespace
31
InputDeviceCooperateStateIn(const std::string & startDhid)32 InputDeviceCooperateStateIn::InputDeviceCooperateStateIn(const std::string &startDhid) : startDhid_(startDhid) {}
33
StartInputDeviceCooperate(const std::string & remoteNetworkId,int32_t startInputDeviceId)34 int32_t InputDeviceCooperateStateIn::StartInputDeviceCooperate(const std::string &remoteNetworkId,
35 int32_t startInputDeviceId)
36 {
37 CALL_INFO_TRACE;
38 if (remoteNetworkId.empty()) {
39 MMI_HILOGE("RemoteNetworkId is empty");
40 return static_cast<int32_t>(CooperationMessage::COOPERATION_DEVICE_ERROR);
41 }
42 std::string localNetworkId = GetLocalDeviceId();
43 if (localNetworkId.empty() || remoteNetworkId == localNetworkId) {
44 MMI_HILOGE("Input Parameters error");
45 return static_cast<int32_t>(CooperationMessage::COOPERATION_DEVICE_ERROR);
46 }
47 int32_t ret = DevCooperateSoftbusAdapter->StartRemoteCooperate(localNetworkId, remoteNetworkId);
48 if (ret != RET_OK) {
49 MMI_HILOGE("Start input device cooperate fail");
50 return static_cast<int32_t>(CooperationMessage::COOPERATE_FAIL);
51 }
52 std::string taskName = "process_start_task";
53 std::function<void()> handleProcessStartFunc =
54 std::bind(&InputDeviceCooperateStateIn::ProcessStart, this, remoteNetworkId, startInputDeviceId);
55 CHKPR(eventHandler_, RET_ERR);
56 eventHandler_->ProxyPostTask(handleProcessStartFunc, taskName, 0);
57 return RET_OK;
58 }
59
ProcessStart(const std::string & remoteNetworkId,int32_t startInputDeviceId)60 int32_t InputDeviceCooperateStateIn::ProcessStart(const std::string &remoteNetworkId, int32_t startInputDeviceId)
61 {
62 CALL_DEBUG_ENTER;
63 std::string originNetworkId = InputDevMgr->GetOriginNetworkId(startInputDeviceId);
64 if (remoteNetworkId == originNetworkId) {
65 ComeBack(remoteNetworkId, startInputDeviceId);
66 return RET_OK;
67 } else {
68 return RelayComeBack(remoteNetworkId, startInputDeviceId);
69 }
70 }
71
StopInputDeviceCooperate(const std::string & networkId)72 int32_t InputDeviceCooperateStateIn::StopInputDeviceCooperate(const std::string &networkId)
73 {
74 CALL_DEBUG_ENTER;
75 int32_t ret = DevCooperateSoftbusAdapter->StopRemoteCooperate(networkId);
76 if (ret != RET_OK) {
77 MMI_HILOGE("Stop input device cooperate fail");
78 return ret;
79 }
80 std::string taskName = "process_stop_task";
81 std::function<void()> handleProcessStopFunc = std::bind(&InputDeviceCooperateStateIn::ProcessStop, this);
82 CHKPR(eventHandler_, RET_ERR);
83 eventHandler_->ProxyPostTask(handleProcessStopFunc, taskName, 0);
84 return RET_OK;
85 }
86
ProcessStop()87 int32_t InputDeviceCooperateStateIn::ProcessStop()
88 {
89 CALL_DEBUG_ENTER;
90 std::vector<std::string> dhids = InputDevMgr->GetCooperateDhids(startDhid_);
91 std::string sink = InputDevMgr->GetOriginNetworkId(startDhid_);
92 int32_t ret = DistributedAdapter->StopRemoteInput(
93 sink, dhids, [this, sink](bool isSuccess) { this->OnStopRemoteInput(isSuccess, sink, -1); });
94 if (ret != RET_OK) {
95 InputDevCooSM->OnStopFinish(false, sink);
96 }
97 return RET_OK;
98 }
99
OnStartRemoteInput(bool isSuccess,const std::string & srcNetworkId,int32_t startInputDeviceId)100 void InputDeviceCooperateStateIn::OnStartRemoteInput(
101 bool isSuccess, const std::string &srcNetworkId, int32_t startInputDeviceId)
102 {
103 CALL_DEBUG_ENTER;
104 if (!isSuccess) {
105 IInputDeviceCooperateState::OnStartRemoteInput(isSuccess, srcNetworkId, startInputDeviceId);
106 return;
107 }
108 std::string sinkNetworkId = InputDevMgr->GetOriginNetworkId(startInputDeviceId);
109 std::vector<std::string> dhid = InputDevMgr->GetCooperateDhids(startInputDeviceId);
110
111 std::string taskName = "relay_stop_task";
112 std::function<void()> handleRelayStopFunc = std::bind(&InputDeviceCooperateStateIn::StopRemoteInput,
113 this, sinkNetworkId, srcNetworkId, dhid, startInputDeviceId);
114 CHKPV(eventHandler_);
115 eventHandler_->ProxyPostTask(handleRelayStopFunc, taskName, 0);
116 }
117
StopRemoteInput(const std::string & sinkNetworkId,const std::string & srcNetworkId,const std::vector<std::string> & dhid,int32_t startInputDeviceId)118 void InputDeviceCooperateStateIn::StopRemoteInput(const std::string &sinkNetworkId,
119 const std::string &srcNetworkId, const std::vector<std::string> &dhid, int32_t startInputDeviceId)
120 {
121 int32_t ret = DistributedAdapter->StopRemoteInput(sinkNetworkId, dhid,
122 [this, srcNetworkId, startInputDeviceId](bool isSuccess) {
123 this->OnStopRemoteInput(isSuccess, srcNetworkId, startInputDeviceId);
124 });
125 if (ret != RET_OK) {
126 InputDevCooSM->OnStartFinish(false, sinkNetworkId, startInputDeviceId);
127 }
128 }
129
OnStopRemoteInput(bool isSuccess,const std::string & remoteNetworkId,int32_t startInputDeviceId)130 void InputDeviceCooperateStateIn::OnStopRemoteInput(bool isSuccess,
131 const std::string &remoteNetworkId, int32_t startInputDeviceId)
132 {
133 CALL_DEBUG_ENTER;
134 if (InputDevCooSM->IsStarting()) {
135 std::string taskName = "start_finish_task";
136 std::function<void()> handleStartFinishFunc = std::bind(&InputDeviceCooperateSM::OnStartFinish,
137 InputDevCooSM, isSuccess, remoteNetworkId, startInputDeviceId);
138 CHKPV(eventHandler_);
139 eventHandler_->ProxyPostTask(handleStartFinishFunc, taskName, 0);
140 } else if (InputDevCooSM->IsStopping()) {
141 std::string taskName = "stop_finish_task";
142 std::function<void()> handleStopFinishFunc =
143 std::bind(&InputDeviceCooperateSM::OnStopFinish, InputDevCooSM, isSuccess, remoteNetworkId);
144 CHKPV(eventHandler_);
145 eventHandler_->ProxyPostTask(handleStopFinishFunc, taskName, 0);
146 }
147 }
148
ComeBack(const std::string & sinkNetworkId,int32_t startInputDeviceId)149 void InputDeviceCooperateStateIn::ComeBack(const std::string &sinkNetworkId, int32_t startInputDeviceId)
150 {
151 CALL_DEBUG_ENTER;
152 std::vector<std::string> dhids = InputDevMgr->GetCooperateDhids(startInputDeviceId);
153 if (dhids.empty()) {
154 InputDevCooSM->OnStartFinish(false, sinkNetworkId, startInputDeviceId);
155 }
156 int32_t ret = DistributedAdapter->StopRemoteInput(sinkNetworkId, dhids,
157 [this, sinkNetworkId, startInputDeviceId](bool isSuccess) {
158 this->OnStopRemoteInput(isSuccess, sinkNetworkId, startInputDeviceId);
159 });
160 if (ret != RET_OK) {
161 InputDevCooSM->OnStartFinish(false, sinkNetworkId, startInputDeviceId);
162 }
163 }
164
RelayComeBack(const std::string & srcNetworkId,int32_t startInputDeviceId)165 int32_t InputDeviceCooperateStateIn::RelayComeBack(const std::string &srcNetworkId, int32_t startInputDeviceId)
166 {
167 CALL_DEBUG_ENTER;
168 return PrepareAndStart(srcNetworkId, startInputDeviceId);
169 }
170 } // namespace MMI
171 } // namespace OHOS
172