michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIdleServiceX.h" michael@0: #include "nsObjCExceptions.h" michael@0: #include "nsIServiceManager.h" michael@0: #import michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceX, nsIdleService) michael@0: michael@0: bool michael@0: nsIdleServiceX::PollIdleTime(uint32_t *aIdleTime) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; michael@0: michael@0: kern_return_t rval; michael@0: mach_port_t masterPort; michael@0: michael@0: rval = IOMasterPort(kIOMasterPortDefault, &masterPort); michael@0: if (rval != KERN_SUCCESS) michael@0: return false; michael@0: michael@0: io_iterator_t hidItr; michael@0: rval = IOServiceGetMatchingServices(masterPort, michael@0: IOServiceMatching("IOHIDSystem"), michael@0: &hidItr); michael@0: michael@0: if (rval != KERN_SUCCESS) michael@0: return false; michael@0: NS_ASSERTION(hidItr, "Our iterator is null, but it ought not to be!"); michael@0: michael@0: io_registry_entry_t entry = IOIteratorNext(hidItr); michael@0: NS_ASSERTION(entry, "Our IO Registry Entry is null, but it shouldn't be!"); michael@0: michael@0: IOObjectRelease(hidItr); michael@0: michael@0: NSMutableDictionary *hidProps; michael@0: rval = IORegistryEntryCreateCFProperties(entry, michael@0: (CFMutableDictionaryRef*)&hidProps, michael@0: kCFAllocatorDefault, 0); michael@0: if (rval != KERN_SUCCESS) michael@0: return false; michael@0: NS_ASSERTION(hidProps, "HIDProperties is null, but no error was returned."); michael@0: [hidProps autorelease]; michael@0: michael@0: id idleObj = [hidProps objectForKey:@"HIDIdleTime"]; michael@0: NS_ASSERTION([idleObj isKindOfClass: [NSData class]] || michael@0: [idleObj isKindOfClass: [NSNumber class]], michael@0: "What we got for the idle object is not what we expect!"); michael@0: michael@0: uint64_t time; michael@0: if ([idleObj isKindOfClass: [NSData class]]) michael@0: [idleObj getBytes: &time]; michael@0: else michael@0: time = [idleObj unsignedLongLongValue]; michael@0: michael@0: IOObjectRelease(entry); michael@0: michael@0: // convert to ms from ns michael@0: time /= 1000000; michael@0: if (time > UINT32_MAX) // Overflow will occur michael@0: return false; michael@0: michael@0: *aIdleTime = static_cast(time); michael@0: michael@0: return true; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false); michael@0: } michael@0: michael@0: bool michael@0: nsIdleServiceX::UsePollMode() michael@0: { michael@0: return true; michael@0: } michael@0: