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#ifdef HAS_MULTIMODALINPUT_INPUT_PART
25#include "i_input_event_consumer.h"
26#endif
27#include "power_state_machine.h"
28#include "system_ability.h"
29#ifdef HAS_SENSORS_SENSOR_PART
30#include "sensor_agent.h"
31#endif
32#include "wakeup_sources.h"
33#include "wakeup_source_parser.h"
34
35namespace OHOS {
36namespace PowerMgr {
37
38using WakeupListener = std::function<void(WakeupDeviceType)>;
39using namespace OHOS::MMI;
40class WakeupMonitor;
41class WakeupController : public std::enable_shared_from_this<WakeupController> {
42public:
43    explicit WakeupController(std::shared_ptr<PowerStateMachine>& stateMachine);
44    ~WakeupController();
45    void Init();
46    void Cancel();
47    void RegisterSettingsObserver();
48    void UnregisterSettingsObserver();
49    void ExecWakeupMonitorByReason(WakeupDeviceType reason);
50    void Wakeup();
51    void NotifyDisplayActionDone(uint32_t event);
52    void SetOriginSettingValue(WakeupSource& source);
53#ifdef POWER_MANAGER_WAKEUP_ACTION
54    bool IsLowCapacityWakeup(WakeupDeviceType reason);
55    void ProcessLowCapacityWakeup();
56#endif
57    static int32_t SetWakeupDoubleClickSensor(bool enable);
58    static void ChangeWakeupSourceConfig(bool updateEnable);
59    static void ChangePickupWakeupSourceConfig(bool updataEnable);
60    static void PickupConnectMotionConfig(bool databaseSwitchValue);
61    static void ChangeLidWakeupSourceConfig(bool updataEnable);
62    std::shared_ptr<PowerStateMachine> GetStateMachine()
63    {
64        return stateMachine_;
65    }
66    WakeupDeviceType GetLastReason() const
67    {
68        return wakeupReason_;
69    }
70    bool CheckEventReciveTime(WakeupDeviceType wakeupType);
71#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
72    void PowerOnInternalScreen(WakeupDeviceType type);
73    void PowerOnAllScreens(WakeupDeviceType type);
74#endif
75
76private:
77    class SleepGuard final {
78    public:
79        explicit SleepGuard(const sptr<PowerMgrService>& pms);
80        ~SleepGuard();
81        SleepGuard(const SleepGuard&) = delete;
82        SleepGuard(SleepGuard&&) = delete;
83        SleepGuard& operator=(const SleepGuard&) = delete;
84        SleepGuard& operator=(SleepGuard&&) = delete;
85
86    private:
87        sptr<IRemoteObject> token_;
88        sptr<PowerMgrService> pms_;
89    };
90
91#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
92    bool IsPowerOnInernalScreenOnlyScene(WakeupDeviceType reason) const;
93    void ProcessPowerOnInternalScreenOnly(const sptr<PowerMgrService>& pms, WakeupDeviceType reason);
94#endif
95    bool NeedToSkipCurrentWakeup(const sptr<PowerMgrService>& pms, WakeupDeviceType reason) const;
96    void HandleWakeup(const sptr<PowerMgrService>& pms, WakeupDeviceType reason);
97    void ControlListener(WakeupDeviceType reason);
98
99    std::vector<WakeupSource> sourceList_;
100    std::map<WakeupDeviceType, std::shared_ptr<WakeupMonitor>> monitorMap_;
101    std::map<WakeupDeviceType, int64_t> eventHandleMap_;
102    std::shared_ptr<PowerStateMachine> stateMachine_;
103    WakeupDeviceType wakeupReason_ {0};
104    std::mutex mutex_;
105    std::mutex monitorMutex_;
106    std::mutex eventHandleMutex_;
107    static std::mutex sourceUpdateMutex_;
108    int32_t monitorId_ {-1};
109};
110
111#ifdef HAS_MULTIMODALINPUT_INPUT_PART
112class InputCallback : public IInputEventConsumer {
113public:
114    virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
115    virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
116    virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
117    bool NonWindowEvent(const std::shared_ptr<PointerEvent>& pointerEvent) const;
118private:
119    bool isRemoteEvent(std::shared_ptr<InputEvent> event) const;
120    bool isKeyboardKeycode(int32_t keyCode) const;
121};
122#endif
123
124class WakeupMonitor {
125public:
126    static std::shared_ptr<WakeupMonitor> CreateMonitor(WakeupSource& source);
127    static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
128
129    virtual ~WakeupMonitor() = default;
130    virtual bool Init() = 0;
131    virtual void Cancel() = 0;
132    virtual void HandleEvent(uint32_t eventId)
133    {
134        // do nothing in base class
135    }
136
137    WakeupDeviceType GetReason() const
138    {
139        return reason_;
140    }
141
142    void RegisterListener(WakeupListener listener)
143    {
144        listener_ = listener;
145    }
146
147    void Notify()
148    {
149        listener_(reason_);
150    }
151
152protected:
153    explicit WakeupMonitor(WakeupSource& source)
154    {
155        reason_ = source.GetReason();
156    }
157
158    WakeupDeviceType reason_;
159    WakeupListener listener_;
160};
161
162class PowerkeyWakeupMonitor : public WakeupMonitor {
163public:
164    explicit PowerkeyWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
165    ~PowerkeyWakeupMonitor() override = default;
166    bool Init() override;
167    void Cancel() override;
168
169private:
170    int32_t powerkeyShortPressId_ {-1};
171};
172
173class KeyboardWakeupMonitor : public WakeupMonitor {
174public:
175    explicit KeyboardWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
176    ~KeyboardWakeupMonitor() override = default;
177    bool Init() override;
178    void Cancel() override;
179};
180
181class MousekeyWakeupMonitor : public WakeupMonitor {
182public:
183    explicit MousekeyWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
184    ~MousekeyWakeupMonitor() override = default;
185    bool Init() override;
186    void Cancel() override;
187};
188
189class TouchpadWakeupMonitor : public WakeupMonitor {
190public:
191    explicit TouchpadWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
192    ~TouchpadWakeupMonitor() override = default;
193    bool Init() override;
194    void Cancel() override;
195};
196
197class PenWakeupMonitor : public WakeupMonitor {
198public:
199    explicit PenWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
200    ~PenWakeupMonitor() override = default;
201    bool Init() override;
202    void Cancel() override;
203};
204
205class SingleClickWakeupMonitor : public WakeupMonitor {
206public:
207    explicit SingleClickWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
208    ~SingleClickWakeupMonitor() override = default;
209    bool Init() override;
210    void Cancel() override;
211};
212
213class DoubleClickWakeupMonitor : public WakeupMonitor {
214public:
215    explicit DoubleClickWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
216    ~DoubleClickWakeupMonitor() override = default;
217    bool Init() override;
218    void Cancel() override;
219};
220
221class LidWakeupMonitor : public WakeupMonitor {
222public:
223    explicit LidWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
224    ~LidWakeupMonitor() override = default;
225    bool Init() override;
226    void Cancel() override;
227};
228
229class SwitchWakeupMonitor : public WakeupMonitor {
230public:
231    explicit SwitchWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
232    ~SwitchWakeupMonitor() override = default;
233    bool Init() override;
234    void Cancel() override;
235};
236
237class PickupWakeupMonitor : public WakeupMonitor {
238public:
239    explicit PickupWakeupMonitor(WakeupSource& source) : WakeupMonitor(source) {}
240    ~PickupWakeupMonitor() override = default;
241    bool Init() override;
242    void Cancel() override;
243};
244
245} // namespace PowerMgr
246} // namespace OHOS
247
248#endif