• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "input_device_cooperate_sm.h"
17 
18 #include <cstdio>
19 
20 #include "device_manager.h"
21 #include "hitrace_meter.h"
22 
23 #include "bytrace_adapter.h"
24 #include "cooperate_event_manager.h"
25 #include "cooperation_message.h"
26 #include "define_multimodal.h"
27 #include "device_cooperate_softbus_adapter.h"
28 #include "device_profile_adapter.h"
29 #include "i_pointer_drawing_manager.h"
30 #include "input_device_cooperate_state_free.h"
31 #include "input_device_cooperate_state_in.h"
32 #include "input_device_cooperate_state_out.h"
33 #include "input_device_cooperate_util.h"
34 #include "input_device_manager.h"
35 #include "key_auto_repeat.h"
36 #include "mouse_event_normalize.h"
37 #include "timer_manager.h"
38 #include "util_ex.h"
39 
40 namespace OHOS {
41 namespace MMI {
42 namespace {
43 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceCooperateSM" };
44 constexpr int32_t INTERVAL_MS = 2000;
45 constexpr int32_t MOUSE_ABS_LOCATION = 100;
46 constexpr int32_t MOUSE_ABS_LOCATION_X = 50;
47 constexpr int32_t MOUSE_ABS_LOCATION_Y = 50;
48 } // namespace
49 
InputDeviceCooperateSM()50 InputDeviceCooperateSM::InputDeviceCooperateSM() {}
~InputDeviceCooperateSM()51 InputDeviceCooperateSM::~InputDeviceCooperateSM() {}
52 
Init(DelegateTasksCallback delegateTasksCallback)53 void InputDeviceCooperateSM::Init(DelegateTasksCallback delegateTasksCallback)
54 {
55     CHKPL(delegateTasksCallback);
56     delegateTasksCallback_ = delegateTasksCallback;
57     preparedNetworkId_ = std::make_pair("", "");
58     currentStateSM_ = std::make_shared<InputDeviceCooperateStateFree>();
59     DevCooperateSoftbusAdapter->Init();
60     TimerMgr->AddTimer(INTERVAL_MS, 1, [this]() {
61         this->InitDeviceManager();
62     });
63 }
64 
Reset(const std::string & networkId)65 void InputDeviceCooperateSM::Reset(const std::string &networkId)
66 {
67     CALL_INFO_TRACE;
68     std::lock_guard<std::mutex> guard(mutex_);
69     bool needReset = true;
70     if (cooperateState_ == CooperateState::STATE_OUT) {
71         if (networkId != srcNetworkId_) {
72             needReset = false;
73         }
74     }
75     if (cooperateState_ == CooperateState::STATE_IN) {
76         std::string sinkNetwoekId = InputDevMgr->GetOriginNetworkId(startDhid_);
77         if (networkId != sinkNetwoekId) {
78             needReset = false;
79         }
80     }
81     if (needReset) {
82         preparedNetworkId_ = std::make_pair("", "");
83         Reset(true);
84     }
85 }
86 
Reset(bool adjustAbsolutionLocation)87 void InputDeviceCooperateSM::Reset(bool adjustAbsolutionLocation)
88 {
89     CALL_INFO_TRACE;
90     startDhid_ = "";
91     srcNetworkId_ = "";
92     currentStateSM_ = std::make_shared<InputDeviceCooperateStateFree>();
93     cooperateState_ = CooperateState::STATE_FREE;
94     bool hasPointer = InputDevMgr->HasLocalPointerDevice();
95     if (hasPointer && adjustAbsolutionLocation) {
96         MouseEventHdr->SetAbsolutionLocation(MOUSE_ABS_LOCATION_X, MOUSE_ABS_LOCATION_Y);
97     } else {
98         IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), hasPointer);
99     }
100     isStarting_ = false;
101     isStopping_ = false;
102 }
103 
OnCooperateChanged(const std::string & networkId,bool isOpen)104 void InputDeviceCooperateSM::OnCooperateChanged(const std::string &networkId, bool isOpen)
105 {
106     CALL_DEBUG_ENTER;
107     CooperationMessage msg = isOpen ? CooperationMessage::STATE_ON : CooperationMessage::STATE_OFF;
108     delegateTasksCallback_(std::bind(&CooperateEventManager::OnCooperateMessage, CooperateEventMgr, msg, networkId));
109     if (!isOpen) {
110         OnCloseCooperation(networkId, false);
111     }
112 }
113 
OnCloseCooperation(const std::string & networkId,bool isLocal)114 void InputDeviceCooperateSM::OnCloseCooperation(const std::string &networkId, bool isLocal)
115 {
116     CALL_INFO_TRACE;
117     std::lock_guard<std::mutex> guard(mutex_);
118     if (!preparedNetworkId_.first.empty() && !preparedNetworkId_.second.empty()) {
119         if (networkId == preparedNetworkId_.first || networkId == preparedNetworkId_.second) {
120             if (cooperateState_ != CooperateState::STATE_FREE) {
121                 auto  dhids = InputDevMgr->GetCooperateDhids(startDhid_);
122                 DistributedAdapter->StopRemoteInput(preparedNetworkId_.first, preparedNetworkId_.second,
123                     dhids, [](bool isSuccess) {
124                     MMI_HILOGI("Failed to stop remote");
125                 });
126             }
127             DistributedAdapter->UnPrepareRemoteInput(preparedNetworkId_.first, preparedNetworkId_.second,
128                 [](bool isSuccess) {});
129         }
130     }
131     preparedNetworkId_ = std::make_pair("", "");
132     if (cooperateState_ == CooperateState::STATE_FREE) {
133         return;
134     }
135     if (isLocal || networkId == srcNetworkId_) {
136         Reset(true);
137         return;
138     }
139     std::string originNetworkId = InputDevMgr->GetOriginNetworkId(startDhid_);
140     if (originNetworkId == networkId) {
141         Reset();
142     }
143 }
144 
SetVirtualKeyBoardDevId(int32_t deviceId)145 void InputDeviceCooperateSM::SetVirtualKeyBoardDevId(int32_t deviceId)
146 {
147     virtualKeyBoardId_ = deviceId;
148     MMI_HILOGI("virtualKeyBoardId_ has been set to%{public}d", virtualKeyBoardId_);
149 }
150 
GetVirtualKeyBoardDevId()151 int32_t InputDeviceCooperateSM::GetVirtualKeyBoardDevId()
152 {
153     return virtualKeyBoardId_;
154 }
155 
GetCooperateState(const std::string & deviceId)156 void InputDeviceCooperateSM::GetCooperateState(const std::string &deviceId)
157 {
158     CALL_INFO_TRACE;
159     bool state = DProfileAdapter->GetCrossingSwitchState(deviceId);
160     CooperateEventMgr->OnGetState(state);
161 }
162 
EnableInputDeviceCooperate(bool enabled)163 void InputDeviceCooperateSM::EnableInputDeviceCooperate(bool enabled)
164 {
165     CALL_INFO_TRACE;
166     if (enabled) {
167         BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::START_EVENT);
168         DProfileAdapter->UpdateCrossingSwitchState(enabled, onlineDevice_);
169         BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::START_EVENT);
170     } else {
171         DProfileAdapter->UpdateCrossingSwitchState(enabled, onlineDevice_);
172         std::string localNetworkId = GetLocalDeviceId();
173         OnCloseCooperation(localNetworkId, true);
174     }
175 }
176 
StartInputDeviceCooperate(const std::string & remoteNetworkId,int32_t startInputDeviceId)177 int32_t InputDeviceCooperateSM::StartInputDeviceCooperate(
178     const std::string &remoteNetworkId, int32_t startInputDeviceId)
179 {
180     CALL_INFO_TRACE;
181     std::lock_guard<std::mutex> guard(mutex_);
182     if (isStarting_) {
183         MMI_HILOGE("In transition state, not process");
184         return static_cast<int32_t>(CooperationMessage::COOPERATE_FAIL);
185     }
186     CHKPR(currentStateSM_, ERROR_NULL_POINTER);
187     BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::LAUNCH_EVENT);
188     isStarting_ = true;
189     DevCooperateSoftbusAdapter->OpenInputSoftbus(remoteNetworkId);
190     int32_t ret = currentStateSM_->StartInputDeviceCooperate(remoteNetworkId, startInputDeviceId);
191     if (ret != RET_OK) {
192         MMI_HILOGE("Start remote input fail");
193         BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::LAUNCH_EVENT);
194         isStarting_ = false;
195         return ret;
196     }
197     UpdateMouseLocation();
198     if (cooperateState_ == CooperateState::STATE_FREE) {
199         srcNetworkId_ = remoteNetworkId;
200     }
201     return ret;
202 }
203 
StopInputDeviceCooperate()204 int32_t InputDeviceCooperateSM::StopInputDeviceCooperate()
205 {
206     CALL_INFO_TRACE;
207     std::lock_guard<std::mutex> guard(mutex_);
208     if (isStopping_) {
209         MMI_HILOGE("In transition state, not process");
210         return RET_ERR;
211     }
212     CHKPR(currentStateSM_, ERROR_NULL_POINTER);
213     BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::STOP_EVENT);
214     isStopping_ = true;
215     std::string stopNetworkId = "";
216     if (cooperateState_ == CooperateState::STATE_IN) {
217         stopNetworkId = InputDevMgr->GetOriginNetworkId(startDhid_);
218     }
219     if (cooperateState_ == CooperateState::STATE_OUT) {
220         stopNetworkId = srcNetworkId_;
221     }
222     int32_t ret = currentStateSM_->StopInputDeviceCooperate(stopNetworkId);
223     if (ret != RET_OK) {
224         MMI_HILOGE("Stop input device cooperate fail");
225         BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::STOP_EVENT);
226         isStopping_ = false;
227     }
228     return ret;
229 }
230 
StartRemoteCooperate(const std::string & remoteNetworkId)231 void InputDeviceCooperateSM::StartRemoteCooperate(const std::string &remoteNetworkId)
232 {
233     CALL_INFO_TRACE;
234     std::lock_guard<std::mutex> guard(mutex_);
235     CHKPV(delegateTasksCallback_);
236     delegateTasksCallback_(std::bind(&CooperateEventManager::OnCooperateMessage,
237         CooperateEventMgr, CooperationMessage::INFO_START, remoteNetworkId));
238     isStarting_ = true;
239 }
240 
StartRemoteCooperateResult(bool isSuccess,const std::string & startDhid,int32_t xPercent,int32_t yPercent)241 void InputDeviceCooperateSM::StartRemoteCooperateResult(bool isSuccess,
242     const std::string& startDhid, int32_t xPercent, int32_t yPercent)
243 {
244     CALL_INFO_TRACE;
245     std::lock_guard<std::mutex> guard(mutex_);
246     if (!isStarting_) {
247         MMI_HILOGI("Not in starting");
248         return;
249     }
250     startDhid_ = startDhid;
251     CooperationMessage msg =
252             isSuccess ? CooperationMessage::INFO_SUCCESS : CooperationMessage::INFO_FAIL;
253     delegateTasksCallback_(std::bind(&CooperateEventManager::OnCooperateMessage, CooperateEventMgr, msg, ""));
254 
255     if (!isSuccess || cooperateState_ == CooperateState::STATE_IN) {
256         isStarting_ = false;
257         return;
258     }
259     if (cooperateState_ == CooperateState::STATE_FREE) {
260         MouseEventHdr->SetAbsolutionLocation(MOUSE_ABS_LOCATION - xPercent, yPercent);
261         UpdateState(CooperateState::STATE_IN);
262         InputDevMgr->NotifyVirtualKeyBoardStatus(GetVirtualKeyBoardDevId(), true);
263     }
264     if (cooperateState_ == CooperateState::STATE_OUT) {
265         MouseEventHdr->SetAbsolutionLocation(MOUSE_ABS_LOCATION - xPercent, yPercent);
266         UpdateState(CooperateState::STATE_FREE);
267     }
268     isStarting_ = false;
269 }
270 
StopRemoteCooperate()271 void InputDeviceCooperateSM::StopRemoteCooperate()
272 {
273     CALL_INFO_TRACE;
274     std::lock_guard<std::mutex> guard(mutex_);
275     isStopping_ = true;
276 }
277 
StopRemoteCooperateResult(bool isSuccess)278 void InputDeviceCooperateSM::StopRemoteCooperateResult(bool isSuccess)
279 {
280     CALL_INFO_TRACE;
281     std::lock_guard<std::mutex> guard(mutex_);
282     if (!isStopping_) {
283         MMI_HILOGI("Not in stopping");
284         return;
285     }
286     if (isSuccess) {
287         Reset(true);
288     }
289     KeyRepeat->RemoveTimer();
290     isStopping_ = false;
291 }
292 
StartCooperateOtherResult(const std::string & srcNetworkId)293 void InputDeviceCooperateSM::StartCooperateOtherResult(const std::string& srcNetworkId)
294 {
295     CALL_INFO_TRACE;
296     std::lock_guard<std::mutex> guard(mutex_);
297     srcNetworkId_ = srcNetworkId;
298 }
299 
OnStartFinish(bool isSuccess,const std::string & remoteNetworkId,int32_t startInputDeviceId)300 void InputDeviceCooperateSM::OnStartFinish(bool isSuccess,
301     const std::string &remoteNetworkId, int32_t startInputDeviceId)
302 {
303     CALL_INFO_TRACE;
304     std::lock_guard<std::mutex> guard(mutex_);
305     if (!isStarting_) {
306         MMI_HILOGE("Not in starting");
307         return;
308     }
309 
310     BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::LAUNCH_EVENT);
311     if (!isSuccess) {
312         MMI_HILOGE("Start distributed fail, startInputDevice: %{public}d", startInputDeviceId);
313         NotifyRemoteStartFail(remoteNetworkId);
314     } else {
315         startDhid_ = InputDevMgr->GetDhid(startInputDeviceId);
316         NotifyRemoteStartSuccess(remoteNetworkId, startDhid_);
317         if (cooperateState_ == CooperateState::STATE_FREE) {
318             UpdateState(CooperateState::STATE_OUT);
319         } else if (cooperateState_ == CooperateState::STATE_IN) {
320             std::string sink = InputDevMgr->GetOriginNetworkId(startInputDeviceId);
321             if (!sink.empty() && remoteNetworkId != sink) {
322                 DevCooperateSoftbusAdapter->StartCooperateOtherResult(sink, remoteNetworkId);
323             }
324             UpdateState(CooperateState::STATE_FREE);
325             InputDevMgr->NotifyVirtualKeyBoardStatus(GetVirtualKeyBoardDevId(), false);
326             KeyRepeat->RemoveTimer();
327         } else {
328             MMI_HILOGI("Current state is out");
329         }
330     }
331     isStarting_ = false;
332 }
333 
OnStopFinish(bool isSuccess,const std::string & remoteNetworkId)334 void InputDeviceCooperateSM::OnStopFinish(bool isSuccess, const std::string &remoteNetworkId)
335 {
336     CALL_INFO_TRACE;
337     std::lock_guard<std::mutex> guard(mutex_);
338     if (!isStopping_) {
339         MMI_HILOGE("Not in stopping");
340         return;
341     }
342     BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::STOP_EVENT);
343     NotifyRemoteStopFinish(isSuccess, remoteNetworkId);
344     if (isSuccess) {
345         if (InputDevMgr->HasLocalPointerDevice()) {
346             MouseEventHdr->SetAbsolutionLocation(MOUSE_ABS_LOCATION_X, MOUSE_ABS_LOCATION_Y);
347         }
348         if (cooperateState_ == CooperateState::STATE_IN || cooperateState_ == CooperateState::STATE_OUT) {
349             UpdateState(CooperateState::STATE_FREE);
350         } else {
351             MMI_HILOGI("Current state is free");
352         }
353     }
354     DevCooperateSoftbusAdapter->CloseInputSoftbus(remoteNetworkId);
355     isStopping_ = false;
356 }
357 
NotifyRemoteStartFail(const std::string & remoteNetworkId)358 void InputDeviceCooperateSM::NotifyRemoteStartFail(const std::string &remoteNetworkId)
359 {
360     CALL_DEBUG_ENTER;
361     DevCooperateSoftbusAdapter->StartRemoteCooperateResult(remoteNetworkId, false, "",  0, 0);
362     CooperateEventMgr->OnStart(CooperationMessage::INFO_FAIL);
363 }
364 
NotifyRemoteStartSuccess(const std::string & remoteNetworkId,const std::string & startDhid)365 void InputDeviceCooperateSM::NotifyRemoteStartSuccess(const std::string &remoteNetworkId, const std::string& startDhid)
366 {
367     CALL_DEBUG_ENTER;
368     DevCooperateSoftbusAdapter->StartRemoteCooperateResult(remoteNetworkId,
369         true, startDhid, mouseLocation_.first, mouseLocation_.second);
370     CooperateEventMgr->OnStart(CooperationMessage::INFO_SUCCESS);
371 }
372 
NotifyRemoteStopFinish(bool isSuccess,const std::string & remoteNetworkId)373 void InputDeviceCooperateSM::NotifyRemoteStopFinish(bool isSuccess, const std::string &remoteNetworkId)
374 {
375     CALL_DEBUG_ENTER;
376     DevCooperateSoftbusAdapter->StopRemoteCooperateResult(remoteNetworkId, isSuccess);
377     if (!isSuccess) {
378         CooperateEventMgr->OnStop(CooperationMessage::COOPERATE_FAIL);
379     } else {
380         CooperateEventMgr->OnStop(CooperationMessage::STOP_SUCCESS);
381     }
382 }
383 
UpdateMouseLocation()384 bool InputDeviceCooperateSM::UpdateMouseLocation()
385 {
386     CALL_DEBUG_ENTER;
387     auto pointerEvent = MouseEventHdr->GetPointerEvent();
388     CHKPF(pointerEvent);
389     int32_t displayId = pointerEvent->GetTargetDisplayId();
390     auto displayGroupInfo =  WinMgr->GetDisplayGroupInfo();
391     struct DisplayInfo physicalDisplayInfo;
392     for (auto &it : displayGroupInfo.displaysInfo) {
393         if (it.id == displayId) {
394             physicalDisplayInfo = it;
395             break;
396         }
397     }
398     int32_t displayWidth = physicalDisplayInfo.width;
399     int32_t displayHeight = physicalDisplayInfo.height;
400     if (displayWidth == 0 || displayHeight == 0) {
401         MMI_HILOGE("display width or height is 0");
402         return false;
403     }
404     auto mouseInfo = WinMgr->GetMouseInfo();
405     int32_t xPercent = mouseInfo.physicalX * MOUSE_ABS_LOCATION / displayWidth;
406     int32_t yPercent = mouseInfo.physicalY * MOUSE_ABS_LOCATION / displayHeight;
407     MMI_HILOGI("displayWidth: %{public}d, displayHeight: %{public}d, physicalX: %{public}d, physicalY: %{public}d,",
408         displayWidth, displayHeight, mouseInfo.physicalX, mouseInfo.physicalY);
409     mouseLocation_ = std::make_pair(xPercent, yPercent);
410     return true;
411 }
412 
UpdateState(CooperateState state)413 void InputDeviceCooperateSM::UpdateState(CooperateState state)
414 {
415     MMI_HILOGI("state: %{public}d", state);
416     switch (state) {
417         case CooperateState::STATE_FREE: {
418             Reset();
419             break;
420         }
421         case CooperateState::STATE_IN: {
422             currentStateSM_ = std::make_shared<InputDeviceCooperateStateIn>(startDhid_);
423             break;
424         }
425         case CooperateState::STATE_OUT: {
426             IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), false);
427             currentStateSM_ = std::make_shared<InputDeviceCooperateStateOut>(startDhid_);
428             break;
429         }
430         default:
431             break;
432     }
433     cooperateState_ = state;
434 }
435 
GetCurrentCooperateState() const436 CooperateState InputDeviceCooperateSM::GetCurrentCooperateState() const
437 {
438     CALL_DEBUG_ENTER;
439     std::lock_guard<std::mutex> guard(mutex_);
440     return cooperateState_;
441 }
442 
UpdatePreparedDevices(const std::string & srcNetworkId,const std::string & sinkNetworkId)443 void InputDeviceCooperateSM::UpdatePreparedDevices(const std::string &srcNetworkId, const std::string &sinkNetworkId)
444 {
445     CALL_DEBUG_ENTER;
446     std::lock_guard<std::mutex> guard(mutex_);
447     preparedNetworkId_ = std::make_pair(srcNetworkId, sinkNetworkId);
448 }
449 
GetPreparedDevices() const450 std::pair<std::string, std::string> InputDeviceCooperateSM::GetPreparedDevices() const
451 {
452     CALL_DEBUG_ENTER;
453     return preparedNetworkId_;
454 }
455 
IsStarting() const456 bool InputDeviceCooperateSM::IsStarting() const
457 {
458     std::lock_guard<std::mutex> guard(mutex_);
459     return isStarting_;
460 }
461 
IsStopping() const462 bool InputDeviceCooperateSM::IsStopping() const
463 {
464     std::lock_guard<std::mutex> guard(mutex_);
465     return isStopping_;
466 }
467 
OnKeyboardOnline(const std::string & dhid)468 void InputDeviceCooperateSM::OnKeyboardOnline(const std::string &dhid)
469 {
470     CALL_INFO_TRACE;
471     std::lock_guard<std::mutex> guard(mutex_);
472     CHKPV(currentStateSM_);
473     currentStateSM_->OnKeyboardOnline(dhid);
474 }
475 
OnPointerOffline(const std::string & dhid,const std::string & sinkNetworkId,const std::vector<std::string> & keyboards)476 void InputDeviceCooperateSM::OnPointerOffline(const std::string &dhid, const std::string &sinkNetworkId,
477     const std::vector<std::string> &keyboards)
478 {
479     CALL_INFO_TRACE;
480     std::lock_guard<std::mutex> guard(mutex_);
481     if (cooperateState_ == CooperateState::STATE_FREE) {
482         Reset();
483         return;
484     }
485     if (cooperateState_ == CooperateState::STATE_IN && startDhid_ == dhid) {
486         Reset();
487         return;
488     }
489     if (cooperateState_ == CooperateState::STATE_OUT && startDhid_ == dhid) {
490         std::string src = srcNetworkId_;
491         if (src.empty()) {
492             src = preparedNetworkId_.first;
493         }
494         DistributedAdapter->StopRemoteInput(src, sinkNetworkId, keyboards, [this, src](bool isSuccess) {});
495         Reset();
496     }
497 }
498 
HandleEvent(libinput_event * event)499 void InputDeviceCooperateSM::HandleEvent(libinput_event *event)
500 {
501     MMI_HILOGI("current state :%{public}d", cooperateState_);
502     CHKPV(event);
503     auto type = libinput_event_get_type(event);
504     switch (type) {
505         case LIBINPUT_EVENT_POINTER_MOTION:
506         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
507         case LIBINPUT_EVENT_POINTER_BUTTON:
508         case LIBINPUT_EVENT_POINTER_AXIS: {
509             CheckPointerEvent(event);
510             break;
511         }
512         default: {
513             auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
514             CHKPV(inputEventNormalizeHandler);
515             inputEventNormalizeHandler->HandleEvent(event);
516             break;
517         }
518     }
519 }
520 
CheckPointerEvent(struct libinput_event * event)521 void InputDeviceCooperateSM::CheckPointerEvent(struct libinput_event *event)
522 {
523     std::lock_guard<std::mutex> guard(mutex_);
524     if (isStopping_ || isStarting_) {
525         MMI_HILOGE("In transition state, not process");
526         return;
527     }
528     auto inputDevice = libinput_event_get_device(event);
529     if (cooperateState_ == CooperateState::STATE_IN) {
530         if (!InputDevMgr->IsRemote(inputDevice)) {
531             CHKPV(currentStateSM_);
532             isStopping_ = true;
533             std::string sink = InputDevMgr->GetOriginNetworkId(startDhid_);
534             int32_t ret = currentStateSM_->StopInputDeviceCooperate(sink);
535             if (ret != RET_OK) {
536                 MMI_HILOGE("Stop input device cooperate fail");
537                 isStopping_ = false;
538             }
539             return;
540         }
541     } else if (cooperateState_ == CooperateState::STATE_OUT) {
542         int32_t deviceId = InputDevMgr->FindInputDeviceId(inputDevice);
543         std::string dhid = InputDevMgr->GetDhid(deviceId);
544         if (startDhid_ != dhid) {
545             MMI_HILOGI("Move other mouse, stop input device cooperate");
546             CHKPV(currentStateSM_);
547             isStopping_ = true;
548             int32_t ret = currentStateSM_->StopInputDeviceCooperate(srcNetworkId_);
549             if (ret != RET_OK) {
550                 MMI_HILOGE("Stop input device cooperate fail");
551                 isStopping_ = false;
552             }
553         }
554         return;
555     } else {
556         if (InputDevMgr->IsRemote(inputDevice)) {
557             return;
558         }
559     }
560     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
561     CHKPV(inputEventNormalizeHandler);
562     inputEventNormalizeHandler->HandleEvent(event);
563 }
564 
InitDeviceManager()565 bool InputDeviceCooperateSM::InitDeviceManager()
566 {
567     CALL_DEBUG_ENTER;
568     initCallback_ = std::make_shared<DeviceInitCallBack>();
569     int32_t ret = DisHardware.InitDeviceManager(MMI_DINPUT_PKG_NAME, initCallback_);
570     if (ret != 0) {
571         MMI_HILOGE("Init device manager failed, ret:%{public}d", ret);
572         return false;
573     }
574     stateCallback_ = std::make_shared<MmiDeviceStateCallback>();
575     ret = DisHardware.RegisterDevStateCallback(MMI_DINPUT_PKG_NAME, "", stateCallback_);
576     if (ret != 0) {
577         MMI_HILOGE("Register devStateCallback failed, ret:%{public}d", ret);
578         return false;
579     }
580     return true;
581 }
582 
OnDeviceOnline(const std::string & networkId)583 void InputDeviceCooperateSM::OnDeviceOnline(const std::string &networkId)
584 {
585     CALL_INFO_TRACE;
586     std::lock_guard<std::mutex> guard(mutex_);
587     onlineDevice_.push_back(networkId);
588     DProfileAdapter->RegisterCrossingStateListener(networkId,
589         std::bind(&InputDeviceCooperateSM::OnCooperateChanged,
590         InputDevCooSM, std::placeholders::_1, std::placeholders::_2));
591 }
592 
OnDeviceOffline(const std::string & networkId)593 void InputDeviceCooperateSM::OnDeviceOffline(const std::string &networkId)
594 {
595     CALL_INFO_TRACE;
596     DProfileAdapter->UnregisterCrossingStateListener(networkId);
597     Reset(networkId);
598     std::lock_guard<std::mutex> guard(mutex_);
599     if (!onlineDevice_.empty()) {
600         auto it = std::find(onlineDevice_.begin(), onlineDevice_.end(), networkId);
601         if (it != onlineDevice_.end()) {
602             onlineDevice_.erase(it);
603         }
604     }
605 }
606 
Dump(int32_t fd,const std::vector<std::string> & args)607 void InputDeviceCooperateSM::Dump(int32_t fd, const std::vector<std::string> &args)
608 {
609     CALL_DEBUG_ENTER;
610     std::lock_guard<std::mutex> guard(mutex_);
611     mprintf(fd, "Keyboard and mouse crossing information:");
612     mprintf(fd, "State machine status: %d\t", cooperateState_);
613     mprintf(fd, "Peripheral keyboard and mouse information: startDhid_  srcNetworkId_:\t");
614     mprintf(fd, "%s", startDhid_.c_str());
615     mprintf(fd, "%s", srcNetworkId_.c_str());
616     mprintf(fd, "Run successfully");
617 }
618 
OnRemoteDied()619 void InputDeviceCooperateSM::DeviceInitCallBack::OnRemoteDied()
620 {
621     CALL_INFO_TRACE;
622 }
623 
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)624 void InputDeviceCooperateSM::MmiDeviceStateCallback::OnDeviceOnline(
625     const DistributedHardware::DmDeviceInfo &deviceInfo)
626 {
627     CALL_DEBUG_ENTER;
628     InputDevCooSM->OnDeviceOnline(deviceInfo.deviceId);
629 }
630 
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)631 void InputDeviceCooperateSM::MmiDeviceStateCallback::OnDeviceOffline(
632     const DistributedHardware::DmDeviceInfo &deviceInfo)
633 {
634     CALL_INFO_TRACE;
635     InputDevCooSM->OnDeviceOffline(deviceInfo.deviceId);
636 }
637 
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)638 void InputDeviceCooperateSM::MmiDeviceStateCallback::OnDeviceChanged(
639     const DistributedHardware::DmDeviceInfo &deviceInfo)
640 {
641     CALL_INFO_TRACE;
642 }
643 
OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)644 void InputDeviceCooperateSM::MmiDeviceStateCallback::OnDeviceReady(
645     const DistributedHardware::DmDeviceInfo &deviceInfo)
646 {
647     CALL_INFO_TRACE;
648 }
649 } // namespace MMI
650 } // namespace OHOS
651