For anybody who cares, the following predates CGSecondsSinceLastInputEvent,
and uses only public APIs. It's probably just personal preference as to
which is less vile ;-)

- (float) getSystemIdleSeconds
{
    static mach_port_t masterPort = 0;
    static io_iterator_t iter;
    static io_registry_entry_t curio;

    if( masterPort == 0 )
    {
        if( IOMasterPort( MACH_PORT_NULL, &masterPort ) == KERN_SUCCESS &&
                IOServiceGetMatchingServices( masterPort, IOServiceMatching(
"IOHIDSystem" ), &iter ) == KERN_SUCCESS )
        {
            curio = IOIteratorNext( iter );
            IOObjectRelease( iter );
        }
    }

    CFTypeRef obj = 0;
    if( curio )
        obj = IORegistryEntryCreateCFProperty( curio, CFSTR( "HIDIdleTime"
), kCFAllocatorDefault, 0 );

    float val = 0;
    if( obj ) 
    {
        uint64_t nano = 0;
        CFTypeID type = CFGetTypeID( obj );

        if( type == CFDataGetTypeID() )
            CFDataGetBytes( (CFDataRef) obj, CFRangeMake( 0, sizeof( nano
)), (UInt8*) &nano );
        else if( type == CFNumberGetTypeID() )
            CFNumberGetValue( (CFNumberRef) obj, kCFNumberSInt64Type, &nano
);

        CFRelease( obj );

        val = nano / 1000000000.0;
    }

    return val;
}


-- 
Scott Ribe
scott_r...@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to