• 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_impl.h"
17 
18 #include "mmi_log.h"
19 #include "multimodal_event_handler.h"
20 #include "multimodal_input_connect_manager.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceCooperateImpl"};
26 } // namespace
27 
GetInstance()28 InputDeviceCooperateImpl &InputDeviceCooperateImpl::GetInstance()
29 {
30     static InputDeviceCooperateImpl instance;
31     return instance;
32 }
33 
RegisterCooperateListener(InputDevCooperateListenerPtr listener)34 int32_t InputDeviceCooperateImpl::RegisterCooperateListener(InputDevCooperateListenerPtr listener)
35 {
36     CALL_DEBUG_ENTER;
37     CHKPR(listener, RET_ERR);
38     std::lock_guard<std::mutex> guard(mtx_);
39     for (const auto &item : devCooperateListener_) {
40         if (item == listener) {
41             MMI_HILOGW("The listener already exists");
42             return RET_ERR;
43         }
44     }
45     devCooperateListener_.push_back(listener);
46     if (!isListeningProcess_) {
47         MMI_HILOGI("Start monitoring");
48         isListeningProcess_ = true;
49         return MultimodalInputConnMgr->RegisterCooperateListener();
50     }
51     return RET_OK;
52 }
53 
UnregisterCooperateListener(InputDevCooperateListenerPtr listener)54 int32_t InputDeviceCooperateImpl::UnregisterCooperateListener(InputDevCooperateListenerPtr listener)
55 {
56     CALL_DEBUG_ENTER;
57     std::lock_guard<std::mutex> guard(mtx_);
58     if (listener == nullptr) {
59         devCooperateListener_.clear();
60         goto listenerLabel;
61     }
62     for (auto it = devCooperateListener_.begin(); it != devCooperateListener_.end(); ++it) {
63         if (*it == listener) {
64             devCooperateListener_.erase(it);
65             goto listenerLabel;
66         }
67     }
68 
69 listenerLabel:
70     if (isListeningProcess_ && devCooperateListener_.empty()) {
71         isListeningProcess_ = false;
72         return MultimodalInputConnMgr->UnregisterCooperateListener();
73     }
74     return RET_OK;
75 }
76 
EnableInputDeviceCooperate(bool enabled,FuncCooperationMessage callback)77 int32_t InputDeviceCooperateImpl::EnableInputDeviceCooperate(bool enabled, FuncCooperationMessage callback)
78 {
79     CALL_DEBUG_ENTER;
80     std::lock_guard<std::mutex> guard(mtx_);
81     CooperateEvent event;
82     event.msg = callback;
83     if (userData_ == INT32_MAX) {
84         MMI_HILOGE("userData exceeds the maximum");
85         return RET_ERR;
86     }
87     devCooperateEvent_[userData_] = event;
88     return MultimodalInputConnMgr->EnableInputDeviceCooperate(userData_++, enabled);
89 }
90 
StartInputDeviceCooperate(const std::string & sinkDeviceId,int32_t srcInputDeviceId,FuncCooperationMessage callback)91 int32_t InputDeviceCooperateImpl::StartInputDeviceCooperate(const std::string &sinkDeviceId, int32_t srcInputDeviceId,
92     FuncCooperationMessage callback)
93 {
94     CALL_DEBUG_ENTER;
95     std::lock_guard<std::mutex> guard(mtx_);
96     CooperateEvent event;
97     event.msg = callback;
98     if (userData_ == INT32_MAX) {
99         MMI_HILOGE("userData exceeds the maximum");
100         return RET_ERR;
101     }
102     devCooperateEvent_[userData_] = event;
103     return MultimodalInputConnMgr->StartInputDeviceCooperate(userData_++, sinkDeviceId, srcInputDeviceId);
104 }
105 
StopDeviceCooperate(FuncCooperationMessage callback)106 int32_t InputDeviceCooperateImpl::StopDeviceCooperate(FuncCooperationMessage callback)
107 {
108     CALL_DEBUG_ENTER;
109     std::lock_guard<std::mutex> guard(mtx_);
110     CooperateEvent event;
111     event.msg = callback;
112     if (userData_ == INT32_MAX) {
113         MMI_HILOGE("userData exceeds the maximum");
114         return RET_ERR;
115     }
116     devCooperateEvent_[userData_] = event;
117     return MultimodalInputConnMgr->StopDeviceCooperate(userData_++);
118 }
119 
GetInputDeviceCooperateState(const std::string & deviceId,FuncCooperateionState callback)120 int32_t InputDeviceCooperateImpl::GetInputDeviceCooperateState(
121     const std::string &deviceId, FuncCooperateionState callback)
122 {
123     CALL_DEBUG_ENTER;
124     std::lock_guard<std::mutex> guard(mtx_);
125     CooperateEvent event;
126     event.state = callback;
127     if (userData_ == INT32_MAX) {
128         MMI_HILOGE("userData exceeds the maximum");
129         return RET_ERR;
130     }
131     devCooperateEvent_[userData_] = event;
132     return MultimodalInputConnMgr->GetInputDeviceCooperateState(userData_++, deviceId);
133 }
134 
OnDevCooperateListener(const std::string deviceId,CooperationMessage msg)135 void InputDeviceCooperateImpl::OnDevCooperateListener(const std::string deviceId, CooperationMessage msg)
136 {
137     CALL_DEBUG_ENTER;
138     std::lock_guard<std::mutex> guard(mtx_);
139     for (const auto &item : devCooperateListener_) {
140         item->OnCooperateMessage(deviceId, msg);
141     }
142 }
143 
OnCooprationMessage(int32_t userData,const std::string deviceId,CooperationMessage msg)144 void InputDeviceCooperateImpl::OnCooprationMessage(int32_t userData, const std::string deviceId, CooperationMessage msg)
145 {
146     CALL_DEBUG_ENTER;
147     CHK_PID_AND_TID();
148     std::lock_guard<std::mutex> guard(mtx_);
149     auto event = GetCooprateMessageEvent(userData);
150     CHKPV(event);
151     (*event)(deviceId, msg);
152 }
153 
OnCooperationState(int32_t userData,bool state)154 void InputDeviceCooperateImpl::OnCooperationState(int32_t userData, bool state)
155 {
156     CALL_DEBUG_ENTER;
157     CHK_PID_AND_TID();
158     std::lock_guard<std::mutex> guard(mtx_);
159     auto event = GetCooprateStateEvent(userData);
160     CHKPV(event);
161     (*event)(state);
162     MMI_HILOGD("Cooperatinon state event callback userData:%{public}d state:(%{public}d)", userData, state);
163 }
164 
GetUserData()165 int32_t InputDeviceCooperateImpl::GetUserData()
166 {
167     std::lock_guard<std::mutex> guard(mtx_);
168     return userData_;
169 }
170 
GetCooprateMessageEvent(int32_t userData) const171 const InputDeviceCooperateImpl::DevCooperationMsg *InputDeviceCooperateImpl::GetCooprateMessageEvent(
172     int32_t userData) const
173 {
174     auto iter = devCooperateEvent_.find(userData);
175     return iter == devCooperateEvent_.end() ? nullptr : &iter->second.msg;
176 }
177 
GetCooprateStateEvent(int32_t userData) const178 const InputDeviceCooperateImpl::DevCooperateionState *InputDeviceCooperateImpl::GetCooprateStateEvent(
179     int32_t userData) const
180 {
181     auto iter = devCooperateEvent_.find(userData);
182     return iter == devCooperateEvent_.end() ? nullptr : &iter->second.state;
183 }
184 } // namespace MMI
185 } // namespace OHOS