Move migration to core, check for errors
[pidgin-git.git] / pidgin / gtkidle.c
blob3fd005483340af2b5d78bd44a909c921fea7bdee
1 /*
2 * pidgin
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
24 #include "gtkidle.h"
26 #ifdef HAVE_IOKIT
27 # include <CoreFoundation/CoreFoundation.h>
28 # include <IOKit/IOKitLib.h>
29 #else
30 # ifdef USE_SCREENSAVER
31 # ifdef _WIN32
32 # include "gtkwin32dep.h"
33 # else
34 /* We're on X11 and not MacOS X with IOKit. */
35 # include <X11/Xlib.h>
36 # include <X11/Xutil.h>
37 # include <X11/extensions/scrnsaver.h>
38 # include <gdk/gdkx.h>
39 # endif /* !_WIN32 */
40 # endif /* USE_SCREENSAVER */
41 #endif /* !HAVE_IOKIT */
43 #include "idle.h"
46 * pidgin_get_time_idle:
48 * Get the number of seconds the user has been idle. In Unix-world
49 * this is based on the X Windows usage. In MS Windows this is
50 * based on keyboard/mouse usage information obtained from the OS.
51 * In MacOS X, this is based on keyboard/mouse usage information
52 * obtained from the OS, if configure detected IOKit. Otherwise,
53 * MacOS X is handled as a case of X Windows.
55 * In Debian bug #271639, jwz says:
57 * Purple should simply ask xscreensaver how long the user has been idle:
58 * % xscreensaver-command -time
59 * XScreenSaver 4.18: screen blanked since Tue Sep 14 14:10:45 2004
61 * Or you can monitor the _SCREENSAVER_STATUS property on root window #0.
62 * Element 0 is the status (0, BLANK, LOCK), element 1 is the time_t since
63 * the last state change, and subsequent elements are which hack is running
64 * on the various screens:
65 * % xprop -f _SCREENSAVER_STATUS 32ac -root _SCREENSAVER_STATUS
66 * _SCREENSAVER_STATUS(INTEGER) = BLANK, 1095196626, 10, 237
68 * See watch() in xscreensaver/driver/xscreensaver-command.c.
70 * Returns: The number of seconds the user has been idle.
72 #if defined(USE_SCREENSAVER) || defined(HAVE_IOKIT)
73 static time_t
74 pidgin_get_time_idle(void)
76 # ifdef HAVE_IOKIT
77 /* Query the IOKit API */
79 static io_service_t macIOsrvc = NULL;
80 CFTypeRef property;
81 uint64_t idle_time = 0; /* nanoseconds */
83 if (macIOsrvc == NULL)
85 mach_port_t master;
86 IOMasterPort(MACH_PORT_NULL, &master);
87 macIOsrvc = IOServiceGetMatchingService(master,
88 IOServiceMatching("IOHIDSystem"));
91 property = IORegistryEntryCreateCFProperty(macIOsrvc, CFSTR("HIDIdleTime"),
92 kCFAllocatorDefault, 0);
93 CFNumberGetValue((CFNumberRef)property,
94 kCFNumberSInt64Type, &idle_time);
95 CFRelease(property);
97 /* convert nanoseconds to seconds */
98 return idle_time / 1000000000;
99 # else
100 # ifdef _WIN32
101 /* Query Windows */
102 return (GetTickCount() - winpidgin_get_lastactive()) / 1000;
103 # else
104 /* We're on X11 and not MacOS X with IOKit. */
106 /* Query xscreensaver */
107 static XScreenSaverInfo *mit_info = NULL;
108 static int has_extension = -1;
109 int event_base, error_base;
111 if (has_extension == -1)
112 has_extension = XScreenSaverQueryExtension(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
113 &event_base, &error_base);
115 if (has_extension)
117 if (mit_info == NULL)
118 mit_info = XScreenSaverAllocInfo();
120 XScreenSaverQueryInfo(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
121 GDK_ROOT_WINDOW(), mit_info);
122 return (mit_info->idle) / 1000;
124 else
125 return 0;
126 # endif /* !_WIN32 */
127 # endif /* !HAVE_IOKIT */
129 #endif /* USE_SCREENSAVER || HAVE_IOKIT */
131 static PurpleIdleUiOps ui_ops =
133 #if defined(USE_SCREENSAVER) || defined(HAVE_IOKIT)
134 pidgin_get_time_idle,
135 #else
136 NULL,
137 #endif /* USE_SCREENSAVER || HAVE_IOKIT */
138 NULL,
139 NULL,
140 NULL,
141 NULL
144 PurpleIdleUiOps *
145 pidgin_idle_get_ui_ops()
147 return &ui_ops;