The following piece of code, prints idle time of a user running GNOME. Now I included a very similar code into a patch for BOINC client, to let it detect user idle time. I am afraid it will not work because:
- BOINC client runs as systemd service using an unprivileged user and it will have to detect user idle time of users that use GNOME. I haven’t tested it yet on BOINC client running as service because I am getting a crash that does not show debuginfos / symbols;
- the IdleMonitor DBus interface runs on session bus, not system bus.
What do you think about? Thank you
/*
* Compile with:
* gcc -Wall print_user_idle_time-gnome.c -o print_user_idle_time-gnome `pkg-config --libs gio-2.0 --cflags`
*/
#include <gio/gio.h>
static void
print_user_idle_time (GDBusProxy *proxy)
{
guint64 user_idle_time;
gchar *method = "GetIdletime";
GError *error = NULL;
GVariant *ret = NULL;
ret = g_dbus_proxy_call_sync(proxy,
method,
NULL,
G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (!ret) {
g_dbus_error_strip_remote_error (error);
g_print ("GetIdletime failed: %s\n", error->message);
g_error_free (error);
return;
}
g_variant_get(ret, "(t)", &user_idle_time);
/*
* In another example
* g_variant_get (ret, "(^ao)", &user_idle_time);
* has been replaced with
* user_idle_time = g_variant_get_uint64 (ret);
*/
g_print("%lu\n", user_idle_time);
g_variant_unref (ret);
}
int
main (int argc, char *argv[])
{
/*
* this should be the equivalent of bash command
* $ sleep 1 && dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime
*/
GDBusProxy *proxy = NULL;
gchar *name = "org.gnome.Mutter.IdleMonitor";
gchar *object_path = "/org/gnome/Mutter/IdleMonitor/Core";
gchar *interface_name = "org.gnome.Mutter.IdleMonitor";
/* Create a D-Bus proxy */
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
name,
object_path,
interface_name,
NULL, NULL);
g_assert (proxy != NULL);
print_user_idle_time (proxy);
g_object_unref (proxy);
return 0;
}
aaa