1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 class IPowerEventsCallback
12 {
13 public:
14   virtual ~IPowerEventsCallback() = default;
15 
16   virtual void OnSleep() = 0;
17   virtual void OnWake() = 0;
18 
19   virtual void OnLowBattery() = 0;
20 };
21 
22 class IPowerSyscall;
23 using CreatePowerSyscallFunc = IPowerSyscall* (*)();
24 
25 class IPowerSyscall
26 {
27 public:
28   /**\brief Called by power manager to create platform power system adapter
29   *
30   * This method used to create platfrom specified power system adapter
31   */
32   static IPowerSyscall* CreateInstance();
33   static void RegisterPowerSyscall(CreatePowerSyscallFunc createFunc);
34 
35   virtual ~IPowerSyscall() = default;
36   virtual bool Powerdown()    = 0;
37   virtual bool Suspend()      = 0;
38   virtual bool Hibernate()    = 0;
39   virtual bool Reboot()       = 0;
40 
41 // Might need to be membervariables instead for speed
42   virtual bool CanPowerdown() = 0;
43   virtual bool CanSuspend()   = 0;
44   virtual bool CanHibernate() = 0;
45   virtual bool CanReboot()    = 0;
46 
47   virtual int  CountPowerFeatures() = 0;
48 
49 // Battery related functions
50   virtual int  BatteryLevel() = 0;
51 
52   /*!
53    \brief Pump power related events back to xbmc.
54 
55    PumpPowerEvents is called from Application Thread and the platform implementation may signal
56    power related events back to xbmc through the callback.
57 
58    return true if an event occurred and false if not.
59 
60    \param callback the callback to signal to
61    */
62   virtual bool PumpPowerEvents(IPowerEventsCallback *callback) = 0;
63 
64   static const int MAX_COUNT_POWER_FEATURES = 4;
65 
66 private:
67   static CreatePowerSyscallFunc m_createFunc;
68 };
69 
70 class CAbstractPowerSyscall : public IPowerSyscall
71 {
72 public:
CountPowerFeatures()73   int CountPowerFeatures() override
74   {
75       return (CanPowerdown() ? 1 : 0)
76              + (CanSuspend() ? 1 : 0)
77              + (CanHibernate() ? 1 : 0)
78              + (CanReboot() ? 1 : 0);
79   }
80 };
81 
82 class CPowerSyscallWithoutEvents : public CAbstractPowerSyscall
83 {
84 public:
CPowerSyscallWithoutEvents()85   CPowerSyscallWithoutEvents() { m_OnResume = false; m_OnSuspend = false; }
86 
Suspend()87   bool Suspend() override { m_OnSuspend = true; return false; }
Hibernate()88   bool Hibernate() override { m_OnSuspend = true; return false; }
89 
PumpPowerEvents(IPowerEventsCallback * callback)90   bool PumpPowerEvents(IPowerEventsCallback *callback) override
91   {
92     if (m_OnSuspend)
93     {
94       callback->OnSleep();
95       m_OnSuspend = false;
96       m_OnResume = true;
97       return true;
98     }
99     else if (m_OnResume)
100     {
101       callback->OnWake();
102       m_OnResume = false;
103       return true;
104     }
105     else
106       return false;
107   }
108 private:
109   bool m_OnResume;
110   bool m_OnSuspend;
111 };
112