• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "scene_input_manager.h"
17 
18 #include <hitrace_meter.h>
19 #include "scene_session_dirty_manager.h"
20 #include "screen_session_manager/include/screen_session_manager_client.h"
21 #include "session_manager/include/scene_session_manager.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneInputManager" };
28 const std::string SCENE_INPUT_MANAGER_THREAD = "SceneInputManager";
29 const std::string FLUSH_DISPLAY_INFO_THREAD = "OS_FlushDisplayInfoThread";
30 
31 constexpr int MAX_WINDOWINFO_NUM = 15;
32 constexpr int DEFALUT_DISPLAYID = 0;
33 constexpr int EMPTY_FOCUS_WINDOW_ID = -1;
34 constexpr int INVALID_PERSISTENT_ID = 0;
35 constexpr int DEFAULT_SCREEN_POS = 0;
36 
37 bool IsEqualUiExtentionWindowInfo(const std::vector<MMI::WindowInfo>& a, const std::vector<MMI::WindowInfo>& b);
38 constexpr unsigned int TRANSFORM_DATA_LEN = 9;
39 
operator !=(const MMI::Rect & a,const MMI::Rect & b)40 bool operator!=(const MMI::Rect& a, const MMI::Rect& b)
41 {
42     if (a.x != b.x || a.y != b.y || a.width != b.width || a.height != b.height) {
43         return true;
44     }
45     return false;
46 }
47 
operator ==(const MMI::DisplayInfo & a,const MMI::DisplayInfo & b)48 bool operator==(const MMI::DisplayInfo& a, const MMI::DisplayInfo& b)
49 {
50     if (a.id != b.id || a.x != b.x || a.y != b.y || a.width != b.width ||
51         a.height != b.height || a.dpi != b.dpi || a.name != b.name || a.uniq != b.uniq ||
52         static_cast<int32_t>(a.direction) != static_cast<int32_t>(b.direction) ||
53         static_cast<int32_t>(a.displayDirection) != static_cast<int32_t>(b.displayDirection) ||
54         static_cast<int32_t>(a.displayMode) != static_cast<int32_t>(b.displayMode) ||
55         a.transform != b.transform) {
56         return false;
57     }
58     return true;
59 }
60 
operator !=(const std::vector<float> & a,const std::vector<float> & b)61 bool operator!=(const std::vector<float>& a, const std::vector<float>& b)
62 {
63     if (a.size() != b.size()) {
64         return true;
65     }
66     int sizeOfA = static_cast<int>(a.size());
67     for (int index = 0; index < sizeOfA; index++) {
68         if (a[index] != b[index]) {
69             return true;
70         }
71     }
72     return false;
73 }
74 
IsEqualWindowInfo(const MMI::WindowInfo & a,const MMI::WindowInfo & b)75 bool IsEqualWindowInfo(const MMI::WindowInfo& a, const MMI::WindowInfo& b)
76 {
77     if (a.id != b.id || a.pid != b.pid || a.uid != b.uid || a.agentWindowId != b.agentWindowId || a.flags != b.flags ||
78         a.displayId != b.displayId || a.zOrder != b.zOrder) {
79         return false;
80     }
81 
82     if (a.windowInputType != b.windowInputType || a.privacyMode != b.privacyMode ||
83         a.windowType != b.windowType || a.pixelMap != b.pixelMap) {
84         return false;
85     }
86     return true;
87 }
88 
operator ==(const MMI::WindowInfo & a,const MMI::WindowInfo & b)89 bool operator==(const MMI::WindowInfo& a, const MMI::WindowInfo& b)
90 {
91     if (!IsEqualWindowInfo(a, b)) {
92         return false;
93     }
94 
95     if (a.area != b.area || a.defaultHotAreas.size() != b.defaultHotAreas.size() ||
96         a.pointerHotAreas.size() != b.pointerHotAreas.size() ||
97         a.pointerChangeAreas.size() != b.pointerChangeAreas.size() || a.transform.size() != b.transform.size()) {
98         return false;
99     }
100 
101     int sizeOfDefaultHotAreas = static_cast<int>(a.defaultHotAreas.size());
102     for (int index = 0; index < sizeOfDefaultHotAreas; index++) {
103         if (a.defaultHotAreas[index] != b.defaultHotAreas[index]) {
104             return false;
105         }
106     }
107     int sizeOfPointerHotAreas = static_cast<int>(a.pointerHotAreas.size());
108     for (int index = 0; index < sizeOfPointerHotAreas; index++) {
109         if (a.pointerHotAreas[index] != b.pointerHotAreas[index]) {
110             return false;
111         }
112     }
113     int sizeOfPointerChangeAreas = static_cast<int>(a.pointerChangeAreas.size());
114     for (int index = 0; index < sizeOfPointerChangeAreas; index++) {
115         if (a.pointerChangeAreas[index] != b.pointerChangeAreas[index]) {
116             return false;
117         }
118     }
119 
120     if (a.transform != b.transform) {
121         return false;
122     }
123     if (!IsEqualUiExtentionWindowInfo(a.uiExtentionWindowInfo, b.uiExtentionWindowInfo)) {
124         return false;
125     }
126     return true;
127 }
128 
operator !=(const MMI::WindowInfo & a,const MMI::WindowInfo & b)129 bool operator!=(const MMI::WindowInfo& a, const MMI::WindowInfo& b)
130 {
131     if (a == b) {
132         return false;
133     }
134     return true;
135 }
136 
IsEqualUiExtentionWindowInfo(const std::vector<MMI::WindowInfo> & a,const std::vector<MMI::WindowInfo> & b)137 bool IsEqualUiExtentionWindowInfo(const std::vector<MMI::WindowInfo>& a, const std::vector<MMI::WindowInfo>& b)
138 {
139     if (a.size() != b.size()) {
140         return false;
141     }
142     int size = static_cast<int>(a.size());
143     for (int i = 0; i < size; i++) {
144         if (a[i] != b[i]) {
145             return false;
146         }
147     }
148     return true;
149 }
150 
DumpTransformInDisplayInfo(const std::vector<float> & transform)151 std::string DumpTransformInDisplayInfo(const std::vector<float>& transform)
152 {
153     std::stringstream stream("[");
154     for (float transformItem : transform) {
155         stream << transformItem << ",";
156     }
157     stream << "]";
158     return stream.str();
159 }
160 
DumpDisplayInfo(const MMI::DisplayInfo & info)161 std::string DumpDisplayInfo(const MMI::DisplayInfo& info)
162 {
163     std::string infoStr =  "DisplayInfo: ";
164     infoStr = infoStr + " id: " + std::to_string(info.id) + " x: " + std::to_string(info.x) +
165         "y: " + std::to_string(info.y) + " width: " + std::to_string(info.width) +
166         "height: " + std::to_string(info.height) + " dpi: " + std::to_string(info.dpi) + " name:" + info.name +
167         " uniq: " + info.uniq + " displayMode: " + std::to_string(static_cast<int>(info.displayMode)) +
168         " direction: " + std::to_string(static_cast<int>(info.direction)) +
169         " transform: " + DumpTransformInDisplayInfo(info.transform);
170     return infoStr;
171 }
172 } //namespace
173 
174 
WM_IMPLEMENT_SINGLE_INSTANCE(SceneInputManager)175 WM_IMPLEMENT_SINGLE_INSTANCE(SceneInputManager)
176 
177 void SceneInputManager::Init()
178 {
179     sceneSessionDirty_ = std::make_shared<SceneSessionDirtyManager>();
180     eventLoop_ = AppExecFwk::EventRunner::Create(FLUSH_DISPLAY_INFO_THREAD);
181     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventLoop_);
182     auto callback = [this]() {
183         FlushDisplayInfoToMMI();
184     };
185     sceneSessionDirty_->RegisterFlushWindowInfoCallback(callback);
186     SceneSession::RegisterGetConstrainedModalExtWindowInfo(
187         [](const sptr<SceneSession>& sceneSession) -> std::optional<ExtensionWindowEventInfo> {
188             return SceneInputManager::GetInstance().GetConstrainedModalExtWindowInfo(sceneSession);
189         });
190 }
191 
ConstructDisplayInfos(std::vector<MMI::DisplayInfo> & displayInfos)192 void SceneInputManager::ConstructDisplayInfos(std::vector<MMI::DisplayInfo>& displayInfos)
193 {
194     std::map<ScreenId, ScreenProperty> screensProperties =
195         ScreenSessionManagerClient::GetInstance().GetAllScreensProperties();
196     if (screensProperties.empty()) {
197         TLOGE(WmsLogTag::WMS_EVENT, "screensProperties is empty");
198         return;
199     }
200     auto displayMode = ScreenSessionManagerClient::GetInstance().GetFoldDisplayMode();
201     for (auto& [screenId, screenProperty] : screensProperties) {
202         auto screenSession = ScreenSessionManagerClient::GetInstance().GetScreenSessionById(screenId);
203         auto screenWidth = screenProperty.GetPhysicalTouchBounds().rect_.GetWidth();
204         auto screenHeight = screenProperty.GetPhysicalTouchBounds().rect_.GetHeight();
205         auto transform = Matrix3f::IDENTITY;
206         Vector2f scale(screenProperty.GetScaleX(), screenProperty.GetScaleY());
207         transform = transform.Scale(scale, screenProperty.GetPivotX() * screenWidth,
208             screenProperty.GetPivotY() * screenHeight).Inverse();
209         std::vector<float> transformData(transform.GetData(), transform.GetData() + TRANSFORM_DATA_LEN);
210         int32_t screenOneHandX = DEFAULT_SCREEN_POS;
211         int32_t screenOneHandY = DEFAULT_SCREEN_POS;
212         if (screenId == ScreenSessionManagerClient::GetInstance().GetDefaultScreenId()) {
213             screenOneHandX = SceneSessionManager::GetInstance().GetNormalSingleHandTransform().posX;
214             screenOneHandY = SceneSessionManager::GetInstance().GetNormalSingleHandTransform().posY;
215         }
216         MMI::DisplayInfo displayInfo = {
217             .id = screenId,
218             .x = screenProperty.GetOffsetX(),
219             .y = screenProperty.GetOffsetY(),
220             .width = screenWidth,
221             .height = screenHeight,
222             .dpi = screenProperty.GetDensity() *  DOT_PER_INCH,
223             .name = "display" + std::to_string(screenId),
224             .uniq = "default" + std::to_string(screenId),
225             .direction = ConvertDegreeToMMIRotation(screenProperty.GetPhysicalRotation()),
226             .displayDirection = ConvertDegreeToMMIRotation(screenProperty.GetScreenComponentRotation()),
227             .displayMode = static_cast<MMI::DisplayMode>(displayMode),
228             .transform = transformData,
229             .oneHandX = screenOneHandX,
230             .oneHandY = screenOneHandY
231         };
232         displayInfos.emplace_back(displayInfo);
233     }
234 }
235 
FlushFullInfoToMMI(const std::vector<MMI::DisplayInfo> & displayInfos,const std::vector<MMI::WindowInfo> & windowInfoList)236 void SceneInputManager::FlushFullInfoToMMI(const std::vector<MMI::DisplayInfo>& displayInfos,
237     const std::vector<MMI::WindowInfo>& windowInfoList)
238 {
239     int mainScreenWidth = 0;
240     int mainScreenHeight = 0;
241     if (!displayInfos.empty()) {
242         mainScreenWidth = displayInfos[0].width;
243         mainScreenHeight = displayInfos[0].height;
244     }
245     if (sceneSessionDirty_ == nullptr) {
246         WLOGFE("scene session dirty is null");
247         return;
248     }
249 
250     MMI::DisplayGroupInfo displayGroupInfo = {
251         .width = mainScreenWidth,
252         .height = mainScreenHeight,
253         .focusWindowId = focusedSessionId_,
254         .currentUserId = currentUserId_,
255         .windowsInfo = windowInfoList,
256         .displaysInfo = displayInfos};
257     for (const auto& displayInfo : displayGroupInfo.displaysInfo) {
258         TLOGD(WmsLogTag::WMS_EVENT, "[EventDispatch] - %{public}s", DumpDisplayInfo(displayInfo).c_str());
259     }
260     std::string windowInfoListDump = "windowinfo  ";
261     for (const auto& windowInfo : displayGroupInfo.windowsInfo) {
262         windowInfoListDump.append(DumpWindowInfo(windowInfo).append("  ||  "));
263     }
264     TLOGD(WmsLogTag::WMS_EVENT, "[EventDispatch] - %{public}s", windowInfoListDump.c_str());
265     MMI::InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
266 }
267 
FlushEmptyInfoToMMI()268 void SceneInputManager::FlushEmptyInfoToMMI()
269 {
270     auto task = [this]() {
271         std::vector<MMI::DisplayInfo> displayInfos;
272         ConstructDisplayInfos(displayInfos);
273         int mainScreenWidth = 0;
274         int mainScreenHeight = 0;
275         if (!displayInfos.empty()) {
276             mainScreenWidth = displayInfos[0].width;
277             mainScreenHeight = displayInfos[0].height;
278         }
279         MMI::DisplayGroupInfo displayGroupInfo = {
280             .width = mainScreenWidth,
281             .height = mainScreenHeight,
282             .focusWindowId = EMPTY_FOCUS_WINDOW_ID,
283             .currentUserId = currentUserId_,
284             .displaysInfo = displayInfos
285         };
286         TLOGI(WmsLogTag::WMS_EVENT, "currUserId:%{public}d width:%{public}d height:%{public}d",
287             currentUserId_, mainScreenWidth, mainScreenHeight);
288         MMI::InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
289     };
290     if (eventHandler_) {
291         eventHandler_->PostTask(task);
292     }
293 }
294 
NotifyWindowInfoChange(const sptr<SceneSession> & sceneSession,const WindowUpdateType & type)295 void SceneInputManager::NotifyWindowInfoChange(const sptr<SceneSession>& sceneSession, const WindowUpdateType& type)
296 {
297     if (sceneSessionDirty_) {
298         sceneSessionDirty_->NotifyWindowInfoChange(sceneSession, type);
299     }
300 }
301 
NotifyMMIWindowPidChange(const sptr<SceneSession> & sceneSession,const bool startMoving)302 void SceneInputManager::NotifyMMIWindowPidChange(const sptr<SceneSession>& sceneSession, const bool startMoving)
303 {
304     if (sceneSessionDirty_) {
305         sceneSessionDirty_->NotifyWindowInfoChange(sceneSession,
306             WindowUpdateType::WINDOW_UPDATE_PROPERTY, startMoving);
307         if (sceneSession == nullptr) {
308             return;
309         }
310         sceneSession->SetIsStartMoving(startMoving);
311     }
312 }
313 
NotifyWindowInfoChangeFromSession(const sptr<SceneSession> & sceneSesion)314 void SceneInputManager::NotifyWindowInfoChangeFromSession(const sptr<SceneSession>& sceneSesion)
315 {
316     if (sceneSessionDirty_) {
317         sceneSessionDirty_->NotifyWindowInfoChange(sceneSesion, WindowUpdateType::WINDOW_UPDATE_PROPERTY);
318     }
319 }
320 
FlushChangeInfoToMMI(const std::map<uint64_t,std::vector<MMI::WindowInfo>> & screenId2Windows)321 void SceneInputManager::FlushChangeInfoToMMI(const std::map<uint64_t, std::vector<MMI::WindowInfo>>& screenId2Windows)
322 {
323     for (auto& iter : screenId2Windows) {
324         auto displayId = iter.first;
325         auto& windowInfos = iter.second;
326         std::string windowInfoListDump = "windowinfo  ";
327         for (auto& windowInfo : windowInfos) {
328             windowInfoListDump.append(DumpWindowInfo(windowInfo).append("  ||  "));
329         }
330         TLOGD(WmsLogTag::WMS_EVENT, "[EventDispatch] --- %{public}s", windowInfoListDump.c_str());
331         MMI::WindowGroupInfo windowGroup = {focusedSessionId_, displayId, windowInfos};
332         MMI::InputManager::GetInstance()->UpdateWindowInfo(windowGroup);
333     }
334 }
335 
CheckNeedUpdate(const std::vector<MMI::DisplayInfo> & displayInfos,const std::vector<MMI::WindowInfo> & windowInfoList)336 bool SceneInputManager::CheckNeedUpdate(const std::vector<MMI::DisplayInfo>& displayInfos,
337     const std::vector<MMI::WindowInfo>& windowInfoList)
338 {
339     int32_t focusId = SceneSessionManager::GetInstance().GetFocusedSessionId();
340     if (focusId != lastFocusId_) {
341         lastFocusId_ = focusId;
342         lastDisplayInfos_ = displayInfos;
343         lastWindowInfoList_ = windowInfoList;
344         return true;
345     }
346 
347     if (displayInfos.size() != lastDisplayInfos_.size() || windowInfoList.size() != lastWindowInfoList_.size()) {
348         lastDisplayInfos_ = displayInfos;
349         lastWindowInfoList_ = windowInfoList;
350         return true;
351     }
352 
353     int sizeOfDisplayInfos = static_cast<int>(displayInfos.size());
354     for (int index = 0; index < sizeOfDisplayInfos; index++) {
355         if (!(displayInfos[index] == lastDisplayInfos_[index])) {
356             lastDisplayInfos_ = displayInfos;
357             lastWindowInfoList_ = windowInfoList;
358             return true;
359         }
360     }
361 
362     int sizeOfWindowInfoList = static_cast<int>(windowInfoList.size());
363     for (int index = 0; index < sizeOfWindowInfoList; index++) {
364         if (!(windowInfoList[index] == lastWindowInfoList_[index])) {
365             lastWindowInfoList_ = windowInfoList;
366             return true;
367         }
368     }
369     return false;
370 }
371 
UpdateFocusedSessionId(int32_t focusedSessionId)372 void SceneInputManager::UpdateFocusedSessionId(int32_t focusedSessionId)
373 {
374     auto focusedSceneSession = SceneSessionManager::GetInstance().GetSceneSession(focusedSessionId);
375     if (focusedSceneSession == nullptr) {
376         TLOGE(WmsLogTag::WMS_EVENT, "focusedSceneSession is null");
377         return;
378     }
379     if (auto modalUIExtensionEventInfo = focusedSceneSession->GetLastModalUIExtensionEventInfo()) {
380         focusedSessionId_ = modalUIExtensionEventInfo.value().persistentId;
381     }
382 }
383 
DumpUIExtentionWindowInfo(const MMI::WindowInfo & windowInfo)384 void DumpUIExtentionWindowInfo(const MMI::WindowInfo& windowInfo)
385 {
386     auto sceneSession = SceneSessionManager::GetInstance().GetSceneSession(windowInfo.id);
387     if (sceneSession == nullptr) {
388         TLOGE(WmsLogTag::WMS_EVENT, "sceneSession is null");
389         return;
390     }
391     auto surfaceNode = sceneSession->GetSurfaceNode();
392     if (surfaceNode == nullptr) {
393         TLOGE(WmsLogTag::WMS_EVENT, "surfaceNode is null");
394         return;
395     }
396     auto surfaceId = surfaceNode->GetId();
397     TLOGI(WmsLogTag::WMS_EVENT, "HostId:%{public}d surfaceId:%{public}" PRIu64
398         " uiExtentionWindowInfo:%{public}d",
399         windowInfo.id, surfaceId, static_cast<int>(windowInfo.uiExtentionWindowInfo.size()));
400     for (auto uiExWindowinfo : windowInfo.uiExtentionWindowInfo) {
401         auto str = DumpWindowInfo(uiExWindowinfo);
402         str = "sec:" + std::to_string(uiExWindowinfo.privacyUIFlag) + " " + str;
403         TLOGI(WmsLogTag::WMS_EVENT, "uiExWindowinfo:%{public}s", str.c_str());
404     }
405 }
406 
PrintWindowInfo(const std::vector<MMI::WindowInfo> & windowInfoList)407 void SceneInputManager::PrintWindowInfo(const std::vector<MMI::WindowInfo>& windowInfoList)
408 {
409     int windowListSize = static_cast<int>(windowInfoList.size());
410     std::string idList;
411     static std::string lastIdList;
412     static uint32_t windowEventID = 0;
413     if (windowEventID == UINT32_MAX) {
414         windowEventID = 0;
415     }
416     focusedSessionId_ = SceneSessionManager::GetInstance().GetFocusedSessionId();
417     std::unordered_map<int32_t, MMI::Rect> currWindowDefaultHotArea;
418     static std::unordered_map<int32_t, MMI::Rect> lastWindowDefaultHotArea;
419     for (auto& e : windowInfoList) {
420         idList += std::to_string(e.id) + "|" + std::to_string(e.flags) + "|" +
421             std::to_string(static_cast<int32_t>(e.zOrder)) + "|" +
422             std::to_string(e.pid) + "|" +
423             std::to_string(e.defaultHotAreas.size());
424 
425         if (e.defaultHotAreas.size() > 0) {
426             auto iter = lastWindowDefaultHotArea.find(e.id);
427             if (iter == lastWindowDefaultHotArea.end() || iter->second != e.defaultHotAreas[0]) {
428                 idList += "|" + std::to_string(e.defaultHotAreas[0].x) + "|" +
429                     std::to_string(e.defaultHotAreas[0].y) + "|" +
430                     std::to_string(e.defaultHotAreas[0].width) + "|" +
431                     std::to_string(e.defaultHotAreas[0].height);
432             }
433             currWindowDefaultHotArea.insert({e.id, e.defaultHotAreas[0]});
434         }
435         idList += ",";
436         if ((focusedSessionId_ == e.id) && (e.id == e.agentWindowId)) {
437             UpdateFocusedSessionId(focusedSessionId_);
438         }
439         if (e.uiExtentionWindowInfo.size() > 0) {
440             DumpUIExtentionWindowInfo(e);
441         }
442     }
443     lastWindowDefaultHotArea = currWindowDefaultHotArea;
444     SingleHandTransform transform = SceneSessionManager::GetInstance().GetNormalSingleHandTransform();
445     idList += std::to_string(focusedSessionId_) + "|" +
446         std::to_string(transform.posX) + "|" + std::to_string(transform.posY) + "|" +
447         std::to_string(transform.scaleX) + "|" + std::to_string(transform.scaleY);
448     if (lastIdList != idList) {
449         windowEventID++;
450         TLOGI(WmsLogTag::WMS_EVENT, "eid:%{public}d,size:%{public}d,idList:%{public}s",
451             windowEventID, windowListSize, idList.c_str());
452         lastIdList = idList;
453     }
454 }
455 
PrintDisplayInfo(const std::vector<MMI::DisplayInfo> & displayInfos)456 void SceneInputManager::PrintDisplayInfo(const std::vector<MMI::DisplayInfo>& displayInfos)
457 {
458     int displayListSize = static_cast<int>(displayInfos.size());
459     std::ostringstream displayListStream;
460     static std::string lastDisplayList = "";
461     for (auto& displayInfo : displayInfos) {
462         displayListStream << displayInfo.id << "|" << displayInfo.x << "|" << displayInfo.y << "|"
463                           << displayInfo.width << "|" << displayInfo.height << "|"
464                           << static_cast<int32_t>(displayInfo.direction) << "|"
465                           << static_cast<int32_t>(displayInfo.displayDirection) << "|"
466                           << static_cast<int32_t>(displayInfo.displayMode) << ",";
467     }
468 
469     std::string displayList = displayListStream.str();
470     if (lastDisplayList != displayList) {
471         TLOGI(WmsLogTag::WMS_EVENT, "num:%{public}d,list:%{public}s", displayListSize, displayList.c_str());
472         lastDisplayList = displayList;
473     }
474 }
475 
SetUserBackground(bool userBackground)476 void SceneInputManager::SetUserBackground(bool userBackground)
477 {
478     TLOGI(WmsLogTag::WMS_MULTI_USER, "userBackground = %{public}d", userBackground);
479     isUserBackground_.store(userBackground);
480 }
481 
SetCurrentUserId(int32_t userId)482 void SceneInputManager::SetCurrentUserId(int32_t userId)
483 {
484     TLOGI(WmsLogTag::WMS_MULTI_USER, "Current userId = %{public}d", userId);
485     currentUserId_ = userId;
486     MMI::InputManager::GetInstance()->SetCurrentUser(userId);
487 }
488 
UpdateDisplayAndWindowInfo(const std::vector<MMI::DisplayInfo> & displayInfos,std::vector<MMI::WindowInfo> windowInfoList)489 void SceneInputManager::UpdateDisplayAndWindowInfo(const std::vector<MMI::DisplayInfo>& displayInfos,
490     std::vector<MMI::WindowInfo> windowInfoList)
491 {
492     if (windowInfoList.size() == 0) {
493         return;
494     }
495     int32_t windowBatchSize = MAX_WINDOWINFO_NUM;
496     if (windowInfoList[0].defaultHotAreas.size() > MMI::WindowInfo::DEFAULT_HOTAREA_COUNT) {
497         windowBatchSize = MMI::InputManager::GetInstance()->GetWinSyncBatchSize(
498             static_cast<int32_t>(windowInfoList[0].defaultHotAreas.size()),
499             static_cast<int32_t>(displayInfos.size()));
500     }
501     windowInfoList.back().action = MMI::WINDOW_UPDATE_ACTION::ADD_END;
502     int32_t windowListSize = static_cast<int32_t>(windowInfoList.size());
503     if (windowListSize <= windowBatchSize) {
504         FlushFullInfoToMMI(displayInfos, windowInfoList);
505         return;
506     }
507     auto iterBegin = windowInfoList.begin();
508     auto iterEnd = windowInfoList.end();
509     auto iterNext = std::next(iterBegin, windowBatchSize);
510     FlushFullInfoToMMI(displayInfos, std::vector<MMI::WindowInfo>(iterBegin, iterNext));
511     while (iterNext != iterEnd) {
512         auto iterNewBegin = iterNext;
513         if (iterNewBegin->defaultHotAreas.size() <= MMI::WindowInfo::DEFAULT_HOTAREA_COUNT) {
514             windowBatchSize = MAX_WINDOWINFO_NUM;
515         }
516         if (std::distance(iterNewBegin, iterEnd) <= windowBatchSize) {
517             iterNext = iterEnd;
518         } else {
519             iterNext = std::next(iterNewBegin, windowBatchSize);
520         }
521         std::map<uint64_t, std::vector<MMI::WindowInfo>> screenToWindowInfoList;
522         screenToWindowInfoList.emplace(DEFALUT_DISPLAYID, std::vector<MMI::WindowInfo>(iterNewBegin, iterNext));
523         FlushChangeInfoToMMI(screenToWindowInfoList);
524     }
525 }
526 
FlushDisplayInfoToMMI(const bool forceFlush)527 void SceneInputManager::FlushDisplayInfoToMMI(const bool forceFlush)
528 {
529     auto task = [this, forceFlush]() {
530         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "FlushDisplayInfoToMMI");
531         if (isUserBackground_.load()) {
532             TLOGD(WmsLogTag::WMS_MULTI_USER, "User in background, no need to flush display info");
533             return;
534         }
535         if (sceneSessionDirty_ == nullptr) {
536             TLOGE(WmsLogTag::WMS_EVENT, "sceneSessionDirty_ is nullptr");
537             return;
538         }
539         sceneSessionDirty_->ResetSessionDirty();
540         std::vector<MMI::DisplayInfo> displayInfos;
541         ConstructDisplayInfos(displayInfos);
542         auto [windowInfoList, pixelMapList] = sceneSessionDirty_->GetFullWindowInfoList();
543         if (!forceFlush && !CheckNeedUpdate(displayInfos, windowInfoList)) {
544             return;
545         }
546         PrintDisplayInfo(displayInfos);
547         PrintWindowInfo(windowInfoList);
548         if (windowInfoList.size() == 0) {
549             FlushFullInfoToMMI(displayInfos, windowInfoList);
550             return;
551         }
552         UpdateDisplayAndWindowInfo(displayInfos, std::move(windowInfoList));
553     };
554     if (eventHandler_) {
555         eventHandler_->PostTask(task);
556     }
557 }
558 
UpdateSecSurfaceInfo(const std::map<uint64_t,std::vector<SecSurfaceInfo>> & secSurfaceInfoMap)559 void SceneInputManager::UpdateSecSurfaceInfo(const std::map<uint64_t, std::vector<SecSurfaceInfo>>& secSurfaceInfoMap)
560 {
561     if (sceneSessionDirty_ == nullptr) {
562         TLOGE(WmsLogTag::WMS_EVENT, "sceneSessionDirty_ is nullptr");
563         return;
564     }
565     sceneSessionDirty_->UpdateSecSurfaceInfo(secSurfaceInfoMap);
566 }
567 
UpdateConstrainedModalUIExtInfo(const std::map<uint64_t,std::vector<SecSurfaceInfo>> & constrainedModalUIExtInfoMap)568 void SceneInputManager::UpdateConstrainedModalUIExtInfo(
569     const std::map<uint64_t, std::vector<SecSurfaceInfo>>& constrainedModalUIExtInfoMap)
570 {
571     if (sceneSessionDirty_ == nullptr) {
572         TLOGE(WmsLogTag::WMS_EVENT, "sceneSessionDirty_ is nullptr");
573         return;
574     }
575     sceneSessionDirty_->UpdateConstrainedModalUIExtInfo(constrainedModalUIExtInfoMap);
576 }
577 
GetConstrainedModalExtWindowInfo(const sptr<SceneSession> & sceneSession)578 std::optional<ExtensionWindowEventInfo> SceneInputManager::GetConstrainedModalExtWindowInfo(
579     const sptr<SceneSession>& sceneSession)
580 {
581     if (sceneSession == nullptr) {
582         TLOGE(WmsLogTag::WMS_EVENT, "sceneSession is nullptr");
583         return std::nullopt;
584     }
585     if (sceneSessionDirty_ == nullptr) {
586         TLOGE(WmsLogTag::WMS_EVENT, "sceneSessionDirty_ is nullptr");
587         return std::nullopt;
588     }
589     SecSurfaceInfo constrainedModalUIExtInfo;
590     if (!sceneSessionDirty_->GetLastConstrainedModalUIExtInfo(sceneSession, constrainedModalUIExtInfo)) {
591         return std::nullopt;
592     }
593     auto persistentId = sceneSession->GetUIExtPersistentIdBySurfaceNodeId(constrainedModalUIExtInfo.uiExtensionNodeId);
594     if (persistentId == INVALID_PERSISTENT_ID) {
595         TLOGE(WmsLogTag::WMS_EVENT, "invalid persistentId");
596         return std::nullopt;
597     }
598     return ExtensionWindowEventInfo {
599         .persistentId = persistentId,
600         .pid = constrainedModalUIExtInfo.uiExtensionPid,
601         .isConstrainedModal = true };
602 }
603 }
604 } // namespace OHOS::Rosen
605