在Gjs/Gnome Shell中调用DBus方法

jdm*_*jdm 5 dbus gnome-shell gjs

如果我有总线名称,对象路径和接口,如何从Gjs调用DBus方法(在gnome-shell扩展中)?

我正在寻找以下python代码的等价物:

import dbus
bus = dbus.SessionBus()
obj = bus.get_object("org.gnome.Caribou.Keyboard", "/org/gnome/SessionManager/EndSessionDialog")
obj.Open(0, 0, 120, dbus.Array(signature="o"))
Run Code Online (Sandbox Code Playgroud)

(请注意,由于某些python-dbus魔法,我没有显式使用该接口,但我可以使用iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog").由于我有接口名称,我可以使用查询它的解决方案.另请注意,此示例将是在Gjs中愚蠢,因为它回调到gnome-shell)

nem*_*emo 8

imports.dbus从gnome-shell 3.4开始,不推荐导入.新的方法是使用Gio所描述的在这里:

const Gio = imports.gi.Gio;

const MyIface = '<interface name="org.example.MyInterface">\
<method name="Activate" />\
</interface>';
const MyProxy = Gio.DBusProxy.makeProxyWrapper(MyIface);

let instance = new MyProxy(Gio.DBus.session, 'org.example.BusName',
'/org/example/Path');
Run Code Online (Sandbox Code Playgroud)

(注意原帖使用makeProxyClass,正确的是makeProxyWrapper.)

您可以通过使用内省来获取接口定义.对于洋泾浜/紫色做:

$ dbus-send --print-reply --dest=im.pidgin.purple.PurpleService \
/im/pidgin/purple/PurpleObject org.freedesktop.DBus.Introspectable.Introspect
Run Code Online (Sandbox Code Playgroud)

可以在此处找到有关界面内省和检查的进一步说明.


spi*_*ker 1

这应该会给你一个更好的主意:

gjs> const DBus = imports.dbus;
gjs> for (let i in DBus) log(i);
Run Code Online (Sandbox Code Playgroud)