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