Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -103,7 +103,8 @@ QT_MOC_CPP = \

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

QT_MOC = \
qt/dash.moc \
Expand Down Expand Up @@ -142,6 +143,7 @@ BITCOIN_QT_H = \
qt/intro.h \
qt/macdockiconhandler.h \
qt/macnotificationhandler.h \
qt/macos_appnap.h \
qt/modaloverlay.h \
qt/masternodelist.h \
qt/networkstyle.h \
Expand Down
19 changes: 19 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "utilitydialog.h"

#ifdef ENABLE_WALLET
#include "privatesend/privatesend-client.h"
#include "walletframe.h"
#include "walletmodel.h"
#endif // ENABLE_WALLET
Expand Down Expand Up @@ -266,6 +267,10 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
connect(progressBar, SIGNAL(clicked(QPoint)), this, SLOT(showModalOverlay()));
}
#endif

#ifdef Q_OS_MAC
m_app_nap_inhibitor = new CAppNapInhibitor;
#endif
}

BitcoinGUI::~BitcoinGUI()
Expand All @@ -277,6 +282,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 @@ -950,6 +956,19 @@ void BitcoinGUI::updateHeadersSyncProgressLabel()

void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
{
#ifdef Q_OS_MAC
Copy link

@codablock codablock Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my... :D
The original from Bitcoin was already bad...using the ternary operator as a replacement for if/else and at the same time relying on the side effects of the evaluation result... :D
Can you maybe change this to something as simple as:

#ifdef Q_OS_MAC
    bool disableAppNap = !masternodeSync.IsSynced();
#ifdef ENABLE_WALLET
    if (privateSendClient.fPrivateSendRunning) {
        disableAppNap = true;
    }
#endif
    if (disableAppNap) {
        m_app_nap_inhibitor->disableAppNap()
    } else {
        m_app_nap_inhibitor->enableAppNap()
    }
#edif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code sample to use disableAppNap instead of enableAppNap

Copy link
Author

@UdjinM6 UdjinM6 Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 7ffb163? :) (mostly the same as yours, just changed if (privateSendClient.fPrivateSendRunning) part)

// 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 (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 @@ -19,6 +19,10 @@
#include <QPushButton>
#include <QSystemTrayIcon>

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

class ClientModel;
class NetworkStyle;
class Notificator;
Expand Down Expand Up @@ -133,6 +137,10 @@ class BitcoinGUI : public QMainWindow
HelpMessageDialog *helpMessageDialog;
ModalOverlay *modalOverlay;

#ifdef Q_OS_MAC
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
#endif

/** Keep track of previous number of blocks, to detect progress */
int prevBlocks;
int spinnerFrame;
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 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();
}