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