在Gjs/Gnome Shell中调用DBus方法

问题描述:

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

我找下面的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")) 

(请注意,我并没有明确的使用界面,由于一些Python-dbus的魔法,但我可以有iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog")。因为我有接口名称,所以我很满意一个查询它的解决方案。还要注意,这个例子在Gjs中很愚蠢,因为它调用回gnome-shell)

这应该会给你一个更好的想法:

gjs> const DBus = imports.dbus; 
gjs> for (let i in DBus) log(i); 

从gnome-shell 3.4开始,不推荐使用导入imports.dbus。 新的方法是使用Gio描述here

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'); 

(请注意,原来的职位采用makeProxyClass,正确的是makeProxyWrapper。)

你可以得到的接口定义,例如,通过使用内省。 对于皮钦/紫色DO:

$ dbus-send --print-reply --dest=im.pidgin.purple.PurpleService \ 
/im/pidgin/purple/PurpleObject org.freedesktop.DBus.Introspectable.Introspect 

上内省和接口检查进一步解释,可以发现here