1 /*
2  * Copyright (c) 2021-2024 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 "distributed_input_sink_manager.h"
17 
18 #include <algorithm>
19 #include <fcntl.h>
20 #include <pthread.h>
21 #include <thread>
22 #include <linux/input.h>
23 
24 #include "dinput_softbus_define.h"
25 #include "distributed_hardware_fwk_kit.h"
26 #include "if_system_ability_manager.h"
27 #include "iservice_registry.h"
28 #include "screen_manager.h"
29 #include "string_ex.h"
30 #include "system_ability_definition.h"
31 
32 #include "distributed_input_collector.h"
33 #include "distributed_input_sink_switch.h"
34 #include "distributed_input_sink_transport.h"
35 #include "distributed_input_transport_base.h"
36 
37 #include "dinput_context.h"
38 #include "dinput_errcode.h"
39 #include "dinput_log.h"
40 #include "dinput_utils_tool.h"
41 #include "hidumper.h"
42 #include "hisysevent_util.h"
43 #include "white_list_util.h"
44 
45 namespace OHOS {
46 namespace DistributedHardware {
47 namespace DistributedInput {
48 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSinkManager, DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, true);
49 
DistributedInputSinkManager(int32_t saId,bool runOnCreate)50 DistributedInputSinkManager::DistributedInputSinkManager(int32_t saId, bool runOnCreate)
51     : SystemAbility(saId, runOnCreate)
52 {
53     DHLOGI("DistributedInputSinkManager ctor!");
54     inputTypes_ = DInputDeviceType::NONE;
55 }
56 
~DistributedInputSinkManager()57 DistributedInputSinkManager::~DistributedInputSinkManager()
58 {
59     DHLOGI("DistributedInputSinkManager dtor!");
60     projectWindowListener_ = nullptr;
61     pluginStartListener_ = nullptr;
62 }
63 
DInputSinkMgrListener(DistributedInputSinkManager * manager)64 DistributedInputSinkManager::DInputSinkMgrListener::DInputSinkMgrListener(DistributedInputSinkManager *manager)
65 {
66     sinkManagerObj_ = manager;
67     DHLOGI("DInputSinkMgrListener init.");
68 }
69 
~DInputSinkMgrListener()70 DistributedInputSinkManager::DInputSinkMgrListener::~DInputSinkMgrListener()
71 {
72     sinkManagerObj_ = nullptr;
73     DHLOGI("DInputSinkMgrListener destory.");
74 }
75 
ResetSinkMgrResStatus()76 void DistributedInputSinkManager::DInputSinkMgrListener::ResetSinkMgrResStatus()
77 {
78     DHLOGI("DInputSinkMgrListener ResetSinkMgrResStatus.");
79     if (sinkManagerObj_ == nullptr) {
80         DHLOGE("ResetSinkMgrResStatus sinkManagerObj is null.");
81         return;
82     }
83     sinkManagerObj_->ClearResourcesStatus();
84 }
85 
DInputSinkListener(DistributedInputSinkManager * manager)86 DistributedInputSinkManager::DInputSinkListener::DInputSinkListener(DistributedInputSinkManager *manager)
87 {
88     sinkManagerObj_ = manager;
89     sinkManagerObj_->SetInputTypes(static_cast<uint32_t>(DInputDeviceType::NONE));
90     DHLOGI("DInputSinkListener init.");
91 }
92 
~DInputSinkListener()93 DistributedInputSinkManager::DInputSinkListener::~DInputSinkListener()
94 {
95     if (sinkManagerObj_ != nullptr) {
96         sinkManagerObj_->SetInputTypes(static_cast<uint32_t>(DInputDeviceType::NONE));
97         sinkManagerObj_ = nullptr;
98         DHLOGI("DInputSinkListener destory.");
99     }
100 }
101 
QueryLocalWhiteList(nlohmann::json & jsonStr)102 void DistributedInputSinkManager::QueryLocalWhiteList(nlohmann::json &jsonStr)
103 {
104     TYPE_WHITE_LIST_VEC vecFilter;
105     std::string localNetworkId = GetLocalDeviceInfo().networkId;
106     if (!localNetworkId.empty()) {
107         WhiteListUtil::GetInstance().GetWhiteList(localNetworkId, vecFilter);
108     } else {
109         DHLOGE("Query local network id from softbus failed");
110         jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = "";
111         return;
112     }
113 
114     if (vecFilter.empty() || vecFilter[0].empty() || vecFilter[0][0].empty()) {
115         DHLOGE("White list is null.");
116         jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = "";
117         return;
118     }
119     nlohmann::json filterMsg(vecFilter);
120     std::string object = filterMsg.dump();
121     jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object;
122 }
123 
ClearResourcesStatus()124 void DistributedInputSinkManager::ClearResourcesStatus()
125 {
126     std::lock_guard<std::mutex> lock(mutex_);
127     sharingDhIdsMap_.clear();
128     sharingDhIds_.clear();
129 }
130 
OnPrepareRemoteInput(const int32_t & sessionId,const std::string & deviceId)131 void DistributedInputSinkManager::DInputSinkListener::OnPrepareRemoteInput(
132     const int32_t &sessionId, const std::string &deviceId)
133 {
134     if (sinkManagerObj_ == nullptr) {
135         DHLOGE("sinkManagerObj is null.");
136         return;
137     }
138     DHLOGI("OnPrepareRemoteInput called, sessionId: %{public}d, devId: %{public}s", sessionId,
139         GetAnonyString(deviceId).c_str());
140     nlohmann::json jsonStr;
141     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONPREPARE;
142     std::string smsg = "";
143 
144     DistributedInputSinkSwitch::GetInstance().AddSession(sessionId);
145     sinkManagerObj_->QueryLocalWhiteList(jsonStr);
146     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
147     smsg = jsonStr.dump();
148     DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg);
149 }
150 
OnUnprepareRemoteInput(const int32_t & sessionId)151 void DistributedInputSinkManager::DInputSinkListener::OnUnprepareRemoteInput(const int32_t &sessionId)
152 {
153     DHLOGI("OnUnprepareRemoteInput called, sessionId: %{public}d", sessionId);
154     std::vector<std::string> sharingDhIds = DistributedInputCollector::GetInstance().GetSharingDhIds();
155     if (!sharingDhIds.empty()) {
156         OnStopRemoteInputDhid(sessionId, JointDhIds(sharingDhIds));
157     }
158     DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId);
159 
160     nlohmann::json jsonStr;
161     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONUNPREPARE;
162     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
163     std::string smsg = jsonStr.dump();
164     DistributedInputSinkTransport::GetInstance().RespUnprepareRemoteInput(sessionId, smsg);
165 }
166 
OnRelayPrepareRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId)167 void DistributedInputSinkManager::DInputSinkListener::OnRelayPrepareRemoteInput(const int32_t &toSrcSessionId,
168     const int32_t &toSinkSessionId, const std::string &deviceId)
169 {
170     if (sinkManagerObj_ == nullptr) {
171         DHLOGE("sinkManagerObj is null.");
172         return;
173     }
174     DHLOGI("OnRelayPrepareRemoteInput called, toSinkSessionId: %{public}d, devId: %{public}s", toSinkSessionId,
175         GetAnonyString(deviceId).c_str());
176     nlohmann::json jsonStr;
177     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_PREPARE;
178     jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
179     std::string smsg = "";
180 
181     DistributedInputSinkSwitch::GetInstance().AddSession(toSinkSessionId);
182     sinkManagerObj_->QueryLocalWhiteList(jsonStr);
183     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
184     smsg = jsonStr.dump();
185     DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(toSinkSessionId, smsg);
186 }
187 
OnRelayUnprepareRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId)188 void DistributedInputSinkManager::DInputSinkListener::OnRelayUnprepareRemoteInput(const int32_t &toSrcSessionId,
189     const int32_t &toSinkSessionId, const std::string &deviceId)
190 {
191     DHLOGI("OnRelayUnprepareRemoteInput called, toSinkSessionId: %{public}d, devId: %{public}s", toSinkSessionId,
192         GetAnonyString(deviceId).c_str());
193     std::vector<std::string> sharingDhIds = DistributedInputCollector::GetInstance().GetSharingDhIds();
194     if (!sharingDhIds.empty()) {
195         OnStopRemoteInputDhid(toSinkSessionId, JointDhIds(sharingDhIds));
196     }
197     DistributedInputSinkSwitch::GetInstance().RemoveSession(toSinkSessionId);
198 
199     nlohmann::json jsonStr;
200     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_UNPREPARE;
201     jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
202     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
203     std::string smsg = jsonStr.dump();
204     DistributedInputSinkTransport::GetInstance().RespUnprepareRemoteInput(toSinkSessionId, smsg);
205 }
206 
OnStartRemoteInput(const int32_t & sessionId,const uint32_t & inputTypes)207 void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput(
208     const int32_t &sessionId, const uint32_t &inputTypes)
209 {
210     if (sinkManagerObj_ == nullptr) {
211         DHLOGE("sinkManagerObj is null.");
212         return;
213     }
214     DHLOGI("OnStartRemoteInput called, sessionId: %{public}d, inputTypes: %{public}u.", sessionId, inputTypes);
215     // set new session
216     int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId);
217 
218     sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE
219         : DInputServerType::NULL_SERVER_TYPE);
220 
221     bool result = (startRes == DH_SUCCESS);
222     nlohmann::json jsonStrSta;
223     jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONSTART;
224     jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes;
225     jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result;
226     std::string smsg = jsonStrSta.dump();
227     DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg);
228 
229     if (startRes == DH_SUCCESS) {
230         sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes);
231         AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true,
232             sinkManagerObj_->GetInputTypes());
233         sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds);
234         DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds);
235     }
236 
237     bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast<uint32_t>(DInputDeviceType::MOUSE)) != 0;
238     if (isMouse) {
239         std::map<int32_t, std::string> deviceInfos;
240         DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast<uint32_t>(DInputDeviceType::MOUSE),
241             deviceInfos);
242         for (const auto &deviceInfo : deviceInfos) {
243             DHLOGI("deviceInfo dhId, %{public}s", GetAnonyString(deviceInfo.second).c_str());
244             std::vector<std::string> devDhIds;
245             SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds);
246             DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId);
247         }
248     }
249 }
250 
OnStopRemoteInput(const int32_t & sessionId,const uint32_t & inputTypes)251 void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInput(
252     const int32_t &sessionId, const uint32_t &inputTypes)
253 {
254     if (sinkManagerObj_ == nullptr) {
255         DHLOGE("sinkManagerObj is null.");
256         return;
257     }
258     DHLOGI("OnStopRemoteInput called, sessionId: %{public}d, inputTypes: %{public}d, curInputTypes: %{public}d",
259         sessionId, inputTypes, sinkManagerObj_->GetInputTypes());
260 
261     sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() -
262         (sinkManagerObj_->GetInputTypes() & inputTypes));
263     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(false, inputTypes);
264     std::vector<std::string> stopIndeedDhIds;
265     sinkManagerObj_->DeleteStopDhids(sessionId, affDhIds.noSharingDhIds, stopIndeedDhIds);
266     AffectDhIds stopIndeedOnes;
267     stopIndeedOnes.noSharingDhIds = stopIndeedDhIds;
268     DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes);
269 
270     nlohmann::json jsonStr;
271     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONSTOP;
272     jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes;
273     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
274     std::string smsg = jsonStr.dump();
275     DistributedInputSinkTransport::GetInstance().RespStopRemoteInput(sessionId, smsg);
276 
277     bool isAllClosed = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
278     if (isAllClosed) {
279         DistributedInputSinkSwitch::GetInstance().StopAllSwitch();
280         if (DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession() ==
281             ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) {
282             DHLOGI("all session is stop.");
283             sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
284         }
285     }
286 }
287 
OnStartRemoteInputDhid(const int32_t & sessionId,const std::string & strDhids)288 void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(const int32_t &sessionId,
289     const std::string &strDhids)
290 {
291     DHLOGI("OnStartRemoteInputDhid called, sessionId: %{public}d", sessionId);
292     // set new session
293     int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId);
294 
295     bool result = (startRes == DH_SUCCESS);
296     nlohmann::json jsonStrSta;
297     jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_DHID_ONSTART;
298     jsonStrSta[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids;
299     jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result;
300     std::string smsg = jsonStrSta.dump();
301     DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg);
302 
303     if (startRes != DH_SUCCESS) {
304         DHLOGE("StartSwitch error.");
305         return;
306     }
307     if (sinkManagerObj_ == nullptr) {
308         DHLOGE("sinkManagerObj is null.");
309         return;
310     }
311     std::vector<std::string> devDhIds;
312     SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds);
313     DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId);
314     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds);
315     sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds);
316     DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds);
317 }
318 
OnStopRemoteInputDhid(const int32_t & sessionId,const std::string & strDhids)319 void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(const int32_t &sessionId,
320     const std::string &strDhids)
321 {
322     if (sinkManagerObj_ == nullptr) {
323         DHLOGE("sinkManagerObj is null.");
324         return;
325     }
326     DHLOGI("OnStopRemoteInputDhid called, sessionId: %{public}d", sessionId);
327     std::vector<std::string> stopIndeedDhIds;
328     std::vector<std::string> stopOnCmdDhIds;
329     SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds);
330     sinkManagerObj_->DeleteStopDhids(sessionId, stopOnCmdDhIds, stopIndeedDhIds);
331     (void)DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds);
332     AffectDhIds stopIndeedOnes;
333     stopIndeedOnes.noSharingDhIds = stopIndeedDhIds;
334     DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes);
335 
336     DInputSinkState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, sessionId);
337 
338     if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) {
339         DHLOGE("All dhid stop sharing, sessionId: %{public}d is closed.", sessionId);
340         DistributedInputSinkSwitch::GetInstance().StopSwitch(sessionId);
341     }
342 
343     nlohmann::json jsonStr;
344     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_DHID_ONSTOP;
345     jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids;
346     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
347     std::string smsg = jsonStr.dump();
348     DistributedInputSinkTransport::GetInstance().RespStopRemoteInput(sessionId, smsg);
349 
350     bool isAllClosed = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
351     if (isAllClosed) {
352         DistributedInputSinkSwitch::GetInstance().StopAllSwitch();
353         sinkManagerObj_->SetInputTypes(static_cast<uint32_t>(DInputDeviceType::NONE));
354         if (DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession() ==
355             ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) {
356             DHLOGI("all session is stop.");
357             sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
358         }
359     }
360 }
361 
OnRelayStartDhidRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId,const std::string & strDhids)362 void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInput(const int32_t &toSrcSessionId,
363     const int32_t &toSinkSessionId, const std::string &deviceId, const std::string &strDhids)
364 {
365     DHLOGI("onRelayStartDhidRemoteInput called, toSinkSessionId: %{public}d", toSinkSessionId);
366     // set new session
367     int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId);
368     bool result = (startRes == DH_SUCCESS);
369     nlohmann::json jsonStrSta;
370     jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STARTDHID;
371     jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
372     jsonStrSta[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids;
373     jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result;
374     std::string smsg = jsonStrSta.dump();
375     DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg);
376 
377     if (startRes != DH_SUCCESS) {
378         DHLOGE("StartSwitch error.");
379         return;
380     }
381     if (sinkManagerObj_ == nullptr) {
382         DHLOGE("sinkManagerObj is null.");
383         return;
384     }
385     std::vector<std::string> devDhIds;
386     SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds);
387     DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId);
388     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds);
389     sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds);
390     DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds);
391 }
392 
OnRelayStopDhidRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId,const std::string & strDhids)393 void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput(const int32_t &toSrcSessionId,
394     const int32_t &toSinkSessionId, const std::string &deviceId, const std::string &strDhids)
395 {
396     if (sinkManagerObj_ == nullptr) {
397         DHLOGE("sinkManagerObj is null.");
398         return;
399     }
400     DHLOGI("onRelayStopDhidRemoteInput called, toSinkSessionId: %{public}d", toSinkSessionId);
401     std::vector<std::string> stopIndeedDhIds;
402     std::vector<std::string> stopOnCmdDhIds;
403     SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds);
404     sinkManagerObj_->DeleteStopDhids(toSinkSessionId, stopOnCmdDhIds, stopIndeedDhIds);
405     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds);
406     AffectDhIds stopIndeedOnes;
407     stopIndeedOnes.noSharingDhIds = stopIndeedDhIds;
408     DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes);
409 
410     DInputSinkState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, toSinkSessionId);
411 
412     if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) {
413         DHLOGE("All dhid stop sharing, sessionId: %{public}d is closed.", toSinkSessionId);
414         DistributedInputSinkSwitch::GetInstance().StopSwitch(toSinkSessionId);
415     }
416 
417     nlohmann::json jsonStr;
418     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STOPDHID;
419     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
420     jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
421     jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids;
422     std::string smsg = jsonStr.dump();
423     DistributedInputSinkTransport::GetInstance().RespStopRemoteInput(toSinkSessionId, smsg);
424 
425     bool isAllClosed = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
426     if (!isAllClosed) {
427         DHLOGE("Not all devices are stopped.");
428         return;
429     }
430     DistributedInputSinkSwitch::GetInstance().StopAllSwitch();
431     sinkManagerObj_->SetInputTypes(static_cast<uint32_t>(DInputDeviceType::NONE));
432     if (DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession() ==
433         ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) {
434         DHLOGI("All session is stop.");
435         sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
436     }
437 }
438 
OnRelayStartTypeRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId,uint32_t inputTypes)439 void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInput(const int32_t &toSrcSessionId,
440     const int32_t &toSinkSessionId, const std::string &deviceId, uint32_t inputTypes)
441 {
442     if (sinkManagerObj_ == nullptr) {
443         DHLOGE("sinkManagerObj is null.");
444         return;
445     }
446     DHLOGI("OnRelayStartTypeRemoteInput called, toSinkSessionId: %{public}d", toSinkSessionId);
447     // set new session
448     int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId);
449 
450     sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE
451         : DInputServerType::NULL_SERVER_TYPE);
452     bool result = (startRes == DH_SUCCESS);
453     nlohmann::json jsonStrSta;
454     jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STARTTYPE;
455     jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes;
456     jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result;
457     jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
458     std::string smsg = jsonStrSta.dump();
459     DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg);
460 
461     if (startRes != DH_SUCCESS) {
462         DHLOGE("OnRelayStartTypeRemoteInput startSwitch error.");
463         return;
464     }
465 
466     if (sinkManagerObj_->GetEventHandler() == nullptr) {
467         DHLOGE("eventhandler is null.");
468         return;
469     }
470 
471     sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes);
472     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true,
473         sinkManagerObj_->GetInputTypes());
474     sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds);
475     DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds);
476 
477     bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast<uint32_t>(DInputDeviceType::MOUSE)) != 0;
478     if (!isMouse) {
479         DHLOGE("No mouse type.");
480         return;
481     }
482     std::map<int32_t, std::string> deviceInfos;
483     DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast<uint32_t>(DInputDeviceType::MOUSE),
484         deviceInfos);
485     for (auto deviceInfo : deviceInfos) {
486         DHLOGI("deviceInfo dhId, %{public}s", GetAnonyString(deviceInfo.second).c_str());
487         std::vector<std::string> devDhIds;
488         SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds);
489         DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId);
490     }
491 }
492 
OnRelayStopTypeRemoteInput(const int32_t & toSrcSessionId,const int32_t & toSinkSessionId,const std::string & deviceId,uint32_t inputTypes)493 void DistributedInputSinkManager::DInputSinkListener::OnRelayStopTypeRemoteInput(const int32_t &toSrcSessionId,
494     const int32_t &toSinkSessionId, const std::string &deviceId, uint32_t inputTypes)
495 {
496     if (sinkManagerObj_ == nullptr) {
497         DHLOGE("sinkManagerObj is null.");
498         return;
499     }
500     DHLOGI("OnRelayStopTypeRemoteInput called, sessionId: %{public}d, inputTypes: %{public}d, curInputType: %{public}d",
501         toSinkSessionId, inputTypes, sinkManagerObj_->GetInputTypes());
502 
503     sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() -
504         (sinkManagerObj_->GetInputTypes() & inputTypes));
505     AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(false, inputTypes);
506     std::vector<std::string> stopIndeedDhIds;
507     sinkManagerObj_->DeleteStopDhids(toSinkSessionId, affDhIds.noSharingDhIds, stopIndeedDhIds);
508     AffectDhIds stopIndeedOnes;
509     stopIndeedOnes.noSharingDhIds = stopIndeedDhIds;
510     DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes);
511 
512     nlohmann::json jsonStr;
513     jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STOPTYPE;
514     jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes;
515     jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
516     jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId;
517     std::string smsg = jsonStr.dump();
518     DistributedInputSinkTransport::GetInstance().RespStopRemoteInput(toSinkSessionId, smsg);
519 
520     bool isAllClosed = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
521     if (!isAllClosed) {
522         DHLOGE("Not all devices are stopped.");
523         return;
524     }
525     DistributedInputSinkSwitch::GetInstance().StopAllSwitch();
526     if (DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession() ==
527         ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) {
528         DHLOGI("All session is stop.");
529         sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
530     }
531 }
532 
DeleteStopDhids(int32_t sessionId,const std::vector<std::string> stopDhIds,std::vector<std::string> & stopIndeedDhIds)533 void DistributedInputSinkManager::DeleteStopDhids(int32_t sessionId, const std::vector<std::string> stopDhIds,
534     std::vector<std::string> &stopIndeedDhIds)
535 {
536     std::lock_guard<std::mutex> lock(mutex_);
537     if (sharingDhIdsMap_.count(sessionId) <= 0) {
538         DHLOGE("DeleteStopDhids sessionId: %{public}d is not exist.", sessionId);
539         return;
540     }
541     DHLOGI("DeleteStopDhids sessionId=%{public}d before has dhid.size=%{public}zu, delDhIds.size=%{public}zu.",
542         sessionId, sharingDhIdsMap_[sessionId].size(), stopDhIds.size());
543     for (auto stopDhId : stopDhIds) {
544         sharingDhIdsMap_[sessionId].erase(stopDhId);
545     }
546     if (sharingDhIdsMap_[sessionId].size() == 0) {
547         sharingDhIdsMap_.erase(sessionId);
548         DHLOGI("DeleteStopDhids sessionId=%{public}d is delete.", sessionId);
549     } else {
550         DHLOGI("DeleteStopDhids sessionId=%{public}d after has dhid.size=%{public}zu.", sessionId,
551             sharingDhIdsMap_[sessionId].size());
552     }
553 
554     stopIndeedDhIds.assign(stopDhIds.begin(), stopDhIds.end());
555     for (auto &id : stopDhIds) {
556         sharingDhIds_.erase(id);
557     }
558 }
559 
StoreStartDhids(int32_t sessionId,const std::vector<std::string> & dhIds)560 void DistributedInputSinkManager::StoreStartDhids(int32_t sessionId, const std::vector<std::string> &dhIds)
561 {
562     std::set<std::string> tmpDhids;
563     std::lock_guard<std::mutex> lock(mutex_);
564     if (sharingDhIdsMap_.count(sessionId) > 0) {
565         tmpDhids = sharingDhIdsMap_[sessionId];
566     }
567     DHLOGI("StoreStartDhids start tmpDhids.size=%{public}zu, add dhIds.size=%{public}zu.", tmpDhids.size(),
568         dhIds.size());
569     for (auto iter : dhIds) {
570         tmpDhids.insert(iter);
571         sharingDhIds_.insert(iter);
572     }
573     sharingDhIdsMap_[sessionId] = tmpDhids;
574     DHLOGI("StoreStartDhids end tmpDhids.size=%{public}zu", tmpDhids.size());
575 }
576 
OnStart()577 void DistributedInputSinkManager::OnStart()
578 {
579     if (serviceRunningState_ == ServiceSinkRunningState::STATE_RUNNING) {
580         DHLOGI("dinput Manager Service has already started.");
581         return;
582     }
583     DHLOGI("dinput Manager Service started.");
584     if (!InitAuto()) {
585         DHLOGE("failed to init service.");
586         return;
587     }
588     if (runner_ == nullptr) {
589         DHLOGE("runner_ is nullptr.");
590         return;
591     }
592     serviceRunningState_ = ServiceSinkRunningState::STATE_RUNNING;
593     runner_->Run();
594 
595     /*
596 	 * Publish service maybe failed, so we need call this function at the last,
597      * so it can't affect the TDD test program.
598      */
599     bool ret = Publish(this);
600     if (!ret) {
601         return;
602     }
603 
604     DHLOGI("DistributedInputSinkManager  start success.");
605 }
606 
InitAuto()607 bool DistributedInputSinkManager::InitAuto()
608 {
609     runner_ = AppExecFwk::EventRunner::Create(true);
610     if (runner_ == nullptr) {
611         return false;
612     }
613 
614     handler_ = std::make_shared<DistributedInputSinkEventHandler>(runner_);
615     DHLOGI("init success");
616     return true;
617 }
618 
OnStop()619 void DistributedInputSinkManager::OnStop()
620 {
621     DHLOGI("stop service");
622     runner_.reset();
623     handler_.reset();
624     serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START;
625 }
626 
627 /*
628  * get event handler
629  *
630  * @return event handler object.
631  */
GetEventHandler()632 std::shared_ptr<DistributedInputSinkEventHandler> DistributedInputSinkManager::GetEventHandler()
633 {
634     return handler_;
635 }
636 
Init()637 int32_t DistributedInputSinkManager::Init()
638 {
639     DHLOGI("enter");
640     isStartTrans_ = DInputServerType::NULL_SERVER_TYPE;
641     // transport init session
642     int32_t ret = DistributedInputSinkTransport::GetInstance().Init();
643     if (ret != DH_SUCCESS) {
644         return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL;
645     }
646 
647     ret = DInputSinkState::GetInstance().Init();
648     if (ret != DH_SUCCESS) {
649         DHLOGE("DInputSinkState init fail!");
650         return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL;
651     }
652 
653     statuslistener_ = std::make_shared<DInputSinkListener>(this);
654     DistributedInputSinkTransport::GetInstance().RegistSinkRespCallback(statuslistener_);
655 
656     sinkMgrListener_ = std::make_shared<DInputSinkMgrListener>(this);
657     DistributedInputTransportBase::GetInstance().RegisterSinkManagerCallback(sinkMgrListener_);
658 
659     serviceRunningState_ = ServiceSinkRunningState::STATE_RUNNING;
660 
661     std::shared_ptr<DistributedHardwareFwkKit> dhFwkKit = DInputContext::GetInstance().GetDHFwkKit();
662     if (dhFwkKit == nullptr) {
663         DHLOGE("dhFwkKit obtain fail!");
664         return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL;
665     }
666     projectWindowListener_ = sptr<ProjectWindowListener>(new ProjectWindowListener(this));
667     dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, projectWindowListener_);
668     pluginStartListener_ = sptr<PluginStartListener>(new PluginStartListener());
669     dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_PHY_DEV_PLUGIN, pluginStartListener_);
670     DistributedInputCollector::GetInstance().PreInit();
671 
672     DHLOGI("init InputCollector.");
673     int result = DistributedInputCollector::GetInstance().StartCollectionThread(
674         DistributedInputSinkTransport::GetInstance().GetEventHandler());
675     if (result != DH_SUCCESS) {
676         DHLOGE("init InputCollector error.");
677     }
678 
679     return DH_SUCCESS;
680 }
681 
Release()682 int32_t DistributedInputSinkManager::Release()
683 {
684     DHLOGI("Release sink Manager.");
685     DistributedInputSinkSwitch::GetInstance().StopAllSwitch();
686     DistributedInputSinkTransport::GetInstance().CloseAllSession();
687 
688     {
689         std::lock_guard<std::mutex> lock(mutex_);
690         sharingDhIds_.clear();
691         sharingDhIdsMap_.clear();
692     }
693 
694     // notify callback servertype
695     SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
696     // Release input collect resource
697     DistributedInputCollector::GetInstance().StopCollectionThread();
698 
699     serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START;
700     std::shared_ptr<DistributedHardwareFwkKit> dhFwkKit = DInputContext::GetInstance().GetDHFwkKit();
701     if (dhFwkKit != nullptr && projectWindowListener_ != nullptr) {
702         DHLOGI("UnPublish ProjectWindowListener");
703         dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, projectWindowListener_);
704     }
705     if (dhFwkKit != nullptr && pluginStartListener_ != nullptr) {
706         DHLOGI("UnPublish PluginStartListener");
707         dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_PHY_DEV_PLUGIN, pluginStartListener_);
708     }
709     if (dhFwkKit != nullptr) {
710         DHLOGD("Disable low Latency!");
711         dhFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, DISABLE_LOW_LATENCY.dump());
712     }
713 
714     HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_EXIT, "dinput sink sa exit success.");
715     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
716     if (systemAbilityMgr == nullptr) {
717         DHLOGE("Failed to get SystemAbilityManager.");
718         return ERR_DH_INPUT_SERVER_SINK_MANAGER_RELEASE_FAIL;
719     }
720     int32_t ret = systemAbilityMgr->UnloadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID);
721     if (ret != DH_SUCCESS) {
722         DHLOGE("Failed to UnloadSystemAbility service! errcode: %{public}d.", ret);
723         return ERR_DH_INPUT_SERVER_SINK_MANAGER_RELEASE_FAIL;
724     }
725     DHLOGI("Sink unloadSystemAbility successfully.");
726     return DH_SUCCESS;
727 }
728 
RegisterGetSinkScreenInfosCallback(sptr<IGetSinkScreenInfosCallback> callback)729 int32_t DistributedInputSinkManager::RegisterGetSinkScreenInfosCallback(
730     sptr<IGetSinkScreenInfosCallback> callback)
731 {
732     DHLOGI("start");
733     if (callback != nullptr) {
734         std::lock_guard<std::mutex> lock(mutex_);
735         getSinkScreenInfosCallbacks_.insert(callback);
736     }
737     return DH_SUCCESS;
738 }
739 
GetSinkScreenInfosCbackSize()740 uint32_t DistributedInputSinkManager::GetSinkScreenInfosCbackSize()
741 {
742     return getSinkScreenInfosCallbacks_.size();
743 }
744 
GetStartTransFlag()745 DInputServerType DistributedInputSinkManager::GetStartTransFlag()
746 {
747     return isStartTrans_;
748 }
SetStartTransFlag(const DInputServerType flag)749 void DistributedInputSinkManager::SetStartTransFlag(const DInputServerType flag)
750 {
751     DHLOGI("Set Sink isStartTrans_ %{public}d", static_cast<int32_t>(flag));
752     isStartTrans_ = flag;
753 }
754 
GetInputTypes()755 uint32_t DistributedInputSinkManager::GetInputTypes()
756 {
757     return static_cast<uint32_t>(inputTypes_);
758 }
759 
SetInputTypes(const uint32_t & inputTypes)760 void DistributedInputSinkManager::SetInputTypes(const uint32_t &inputTypes)
761 {
762     inputTypes_ = static_cast<DInputDeviceType>(inputTypes);
763 }
764 
ProjectWindowListener(DistributedInputSinkManager * manager)765 DistributedInputSinkManager::ProjectWindowListener::ProjectWindowListener(DistributedInputSinkManager *manager)
766 {
767     DHLOGI("ProjectWindowListener ctor!");
768     std::lock_guard<std::mutex> lock(handleScreenMutex_);
769     sinkManagerObj_ = manager;
770     if (screen_ == nullptr) {
771         std::vector<sptr<Rosen::Screen>> screens;
772         Rosen::ScreenManager::GetInstance().GetAllScreens(screens);
773         if (screens.size() > 0) {
774             screen_ = screens[SCREEN_ID_DEFAULT];
775         }
776     }
777 }
778 
~ProjectWindowListener()779 DistributedInputSinkManager::ProjectWindowListener::~ProjectWindowListener()
780 {
781     DHLOGI("ProjectWindowListener dtor!");
782     std::lock_guard<std::mutex> lock(handleScreenMutex_);
783     sinkManagerObj_ = nullptr;
784     screen_ = nullptr;
785 }
786 
OnMessage(const DHTopic topic,const std::string & message)787 void DistributedInputSinkManager::ProjectWindowListener::OnMessage(const DHTopic topic, const std::string &message)
788 {
789     DHLOGI("ProjectWindowListener OnMessage!");
790     if (topic != DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) {
791         DHLOGE("this topic is wrong, %{public}u", static_cast<uint32_t>(topic));
792         return;
793     }
794     std::string srcDeviceId = "";
795     uint64_t srcWinId = 0;
796     SinkScreenInfo sinkScreenInfo = {};
797     int32_t parseRes = ParseMessage(message, srcDeviceId, srcWinId, sinkScreenInfo);
798     if (parseRes != DH_SUCCESS) {
799         DHLOGE("message parse failed!");
800         return;
801     }
802     int32_t saveRes = UpdateSinkScreenInfoCache(srcDeviceId, srcWinId, sinkScreenInfo);
803     if (saveRes != DH_SUCCESS) {
804         DHLOGE("Save sink screen info failed!");
805         return;
806     }
807     sptr<IRemoteObject> dScreenSinkSA = DInputContext::GetInstance().GetRemoteObject(
808         DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID);
809     sptr<DScreenSinkSvrRecipient> dScreenSinkDeathRecipient(new(std::nothrow) DScreenSinkSvrRecipient(srcDeviceId,
810         srcWinId));
811     if (dScreenSinkSA == nullptr) {
812         DHLOGE("dScreenSinkSA is nullptr");
813         return;
814     }
815     dScreenSinkSA->AddDeathRecipient(dScreenSinkDeathRecipient);
816     DInputContext::GetInstance().AddRemoteObject(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, dScreenSinkSA);
817 }
818 
ParseMessage(const std::string & message,std::string & srcDeviceId,uint64_t & srcWinId,SinkScreenInfo & sinkScreenInfo)819 int32_t DistributedInputSinkManager::ProjectWindowListener::ParseMessage(const std::string &message,
820     std::string &srcDeviceId, uint64_t &srcWinId, SinkScreenInfo &sinkScreenInfo)
821 {
822     nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false);
823     if (jsonObj.is_discarded()) {
824         DHLOGE("jsonObj parse failed!");
825         return ERR_DH_INPUT_JSON_PARSE_FAIL;
826     }
827     if (!IsString(jsonObj, SOURCE_DEVICE_ID)) {
828         DHLOGE("sourceDevId key is invalid");
829         return ERR_DH_INPUT_JSON_PARSE_FAIL;
830     }
831     srcDeviceId = jsonObj[SOURCE_DEVICE_ID].get<std::string>();
832     if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) {
833         DHLOGE("sourceWinId key is invalid");
834         return ERR_DH_INPUT_JSON_PARSE_FAIL;
835     }
836     srcWinId = jsonObj[SOURCE_WINDOW_ID].get<uint64_t>();
837     if (!IsUInt64(jsonObj, SINK_SHOW_WINDOW_ID)) {
838         DHLOGE("sinkWinId key is invalid");
839         return ERR_DH_INPUT_JSON_PARSE_FAIL;
840     }
841     sinkScreenInfo.sinkShowWinId = jsonObj[SINK_SHOW_WINDOW_ID].get<uint64_t>();
842     if (!IsUInt32(jsonObj, SINK_PROJECT_SHOW_WIDTH)) {
843         DHLOGE("sourceWinHeight key is invalid");
844         return ERR_DH_INPUT_JSON_PARSE_FAIL;
845     }
846     sinkScreenInfo.sinkProjShowWidth = jsonObj[SINK_PROJECT_SHOW_WIDTH].get<std::uint32_t>();
847     if (!IsUInt32(jsonObj, SINK_PROJECT_SHOW_HEIGHT)) {
848         DHLOGE("sourceWinHeight key is invalid");
849         return ERR_DH_INPUT_JSON_PARSE_FAIL;
850     }
851     sinkScreenInfo.sinkProjShowHeight = jsonObj[SINK_PROJECT_SHOW_HEIGHT].get<std::uint32_t>();
852     if (!IsUInt32(jsonObj, SINK_WINDOW_SHOW_X)) {
853         DHLOGE("sourceWinHeight key is invalid");
854         return ERR_DH_INPUT_JSON_PARSE_FAIL;
855     }
856     sinkScreenInfo.sinkWinShowX = jsonObj[SINK_WINDOW_SHOW_X].get<std::uint32_t>();
857     if (!IsUInt32(jsonObj, SINK_WINDOW_SHOW_Y)) {
858         DHLOGE("sourceWinHeight key is invalid");
859         return ERR_DH_INPUT_JSON_PARSE_FAIL;
860     }
861     sinkScreenInfo.sinkWinShowY = jsonObj[SINK_WINDOW_SHOW_Y].get<std::uint32_t>();
862     return DH_SUCCESS;
863 }
864 
UpdateSinkScreenInfoCache(const std::string & srcDevId,const uint64_t srcWinId,const SinkScreenInfo & sinkScreenInfoTmp)865 int32_t DistributedInputSinkManager::ProjectWindowListener::UpdateSinkScreenInfoCache(const std::string &srcDevId,
866     const uint64_t srcWinId, const SinkScreenInfo &sinkScreenInfoTmp)
867 {
868     if (sinkManagerObj_ == nullptr) {
869         DHLOGE("sinkManagerObj is null.");
870         return ERR_DH_INPUT_SERVER_SINK_MANAGER_IS_NULL;
871     }
872     std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, srcWinId);
873     SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(srcScreenInfoKey);
874     sinkScreenInfo.sinkShowWinId = sinkScreenInfoTmp.sinkShowWinId;
875     sinkScreenInfo.sinkProjShowWidth = sinkScreenInfoTmp.sinkProjShowWidth;
876     sinkScreenInfo.sinkProjShowHeight = sinkScreenInfoTmp.sinkProjShowHeight;
877     sinkScreenInfo.sinkWinShowX = sinkScreenInfoTmp.sinkWinShowX;
878     sinkScreenInfo.sinkWinShowY = sinkScreenInfoTmp.sinkWinShowY;
879     sinkScreenInfo.sinkShowWidth = GetScreenWidth();
880     sinkScreenInfo.sinkShowHeight = GetScreenHeight();
881     LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo;
882     sinkScreenInfo.sinkPhyWidth = static_cast<uint32_t>(info.absMtPositionXMax + 1);
883     sinkScreenInfo.sinkPhyHeight = static_cast<uint32_t>(info.absMtPositionYMax + 1);
884     DHLOGI("sinkShowWinId: %{public}" PRIu64 ", sinkProjShowWidth: %{public}d, sinkProjShowHeight: %{public}d, "
885         "sinkWinShowX: %{public}d, sinkWinShowY: %{public}d, sinkShowWidth: %{public}d, sinkShowHeight: %{public}d, "
886         "sinkPhyWidth: %{public}d, sinkPhyHeight: %{public}d", sinkScreenInfo.sinkShowWinId,
887         sinkScreenInfo.sinkProjShowWidth, sinkScreenInfo.sinkProjShowHeight, sinkScreenInfo.sinkWinShowX,
888         sinkScreenInfo.sinkWinShowY, sinkScreenInfo.sinkShowWidth, sinkScreenInfo.sinkShowHeight,
889         sinkScreenInfo.sinkPhyWidth, sinkScreenInfo.sinkPhyHeight);
890     int32_t ret = DInputContext::GetInstance().UpdateSinkScreenInfo(srcScreenInfoKey, sinkScreenInfo);
891     std::lock_guard<std::mutex> lock(sinkManagerObj_->mutex_);
892     if ((ret == DH_SUCCESS) && (sinkManagerObj_->GetSinkScreenInfosCbackSize() > 0)) {
893         sinkManagerObj_->CallBackScreenInfoChange();
894     }
895     return ret;
896 }
897 
GetScreenWidth()898 uint32_t DistributedInputSinkManager::ProjectWindowListener::GetScreenWidth()
899 {
900     std::lock_guard<std::mutex> lock(handleScreenMutex_);
901     if (screen_ == nullptr) {
902         DHLOGE("screen is nullptr!");
903         return DEFAULT_VALUE;
904     }
905     return screen_->GetWidth();
906 }
907 
GetScreenHeight()908 uint32_t DistributedInputSinkManager::ProjectWindowListener::GetScreenHeight()
909 {
910     std::lock_guard<std::mutex> lock(handleScreenMutex_);
911     if (screen_ == nullptr) {
912         DHLOGE("screen is nullptr!");
913         return DEFAULT_VALUE;
914     }
915     return screen_->GetHeight();
916 }
917 
~PluginStartListener()918 DistributedInputSinkManager::PluginStartListener::~PluginStartListener()
919 {
920     DHLOGI("PluginStartListener dtor!");
921 }
922 
OnMessage(const DHTopic topic,const std::string & message)923 void DistributedInputSinkManager::PluginStartListener::OnMessage(const DHTopic topic,
924     const std::string &message)
925 {
926     DHLOGI("PluginStartListener OnMessage!");
927     if (topic != DHTopic::TOPIC_PHY_DEV_PLUGIN) {
928         DHLOGE("this topic is wrong, %{public}u", static_cast<uint32_t>(topic));
929         return;
930     }
931     if (message.empty()) {
932         DHLOGE("this message is empty");
933         return;
934     }
935     DistributedInputCollector::GetInstance().ClearSkipDevicePaths();
936 }
937 
DScreenSinkSvrRecipient(const std::string & srcDevId,const uint64_t srcWinId)938 DistributedInputSinkManager::DScreenSinkSvrRecipient::DScreenSinkSvrRecipient(const std::string &srcDevId,
939     const uint64_t srcWinId)
940 {
941     DHLOGI("DScreenStatusListener ctor!");
942     this->srcDevId_ = srcDevId;
943     this->srcWinId_ = srcWinId;
944 }
945 
~DScreenSinkSvrRecipient()946 DistributedInputSinkManager::DScreenSinkSvrRecipient::~DScreenSinkSvrRecipient()
947 {
948     DHLOGI("DScreenStatusListener dtor!");
949 }
950 
OnRemoteDied(const wptr<IRemoteObject> & remote)951 void DistributedInputSinkManager::DScreenSinkSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
952 {
953     DHLOGI("DScreenSinkSvrRecipient OnRemoteDied");
954     sptr<IRemoteObject> remoteObject = remote.promote();
955     if (!remoteObject) {
956         DHLOGE("OnRemoteDied remote promoted failed");
957         return;
958     }
959     std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId_, srcWinId_);
960     DInputContext::GetInstance().RemoveSinkScreenInfo(srcScreenInfoKey);
961     DInputContext::GetInstance().RemoveRemoteObject(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID);
962 }
963 
NotifyStartDScreen(const SrcScreenInfo & srcScreenInfo)964 int32_t DistributedInputSinkManager::NotifyStartDScreen(const SrcScreenInfo &srcScreenInfo)
965 {
966     DHLOGI("NotifyStartDScreen start!");
967 
968     CleanExceptionalInfo(srcScreenInfo);
969 
970     std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcScreenInfo.devId,
971         srcScreenInfo.sourceWinId);
972     SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(screenInfoKey);
973     sinkScreenInfo.srcScreenInfo = srcScreenInfo;
974     DHLOGI("OnRemoteRequest the data: devId: %{public}s, sourceWinId: %{public}" PRIu64 ", sourceWinWidth: %{public}d, "
975         "sourceWinHeight: %{public}d, sourcePhyId: %{public}s, sourcePhyFd: %{public}d, sourcePhyWidth: %{public}d, "
976         "sourcePhyHeight: %{public}d", GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId,
977         srcScreenInfo.sourceWinWidth, srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(),
978         srcScreenInfo.sourcePhyFd, srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight);
979     int32_t ret = DInputContext::GetInstance().UpdateSinkScreenInfo(screenInfoKey, sinkScreenInfo);
980     std::lock_guard<std::mutex> lock(mutex_);
981     if ((ret == DH_SUCCESS) && (getSinkScreenInfosCallbacks_.size() > 0)) {
982         CallBackScreenInfoChange();
983     }
984     return ret;
985 }
986 
CallBackScreenInfoChange()987 void DistributedInputSinkManager::CallBackScreenInfoChange()
988 {
989     DHLOGI("start!");
990     std::vector<std::vector<uint32_t>> transInfos;
991     auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo();
992     std::vector<uint32_t> info;
993     for (const auto &[id, sinkInfo] : sinkInfos) {
994         info.clear();
995         info.emplace_back(sinkInfo.transformInfo.sinkWinPhyX);
996         info.emplace_back(sinkInfo.transformInfo.sinkWinPhyY);
997         info.emplace_back(sinkInfo.transformInfo.sinkProjPhyWidth);
998         info.emplace_back(sinkInfo.transformInfo.sinkProjPhyHeight);
999         transInfos.emplace_back(info);
1000     }
1001     nlohmann::json screenMsg(transInfos);
1002     std::string str = screenMsg.dump();
1003     for (const auto &iter : getSinkScreenInfosCallbacks_) {
1004         iter->OnResult(str);
1005     }
1006 }
1007 
CleanExceptionalInfo(const SrcScreenInfo & srcScreenInfo)1008 void DistributedInputSinkManager::CleanExceptionalInfo(const SrcScreenInfo &srcScreenInfo)
1009 {
1010     DHLOGI("CleanExceptionalInfo start!");
1011     std::string uuid = srcScreenInfo.uuid;
1012     int32_t sessionId = srcScreenInfo.sessionId;
1013     auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo();
1014 
1015     for (const auto &[id, sinkInfo] : sinkInfos) {
1016         auto srcInfo = sinkInfo.srcScreenInfo;
1017         if ((std::strcmp(srcInfo.uuid.c_str(), uuid.c_str()) == 0) && (srcInfo.sessionId != sessionId)) {
1018             DInputContext::GetInstance().RemoveSinkScreenInfo(id);
1019             DHLOGI("CleanExceptionalInfo screenInfoKey: %{public}s, sessionId: %{public}d", id.c_str(), sessionId);
1020         }
1021     }
1022 }
1023 
NotifyStopDScreen(const std::string & srcScreenInfoKey)1024 int32_t DistributedInputSinkManager::NotifyStopDScreen(const std::string &srcScreenInfoKey)
1025 {
1026     DHLOGI("NotifyStopDScreen start, srcScreenInfoKey: %{public}s", GetAnonyString(srcScreenInfoKey).c_str());
1027     if (srcScreenInfoKey.empty()) {
1028         DHLOGE("srcScreenInfoKey is empty, srcScreenInfoKey: %{public}s", GetAnonyString(srcScreenInfoKey).c_str());
1029         return ERR_DH_INPUT_SERVER_SINK_SCREEN_INFO_IS_EMPTY;
1030     }
1031     return DInputContext::GetInstance().RemoveSinkScreenInfo(srcScreenInfoKey);
1032 }
1033 
RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)1034 int32_t DistributedInputSinkManager::RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)
1035 {
1036     DHLOGI("RegisterSharingDhIdListener");
1037     DistributedInputCollector::GetInstance().RegisterSharingDhIdListener(sharingDhIdListener);
1038     return DH_SUCCESS;
1039 }
1040 
Dump(int32_t fd,const std::vector<std::u16string> & args)1041 int32_t DistributedInputSinkManager::Dump(int32_t fd, const std::vector<std::u16string> &args)
1042 {
1043     DHLOGI("DistributedInputSinkManager Dump.");
1044     std::vector<std::string> argsStr(args.size());
1045     std::transform(args.begin(), args.end(), argsStr.begin(), [](const auto &item) {return Str16ToStr8(item);});
1046     std::string result("");
1047     if (!HiDumper::GetInstance().HiDump(argsStr, result)) {
1048         DHLOGI("Hidump error.");
1049         return ERR_DH_INPUT_HIDUMP_DUMP_PROCESS_FAIL;
1050     }
1051 
1052     int32_t ret = dprintf(fd, "%s\n", result.c_str());
1053     if (ret < 0) {
1054         DHLOGE("dprintf error.");
1055         return ERR_DH_INPUT_HIDUMP_DPRINTF_FAIL;
1056     }
1057     return DH_SUCCESS;
1058 }
1059 } // namespace DistributedInput
1060 } // namespace DistributedHardware
1061 } // namespace OHOS
1062