Move to a newer method for getting the idle time on macos

Sun, 13 Oct 2019 05:14:08 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Sun, 13 Oct 2019 05:14:08 -0500
changeset 40038
e2967758a0c4
parent 40037
8e1ed016f5a4
child 40039
dac8511b1992

Move to a newer method for getting the idle time on macos

pidgin/gtkidle.c file | annotate | diff | comparison | revisions
--- a/pidgin/gtkidle.c	Sun Oct 13 03:52:05 2019 -0500
+++ b/pidgin/gtkidle.c	Sun Oct 13 05:14:08 2019 -0500
@@ -24,6 +24,9 @@
 #include "gtkidle.h"
 
 #ifdef HAVE_IOKIT
+# ifndef HAVE_UNISTD_H
+#  define HAVE_UNISTD_H
+# endif
 # include <CoreFoundation/CoreFoundation.h>
 # include <IOKit/IOKitLib.h>
 #elif defined (_WIN32)
@@ -73,27 +76,29 @@
 {
 # ifdef HAVE_IOKIT
 	/* Query the IOKit API */
-
-	static io_service_t macIOsrvc = NULL;
-	CFTypeRef property;
-	uint64_t idle_time = 0; /* nanoseconds */
-
-	if (macIOsrvc == NULL)
-	{
-		mach_port_t master;
-		IOMasterPort(MACH_PORT_NULL, &master);
-		macIOsrvc = IOServiceGetMatchingService(master,
-		                                        IOServiceMatching("IOHIDSystem"));
-	}
-
-	property = IORegistryEntryCreateCFProperty(macIOsrvc, CFSTR("HIDIdleTime"),
-	                                           kCFAllocatorDefault, 0);
-	CFNumberGetValue((CFNumberRef)property,
-	                 kCFNumberSInt64Type, &idle_time);
-	CFRelease(property);
-
-	/* convert nanoseconds to seconds */
-	return idle_time / 1000000000;
+    double idleSeconds = -1;
+    io_iterator_t iter = 0;
+    if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
+        io_registry_entry_t entry = IOIteratorNext(iter);
+        if (entry) {
+            CFMutableDictionaryRef dict = NULL;
+            kern_return_t status;
+            status = IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0);
+            if (status == KERN_SUCCESS) {
+                CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
+                if (obj) {
+                    int64_t nanoseconds = 0;
+                    if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
+                        idleSeconds = (double) nanoseconds / NSEC_PER_SEC;
+                    }
+                }
+                CFRelease(dict);
+            }
+            IOObjectRelease(entry);
+        }
+        IOObjectRelease(iter);
+    }
+    return idleSeconds;
 # else
 #  ifdef _WIN32
 	/* Query Windows */

mercurial