S
S
silvansky2012-10-08 16:25:24
Cocoa
silvansky, 2012-10-08 16:25:24

How to block a screensaver from running in OS X and unblock it back?

Good day everyone!
I have a seemingly simple goal: to temporarily block the launch of the screensaver, as, for example, any video player does.
Functions IOPMAssertionCreateWithNameand IOPMAssertionReleasefrom IOKitgoogled quickly, as did the demo code. But unfortunately it didn't work for me.
More precisely, earned, but not fully. The screensaver is locked but not unlocked.
I made a small test example: github.com/silvansky/OSX-TestSleep - the essence is reduced to this piece:

- (IBAction)toggle:(id)sender
{
  if (_assertionID == kIOPMNullAssertionID)
  {
    // toggle on
    CFStringRef reasonForActivity= CFSTR("Test reason");

    IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &_assertionID);
    [self.statusField setStringValue:@"Assert ON"];
    NSLog(@"*** lock: %d, success: %d", _assertionID, success);
  }
  else
  {
    // toggle off
    IOReturn success = IOPMAssertionRelease(_assertionID);
    [self.statusField setStringValue:@"Assert OFF"];
    NSLog(@"*** unlock: %d. success: %d", _assertionID, success);
    _assertionID = kIOPMNullAssertionID;
  }
}

On the first click on the button, everything is OK - the screensaver does not start. On the second - everything is not ok, the screensaver still does not start. Only the end of the program helps.
Can anyone suggest what is the problem here?
Tested on OS X 10.8.2, Xcode 4.5.1.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcholberg, 2012-10-08
@silvansky

You can add a button with the following call:

- (IBAction)log:(id)sender
{
    NSLog(@"\n_____________________\n");
    NSDictionary* assertions = nil;
    IOPMCopyAssertionsByProcess((CFDictionaryRef*)&assertions);
    NSLog(@"%@", assertions);
}

In my case, the log shows that when the Assertion is released, another powerd process is created, which blocks the sleep mode / screensaver for an extra 60 seconds. It looks like this bug this feature appeared in 10.8.2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question