该名称在g_dbus_proxy_call_sync上不可激活

时间:2019-05-27 16:56:46

标签: c glib gnome dbus gdbus

在GNOME Xorg会话上,要获取DBus公开的方法GetIdletime的返回值,您可以使用

$ dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime

$ gdbus call --session --dest org.gnome.Mutter.IdleMonitor --object-path /org/gnome/Mutter/IdleMonitor/Core --method org.gnome.Mutter.IdleMonitor.GetIdletime

我需要使用GDBus API检索此值,所以我编写了以下代码

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

但是当我运行它时,出现错误GetIdletime failed: The name is not activable。怎么了?谢谢

1 个答案:

答案 0 :(得分:1)

org.gnome.Mutter.IdleMonitor在会话总线上,而不在系统总线上;因此您需要使用G_BUS_TYPE_SESSION