• 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 "node_api_types.h"
25 #include "js_native_api.h"
26 #include "js_native_api_types.h"
27 
28 #include "power_state_machine_info.h"
29 #include "brightness.h"
30 #include "display_common.h"
31 #include "display_log.h"
32 #include "power_mgr_client.h"
33 #include "xpower_event_js.h"
34 
35 using namespace OHOS::DisplayPowerMgr;
36 using namespace OHOS::PowerMgr;
37 namespace {
38 std::shared_ptr<RunningLock> runningLock =
39     PowerMgrClient::GetInstance().CreateRunningLock(std::string("KeepScreenOn"), RunningLockType::RUNNINGLOCK_SCREEN);
40 }
41 
SyncWork(napi_env env,const std::string resName,const std::string valName,napi_callback_info & info,napi_async_complete_callback complete)42 static napi_value SyncWork(napi_env env, const std::string resName, const std::string valName,
43     napi_callback_info& info, napi_async_complete_callback complete)
44 {
45     napi_value undefined;
46     napi_get_undefined(env, &undefined);
47     std::unique_ptr<Brightness> asyncBrightness = std::make_unique<Brightness>(env);
48     RETURN_IF_WITH_RET(asyncBrightness == nullptr, undefined);
49     napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object);
50     RETURN_IF_WITH_RET(options == nullptr, undefined);
51     RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), undefined);
52     if (!valName.empty()) {
53         asyncBrightness->CreateValueRef(options, valName, napi_number);
54     }
55 
56     napi_value resource = nullptr;
57     NAPI_CALL(env, napi_create_string_utf8(env, resName.c_str(), NAPI_AUTO_LENGTH, &resource));
58     napi_create_async_work(
59         env,
60         nullptr,
61         resource,
62         [](napi_env env, void *data) {},
63         complete,
64         reinterpret_cast<void*>(asyncBrightness.get()),
65         &asyncBrightness->asyncWork);
66     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncBrightness->asyncWork, napi_qos_utility));
67     asyncBrightness.release();
68 
69     return nullptr;
70 }
71 
GetValue(napi_env env,napi_callback_info info)72 static napi_value GetValue(napi_env env, napi_callback_info info)
73 {
74     return SyncWork(
75         env,
76         "GetValue",
77         "",
78         info,
79         [](napi_env env, napi_status status, void *data) {
80             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
81             if (asyncBrightness != nullptr) {
82                 asyncBrightness->GetValue();
83                 napi_delete_async_work(env, asyncBrightness->asyncWork);
84                 delete asyncBrightness;
85             }
86         }
87     );
88 }
89 
SetValue(napi_env env,napi_callback_info info)90 static napi_value SetValue(napi_env env, napi_callback_info info)
91 {
92     napi_value res = SyncWork(
93         env,
94         "SetValue",
95         Brightness::BRIGHTNESS_VALUE,
96         info,
97         [](napi_env env, napi_status status, void *data) {
98             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
99             if (asyncBrightness != nullptr) {
100                 asyncBrightness->SystemSetValue();
101                 napi_delete_async_work(env, asyncBrightness->asyncWork);
102                 delete asyncBrightness;
103             }
104         }
105     );
106     if (res != nullptr) {
107         Brightness brightness(env);
108         brightness.SetValue(info);
109         OHOS::HiviewDFX::ReportXPowerJsStackSysEvent(env, "Brightness::SetValue");
110     }
111     return nullptr;
112 }
113 
GetMode(napi_env env,napi_callback_info info)114 static napi_value GetMode(napi_env env, napi_callback_info info)
115 {
116     return SyncWork(
117         env,
118         "GetMode",
119         "",
120         info,
121         [](napi_env env, napi_status status, void *data) {
122             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
123             if (asyncBrightness != nullptr) {
124                 asyncBrightness->GetMode();
125                 napi_delete_async_work(env, asyncBrightness->asyncWork);
126                 delete asyncBrightness;
127             }
128         }
129     );
130 }
131 
SetMode(napi_env env,napi_callback_info info)132 static napi_value SetMode(napi_env env, napi_callback_info info)
133 {
134     return SyncWork(
135         env,
136         "SetMode",
137         Brightness::BRIGHTNESS_MODE,
138         info,
139         [](napi_env env, napi_status status, void *data) {
140             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
141             if (asyncBrightness != nullptr) {
142                 asyncBrightness->SetMode();
143                 napi_delete_async_work(env, asyncBrightness->asyncWork);
144                 delete asyncBrightness;
145             }
146         }
147     );
148 }
149 
SetKeepScreenOn(napi_env env,napi_callback_info info)150 static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info)
151 {
152     std::unique_ptr<Brightness> asyncBrightness = std::make_unique<Brightness>(env, runningLock);
153     RETURN_IF_WITH_RET(asyncBrightness == nullptr, nullptr);
154     napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object);
155     RETURN_IF_WITH_RET(options == nullptr, nullptr);
156     RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), nullptr);
157     asyncBrightness->CreateValueRef(options, Brightness::KEEP_SCREENON, napi_boolean);
158 
159     napi_value resource = nullptr;
160     NAPI_CALL(env, napi_create_string_utf8(env, "SetKeepScreenOn", NAPI_AUTO_LENGTH, &resource));
161     napi_create_async_work(
162         env,
163         nullptr,
164         resource,
165         [](napi_env env, void *data) {},
166         [](napi_env env, napi_status status, void *data) {
167             Brightness *asyncBrightness = reinterpret_cast<Brightness*>(data);
168             if (asyncBrightness != nullptr) {
169                 asyncBrightness->SetKeepScreenOn();
170                 napi_delete_async_work(env, asyncBrightness->asyncWork);
171                 delete asyncBrightness;
172             }
173         },
174         reinterpret_cast<void*>(asyncBrightness.get()),
175         &asyncBrightness->asyncWork);
176     NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncBrightness->asyncWork, napi_qos_utility));
177     asyncBrightness.release();
178 
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_HILOGI(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_HILOGI(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