Check if the display is displayed during sleep or not

I have an application utility that becomes useless when there is no user. Thus, in order to save resources, I would like to know when / and whether the display is displayed.

There's a dedicated article on waking / hibernation notifications from Apple, but it only covers computer sleep and does not display sleep.

Is there a way for the app to sleep when the display is in a dream?

thanks

+1
source share
3 answers

Since I couldn’t find the call that appeared on the screen to fall asleep (maybe the screensaver does this? Most likely, it will hit before the system falls into sleep mode), I would suggest determining the idle time manually and then compare it with sleep display settings. This article describes how to get downtime from IOKit, and you should be able to easily get your current sleep settings, for example. with "pmset -g | grep sleep".

Two minutes after posting above, I discovered an open source command line tool that will probably help you with this: SleepWatcher seems to be able to do what you requested.

+1
source

The DisplayWrangler service sends notifications when the display turns off:

// Doesn't include error checking - just a quick example io_service_t displayWrangler; IONotificationPortRef notificationPort; io_object_t notification; displayWrangler = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceNameMatching("IODisplayWrangler"); notificationPort = IONotificationPortCreate(kIOMasterPortDefault); IOServiceAddInterestNotification(notificationPort, displayWrangler, kIOGeneralInterest, displayPowerNotificationsCallback, NULL, &notification); CFRunLoopAddSource (CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode); IOObjectRelease (displayWrangler); 

Then the callback looks something like this:

 void displayPowerNotificationsCallback(void *refcon, io_service_t service, natural_t messageType, void *messageArgument) { switch (messageType) { case kIOMessageDeviceWillPowerOff : // This is called twice - once for display dim event, then once // for display power off break; case kIOMessageDeviceHasPoweredOn : // Display powering back on break; } } 
+8
source

This is the answer to a question asked a while ago, but I thought it would be useful to add my answer.

NSWorkspace has a couple of notifications when waking and sleeping are displayed: NSWorkspaceScreensDidSleepNotification and NSWorkspaceScreensDidWakeNotification

+5
source

All Articles