Skip to content
Permalink
Browse files

macOS: disable AppNap during sync

Backport from Bitcoin core
[Includes] Add missing include for macos appnap code


[Trivial] Fix include on appnap 


[Trivial] include fixes


[Includes] include macoss appnap header after qt imports


[Qt] Fix variable name


[Trival] Fix syntax error


[Trivial] Fix syntax
  • Loading branch information...
akshaynexus committed Jul 7, 2019
1 parent 3defdbf commit 0c741bc7757176eb6328bed2cb90fd27cdb686d9
Showing with 115 additions and 11 deletions.
  1. +1 −0 contrib/dogecash-qt.pro
  2. +0 −4 share/qt/Info.plist.in
  3. +3 −2 src/Makefile.qt.include
  4. +10 −3 src/qt/bitcoingui.cpp
  5. +6 −2 src/qt/bitcoingui.h
  6. +24 −0 src/qt/macos_appnap.h
  7. +71 −0 src/qt/macos_appnap.mm
@@ -164,6 +164,7 @@ HEADERS += src/activemasternode.h \
src/obj/build.h \
src/primitives/block.h \
src/primitives/transaction.h \
src/qt/macos_appnap.h \
src/qt/addressbookpage.h \
src/qt/addresstablemodel.h \
src/qt/askpassphrasedialog.h \
@@ -96,10 +96,6 @@

<key>NSHighResolutionCapable</key>
<string>True</string>

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

<key>LSApplicationCategoryType</key>
<string>public.app-category.finance</string>
</dict>
@@ -126,8 +126,8 @@ QT_MOC_CPP = \

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

qt/macnotificationhandler.mm \
qt/macos_appnap.mm
QT_MOC = \
qt/dogecash.moc \
qt/bitcoinamountfield.moc \
@@ -146,6 +146,7 @@ PROTOBUF_PROTO = qt/paymentrequest.proto

BITCOIN_QT_H = \
qt/addressbookpage.h \
qt/macos_appnap.h \
qt/addresstablemodel.h \
qt/askpassphrasedialog.h \
qt/bantablemodel.h \
@@ -276,7 +276,9 @@ BitcoinGUI::BitcoinGUI(const NetworkStyle* networkStyle, QWidget* parent) : QMai
timerAutoMintIcon->start(10000);
setAutoMintStatus();
}

#ifdef Q_OS_MAC
CAppNapInhibitor* m_app_nap_inhibitor = new CAppNapInhibitor;
#endif
BitcoinGUI::~BitcoinGUI()
{
// Unsubscribe from notifications from core
@@ -286,6 +288,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
@@ -924,14 +927,18 @@ void BitcoinGUI::setNumConnections(int count)

void BitcoinGUI::setNumBlocks(int count)
{
// Acquire current block source
enum BlockSource blockSource = clientModel->getBlockSource();
#ifdef Q_OS_MAC
(IsInitialBlockDownload() || blockSource == BLOCK_SOURCE_REINDEX || blockSource == BLOCK_SOURCE_DISK ) ? m_app_nap_inhibitor->disableAppNap() : m_app_nap_inhibitor->enableAppNap();
#endif
if (!clientModel)
return;

// Prevent orphan statusbar messages (e.g. hover Quit in main menu, wait until chain-sync starts -> garbelled text)
statusBar()->clearMessage();

// Acquire current block source
enum BlockSource blockSource = clientModel->getBlockSource();

switch (blockSource) {
case BLOCK_SOURCE_NETWORK:
progressBarLabel->setText(tr("Synchronizing with network..."));
@@ -19,7 +19,9 @@
#include <QPoint>
#include <QPushButton>
#include <QSystemTrayIcon>

#ifdef Q_OS_MAC
#include "qt/macos_appnap.h"
#endif
class ClientModel;
class NetworkStyle;
class Notificator;
@@ -137,7 +139,9 @@ class BitcoinGUI : public QMainWindow
Notificator* notificator;
RPCConsole* rpcConsole;
BlockExplorer* explorerWindow;

#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;
@@ -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
@@ -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 0c741bc

Please sign in to comment.
You can’t perform that action at this time.