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
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
27 # include <CoreFoundation/CoreFoundation.h>
28 # include <IOKit/IOKitLib.h>
29 #elif defined (_WIN32)
30 # include "win32/gtkwin32dep.h"
35 #if !defined(HAVE_IOKIT) && !defined(_WIN32)
40 } PidginDBusScreenSaverInfo
;
42 static const PidginDBusScreenSaverInfo screensavers
[] = {
44 "org.freedesktop.ScreenSaver",
45 "/org/freedesktop/ScreenSaver",
46 "org.freedesktop.ScreenSaver"
48 "org.gnome.ScreenSaver",
49 "/org/gnome/ScreenSaver",
50 "org.gnome.ScreenSaver"
52 "org.kde.ScreenSaver",
53 "/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.
72 pidgin_get_time_idle(void)
75 /* Query the IOKit API */
77 static io_service_t macIOsrvc
= NULL
;
79 uint64_t idle_time
= 0; /* nanoseconds */
81 if (macIOsrvc
== NULL
)
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
);
95 /* convert nanoseconds to seconds */
96 return idle_time
/ 1000000000;
100 return (GetTickCount() - winpidgin_get_lastactive()) / 1000;
102 static guint idx
= 0;
104 GDBusConnection
*conn
;
105 GVariant
*reply
= NULL
;
106 guint32 active_time
= 0;
107 GError
*error
= NULL
;
109 app
= g_application_get_default();
112 purple_debug_error("gtkidle",
113 "Unable to retrieve GApplication");
117 conn
= g_application_get_dbus_connection(app
);
120 purple_debug_misc("gtkidle",
121 "GApplication lacking DBus connection. "
122 "Skip checking ScreenSaver interface");
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,
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",
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",
153 purple_debug_error("gtkidle",
154 "Querying idle time on '%s' "
155 "error: %s", info
->bus_name
,
159 g_clear_error(&error
);
163 purple_debug_warning("gtkidle",
164 "Failed to query ScreenSaver active time: "
165 "No working ScreenSaver interfaces");
169 g_variant_get(reply
, "(u)", &active_time
);
170 g_variant_unref(reply
);
173 # endif /* !_WIN32 */
174 # endif /* !HAVE_IOKIT */
177 static PurpleIdleUiOps ui_ops
=
179 pidgin_get_time_idle
,
187 pidgin_idle_get_ui_ops()