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 #ifndef POWERMGR_BRIGHTNESS_H 17 #define POWERMGR_BRIGHTNESS_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <functional> 22 #include <iosfwd> 23 #include <map> 24 #include <memory> 25 #include <string> 26 27 #include "display_mgr_errors.h" 28 #include "errors.h" 29 #include "napi/native_api.h" 30 #include "napi/native_node_api.h" 31 #include "running_lock.h" 32 33 namespace OHOS { 34 namespace DisplayPowerMgr { 35 class Brightness { 36 public: 37 explicit Brightness(napi_env env, std::shared_ptr<PowerMgr::RunningLock> runningLock = nullptr); 38 ~Brightness(); 39 void GetValue(); 40 void SetValue(napi_callback_info& info); 41 void SystemSetValue(); 42 void GetMode(); 43 void SetMode(); 44 void SetKeepScreenOn(); 45 napi_value GetCallbackInfo(napi_callback_info& info, uint32_t index, napi_valuetype checkType); 46 napi_value GetCallbackInfoWithThrow(napi_callback_info& info, uint32_t index, napi_valuetype checkType); 47 bool CreateCallbackRef(napi_value& options); 48 void CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType); 49 50 napi_async_work asyncWork = nullptr; 51 52 static constexpr const char* BRIGHTNESS_VALUE = "value"; 53 static constexpr const char* BRIGHTNESS_MODE = "mode"; 54 static constexpr const char* KEEP_SCREENON = "keepScreenOn"; 55 56 private: 57 class Result { 58 public: 59 void Error(int32_t code, const std::string& msg = ""); 60 void GetError(napi_env env, napi_value* error, size_t& size) const; 61 napi_value GetError(napi_env& env); 62 napi_value ThrowError(napi_env& env, DisplayErrors code = DisplayErrors::ERR_OK); 63 napi_value GetResult(napi_env env); SetResult(const std::string & key,int32_t value)64 inline void SetResult(const std::string& key, int32_t value) 65 { 66 mapResult_.emplace(key, value); 67 } IsError()68 inline bool IsError() const 69 { 70 return code_ != ERR_OK; 71 } 72 73 private: 74 int32_t code_ {ERR_OK}; 75 std::string msg_; 76 std::map<std::string, int32_t> mapResult_; 77 static std::map<DisplayErrors, std::string> errorTable_; 78 }; 79 80 class BrightnessInfo { 81 public: 82 uint32_t GetBrightness() const; 83 bool SetBrightness(int32_t value, bool continuous); 84 int32_t GetAutoMode() const; 85 bool SetAutoMode(bool mode); 86 void ScreenOn(bool keep, std::shared_ptr<PowerMgr::RunningLock>& runningLock); 87 DisplayErrors GetServiceError() const; 88 }; 89 90 void ExecuteCallback(); 91 bool CheckValueType(napi_value& value, napi_valuetype checkType); 92 napi_value GetOptions(napi_value& options, const std::string& name, napi_valuetype checkType); 93 void CallFunction(napi_ref& callbackRef, size_t argc, napi_value* response); 94 void ReleaseReference(napi_ref& ref); 95 96 napi_env env_; 97 Result result_; 98 BrightnessInfo brightnessInfo_; 99 100 napi_ref successRef_ = nullptr; 101 napi_ref failRef_ = nullptr; 102 napi_ref completeRef_ = nullptr; 103 napi_ref napiValRef_ = nullptr; 104 std::shared_ptr<PowerMgr::RunningLock> runningLock_ = nullptr; 105 }; 106 } // namespace DisplayPowerMgr 107 } // namespace OHOS 108 109 #endif // POWERMGR_BRIGHTNESS_H 110