-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
278 lines (209 loc) · 6.92 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
// Extension developed by Giulio Piemontese ([email protected])
// https://github.com/gpiemont
// Copyright 2012 Giulio Piemontese
// Licence: GPLv2
// Largely Based on Radeon Power Profile Manager
// (Francisco Pina Martins https://github.com/StuntsPT )
//
//
// This file is part of Nouveau Perflvl Switcher
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// If not, see <http://www.gnu.org/licenses/>.
const St = imports.gi.St;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Shell = imports.gi.Shell;
const Util = imports.misc.util;
const Clutter = imports.gi.Clutter;
function nv_log(message)
{
log("[nouveau pm switch] " + message + "\n");
global.log("[nouveau pm switch] " + message + "\n");
}
function nv_err(message)
{
global.logError("[nouveau pm switch] " + message + "\n");
}
// ProfileManager function
function ProfileManager(metadata)
{
// Main sysfs files (card 0,1)
this.meta = metadata;
this.file = [] ;
this.file[0] = "/sys/class/drm/card0/device/performance_level" ;
this.file[1] = "/sys/class/drm/card1/device/performance_level" ;
this.second_card = this.file[1] ;
// Paths to sysfs dirs
this.path = [] ;
this.path[0] = "/sys/class/drm/card0/device/" ;
this.path[1] = "/sys/class/drm/card1/device/" ;
this.perflvls = 0;
if (CheckForNVFile(this.file[0]))
{
this.perflvls = AvailPerflvls(this.path[0]);
this.cardno = 0;
} else if(this.perflvls == 0 && CheckForNVFile(this.second_card))
{
this.perflvls = AvailPerflvls(this.path[1]);
this.cardno = 1;
}
nv_log( this.perflvls + " available Performance Levels : ") ;
if ( this.perflvls == 0 )
{
nv_err("No performance levels are available on this card. Maybe not using nouveau?");
return 0;
}
this.Icon = Clutter.Texture.new_from_file(_(metadata.path + "/nun.svg"));
//this.Icon = [] ;
//for ( let pl = 0; pl < this.perflvls ; pl++) {
// this.Icon[pl] = Clutter.Texture.new_from_file(_(metadata.path+"/icons/nv_p" + pl + ".png"));
//}
this._init();
return 1;
}
// Prototype
ProfileManager.prototype =
{
__proto__: PanelMenu.SystemStatusButton.prototype,
_init: function()
{
PanelMenu.Button.prototype._init.call(this, St.Align.START);
this.temp = new St.BoxLayout();
this.temp.set_width(18);
this.temp.set_height(18);
this.actor.add_actor(this.temp);
this.actor.add_style_class_name('panel-status-button');
this.actor.has_tooltip = true;
this.perflvl = [];
this.currplvl = 0;
for ( let pl = 0; pl < this.perflvls ; pl++)
{
this.perflvl[pl] = _(this.path[0] + 'performance_level_' + pl) ;
nv_log(_("perflvl[" + pl + "]" + " : " + this.perflvl[pl]));
}
this._refresh();
},
_refresh: function()
{
let varFile = 0;
let tasksMenu = this.menu;
let temp = this.temp;
// Clear
tasksMenu.removeAll();
varFile = this.file[this.cardno];
// Sync
if (varFile)
{
let content = Shell.get_file_contents_utf8_sync(varFile).split("\n");
let profile = content[0].substring(content[0].indexOf(',') + 1);
this.currplvl = profile.trim() ;
// No icon update for now
//Icon = this.Icon[this.currplvl];
temp.add_actor(this.Icon,1);
// Performance level switch section
this.switchSection = new PopupMenu.PopupMenuSection();
//Create power profile changing buttons:
this.switchSection.powerbutton = [];
for ( let pl = 0; pl < this.perflvls ; pl++)
{
let entryname = "Performance Level " + pl ;
// Usually, the boot performance entry level
if ( pl == 1 )
entryname = entryname + " (boot)"
let item = new PopupMenu.PopupMenuItem(_(entryname));
let p = pl;
if ( p == this.currplvl)
item.setShowDot(true);
//Give the buttons an action:
item.connect('activate', function()
{
//temp.remove_actor(Icon);
changePerflvl(p,varFile);
});
this.switchSection.powerbutton[pl] = item ;
tasksMenu.addMenuItem(item);
}
}
},
enable: function()
{
Main.panel.addToStatusArea('perfswitch', this);
// Refresh menu
let fileM = Gio.file_new_for_path(this.file[this.cardno]);
this.monitor = fileM.monitor(Gio.FileMonitorFlags.NONE, null);
this.monitor.connect('changed', Lang.bind(this, this._refresh));
},
disable: function()
{
this.monitor.cancel();
this.destroy();
}
}
// Change performance level to 'n' in sysfs
function changePerflvl(n,file)
{
if (CheckForNVFile(file))
{
nv_log("Switching to performance level: " + n);
// Full command line (elevates privileges with polkit):
// pkexec /bin/sh -c "/bin/echo n > /sys/class/drm/cardX/performance_level"
//let [ret, argv] = GLib.shell_parse_argv(_("pkexec /bin/sh -c " + "\"" + " echo " + n + " > " + file + "\""));
let cmdline = _("pkexec /bin/sh -c " + "\"" + " echo " + n + " > " + file + "\"")
try {
Util.Spawn([cmdline]);
ret = n;
} catch (e) {
nv_err("Couldn't change perflvl as superuser : error = " e.toString());
ret = -1;
}
return ret;
}
}
// Checks if we're actually dealing with a nouveau card.
// Probably unneeded, but it tell us the card number so let's keep it for future purposes
function CheckForNVFile(filename)
{
if (GLib.file_test(filename, GLib.FileTest.EXISTS) && filename.indexOf("performance_level") != -1)
{
if (filename.indexOf("/card1/") != -1 )
{
nv_log("Working on secondary card");
return 2;
}
return 1;
}
nv_err(filename + " is not a nouveau sysfs knob");
return 0;
}
// How many performance levels do we have?
// XXX Actually tells us if cardX is nouveau
function AvailPerflvls(path)
{
// Check for at most 8 performance levels
// NOUVEAU_PM_MAX_LEVELS #defined to 8 in nouveau_perf.c
let p;
for ( p = 0 ; p < 8 ; p++ )
{
if ( GLib.file_test(path + '/performance_level_' + p, GLib.FileTest.EXISTS) != 1)
break;
}
return p;
}
// Init function
function init(metadata)
{
return new ProfileManager(metadata);
}