• 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 <string>
17 #include <iosfwd>
18 #include <memory>
19 
20 #include <napi/native_api.h>
21 #include <napi/native_common.h>
22 
23 #include "new"
24 #include "js_native_api.h"
25 #include "js_native_api_types.h"
26 
27 #include "power_state_machine_info.h"
28 #include "brightness.h"
29 #include "display_common.h"
30 #include "display_log.h"
31 #include "power_mgr_client.h"
32 #ifdef HAS_DFX_HIVIEW_PART
33 #include "xpower_event_js.h"
34 #endif
35 
36 using namespace OHOS::DisplayPowerMgr;
37 using namespace OHOS::PowerMgr;
38 namespace {
39 std::shared_ptr<RunningLock> runningLock =
40     PowerMgrClient::GetInstance().CreateRunningLock(std::string("KeepScreenOn"), RunningLockType::RUNNINGLOCK_SCREEN);
41 }
42 
43 typedef void (*brightness_callback)(void* data);
44 
SyncWorkSendEvent(napi_env env,Brightness * asyncContext,brightness_callback complete,napi_event_priority prio)45 static void SyncWorkSendEvent(napi_env env, Brightness *asyncContext,
46     brightness_callback complete, napi_event_priority prio)
47 {
48     auto task = [asyncContext, complete]() {
49         DISPLAY_HILOGD(COMP_FWK, "CompleteCallback In");
50         if (asyncContext == nullptr) {
51             DISPLAY_HILOGD(COMP_FWK, "asyncContext is nullptr");
52             return;
53         }
54         complete(asyncContext);
55     };
56     if (napi_send_event(env, task, prio) != napi_status::napi_ok) {
57         DISPLAY_HILOGE(COMP_FWK, "failed to SendEvent!");
58         delete asyncContext;
59     }
60 }
61 
SyncWork(napi_env env,const std::string resName,const std::string valName,napi_callback_info & info,brightness_callback complete)62 static napi_value SyncWork(napi_env env, const std::string resName, const std::string valName,
63     napi_callback_info& info, brightness_callback complete)
64 {
65     napi_value undefined;
66     napi_get_undefined(env, &undefined);
67     std::unique_ptr<Brightness> asyncBrightness = std::make_unique<Brightness>(env);
68     RETURN_IF_WITH_RET(asyncBrightness == nullptr, undefined);
69     napi_value options = asyncBrightness->GetCallbackInfo(info, 0, napi_object);
70     RETURN_IF_WITH_RET(options == nullptr, undefined);
71     RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), undefined);
72     if (!valName.empty()) {
73         asyncBrightness->CreateValueRef(options, valName, napi_number);
74     }
75     SyncWorkSendEvent(env, asyncBrightness.get(), complete, napi_eprio_low);
76     asyncBrightness.release();
77     return nullptr;
78 }
79 
GetValue(napi_env env,napi_callback_info info)80 static napi_value GetValue(napi_env env, napi_callback_info info)
81 {
82     return SyncWork(
83         env,
84         "GetValue",
85         "",
86         info,
87         [](void *data) {
88             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
89             if (asyncBrightness != nullptr) {
90                 asyncBrightness->GetValue();
91                 delete asyncBrightness;
92             }
93         }
94     );
95 }
96 
SetValue(napi_env env,napi_callback_info info)97 static napi_value SetValue(napi_env env, napi_callback_info info)
98 {
99     napi_value res = SyncWork(
100         env,
101         "SetValue",
102         Brightness::BRIGHTNESS_VALUE,
103         info,
104         [](void *data) {
105             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
106             if (asyncBrightness != nullptr) {
107                 asyncBrightness->SystemSetValue();
108                 delete asyncBrightness;
109             }
110         }
111     );
112     if (res != nullptr) {
113         Brightness brightness(env);
114         brightness.SetValue(info);
115 #ifdef HAS_DFX_HIVIEW_PART
116         OHOS::HiviewDFX::ReportXPowerJsStackSysEvent(env, "Brightness::SetValue");
117 #endif
118     }
119     return nullptr;
120 }
121 
GetMode(napi_env env,napi_callback_info info)122 static napi_value GetMode(napi_env env, napi_callback_info info)
123 {
124     return SyncWork(
125         env,
126         "GetMode",
127         "",
128         info,
129         [](void *data) {
130             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
131             if (asyncBrightness != nullptr) {
132                 asyncBrightness->GetMode();
133                 delete asyncBrightness;
134             }
135         }
136     );
137 }
138 
SetMode(napi_env env,napi_callback_info info)139 static napi_value SetMode(napi_env env, napi_callback_info info)
140 {
141     return SyncWork(
142         env,
143         "SetMode",
144         Brightness::BRIGHTNESS_MODE,
145         info,
146         [](void *data) {
147             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
148             if (asyncBrightness != nullptr) {
149                 asyncBrightness->SetMode();
150                 delete asyncBrightness;
151             }
152         }
153     );
154 }
155 
SetKeepScreenOnSendEvent(napi_env env,Brightness * asyncContext,napi_event_priority prio)156 static void SetKeepScreenOnSendEvent(napi_env env, Brightness *asyncContext, napi_event_priority prio)
157 {
158     auto task = [env, asyncContext]() {
159         DISPLAY_HILOGD(COMP_FWK, "CompleteCallback In");
160         if (asyncContext == nullptr) {
161             DISPLAY_HILOGD(COMP_FWK, "asyncContext is nullptr");
162             return;
163         }
164         asyncContext->SetKeepScreenOn();
165         delete asyncContext;
166     };
167     if (napi_send_event(env, task, prio) != napi_status::napi_ok) {
168         DISPLAY_HILOGE(COMP_FWK, "failed to SendEvent!");
169         delete asyncContext;
170     }
171 }
172 
SetKeepScreenOn(napi_env env,napi_callback_info info)173 static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info)
174 {
175     std::unique_ptr<Brightness> asyncBrightness = std::make_unique<Brightness>(env, runningLock);
176     RETURN_IF_WITH_RET(asyncBrightness == nullptr, nullptr);
177     napi_value options = asyncBrightness->GetCallbackInfo(info, 0, napi_object);
178     RETURN_IF_WITH_RET(options == nullptr, nullptr);
179     RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), nullptr);
180     asyncBrightness->CreateValueRef(options, Brightness::KEEP_SCREENON, napi_boolean);
181     SetKeepScreenOnSendEvent(env, asyncBrightness.get(), napi_eprio_low);
182     asyncBrightness.release();
183     return nullptr;
184 }
185 
186 EXTERN_C_START
Init(napi_env env,napi_value exports)187 static napi_value Init(napi_env env, napi_value exports)
188 {
189     DISPLAY_HILOGD(COMP_FWK, "brightness init");
190     napi_property_descriptor desc[] = {
191         DECLARE_NAPI_FUNCTION("getValue", GetValue),
192         DECLARE_NAPI_FUNCTION("setValue", SetValue),
193         DECLARE_NAPI_FUNCTION("getMode", GetMode),
194         DECLARE_NAPI_FUNCTION("setMode", SetMode),
195         DECLARE_NAPI_FUNCTION("setKeepScreenOn", SetKeepScreenOn)
196     };
197 
198     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
199     DISPLAY_HILOGD(COMP_FWK, "brightness init end");
200     return exports;
201 }
202 EXTERN_C_END
203 
204 static napi_module g_module = {
205     .nm_version = 1,
206     .nm_flags = 0,
207     .nm_filename = "brightness",
208     .nm_register_func = Init,
209     .nm_modname = "brightness",
210     .nm_priv = ((void *)0),
211     .reserved = { 0 }
212 };
213 
RegisterModule(void)214 extern "C" __attribute__((constructor)) void RegisterModule(void)
215 {
216     napi_module_register(&g_module);
217 }
218