Как отправить строку в расширение gnome-shell?

Я думаю, что D-Bus следует использовать. По сути, я хочу что-то подобное — https://wiki.gnome.org/Gjs/Examples/DBusClient — но наоборот.

В расширении будет функция:

function f(s) { doSomethingWithS; }

И эта функция будет вызываться после запуска:

$ <something> "abc"

… в терминале с s == "abc".


После предложений от @Jasper и @owen в #gnome-shell на irc.gnome.org я адаптировал некоторый код из https://github.com/GNOME/gnome-shell/blob/master/js/ui/magnifierDBus.js:

const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Main = imports.ui.main;

let text;

function init() {
    text = new St.Label({ text: "0:0", style_class: 'panel-text' });
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(text, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(text);
}

const TextInTaskBarIface = '<node> \
<interface name="com.michalrus.TextInTaskBar"> \
<method name="setText"> \
    <arg type="s" direction="in" /> \
</method> \
</interface> \
</node>';

const TextInTaskBar = new Lang.Class({
    Name: 'TextInTaskBar',

    _init: function() {
        this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(TextInTaskBarIface, this);
        this._dbusImpl.export(Gio.DBus.session, '/com/michalrus/TextInTaskBar');
    },

    setText: function(str) {
        text.text = str;
    }
});

Теперь, после выдачи:

% dbus-send --dest=com.michalrus.TextInTaskBar /com/michalrus/TextInTaskBar \
    com.michalrus.TextInTaskBar.setText string:"123"

… Ничего не произошло.


person Michal Rus    schedule 07.10.2015    source источник


Ответы (1)


Окончательная рабочая версия сервера D-Bus с расширением gnome-shell:

const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Main = imports.ui.main;

let text = null;
let textDBusService = null;

function init() {
    text = new St.Label({ text: "0:0", style_class: 'panel-text' });
    textDBusService = new TextInTaskBar();
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(text, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(text);
}

const TextInTaskBarIface = '<node> \
<interface name="com.michalrus.TextInTaskBar"> \
<method name="setText"> \
    <arg type="s" direction="in" /> \
</method> \
</interface> \
</node>';

const TextInTaskBar = new Lang.Class({
    Name: 'TextInTaskBar',

    _init: function() {
    text.text = "abc";
        this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(TextInTaskBarIface, this);
        this._dbusImpl.export(Gio.DBus.session, '/com/michalrus/TextInTaskBar');
    },

    setText: function(str) {
    text.text = str;
    }
});

Позвонить с помощью:

$ gdbus call --session --dest org.gnome.Shell --object-path /com/michalrus/TextInTaskBar --method com.michalrus.TextInTaskBar.setText 'some text'
person Michal Rus    schedule 07.10.2015