-
Notifications
You must be signed in to change notification settings - Fork 11
/
extension.js
301 lines (275 loc) · 13.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
const Gio = imports.gi.Gio;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Keybindings = Me.imports.keybindings;
const utils = Me.imports.utils;
const ScreenpadSysfsPath = '/sys/class/leds/asus::screenpad';
class Extension {
constructor() {
this._firstRun = true;
}
enable() {
if (this._firstRun) {
this._screenpadBrightnessFile = Gio.File.new_for_path(`${ScreenpadSysfsPath}/brightness`);
if (!this._screenpadBrightnessFile.query_exists(null)) {
this._showNotification(
'The Screenpad brightness file does not exist',
'Ensure the asus-wmi-screenpad module is installed and loaded and that your device is compatible with this module.',
'Click here to see how to do this',
function () {
Gio.AppInfo.launch_default_for_uri_async(
'https://github.com/Plippo/asus-wmi-screenpad#readme',
null,
null,
null
);
}
);
} else {
utils.checkInstalled().then((result) => {
switch (result) {
case utils.EXIT_SUCCESS:
// Only check for the udev rule if the additional files are installed
const udevRuleFile = Gio.File.new_for_path('/etc/udev/rules.d/99-screenpad.rules');
if (udevRuleFile.query_exists(null)) {
this._showNotification(
'You still have the old udev rule on your system',
"This rule was previously used to get write access on the brightness file, but it isn't needed anymore.",
'Click here to see how to remove it',
function () {
Gio.AppInfo.launch_default_for_uri_async(
'https://github.com/laurinneff/gnome-shell-extension-zenbook-duo/blob/master/docs/permissions.md#removing-the-old-udev-rule',
null,
null,
null
);
}
);
}
break;
case utils.EXIT_NOT_INSTALLED:
this._showNotification(
'This extension requires additional configuration',
"In order for this extension to work, it needs to install some files. You can undo this in the extension's settings.",
'Click here to do this automatically',
async function () {
switch (await utils.install()) {
case utils.EXIT_SUCCESS:
this._showNotification(
'Successfully installed files',
'The files have been installed successfully. You can now use the extension.'
);
break;
case utils.EXIT_FAILURE:
this._showNotification(
'Failed to install the files',
'The files could not be installed.'
);
break;
}
}
);
break;
case utils.EXIT_NEEDS_UPDATE:
this._showNotification(
'The additional files for the Screenpad+ extension requires an update',
'The extension has been updated, but the additional files need to be updated separately.',
'Click here to do this automatically',
async function () {
switch (await utils.install()) {
case utils.EXIT_SUCCESS:
this._showNotification(
'Successfully updated files',
'The files have been files successfully. You can now use the extension.'
);
break;
case utils.EXIT_FAILURE:
this._showNotification(
'Failed to update the files',
'The files could not be updated.'
);
break;
}
}
);
break;
}
});
}
/*
This variable is kept between enabling/disabling (so that the extension doesn't check if the
brightness file exists and if the additional files are installed after unlocking the screen)
*/
this._firstRun = false;
}
this.settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.zenbook-duo');
this._keybindingManager = new Keybindings.Manager();
// Screenpad toggle key
this._keybindingManager.add(
'Launch7',
async function () {
try {
let brightness = await this._getBrightness();
if (brightness === 0) {
// Range from 1 to 255 so the screenpad can't be turned off completely by changing the brightness
this._setBrightness(this._brightnessSlider.value * 254 + 1);
} else {
this._setBrightness(0);
}
} catch (e) {
logError(e);
}
}.bind(this)
);
// Swap windows key
this._keybindingManager.add(
'Launch6',
function () {
// TODO: Swap windows
this._showNotification('This key is not implemented yet.');
}.bind(this)
);
// Screenshot key
this._keybindingManager.add(
'<Shift><Super>s',
function () {
let args;
switch (this.settings.get_string('screenshot-type')) {
case 'Screen':
args = []; // gnome-screenshot without args will take a screenshot of the whole screen
break;
case 'Window':
args = ['--window'];
break;
case 'Selection':
args = ['--area'];
break;
case 'Interactive':
args = ['--interactive'];
break;
}
if (this.settings.get_boolean('screenshot-include-cursor')) {
args.push('--include-pointer');
}
try {
// The process starts running immediately after this function is called. Any
// error thrown here will be a result of the process failing to start, not
// the success or failure of the process itself.
let proc = Gio.Subprocess.new(
// The program and command options are passed as a list of arguments
['gnome-screenshot', ...args],
// The flags control what I/O pipes are opened and how they are directed
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
} catch (e) {
logError(e);
}
}.bind(this)
);
// MyASUS key
this._keybindingManager.add(
'Tools',
function () {
try {
// The process starts running immediately after this function is called. Any
// error thrown here will be a result of the process failing to start, not
// the success or failure of the process itself.
let proc = Gio.Subprocess.new(
// The program and command options are passed as a list of arguments
['sh', '-c', this.settings.get_string('myasus-cmd')],
// The flags control what I/O pipes are opened and how they are directed
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
} catch (e) {
logError(e);
}
}.bind(this)
);
// Toggle webcam key
this._keybindingManager.add(
'WebCam',
function () {
// TODO: Toggle camera
this._showNotification('This key is not implemented yet.');
}.bind(this)
);
this._brightnessSlider = imports.ui.main.panel.statusArea.aggregateMenu._brightness._slider;
this._brightnessListenerId = this._brightnessSlider.connect(
'notify::value',
async function () {
try {
if ((await this._getBrightness()) === 0) return; // Don't turn Screenpad on when it was turned off
// Range from 1 to 255 so the screenpad can't be turned off completely by changing the brightness
await this._setBrightness(this._brightnessSlider.value * 254 + 1);
} catch (e) {
logError(e);
}
}.bind(this)
);
this.settings.connect(
'changed::uninstall',
async function () {
if (this.settings.get_boolean('uninstall')) {
switch (await utils.uninstall()) {
case utils.EXIT_SUCCESS:
this._showNotification(
'Successfully uninstalled files',
'The files have been uninstalled successfully. You can now remove the extension.'
);
break;
case utils.EXIT_FAILURE:
this._showNotification(
'Failed to uninstall the files',
'The files could not be uninstalled.'
);
break;
}
this.settings.set_boolean('uninstall', false);
}
}.bind(this)
);
}
disable() {
this._brightnessSlider.disconnect(this._brightnessListenerId);
this._keybindingManager.destroy();
if (this._notifSource) {
this._notifSource.destroy();
this._notifSource = null;
}
this._keybindingManager = null;
this.settings = null;
}
// Shamelessly stolen from https://github.com/RaphaelRochet/arch-update/blob/3d3f5927ec0d33408a802d6d38af39c1b9b6f8e5/extension.js#L473-L497
_showNotification(title, message, btnText, btnAction) {
if (this._notifSource == null) {
// We have to prepare this only once
this._notifSource = new MessageTray.SystemNotificationSource();
// Take care of note leaving unneeded sources
this._notifSource.connect('destroy', () => {
this._notifSource = null;
});
Main.messageTray.add(this._notifSource);
}
let notification = null;
notification = new MessageTray.Notification(this._notifSource, title, message);
if (btnText) notification.addAction(btnText, btnAction.bind(this));
notification.setTransient(true);
this._notifSource.showNotification(notification);
}
async _getBrightness() {
const ret = await utils.runScreenpadTool(false, 'get');
return +ret.stdout;
}
async _setBrightness(brightness) {
const ret = await utils.runScreenpadTool(true, 'set', Math.floor(brightness).toString());
return ret.ok;
}
}
function init() {
return new Extension();
}