使用 gjs 连接 DBus

使用 gjs 连接 DBus

我对 gjs 非常陌生,我想连接 dbus。我想创建一个服务并监听它。

一个小例子或指南会很有用。谢谢

答案1

我读到 Gnome 正在将 Java Script (GJS) 路线应用于其大部分桌面,并减少 C++ 和 Python 的数量。所以今天我发现这个问题特别有趣。

这是一个Java 脚本示例我将亲自尝试。在此示例中,您将学习如何创建 D-Bus 客户端以连接到服务,您将学习如何调用方法、连接到信号以及从服务获取属性。该示例使用管理键盘背光的 D-Bus 服务:

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

如果尚未完成,请安装 gjs:

sudo apt update && sudo apt install gjs

为您的 Java 脚本创建一个目录:

mkdir ~/javascript

使用gedit上面创建示例脚本并保存:

gedit ~/javascript/dbusclient.js

现在运行它:

cd ~/javascript
gjs dbusclient.js

报告了 WIP 错误(我将修复它并返回此问答页面并提供结果):

(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

没有键盘灯

虽然笔记本电脑有键盘灯,并且无线键盘有 3 种灯光设置,但 Gnome 的 UPOWER 无法将它们视为以下 shell 命令:

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

返回此:

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

不过,这可能是一个方便的脚本,可以修改以监控笔记本电脑电池的充电百分比。

(待续...)

相关内容