[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / pvr / guilib / PVRGUIActionsPowerManagement.cpp
blob5a5ef23662337b1963be47f52ba7cbb226f9e3c1
1 /*
2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "PVRGUIActionsPowerManagement.h"
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "guilib/LocalizeStrings.h"
14 #include "messaging/helpers/DialogHelper.h"
15 #include "network/Network.h"
16 #include "pvr/PVRManager.h"
17 #include "pvr/addons/PVRClient.h"
18 #include "pvr/timers/PVRTimerInfoTag.h"
19 #include "pvr/timers/PVRTimers.h"
20 #include "settings/Settings.h"
21 #include "utils/StringUtils.h"
22 #include "utils/Variant.h"
24 #include <memory>
25 #include <string>
26 #include <vector>
28 using namespace PVR;
29 using namespace KODI::MESSAGING;
31 CPVRGUIActionsPowerManagement::CPVRGUIActionsPowerManagement()
32 : m_settings({CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME,
33 CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME})
37 bool CPVRGUIActionsPowerManagement::CanSystemPowerdown(bool bAskUser /*= true*/) const
39 bool bReturn(true);
40 if (CServiceBroker::GetPVRManager().IsStarted())
42 std::shared_ptr<CPVRTimerInfoTag> cause;
43 if (!AllLocalBackendsIdle(cause))
45 if (bAskUser)
47 std::string text;
49 if (cause)
51 if (cause->IsRecording())
53 text = StringUtils::Format(
54 g_localizeStrings.Get(19691), // "PVR is currently recording...."
55 cause->Title(), cause->ChannelName());
57 else
59 // Next event is due to a local recording or reminder.
60 const CDateTime now(CDateTime::GetUTCDateTime());
61 const CDateTime start(cause->StartAsUTC());
62 const CDateTimeSpan prestart(0, 0, cause->MarginStart(), 0);
64 CDateTimeSpan diff(start - now);
65 diff -= prestart;
66 int mins = diff.GetSecondsTotal() / 60;
68 std::string dueStr;
69 if (mins > 1)
71 // "%d minutes"
72 dueStr = StringUtils::Format(g_localizeStrings.Get(19694), mins);
74 else
76 // "about a minute"
77 dueStr = g_localizeStrings.Get(19695);
80 text = StringUtils::Format(
81 cause->IsReminder()
82 ? g_localizeStrings.Get(19690) // "PVR has scheduled a reminder...."
83 : g_localizeStrings.Get(19692), // "PVR will start recording...."
84 cause->Title(), cause->ChannelName(), dueStr);
87 else
89 // Next event is due to automatic daily wakeup of PVR.
90 const CDateTime now(CDateTime::GetUTCDateTime());
92 CDateTime dailywakeuptime;
93 dailywakeuptime.SetFromDBTime(
94 m_settings.GetStringValue(CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME));
95 dailywakeuptime = dailywakeuptime.GetAsUTCDateTime();
97 const CDateTimeSpan diff(dailywakeuptime - now);
98 int mins = diff.GetSecondsTotal() / 60;
100 std::string dueStr;
101 if (mins > 1)
103 // "%d minutes"
104 dueStr = StringUtils::Format(g_localizeStrings.Get(19694), mins);
106 else
108 // "about a minute"
109 dueStr = g_localizeStrings.Get(19695);
112 text = StringUtils::Format(g_localizeStrings.Get(19693), // "Daily wakeup is due in...."
113 dueStr);
116 // Inform user about PVR being busy. Ask if user wants to powerdown anyway.
117 bReturn = HELPERS::ShowYesNoDialogText(CVariant{19685}, // "Confirm shutdown"
118 CVariant{text}, CVariant{222}, // "Shutdown anyway",
119 CVariant{19696}, // "Cancel"
120 10000) // timeout value before closing
121 == HELPERS::DialogResponse::CHOICE_YES;
123 else
124 bReturn = false; // do not powerdown (busy, but no user interaction requested).
127 return bReturn;
130 bool CPVRGUIActionsPowerManagement::AllLocalBackendsIdle(
131 std::shared_ptr<CPVRTimerInfoTag>& causingEvent) const
133 // active recording on local backend?
134 const std::vector<std::shared_ptr<CPVRTimerInfoTag>> activeRecordings =
135 CServiceBroker::GetPVRManager().Timers()->GetActiveRecordings();
136 for (const auto& timer : activeRecordings)
138 if (EventOccursOnLocalBackend(timer))
140 causingEvent = timer;
141 return false;
145 // soon recording on local backend?
146 if (IsNextEventWithinBackendIdleTime())
148 const std::shared_ptr<CPVRTimerInfoTag> timer =
149 CServiceBroker::GetPVRManager().Timers()->GetNextActiveTimer(false);
150 if (!timer)
152 // Next event is due to automatic daily wakeup of PVR!
153 causingEvent.reset();
154 return false;
157 if (EventOccursOnLocalBackend(timer))
159 causingEvent = timer;
160 return false;
163 return true;
166 bool CPVRGUIActionsPowerManagement::EventOccursOnLocalBackend(
167 const std::shared_ptr<CPVRTimerInfoTag>& event) const
169 const std::shared_ptr<const CPVRClient> client =
170 CServiceBroker::GetPVRManager().GetClient(CFileItem(event));
171 if (client)
173 const std::string hostname = client->GetBackendHostname();
174 if (!hostname.empty() && CServiceBroker::GetNetwork().IsLocalHost(hostname))
175 return true;
177 return false;
180 bool CPVRGUIActionsPowerManagement::IsNextEventWithinBackendIdleTime() const
182 // timers going off soon?
183 const CDateTime now(CDateTime::GetUTCDateTime());
184 const CDateTimeSpan idle(
185 0, 0, m_settings.GetIntValue(CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME), 0);
186 const CDateTime next(CServiceBroker::GetPVRManager().Timers()->GetNextEventTime());
187 const CDateTimeSpan delta(next - now);
189 return (delta <= idle);