Merge pull request #19346 from Chocobo1/powerMgt

Avoid excessive power management updates
This commit is contained in:
Chocobo1 2023-07-22 12:45:55 +08:00 committed by GitHub
commit c20a77aa77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 31 deletions

View file

@ -323,8 +323,10 @@ MainWindow::MainWindow(IGUIApplication *app, WindowState initialState)
// Initialise system sleep inhibition timer
m_pwr = new PowerManagement(this);
m_preventTimer = new QTimer(this);
m_preventTimer->setSingleShot(true);
connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::updatePowerManagementState);
m_preventTimer->start(PREVENT_SUSPEND_INTERVAL);
connect(pref, &Preferences::changed, this, &MainWindow::updatePowerManagementState);
updatePowerManagementState();
// Configure BT session according to options
loadPreferences();
@ -1465,8 +1467,6 @@ void MainWindow::loadPreferences()
showStatusBar(pref->isStatusbarDisplayed());
updatePowerManagementState();
m_transferListWidget->setAlternatingRowColors(pref->useAlternatingRowColors());
m_propertiesWidget->getFilesList()->setAlternatingRowColors(pref->useAlternatingRowColors());
m_propertiesWidget->getTrackerList()->setAlternatingRowColors(pref->useAlternatingRowColors());
@ -1927,10 +1927,11 @@ void MainWindow::on_actionAutoShutdown_toggled(bool enabled)
Preferences::instance()->setShutdownWhenDownloadsComplete(enabled);
}
void MainWindow::updatePowerManagementState()
void MainWindow::updatePowerManagementState() const
{
const bool preventFromSuspendWhenDownloading = Preferences::instance()->preventFromSuspendWhenDownloading();
const bool preventFromSuspendWhenSeeding = Preferences::instance()->preventFromSuspendWhenSeeding();
const auto *pref = Preferences::instance();
const bool preventFromSuspendWhenDownloading = pref->preventFromSuspendWhenDownloading();
const bool preventFromSuspendWhenSeeding = pref->preventFromSuspendWhenSeeding();
const QVector<BitTorrent::Torrent *> allTorrents = BitTorrent::Session::instance()->torrents();
const bool inhibitSuspend = std::any_of(allTorrents.cbegin(), allTorrents.cend(), [&](const BitTorrent::Torrent *torrent)
@ -1944,6 +1945,8 @@ void MainWindow::updatePowerManagementState()
return torrent->isMoving();
});
m_pwr->setActivityState(inhibitSuspend);
m_preventTimer->start(PREVENT_SUSPEND_INTERVAL);
}
void MainWindow::applyTransferListFilter()

View file

@ -164,7 +164,7 @@ private slots:
void on_actionExit_triggered();
void on_actionLock_triggered();
// Check for unpaused downloading or seeding torrents and prevent system suspend/sleep according to preferences
void updatePowerManagementState();
void updatePowerManagementState() const;
void toolbarMenuRequested();
void toolbarIconsOnly();

View file

@ -44,10 +44,15 @@
PowerManagement::PowerManagement(QObject *parent)
: QObject(parent)
{
#ifdef QBT_USES_DBUS
m_inhibitor = new PowerManagementInhibitor(this);
, m_inhibitor {new PowerManagementInhibitor(this)}
#endif
{
}
PowerManagement::~PowerManagement()
{
setIdle();
}
void PowerManagement::setActivityState(const bool busy)
@ -60,15 +65,16 @@ void PowerManagement::setActivityState(const bool busy)
void PowerManagement::setBusy()
{
if (m_busy) return;
if (m_busy)
return;
m_busy = true;
#ifdef Q_OS_WIN
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
::SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
#elif defined(QBT_USES_DBUS)
m_inhibitor->requestBusy();
#elif defined(Q_OS_MACOS)
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn
const IOReturn success = ::IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn
, tr("qBittorrent is active").toCFString(), &m_assertionID);
if (success != kIOReturnSuccess)
m_busy = false;
@ -77,14 +83,15 @@ void PowerManagement::setBusy()
void PowerManagement::setIdle()
{
if (!m_busy) return;
if (!m_busy)
return;
m_busy = false;
#ifdef Q_OS_WIN
SetThreadExecutionState(ES_CONTINUOUS);
::SetThreadExecutionState(ES_CONTINUOUS);
#elif defined(QBT_USES_DBUS)
m_inhibitor->requestIdle();
#elif defined(Q_OS_MACOS)
IOPMAssertionRelease(m_assertionID);
::IOPMAssertionRelease(m_assertionID);
#endif
}

View file

@ -36,31 +36,30 @@
#endif
#ifdef QBT_USES_DBUS
// Require DBus
class PowerManagementInhibitor;
#endif
class PowerManagement : public QObject
class PowerManagement final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(PowerManagement)
Q_OBJECT
Q_DISABLE_COPY_MOVE(PowerManagement)
public:
PowerManagement(QObject *parent = nullptr);
virtual ~PowerManagement() = default;
PowerManagement(QObject *parent = nullptr);
~PowerManagement() override;
void setActivityState(bool busy);
void setActivityState(bool busy);
private:
void setBusy();
void setIdle();
void setBusy();
void setIdle();
bool m_busy = false;
bool m_busy = false;
#ifdef QBT_USES_DBUS
PowerManagementInhibitor *m_inhibitor = nullptr;
PowerManagementInhibitor *m_inhibitor = nullptr;
#endif
#ifdef Q_OS_MACOS
IOPMAssertionID m_assertionID {};
IOPMAssertionID m_assertionID {};
#endif
};

View file

@ -80,15 +80,15 @@ void PowerManagementInhibitor::requestIdle()
if ((m_state == Error) || (m_state == Idle) || (m_state == RequestIdle) || (m_state == RequestBusy))
return;
m_state = RequestIdle;
if (m_manager == ManagerType::Systemd)
{
QDBusUnixFileDescriptor dummy;
m_fd.swap(dummy);
m_fd = {};
m_state = Idle;
return;
}
m_state = RequestIdle;
const QString method = (m_manager == ManagerType::Gnome)
? u"Uninhibit"_s
: u"UnInhibit"_s;