-
Notifications
You must be signed in to change notification settings - Fork 8
/
extension.js
149 lines (121 loc) · 4.15 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
const BaseIndicator = imports.ui.status.power.Indicator;
const ExtensionUtils = imports.misc.extensionUtils;
const Panel = imports.ui.main.panel;
const Shell = imports.gi.Shell;
const GObject = imports.gi.GObject;
const GLib = imports.gi.GLib;
const Config = imports.misc.config;
/** Settings
*/
const BAT_STATUS = "/sys/class/power_supply/BAT0/status";
const POWER_NOW = "/sys/class/power_supply/BAT0/power_now";
const LAPMODE = "/sys/devices/platform/thinkpad_acpi/dytc_lapmode"
/** Indicator
*/
var TPIndicator = GObject.registerClass(
{
GTypeName: 'TPIndicator',
},
class TPIndicator extends BaseIndicator {
_init() {
super._init();
this.settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');
this.settings.connect('changed::period-sec', () => { this._spawn(); }); // restart timers on setting change
this.readings = [];
this.last_value = 0.0;
this.tm_measure = null;
}
_getBatteryStatus() {
const pct = this._proxy.Percentage.toFixed(0);
const power = this.last_value.toFixed(1);
const status = this._read_file(BAT_STATUS, '???');
let sign = ' ';
if (status == 'Charging') {
sign = '+';
} else if (status == 'Discharging') {
sign = '-';
}
let lap_mode_char = this._getLapMode()
return _("%s%% %s%sW%s").format(pct, sign, power, lap_mode_char);
}
_getLapMode() {
const show_lap_mode = this.settings.get_boolean('lap-mode');
if (!show_lap_mode){
return ('')
}
const status = this._read_file(LAPMODE, '???');
let lap_mode_char = '';
if (status == '1') {
lap_mode_char = " lap";
}
else {
lap_mode_char = ''
}
return (lap_mode_char)
}
_sync() {
super._sync();
this._percentageLabel.clutter_text.set_text(this._getBatteryStatus());
return true;
}
_read_file(filePath, defaultValue) {
try {
return Shell.get_file_contents_utf8_sync(filePath).trim();
} catch (e) {
log(`Cannot read file ${filePath}`, e);
}
return defaultValue;
}
_measure() {
const power = parseFloat(this._read_file(POWER_NOW), 0) / 1000000;
this.readings.push(power)
const avg_of = this.settings.get_int('avg-of');
if (this.readings.length >= avg_of) {
this.last_value = this.readings.reduce((acc, elem) => acc + elem, 0.0) / this.readings.length; // simple mean
this._sync(); // update battery widget now!
this.readings.length = 0; // fastest way to clear array?
}
return true;
}
_spawn() {
if (this.tm_measure !== null) {
GLib.source_remove(this.tm_measure);
}
this.tm_measure = GLib.timeout_add(
GLib.PRIORITY_DEFAULT,
this.settings.get_double('period-sec') * 1000,
this._measure.bind(this),
);
}
_stop() {
GLib.source_remove(this.tm_measure);
this.tm_measure = null;
}
}
);
/** Extension
*/
class TPWattMeter {
constructor() {
this.customIndicator = new TPIndicator();
this.customIndicator._spawn();
this.aggregateMenu = Panel.statusArea['aggregateMenu'];
this.originalIndicator = this.aggregateMenu._power;
this.aggregateMenu._indicators.replace_child(this.originalIndicator, this.customIndicator);
}
destroy(arg) {
this.customIndicator._stop();
this.aggregateMenu._indicators.replace_child(this.customIndicator, this.originalIndicator);
this.customIndicator = null;
}
}
/** Init
*/
let tp_wattmeter;
function enable() {
tp_wattmeter = new TPWattMeter();
}
function disable() {
tp_wattmeter.destroy();
tp_wattmeter = null;
}