-
Notifications
You must be signed in to change notification settings - Fork 60
/
convenience.js
77 lines (65 loc) · 2.41 KB
/
convenience.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
/*
* This is a part of CPUFreq Manager
* Copyright (C) 2016-2019 konkor <konkor.github.io>
*
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Gettext = imports.gettext;
const ByteArray = imports.byteArray;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
var Format = imports.format;
String.prototype.format = Format.format;
function initTranslations (domain) {
domain = domain || 'org-konkor-cpufreq';
let localeDir = Gio.File.new_for_path (getCurrentFile()[1] + '/locale');
if (localeDir.query_exists (null))
Gettext.bindtextdomain (domain, localeDir.get_path());
else
Gettext.bindtextdomain (domain, '/usr/share/locale');
}
function getSettings (schema) {
schema = schema || 'org.gnome.shell.extensions.cpufreq';
const GioSSS = Gio.SettingsSchemaSource;
let schemaDir = Gio.File.new_for_path (getCurrentFile()[1] + '/schemas');
let schemaSource;
if (schemaDir.query_exists (null))
schemaSource = GioSSS.new_from_directory (
schemaDir.get_path (), GioSSS.get_default (), false
);
else
schemaSource = GioSSS.get_default ();
let schemaObj = schemaSource.lookup (schema, true);
if (!schemaObj)
throw new Error ('Schema ' + schema + ' could not be found for extension ' +
'cpufreq@konkor. Please check your installation.');
return new Gio.Settings ({ settings_schema: schemaObj });
}
function getCurrentFile () {
let stack = (new Error()).stack;
let stackLine = stack.split('\n')[1];
if (!stackLine)
throw new Error ('Could not find current file');
let match = new RegExp ('@(.+):\\d+').exec(stackLine);
if (!match)
throw new Error ('Could not find current file');
let path = match[1];
let file = Gio.File.new_for_path (path);
return [file.get_path(), file.get_parent().get_path(), file.get_basename()];
}
function byteArrayToString (array) {
return array instanceof Uint8Array ? ByteArray.toString (array):array;
}
function get_cpu_number () {
let c = 0;
let cpulist = null;
let ret = GLib.spawn_command_line_sync ("cat /sys/devices/system/cpu/present");
if (ret[0]) cpulist = byteArrayToString(ret[1]).toString().split("\n", 1)[0].split("-");
cpulist.forEach ((f) => {
if (parseInt (f) > 0) c = parseInt (f);
});
return c + 1;
}