【问题标题】:call DBus method using GDBus in gjs, no output?在gjs中使用GDBus调用DBus方法,没有输出?
【发布时间】:2013-09-24 23:12:28
【问题描述】:

我想写一个 gnome-shell 扩展,其中涉及到 gjs 中的一些 dbus 调用。

我了解到 Gio.DBus 是正确使用的模块,但我未能使其正常运行。为了说明我的意思,我准备了以下“不正确”的代码,它试图调用 org.freedesktop.DBus 接口中的 ListNames 方法。运行此错误代码时,我没有看到任何输出。

代码不正确:

const Gio = imports.gi.Gio;

const DBusDaemonIface = <interface name='org.freedesktop.DBus'>
    <method name='ListNames'>
        <arg type='as' direction='out'/>
    </method>
</interface>;

const DBusDaemonProxy = Gio.DBusProxy.makeProxyWrapper(DBusDaemonIface);

let main = function () {
    var gdbusProxy = new DBusDaemonProxy(Gio.DBus.session, 'org.freedesktop.DBus', '/org/freedesktop/DBus');
    gdbusProxy.ListNamesRemote(function(result, error){ print(result); });
};

main();

为了比较,以下代码有效。我所做的不同之处在于定义了一个扩展 Gio.Application 的 TestApp 类,该类在 main() 函数中被实例化。

正确代码:

const Gio = imports.gi.Gio;
const Lang = imports.lang;

const DBusDaemonIface = <interface name='org.freedesktop.DBus'>
    <method name='ListNames'>
        <arg type='as' direction='out'/>
    </method>
</interface>;

const DBusDaemonProxy = Gio.DBusProxy.makeProxyWrapper(DBusDaemonIface);

TestApp = new Lang.Class({
    Name: 'TestApp',
    Extends: Gio.Application,

    _init: function() {
        this.parent({application_id: 'testapp_id',
            flags: Gio.ApplicationFlags.NON_UNIQUE });

        this._gdbusProxy = new DBusDaemonProxy(Gio.DBus.session,
            'org.freedesktop.DBus', '/org/freedesktop/DBus');
    this._gdbusProxy.ListNamesRemote(Lang.bind(this, this._listNames));
    },

    _listNames: function(result, error) {
    print(result);
    },

    vfunc_activate: function() {
        this.hold();
    },

});

let main = function () {
    let app = new TestApp();
    return app.run(ARGV);
};

main();

所以我的猜测是让 GDBus 工作,你需要一个 Gio.Application 来运行吗?这可能是一个非常愚蠢的问题,因为我对 GNOME 的编程经验为零。谢谢。

【问题讨论】:

    标签: gnome dbus gnome-shell gjs gdbus


    【解决方案1】:

    代码很好,只是你没有运行主循环,所以应用程序在ListNamesRemote 回调有时间运行之前退出。 app.run(ARGV) 会给你一个主循环,但你也可以用普通的 GLib 来做:

    const GLib = imports.gi.GLib;
    
    // ... then in the end of your main() function:
    var main_loop = new GLib.MainLoop(null, true);
    main_loop.run();
    

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 2015-12-04
      相关资源
      最近更新 更多