Backport #12783: macOS: disable AppNap during sync (and mixing) (#3024)

* Merge #12783: macOS: disable AppNap during sync

1e0f3c44992fb82e6bf36c2ef9277b0759c17c4c macOS: disable AppNap during sync (Alexey Ivanov)

Pull request description:

  Code based on pull/5804. Tested only on macOS 10.13.3 and should support 10.9+.

  What macOS versions bitcoin core currently supports?

Tree-SHA512: 85809b8d8d8a05169437b4268988da0b7372c29c6da3223ebdc106dc16dcb6d3caa5c52ace3591467005b50a63fd8b2ab1cb071cb4f450032932df25d5063315

* Refactor

* Drop `#include <memory>` from `src/qt/bitcoingui.h`

Was included by mistake.
This commit is contained in:
UdjinM6 2019-07-15 14:38:55 +03:00 committed by GitHub
parent 7eeee33144
commit d57cbc615b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 125 additions and 4 deletions

View File

@ -97,9 +97,6 @@
<key>NSHighResolutionCapable</key> <key>NSHighResolutionCapable</key>
<string>True</string> <string>True</string>
<key>LSAppNapIsDisabled</key>
<string>True</string>
<key>NSRequiresAquaSystemAppearance</key> <key>NSRequiresAquaSystemAppearance</key>
<string>True</string> <string>True</string>

View File

@ -103,7 +103,8 @@ QT_MOC_CPP = \
BITCOIN_MM = \ BITCOIN_MM = \
qt/macdockiconhandler.mm \ qt/macdockiconhandler.mm \
qt/macnotificationhandler.mm qt/macnotificationhandler.mm \
qt/macos_appnap.mm
QT_MOC = \ QT_MOC = \
qt/dash.moc \ qt/dash.moc \
@ -142,6 +143,7 @@ BITCOIN_QT_H = \
qt/intro.h \ qt/intro.h \
qt/macdockiconhandler.h \ qt/macdockiconhandler.h \
qt/macnotificationhandler.h \ qt/macnotificationhandler.h \
qt/macos_appnap.h \
qt/modaloverlay.h \ qt/modaloverlay.h \
qt/masternodelist.h \ qt/masternodelist.h \
qt/networkstyle.h \ qt/networkstyle.h \

View File

@ -24,6 +24,7 @@
#include "utilitydialog.h" #include "utilitydialog.h"
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include "privatesend/privatesend-client.h"
#include "walletframe.h" #include "walletframe.h"
#include "walletmodel.h" #include "walletmodel.h"
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
@ -266,6 +267,10 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
connect(progressBar, SIGNAL(clicked(QPoint)), this, SLOT(showModalOverlay())); connect(progressBar, SIGNAL(clicked(QPoint)), this, SLOT(showModalOverlay()));
} }
#endif #endif
#ifdef Q_OS_MAC
m_app_nap_inhibitor = new CAppNapInhibitor;
#endif
} }
BitcoinGUI::~BitcoinGUI() BitcoinGUI::~BitcoinGUI()
@ -277,6 +282,7 @@ BitcoinGUI::~BitcoinGUI()
if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu) if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
trayIcon->hide(); trayIcon->hide();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
delete m_app_nap_inhibitor;
delete appMenuBar; delete appMenuBar;
MacDockIconHandler::cleanup(); MacDockIconHandler::cleanup();
#endif #endif
@ -950,6 +956,19 @@ void BitcoinGUI::updateHeadersSyncProgressLabel()
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header) void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
{ {
#ifdef Q_OS_MAC
// Disabling macOS App Nap on initial sync, disk, reindex operations and mixing.
bool disableAppNap = !masternodeSync.IsSynced();
#ifdef ENABLE_WALLET
disableAppNap |= privateSendClient.fPrivateSendRunning;
#endif // ENABLE_WALLET
if (disableAppNap) {
m_app_nap_inhibitor->disableAppNap();
} else {
m_app_nap_inhibitor->enableAppNap();
}
#endif // Q_OS_MAC
if (modalOverlay) if (modalOverlay)
{ {
if (header) if (header)

View File

@ -19,6 +19,10 @@
#include <QPushButton> #include <QPushButton>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#ifdef Q_OS_MAC
#include <qt/macos_appnap.h>
#endif
class ClientModel; class ClientModel;
class NetworkStyle; class NetworkStyle;
class Notificator; class Notificator;
@ -131,6 +135,10 @@ private:
HelpMessageDialog *helpMessageDialog; HelpMessageDialog *helpMessageDialog;
ModalOverlay *modalOverlay; ModalOverlay *modalOverlay;
#ifdef Q_OS_MAC
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
#endif
/** Keep track of previous number of blocks, to detect progress */ /** Keep track of previous number of blocks, to detect progress */
int prevBlocks; int prevBlocks;
int spinnerFrame; int spinnerFrame;

24
src/qt/macos_appnap.h Normal file
View File

@ -0,0 +1,24 @@
// Copyright (c) 2011-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_QT_MACOS_APPNAP_H
#define BITCOIN_QT_MACOS_APPNAP_H
#include <memory>
class CAppNapInhibitor final
{
public:
explicit CAppNapInhibitor();
~CAppNapInhibitor();
void disableAppNap();
void enableAppNap();
private:
class CAppNapImpl;
std::unique_ptr<CAppNapImpl> impl;
};
#endif // BITCOIN_QT_MACOS_APPNAP_H

71
src/qt/macos_appnap.mm Normal file
View File

@ -0,0 +1,71 @@
// Copyright (c) 2011-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "macos_appnap.h"
#include <AvailabilityMacros.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/Foundation.h>
class CAppNapInhibitor::CAppNapImpl
{
public:
~CAppNapImpl()
{
if(activityId)
enableAppNap();
}
void disableAppNap()
{
if (!activityId)
{
@autoreleasepool {
const NSActivityOptions activityOptions =
NSActivityUserInitiatedAllowingIdleSystemSleep &
~(NSActivitySuddenTerminationDisabled |
NSActivityAutomaticTerminationDisabled);
id processInfo = [NSProcessInfo processInfo];
if ([processInfo respondsToSelector:@selector(beginActivityWithOptions:reason:)])
{
activityId = [processInfo beginActivityWithOptions: activityOptions reason:@"Temporarily disable App Nap for dash-qt."];
[activityId retain];
}
}
}
}
void enableAppNap()
{
if(activityId)
{
@autoreleasepool {
id processInfo = [NSProcessInfo processInfo];
if ([processInfo respondsToSelector:@selector(endActivity:)])
[processInfo endActivity:activityId];
[activityId release];
activityId = nil;
}
}
}
private:
NSObject* activityId;
};
CAppNapInhibitor::CAppNapInhibitor() : impl(new CAppNapImpl()) {}
CAppNapInhibitor::~CAppNapInhibitor() = default;
void CAppNapInhibitor::disableAppNap()
{
impl->disableAppNap();
}
void CAppNapInhibitor::enableAppNap()
{
impl->enableAppNap();
}