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
UpdateFocusedSessionId(int32_t persistentId)21 WSError FocusGroup::UpdateFocusedSessionId(int32_t persistentId)
22 {
23 TLOGD(WmsLogTag::WMS_FOCUS, "focusedId change: %{public}d -> %{public}d", focusedSessionId_, persistentId);
24 if (focusedSessionId_ == persistentId) {
25 TLOGD(WmsLogTag::WMS_FOCUS, "focus scene not change, id: %{public}d", focusedSessionId_);
26 return WSError::WS_DO_NOTHING;
27 }
28 lastFocusedSessionId_ = focusedSessionId_;
29 focusedSessionId_ = persistentId;
30 return WSError::WS_OK;
31 }
32
UpdateFocusedAppSessionId(int32_t persistentId)33 WSError FocusGroup::UpdateFocusedAppSessionId(int32_t persistentId)
34 {
35 lastFocusedAppSessionId_ = persistentId;
36 return WSError::WS_OK;
37 }
38
WindowFocusController()39 WindowFocusController::WindowFocusController() noexcept
40 {
41 TLOGI(WmsLogTag::WMS_FOCUS, "constructor");
42 AddFocusGroup(DEFAULT_DISPLAY_ID);
43 }
44
GetDisplayGroupId(DisplayId displayId)45 DisplayId WindowFocusController::GetDisplayGroupId(DisplayId displayId)
46 {
47 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
48 if (displayId == DEFAULT_DISPLAY_ID || virtualScreenDisplayIdSet_.size() == 0) {
49 return DEFAULT_DISPLAY_ID;
50 }
51 if (virtualScreenDisplayIdSet_.find(displayId) != virtualScreenDisplayIdSet_.end()) {
52 return displayId;
53 }
54 return DEFAULT_DISPLAY_ID;
55 }
56
AddFocusGroup(DisplayId displayId)57 WSError WindowFocusController::AddFocusGroup(DisplayId displayId)
58 {
59 TLOGI(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
60 if (displayId == DISPLAY_ID_INVALID) {
61 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
62 return WSError::WS_ERROR_INVALID_PARAM;
63 }
64 sptr<FocusGroup> focusGroup = sptr<FocusGroup>::MakeSptr(displayId);
65 focusGroupMap_.insert(std::make_pair(displayId, focusGroup));
66 if (displayId != DEFAULT_DISPLAY_ID) {
67 virtualScreenDisplayIdSet_.emplace(displayId);
68 }
69 return WSError::WS_OK;
70 }
71
RemoveFocusGroup(DisplayId displayId)72 WSError WindowFocusController::RemoveFocusGroup(DisplayId displayId)
73 {
74 TLOGI(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
75 if (displayId == DISPLAY_ID_INVALID) {
76 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
77 return WSError::WS_ERROR_INVALID_PARAM;
78 }
79 focusGroupMap_.erase(displayId);
80 if (displayId != DEFAULT_DISPLAY_ID) {
81 virtualScreenDisplayIdSet_.erase(displayId);
82 }
83 return WSError::WS_OK;
84 }
85
GetFocusGroupInner(DisplayId displayId)86 sptr<FocusGroup> WindowFocusController::GetFocusGroupInner(DisplayId displayId)
87 {
88 DisplayId displayGroupId = GetDisplayGroupId(displayId);
89 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64 ", displayGroupId: %{public}" PRIu64,
90 displayId, displayGroupId);
91 if (displayGroupId == DEFAULT_DISPLAY_ID) {
92 return focusGroupMap_[DEFAULT_DISPLAY_ID];
93 }
94 auto iter = focusGroupMap_.find(displayGroupId);
95 if (iter == focusGroupMap_.end()) {
96 TLOGE(WmsLogTag::WMS_FOCUS, "Not found focus group with displayId: %{public}" PRIu64, displayId);
97 return nullptr;
98 }
99 return iter->second;
100 }
101
GetFocusedSessionId(DisplayId displayId)102 int32_t WindowFocusController::GetFocusedSessionId(DisplayId displayId)
103 {
104 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
105 if (displayId == DISPLAY_ID_INVALID) {
106 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
107 return INVALID_SESSION_ID;
108 }
109 auto focusGroup = GetFocusGroupInner(displayId);
110 if (focusGroup == nullptr) {
111 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
112 return INVALID_SESSION_ID;
113 }
114 return focusGroup->GetFocusedSessionId();
115 }
116
GetFocusGroup(DisplayId displayId)117 sptr<FocusGroup> WindowFocusController::GetFocusGroup(DisplayId displayId)
118 {
119 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64, displayId);
120 if (displayId == DISPLAY_ID_INVALID) {
121 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
122 return nullptr;
123 }
124 return GetFocusGroupInner(displayId);
125 }
126
GetAllFocusedSessionList() const127 std::vector<std::pair<DisplayId, int32_t>> WindowFocusController::GetAllFocusedSessionList() const
128 {
129 std::vector<std::pair<DisplayId, int32_t>> allFocusGroup;
130 for (const auto& elem : focusGroupMap_) {
131 auto curFocusGroup = elem.second;
132 if (curFocusGroup == nullptr) {
133 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null");
134 continue;
135 }
136 allFocusGroup.emplace_back(std::make_pair(elem.first, curFocusGroup->GetFocusedSessionId()));
137 }
138 return allFocusGroup;
139 }
140
UpdateFocusedSessionId(DisplayId displayId,int32_t persistentId)141 WSError WindowFocusController::UpdateFocusedSessionId(DisplayId displayId, int32_t persistentId)
142 {
143 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64 ", persistentId: %{public}d", displayId, persistentId);
144 if (displayId == DISPLAY_ID_INVALID) {
145 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
146 return WSError::WS_ERROR_INVALID_PARAM;
147 }
148 auto focusGroup = GetFocusGroupInner(displayId);
149 if (focusGroup == nullptr) {
150 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
151 return WSError::WS_ERROR_NULLPTR;
152 }
153 return focusGroup->UpdateFocusedSessionId(persistentId);
154 }
155
UpdateFocusedAppSessionId(DisplayId displayId,int32_t persistentId)156 WSError WindowFocusController::UpdateFocusedAppSessionId(DisplayId displayId, int32_t persistentId)
157 {
158 TLOGD(WmsLogTag::WMS_FOCUS, "displayId: %{public}" PRIu64 ", persistentId: %{public}d", displayId, persistentId);
159 if (displayId == DISPLAY_ID_INVALID) {
160 TLOGE(WmsLogTag::WMS_FOCUS, "displayId invalid");
161 return WSError::WS_ERROR_INVALID_PARAM;
162 }
163 auto focusGroup = GetFocusGroupInner(displayId);
164 if (focusGroup == nullptr) {
165 TLOGE(WmsLogTag::WMS_FOCUS, "focus group is null, displayId: %{public}" PRIu64, displayId);
166 return WSError::WS_ERROR_NULLPTR;
167 }
168 return focusGroup->UpdateFocusedAppSessionId(persistentId);
169 }
170 }
171 }
172