1 /* 2 * Copyright (c) 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_WAKEUP_CONTROLLER_H 17 #define POWERMGR_WAKEUP_CONTROLLER_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <vector> 23 24 #include "i_input_event_consumer.h" 25 #include "power_state_machine.h" 26 #include "system_ability.h" 27 #include "sensor_agent.h" 28 #include "wakeup_sources.h" 29 #include "wakeup_source_parser.h" 30 31 namespace OHOS { 32 namespace PowerMgr { 33 34 using WakeupListener = std::function<void(WakeupDeviceType)>; 35 using namespace OHOS::MMI; 36 class WakeupMonitor; 37 class WakeupController : public std::enable_shared_from_this<WakeupController> { 38 public: 39 explicit WakeupController(std::shared_ptr<PowerStateMachine>& stateMachine); 40 ~WakeupController(); 41 void Init(); 42 void Cancel(); 43 void RegisterSettingsObserver(); 44 void ExecWakeupMonitorByReason(WakeupDeviceType reason); 45 void Wakeup(); 46 void NotifyDisplayActionDone(uint32_t event); GetStateMachine()47 std::shared_ptr<PowerStateMachine> GetStateMachine() 48 { 49 return stateMachine_; 50 } GetLastReason()51 WakeupDeviceType GetLastReason() const 52 { 53 return wakeupReason_; 54 } 55 bool CheckEventReciveTime(WakeupDeviceType wakeupType); 56 57 private: 58 void ControlListener(WakeupDeviceType reason); 59 void StartWakeupTimer(); 60 void HandleScreenOnTimeout(); 61 std::vector<WakeupSource> sourceList_; 62 std::map<WakeupDeviceType, std::shared_ptr<WakeupMonitor>> monitorMap_; 63 std::map<WakeupDeviceType, int64_t> eventHandleMap_; 64 std::shared_ptr<PowerStateMachine> stateMachine_; 65 WakeupDeviceType wakeupReason_ {0}; 66 std::mutex mutex_; 67 std::mutex monitorMutex_; 68 std::mutex eventHandleMutex_; 69 int32_t monitorId_ {-1}; 70 }; 71 72 class InputCallback : public IInputEventConsumer { 73 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const; 74 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const; 75 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const; 76 }; 77 78 class WakeupMonitor { 79 public: 80 static std::shared_ptr<WakeupMonitor> CreateMonitor(WakeupSource& source); 81 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 82 83 virtual ~WakeupMonitor() = default; 84 virtual bool Init() = 0; 85 virtual void Cancel() = 0; HandleEvent(uint32_t eventId)86 virtual void HandleEvent(uint32_t eventId) 87 { 88 // do nothing in base class 89 } 90 GetReason()91 WakeupDeviceType GetReason() const 92 { 93 return reason_; 94 } 95 RegisterListener(WakeupListener listener)96 void RegisterListener(WakeupListener listener) 97 { 98 listener_ = listener; 99 } 100 Notify()101 void Notify() 102 { 103 listener_(reason_); 104 } 105 106 protected: WakeupMonitor(WakeupSource & source)107 explicit WakeupMonitor(WakeupSource& source) 108 { 109 reason_ = source.GetReason(); 110 } 111 112 WakeupDeviceType reason_; 113 WakeupListener listener_; 114 }; 115 116 class PowerkeyWakeupMonitor : public WakeupMonitor { 117 public: PowerkeyWakeupMonitor(WakeupSource & source)118 explicit PowerkeyWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 119 ~PowerkeyWakeupMonitor() override = default; 120 bool Init() override; 121 void Cancel() override; 122 123 private: 124 int32_t powerkeyShortPressId_ {-1}; 125 }; 126 127 class KeyboardWakeupMonitor : public WakeupMonitor { 128 public: KeyboardWakeupMonitor(WakeupSource & source)129 explicit KeyboardWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 130 ~KeyboardWakeupMonitor() override = default; 131 bool Init() override; 132 void Cancel() override; 133 }; 134 135 class MousekeyWakeupMonitor : public WakeupMonitor { 136 public: MousekeyWakeupMonitor(WakeupSource & source)137 explicit MousekeyWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 138 ~MousekeyWakeupMonitor() override = default; 139 bool Init() override; 140 void Cancel() override; 141 }; 142 143 class TouchpadWakeupMonitor : public WakeupMonitor { 144 public: TouchpadWakeupMonitor(WakeupSource & source)145 explicit TouchpadWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 146 ~TouchpadWakeupMonitor() override = default; 147 bool Init() override; 148 void Cancel() override; 149 }; 150 151 class PenWakeupMonitor : public WakeupMonitor { 152 public: PenWakeupMonitor(WakeupSource & source)153 explicit PenWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 154 ~PenWakeupMonitor() override = default; 155 bool Init() override; 156 void Cancel() override; 157 }; 158 159 class SingleClickWakeupMonitor : public WakeupMonitor { 160 public: SingleClickWakeupMonitor(WakeupSource & source)161 explicit SingleClickWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 162 ~SingleClickWakeupMonitor() override = default; 163 bool Init() override; 164 void Cancel() override; 165 }; 166 167 class DoubleClickWakeupMonitor : public WakeupMonitor { 168 public: DoubleClickWakeupMonitor(WakeupSource & source)169 explicit DoubleClickWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 170 ~DoubleClickWakeupMonitor() override = default; 171 bool Init() override; 172 void Cancel() override; 173 }; 174 175 class LidWakeupMonitor : public WakeupMonitor { 176 public: LidWakeupMonitor(WakeupSource & source)177 explicit LidWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 178 ~LidWakeupMonitor() override = default; 179 bool Init() override; 180 void Cancel() override; 181 }; 182 183 class SwitchWakeupMonitor : public WakeupMonitor { 184 public: SwitchWakeupMonitor(WakeupSource & source)185 explicit SwitchWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {} 186 ~SwitchWakeupMonitor() override = default; 187 bool Init() override; 188 void Cancel() override; 189 }; 190 191 } // namespace PowerMgr 192 } // namespace OHOS 193 194 #endif