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

osx: disable app-nap programatically #5804

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -306,6 +306,7 @@ case $host in
esac
fi

AX_CHECK_LINK_FLAG([[-framework Foundation]], [LDFLAGS="$LDFLAGS -framework Foundation"])
AX_CHECK_LINK_FLAG([[-Wl,-headerpad_max_install_names]], [LDFLAGS="$LDFLAGS -Wl,-headerpad_max_install_names"])
CPPFLAGS="$CPPFLAGS -DMAC_OSX"
;;
Expand Down
3 changes: 0 additions & 3 deletions share/qt/Info.plist.in
Expand Up @@ -91,9 +91,6 @@
<key>NSHighResolutionCapable</key>
<string>True</string>

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

<key>LSApplicationCategoryType</key>
<string>public.app-category.finance</string>
</dict>
Expand Down
5 changes: 5 additions & 0 deletions src/Makefile.am
Expand Up @@ -70,6 +70,7 @@ endif
.PHONY: FORCE
# bitcoin core #
BITCOIN_CORE_H = \
appnap.h \
addrman.h \
alert.h \
allocators.h \
Expand Down Expand Up @@ -189,6 +190,10 @@ libbitcoin_server_a_SOURCES = \
$(JSON_H) \
$(BITCOIN_CORE_H)

if TARGET_DARWIN
libbitcoin_server_a_SOURCES += appnap.mm
endif

# wallet: shared between bitcoind and bitcoin-qt, but only linked
# when wallet enabled
libbitcoin_wallet_a_CPPFLAGS = $(BITCOIN_INCLUDES)
Expand Down
20 changes: 20 additions & 0 deletions src/appnap.h
@@ -0,0 +1,20 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 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_APPNAPINHIBITOR_H
Copy link
Member

Choose a reason for hiding this comment

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

Please don't add these kind of one-use utilities in the top-level src directory. As I've suggested before, a an src/support or such directory would make sense.

#define BITCOIN_APPNAPINHIBITOR_H

class CAppNapInhibitorInt;

class CAppNapInhibitor
{
public:
CAppNapInhibitor(const char* strReason);
~CAppNapInhibitor();
private:
CAppNapInhibitorInt *priv;
};

#endif // BITCOIN_APPNAPINHIBITOR_H
28 changes: 28 additions & 0 deletions src/appnap.mm
@@ -0,0 +1,28 @@
#include "appnap.h"

#include <AvailabilityMacros.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/Foundation.h>

class CAppNapInhibitorInt
Copy link
Member

Choose a reason for hiding this comment

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

Objective C in the core? :(

{
public:
id<NSObject> activityId;
};

CAppNapInhibitor::CAppNapInhibitor(const char* strReason) : priv(new CAppNapInhibitorInt)
{
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)])
priv->activityId = [[NSProcessInfo processInfo ] beginActivityWithOptions: NSActivityUserInitiatedAllowingIdleSystemSleep reason:@(strReason)];
#endif
}

CAppNapInhibitor::~CAppNapInhibitor()
{
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(endActivity:)])
[[NSProcessInfo processInfo] endActivity:priv->activityId];
#endif
delete priv;
}
2 changes: 2 additions & 0 deletions src/bitcoind.cpp
Expand Up @@ -59,6 +59,8 @@ bool AppInit(int argc, char* argv[])

bool fRet = false;

CIdleInhibitor noIdle("bitcoind");

//
// Parameters
//
Expand Down
2 changes: 2 additions & 0 deletions src/qt/bitcoin.cpp
Expand Up @@ -490,6 +490,8 @@ int main(int argc, char *argv[])
{
SetupEnvironment();

CIdleInhibitor noIdle("bitcoin-qt");

/// 1. Parse command-line options. These take precedence over anything else.
// Command-line options take precedence:
ParseParameters(argc, argv);
Expand Down
23 changes: 23 additions & 0 deletions src/util.h
Expand Up @@ -18,6 +18,10 @@
#include "tinyformat.h"
#include "utiltime.h"

#ifdef MAC_OSX
#include "appnap.h"
#endif

#include <exception>
#include <map>
#include <stdint.h>
Expand Down Expand Up @@ -229,4 +233,23 @@ template <typename Callable> void TraceThread(const char* name, Callable func)
}
}

// A RAII wrapper around calls to forbid and re-allow entering into
// low-priority states, specifically OSX's app-nap. For the scope of
// the object, napping is disabled. Several objects may co-exist, napping is
// allowed when the last one is destroyed.

class CIdleInhibitor
{
public:
#ifdef MAC_OSX
CIdleInhibitor(const char* strReason) : inhibit(strReason){}
#else
CIdleInhibitor(const char*){}
#endif
private:
#ifdef MAC_OSX
CAppNapInhibitor inhibit;
#endif
};

#endif // BITCOIN_UTIL_H