mark PurpleImageClass as private
[pidgin-git.git] / pidgin / gtkidle.c
blob8838cf64cc6effa49a93c038038f76e72befd53e
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 #elif defined (_WIN32)
30 # include "win32/gtkwin32dep.h"
31 #endif
33 #include "idle.h"
35 #if !defined(HAVE_IOKIT) && !defined(_WIN32)
36 typedef struct {
37 gchar *bus_name;
38 gchar *object_path;
39 gchar *iface_name;
40 } PidginDBusScreenSaverInfo;
42 static const PidginDBusScreenSaverInfo screensavers[] = {
44 "org.freedesktop.ScreenSaver",
45 "/org/freedesktop/ScreenSaver",
46 "org.freedesktop.ScreenSaver"
47 }, {
48 "org.gnome.ScreenSaver",
49 "/org/gnome/ScreenSaver",
50 "org.gnome.ScreenSaver"
51 }, {
52 "org.kde.ScreenSaver",
53 "/org/kde/ScreenSaver",
54 "org.kde.ScreenSaver"
57 #endif /* !HAVE_IOKIT && !_WIN32 */
60 * pidgin_get_time_idle:
62 * Get the number of seconds the user has been idle. In Unix-world
63 * this is based on the DBus ScreenSaver interfaces. In MS Windows this
64 * is based on keyboard/mouse usage information obtained from the OS.
65 * In MacOS X, this is based on keyboard/mouse usage information
66 * obtained from the OS, if configure detected IOKit. Otherwise,
67 * MacOS X is handled as a case of Unix.
69 * Returns: The number of seconds the user has been idle.
71 static time_t
72 pidgin_get_time_idle(void)
74 # ifdef HAVE_IOKIT
75 /* Query the IOKit API */
77 static io_service_t macIOsrvc = NULL;
78 CFTypeRef property;
79 uint64_t idle_time = 0; /* nanoseconds */
81 if (macIOsrvc == NULL)
83 mach_port_t master;
84 IOMasterPort(MACH_PORT_NULL, &master);
85 macIOsrvc = IOServiceGetMatchingService(master,
86 IOServiceMatching("IOHIDSystem"));
89 property = IORegistryEntryCreateCFProperty(macIOsrvc, CFSTR("HIDIdleTime"),
90 kCFAllocatorDefault, 0);
91 CFNumberGetValue((CFNumberRef)property,
92 kCFNumberSInt64Type, &idle_time);
93 CFRelease(property);
95 /* convert nanoseconds to seconds */
96 return idle_time / 1000000000;
97 # else
98 # ifdef _WIN32
99 /* Query Windows */
100 return (GetTickCount() - winpidgin_get_lastactive()) / 1000;
101 # else
102 static guint idx = 0;
103 GApplication *app;
104 GDBusConnection *conn;
105 GVariant *reply = NULL;
106 guint32 active_time = 0;
107 GError *error = NULL;
109 app = g_application_get_default();
111 if (app == NULL) {
112 purple_debug_error("gtkidle",
113 "Unable to retrieve GApplication");
114 return 0;
117 conn = g_application_get_dbus_connection(app);
119 if (conn == NULL) {
120 purple_debug_misc("gtkidle",
121 "GApplication lacking DBus connection. "
122 "Skip checking ScreenSaver interface");
123 return 0;
126 for (; idx < G_N_ELEMENTS(screensavers); ++idx) {
127 const PidginDBusScreenSaverInfo *info = &screensavers[idx];
129 reply = g_dbus_connection_call_sync(conn,
130 info->bus_name, info->object_path,
131 info->iface_name, "GetActiveTime",
132 NULL, G_VARIANT_TYPE("(u)"),
133 G_DBUS_CALL_FLAGS_NO_AUTO_START, 1000,
134 NULL, &error);
136 if (reply != NULL) {
137 break;
140 if (g_error_matches(error, G_DBUS_ERROR,
141 G_DBUS_ERROR_NOT_SUPPORTED)) {
142 purple_debug_info("gtkidle",
143 "Querying idle time on '%s' "
144 "unsupported. Trying the next one",
145 info->bus_name);
146 } else if (g_error_matches(error, G_DBUS_ERROR,
147 G_DBUS_ERROR_NAME_HAS_NO_OWNER)) {
148 purple_debug_info("gtkidle",
149 "Querying idle time on '%s' "
150 "not found. Trying the next one",
151 info->bus_name);
152 } else {
153 purple_debug_error("gtkidle",
154 "Querying idle time on '%s' "
155 "error: %s", info->bus_name,
156 error->message);
159 g_clear_error(&error);
162 if (reply == NULL) {
163 purple_debug_warning("gtkidle",
164 "Failed to query ScreenSaver active time: "
165 "No working ScreenSaver interfaces");
166 return 0;
169 g_variant_get(reply, "(u)", &active_time);
170 g_variant_unref(reply);
172 return active_time;
173 # endif /* !_WIN32 */
174 # endif /* !HAVE_IOKIT */
177 static PurpleIdleUiOps ui_ops =
179 pidgin_get_time_idle,
180 NULL,
181 NULL,
182 NULL,
183 NULL
186 PurpleIdleUiOps *
187 pidgin_idle_get_ui_ops()
189 return &ui_ops;