forked from isDipesh/gnome-shell-extension-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
378 lines (348 loc) · 15.4 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
const St = imports.gi.St;
const Lang = imports.lang;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;
const Util = imports.misc.util;
const Mainloop = imports.mainloop;
function CpuTemperature() {
this._init.apply(this, arguments);
}
CpuTemperature.prototype = {
__proto__: PanelMenu.SystemStatusButton.prototype,
_init: function(){
PanelMenu.SystemStatusButton.prototype._init.call(this, 'temperature');
this.lang = {
'acpi' : 'ACPI Adapter',
'pci' : 'PCI Adapter',
'virt' : 'Virtual Thermal Zone'
};
this.statusLabel = new St.Label({
text: "--",
style_class: "temperature-label"
});
// destroy all previously created children, and add our statusLabel
this.actor.get_children().forEach(function(c) {
c.destroy()
});
this.actor.add_actor(this.statusLabel);
this.sensorsPath = this._detectSensors();
this.command=["xdg-open", "http://github.com/xtranophilist/gnome-shell-extension-cpu-temperature/issues/"];
if(this.sensorsPath){
this.title='Error';
this.content='Run sensors-detect as root. If it doesn\'t help, click here to report with your sensors output!';
}
else{
this.title='Warning';
this.content='Please install lm_sensors. If it doesn\'t help, click here to report with your sensors output!';
}
this._update_temp();
//update every 15 seconds
event = GLib.timeout_add_seconds(0, 15, Lang.bind(this, function () {
this._update_temp();
return true;
}));
},
_detectSensors: function(){
//detect if sensors is installed
let ret = GLib.spawn_command_line_sync("which sensors");
if ( (ret[0]) && (ret[3] == 0) ) {//if yes
return ret[1].toString().split("\n", 1)[0];//find the path of the sensors
}
return null;
},
_update_temp: function() {
let items = new Array();
let tempInfo=null;
if (this.sensorsPath){
let sensors_output = GLib.spawn_command_line_sync(this.sensorsPath);//get the output of the sensors command
if(sensors_output[0]) tempInfo = this._findTemperatureFromSensorsOutput(sensors_output[1].toString());//get temperature from sensors
if (tempInfo){
//destroy all items in popup
this.menu.box.get_children().forEach(function(c) {
c.destroy()
});
var s=0, n=0;//sum and count
for (let adapter in tempInfo){
if(adapter!=0){
//ISA Adapters
if (adapter=='isa'){
for (let cpu in tempInfo[adapter]){
items.push("ISA Adapter "+cpu+": ");
for (let core in tempInfo[adapter][cpu]){
s+=tempInfo[adapter][cpu][core]['temp'];
n++;
items.push(core+' : '+this._formatTemp(tempInfo[adapter][cpu][core]['temp']));
}
}
}else if (tempInfo[adapter]['temp']>0){
s+=tempInfo[adapter]['temp'];
n++;
items.push(this.lang[adapter] + ' : '+this._formatTemp(tempInfo[adapter]['temp']));
}
}
}
if (n!=0){//if temperature is detected
this.title=this._formatTemp(s/n);//set title as average
}
}
}
//if we don't have the temperature yet, use some known files
if(!tempInfo){
tempInfo = this._findTemperatureFromFiles();
if(tempInfo.temp){
this.menu.box.get_children().forEach(function(c) {
c.destroy()
});
this.title=this._formatTemp(tempInfo.temp);
items.push('Current Temperature : '+this._formatTemp(tempInfo.temp));
if (tempInfo.crit)
items.push('Critical Temperature : '+this._formatTemp(tempInfo.crit));
}
}
this.statusLabel.set_text(this.title);
this.menu.box.get_children().forEach(function(c) {
c.destroy()
});
let section = new PopupMenu.PopupMenuSection("Temperature");
if (items.length>0){
let item;
for each (let itemText in items){
item = new PopupMenu.PopupMenuItem("");
item.addActor(new St.Label({
text:itemText,
style_class: "sm-label"
}));
section.addMenuItem(item);
}
}else{
let command=this.command;
let item = new PopupMenu.PopupMenuItem("");
item.addActor(new St.Label({
text:this.content,
style_class: "sm-label"
}));
item.connect('activate',function() {
Util.spawn(command);
});
section.addMenuItem(item);
}
this.menu.addMenuItem(section);
},
_createSectionForText: function(txt){
let section = new PopupMenu.PopupMenuSection("Temperature");
let item = new PopupMenu.PopupMenuItem("");
item.addActor(new St.Label({
text:txt,
style_class: "sm-label"
}));
section.addMenuItem(item);
return section;
},
_findTemperatureFromFiles: function(){
let info = new Array();
let temp_files = [
//hwmon for new 2.6.39, 3.x linux kernels
'/sys/class/hwmon/hwmon0/temp1_input',
'/sys/devices/platform/coretemp.0/temp1_input',
'/sys/bus/acpi/devices/LNXTHERM\:00/thermal_zone/temp',
'/sys/devices/virtual/thermal/thermal_zone0/temp',
'/sys/bus/acpi/drivers/ATK0110/ATK0110:00/hwmon/hwmon0/temp1_input',
//old kernels with proc fs
'/proc/acpi/thermal_zone/THM0/temperature',
'/proc/acpi/thermal_zone/THRM/temperature',
'/proc/acpi/thermal_zone/THR0/temperature',
'/proc/acpi/thermal_zone/TZ0/temperature',
//Debian Sid/Experimental on AMD-64
'/sys/class/hwmon/hwmon0/device/temp1_input'];
for each (let file in temp_files){
if(GLib.file_test(file,1<<4)){
//let f = Gio.file_new_for_path(file);
//f.read_async(0, null, function(source, result) {debug(source.read_finish(result).read())});
let temperature = GLib.file_get_contents(file);
if(temperature[0]) {
info['temp']= parseInt(temperature[1])/1000;
}
}
break;
}
let crit_files = ['/sys/devices/platform/coretemp.0/temp1_crit',
'/sys/bus/acpi/drivers/ATK0110/ATK0110:00/hwmon/hwmon0/temp1_crit',
//hwmon for new 2.6.39, 3.0 linux kernels
'/sys/class/hwmon/hwmon0/temp1_crit',
//Debian Sid/Experimental on AMD-64
'/sys/class/hwmon/hwmon0/device/temp1_crit'];
for each (let file in crit_files){
if(GLib.file_test(file,1<<4)){
let temperature = GLib.file_get_contents(file);
if(temperature[0]) {
info['crit']= parseInt(temperature[1])/1000;
}
}
}
return info;
},
_findTemperatureFromSensorsOutput: function(txt){
let senses_lines=txt.split("\n");
let line = '';
let type = '';
let s= new Array();
s['isa'] = new Array();
let n=0,c=0;
let f;
//iterate through each lines
for(let i = 0; i < senses_lines.length; i++) {
line = senses_lines[i];
//check for adapter
if (this._isAdapter(line)){
type=line.substr(9,line.length-9);
switch (type){
case 'ISA adapter':
//reset flag
f=0;
//starting from the next line, loop, also increase the outer line counter i
for (let j=i+1;;j++,i++){
//continue only if line exists and isn't adapter
if(senses_lines[j] && !this._isAdapter(senses_lines[j])){
if(senses_lines[j].substr(0,4)=='Core'){
senses_lines[j]=senses_lines[j].replace(/\s/g, "");
//get the core number
let k = senses_lines[j].substr(0,5);
//test if it's the first match for this adapter, if yes, initialize array
if (!f++){
s['isa'][++n]=new Array();
}
s['isa'][n][k]=new Array();
s['isa'][n][k]['temp']=parseFloat(senses_lines[j].substr(7,4));
s['isa'][n][k]['high']=this._getHigh(senses_lines[j]);
s['isa'][n][k]['crit']=this._getCrit(senses_lines[j]);
s['isa'][n][k]['hyst']=this._getHyst(senses_lines[j]);
c++;
}
}
else break;
}
break;
case 'Virtual device':
//starting from the next line, loop, also increase the outer line counter i
for (let j=i+1;;j++,i++){
//continue only if line exists and isn't adapter
if(senses_lines[j] && !this._isAdapter(senses_lines[j])){
if(senses_lines[j].substr(0,5)=='temp1'){
//remove all space characters
senses_lines[j]=senses_lines[j].replace(/\s/g, "");
s['virt'] = new Array();
s['virt']['temp']=parseFloat(senses_lines[j].substr(7,4));
s['virt']['high']=this._getHigh(senses_lines[j]);
s['virt']['crit']=this._getCrit(senses_lines[j]);
s['virt']['hyst']=this._getHyst(senses_lines[j]);
c++;
}
}
else break;
}
break;
case 'ACPI interface':
//starting from the next line, loop, also increase the outer line counter i
for (let j=i+1;;j++,i++){
//continue only if line exists and isn't adapter
if(senses_lines[j] && !this._isAdapter(senses_lines[j])){
if(senses_lines[j].substr(0,8)=='CPU Temp'){
senses_lines[j]=senses_lines[j].replace(/\s/g, "");
s['acpi'] = new Array();
s['acpi']['temp']=parseFloat(senses_lines[j].substr(16,4));
s['acpi']['high']=this._getHigh(senses_lines[j]);
s['acpi']['crit']=this._getCrit(senses_lines[j]);
s['acpi']['hyst']=this._getHyst(senses_lines[j]);
c++;
}
}
else break;
}
break;
case 'PCI adapter':
if (senses_lines[i-1].substr(0,6)=='k10tem' || senses_lines[i-1].substr(0,6)=='k8temp'){
//starting from the next line, loop, also increase the outer line counter i
for (let j=i+1;;j++,i++){
//continue only if line exists and isn't adapter
if(senses_lines[j] && !this._isAdapter(senses_lines[j])){
if(senses_lines[j].substr(0,5)=='temp1'){
senses_lines[j]=senses_lines[j].replace(/\s/g, "");
s['pci'] = new Array();
s['pci']['temp']=parseFloat(senses_lines[j].substr(7,4));
s['pci']['high']=this._getHigh(senses_lines[j]);
s['pci']['crit']=this._getCrit(senses_lines[j]);
s['pci']['hyst']=this._getHyst(senses_lines[j]);
//In some cases crit,hyst temp may be on next line
let nextLine=senses_lines[j+1].replace(/\s/g, "");
if (nextLine.substr(0,1)=='('){
if (!s['pci']['high']) s['pci']['high']=this._getHigh(nextLine);
if (!s['pci']['crit']) s['pci']['crit']=this._getCrit(nextLine);
if (!s['pci']['hyst']) s['pci']['hyst']=this._getHyst(nextLine);
}
c++;
}
}
else break;
}
}
break;
default:
break;
}
//uncomment next line to return temperature from only one adapter
//if (c==1) break;
}
}
return s;
},
_isAdapter: function(line){
if(line.substr(0, 8)=='Adapter:') {
return true;
}
return false;
},
_getHigh: function(t){
let r;
return (r=/high=\+(\d{1,3}.\d)/.exec(t))?parseFloat(r[1]):null;
},
_getCrit: function(t){
let r;
return (r=/crit=\+(\d{1,3}.\d)/.exec(t))?parseFloat(r[1]):null;
},
_getHyst: function(t){
let r;
return (r=/hyst=\+(\d{1,3}.\d)/.exec(t))?parseFloat(r[1]):null;
},
_toFahrenheit: function(c){
return ((9/5)*c+32).toFixed(1);
},
_getContent: function(c){
return c.toString()+"\u1d3cC / "+this._toFahrenheit(c).toString()+"\u1d3cF";
},
_formatTemp: function(t) {
//uncomment the next line to display temperature in Fahrenheit
//return this._toFahrenheit(t).toString()+"\u1d3cF";
return (Math.round(t*10)/10).toFixed(1).toString()+"\u1d3cC";
}
}
//for debugging
function debug(a){
global.log(a);
Util.spawn(['echo',a]);
}
function init() {
//do nothing
}
let indicator;
let event=null;
function enable() {
indicator = new CpuTemperature();
Main.panel.addToStatusArea('temperature', indicator);
}
function disable() {
indicator.destroy();
Mainloop.source_remove(event);
indicator = null;
}