diff src/gtkidle.c @ 13323:723f5b7ef6a2

[gaim-migrate @ 15693] Long long ago, in a far-off land, a patch was sent to gaim-devel... I modified this patch, but couldn't test it, so it sat. Today, I got to borrow an evaluation laptop from Apple (here at the University of Minnesota, Crookston). So, I bring you... A patch from Michael Culbertson: On Mac OS X, obtain the system idle time via IOKit. Thus, our idle time calculations will reflect system usage rather than X11 application usage. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Fri, 24 Feb 2006 03:38:09 +0000
parents bc249de5ea02
children
line wrap: on
line diff
--- a/src/gtkidle.c	Fri Feb 24 02:16:25 2006 +0000
+++ b/src/gtkidle.c	Fri Feb 24 03:38:09 2006 +0000
@@ -23,23 +23,32 @@
 #include "internal.h"
 #include "gtkidle.h"
 
-#ifdef USE_SCREENSAVER
-# ifndef _WIN32
-#  include <X11/Xlib.h>
-#  include <X11/Xutil.h>
-#  include <X11/extensions/scrnsaver.h>
-#  include <gdk/gdkx.h>
-# else
-#  include "idletrack.h"
-# endif
-#endif /* USE_SCREENSAVER */
+#ifdef HAVE_IOKIT
+# include <CoreFoundation/CoreFoundation.h>
+# include <IOKit/IOKitLib.h>
+#else
+# ifdef USE_SCREENSAVER
+#  ifdef _WIN32
+#   include "idletrack.h"
+#  else
+    /* We're on X11 and not MacOS X with IOKit. */
+#   include <X11/Xlib.h>
+#   include <X11/Xutil.h>
+#   include <X11/extensions/scrnsaver.h>
+#   include <gdk/gdkx.h>
+#  endif /* !_WIN32 */
+# endif /* USE_SCREENSAVER */
+#endif /* !HAVE_IOKIT */
 
 #include "idle.h"
 
 /**
  * Get the number of seconds the user has been idle.  In Unix-world
- * this is based on the X Windows usage.  In MS Windows this is based
- * on keyboard/mouse usage.
+ * this is based on the X Windows usage.  In MS Windows this is
+ * based on keyboard/mouse usage information obtained from the OS.
+ * In MacOS X, this is based on keyboard/mouse usage information
+ * obtained from the OS, if configure detected IOKit.  Otherwise,
+ * MacOS X is handled as a case of X Windows.
  *
  * In Debian bug #271639, jwz says:
  *
@@ -58,11 +67,40 @@
  *
  * @return The number of seconds the user has been idle.
  */
-#ifdef USE_SCREENSAVER
+#if defined(USE_SCREENSAVER) || defined(HAVE_IOKIT)
 static time_t
 gaim_gtk_get_time_idle()
 {
-# ifndef _WIN32
+# 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;
+# else
+#  ifdef _WIN32
+	/* Query Windows */
+	return (GetTickCount() - wgaim_get_lastactive()) / 1000;
+#  else
+	/* We're on X11 and not MacOS X with IOKit. */
+
 	/* Query xscreensaver */
 	static XScreenSaverInfo *mit_info = NULL;
 	int event_base, error_base;
@@ -74,20 +112,18 @@
 		return (mit_info->idle) / 1000;
 	} else
 		return 0;
-# else
-	/* Query windows */
-	return (GetTickCount() - wgaim_get_lastactive()) / 1000;
-# endif /* _WIN32 */
+#  endif /* !_WIN32 */
+# endif /* !HAVE_IOKIT */
 }
-#endif /* USE_SCREENSAVER */
+#endif /* USE_SCREENSAVER || HAVE_IOKIT */
 
 static GaimIdleUiOps ui_ops =
 {
-#ifdef USE_SCREENSAVER
+#if defined(USE_SCREENSAVER) || defined(HAVE_IOKIT)
 	gaim_gtk_get_time_idle
 #else
 	NULL
-#endif /* USE_SCREENSAVER */
+#endif /* USE_SCREENSAVER || HAVE_IOKIT */
 };
 
 GaimIdleUiOps *