Nidhoegger
Nidhoegger

Reputation: 5232

Get the contents of GVariant

I currently try to communicate with dbus and have a function, that will return array of struct(string, uint32, string, string, object path). I have the result stored in a GVariant and printing this GVariant shows that there are correct result values in there.

More descriptive: I try to get the result of Systemd's Logind Managers ListSessions.

The output of print is:

[('2', uint32 1000, 'nidhoegger', 'seat0', objectpath
'/org/freedesktop/login1/session/_32'), ('6', 1001, 'test', 'seat0',
'/org/freedesktop/login1/session/_36'), ('c2', 111, 'lightdm',
'seat0', '/org/freedesktop/login1/session/c2')]

What I am trying right now is getting each array element in a loop using:

for (uint32_t i = 0; i < ::g_variant_n_children(v); ++i)
{
    GVariant *child = ::g_variant_get_child_value(v, i);
}

When printing the child I get:

<('2', uint32 1000, 'nidhoegger', 'seat0', objectpath '/org/freedesktop/login1/session/_32')>

So far so good. Now I am trying to get the single items using g_variant_get this way:

gchar *id = NULL;
uint32_t uid = 0;
gchar *user = NULL;
gchar *seat = NULL;
gchar *session_path = NULL;

::g_variant_get(v, "(susso)", &id, &uid, &user, &seat, &session_path);

But it only gives me this assertion:

(process:12712): GLib-CRITICAL **: the GVariant format string '(susso)' has a type of '(susso)' but the given value has a type of 'v'

(process:12712): GLib-CRITICAL **: g_variant_get_va: assertion 'valid_format_string (format_string, !endptr, value)' failed

If this is relevant: I generated the code to communicate with gdbus-codegen and the function to obtain the value has this signature:

gboolean login1_manager_call_list_sessions_sync (
    Login1Manager *proxy,
    GVariant **out_unnamed_arg0,
    GCancellable *cancellable,
    GError **error);

What am I doing wrong? Why is it returning "v" as value?

Upvotes: 2

Views: 4440

Answers (1)

Philip Withnall
Philip Withnall

Reputation: 5713

::g_variant_get(v, "(susso)", &id, &uid, &user, &seat, &session_path);

That looks suspect. You should be calling it on child, not v.

The following C code works fine for me:

/* gcc `pkg-config --cflags --libs glib-2.0` -o test test.c */
#include <glib.h>

int
main (void)
{
  g_autoptr(GVariant) sessions = NULL;

  sessions = g_variant_new_parsed ("[('2', uint32 1000, 'nidhoegger', 'seat0', objectpath '/org/freedesktop/login1/session/_32'), ('6', 1001, 'test', 'seat0', '/org/freedesktop/login1/session/_36'), ('c2', 111, 'lightdm', 'seat0', '/org/freedesktop/login1/session/c2')]");

  for (gsize i = 0; i < g_variant_n_children (sessions); i++)
    {
      g_autoptr(GVariant) child = g_variant_get_child_value (sessions, i);
      g_message ("Child %" G_GSIZE_FORMAT ": %s", i, g_variant_get_type_string (child));

      guint32 uid;
      const gchar *id, *user, *seat, *session_path;

      g_variant_get (child, "(&su&s&s&o)", &id, &uid, &user, &seat, &session_path);

      g_message ("%s, %u, %s, %s, %s", id, uid, user, seat, session_path);
    }

  return 0;
}

It prints the following:

** Message: Child 0: (susso)
** Message: 2, 1000, nidhoegger, seat0, /org/freedesktop/login1/session/_32
** Message: Child 1: (susso)
** Message: 6, 1001, test, seat0, /org/freedesktop/login1/session/_36
** Message: Child 2: (susso)
** Message: c2, 111, lightdm, seat0, /org/freedesktop/login1/session/c2

Upvotes: 2

Related Questions