1 /*
2 * Copyright (c) 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 "session_manager/include/window_focus_controller.h"
17
18 namespace OHOS {
19 namespace Rosen {
20
21 // LCOV_EXCL_START
UpdateFocusedSessionId(int32_t persistentId)22 WSError FocusGroup::UpdateFocusedSessionId(int32_t persistentId)
23 {
24 TLOGD(WmsLogTag::WMS_FOCUS, "focusedId change: %{public}d -> %{public}d", focusedSessionId_, persistentId);
25 if (focusedSessionId_ == persistentId) {
26 TLOGD(WmsLogTag::WMS_FOCUS, "focus scene not change, id: %{public}d", focusedSessionId_);
27 return WSError::WS_DO_NOTHING;
28 }
29 lastFocusedSessionId_ = focusedSessionId_;
30 focusedSessionId_ = persistentId;
31 return WSError::WS_OK;
32 }
33
UpdateFocusedAppSessionId(int32_t persistentId)34 WSError FocusGroup::UpdateFocusedAppSessionId(int32_t persistentId)
35 {
36 lastFocusedAppSessionId_ = persistentId;
37 return WSError::WS_OK;
38 }
39 // LCOV_EXCL_STOP
40
WindowFocusController()41 WindowFocusController::WindowFocusController() noexcept
42 {
43 TLOGI(WmsLogTag::WMS_FOCUS, "constructor");
44 AddFocusGroup(DEFAULT_DISPLAY_ID, DEFAULT_DISPLAY_ID);
45 }
46
GetDisplayGroupId(DisplayId displayId)47 DisplayId WindowFocusController::GetDisplayGroupId(DisplayId displayId)
48 {
49 if (displayId == DEFAULT_DISPLAY_ID) {
50 return DEFAULT_DISPLAY_ID;
51 }
52 auto iter = displayId2GroupIdMap_.find(displayId);
53 if (iter != displayId2GroupIdMap_.end()) {
54 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64", displayGroupId: %{public}" PRIu64,
55 displayId, iter->second);
56 return iter->second;
57 }
58 if (deletedDisplayId2GroupIdMap_.find(displayId) != deletedDisplayId2GroupIdMap_.end()) {
59 return DISPLAY_ID_INVALID;
60 } else {
61 return DEFAULT_DISPLAY_ID;
62 }
63 }
64
AddFocusGroup(DisplayGroupId displayGroupId,DisplayId displayId)65 WSError WindowFocusController::AddFocusGroup(DisplayGroupId displayGroupId, DisplayId displayId)
66 {
67 TLOGI(WmsLogTag::WMS_FOCUS,
68 "displayId: %{public}" PRIu64", displayGroupId: %{public}" PRIu64,
69 displayId,
70 displayGroupId);
71 if (displayId == DISPLAY_ID_INVALID) {
72 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
73 return WSError::WS_ERROR_INVALID_PARAM;
74 }
75 displayId2GroupIdMap_[displayId] = displayGroupId;
76 auto iter = focusGroupMap_.find(displayGroupId);
77 if (iter == focusGroupMap_.end()) {
78 sptr<FocusGroup> focusGroup = sptr<FocusGroup>::MakeSptr(displayGroupId);
79 focusGroup->displayIds_.insert(displayId);
80 focusGroupMap_.insert(std::make_pair(displayGroupId, focusGroup));
81 LogDisplayIds();
82 } else {
83 iter->second->displayIds_.insert(displayId);
84 LogDisplayIds();
85 }
86 if (deletedDisplayId2GroupIdMap_.find(displayId) != deletedDisplayId2GroupIdMap_.end()) {
87 deletedDisplayId2GroupIdMap_.erase(displayId);
88 }
89 return WSError::WS_OK;
90 }
91
92 // LCOV_EXCL_START
RemoveFocusGroup(DisplayGroupId displayGroupId,DisplayId displayId)93 WSError WindowFocusController::RemoveFocusGroup(DisplayGroupId displayGroupId, DisplayId displayId)
94 {
95 TLOGI(WmsLogTag::WMS_FOCUS,
96 "displayId: %{public}" PRIu64", displayGroupId: %{public}" PRIu64,
97 displayId,
98 displayGroupId);
99 if (displayId == DISPLAY_ID_INVALID) {
100 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
101 return WSError::WS_ERROR_INVALID_PARAM;
102 }
103 auto iter = focusGroupMap_.find(displayGroupId);
104 if (iter != focusGroupMap_.end()) {
105 auto displayIds = iter->second->displayIds_;
106 displayIds.erase(displayId);
107 if (displayIds.size() == 0) {
108 focusGroupMap_.erase(displayGroupId);
109 }
110 } else {
111 TLOGE(WmsLogTag::WMS_FOCUS, "displayGroupId invalid, displayGroupId: %{public}" PRIu64, displayGroupId);
112 }
113 displayId2GroupIdMap_.erase(displayId);
114 deletedDisplayId2GroupIdMap_[displayId] = displayGroupId;
115 LogDisplayIds();
116 return WSError::WS_OK;
117 }
118 // LCOV_EXCL_STOP
119
GetFocusGroupInner(DisplayId displayId)120 sptr<FocusGroup> WindowFocusController::GetFocusGroupInner(DisplayId displayId)
121 {
122 DisplayId displayGroupId = GetDisplayGroupId(displayId);
123 if (displayGroupId == DEFAULT_DISPLAY_ID) {
124 return focusGroupMap_[DEFAULT_DISPLAY_ID];
125 }
126 auto iter = focusGroupMap_.find(displayGroupId);
127 if (iter == focusGroupMap_.end()) {
128 TLOGE(WmsLogTag::WMS_FOCUS, "Not found focus group with displayId: %{public}" PRIu64, displayId);
129 return nullptr;
130 }
131 return iter->second;
132 }
133
GetFocusedSessionId(DisplayId displayId)134 int32_t WindowFocusController::GetFocusedSessionId(DisplayId displayId)
135 {
136 if (displayId == DISPLAY_ID_INVALID) {
137 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
138 return INVALID_SESSION_ID;
139 }
140 auto focusGroup = GetFocusGroupInner(displayId);
141 if (focusGroup == nullptr) {
142 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
143 return INVALID_SESSION_ID;
144 }
145 return focusGroup->GetFocusedSessionId();
146 }
147
GetFocusGroup(DisplayId displayId)148 sptr<FocusGroup> WindowFocusController::GetFocusGroup(DisplayId displayId)
149 {
150 if (displayId == DISPLAY_ID_INVALID) {
151 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
152 return nullptr;
153 }
154 return GetFocusGroupInner(displayId);
155 }
156
GetAllFocusedSessionList() const157 std::vector<std::pair<DisplayId, int32_t>> WindowFocusController::GetAllFocusedSessionList() const
158 {
159 std::vector<std::pair<DisplayId, int32_t>> allFocusGroup;
160 for (const auto& elem : focusGroupMap_) {
161 auto curFocusGroup = elem.second;
162 if (curFocusGroup == nullptr) {
163 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null");
164 continue;
165 }
166 allFocusGroup.emplace_back(std::make_pair(elem.first, curFocusGroup->GetFocusedSessionId()));
167 }
168 return allFocusGroup;
169 }
170
171 // LCOV_EXCL_START
UpdateFocusedSessionId(DisplayId displayId,int32_t persistentId)172 WSError WindowFocusController::UpdateFocusedSessionId(DisplayId displayId, int32_t persistentId)
173 {
174 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64 ", persistentId: %{public}d", displayId, persistentId);
175 if (displayId == DISPLAY_ID_INVALID) {
176 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
177 return WSError::WS_ERROR_INVALID_PARAM;
178 }
179 auto focusGroup = GetFocusGroupInner(displayId);
180 if (focusGroup == nullptr) {
181 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
182 return WSError::WS_ERROR_NULLPTR;
183 }
184 return focusGroup->UpdateFocusedSessionId(persistentId);
185 }
186
UpdateFocusedAppSessionId(DisplayId displayId,int32_t persistentId)187 WSError WindowFocusController::UpdateFocusedAppSessionId(DisplayId displayId, int32_t persistentId)
188 {
189 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64 ", persistentId: %{public}d", displayId, persistentId);
190 if (displayId == DISPLAY_ID_INVALID) {
191 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
192 return WSError::WS_ERROR_INVALID_PARAM;
193 }
194 auto focusGroup = GetFocusGroupInner(displayId);
195 if (focusGroup == nullptr) {
196 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
197 return WSError::WS_ERROR_NULLPTR;
198 }
199 return focusGroup->UpdateFocusedAppSessionId(persistentId);
200 }
201 // LCOV_EXCL_STOP
202
LogDisplayIds()203 void WindowFocusController::LogDisplayIds()
204 {
205 std::ostringstream oss;
206 {
207 for (auto it = focusGroupMap_.begin(); it != focusGroupMap_.end(); it++) {
208 oss << "focusGroupId: " << it->first << ", displayids:";
209 auto displayIds = it->second->displayIds_;
210 for (auto it2 = displayIds.begin(); it2 != displayIds.end(); it2++) {
211 oss << *it2;
212 if (std::next(it2) != displayIds.end()) {
213 oss << ",";
214 } else {
215 oss << ";";
216 }
217 }
218 }
219 for (auto it = displayId2GroupIdMap_.begin(); it != displayId2GroupIdMap_.end(); it++) {
220 oss << "displayId2GroupIdMap:" << it->first << "-" << it->second;
221 if (std::next(it) != displayId2GroupIdMap_.end()) {
222 oss << ", ";
223 }
224 }
225 }
226 TLOGI(WmsLogTag::WMS_FOCUS, "%{public}s", oss.str().c_str());
227 }
228 } // namespace Rosen
229 } // namespace OHOS
230