Re: Gjs DBus bindings questions (introspection, method names and callbacks)
- From: Giovanni Campagna <scampa giovanni gmail com>
- To: Jason Heeris <jason heeris gmail com>
- Cc: gnome-shell-list <gnome-shell-list gnome org>
- Subject: Re: Gjs DBus bindings questions (introspection, method names and callbacks)
- Date: Mon, 25 Feb 2013 15:07:25 +0100
2013/2/25 Jason Heeris <jason heeris gmail com>:
I'm trying to figure out how to use DBus from my Gnome Shell
extension. I have the option of using the GLib DBus interface, but
then I saw that there's actual Gjs DBus bindings! The Gnome Live Gjs
page only has a link to here:
http://erick2red.github.com/blog/2011/05/28/dbus-in-gjs-and-gnome-shell/
...but I still have a few questions:
1. That page doesn't mention introspection - is it supported? Can I
get away with not explicitly specifying the signatures on the
interfaces I want to use?
2. The method called Notify in the interface definition becomes
NotifyRemote when it's called. Why is that?
3. How do the callbacks work - is it a single function with two
parameters: 'result' and 'error'? Can I instead use two separate
callbacks? Is 'result' undefined if there's an error, or is it
something else (ie. how do I test for success)?
4. Can I make blocking calls instead?
5. Can I connect to signals?
Hello!
Unfortunately, that page is significantly outdated, and still mentions
the dbus-glib based bindings, which are obsolete since 3.4 and were
removed in 3.8. The modern bindings are offered as a set of overrides
over the GDBus introspected API.
A simple example is:
const Gio = imports.gi.Gio;
const MyIface = <interface name="org.example.MyInterface">
<method name="Activate" />
</interface>;
const MyProxy = Gio.DBusProxy.makeProxyClass(MyIface);
let instance = new MyProxy(Gio.DBus.session, 'org.example.BusName',
'/org/example/Path');
Introspection is needed, in E4X or string format. If you don't want
that, you can use a raw Gio.DBusProxy.
The constructor is synchrnous, but you can add an optional callback
with the same conventions as method calls to get the async variant.
Note that constructing a proxy will cause DBus activation to occur.
Method names are exported as Method + "Remote" for the async variant
and Method + "Sync" for the sync variant. They take a variable number
of arguments (depending on the number of actual arguments of the DBus
interface), plus optionally a Gio.Cancellable and Gio.DBusCallFlags.
The aync version takes a callback with two arguments, result and
error. If successful, result contains the out arguments from the DBus
call, as an array, and error is null. If the remote call fails, result
will be null and error will be a GLib.Error. You can use Gio.DBusError
methods to extract the DBus error name from it.
The sync version takes no callback, but returns the result tuple
directly, or throws an exception in case of errors.
You connect to signals with .connectSignal('...', function(proxy,
sender, parameters) { }) and disconnect with disconnectSignals(id);
Note that disconnecting from signals will not remove the AddMatch
rule, you must run_dispose() the proxy for that.
Properties work as getter and setters, and are cached in the
constructor. Errors from setting are ignore. Connect to
'g-properties-changed' GObject signal to receive notification of
changes.
A full example (including the server side version) is in the gjs tree,
as test/js/testGDBus.js
And naturally, if you want to help documenting it better, you're very welcome!
Giovanni
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]