• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: System Idle Time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: System Idle Time


  • Subject: Re: System Idle Time
  • From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
  • Date: Tue, 26 Oct 2004 13:23:09 -0500

Mike Lewis, email@hidden, wrote:
>How can I get the system idle time in Cocoa? I want to know how long the
>user has been away from computer. I tried using a timer called
>EventLoopIdleTimer in Carbon, which works fine, but it only gives me the
>idle time of my application. I also read something about HIDIdleTime in the
>archives, but couldn't find any source code examples on how to use it.

I wrote the following a little while back. You'll have to massage it a
little bit since it uses my JRFoundation, which is currently nonpublic.
Try compiling it, and fix up the errors, and you should be good to go.

---- HIDIdleTime.h ----
#import <Foundation/Foundation.h>
@interface HIDIdleTime : NSObject {
io_registry_entry_t _hidEntry;
}
- (uint64_t)idleTime;
- (unsigned)idleTimeInSeconds;
@end

---- HIDIdleTime ----
#import "HIDIdleTime.h"
#import <JRFoundation/JRFoundation.h>

@implementation HIDIdleTime

- (id)init {
self = [super init];
if( self ) {
mach_port_t masterPort;
kern_return_t err = IOMasterPort( MACH_PORT_NULL, &masterPort );
[NSException raiseKernReturn:err];

io_iterator_t hidIter;
err = IOServiceGetMatchingServices( masterPort,
IOServiceMatching("IOHIDSystem"), &hidIter );
[NSException raiseKernReturn:err];
NSAssert0( hidIter != NULL );

_hidEntry = IOIteratorNext( hidIter );
NSAssert0( _hidEntry != NULL );
IOObjectRelease(hidIter);
}
return self;
}

- (void)dealloc {
if( _hidEntry ) {
kern_return_t err = IOObjectRelease( _hidEntry );
[NSException raiseKernReturn:err];
_hidEntry = NULL;
}
[super dealloc];
}

- (uint64_t)idleTime {
NSMutableDictionary *hidProperties;
kern_return_t err = IORegistryEntryCreateCFProperties( _hidEntry,
(CFMutableDictionaryRef*) &hidProperties, kCFAllocatorDefault, 0 );
[NSException raiseKernReturn:err];
NSAssert0( hidProperties != nil );
[hidProperties autorelease];

id hidIdleTimeObj = [hidProperties objectForKey:@"HIDIdleTime"];
NSAssert0( [hidIdleTimeObj isKindOfClass:[NSData class]] ||
[hidIdleTimeObj isKindOfClass:[NSNumber class]] );
uint64_t result;
if( [hidIdleTimeObj isKindOfClass:[NSData class]] ) {
NSAssert0( [(NSData*)hidIdleTimeObj length] == sizeof( result ) );
[hidIdleTimeObj getBytes:&result];
} else {
ASSERT_CAST( result, long long );
result = [hidIdleTimeObj longLongValue];
}

return result;
}

- (unsigned)idleTimeInSeconds {
#define NS_SECONDS 1000000000 // 10^9 -- number of ns in a second
return [self idleTime] / NS_SECONDS;
}

@end

| Jonathan 'Wolf' Rentzsch http://rentzsch.com
| Red Shed Software http://redshed.net
| "better" necessarily means "different"

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: [short] Categories vs. subclassing
  • Next by Date: ORM
  • Previous by thread: Re: System Idle Time
  • Next by thread: Re: System Idle Time
  • Index(es):
    • Date
    • Thread