Skip to content

Commit 573a372

Browse files
UdjinM6codablock
authored andcommitted
Backport bitcoin#12783: macOS: disable AppNap during sync (and mixing) (dashpay#3024)
* Merge bitcoin#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 * Refactor * Drop `#include <memory>` from `src/qt/bitcoingui.h` Was included by mistake.
1 parent 4286dde commit 573a372

File tree

6 files changed

+125
-4
lines changed

6 files changed

+125
-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
@@ -102,7 +102,8 @@ QT_MOC_CPP = \
102102

103103
BITCOIN_MM = \
104104
qt/macdockiconhandler.mm \
105-
qt/macnotificationhandler.mm
105+
qt/macnotificationhandler.mm \
106+
qt/macos_appnap.mm
106107

107108
QT_MOC = \
108109
qt/dash.moc \
@@ -139,6 +140,7 @@ BITCOIN_QT_H = \
139140
qt/intro.h \
140141
qt/macdockiconhandler.h \
141142
qt/macnotificationhandler.h \
143+
qt/macos_appnap.h \
142144
qt/modaloverlay.h \
143145
qt/masternodelist.h \
144146
qt/networkstyle.h \

src/qt/bitcoingui.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "utilitydialog.h"
2525

2626
#ifdef ENABLE_WALLET
27+
#include "privatesend/privatesend-client.h"
2728
#include "walletframe.h"
2829
#include "walletmodel.h"
2930
#endif // ENABLE_WALLET
@@ -266,6 +267,10 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
266267
connect(progressBar, SIGNAL(clicked(QPoint)), this, SLOT(showModalOverlay()));
267268
}
268269
#endif
270+
271+
#ifdef Q_OS_MAC
272+
m_app_nap_inhibitor = new CAppNapInhibitor;
273+
#endif
269274
}
270275

271276
BitcoinGUI::~BitcoinGUI()
@@ -277,6 +282,7 @@ BitcoinGUI::~BitcoinGUI()
277282
if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
278283
trayIcon->hide();
279284
#ifdef Q_OS_MAC
285+
delete m_app_nap_inhibitor;
280286
delete appMenuBar;
281287
MacDockIconHandler::cleanup();
282288
#endif
@@ -950,6 +956,19 @@ void BitcoinGUI::updateHeadersSyncProgressLabel()
950956

951957
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
952958
{
959+
#ifdef Q_OS_MAC
960+
// Disabling macOS App Nap on initial sync, disk, reindex operations and mixing.
961+
bool disableAppNap = !masternodeSync.IsSynced();
962+
#ifdef ENABLE_WALLET
963+
disableAppNap |= privateSendClient.fPrivateSendRunning;
964+
#endif // ENABLE_WALLET
965+
if (disableAppNap) {
966+
m_app_nap_inhibitor->disableAppNap();
967+
} else {
968+
m_app_nap_inhibitor->enableAppNap();
969+
}
970+
#endif // Q_OS_MAC
971+
953972
if (modalOverlay)
954973
{
955974
if (header)

src/qt/bitcoingui.h

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

22+
#ifdef Q_OS_MAC
23+
#include <qt/macos_appnap.h>
24+
#endif
25+
2226
class ClientModel;
2327
class NetworkStyle;
2428
class Notificator;
@@ -133,6 +137,10 @@ class BitcoinGUI : public QMainWindow
133137
HelpMessageDialog *helpMessageDialog;
134138
ModalOverlay *modalOverlay;
135139

140+
#ifdef Q_OS_MAC
141+
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
142+
#endif
143+
136144
/** Keep track of previous number of blocks, to detect progress */
137145
int prevBlocks;
138146
int spinnerFrame;

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 dash-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)