Browse Source

Merge pull request #19346 from Chocobo1/powerMgt

Avoid excessive power management updates
adaptive-webui-19844
Chocobo1 1 year ago committed by GitHub
parent
commit
c20a77aa77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      src/gui/mainwindow.cpp
  2. 2
      src/gui/mainwindow.h
  3. 23
      src/gui/powermanagement/powermanagement.cpp
  4. 23
      src/gui/powermanagement/powermanagement.h
  5. 8
      src/gui/powermanagement/powermanagement_x11.cpp

15
src/gui/mainwindow.cpp

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

2
src/gui/mainwindow.h

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

23
src/gui/powermanagement/powermanagement.cpp

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

23
src/gui/powermanagement/powermanagement.h

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

8
src/gui/powermanagement/powermanagement_x11.cpp

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

Loading…
Cancel
Save