Skip to content

Commit 54eb655

Browse files
committed
Update fix restoring user profile after battery event
Add battery_test for simulations of battery events and additional debug strings
1 parent 947c2b4 commit 54eb655

4 files changed

+114
-11
lines changed

extension.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,14 @@ const FrequencyIndicator = new Lang.Class({
111111
error (e.message);
112112
return;
113113
}
114-
this.on_power_state ();
114+
this.on_power_state (proxy.State, proxy.Percentage);
115115
if (save && first_boot && !guid_battery) this.launch_app ("--profile=user");
116116
first_boot = false;
117117
GLib.timeout_add (0, 8000, () => {
118-
powerID = this.power.connect ('g-properties-changed', this.on_power_state.bind (this));
118+
powerID = this.power.connect ('g-properties-changed', (o,a,b) => {
119+
debug ("power g-properties-changed: %s:%s:%s".format (o,a,b));
120+
this.on_power_state (this.power.State, this.power.Percentage);
121+
});
119122
});
120123
});
121124
},
@@ -150,22 +153,29 @@ const FrequencyIndicator = new Lang.Class({
150153
}
151154

152155
if ((key == LABEL_KEY) && !monitor_timeout) this.statusLabel.set_text (this.get_title ());
156+
157+
if ((key == "power-state") || (key == "power-percentage")) {
158+
debug ("power-state changed...");
159+
this.on_power_state (o.get_uint ("power-state"), o.get_double ("power-percentage"));
160+
}
153161
},
154162

155-
on_power_state: function () {
163+
on_power_state: function (state, percentage) {
156164
let id = eprofiles[1].guid;
157-
debug ("on_power_state: %d %.2f%%".format (this.power.State, this.power.Percentage));
158-
if (!id || id == guid_battery) return;
159-
if (this.power.State == 2) {
165+
//debug ("on_power_state: %s %s%%".format (this.power.State, this.power.Percentage));
166+
debug ("on_power_state: %s %s%%".format (state, percentage));
167+
if (!id) return;
168+
if (state == 2) {
160169
//on battery
161-
if (this.power.Percentage < eprofiles[1].percent) {
170+
if (id == guid_battery) return;
171+
if (percentage < eprofiles[1].percent) {
162172
this.schedule_profile ('--no-save --profile=' + id);
163173
guid_battery = id;
164174
}
165175
} else {
166176
//restoring prev state
167177
if (guid_battery == this.guid) return;
168-
if (this.power.Percentage >= eprofiles[1].percent) {
178+
if (percentage >= eprofiles[1].percent) {
169179
this.schedule_profile ('--profile=user');
170180
guid_battery = this.guid;
171181
}
@@ -179,16 +189,16 @@ const FrequencyIndicator = new Lang.Class({
179189

180190
schedule_profile: function (options) {
181191
if (scheduleID) this.unschedule_profile ();
182-
scheduleID = GLib.timeout_add (0, 4000, () => {
192+
scheduleID = GLib.timeout_add (0, 5000, () => {
183193
this.launch_app (options);
184194
scheduleID = 0;
185195
});
186196
},
187197

188198
launch_app: function (options) {
189199
let extra = "";
190-
if (Logger.debug_lvl == 2) extra = " --debug";
191-
else if (Logger.debug_lvl == 1) extra = " --verbose";
200+
/*if (Logger.debug_lvl == 2) extra = " --debug";
201+
else if (Logger.debug_lvl == 1) extra = " --verbose";*/
192202
options = options || "--extension";
193203
info ("launch_app " + options + extra);
194204
GLib.spawn_command_line_async ("%s %s".format (APP_PATH, options + extra));

schemas/gschemas.compiled

117 Bytes
Binary file not shown.

schemas/org.gnome.shell.extensions.cpufreq.gschema.xml

+12
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,17 @@
141141
<description>Window maximized</description>
142142
</key>
143143

144+
<key type="u" name="power-state">
145+
<default>0</default>
146+
<summary>Debug Power State</summary>
147+
<description>Emulate Power State</description>
148+
</key>
149+
150+
<key type="d" name="power-percentage">
151+
<default>0.0</default>
152+
<summary>Debug Power Percentage</summary>
153+
<description>Emulate Power Percentage</description>
154+
</key>
155+
144156
</schema>
145157
</schemalist>

tests/battery_test.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/gjs
2+
3+
/*
4+
* This is a part of CPUFreq Manager
5+
* Copyright (C) 2016-2019 konkor <konkor.github.io>
6+
*
7+
* Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
* You should have received a copy of the GNU General Public License along
10+
* with this program. If not, see <http://www.gnu.org/licenses/>.
11+
*/
12+
13+
const Gio = imports.gi.Gio;
14+
const GLib = imports.gi.GLib;
15+
16+
var APPDIR = getCurrentFile ()[1];
17+
imports.searchPath.unshift (APPDIR);
18+
const Convenience = imports.convenience;
19+
20+
let settings = Convenience.getSettings ();
21+
22+
23+
print ("battery");
24+
set_percentage (100);
25+
set_state (2);
26+
sleep (2000);
27+
28+
set_percentage (90);
29+
sleep (2000);
30+
set_percentage (80);
31+
sleep (2000);
32+
set_percentage (70);
33+
sleep (2000);
34+
set_percentage (50);
35+
sleep (2000);
36+
37+
set_state (4);
38+
sleep (2000);
39+
40+
set_percentage (70);
41+
sleep (2000);
42+
set_percentage (80);
43+
sleep (2000);
44+
set_percentage (90);
45+
sleep (2000);
46+
47+
set_percentage (100);
48+
set_state (0);
49+
50+
51+
function sleep (ms) {
52+
print ("sleeping " + ms + "ms");
53+
//GLib.usleep (ms);
54+
ms = ms || 0;
55+
let context = GLib.MainContext.default ();
56+
let d = Date.now ();
57+
while ((Date.now() - d) < ms) context.iteration (false);
58+
}
59+
60+
function set_state (state) {
61+
print ("setting state " + state);
62+
settings.set_uint ('power-state', state);
63+
}
64+
65+
function set_percentage (state) {
66+
print ("battery percentage " + state + "%");
67+
settings.set_double ('power-percentage', state);
68+
}
69+
70+
function getCurrentFile () {
71+
let stack = (new Error()).stack;
72+
let stackLine = stack.split("\n")[1];
73+
if (!stackLine)
74+
throw new Error ("Could not find current file");
75+
let match = new RegExp ("@(.+):\\d+").exec(stackLine);
76+
if (!match)
77+
throw new Error ("Could not find current file");
78+
let path = match[1];
79+
let file = Gio.File.new_for_path (path).get_parent();
80+
return [file.get_path(), file.get_parent().get_path(), file.get_basename()];
81+
}

0 commit comments

Comments
 (0)