Skip to content

Commit

Permalink
macOS: disable AppNap during sync
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Ivanov <[email protected]>
  • Loading branch information
krab committed Nov 1, 2018
1 parent f6df989 commit 1e0f3c4
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
3 changes: 0 additions & 3 deletions share/qt/Info.plist.in
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@
<key>NSHighResolutionCapable</key>
<string>True</string>

<key>LSAppNapIsDisabled</key>
<string>True</string>

<key>NSRequiresAquaSystemAppearance</key>
<string>True</string>

Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ QT_MOC_CPP = \

BITCOIN_MM = \
qt/macdockiconhandler.mm \
qt/macnotificationhandler.mm
qt/macnotificationhandler.mm \
qt/macos_appnap.mm

QT_MOC = \
qt/bitcoin.moc \
Expand Down Expand Up @@ -205,6 +206,7 @@ BITCOIN_QT_H = \
qt/intro.h \
qt/macdockiconhandler.h \
qt/macnotificationhandler.h \
qt/macos_appnap.h \
qt/modaloverlay.h \
qt/networkstyle.h \
qt/notificator.h \
Expand Down
10 changes: 10 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
connect(progressBar, &GUIUtil::ClickableProgressBar::clicked, this, &BitcoinGUI::showModalOverlay);
}
#endif

#ifdef Q_OS_MAC
m_app_nap_inhibitor = new CAppNapInhibitor;
#endif
}

BitcoinGUI::~BitcoinGUI()
Expand All @@ -223,6 +227,7 @@ BitcoinGUI::~BitcoinGUI()
if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
trayIcon->hide();
#ifdef Q_OS_MAC
delete m_app_nap_inhibitor;
delete appMenuBar;
MacDockIconHandler::cleanup();
#endif
Expand Down Expand Up @@ -786,6 +791,11 @@ void BitcoinGUI::openOptionsDialogWithTab(OptionsDialog::Tab tab)

void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
{
// Disabling macOS App Nap on initial sync, disk and reindex operations.
#ifdef Q_OS_MAC
(m_node.isInitialBlockDownload() || m_node.getReindex() || m_node.getImporting()) ? m_app_nap_inhibitor->disableAppNap() : m_app_nap_inhibitor->enableAppNap();
#endif

if (modalOverlay)
{
if (header)
Expand Down
8 changes: 8 additions & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include <QPoint>
#include <QSystemTrayIcon>

#ifdef Q_OS_MAC
#include <qt/macos_appnap.h>
#endif

#include <memory>

class ClientModel;
Expand Down Expand Up @@ -143,6 +147,10 @@ class BitcoinGUI : public QMainWindow
HelpMessageDialog* helpMessageDialog = nullptr;
ModalOverlay* modalOverlay = nullptr;

#ifdef Q_OS_MAC
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
#endif

/** Keep track of previous number of blocks, to detect progress */
int prevBlocks = 0;
int spinnerFrame = 0;
Expand Down
24 changes: 24 additions & 0 deletions src/qt/macos_appnap.h
Original file line number Diff line number Diff line change
@@ -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 changes: 71 additions & 0 deletions src/qt/macos_appnap.mm
Original file line number Diff line number Diff line change
@@ -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 bitcoin-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();
}

0 comments on commit 1e0f3c4

Please sign in to comment.