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 "i_input_device_cooperate_state.h"
17
18 #include "cooperate_event_manager.h"
19 #include "distributed_input_adapter.h"
20 #include "input_device_cooperate_sm.h"
21 #include "input_device_manager.h"
22 #include "mouse_event_normalize.h"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "IInputDeviceCooperateState"};
28 } // namespace
29
IInputDeviceCooperateState()30 IInputDeviceCooperateState::IInputDeviceCooperateState()
31 {
32 runner_ = AppExecFwk::EventRunner::Create(true);
33 CHKPL(runner_);
34 eventHandler_ = std::make_shared<CooperateEventHandler>(runner_);
35 }
36
PrepareAndStart(const std::string & srcNetworkId,int32_t startInputDeviceId)37 int32_t IInputDeviceCooperateState::PrepareAndStart(const std::string &srcNetworkId, int32_t startInputDeviceId)
38 {
39 CALL_INFO_TRACE;
40 std::string sinkNetworkId = InputDevMgr->GetOriginNetworkId(startInputDeviceId);
41 int32_t ret = RET_ERR;
42 if (NeedPrepare(srcNetworkId, sinkNetworkId)) {
43 InputDevCooSM->UpdatePreparedDevices(srcNetworkId, sinkNetworkId);
44 ret = DistributedAdapter->PrepareRemoteInput(
45 srcNetworkId, sinkNetworkId, [this, srcNetworkId, startInputDeviceId](bool isSuccess) {
46 this->OnPrepareDistributedInput(isSuccess, srcNetworkId, startInputDeviceId);
47 });
48 if (ret != RET_OK) {
49 MMI_HILOGE("Prepare remote input fail");
50 InputDevCooSM->OnStartFinish(false, sinkNetworkId, startInputDeviceId);
51 InputDevCooSM->UpdatePreparedDevices("", "");
52 }
53 } else {
54 ret = StartRemoteInput(startInputDeviceId);
55 if (ret != RET_OK) {
56 MMI_HILOGE("Start remoteNetworkId input fail");
57 InputDevCooSM->OnStartFinish(false, sinkNetworkId, startInputDeviceId);
58 }
59 }
60 return ret;
61 }
62
OnPrepareDistributedInput(bool isSuccess,const std::string & srcNetworkId,int32_t startInputDeviceId)63 void IInputDeviceCooperateState::OnPrepareDistributedInput(
64 bool isSuccess, const std::string &srcNetworkId, int32_t startInputDeviceId)
65 {
66 MMI_HILOGI("isSuccess: %{public}s", isSuccess ? "true" : "false");
67 if (!isSuccess) {
68 InputDevCooSM->UpdatePreparedDevices("", "");
69 InputDevCooSM->OnStartFinish(false, srcNetworkId, startInputDeviceId);
70 } else {
71 std::string taskName = "start_dinput_task";
72 std::function<void()> handleStartDinputFunc =
73 std::bind(&IInputDeviceCooperateState::StartRemoteInput, this, startInputDeviceId);
74 CHKPV(eventHandler_);
75 eventHandler_->ProxyPostTask(handleStartDinputFunc, taskName, 0);
76 }
77 }
78
StartRemoteInput(int32_t startInputDeviceId)79 int32_t IInputDeviceCooperateState::StartRemoteInput(int32_t startInputDeviceId)
80 {
81 CALL_DEBUG_ENTER;
82 std::pair<std::string, std::string> networkIds = InputDevCooSM->GetPreparedDevices();
83 std::vector<std::string> dhids = InputDevMgr->GetCooperateDhids(startInputDeviceId);
84 if (dhids.empty()) {
85 InputDevCooSM->OnStartFinish(false, networkIds.first, startInputDeviceId);
86 return static_cast<int32_t>(CooperationMessage::INPUT_DEVICE_ID_ERROR);
87 }
88 int32_t ret = DistributedAdapter->StartRemoteInput(
89 networkIds.first, networkIds.second, dhids, [this, src = networkIds.first, startInputDeviceId](bool isSuccess) {
90 this->OnStartRemoteInput(isSuccess, src, startInputDeviceId);
91 });
92 if (ret != RET_OK) {
93 InputDevCooSM->OnStartFinish(false, networkIds.first, startInputDeviceId);
94 return static_cast<int32_t>(CooperationMessage::COOPERATE_FAIL);
95 }
96 return RET_OK;
97 }
98
OnStartRemoteInput(bool isSuccess,const std::string & srcNetworkId,int32_t startInputDeviceId)99 void IInputDeviceCooperateState::OnStartRemoteInput(
100 bool isSuccess, const std::string &srcNetworkId, int32_t startInputDeviceId)
101 {
102 CALL_DEBUG_ENTER;
103 std::string taskName = "start_finish_task";
104 std::function<void()> handleStartFinishFunc =
105 std::bind(&InputDeviceCooperateSM::OnStartFinish, InputDevCooSM, isSuccess, srcNetworkId, startInputDeviceId);
106 CHKPV(eventHandler_);
107 eventHandler_->ProxyPostTask(handleStartFinishFunc, taskName, 0);
108 }
109
NeedPrepare(const std::string & srcNetworkId,const std::string & sinkNetworkId)110 bool IInputDeviceCooperateState::NeedPrepare(const std::string &srcNetworkId, const std::string &sinkNetworkId)
111 {
112 CALL_DEBUG_ENTER;
113 std::pair<std::string, std::string> prepared = InputDevCooSM->GetPreparedDevices();
114 bool isNeed = !(srcNetworkId == prepared.first && sinkNetworkId == prepared.second);
115 MMI_HILOGI("NeedPrepare?: %{public}s", isNeed ? "true" : "false");
116 return isNeed;
117 }
118 } // namespace MMI
119 } // namespace OHOS
120