diff options
Diffstat (limited to 'SwifTools/Idle/MacOSXIdleQuerier.cpp')
-rw-r--r-- | SwifTools/Idle/MacOSXIdleQuerier.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/SwifTools/Idle/MacOSXIdleQuerier.cpp b/SwifTools/Idle/MacOSXIdleQuerier.cpp new file mode 100644 index 0000000..ac6dff9 --- /dev/null +++ b/SwifTools/Idle/MacOSXIdleQuerier.cpp @@ -0,0 +1,25 @@ +#include "SwifTools/Idle/MacOSXIdleQuerier.h" + +#include <cassert> +#include <iostream> +#include <CoreFoundation/CoreFoundation.h> + +namespace Swift { + +MacOSXIdleQuerier::MacOSXIdleQuerier() : ioService(NULL) { + mach_port_t masterPort; + IOMasterPort(MACH_PORT_NULL, &masterPort); + ioService = IOServiceGetMatchingService(masterPort, IOServiceMatching("IOHIDSystem")); + assert(ioService); +} + +int MacOSXIdleQuerier::getIdleTimeSeconds() { + CFTypeRef property = IORegistryEntryCreateCFProperty(ioService, CFSTR("HIDIdleTime"), kCFAllocatorDefault, 0); + uint64_t idle = 0; + bool result = CFNumberGetValue((CFNumberRef)property, kCFNumberSInt64Type, &idle); + assert(result); + CFRelease(property); + return idle / 1000000000; +} + +} |