Calling DBus methods in Gjs / Gnome Shell
Asked Answered
T

2

7

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)

Thrave answered 18/7, 2012 at 15:20 Comment(0)
A
13

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.

Aurilia answered 27/5, 2013 at 20:39 Comment(0)
M
1

this should give you a better idea:

gjs> const DBus = imports.dbus;
gjs> for (let i in DBus) log(i);
Marcelmarcela answered 15/10, 2012 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.