Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling DBus methods in Gjs / Gnome Shell

If I have a bus name, an object path, and an interface, how do I call DBus methods from Gjs (in a gnome-shell extension)?

I'm looking for the equivalent of the following python code:

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"))

(Note that I didn't explicitly use the interface due to some python-dbus magic, but I could have with iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog"). Since I have the interface name, I'm fine with a solution that queries it. Also note that this example would be silly in Gjs, as it calls back into gnome-shell)

like image 509
jdm Avatar asked Jul 18 '12 15:07

jdm


2 Answers

The import imports.dbus is deprecated since gnome-shell 3.4. The new way is to use Gio as described 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');

(Note that the original post uses makeProxyClass, correct is makeProxyWrapper.)

You can get the interface definition, for example, by using introspection. For pidgin/purple do:

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

Further explanations on introspection and inspection of interfaces can be found here.

like image 132
nemo Avatar answered Oct 18 '22 02:10

nemo


this should give you a better idea:

gjs> const DBus = imports.dbus;
gjs> for (let i in DBus) log(i);
like image 32
spirit walker Avatar answered Oct 18 '22 01:10

spirit walker