* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "power_state_machine_info.h"
#include "napi/native_node_api.h"
#include "power.h"
#include "power_log.h"
#include "power_mode_info.h"
#include "power_napi.h"
using namespace OHOS::PowerMgr;
static napi_value EnumPowerModeClassConstructor(napi_env env, napi_callback_info info)
{
napi_value thisArg = nullptr;
void* data = nullptr;
napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
napi_value global = nullptr;
napi_get_global(env, &global);
return thisArg;
}
static napi_value CreateDevicePowerMode(napi_env env, napi_value exports)
{
napi_value normal = nullptr;
napi_value powerSave = nullptr;
napi_value performance = nullptr;
napi_value extremePowerSave = nullptr;
napi_value customPowerSave = nullptr;
napi_create_int32(env, (int32_t)PowerMode::NORMAL_MODE, &normal);
napi_create_int32(env, (int32_t)PowerMode::POWER_SAVE_MODE, &powerSave);
napi_create_int32(env, (int32_t)PowerMode::PERFORMANCE_MODE, &performance);
napi_create_int32(env, (int32_t)PowerMode::EXTREME_POWER_SAVE_MODE, &extremePowerSave);
napi_create_int32(env, (int32_t)PowerMode::CUSTOM_POWER_SAVE_MODE, &customPowerSave);
napi_property_descriptor desc[] = {
DECLARE_NAPI_STATIC_PROPERTY("MODE_NORMAL", normal),
DECLARE_NAPI_STATIC_PROPERTY("MODE_POWER_SAVE", powerSave),
DECLARE_NAPI_STATIC_PROPERTY("MODE_PERFORMANCE", performance),
DECLARE_NAPI_STATIC_PROPERTY("MODE_EXTREME_POWER_SAVE", extremePowerSave),
DECLARE_NAPI_STATIC_PROPERTY("MODE_CUSTOM_POWER_SAVE", customPowerSave),
};
napi_value result = nullptr;
napi_define_class(env, "DevicePowerMode", NAPI_AUTO_LENGTH, EnumPowerModeClassConstructor, nullptr,
sizeof(desc) / sizeof(*desc), desc, &result);
napi_set_named_property(env, exports, "DevicePowerMode", result);
return exports;
}
static napi_value EnumPowerKeyFilteringStrategyClassConstructor(napi_env env, napi_callback_info info)
{
napi_value thisArg = nullptr;
void* data = nullptr;
napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
napi_value global = nullptr;
napi_get_global(env, &global);
return thisArg;
}
static napi_value CreatePowerKeyFilteringStrategy(napi_env env, napi_value exports)
{
napi_value disableLongPressFiltering = nullptr;
napi_value longPressFilteringOnce = nullptr;
napi_create_int32(env, (int32_t)PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING,
&disableLongPressFiltering);
napi_create_int32(env, (int32_t)PowerKeyFilteringStrategy::LONG_PRESS_FILTERING_ONCE, &longPressFilteringOnce);
napi_property_descriptor desc[] = {
DECLARE_NAPI_STATIC_PROPERTY("DISABLE_LONG_PRESS_FILTERING", disableLongPressFiltering),
DECLARE_NAPI_STATIC_PROPERTY("LONG_PRESS_FILTERING_ONCE", longPressFilteringOnce),
};
napi_value result = nullptr;
napi_define_class(env, "PowerKeyFilteringStrategy", NAPI_AUTO_LENGTH, EnumPowerKeyFilteringStrategyClassConstructor,
nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
napi_set_named_property(env, exports, "PowerKeyFilteringStrategy", result);
return exports;
}
EXTERN_C_START
* function for module exports
*/
static napi_value PowerInit(napi_env env, napi_value exports)
{
POWER_HILOGD(COMP_FWK, "Initialize the Power module");
napi_property_descriptor desc[] = {
DECLARE_NAPI_FUNCTION("shutdownDevice", Power::ShutdownDevice),
DECLARE_NAPI_FUNCTION("rebootDevice", Power::RebootDevice),
DECLARE_NAPI_FUNCTION("isScreenOn", Power::IsScreenOn),
DECLARE_NAPI_FUNCTION("wakeupDevice", Power::WakeupDevice),
DECLARE_NAPI_FUNCTION("suspendDevice", Power::SuspendDevice),
DECLARE_NAPI_FUNCTION("shutdown", PowerNapi::Shutdown),
DECLARE_NAPI_FUNCTION("reboot", PowerNapi::Reboot),
DECLARE_NAPI_FUNCTION("isActive", PowerNapi::IsActive),
DECLARE_NAPI_FUNCTION("wakeup", PowerNapi::Wakeup),
DECLARE_NAPI_FUNCTION("suspend", PowerNapi::Suspend),
DECLARE_NAPI_FUNCTION("hibernate", PowerNapi::Hibernate),
DECLARE_NAPI_FUNCTION("setPowerMode", PowerNapi::SetPowerMode),
DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode),
DECLARE_NAPI_FUNCTION("getPowerConfig", PowerNapi::GetPowerConfig),
DECLARE_NAPI_FUNCTION("setPowerConfig", PowerNapi::SetPowerConfig),
DECLARE_NAPI_FUNCTION("isStandby", PowerNapi::IsStandby),
DECLARE_NAPI_FUNCTION("setScreenOffTime", PowerNapi::SetScreenOffTime),
DECLARE_NAPI_FUNCTION("refreshActivity", PowerNapi::RefreshActivity),
DECLARE_NAPI_FUNCTION("setPowerKeyFilteringStrategy", PowerNapi::SetPowerKeyFilteringStrategy),
DECLARE_NAPI_FUNCTION("registerShutdownCallback", PowerNapi::RegisterShutdownCallback),
DECLARE_NAPI_FUNCTION("unregisterShutdownCallback", PowerNapi::UnRegisterShutdownCallback)};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
CreateDevicePowerMode(env, exports);
CreatePowerKeyFilteringStrategy(env, exports);
POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete");
return exports;
}
EXTERN_C_END
* Module definition
*/
static napi_module g_module = {.nm_version = 1,
.nm_flags = 0,
.nm_filename = "power",
.nm_register_func = PowerInit,
.nm_modname = "power",
.nm_priv = ((void*)0),
.reserved = {0}};
* Module registration
*/
extern "C" __attribute__((constructor)) void RegisterPowerModule(void)
{
napi_module_register(&g_module);
}