Merge pull request #3083 from fritsch/active-ae-compile-fixes
[vuplus_xbmc] / xbmc / powermanagement / PowerManager.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "system.h"
22 #include "PowerManager.h"
23 #include "Application.h"
24 #include "cores/AudioEngine/AEFactory.h"
25 #include "input/KeyboardStat.h"
26 #include "settings/Setting.h"
27 #include "settings/Settings.h"
28 #include "windowing/WindowingFactory.h"
29 #include "utils/log.h"
30 #include "utils/Weather.h"
31 #include "interfaces/Builtins.h"
32 #include "interfaces/AnnouncementManager.h"
33 #include "guilib/LocalizeStrings.h"
34 #include "guilib/GraphicContext.h"
35 #include "guilib/GUIWindowManager.h"
36 #include "dialogs/GUIDialogBusy.h"
37 #include "dialogs/GUIDialogKaiToast.h"
38
39 #if defined(TARGET_DARWIN)
40 #include "osx/CocoaPowerSyscall.h"
41 #elif defined(TARGET_ANDROID)
42 #include "android/AndroidPowerSyscall.h"
43 #elif defined(TARGET_POSIX)
44 #include "linux/FallbackPowerSyscall.h"
45 #if defined(HAS_DBUS)
46 #include "linux/ConsoleUPowerSyscall.h"
47 #include "linux/ConsoleDeviceKitPowerSyscall.h"
48 #include "linux/LogindUPowerSyscall.h"
49 #include "linux/UPowerSyscall.h"
50 #if defined(HAS_HAL)
51 #include "linux/HALPowerSyscall.h"
52 #endif // HAS_HAL
53 #endif // HAS_DBUS
54 #elif defined(TARGET_WINDOWS)
55 #include "powermanagement/windows/Win32PowerSyscall.h"
56 extern HWND g_hWnd;
57 #endif
58
59 using namespace ANNOUNCEMENT;
60
61 CPowerManager g_powerManager;
62
63 CPowerManager::CPowerManager()
64 {
65   m_instance = NULL;
66 }
67
68 CPowerManager::~CPowerManager()
69 {
70   delete m_instance;
71 }
72
73 void CPowerManager::Initialize()
74 {
75 #if defined(TARGET_DARWIN)
76   m_instance = new CCocoaPowerSyscall();
77 #elif defined(TARGET_ANDROID)
78   m_instance = new CAndroidPowerSyscall();
79 #elif defined(TARGET_POSIX)
80 #if defined(HAS_DBUS)
81   if (CConsoleUPowerSyscall::HasConsoleKitAndUPower())
82     m_instance = new CConsoleUPowerSyscall();
83   else if (CConsoleDeviceKitPowerSyscall::HasDeviceConsoleKit())
84     m_instance = new CConsoleDeviceKitPowerSyscall();
85   else if (CLogindUPowerSyscall::HasLogind())
86     m_instance = new CLogindUPowerSyscall();
87   else if (CUPowerSyscall::HasUPower())
88     m_instance = new CUPowerSyscall();
89 #if defined(HAS_HAL)
90   else if(1)
91     m_instance = new CHALPowerSyscall();
92 #endif // HAS_HAL
93   else
94 #endif // HAS_DBUS
95     m_instance = new CFallbackPowerSyscall();
96 #elif defined(TARGET_WINDOWS)
97   m_instance = new CWin32PowerSyscall();
98 #endif
99
100   if (m_instance == NULL)
101     m_instance = new CNullPowerSyscall();
102 }
103
104 void CPowerManager::SetDefaults()
105 {
106   int defaultShutdown = CSettings::Get().GetInt("powermanagement.shutdownstate");
107
108   switch (defaultShutdown)
109   {
110     case POWERSTATE_QUIT:
111     case POWERSTATE_MINIMIZE:
112       // assume we can shutdown if --standalone is passed
113       if (g_application.IsStandAlone())
114         defaultShutdown = POWERSTATE_SHUTDOWN;
115     break;
116     case POWERSTATE_HIBERNATE:
117       if (!g_powerManager.CanHibernate())
118       {
119         if (g_powerManager.CanSuspend())
120           defaultShutdown = POWERSTATE_SUSPEND;
121         else
122           defaultShutdown = g_powerManager.CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
123       }
124     break;
125     case POWERSTATE_SUSPEND:
126       if (!g_powerManager.CanSuspend())
127       {
128         if (g_powerManager.CanHibernate())
129           defaultShutdown = POWERSTATE_HIBERNATE;
130         else
131           defaultShutdown = g_powerManager.CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
132       }
133     break;
134     case POWERSTATE_SHUTDOWN:
135       if (!g_powerManager.CanPowerdown())
136       {
137         if (g_powerManager.CanSuspend())
138           defaultShutdown = POWERSTATE_SUSPEND;
139         else
140           defaultShutdown = g_powerManager.CanHibernate() ? POWERSTATE_HIBERNATE : POWERSTATE_QUIT;
141       }
142     break;
143   }
144
145   ((CSettingInt*)CSettings::Get().GetSetting("powermanagement.shutdownstate"))->SetDefault(defaultShutdown);
146 }
147
148 bool CPowerManager::Powerdown()
149 {
150   if (CanPowerdown() && m_instance->Powerdown())
151   {
152     CGUIDialogBusy* dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
153     if (dialog)
154       dialog->Show();
155
156     return true;
157   }
158
159   return false;
160 }
161
162 bool CPowerManager::Suspend()
163 {
164   if (CanSuspend() && m_instance->Suspend())
165   {
166     CGUIDialogBusy* dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
167     if (dialog)
168       dialog->Show();
169
170     return true;
171   }
172
173   return false;
174 }
175
176 bool CPowerManager::Hibernate()
177 {
178   if (CanHibernate() && m_instance->Hibernate())
179   {
180     CGUIDialogBusy* dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
181     if (dialog)
182       dialog->Show();
183
184     return true;
185   }
186
187   return false;
188 }
189 bool CPowerManager::Reboot()
190 {
191   bool success = CanReboot() ? m_instance->Reboot() : false;
192
193   if (success)
194   {
195     CAnnouncementManager::Announce(System, "xbmc", "OnRestart");
196
197     CGUIDialogBusy* dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
198     if (dialog)
199       dialog->Show();
200   }
201
202   return success;
203 }
204
205 bool CPowerManager::CanPowerdown()
206 {
207   return m_instance->CanPowerdown();
208 }
209 bool CPowerManager::CanSuspend()
210 {
211   return m_instance->CanSuspend();
212 }
213 bool CPowerManager::CanHibernate()
214 {
215   return m_instance->CanHibernate();
216 }
217 bool CPowerManager::CanReboot()
218 {
219   return m_instance->CanReboot();
220 }
221 int CPowerManager::BatteryLevel()
222 {
223   return m_instance->BatteryLevel();
224 }
225 void CPowerManager::ProcessEvents()
226 {
227   static int nesting = 0;
228
229   if (++nesting == 1)
230     m_instance->PumpPowerEvents(this);
231
232   nesting--;
233 }
234
235 void CPowerManager::OnSleep()
236 {
237   CAnnouncementManager::Announce(System, "xbmc", "OnSleep");
238   CLog::Log(LOGNOTICE, "%s: Running sleep jobs", __FUNCTION__);
239
240   // stop lirc
241 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
242   CLog::Log(LOGNOTICE, "%s: Stopping lirc", __FUNCTION__);
243   CBuiltins::Execute("LIRC.Stop");
244 #endif
245
246   g_application.SaveFileState(true);
247   g_application.StopPlaying();
248   g_application.StopShutdownTimer();
249   g_application.StopScreenSaverTimer();
250   g_application.CloseNetworkShares();
251   CAEFactory::Suspend();
252 }
253
254 void CPowerManager::OnWake()
255 {
256   CLog::Log(LOGNOTICE, "%s: Running resume jobs", __FUNCTION__);
257
258   // reset out timers
259   g_application.ResetShutdownTimers();
260
261   CGUIDialogBusy* dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
262   if (dialog)
263     dialog->Close();
264
265 #if defined(HAS_SDL) || defined(TARGET_WINDOWS)
266   if (g_Windowing.IsFullScreen())
267   {
268 #if defined(TARGET_WINDOWS)
269     ShowWindow(g_hWnd,SW_RESTORE);
270     SetForegroundWindow(g_hWnd);
271 #elif !defined(TARGET_DARWIN_OSX)
272     // Hack to reclaim focus, thus rehiding system mouse pointer.
273     // Surely there's a better way?
274     g_graphicsContext.ToggleFullScreenRoot();
275     g_graphicsContext.ToggleFullScreenRoot();
276 #endif
277   }
278   g_application.ResetScreenSaver();
279 #endif
280
281   // restart lirc
282 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
283   CLog::Log(LOGNOTICE, "%s: Restarting lirc", __FUNCTION__);
284   CBuiltins::Execute("LIRC.Start");
285 #endif
286
287   CAEFactory::Resume();
288   g_application.UpdateLibraries();
289   g_weatherManager.Refresh();
290
291   CAnnouncementManager::Announce(System, "xbmc", "OnWake");
292 }
293
294 void CPowerManager::OnLowBattery()
295 {
296   CLog::Log(LOGNOTICE, "%s: Running low battery jobs", __FUNCTION__);
297
298   CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(13050), "");
299
300   CAnnouncementManager::Announce(System, "xbmc", "OnLowBattery");
301 }
302
303 void CPowerManager::SettingOptionsShutdownStatesFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int &current)
304 {
305   if (g_powerManager.CanPowerdown())
306     list.push_back(make_pair(g_localizeStrings.Get(13005), POWERSTATE_SHUTDOWN));
307   if (g_powerManager.CanHibernate())
308     list.push_back(make_pair(g_localizeStrings.Get(13010), POWERSTATE_HIBERNATE));
309   if (g_powerManager.CanSuspend())
310     list.push_back(make_pair(g_localizeStrings.Get(13011), POWERSTATE_SUSPEND));
311   if (!g_application.IsStandAlone())
312   {
313     list.push_back(make_pair(g_localizeStrings.Get(13009), POWERSTATE_QUIT));
314 #if !defined(TARGET_DARWIN_IOS)
315     list.push_back(make_pair(g_localizeStrings.Get(13014), POWERSTATE_MINIMIZE));
316 #endif
317   }
318 }