3

I am extremely new to gjs and I want to interface dbus. I want to create a service and listen to it.

A small example or guide would be useful. Thank You

1 Answer 1

4

I've read Gnome is going the route of Java Script (GJS) for much of it's desktop and reducing amount of C++ and Python. So I found this question particularly interesting today.

Here is an example Java Script and I will be trying it myself. In this example you will learn to create a D-Bus client to connect to a service, you will learn to call methods, connect to signals and get properties from the service. The example uses the D-Bus service that manage the Keyboard Backlight:

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;

// This the D-Bus interface as XML
const KbdBacklightInterface = '<node>\
<interface name="org.freedesktop.UPower.KbdBacklight"> \
    <method name="SetBrightness"> \
        <arg name="value" type="i" direction="in"/> \
    </method> \
    <method name="GetBrightness"> \
        <arg name="value" type="i" direction="out"/> \
    </method> \
    <method name="GetMaxBrightness"> \
        <arg name="value" type="i" direction="out"/> \
    </method> \
    <signal name="BrightnessChanged"> \
        <arg type="i"/> \
    </signal> \
</interface> \
</node>';

// Declare the proxy class based on the interface
const KbdBacklightProxy = Gio.DBusProxy.makeProxyWrapper(KbdBacklightInterface);

// Get the /org/freedesktop/UPower/KbdBacklight instance from the bus
let kbdProxy = new KbdBacklightProxy(
    Gio.DBus.system,
    "org.freedesktop.UPower",
    "/org/freedesktop/UPower/KbdBacklight"
);

// You can use proxy.<method>Sync syntax to 
// call the D-Bus method in a Sync way
print("The max brightness of your keyboard is " + kbdProxy.GetMaxBrightnessSync());

// Or you can use the syntax proxy.<method>Remote
// to call the method in an Async way
kbdProxy.GetBrightnessRemote(function(currentBrightness) {
    print("The current keyboard brightness is " + currentBrightness);
});

// Connecting to a D-Bus signal
kbdProxy.connectSignal("BrightnessChanged", function(proxy) {
    let newBrightness = proxy.GetBrightnessSync();
    print("The keyboard brightness has been changed, new brightness is " + newBrightness);
});

// Also you can get properties value using this syntax
// let property = proxy.PropertyName;

// Or you can set a property value
// proxy.PropertyName = "new value";

let loop = new GLib.MainLoop(null, false);
loop.run();

If not already done, install gjs:

sudo apt update && sudo apt install gjs

Create a directory for your Java Scripts:

mkdir ~/javascript

Use gedit to create sample script above and save it:

gedit ~/javascript/dbusclient.js

Now run it:

cd ~/javascript
gjs dbusclient.js

WIP Errors reported (I'll fix and come back to this Q&A with results):

(gjs:10134): Gjs-WARNING **: JS ERROR: Gio.DBusError: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface 'org.freedesktop.UPower.KbdBacklight' on object at path /org/freedesktop/UPower/KbdBacklight
_proxyInvoker@resource:///org/gnome/gjs/modules/overrides/Gio.js:98
_makeProxyMethod/<@resource:///org/gnome/gjs/modules/overrides/Gio.js:124
@dbusclient.js:36

JS_EvaluateScript() failed

No Keyboard lights

Although the laptop has keyboard lights and the wireless keyboard has 3 light settings, Gnome's UPOWER can't see them as these shell commands:

$ dbus-send --print-reply \
            --system \
            --dest=org.freedesktop.UPower \
            /org/freedesktop/UPower \
            org.freedesktop.UPower.EnumerateDevices

Return this:

method return time=1564075040.686545 sender=:1.49 -> destination=:1.145 serial=4392 reply_serial=2
   array [
      object path "/org/freedesktop/UPower/devices/line_power_ACAD"
      object path "/org/freedesktop/UPower/devices/battery_BAT1"
      object path "/org/freedesktop/UPower/devices/ups_hiddev2"
      object path "/org/freedesktop/UPower/devices/mouse_0003o046Do101Ax0017"
      object path "/org/freedesktop/UPower/devices/keyboard_0003o046Do2010x0018"
   ]

This could be a handy script to modify for monitoring Laptop Battery charge percentage though.

(To be continued...)

5
  • Could you please show an example of how to broadcast signal via a gjs and listen to it via another gjs process? and this code doesnt explain how to call methods or listen to signals Jul 29, 2019 at 3:12
  • How did you get the GetMaxBrightnessSync() ? Jul 29, 2019 at 3:19
  • @TamoghnaMaitra I'll put that on my to-do list. Currently I'm finding gjs examples published by gnome don't work for me because I'm on Ubuntu 16.04 which uses gjs version 1.44 and I need to use version 1.50 for their examples which means upgrade to 18.04 or 19.04 which I have underway (it's taking a long time). Jul 29, 2019 at 3:20
  • @TamoghnaMaitra For your questions on GetMaxBrightnessSync(), or any other code, you can ask the gnome folks directly for immediate support. I did that to find out I needed to upgrade. If you can wait awhile I'll probably be able to help you after I upgrade my gjs. Jul 29, 2019 at 3:26
  • hey thanks for your help. Can you please share any contact of any gnome dev? Or any link where I post my doubts? Jul 29, 2019 at 3:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .