Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension event loop in Gnome 3.10 vs 3.14

I wrote this accessibility extension:

  • https://extensions.gnome.org/extension/975/keyboard-modifiers-status/
  • https://github.com/sneetsher/Keyboard-Modifiers-Status

Which works as supposed in Gnome Shell v3.14 & v3.16 but not in v3.10. It shows the only the initial keyboard modifiers state after i restarted it and never update it after that.

Here the full code:

const St = imports.gi.St;
const Mainloop = imports.mainloop;
const Main = imports.ui.main;
const Gdk = imports.gi.Gdk

let button, label, keymap;

function _update() {
    let symbols = "⇧⇬⋀⌥①◆⌘⎇";
    let state = keymap.get_modifier_state();    
    label.text = " ";
    for (var i=0; i<=8; i++ ) { 
        if (state & 1<<i) {
            label.text += symbols[i];
        } else {
            //label.text += "";
        }
    }
    label.text += " ";
}

function init() {
    button = new St.Bin({ style_class: 'panel-button',
                          reactive: false,
                          can_focus: false,
                          x_fill: true,
                          y_fill: false,
                          track_hover: false });

    label = new St.Label({ style_class: "state-label", text: "" });
    button.set_child(label);

    keymap = Gdk.Keymap.get_default();
    keymap.connect('state_changed',  _update );
    Mainloop.timeout_add(1000, _update );
}

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

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

Trying to debug, I modified the code to show (state label + a counter)

let c,button, label, keymap;
c=0;

function _update() {
    Gtk.main_iteration_do(false);
    c++;
    let symbols = "⇧⇬⋀⌥①◆⌘⎇";
    //let keymap = Gdk.Keymap.get_default()
    let state = keymap.get_modifier_state();
    label.text = " ";
    for (var i=0; i<=8; i++ ) {
        if (state & 1<<i) {
            label.text += symbols[i];
        } else {
            //label.text += "";
        }
    }
    label.text += " "+c+" ";
    return true;
}

I can confirm these:

  • keymap.connect('state_changed', _update ); this signal is never raised
  • timeout callback works well
  • label is updated and show the initial state & the incrementing counter

So I think there is something with event loop as it does not pull state update or does not process its events.

Could you please point me to way to fix this and what's the difference between v3.10 & v3.14?

like image 591
user.dz Avatar asked Nov 10 '22 01:11

user.dz


1 Answers

Assuming that commenting out the definition of keymap was intentional, check that it is still assigned elsewhere in your code. Have you tried using a -(minus) rather than a _(underscore)? Most events use the former in JS space, rather than the latter and this has been the problem for me when in several cases where I was attaching events to changing the active workspace, where the back-end for Meta.Display fires workspace_switched, the GJS space connects through workspace-switched and there are a lot more examples there.

For official documentation, including the correct event, property and function names for within GJS space, refer to GNOME DevDocs I don't know when it became official, but they state that it is here

like image 168
RivenSkaye Avatar answered Dec 29 '22 12:12

RivenSkaye