diff options
Diffstat (limited to 'SwifTools/Idle/MacOSXIdleQuerier.cpp')
-rw-r--r-- | SwifTools/Idle/MacOSXIdleQuerier.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/SwifTools/Idle/MacOSXIdleQuerier.cpp b/SwifTools/Idle/MacOSXIdleQuerier.cpp index 8eaece6..89fa050 100644 --- a/SwifTools/Idle/MacOSXIdleQuerier.cpp +++ b/SwifTools/Idle/MacOSXIdleQuerier.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <SwifTools/Idle/MacOSXIdleQuerier.h> @@ -10,26 +10,33 @@ #include <cassert> #include <iostream> +#include <limits> + #include <boost/numeric/conversion/cast.hpp> #include <CoreFoundation/CoreFoundation.h> namespace Swift { MacOSXIdleQuerier::MacOSXIdleQuerier() : ioService(0) { - mach_port_t masterPort; - IOMasterPort(MACH_PORT_NULL, &masterPort); - ioService = IOServiceGetMatchingService(masterPort, IOServiceMatching("IOHIDSystem")); - assert(ioService); + 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); - (void) result; - CFRelease(property); - return boost::numeric_cast<int>(idle / 1000000000); + CFTypeRef property = IORegistryEntryCreateCFProperty(ioService, CFSTR("HIDIdleTime"), kCFAllocatorDefault, 0); + uint64_t idle = 0; + bool result = CFNumberGetValue((CFNumberRef)property, kCFNumberSInt64Type, &idle); + assert(result); + (void) result; + CFRelease(property); + try { + return boost::numeric_cast<int>(idle / 1000000000); + } + catch (const boost::numeric::bad_numeric_cast&) { + return std::numeric_limits<int>::max(); + } } } |