Prevent computer sleep

Hey all,
In my app I was thinking about an option to prevent the users computer from falling to sleep while the app is running.

I found this piece of code in the old forums that works…

  Declare Function UpdateSystemActivity Lib "CoreServices" (activity As UInt8) As Int16
  Const UsrActivity = 1
  
  Call UpdateSystemActivity(1)

…but there should be a “newer” way of doing this using IOKit.
This is a snippet from apples developer site(https://developer.apple.com/library/mac/qa/qa1340/_index.html). Any ideas of how to implement such feature? :slight_smile:

Well, do you use the MBS Plugin?
I think we have a class for this: IOPMAssertionMBS

By the way: UpdateSystemActivity is still okay. It has been deprecated in 10.8, but it works still.

Thanx Christian! I guess I can use UpdateSystemActivity then.
I’ll get your plug-ins some day or another :slight_smile:

macoslib has some IOKit stuff although I’m not sure if it does what you need. See the IOKit module inside the main macoslib folder.

I saw that too but I don’t think it can do this though :confused:

Whatever you do… Don’t trying pouring coffee on the keyboard… Trust me it will NOT keep your computer awake :smiley:

Declares may look intimidating, but they’re not so bad with a couple of quick googles… this took 5 minutes.

–Make a new module (IOKitSleepControl) and add some constants
Const kIOPMAssertionLevelOn = 255

Const kIOPMAssertionTypeNoDisplaySleep = “NoDisplaySleepAssertion”

Const kIOPMAssertionTypeNoIdleSleep = “NoIdleSleepAssertion”

Const kIOPMAssertionTypePreventSystemSleep = “PreventSystemSleep”

Const kIOPMAssertionTypePreventUserIdleDisplaySleep = “PreventUserIdleDisplaySleep”

Const kIOPMAssertionTypePreventUserIdleSystemSleep = “PreventUserIdleSystemSleep”

–add a couple functions

Function noSleep(NoSleepType As String,reasonForNoSleep as String) As integer
declare function IOPMAssertionCreateWithName lib “IOKit” (NoSleeptype as CFStringRef, kIOPMAssertionLevelOn as integer, reasonForActivity as CFStringRef, byref assertionID as integer ) as integer

Return IOPMAssertionCreateWithName(NoSleepType,kIOPMAssertionLevelOn,reasonForNoSleep,AssertionID)
End Function

Function sleepOk() As Integer
declare Function IOPMAssertionRelease lib “IOKit” (AssertionID as integer) as Integer

Return IOPMAssertionRelease(AssertionID)
End Function

–add a property

Private AssertionID As Integer

–Call noSleep
dim res As Integer=IOKitSleepControl.noSleep(kIOPMAssertionTypeNoDisplaySleep,“Just because”)

–Allow Sleep
dim ok as Integer=IOKitSleepControl.sleepOk

seems to work for me OSX10.7.5 Both functions return 0 on success

Jim,
Thank you for that!
Followed your instructions and it works here too on 10.8.4!
And yeah, I need to read up on declares :wink:

Have a great night!

No worries. I would never use coffee!
I’d use Burn instead :wink:

Enable wake on lan. Send a wake up message to yourself.

Jim,
I’ve been using you code since 2013, and it still works OK in Mojave (10.14.5) and Xojo 2019r1.1.
Nevertheless I’d like to know if after so many years something should be changed/modified; or not.

Thanks again for your code.

Wow, time flies! It looks like the declares could use some updating. AssertionLevel and AssertionID are defined as uint32_t… Integer would be wrong on a 64bit build in this case (the old declares are not guaranteed to break, but not guaranteed not to break). IOReturn is defined as int (int32 for Xojo)

[code]Function noSleep(NoSleepType As String,reasonForNoSleep as String) As integer
declare function IOPMAssertionCreateWithName lib “IOKit” (NoSleeptype as CFStringRef, kIOPMAssertionLevelOn as uint32, reasonForActivity as CFStringRef, byref assertionID as uint32 ) as int32

Return IOPMAssertionCreateWithName(NoSleepType,kIOPMAssertionLevelOn,reasonForNoSleep,AssertionID)
End Function

[/code]

[code]Function sleepOk() As Integer
declare Function IOPMAssertionRelease lib “IOKit” (AssertionID as uint32) as int32

Return IOPMAssertionRelease(AssertionID)
End Function

[/code]
and

[code]Private AssertionID As uint32

[/code]

Thank you, Jim. Very much appreciated.