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