widget/cocoa/nsIdleServiceX.mm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/cocoa/nsIdleServiceX.mm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsIdleServiceX.h"
     1.9 +#include "nsObjCExceptions.h"
    1.10 +#include "nsIServiceManager.h"
    1.11 +#import <Foundation/Foundation.h>
    1.12 +
    1.13 +NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceX, nsIdleService)
    1.14 +
    1.15 +bool
    1.16 +nsIdleServiceX::PollIdleTime(uint32_t *aIdleTime)
    1.17 +{
    1.18 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
    1.19 +
    1.20 +  kern_return_t rval;
    1.21 +  mach_port_t masterPort;
    1.22 +
    1.23 +  rval = IOMasterPort(kIOMasterPortDefault, &masterPort);
    1.24 +  if (rval != KERN_SUCCESS)
    1.25 +    return false;
    1.26 +
    1.27 +  io_iterator_t hidItr;
    1.28 +  rval = IOServiceGetMatchingServices(masterPort,
    1.29 +                                      IOServiceMatching("IOHIDSystem"),
    1.30 +                                      &hidItr);
    1.31 +
    1.32 +  if (rval != KERN_SUCCESS)
    1.33 +    return false;
    1.34 +  NS_ASSERTION(hidItr, "Our iterator is null, but it ought not to be!");
    1.35 +
    1.36 +  io_registry_entry_t entry = IOIteratorNext(hidItr);
    1.37 +  NS_ASSERTION(entry, "Our IO Registry Entry is null, but it shouldn't be!");
    1.38 +
    1.39 +  IOObjectRelease(hidItr);
    1.40 +
    1.41 +  NSMutableDictionary *hidProps;
    1.42 +  rval = IORegistryEntryCreateCFProperties(entry,
    1.43 +                                           (CFMutableDictionaryRef*)&hidProps,
    1.44 +                                           kCFAllocatorDefault, 0);
    1.45 +  if (rval != KERN_SUCCESS)
    1.46 +    return false;
    1.47 +  NS_ASSERTION(hidProps, "HIDProperties is null, but no error was returned.");
    1.48 +  [hidProps autorelease];
    1.49 +
    1.50 +  id idleObj = [hidProps objectForKey:@"HIDIdleTime"];
    1.51 +  NS_ASSERTION([idleObj isKindOfClass: [NSData class]] ||
    1.52 +               [idleObj isKindOfClass: [NSNumber class]],
    1.53 +               "What we got for the idle object is not what we expect!");
    1.54 +
    1.55 +  uint64_t time;
    1.56 +  if ([idleObj isKindOfClass: [NSData class]])
    1.57 +    [idleObj getBytes: &time];
    1.58 +  else
    1.59 +    time = [idleObj unsignedLongLongValue];
    1.60 +
    1.61 +  IOObjectRelease(entry);
    1.62 +
    1.63 +  // convert to ms from ns
    1.64 +  time /= 1000000;
    1.65 +  if (time > UINT32_MAX) // Overflow will occur
    1.66 +    return false;
    1.67 +
    1.68 +  *aIdleTime = static_cast<uint32_t>(time);
    1.69 +
    1.70 +  return true;
    1.71 +
    1.72 +  NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
    1.73 +}
    1.74 +
    1.75 +bool
    1.76 +nsIdleServiceX::UsePollMode()
    1.77 +{
    1.78 +  return true;
    1.79 +}
    1.80 +

mercurial