-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextension.js
209 lines (162 loc) · 5.55 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Keyboard Modifiers Status for gnome-shell
Copyright (C) 2015 Abdellah Chelli
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
import St from 'gi://St';
import Clutter from 'gi://Clutter';
//
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import GLib from 'gi://GLib';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
//
const dbg = false;
//TODO: convert into preferrence.
const tag = "KMS-Ext:";
const mod_sym = "⇧⇬⋀⌥①◆⌘⎇";
const latch_sym = "'";
const lock_sym = "◦";
const icon = ""; //"⌨ ";
const opening = ""; //"_";
const closing = ""; //"_";
//TODO: eliminate global variables
let seat;
let button,
label;
let state,
prev_state,
latch,
prev_latch,
lock,
prev_lock;
let indicator;
let timeout_id,
mods_update_id;
// Gnome-shell extension interface
// init, enable, disable
export default class KMS extends Extension {
constructor(metadata) {
super(metadata);
console.debug(`${tag} constructor() ... done ${this.metadata.name}`);
}
enable() {
console.debug(`${tag} enable() ... in`);
//
seat = null;
button = null;
label = null;
state = 0;
prev_state = 0;
latch = 0;
prev_latch = 0;
lock = 0;
prev_lock = 0;
timeout_id = null;
mods_update_id = null;
//
button = new St.Bin({ style_class: 'panel-button',
reactive: false,
can_focus: false,
x_expand: true,
y_expand: false,
track_hover: false });
label = new St.Label({ style_class: "state-label", text: "" });
button.set_child(label);
//console.debug(`${tag} Running Wayland: ` + Meta.is_wayland_compositor());
try {
seat = Clutter.get_default_backend().get_default_seat();
} catch (e) {
seat = Clutter.DeviceManager.get_default();
};
if (seat) {
mods_update_id = seat.connect("kbd-a11y-mods-state-changed", this._a11y_mods_update);
};
Main.panel._rightBox.insert_child_at_index(button, 0);
timeout_id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 200, this._update);
console.debug(`${tag} enable() ... out`);
}
disable() {
console.debug(`${tag} disable() ... in`);
Main.panel._rightBox.remove_child(button);
GLib.source_remove(timeout_id);
if (seat && mods_update_id) {
seat.disconnect(mods_update_id);
};
button.destroy_all_children();
button.destroy();
seat = null;
button = null;
label = null;
timeout_id = null;
mods_update_id = null;
console.debug(`${tag} disable() ... out`);
}
//
_update() {
console.debug(`${tag} _update() ... in`);
//TODO: search for documentation about global
//Note: modifiers state from get_pointer is the base not the effective
// On latch active, it is on too. but on lock active, it is off
// Not the case, using Gdk.Keymap.get_default().get_modifier_state() which
// is the effective
const [x, y, m] = global.get_pointer();
if (typeof m !== 'undefined') {
state = m;
};
if ((state != prev_state) || latch != prev_latch || lock != prev_lock) {
console.debug(`${tag} State changed... ${prev_state}, ${state}`);
indicator = icon + opening + " ";
//TODO: don't relay on internal order
// use standard pre-defined constant masks
for (var i=0; i<8; i++ ) {
if ((state & 1<<i) || (lock & 1<<i)) {
indicator += mod_sym[i];
} else {
//indicator += "";
};
if (latch & 1<<i) {
indicator += latch_sym + " ";
} else {
//indicator += "";
};
if (lock & 1<<i) {
indicator += lock_sym + " ";
} else {
//indicator += "";
};
}
indicator += " " + closing;
label.text = indicator;
prev_state = state;
prev_latch = latch;
prev_lock = lock;
}
console.debug(`${tag} _update() ... out`);
//return true;
return GLib.SOURCE_CONTINUE;
}
_a11y_mods_update(o, latch_new, lock_new) {
console.debug(`${tag} _a11y_mods_update() ... in`);
//TODO: search what's the 1st parameter
if (typeof latch_new !== 'undefined') {
latch = latch_new;
};
if (typeof lock_new !== 'undefined') {
lock = lock_new;
};
console.debug(`${tag} latch: ${latch}, lock: ${lock}`);
console.debug(`${tag} _a11y_mods_update() ... out`);
//return true;
return GLib.SOURCE_CONTINUE;
}
}