Debian Codegraph @

xbmc/powermanagement/PowerManager.cpp

raw
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 #include "PowerManager.h"
10
11 #include "FileItem.h"
12 #include "PowerTypes.h"
13 #include "ServiceBroker.h"
14 #include "application/AppParams.h"
15 #include "application/Application.h"
16 #include "application/ApplicationComponents.h"
17 #include "application/ApplicationPlayer.h"
18 #include "application/ApplicationPowerHandling.h"
19 #include "application/ApplicationStackHelper.h"
20 #include "cores/AudioEngine/Interfaces/AE.h"
21 #include "dialogs/GUIDialogBusyNoCancel.h"
22 #include "dialogs/GUIDialogKaiToast.h"
23 #include "guilib/GUIComponent.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "guilib/LocalizeStrings.h"
26 #include "interfaces/AnnouncementManager.h"
27 #include "network/Network.h"
28 #include "pvr/PVRManager.h"
29 #include "settings/Settings.h"
30 #include "settings/SettingsComponent.h"
31 #include "settings/lib/Setting.h"
32 #include "settings/lib/SettingDefinitions.h"
33 #include "settings/lib/SettingsManager.h"
34 #include "utils/log.h"
35 #include "weather/WeatherManager.h"
36
37 #include <list>
38 #include <memory>
39
40 #if defined(TARGET_WINDOWS_DESKTOP)
41 extern HWND g_hWnd;
42 #endif
43
44 CPowerManager::CPowerManager() : m_settings(CServiceBroker::GetSettingsComponent()->GetSettings())
45 {
46 m_settings->GetSettingsManager()->RegisterSettingOptionsFiller("shutdownstates", SettingOptionsShutdownStatesFiller);
47 }
48
49 CPowerManager::~CPowerManager() = default;
50
51 void CPowerManager::Initialize()
52 {
53 m_instance.reset(IPowerSyscall::CreateInstance());
54 }
55
56 void CPowerManager::SetDefaults()
57 {
58 auto setting = m_settings->GetSetting(CSettings::SETTING_POWERMANAGEMENT_SHUTDOWNSTATE);
59 if (!setting)
60 {
61 CLog::Log(LOGERROR, "Failed to load setting for: {}",
62 CSettings::SETTING_POWERMANAGEMENT_SHUTDOWNSTATE);
63 return;
64 }
65
66 int defaultShutdown = m_settings->GetInt(CSettings::SETTING_POWERMANAGEMENT_SHUTDOWNSTATE);
67
68 switch (defaultShutdown)
69 {
70 case POWERSTATE_QUIT:
71 case POWERSTATE_MINIMIZE:
72 // assume we can shutdown if --standalone is passed
73 if (CServiceBroker::GetAppParams()->IsStandAlone())
74 defaultShutdown = POWERSTATE_SHUTDOWN;
75 break;
76 case POWERSTATE_HIBERNATE:
77 if (!CServiceBroker::GetPowerManager().CanHibernate())
78 {
79 if (CServiceBroker::GetPowerManager().CanSuspend())
80 defaultShutdown = POWERSTATE_SUSPEND;
81 else
82 defaultShutdown = CServiceBroker::GetPowerManager().CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
83 }
84 break;
85 case POWERSTATE_SUSPEND:
86 if (!CServiceBroker::GetPowerManager().CanSuspend())
87 {
88 if (CServiceBroker::GetPowerManager().CanHibernate())
89 defaultShutdown = POWERSTATE_HIBERNATE;
90 else
91 defaultShutdown = CServiceBroker::GetPowerManager().CanPowerdown() ? POWERSTATE_SHUTDOWN : POWERSTATE_QUIT;
92 }
93 break;
94 case POWERSTATE_SHUTDOWN:
95 if (!CServiceBroker::GetPowerManager().CanPowerdown())
96 {
97 if (CServiceBroker::GetPowerManager().CanSuspend())
98 defaultShutdown = POWERSTATE_SUSPEND;
99 else
100 defaultShutdown = CServiceBroker::GetPowerManager().CanHibernate() ? POWERSTATE_HIBERNATE : POWERSTATE_QUIT;
101 }
102 break;
103 }
104
105 std::static_pointer_cast<CSettingInt>(setting)->SetDefault(defaultShutdown);
106 }
107
108 bool CPowerManager::Powerdown()
109 {
110 if (CanPowerdown() && m_instance->Powerdown())
111 {
112 CGUIDialogBusyNoCancel* dialog =
113 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogBusyNoCancel>(
114 WINDOW_DIALOG_BUSY_NOCANCEL);
115 if (dialog)
116 dialog->Open();
117
118 return true;
119 }
120
121 return false;
122 }
123
124 bool CPowerManager::Suspend()
125 {
126 return (CanSuspend() && m_instance->Suspend());
127 }
128
129 bool CPowerManager::Hibernate()
130 {
131 return (CanHibernate() && m_instance->Hibernate());
132 }
133
134 bool CPowerManager::Reboot()
135 {
136 bool success = CanReboot() ? m_instance->Reboot() : false;
137
138 if (success)
139 {
140 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::System, "OnRestart");
141
142 CGUIDialogBusyNoCancel* dialog =
143 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogBusyNoCancel>(
144 WINDOW_DIALOG_BUSY_NOCANCEL);
145 if (dialog)
146 dialog->Open();
147 }
148
149 return success;
150 }
151
152 bool CPowerManager::CanPowerdown()
153 {
154 return m_instance ? m_instance->CanPowerdown() : false;
155 }
156 bool CPowerManager::CanSuspend()
157 {
158 return m_instance ? m_instance->CanSuspend() : false;
159 }
160 bool CPowerManager::CanHibernate()
161 {
162 return m_instance ? m_instance->CanHibernate() : false;
163 }
164 bool CPowerManager::CanReboot()
165 {
166 return m_instance ? m_instance->CanReboot() : false;
167 }
168 int CPowerManager::BatteryLevel()
169 {
170 return m_instance ? m_instance->BatteryLevel() : 0;
171 }
172 void CPowerManager::ProcessEvents()
173 {
174 if (!m_instance)
175 return;
176
177 static int nesting = 0;
178
179 if (++nesting == 1)
180 m_instance->PumpPowerEvents(this);
181
182 nesting--;
183 }
184
185 void CPowerManager::OnSleep()
186 {
187 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::System, "OnSleep");
188
189 CGUIDialogBusyNoCancel* dialog =
190 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogBusyNoCancel>(
191 WINDOW_DIALOG_BUSY_NOCANCEL);
192 if (dialog)
193 dialog->Open();
194
195 CLog::Log(LOGINFO, "{}: Running sleep jobs", __FUNCTION__);
196
197 StorePlayerState();
198
199 g_application.StopPlaying();
200 CServiceBroker::GetPVRManager().OnSleep();
201 auto& components = CServiceBroker::GetAppComponents();
202 const auto appPower = components.GetComponent<CApplicationPowerHandling>();
203 appPower->StopShutdownTimer();
204 appPower->StopScreenSaverTimer();
205 g_application.CloseNetworkShares();
206 CServiceBroker::GetActiveAE()->Suspend();
207 }
208
209 void CPowerManager::OnWake()
210 {
211 CLog::Log(LOGINFO, "{}: Running resume jobs", __FUNCTION__);
212
213 CServiceBroker::GetNetwork().WaitForNet();
214
215 // reset out timers
216 auto& components = CServiceBroker::GetAppComponents();
217 const auto appPower = components.GetComponent<CApplicationPowerHandling>();
218 appPower->ResetShutdownTimers();
219
220 CGUIDialogBusyNoCancel* dialog =
221 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogBusyNoCancel>(
222 WINDOW_DIALOG_BUSY_NOCANCEL);
223 if (dialog)
224 dialog->Close(true); // force close. no closing animation, sound etc at this early stage
225
226 #if defined(TARGET_DARWIN_OSX) || defined(TARGET_WINDOWS)
227 if (CServiceBroker::GetWinSystem()->IsFullScreen())
228 {
229 #if defined(TARGET_WINDOWS_DESKTOP)
230 ShowWindow(g_hWnd, SW_RESTORE);
231 SetForegroundWindow(g_hWnd);
232 #endif
233 }
234 appPower->ResetScreenSaver();
235 #endif
236
237 CServiceBroker::GetActiveAE()->Resume();
238 g_application.UpdateLibraries();
239 CServiceBroker::GetWeatherManager().Refresh();
240 CServiceBroker::GetPVRManager().OnWake();
241 RestorePlayerState();
242
243 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::System, "OnWake");
244 }
245
246 void CPowerManager::OnLowBattery()
247 {
248 CLog::Log(LOGINFO, "{}: Running low battery jobs", __FUNCTION__);
249
250 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(13050), "");
251
252 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::System, "OnLowBattery");
253 }
254
255 void CPowerManager::StorePlayerState()
256 {
257 auto& components = CServiceBroker::GetAppComponents();
258 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
259 if (appPlayer->IsPlaying())
260 {
261 m_lastUsedPlayer = appPlayer->GetCurrentPlayer();
262 m_lastPlayedFileItem = std::make_unique<CFileItem>(g_application.CurrentFileItem());
263 // set the actual offset instead of store and load it from database
264 m_lastPlayedFileItem->SetStartOffset(appPlayer->GetTime());
265 // in case of regular stack, correct the start offset by adding current part start time
266 const auto stackHelper = components.GetComponent<CApplicationStackHelper>();
267 if (stackHelper->IsPlayingRegularStack())
268 m_lastPlayedFileItem->SetStartOffset(m_lastPlayedFileItem->GetStartOffset() +
269 stackHelper->GetCurrentStackPartStartTimeMs());
270 // in case of iso stack, keep track of part number
271 m_lastPlayedFileItem->m_lStartPartNumber =
272 stackHelper->IsPlayingISOStack() ? stackHelper->GetCurrentPartNumber() + 1 : 1;
273 // for iso and iso stacks, keep track of playerstate
274 m_lastPlayedFileItem->SetProperty("savedplayerstate", appPlayer->GetPlayerState());
275 CLog::Log(LOGDEBUG,
276 "CPowerManager::StorePlayerState - store last played item (startOffset: {} ms)",
277 m_lastPlayedFileItem->GetStartOffset());
278 }
279 else
280 {
281 m_lastUsedPlayer.clear();
282 m_lastPlayedFileItem.reset();
283 }
284 }
285
286 void CPowerManager::RestorePlayerState()
287 {
288 if (!m_lastPlayedFileItem)
289 return;
290
291 CLog::Log(LOGDEBUG,
292 "CPowerManager::RestorePlayerState - resume last played item (startOffset: {} ms)",
293 m_lastPlayedFileItem->GetStartOffset());
294 g_application.PlayFile(*m_lastPlayedFileItem, m_lastUsedPlayer);
295 }
296
297 void CPowerManager::SettingOptionsShutdownStatesFiller(const SettingConstPtr& setting,
298 std::vector<IntegerSettingOption>& list,
299 int& current,
300 void* data)
301 {
302 if (CServiceBroker::GetPowerManager().CanPowerdown())
303 list.emplace_back(g_localizeStrings.Get(13005), POWERSTATE_SHUTDOWN);
304 if (CServiceBroker::GetPowerManager().CanHibernate())
305 list.emplace_back(g_localizeStrings.Get(13010), POWERSTATE_HIBERNATE);
306 if (CServiceBroker::GetPowerManager().CanSuspend())
307 list.emplace_back(g_localizeStrings.Get(13011), POWERSTATE_SUSPEND);
308 if (!CServiceBroker::GetAppParams()->IsStandAlone())
309 {
310 list.emplace_back(g_localizeStrings.Get(13009), POWERSTATE_QUIT);
311 #if !defined(TARGET_DARWIN_EMBEDDED)
312 list.emplace_back(g_localizeStrings.Get(13014), POWERSTATE_MINIMIZE);
313 #endif
314 }
315 }