1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright (C) 2005-2009 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#include "PowerManager.h"
#include "Application.h"
#include "KeyboardStat.h"
#include "GUISettings.h"
#include "WindowingFactory.h"
#include "utils/log.h"
#include "AnnouncementManager.h"
#include "LocalizeStrings.h"
#if defined(HAVE_LIBCRYSTALHD)
#include "cores/dvdplayer/DVDCodecs/Video/CrystalHD/CrystalHD.h"
#endif
#ifdef HAS_LCD
#include "utils/LCDFactory.h"
#endif
#ifdef __APPLE__
#include "osx/CocoaPowerSyscall.h"
#elif defined(_LINUX) && defined(HAS_DBUS)
#include "linux/ConsoleUPowerSyscall.h"
#include "linux/ConsoleDeviceKitPowerSyscall.h"
#ifdef HAS_HAL
#include "linux/HALPowerSyscall.h"
#endif
#elif defined(_WIN32)
#include "win32/Win32PowerSyscall.h"
extern HWND g_hWnd;
#endif
#ifdef HAS_LIRC
#include "common/LIRC.h"
#endif
#ifdef HAS_IRSERVERSUITE
#include "common/IRServerSuite/IRServerSuite.h"
#endif
using namespace ANNOUNCEMENT;
CPowerManager g_powerManager;
CPowerManager::CPowerManager()
{
m_instance = NULL;
}
CPowerManager::~CPowerManager()
{
delete m_instance;
}
void CPowerManager::Initialize()
{
#ifdef __APPLE__
m_instance = new CCocoaPowerSyscall();
#elif defined(_LINUX) && defined(HAS_DBUS)
if (CConsoleUPowerSyscall::HasDeviceConsoleKit())
m_instance = new CConsoleUPowerSyscall();
else if (CConsoleDeviceKitPowerSyscall::HasDeviceConsoleKit())
m_instance = new CConsoleDeviceKitPowerSyscall();
#ifdef HAS_HAL
else
m_instance = new CHALPowerSyscall();
#endif
#elif defined(_WIN32)
m_instance = new CWin32PowerSyscall();
#endif
if (m_instance == NULL)
m_instance = new CNullPowerSyscall();
}
void CPowerManager::SetDefaults()
{
int defaultShutdown = g_guiSettings.GetInt("powermanagement.shutdownstate");
switch (defaultShutdown)
{
case POWERSTATE_QUIT:
case POWERSTATE_MINIMIZE:
// assume we can shutdown if --standalone is passed
if (g_application.IsStandAlone())
defaultShutdown = POWERSTATE_SHUTDOWN;
break;
case POWERSTATE_HIBERNATE:
if (!g_powerManager.CanHibernate())
{
if (g_powerManager.CanSuspend())
defaultShutdown = POWERSTATE_SUSPEND;
else
defaultShutdown = g_powerManager.CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
}
break;
case POWERSTATE_SUSPEND:
if (!g_powerManager.CanSuspend())
{
if (g_powerManager.CanHibernate())
defaultShutdown = POWERSTATE_HIBERNATE;
else
defaultShutdown = g_powerManager.CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
}
break;
case POWERSTATE_SHUTDOWN:
if (!g_powerManager.CanPowerdown())
{
if (g_powerManager.CanSuspend())
defaultShutdown = POWERSTATE_SUSPEND;
else
defaultShutdown = g_powerManager.CanHibernate() ? POWERSTATE_HIBERNATE : POWERSTATE_QUIT;
}
break;
}
g_guiSettings.SetInt("powermanagement.shutdownstate", defaultShutdown);
}
bool CPowerManager::Powerdown()
{
bool success = CanPowerdown() ? m_instance->Powerdown() : false;
if (success)
CAnnouncementManager::Announce(System, "xbmc", "Shutdown");
return success;
}
bool CPowerManager::Suspend()
{
bool success = false;
if (CanSuspend())
{
#if defined(HAVE_LIBCRYSTALHD)
CCrystalHD::GetInstance()->Sleep();
#endif
#ifdef HAS_LCD
g_lcd->SetBackLight(0);
#endif
g_Keyboard.ResetState();
success = m_instance->Suspend();
}
if (success)
CAnnouncementManager::Announce(System, "xbmc", "Suspend");
return success;
}
bool CPowerManager::Hibernate()
{
bool success = false;
if (CanHibernate())
{
#if defined(HAVE_LIBCRYSTALHD)
CCrystalHD::GetInstance()->Sleep();
#endif
g_Keyboard.ResetState();
success = m_instance->Hibernate();
}
if (success)
CAnnouncementManager::Announce(System, "xbmc", "Hibernate");
return success;
}
bool CPowerManager::Reboot()
{
bool success = CanReboot() ? m_instance->Reboot() : false;
if (success)
CAnnouncementManager::Announce(System, "xbmc", "Reboot");
return success;
}
bool CPowerManager::CanPowerdown()
{
return m_instance->CanPowerdown();
}
bool CPowerManager::CanSuspend()
{
return m_instance->CanSuspend();
}
bool CPowerManager::CanHibernate()
{
return m_instance->CanHibernate();
}
bool CPowerManager::CanReboot()
{
return m_instance->CanReboot();
}
void CPowerManager::ProcessEvents()
{
m_instance->PumpPowerEvents(this);
}
void CPowerManager::OnSleep()
{
CLog::Log(LOGNOTICE, "%s: Running sleep jobs", __FUNCTION__);
CAnnouncementManager::Announce(System, "xbmc", "Sleep");
g_application.StopPlaying();
}
void CPowerManager::OnWake()
{
CLog::Log(LOGNOTICE, "%s: Running resume jobs", __FUNCTION__);
CAnnouncementManager::Announce(System, "xbmc", "Wake");
#ifdef HAS_SDL
if (g_Windowing.IsFullScreen())
{
#ifdef _WIN32
ShowWindow(g_hWnd,SW_RESTORE);
SetForegroundWindow(g_hWnd);
#else
// Hack to reclaim focus, thus rehiding system mouse pointer.
// Surely there's a better way?
g_graphicsContext.ToggleFullScreenRoot();
g_graphicsContext.ToggleFullScreenRoot();
#endif
}
g_application.ResetScreenSaver();
#endif
#if defined(HAVE_LIBCRYSTALHD)
CCrystalHD::GetInstance()->Wake();
#endif
// restart lirc
#if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
CLog::Log(LOGNOTICE, "%s: Restarting lirc", __FUNCTION__);
g_RemoteControl.Disconnect();
g_RemoteControl.Initialize();
#endif
// restart and undim lcd
#ifdef HAS_LCD
CLog::Log(LOGNOTICE, "%s: Restarting lcd", __FUNCTION__);
g_lcd->SetBackLight(1);
g_lcd->Stop();
g_lcd->Initialize();
#endif
g_application.UpdateLibraries();
CAnnouncementManager::Announce(System, "xbmc", "Resume");
}
void CPowerManager::OnLowBattery()
{
CLog::Log(LOGNOTICE, "%s: Running low battery jobs", __FUNCTION__);
g_application.m_guiDialogKaiToast.QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(13050), "");
CAnnouncementManager::Announce(System, "xbmc", "LowBattery");
}
|