1 /*
2 * Copyright (c) 2022-2025 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 "dinput_context.h"
17
18 #include "constants.h"
19
20 #include "dinput_errcode.h"
21 #include "dinput_utils_tool.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 namespace DistributedInput {
26 IMPLEMENT_SINGLE_INSTANCE(DInputContext);
27
~DInputContext()28 DInputContext::~DInputContext()
29 {
30 {
31 std::lock_guard<std::mutex> lock(sinkMapMutex_);
32 sinkScreenInfoMap_.clear();
33 }
34
35 {
36 std::lock_guard<std::mutex> lock(srcMapMutex_);
37 srcScreenInfoMap_.clear();
38 }
39 }
40
GetScreenInfoKey(const std::string & devId,const uint64_t sourceWinId)41 std::string DInputContext::GetScreenInfoKey(const std::string &devId, const uint64_t sourceWinId)
42 {
43 DHLOGI("GetScreenInfoKey screenInfoKey: %{public}s, sourceWinId: %{public}" PRIu64 "",
44 GetAnonyString(devId).c_str(), sourceWinId);
45 return devId + RESOURCE_SEPARATOR + std::to_string(sourceWinId);
46 }
47
RemoveSinkScreenInfo(const std::string & screenInfoKey)48 int32_t DInputContext::RemoveSinkScreenInfo(const std::string &screenInfoKey)
49 {
50 DHLOGI("RemoveSinkScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
51 std::lock_guard<std::mutex> lock(sinkMapMutex_);
52 sinkScreenInfoMap_.erase(screenInfoKey);
53 return DH_SUCCESS;
54 }
55
UpdateSinkScreenInfo(const std::string & screenInfoKey,const SinkScreenInfo & sinkScreenInfo)56 int32_t DInputContext::UpdateSinkScreenInfo(const std::string &screenInfoKey, const SinkScreenInfo &sinkScreenInfo)
57 {
58 DHLOGI("UpdateSinkScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
59 std::lock_guard<std::mutex> lock(sinkMapMutex_);
60 if (sinkScreenInfoMap_.count(screenInfoKey) <= 0) {
61 DHLOGE("source window id not exist");
62 return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST;
63 }
64
65 SinkScreenInfo tmp = sinkScreenInfo;
66 if (CalculateTransformInfo(tmp) != DH_SUCCESS) {
67 DHLOGE("calculate transform infomation failed");
68 }
69
70 sinkScreenInfoMap_[screenInfoKey] = tmp;
71 return DH_SUCCESS;
72 }
73
GetSinkScreenInfo(const std::string & screenInfoKey)74 SinkScreenInfo DInputContext::GetSinkScreenInfo(const std::string &screenInfoKey)
75 {
76 DHLOGI("GetSinkScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
77 std::lock_guard<std::mutex> lock(sinkMapMutex_);
78 if (sinkScreenInfoMap_.count(screenInfoKey) <= 0) {
79 DHLOGE("screenInfoKey not exist");
80 SinkScreenInfo sinkScreenInfo;
81 sinkScreenInfoMap_[screenInfoKey] = sinkScreenInfo;
82 }
83
84 return sinkScreenInfoMap_[screenInfoKey];
85 }
86
CleanExceptionalInfo(const SrcScreenInfo & srcScreenInfo)87 void DInputContext::CleanExceptionalInfo(const SrcScreenInfo &srcScreenInfo)
88 {
89 DHLOGI("CleanExceptionalInfo start!");
90 std::string uuid = srcScreenInfo.uuid;
91 int32_t sessionId = srcScreenInfo.sessionId;
92 std::lock_guard<std::mutex> lock(sinkMapMutex_);
93 for (auto iter = sinkScreenInfoMap_.begin(); iter != sinkScreenInfoMap_.end();) {
94 auto srcInfo = iter->second.srcScreenInfo;
95 if ((std::strcmp(srcInfo.uuid.c_str(), uuid.c_str()) == 0) && (srcInfo.sessionId != sessionId)) {
96 DHLOGI("CleanExceptionalInfo screenInfoKey: %{public}s, sessionId: %{public}d",
97 GetAnonyString(iter->first).c_str(), sessionId);
98 iter = sinkScreenInfoMap_.erase(iter);
99 } else {
100 ++iter;
101 }
102 }
103 }
104
GetAllSinkScreenInfo()105 const std::unordered_map<std::string, SinkScreenInfo> &DInputContext::GetAllSinkScreenInfo()
106 {
107 std::lock_guard<std::mutex> lock(sinkMapMutex_);
108 return sinkScreenInfoMap_;
109 }
110
RemoveSrcScreenInfo(const std::string & screenInfoKey)111 int32_t DInputContext::RemoveSrcScreenInfo(const std::string &screenInfoKey)
112 {
113 DHLOGI("RemoveSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
114 std::lock_guard<std::mutex> lock(srcMapMutex_);
115 srcScreenInfoMap_.erase(screenInfoKey);
116 return DH_SUCCESS;
117 }
118
UpdateSrcScreenInfo(const std::string & screenInfoKey,const SrcScreenInfo & srcScreenInfo)119 int32_t DInputContext::UpdateSrcScreenInfo(const std::string &screenInfoKey, const SrcScreenInfo &srcScreenInfo)
120 {
121 std::lock_guard<std::mutex> lock(srcMapMutex_);
122 DHLOGI("UpdateSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
123 if (srcScreenInfoMap_.count(screenInfoKey) <= 0) {
124 DHLOGE("source window id not exist");
125 return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST;
126 }
127
128 srcScreenInfoMap_[screenInfoKey] = srcScreenInfo;
129 return DH_SUCCESS;
130 }
131
GetSrcScreenInfo(const std::string & screenInfoKey)132 SrcScreenInfo DInputContext::GetSrcScreenInfo(const std::string &screenInfoKey)
133 {
134 DHLOGI("GetSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
135 std::lock_guard<std::mutex> lock(srcMapMutex_);
136 if (srcScreenInfoMap_.count(screenInfoKey) <= 0) {
137 DHLOGE("source window id not exist");
138 SrcScreenInfo srcScreenInfo;
139 srcScreenInfoMap_[screenInfoKey] = srcScreenInfo;
140 }
141
142 return srcScreenInfoMap_[screenInfoKey];
143 }
144
SetLocalTouchScreenInfo(const LocalTouchScreenInfo & localTouchScreenInfo)145 void DInputContext::SetLocalTouchScreenInfo(const LocalTouchScreenInfo &localTouchScreenInfo)
146 {
147 std::lock_guard<std::mutex> lock(localTouchScreenInfoMutex_);
148 localTouchScreenInfo_ = localTouchScreenInfo;
149 }
150
GetLocalTouchScreenInfo()151 LocalTouchScreenInfo DInputContext::GetLocalTouchScreenInfo()
152 {
153 std::lock_guard<std::mutex> lock(localTouchScreenInfoMutex_);
154 return localTouchScreenInfo_;
155 }
156
CalculateTransformInfo(SinkScreenInfo & sinkScreenInfo)157 int32_t DInputContext::CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo)
158 {
159 if (sinkScreenInfo.sinkShowHeight == 0 || sinkScreenInfo.sinkShowWidth == 0) {
160 DHLOGE("can not calculate transform infomation");
161 return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL;
162 }
163 TransformInfo transformInfo;
164 transformInfo.sinkWinPhyX = static_cast<uint32_t>(sinkScreenInfo.sinkWinShowX /
165 static_cast<double>(sinkScreenInfo.sinkShowWidth)) * sinkScreenInfo.sinkPhyWidth;
166 transformInfo.sinkWinPhyY = static_cast<uint32_t>(sinkScreenInfo.sinkWinShowY /
167 static_cast<double>(sinkScreenInfo.sinkShowHeight)) * sinkScreenInfo.sinkPhyHeight;
168 transformInfo.sinkProjPhyWidth = static_cast<uint32_t>((sinkScreenInfo.sinkProjShowWidth /
169 static_cast<double>(sinkScreenInfo.sinkShowWidth)) * sinkScreenInfo.sinkPhyWidth);
170 transformInfo.sinkProjPhyHeight = static_cast<uint32_t>((sinkScreenInfo.sinkProjShowHeight /
171 static_cast<double>(sinkScreenInfo.sinkShowHeight)) * sinkScreenInfo.sinkPhyHeight);
172 if (transformInfo.sinkProjPhyWidth == 0 || transformInfo.sinkProjPhyHeight == 0) {
173 DHLOGE("can not calculate transform infomation");
174 return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL;
175 }
176
177 // coefficient of the sink projection area in the source touch driver
178 transformInfo.coeffWidth = static_cast<double>(sinkScreenInfo.srcScreenInfo.sourcePhyWidth /
179 static_cast<double>(transformInfo.sinkProjPhyWidth));
180 transformInfo.coeffHeight = static_cast<double>(sinkScreenInfo.srcScreenInfo.sourcePhyHeight /
181 static_cast<double>(transformInfo.sinkProjPhyHeight));
182
183 DHLOGI("CalculateTransformInfo sinkWinPhyX = %{public}d, sinkWinPhyY = %{public}d, sinkProjPhyWidth = %{public}d, "
184 "sinkProjPhyHeight = %{public}d, coeffWidth = %{public}f, coeffHeight = %{public}f", transformInfo.sinkWinPhyX,
185 transformInfo.sinkWinPhyY, transformInfo.sinkProjPhyWidth, transformInfo.sinkProjPhyHeight,
186 transformInfo.coeffWidth, transformInfo.coeffHeight);
187 sinkScreenInfo.transformInfo = transformInfo;
188 return DH_SUCCESS;
189 }
190
GetDHFwkKit()191 std::shared_ptr<DistributedHardwareFwkKit> DInputContext::GetDHFwkKit()
192 {
193 std::lock_guard<std::mutex> lock(dhFwkKitMutex_);
194 if (dhFwkKit_ == nullptr) {
195 dhFwkKit_ = std::make_shared<DistributedHardwareFwkKit>();
196 }
197 return dhFwkKit_;
198 }
199
GetRemoteObject(const int32_t saId)200 sptr<IRemoteObject> DInputContext::GetRemoteObject(const int32_t saId)
201 {
202 DHLOGI("GetDScreenSrcSA start");
203 {
204 std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
205 if (remoteObjects_.find(saId) != remoteObjects_.end()) {
206 DHLOGI("dScreenSrcSA get from cache!");
207 return remoteObjects_[saId];
208 }
209 }
210
211 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
212 if (samgr == nullptr) {
213 DHLOGE("GetSystemAbilityManager fail!");
214 return nullptr;
215 }
216 auto remoteObject = samgr->GetSystemAbility(saId);
217 if (remoteObject == nullptr) {
218 DHLOGE("GetSystemAbility remoteObject is nullptr");
219 return nullptr;
220 }
221 return remoteObject;
222 }
223
AddRemoteObject(const int32_t saId,const sptr<IRemoteObject> & remoteObject)224 void DInputContext::AddRemoteObject(const int32_t saId, const sptr<IRemoteObject> &remoteObject)
225 {
226 std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
227 remoteObjects_[saId] = remoteObject;
228 }
229
RemoveRemoteObject(const int32_t saId)230 void DInputContext::RemoveRemoteObject(const int32_t saId)
231 {
232 std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
233 remoteObjects_.erase(saId);
234 }
235 } // namespace DistributedInput
236 } // namespace DistributedHardware
237 } // namespace OHOS