I want to know the idle time of Mac. Currently I use this code
CFMutableDictionaryRef properties = 0;
CFTypeRef obj;
mach_port_t masterPort;
io_iterator_t iter;
io_registry_entry_t curObj;
IOMasterPort(MACH_PORT_NULL, &masterPort);
/* Get IOHIDSystem */
IOServiceGetMatchingServices(masterPort, IOServiceMatching("IOHIDSystem"), &iter);
if (iter == 0)
{
return -1;
}
else
{
curObj = IOIteratorNext(iter);
}
if (IORegistryEntryCreateCFProperties(curObj, &properties, kCFAllocatorDefault, 0) == KERN_SUCCESS && properties != NULL)
{
obj = CFDictionaryGetValue(properties, CFSTR("HIDIdleTime"));
CFRetain(obj);
}
else
{
return -1;
}
uint64_t tHandle = 0;
if (obj)
{
CFTypeID type = CFGetTypeID(obj);
if (type == CFDataGetTypeID())
{
CFDataGetBytes((CFDataRef) obj, CFRangeMake(0, sizeof(tHandle)), (UInt8*) &tHandle);
}
else if (type == CFNumberGetTypeID())
{
CFNumberGetValue((CFNumberRef)obj, kCFNumberSInt64Type, &tHandle);
}
else
{
// error
tHandle = 0;
}
CFRelease(obj);
tHandle /= 1000000; // return as milliseconds
}
else
{
tHandle = -1;
}
CFRelease((CFTypeRef)properties);
IOObjectRelease(curObj);
IOObjectRelease(iter);
return (double)tHandle;
However, I want the idle time to keep 0 if any video is running.
Is there any code sample or library to check? (include video running on iTunes, VLC, youtube on browser or any other video applications)
I can interpret your question several ways:
How to determine when the screensaver kicks in?
Check the following script (original answer):
In case the screensaver is suppressed, for example by VLC, the result is:
Otherwise it returns the time until the screensaver kicks in, for example:
Run the script above through
NSTaskorNSAppleScriptand parse the result.Please note “sleep” may also rever to the screensaver, depending on which one kicks in first.
How to determine wether the screensaver is suppressed?
The following line does the trick:
In case the screensaver is suppressed it returns
0.Run the line above through
NSTaskorNSAppleScriptand parse the result.How to suppress the screensaver?
The official Apple documentation (including code snippet):
How can I prevent system sleep while my application is running?
EDIT (Response to comment)
I’m not familiar with such a method. I actually doubt it exists.
Quick
NSAppleScriptexample:Just don’t be reticent about using
NSTaskorNSAppleScript🙂