• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetAllSinkScreenInfo()87 const std::unordered_map<std::string, SinkScreenInfo> &DInputContext::GetAllSinkScreenInfo()
88 {
89     std::lock_guard<std::mutex> lock(sinkMapMutex_);
90     return sinkScreenInfoMap_;
91 }
92 
RemoveSrcScreenInfo(const std::string & screenInfoKey)93 int32_t DInputContext::RemoveSrcScreenInfo(const std::string &screenInfoKey)
94 {
95     DHLOGI("RemoveSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
96     std::lock_guard<std::mutex> lock(srcMapMutex_);
97     srcScreenInfoMap_.erase(screenInfoKey);
98     return DH_SUCCESS;
99 }
100 
UpdateSrcScreenInfo(const std::string & screenInfoKey,const SrcScreenInfo & srcScreenInfo)101 int32_t DInputContext::UpdateSrcScreenInfo(const std::string &screenInfoKey, const SrcScreenInfo &srcScreenInfo)
102 {
103     std::lock_guard<std::mutex> lock(srcMapMutex_);
104     DHLOGI("UpdateSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
105     if (srcScreenInfoMap_.count(screenInfoKey) <= 0) {
106         DHLOGE("source window id not exist");
107         return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST;
108     }
109 
110     srcScreenInfoMap_[screenInfoKey] = srcScreenInfo;
111     return DH_SUCCESS;
112 }
113 
GetSrcScreenInfo(const std::string & screenInfoKey)114 SrcScreenInfo DInputContext::GetSrcScreenInfo(const std::string &screenInfoKey)
115 {
116     DHLOGI("GetSrcScreenInfo screenInfoKey: %{public}s", GetAnonyString(screenInfoKey).c_str());
117     std::lock_guard<std::mutex> lock(srcMapMutex_);
118     if (srcScreenInfoMap_.count(screenInfoKey) <= 0) {
119         DHLOGE("source window id not exist");
120         SrcScreenInfo srcScreenInfo;
121         srcScreenInfoMap_[screenInfoKey] = srcScreenInfo;
122     }
123 
124     return srcScreenInfoMap_[screenInfoKey];
125 }
126 
SetLocalTouchScreenInfo(const LocalTouchScreenInfo & localTouchScreenInfo)127 void DInputContext::SetLocalTouchScreenInfo(const LocalTouchScreenInfo &localTouchScreenInfo)
128 {
129     std::lock_guard<std::mutex> lock(localTouchScreenInfoMutex_);
130     localTouchScreenInfo_ = localTouchScreenInfo;
131 }
132 
GetLocalTouchScreenInfo()133 LocalTouchScreenInfo DInputContext::GetLocalTouchScreenInfo()
134 {
135     std::lock_guard<std::mutex> lock(localTouchScreenInfoMutex_);
136     return localTouchScreenInfo_;
137 }
138 
CalculateTransformInfo(SinkScreenInfo & sinkScreenInfo)139 int32_t DInputContext::CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo)
140 {
141     if (sinkScreenInfo.sinkShowHeight == 0 || sinkScreenInfo.sinkShowWidth == 0) {
142         DHLOGE("can not calculate transform infomation");
143         return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL;
144     }
145     TransformInfo transformInfo;
146     transformInfo.sinkWinPhyX = static_cast<uint32_t>(sinkScreenInfo.sinkWinShowX /
147         static_cast<double>(sinkScreenInfo.sinkShowWidth)) * sinkScreenInfo.sinkPhyWidth;
148     transformInfo.sinkWinPhyY = static_cast<uint32_t>(sinkScreenInfo.sinkWinShowY /
149         static_cast<double>(sinkScreenInfo.sinkShowHeight)) * sinkScreenInfo.sinkPhyHeight;
150     transformInfo.sinkProjPhyWidth = static_cast<uint32_t>((sinkScreenInfo.sinkProjShowWidth /
151         static_cast<double>(sinkScreenInfo.sinkShowWidth)) * sinkScreenInfo.sinkPhyWidth);
152     transformInfo.sinkProjPhyHeight = static_cast<uint32_t>((sinkScreenInfo.sinkProjShowHeight /
153         static_cast<double>(sinkScreenInfo.sinkShowHeight)) * sinkScreenInfo.sinkPhyHeight);
154     if (transformInfo.sinkProjPhyWidth == 0 || transformInfo.sinkProjPhyHeight == 0) {
155         DHLOGE("can not calculate transform infomation");
156         return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL;
157     }
158 
159     // coefficient of the sink projection area in the source touch driver
160     transformInfo.coeffWidth = static_cast<double>(sinkScreenInfo.srcScreenInfo.sourcePhyWidth /
161         static_cast<double>(transformInfo.sinkProjPhyWidth));
162     transformInfo.coeffHeight = static_cast<double>(sinkScreenInfo.srcScreenInfo.sourcePhyHeight /
163         static_cast<double>(transformInfo.sinkProjPhyHeight));
164 
165     DHLOGI("CalculateTransformInfo sinkWinPhyX = %{public}d, sinkWinPhyY = %{public}d, sinkProjPhyWidth = %{public}d, "
166         "sinkProjPhyHeight = %{public}d, coeffWidth = %{public}f, coeffHeight = %{public}f", transformInfo.sinkWinPhyX,
167         transformInfo.sinkWinPhyY, transformInfo.sinkProjPhyWidth, transformInfo.sinkProjPhyHeight,
168         transformInfo.coeffWidth, transformInfo.coeffHeight);
169     sinkScreenInfo.transformInfo = transformInfo;
170     return DH_SUCCESS;
171 }
172 
GetDHFwkKit()173 std::shared_ptr<DistributedHardwareFwkKit> DInputContext::GetDHFwkKit()
174 {
175     std::lock_guard<std::mutex> lock(dhFwkKitMutex_);
176     if (dhFwkKit_ == nullptr) {
177         dhFwkKit_ = std::make_shared<DistributedHardwareFwkKit>();
178     }
179     return dhFwkKit_;
180 }
181 
GetRemoteObject(const int32_t saId)182 sptr<IRemoteObject> DInputContext::GetRemoteObject(const int32_t saId)
183 {
184     DHLOGI("GetDScreenSrcSA start");
185     {
186         std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
187         if (remoteObjects_.find(saId) != remoteObjects_.end()) {
188             DHLOGI("dScreenSrcSA get from cache!");
189             return remoteObjects_[saId];
190         }
191     }
192 
193     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
194     if (samgr == nullptr) {
195         DHLOGE("GetSystemAbilityManager fail!");
196         return nullptr;
197     }
198     auto remoteObject = samgr->GetSystemAbility(saId);
199     if (remoteObject == nullptr) {
200         DHLOGE("GetSystemAbility remoteObject is nullptr");
201         return nullptr;
202     }
203     return remoteObject;
204 }
205 
AddRemoteObject(const int32_t saId,const sptr<IRemoteObject> & remoteObject)206 void DInputContext::AddRemoteObject(const int32_t saId, const sptr<IRemoteObject> &remoteObject)
207 {
208     std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
209     remoteObjects_[saId] = remoteObject;
210 }
211 
RemoveRemoteObject(const int32_t saId)212 void DInputContext::RemoveRemoteObject(const int32_t saId)
213 {
214     std::lock_guard<std::mutex> lock(remoteObjectsMutex_);
215     remoteObjects_.erase(saId);
216 }
217 } // namespace DistributedInput
218 } // namespace DistributedHardware
219 } // namespace OHOS