【问题标题】:Calling DBus methods in Gjs / Gnome Shell在 Gjs / Gnome Shell 中调用 DBus 方法
【发布时间】:2012-07-17 16:31:14
【问题描述】:

如果我有总线名称、对象路径和接口,我如何从 Gjs(在 gnome-shell 扩展中)调用 DBus 方法?

我正在寻找以下 python 代码的等价物:

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

(请注意,由于某些 python-dbus 魔术,我没有显式使用该接口,但我可以使用 iface = dbus.interface(obj, "org.gnome.SessionManager.EndSessionDialog")。由于我有接口名称,我可以使用查询它的解决方案。另外请注意,这个示例在 Gjs 中会很愚蠢,因为它会回调到 gnome-shell)

【问题讨论】:

    标签: dbus gnome-shell gjs


    【解决方案1】:

    自 gnome-shell 3.4 起,导入 imports.dbus 已被弃用。 新方法是使用Gio,如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');
    

    (注意原帖使用makeProxyClass,正确的是makeProxyWrapper。)

    您可以获取接口定义,例如,通过使用自省。 对于 pidgin/purple 做:

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

    关于接口自省和检查的进一步解释可以找到here

    【讨论】:

      【解决方案2】:

      这应该会给你一个更好的主意:

      gjs> const DBus = imports.dbus;
      gjs> for (let i in DBus) log(i);
      

      【讨论】:

        猜你喜欢
        • 2013-09-24
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多