gjs での dbus 呼び出しを含む gnome-shell 拡張機能を作成したいと思います。
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();
比較のために、次のコードは機能します。私が行った違いは、main() 関数でインスタンス化される Gio.Application を拡張する TestApp クラスを定義することです。
正しいコード:
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 のプログラミング経験がまったくないので、これは非常にばかげた質問かもしれません。ありがとう。