1 /*
2 * Copyright (c) 2024 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 "cj_display_listener.h"
17
18 #include <functional>
19 #include <hitrace_meter.h>
20 #include <mutex>
21
22 #include "cj_lambda.h"
23 #include "display_ffi.h"
24 #include "display_utils.h"
25 #include "dm_common.h"
26 #include "window_manager_hilog.h"
27
28 namespace OHOS {
29 namespace Rosen {
30
CJDisplayListener()31 CJDisplayListener::CJDisplayListener()
32 {
33 TLOGD(WmsLogTag::DMS, "Constructor execution");
34 }
35
~CJDisplayListener()36 CJDisplayListener::~CJDisplayListener()
37 {
38 TLOGD(WmsLogTag::DMS, "Destructor execution");
39 }
40
AddCallback(const std::string & type,int64_t callbackObject)41 void CJDisplayListener::AddCallback(const std::string& type, int64_t callbackObject)
42 {
43 std::lock_guard<std::mutex> lock(mtx_);
44 TLOGI(WmsLogTag::DMS, "called with type %{public}s", type.c_str());
45 auto callback = CJLambda::Create(reinterpret_cast<void (*)(void*)>(callbackObject));
46 cjCallBack_[type][callbackObject] = callback;
47 }
48
RemoveAllCallback()49 void CJDisplayListener::RemoveAllCallback()
50 {
51 std::lock_guard<std::mutex> lock(mtx_);
52 TLOGD(WmsLogTag::DMS, "called");
53 cjCallBack_.clear();
54 }
55
RemoveCallback(const std::string & type,int64_t callbackObject)56 void CJDisplayListener::RemoveCallback(const std::string& type, int64_t callbackObject)
57 {
58 std::lock_guard<std::mutex> lock(mtx_);
59 TLOGI(WmsLogTag::DMS, "with type %{public}s", type.c_str());
60 auto it = cjCallBack_.find(type);
61 if (it == cjCallBack_.end()) {
62 TLOGE(WmsLogTag::DMS, "no callback to remove");
63 return;
64 }
65 auto& callbacks = it->second;
66 for (auto iter = callbacks.begin(); iter != callbacks.end();) {
67 if (iter->first == callbackObject) {
68 iter = callbacks.erase(iter);
69 } else {
70 iter++;
71 }
72 }
73 TLOGI(WmsLogTag::DMS, "succ cjCallBack_ size %{public}u!",
74 static_cast<uint32_t>(callbacks.size()));
75 }
76
OnCreate(DisplayId id)77 void CJDisplayListener::OnCreate(DisplayId id) {}
78
OnDestroy(DisplayId id)79 void CJDisplayListener::OnDestroy(DisplayId id) {}
80
OnChange(DisplayId id)81 void CJDisplayListener::OnChange(DisplayId id)
82 {
83 std::lock_guard<std::mutex> lock(mtx_);
84 TLOGI(WmsLogTag::DMS, "called, displayId: %{public}d", static_cast<uint32_t>(id));
85 if (cjCallBack_.empty()) {
86 TLOGE(WmsLogTag::DMS, "not register!");
87 return;
88 }
89 if (cjCallBack_.find(EVENT_CHANGE) == cjCallBack_.end()) {
90 TLOGE(WmsLogTag::DMS, "not this event, return");
91 return;
92 }
93 CallCJMethod(EVENT_CHANGE, &id);
94 }
95
OnPrivateWindow(bool hasPrivate)96 void CJDisplayListener::OnPrivateWindow(bool hasPrivate)
97 {
98 std::lock_guard<std::mutex> lock(mtx_);
99 TLOGI(WmsLogTag::DMS, "called");
100 if (cjCallBack_.empty()) {
101 TLOGI(WmsLogTag::DMS, "not register!");
102 return;
103 }
104 if (cjCallBack_.find(EVENT_PRIVATE_MODE_CHANGE) == cjCallBack_.end()) {
105 TLOGE(WmsLogTag::DMS, "not this event, return");
106 return;
107 }
108 CallCJMethod(EVENT_PRIVATE_MODE_CHANGE, &hasPrivate);
109 }
110
OnFoldStatusChanged(FoldStatus foldStatus)111 void CJDisplayListener::OnFoldStatusChanged(FoldStatus foldStatus)
112 {
113 std::lock_guard<std::mutex> lock(mtx_);
114 TLOGI(WmsLogTag::DMS, "called");
115 if (cjCallBack_.empty()) {
116 TLOGE(WmsLogTag::DMS, "not register!");
117 return;
118 }
119 if (cjCallBack_.find(EVENT_FOLD_STATUS_CHANGED) == cjCallBack_.end()) {
120 TLOGE(WmsLogTag::DMS, "not this event, return");
121 return;
122 }
123 CallCJMethod(EVENT_FOLD_STATUS_CHANGED, &foldStatus);
124 }
125
OnFoldAngleChanged(std::vector<float> foldAngles)126 void CJDisplayListener::OnFoldAngleChanged(std::vector<float> foldAngles)
127 {
128 std::lock_guard<std::mutex> lock(mtx_);
129 TLOGI(WmsLogTag::DMS, "called");
130 if (cjCallBack_.empty()) {
131 TLOGE(WmsLogTag::DMS, "Cnot register!");
132 return;
133 }
134 if (cjCallBack_.find(EVENT_FOLD_ANGLE_CHANGED) == cjCallBack_.end()) {
135 TLOGE(WmsLogTag::DMS, "not this event, return");
136 return;
137 }
138 CallCJMethod(EVENT_FOLD_ANGLE_CHANGED, &foldAngles);
139 }
140
OnCaptureStatusChanged(bool isCapture)141 void CJDisplayListener::OnCaptureStatusChanged(bool isCapture)
142 {
143 std::lock_guard<std::mutex> lock(mtx_);
144 TLOGI(WmsLogTag::DMS, "called!");
145 if (cjCallBack_.empty()) {
146 TLOGE(WmsLogTag::DMS, "not register!");
147 return;
148 }
149 if (cjCallBack_.find(EVENT_CAPTURE_STATUS_CHANGED) == cjCallBack_.end()) {
150 TLOGE(WmsLogTag::DMS, "not this event, return");
151 return;
152 }
153 CallCJMethod(EVENT_CAPTURE_STATUS_CHANGED, &isCapture);
154 }
155
OnDisplayModeChanged(FoldDisplayMode displayMode)156 void CJDisplayListener::OnDisplayModeChanged(FoldDisplayMode displayMode)
157 {
158 std::lock_guard<std::mutex> lock(mtx_);
159 TLOGI(WmsLogTag::DMS, "called!");
160 if (cjCallBack_.empty()) {
161 TLOGE(WmsLogTag::DMS, "not register!");
162 return;
163 }
164 if (cjCallBack_.find(EVENT_DISPLAY_MODE_CHANGED) == cjCallBack_.end()) {
165 TLOGE(WmsLogTag::DMS, "not this event, return");
166 return;
167 }
168 CallCJMethod(EVENT_DISPLAY_MODE_CHANGED, &displayMode);
169 }
170
OnAvailableAreaChanged(DMRect area)171 void CJDisplayListener::OnAvailableAreaChanged(DMRect area) {}
172
CallCJMethod(const std::string & methodName,void * argv)173 void CJDisplayListener::CallCJMethod(const std::string& methodName, void* argv)
174 {
175 if (methodName.empty()) {
176 TLOGE(WmsLogTag::DMS, "empty method name str, call method failed");
177 return;
178 }
179 TLOGD(WmsLogTag::DMS, "CallCJMethod methodName = %{public}s", methodName.c_str());
180 for (auto callback : cjCallBack_[methodName]) {
181 callback.second(argv);
182 }
183 }
184 } // namespace Rosen
185 } // namespace OHOS