该名称在 g_dbus_proxy_call_sync 上不可激活

The name is not activable on g_dbus_proxy_call_sync

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

$ 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。怎么了?谢谢

org.gnome.Mutter.IdleMonitor在会话总线上,不在系统总线上;所以你需要使用 G_BUS_TYPE_SESSION.