-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.js
133 lines (119 loc) · 3.31 KB
/
utils.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
// A lot of code in this file is adapted from https://github.com/deinstapel/cpupower
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(),
EXTENSIONDIR = Me.dir.get_path(),
INSTALLER = `${EXTENSIONDIR}/scripts/installer.sh`,
TOOL_SUFFIX = GLib.get_user_name(),
PKEXEC = GLib.find_program_in_path('pkexec'),
SH = GLib.find_program_in_path('sh');
function spawnProcessCheckExitCode(...argv) {
return new Promise((resolve, reject) => {
let [ok, pid, stdin, stdout, stderr] = GLib.spawn_async(
EXTENSIONDIR,
argv,
null,
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null
);
if (!ok) {
reject();
return;
}
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (process, exitStatus) => {
GLib.spawn_close_pid(process);
let exitCode = 0;
try {
GLib.spawn_check_exit_status(exitStatus);
} catch (e) {
exitCode = e.code;
}
resolve(exitCode);
});
});
}
const EXIT_SUCCESS = 0,
EXIT_INVALID_ARGUMENT = 1,
EXIT_FAILURE = 2,
EXIT_NEEDS_UPDATE = 3,
EXIT_NOT_INSTALLED = 4,
EXIT_NEEDS_ROOT = 5;
function checkInstalled() {
return spawnProcessCheckExitCode(
SH,
INSTALLER,
'--prefix',
'/usr',
'--suffix',
TOOL_SUFFIX,
'--extension-path',
EXTENSIONDIR,
'check'
);
}
function install() {
return spawnProcessCheckExitCode(
PKEXEC,
SH,
INSTALLER,
'--prefix',
'/usr',
'--suffix',
TOOL_SUFFIX,
'--extension-path',
EXTENSIONDIR,
'install'
);
}
function uninstall() {
return spawnProcessCheckExitCode(
PKEXEC,
SH,
INSTALLER,
'--prefix',
'/usr',
'--suffix',
TOOL_SUFFIX,
'--extension-path',
EXTENSIONDIR,
'uninstall'
);
}
function runScreenpadTool(pkexecNeeded, ...params) {
return new Promise((resolve, reject) => {
let args = ['/usr/local/bin/screenpad-' + TOOL_SUFFIX].concat(params);
if (pkexecNeeded) {
args.unshift(PKEXEC);
}
let launcher = Gio.SubprocessLauncher.new(Gio.SubprocessFlags.STDOUT_PIPE);
launcher.set_cwd(EXTENSIONDIR);
let proc;
try {
proc = launcher.spawnv(args);
} catch (e) {
reject(e);
return;
}
let stdoutStream = new Gio.DataInputStream({
base_stream: proc.get_stdout_pipe(),
close_base_stream: true,
});
proc.wait_async(null, (proc, result) => {
// this only throws if async call got cancelled, but we
// explicitly passed null for the cancellable
let ok = proc.wait_finish(result);
if (!ok) {
reject();
return;
}
let exitCode = proc.get_exit_status();
let [stdout, _length] = stdoutStream.read_upto('', 0, null);
resolve({
ok: exitCode === 0,
exitCode,
stdout,
});
});
});
}