g_variant_get_uint64: assertion 'g_variant_is_of_type (value, G_VARIANT_TYPE_UINT64)' failed

On a GNOME Xorg session, to get the return value of method GetIdletime exposed on DBus, I run the following code

/*
 * 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;
	}

	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[])
{
	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;
}

but I get error

(process:4497): GLib-CRITICAL **: 21:32:38.058: g_variant_get_uint64: assertion 'g_variant_is_of_type (value, G_VARIANT_TYPE_UINT64)' failed

What is the problem? Thank you

GDBus methods usually return a tuple with the list of out arguments, even if the number of arguments is 1, to avoid lots of special casing in the code. This means that org.gnome.Mutter.IdleMonitor.GetIdleTime may be annotated to return t but you will get a GVariant with a (t) type as the return value.

In order to get to the value, you will need to unroll it:

  // get the first child of the returned GVariant,
  // using the `t` (uint64) type
  g_variant_get_child (ret, 0, "t", &user_idle_time);
  // print the time using the platform-appropriate format string
  g_print ("%" G_GUINT64_FORMAT "\n", user_idle_time);

Alternatively, you can use g_variant_get():

g_variant_get (ret, "(t)", &user_idle_time);

which lets you unpack more values in one go.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.