Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Cocoa equivalent of UpdateSystemActivity?

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
like image 721
i_am_jorf Avatar asked Aug 24 '09 21:08

i_am_jorf


2 Answers

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

like image 192
Roland Rabien Avatar answered Sep 24 '22 10:09

Roland Rabien


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.

like image 24
Dan McCormick Avatar answered Sep 22 '22 10:09

Dan McCormick