6

I'm converting a Carbon app to a Cocoa app and I can't find the Cocoa equivalent for:

UpdateSystemActivity(UsrActivity);

Any Mac people out there care to point me in the right direction? Thanks.

UPDATE: I'm building 64bit. Building 32bit works fine, but I get symbol not declared in this scope errors for UpdateSystemActivity (and others) when I build for 64bit.

UPDATE2: I'm importing the following:

#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <OpenGL/CGLMacro.h>

Is there some other thing I need to import when building 64bit?

UPDATE3: Adding #import <CoreServices/CoreServices.h> did not help. I still get compiler errors telling me UpdateSystemActivity and UsrActivity was not declared in this scope.

UPDATE4: Okay, file not found on OSServices/Power.h. I'm building against the 10.5 SDK and a quick search shows:

$ pwd
/Developer/SDKs
$ find . -name Power.h
./MacOSX10.3.9.sdk/Developer/Headers/CFMCarbon/OSServices/Power.h
./MacOSX10.3.9.sdk/Developer/Headers/CFMCarbon/Power.h
./MacOSX10.3.9.sdk/Developer/Headers/FlatCarbon/Power.h
./MacOSX10.3.9.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/Headers/Power.h

./MacOSX10.4u.sdk/Developer/Headers/CFMCarbon/OSServices/Power.h
./MacOSX10.4u.sdk/Developer/Headers/CFMCarbon/Power.h
./MacOSX10.4u.sdk/Developer/Headers/FlatCarbon/Power.h
./MacOSX10.4u.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/Headers/Power.h

./MacOSX10.5.sdk/Developer/Headers/FlatCarbon/Power.h
./MacOSX10.5.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/Headers/Power.h

Yet I get:

Mac.mm:6:29: error: OSServices/Power.h: No such file or directory
Mac.mm:6:29: error: OSServices/Power.h: No such file or directory
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Based on this line: ./MacOSX10.5.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/Headers/Power.h You'll need to include the OSServices framework into your project -- is it there? – fbrereto Aug 28 '09 at 17:58
  • I haven't had a chance to get back to this. Snow Leopard completely broke our app and I've been racing to just get it working. I will be getting back to this soon, however. Thanks for your continued help. – i_am_jorf Aug 28 '09 at 20:22
  • Well we shelved the 64bit build, so I've not gone further with this. But I think fbereto was on the right track. – i_am_jorf Sep 19 '09 at 00:15
  • If your goal is keeping the screen from dimming, see https://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946 – rogerdpack Dec 15 '17 at 19:59

3 Answers3

5

In OS X 10.6 and later IOKit can be used to disable sleep. Create an IOPMAssertion when you want to disable sleep and destroy it when you want to allow sleep again.

#import <IOKit/pwr_mgt/IOPMLib.h>

// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep
// reasonForActivity is a descriptive string why sleep is disabled

CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");

IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &assertionID);

if (success == kIOReturnSuccess)
{ 
    //Add the work you need to do without the system sleeping here.

    success = IOPMAssertionRelease(assertionID);
    //The system will be able to sleep again.
}

More information: https://developer.apple.com/library/mac/qa/qa1340/_index.html

Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
3

The issue here appears to be the line in OSServices.h that excludes Power.h if __LP64__ is defined. When building 64 bit on 10.5 UpdateSystemActivity is indeed undefined.

The good news is that the symbol does actually exist in CoreServices.framework. There are two ways to get access to it.

  1. Forward declare it: extern "C" OSErr UpdateSystemActivity(UInt8);
  2. Explicitly include Power.h, which you tried. The issue with your attempt is that OSServices/ doesn't find it's way into the search path. You can include it like so: #import </Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks/OSServices.framework/Headers/Power.h>

I don't have a copy of SnowLeopard handy, but the next thing to do would be to check if it's fixed there. If it isn't, file a RADAR as this is clearly an SDK bug.

  • 1
    UpdateSystemActivity() in the Power.h header is indeed back for 64-bit apps in the 10.6 SDK. So it would appear that the omission was unintentional, and UpdateSystemActivity() is still supported for use in preventing idle sleep as described in http://developer.apple.com/mac/library/qa/qa2004/qa1160.html. Just use Xcode's "Open quickly" to get to Power.h, and copy the function declaration and the activity type enum to your own workaround header. – natevw Jan 22 '10 at 01:19
  • 1
    natevw: Even that shouldn't be necessary anymore. Simply #including `` should do it, since the #include of Power.h is no longer covered by an `#if !__LP64__` line. – Peter Hosey Mar 20 '10 at 22:03
2

You should still be able to call UpdateSystemActivity from within your Cocoa app -- it has not been marked deprecated.

The documentation for the API specifies importing CoreServices/CoreServices.h to get the API -- however hunting through the headers (notably in OSServices/OSServices.h) shows that the file is omitted in a 64bit environment. Nevertheless, there are sections of Power.h (where UpdateSystemActivity is defined) that are turned off for 64bits, and UpdateSystemActivity is not one of them.

In light of that, try to #import <OSServices/Power.h> directly and see if that works. (You'll have to include the CoreServices framework in your project for the header to be found as well.)

fbrereto
  • 35,429
  • 19
  • 126
  • 178
  • I am lead to believe that Carbon itself is deprecated with 64bit. I am building a 64bit app. – i_am_jorf Aug 24 '09 at 21:18
  • 1
    You are led to believe semi-incorrectly. Bits of Carbon, most notably HIServices, do not exist in 64-bit. Other bits have been redefined as non-Carbon (e.g. the File Manager, which is now considered a “core component” unrelated to Carbon despite living in CarbonCore.framework). If you can compile and link code using UpdateSystemActivity() in 64-bit, it is unaffected. – Jens Ayton Aug 24 '09 at 21:33
  • Ah, but I cannot compile a link. 32bit compiles, but 64bit says "I know not this symbol UpdateSystemActivity; forsooth, lead me to it." And nothing has changed in what I am including. – i_am_jorf Aug 24 '09 at 21:38
  • What framework are you expecting it to be in? The docs say it should be a part of CoreServices. – fbrereto Aug 24 '09 at 21:46
  • I appreciate your continued efforts on this topic, btw. Thanks. – i_am_jorf Aug 25 '09 at 00:04
  • sure thing :) I'm getting quite curious to see if this API really is available for 64bits, as they claim it to be but aren't making it easy to use! – fbrereto Aug 25 '09 at 00:16
  • Ran out of time today; will pick it up again tomorrow and let you know if works. – i_am_jorf Aug 25 '09 at 01:56
  • 1
    Yes you can still call `UpdateSystemActivity` but it seems that it doesn't have the desired effect in 64-bit apps (the screen still dims or screen saver activated). – adib Mar 30 '10 at 15:53
  • If you're targeting 10.8, it's deprecated now. Downvoting this in favor of the IOKit answer below (even though it was a good answer at the time!) – thakis Jun 24 '16 at 21:21