TheInfiniteKind / appbundler

74 stars 24 forks source link

Add option to disable App Nap #7

Open sreilly opened 9 years ago

sreilly commented 9 years ago

Original report by Anonymous.


App Nap (introduced in Mavericks) has been making my Java app sluggish. It's a real-time music app, so it needs to be responsive. However, OS X decides - after a couple minutes of it being in the background - that it is not doing anything and should be put to sleep: aka the dreaded App Nap. This causes threads to wake up later than they are supposed to and ultimately the music notes lag or the USB device disconnects incorrectly.

There are a couple of hacks around it, for instance:

Hack 1

Putting

#!xml
<key>LSUIElement</key>
<true/>

in the PList, however that makes your app truly run in the background and removes your dock icon.

Hack 2

Another hack is to run this terminal command every time you start the app:

#!javascript
defaults write [app.bundle.name] NSAppSleepDisabled -bool YES

That is also rather hacky, and there are reports of it not working.

Proper Way

The recommended, Apple way of disabling App Nap is in code, like this example:

#!objective-c

[[NSProcessInfo processInfo] beginActivityWithOptions: NSActivityIdleSystemSleepDisabled| NSActivitySuddenTerminationDisabled];

Is there a way we can add an option to the build file that runs this method in the launcher? It also needs to run the endActivity method after the app exits. This is the part I'm not sure about: does JLI_Launch() return once the app exits, or does it return immediately?

sreilly commented 9 years ago

Oops, submitted this anonymously. Commenting to get follow-up emails.

sreilly commented 9 years ago

This sounds like an enhancement request. Would anyone like to contribute the objective-C code to make it work?

sreilly commented 9 years ago

https://github.com/shannah/Java-Objective-C-Bridge/blob/master/java/test/ca/weblite/objc/NSProcessInfoUtils.java

sreilly commented 8 years ago

Adding this to main.m (just above the call to jli_LaunchFxnPtr) seems to have disabled app nap successfully.

    // Attempt to prevent app nap from slowing us down
    if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)])
            [[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"Can't have App Nap"];

    // Invoke JLI_Launch()

It would be nice to have a setting in the config file to turn this on or off, but I'm not sufficiently familiar with how the settings work do add that. Perhaps someone else can add a setting to control this feature.

-JM