-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.js
291 lines (248 loc) · 8.89 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
// This extension was developed by :
// * Francisco Pina Martins https://github.com/StuntsPT
//
// Licence: GPLv2
//
// Copyright 2012 Francisco Pina Martins
//
// This file is part of Radeon Power Profile Manager.
//
// Radeon Power Profile Manager 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.
// Radeon Power Profile Manager 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
// along with Radeon Power Profile Manager. 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 Clutter = imports.gi.Clutter
let meta;
let profilemanager;
let Icon = "";
// ProfileManager function
function ProfileManager(metadata)
{
//Stub files for testing:
//this.profile0 = metadata.path + "/stubs/power_profile";
//this.powerMethod = metadata.path + "/stubs/power_method";
//this.profile1 = metadata.path + "/stubs/power_profile2";
//this.powerMethod2 = metadata.path + "/stubs/power_method";
//Define variables for the sysfs files:
this.profile0 = "/sys/class/drm/card0/device/power_profile";
this.powerMethod0 = "/sys/class/drm/card0/device/power_method";
this.profile1 = "/sys/class/drm/card1/device/power_profile";
this.powerMethod1 = "/sys/class/drm/card1/device/power_method";
this.foundCard = 0;
//Test if the power_method file is set for profile:
if (CheckForFile(this.powerMethod0) == 1)
{
CheckMethod(this.powerMethod0);
this.foundCard = 1;
}
//Test if a second card is present and if it is, define it:
if (CheckForFile(this.profile1) == 1)
{
if (CheckForFile(this.powerMethod1) == 1)
{
CheckMethod(this.powerMethod1);
this.foundCard = 1;
}
}
if (this.foundCard != 1)
{
global.logError("Radeon Power Profile Manager: No cards found!");
}
//Set the icons:
this.LowPowerIcon=Clutter.Texture.new_from_file(metadata.path+"/low.svg");
this.MidPowerIcon=Clutter.Texture.new_from_file(metadata.path+"/mid.svg");
this.HighPowerIcon=Clutter.Texture.new_from_file(metadata.path+"/high.svg");
this.AutoPowerIcon=Clutter.Texture.new_from_file(metadata.path+"/auto.svg");
this._init();
}
// Prototype
ProfileManager.prototype =
{
__proto__: PanelMenu.Button.prototype,
_init: function()
{
PanelMenu.Button.prototype._init.call(this, St.Align.START);
this.temp = new St.BoxLayout();
this.temp.set_width(24);
this.temp.set_height(24);
this.actor.add_actor(this.temp);
this.actor.add_style_class_name('panel-status-button');
this.actor.has_tooltip = false;
this._refresh();
},
_refresh: function()
{
let varfile0 = this.profile0;
let varfile1 = this.profile1;
let tasksMenu = this.menu;
let temp = this.temp;
let content = 0;
// Clear
tasksMenu.removeAll();
// Sync
if (CheckForFile(this.profile0) == 1)
{
content = Shell.get_file_contents_utf8_sync(this.profile0);
}
else
{
content = Shell.get_file_contents_utf8_sync(this.profile1);
}
let message = "Currently on '" + content.trim() + "' profile";
let item = new PopupMenu.PopupMenuItem(_(message));
tasksMenu.addMenuItem(item);
if (Icon != "") {temp.remove_actor(Icon);}
if (content.trim() == "low")
{
Icon = this.LowPowerIcon;
}
else if (content.trim() == "mid")
{
Icon = this.MidPowerIcon;
}
else if (content.trim() == "auto")
{
Icon = this.AutoPowerIcon;
}
else
{
Icon = this.HighPowerIcon;
}
temp.add_actor(Icon,1);
// Separator
this.Separator = new PopupMenu.PopupSeparatorMenuItem();
tasksMenu.addMenuItem(this.Separator);
// Bottom section
let bottomSection = new PopupMenu.PopupMenuSection();
//Create power profile changing buttons:
let lowpowerbutton = new PopupMenu.PopupMenuItem(_("Set profile to 'low'"));
tasksMenu.addMenuItem(lowpowerbutton);
let midpowerbutton = new PopupMenu.PopupMenuItem(_("Set profile to 'mid'"));
tasksMenu.addMenuItem(midpowerbutton);
let highpowerbutton = new PopupMenu.PopupMenuItem(_("Set profile to 'high'"));
tasksMenu.addMenuItem(highpowerbutton);
let autopowerbutton = new PopupMenu.PopupMenuItem(_("Set profile to 'auto'"));
tasksMenu.addMenuItem(autopowerbutton);
//Give the buttons an action:
lowpowerbutton.connect('activate',function()
{
changeProfile("low",varfile0);
changeProfile("low",varfile1);
});
midpowerbutton.connect('activate',function()
{
changeProfile("mid",varfile0);
changeProfile("mid",varfile1);
});
highpowerbutton.connect('activate',function()
{
changeProfile("high",varfile0);
changeProfile("high",varfile1);
});
autopowerbutton.connect('activate',function()
{
changeProfile("auto",varfile0);
changeProfile("auto",varfile1);
});
},
_enable: function()
{
// Refresh menu
let fileM = Gio.file_new_for_path(this.profile0);
this.monitor1 = fileM.monitor(Gio.FileMonitorFlags.NONE, null);
this.monitor1.connect('changed', Lang.bind(this, this._refresh));
// Refresh second card menu
let fileM2 = Gio.file_new_for_path(this.profile1);
this.monitor2 = fileM2.monitor(Gio.FileMonitorFlags.NONE, null);
this.monitor2.connect('changed', Lang.bind(this, this._refresh));
},
_disable: function()
{
this.monitor1.cancel();
this.monitor2.cancel();
}
}
//Change power profile "text" in sysfs file "file". Will use polkit to
//elevate privileges if the user does not have write access to the
//required sysfs file.
function changeProfile(text,file)
{
if (CheckForFile(file) == 1)
{
if (GLib.access(file, 2) == 0)
{
let content = Shell.get_file_contents_utf8_sync(file);
content = text
let f = Gio.file_new_for_path(file);
let out = f.replace(null, false, Gio.FileCreateFlags.NONE, null);
Shell.write_string_to_stream (out, content);
}
else
{
let [result, argv] = GLib.shell_parse_argv(_("pkexec /bin/bash -c \" echo '" + text + "' > " + file + "\""));
if (result)
{
try {
[result, pid] = GLib.spawn_async_with_pipes(null, argv, null, GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null, null);
} catch (e) {
global.logError("Radeon Power Profile Manager: Failed to change profile with elevated privileges using polkit.");
}
}
return result;
}
}
return -1;
}
function CheckForFile(filename)
{
//Checks for the existance of a file
if (GLib.file_test(filename, GLib.FileTest.EXISTS))
{
return 1;
}
else
{
return 0;
}
}
function CheckMethod(filename)
{
//Will check if the current power_method is set to 'profile'
let method = Shell.get_file_contents_utf8_sync(filename);
if (method.trim() != "profile")
{
global.logError("Radeon Power Profile Manager: " + filename + " is not set for 'profile'. Please change this.");
}
}
// Init function
function init(metadata)
{
meta = metadata;
}
function enable()
{
profilemanager = new ProfileManager(meta);
profilemanager._enable();
Main.panel.addToStatusArea('profilemanager', profilemanager);
}
function disable()
{
profilemanager._disable();
profilemanager.destroy();
profilemanager = null;
}