Answer the question
In order to leave comments, you need to log in
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 IOPMAssertionCreateWithName
and IOPMAssertionRelease
from IOKit
googled 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;
}
}
Answer the question
In order to leave comments, you need to log in
You can add a button with the following call:
- (IBAction)log:(id)sender
{
NSLog(@"\n_____________________\n");
NSDictionary* assertions = nil;
IOPMCopyAssertionsByProcess((CFDictionaryRef*)&assertions);
NSLog(@"%@", assertions);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question