widget/cocoa/nsIdleServiceX.mm

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3b345b874c4d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "nsIdleServiceX.h"
6 #include "nsObjCExceptions.h"
7 #include "nsIServiceManager.h"
8 #import <Foundation/Foundation.h>
9
10 NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceX, nsIdleService)
11
12 bool
13 nsIdleServiceX::PollIdleTime(uint32_t *aIdleTime)
14 {
15 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
16
17 kern_return_t rval;
18 mach_port_t masterPort;
19
20 rval = IOMasterPort(kIOMasterPortDefault, &masterPort);
21 if (rval != KERN_SUCCESS)
22 return false;
23
24 io_iterator_t hidItr;
25 rval = IOServiceGetMatchingServices(masterPort,
26 IOServiceMatching("IOHIDSystem"),
27 &hidItr);
28
29 if (rval != KERN_SUCCESS)
30 return false;
31 NS_ASSERTION(hidItr, "Our iterator is null, but it ought not to be!");
32
33 io_registry_entry_t entry = IOIteratorNext(hidItr);
34 NS_ASSERTION(entry, "Our IO Registry Entry is null, but it shouldn't be!");
35
36 IOObjectRelease(hidItr);
37
38 NSMutableDictionary *hidProps;
39 rval = IORegistryEntryCreateCFProperties(entry,
40 (CFMutableDictionaryRef*)&hidProps,
41 kCFAllocatorDefault, 0);
42 if (rval != KERN_SUCCESS)
43 return false;
44 NS_ASSERTION(hidProps, "HIDProperties is null, but no error was returned.");
45 [hidProps autorelease];
46
47 id idleObj = [hidProps objectForKey:@"HIDIdleTime"];
48 NS_ASSERTION([idleObj isKindOfClass: [NSData class]] ||
49 [idleObj isKindOfClass: [NSNumber class]],
50 "What we got for the idle object is not what we expect!");
51
52 uint64_t time;
53 if ([idleObj isKindOfClass: [NSData class]])
54 [idleObj getBytes: &time];
55 else
56 time = [idleObj unsignedLongLongValue];
57
58 IOObjectRelease(entry);
59
60 // convert to ms from ns
61 time /= 1000000;
62 if (time > UINT32_MAX) // Overflow will occur
63 return false;
64
65 *aIdleTime = static_cast<uint32_t>(time);
66
67 return true;
68
69 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
70 }
71
72 bool
73 nsIdleServiceX::UsePollMode()
74 {
75 return true;
76 }
77

mercurial