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