Skip to content

Commit edc7152

Browse files
committed
Merge #12783: macOS: disable AppNap during sync
1e0f3c4 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
2 parents 16e3b17 + 1e0f3c4 commit edc7152

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

share/qt/Info.plist.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@
9797
<key>NSHighResolutionCapable</key>
9898
<string>True</string>
9999

100-
<key>LSAppNapIsDisabled</key>
101-
<string>True</string>
102-
103100
<key>NSRequiresAquaSystemAppearance</key>
104101
<string>True</string>
105102

src/Makefile.qt.include

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ QT_MOC_CPP = \
162162

163163
BITCOIN_MM = \
164164
qt/macdockiconhandler.mm \
165-
qt/macnotificationhandler.mm
165+
qt/macnotificationhandler.mm \
166+
qt/macos_appnap.mm
166167

167168
QT_MOC = \
168169
qt/bitcoin.moc \
@@ -205,6 +206,7 @@ BITCOIN_QT_H = \
205206
qt/intro.h \
206207
qt/macdockiconhandler.h \
207208
qt/macnotificationhandler.h \
209+
qt/macos_appnap.h \
208210
qt/modaloverlay.h \
209211
qt/networkstyle.h \
210212
qt/notificator.h \

src/qt/bitcoingui.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
211211
connect(progressBar, &GUIUtil::ClickableProgressBar::clicked, this, &BitcoinGUI::showModalOverlay);
212212
}
213213
#endif
214+
215+
#ifdef Q_OS_MAC
216+
m_app_nap_inhibitor = new CAppNapInhibitor;
217+
#endif
214218
}
215219

216220
BitcoinGUI::~BitcoinGUI()
@@ -223,6 +227,7 @@ BitcoinGUI::~BitcoinGUI()
223227
if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
224228
trayIcon->hide();
225229
#ifdef Q_OS_MAC
230+
delete m_app_nap_inhibitor;
226231
delete appMenuBar;
227232
MacDockIconHandler::cleanup();
228233
#endif
@@ -786,6 +791,11 @@ void BitcoinGUI::openOptionsDialogWithTab(OptionsDialog::Tab tab)
786791

787792
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
788793
{
794+
// Disabling macOS App Nap on initial sync, disk and reindex operations.
795+
#ifdef Q_OS_MAC
796+
(m_node.isInitialBlockDownload() || m_node.getReindex() || m_node.getImporting()) ? m_app_nap_inhibitor->disableAppNap() : m_app_nap_inhibitor->enableAppNap();
797+
#endif
798+
789799
if (modalOverlay)
790800
{
791801
if (header)

src/qt/bitcoingui.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <QPoint>
2121
#include <QSystemTrayIcon>
2222

23+
#ifdef Q_OS_MAC
24+
#include <qt/macos_appnap.h>
25+
#endif
26+
2327
#include <memory>
2428

2529
class ClientModel;
@@ -143,6 +147,10 @@ class BitcoinGUI : public QMainWindow
143147
HelpMessageDialog* helpMessageDialog = nullptr;
144148
ModalOverlay* modalOverlay = nullptr;
145149

150+
#ifdef Q_OS_MAC
151+
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
152+
#endif
153+
146154
/** Keep track of previous number of blocks, to detect progress */
147155
int prevBlocks = 0;
148156
int spinnerFrame = 0;

src/qt/macos_appnap.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2011-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QT_MACOS_APPNAP_H
6+
#define BITCOIN_QT_MACOS_APPNAP_H
7+
8+
#include <memory>
9+
10+
class CAppNapInhibitor final
11+
{
12+
public:
13+
explicit CAppNapInhibitor();
14+
~CAppNapInhibitor();
15+
16+
void disableAppNap();
17+
void enableAppNap();
18+
19+
private:
20+
class CAppNapImpl;
21+
std::unique_ptr<CAppNapImpl> impl;
22+
};
23+
24+
#endif // BITCOIN_QT_MACOS_APPNAP_H

src/qt/macos_appnap.mm

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2011-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "macos_appnap.h"
6+
7+
#include <AvailabilityMacros.h>
8+
#include <Foundation/NSProcessInfo.h>
9+
#include <Foundation/Foundation.h>
10+
11+
class CAppNapInhibitor::CAppNapImpl
12+
{
13+
public:
14+
~CAppNapImpl()
15+
{
16+
if(activityId)
17+
enableAppNap();
18+
}
19+
20+
void disableAppNap()
21+
{
22+
if (!activityId)
23+
{
24+
@autoreleasepool {
25+
const NSActivityOptions activityOptions =
26+
NSActivityUserInitiatedAllowingIdleSystemSleep &
27+
~(NSActivitySuddenTerminationDisabled |
28+
NSActivityAutomaticTerminationDisabled);
29+
30+
id processInfo = [NSProcessInfo processInfo];
31+
if ([processInfo respondsToSelector:@selector(beginActivityWithOptions:reason:)])
32+
{
33+
activityId = [processInfo beginActivityWithOptions: activityOptions reason:@"Temporarily disable App Nap for bitcoin-qt."];
34+
[activityId retain];
35+
}
36+
}
37+
}
38+
}
39+
40+
void enableAppNap()
41+
{
42+
if(activityId)
43+
{
44+
@autoreleasepool {
45+
id processInfo = [NSProcessInfo processInfo];
46+
if ([processInfo respondsToSelector:@selector(endActivity:)])
47+
[processInfo endActivity:activityId];
48+
49+
[activityId release];
50+
activityId = nil;
51+
}
52+
}
53+
}
54+
55+
private:
56+
NSObject* activityId;
57+
};
58+
59+
CAppNapInhibitor::CAppNapInhibitor() : impl(new CAppNapImpl()) {}
60+
61+
CAppNapInhibitor::~CAppNapInhibitor() = default;
62+
63+
void CAppNapInhibitor::disableAppNap()
64+
{
65+
impl->disableAppNap();
66+
}
67+
68+
void CAppNapInhibitor::enableAppNap()
69+
{
70+
impl->enableAppNap();
71+
}

0 commit comments

Comments
 (0)