forked from eliapasquali/power-profile-switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
210 lines (175 loc) · 6.82 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
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const { loadInterfaceXML } = imports.misc.fileUtils;
const Main = imports.ui.main;
const { Gio, GLib, UPowerGlib:UPower } = imports.gi;
let settings, client, device;
// Checks for changes in settings, must be disconnected in disable
let batteryPercentageWatcher;
let ACDefaultWatcher, batteryDefaultWatcher, platformProfileWatcher;
let batteryThreshold, ACDefault, batteryDefault, activeProfile, perfDebounceTimerId;
let powerManagerProxy, powerManagerCancellable, batteryThresholdWatcher;
let powerProfilesProxy, powerProfilesCancellable, powerProfileWatcher;
const UPOWER_BUS_NAME = 'org.freedesktop.UPower';
const UPOWER_OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
const DisplayDeviceInterface = '<node> \
<interface name="org.freedesktop.UPower.Device"> \
<property name="Type" type="u" access="read"/> \
<property name="State" type="u" access="read"/> \
<property name="Percentage" type="d" access="read"/> \
<property name="TimeToEmpty" type="x" access="read"/> \
<property name="TimeToFull" type="x" access="read"/> \
<property name="IsPresent" type="b" access="read"/> \
<property name="IconName" type="s" access="read"/> \
</interface> \
</node>';
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
const POWER_PROFILES_BUS_NAME = 'net.hadess.PowerProfiles';
const POWER_PROFILES_OBJECT_PATH = '/net/hadess/PowerProfiles';
const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles');
const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
const switchProfile = (profile) => {
if (profile === activeProfile) {
return;
}
try {
Gio.DBus.system.call(
POWER_PROFILES_BUS_NAME,
POWER_PROFILES_OBJECT_PATH,
'org.freedesktop.DBus.Properties',
'Set',
new GLib.Variant('(ssv)', [
'net.hadess.PowerProfiles',
'ActiveProfile',
new GLib.Variant('s', profile)
]),
null,
Gio.DBusCallFlags.NONE,
-1,
null,
(connection, res) => {
try {
connection.call_finish(res);
} catch (e) {
logError(e);
}
}
);
} catch (e) {
logError(e);
}
}
const checkProfile = () => {
getDefaults();
let nextProfile = "balanced";
if (
powerManagerProxy.State === UPower.DeviceState.UNKNOWN ||
client.on_battery === undefined ||
device.percentage === undefined
) {
nextProfile = "balanced";
} else if(
device.state === UPower.DeviceState.PENDING_DISCHARGE ||
device.state === UPower.DeviceState.DISCHARGING
) {
nextProfile = device.percentage >= batteryThreshold ? batteryDefault : "power-saver"
}
else {
nextProfile = ACDefault;
}
switchProfile(nextProfile);
}
const getDefaults = () => {
ACDefault = settings.get_string("ac");
batteryDefault = settings.get_string("bat");
batteryThreshold = settings.get_int("threshold");
}
function init() {
ExtensionUtils.initTranslations(Me.metadata.uuid);
}
function enable() {
client = UPower.Client.new();
device = client.get_display_device();
settings = ExtensionUtils.getSettings(
"org.gnome.shell.extensions.power-profile-switcher"
);
batteryPercentageWatcher = settings.connect(
"changed::threshold",
checkProfile
);
ACDefaultWatcher = settings.connect(
"changed::ac",
checkProfile
);
batteryDefaultWatcher = settings.connect(
"changed::bat",
checkProfile
);
powerManagerCancellable = new Gio.Cancellable();
powerManagerProxy = new PowerManagerProxy(Gio.DBus.system, UPOWER_BUS_NAME, UPOWER_OBJECT_PATH,
(proxy, error) => {
if (error) {
logError(error.message);
return;
}
batteryThresholdWatcher = powerManagerProxy.connect('g-properties-changed', checkProfile);
checkProfile();
}, powerManagerCancellable);
powerProfilesCancellable = new Gio.Cancellable();
powerProfilesProxy = new PowerProfilesProxy(Gio.DBus.system, POWER_PROFILES_BUS_NAME, POWER_PROFILES_OBJECT_PATH,
(proxy, error) => {
if (error) {
logError(error.message);
} else {
powerProfileWatcher = powerProfilesProxy.connect('g-properties-changed', (p, properties) => {
const payload = properties?.deep_unpack();
if (payload?.ActiveProfile) {
activeProfile = payload?.ActiveProfile?.unpack();
if (perfDebounceTimerId) {
GLib.source_remove(perfDebounceTimerId);
perfDebounceTimerId = null;
}
}
const isOnPowerSupply = device?.power_supply ||
device?.state !== UPower.DeviceState.PENDING_DISCHARGE ||
device?.state !== UPower.DeviceState.DISCHARGING;
if (isOnPowerSupply && payload?.PerformanceDegraded) {
try {
const reason = payload?.PerformanceDegraded?.unpack();
if (reason === 'lap-detected') {
perfDebounceTimerId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
checkProfile();
perfDebounceTimerId = null;
return GLib.SOURCE_REMOVE;
});
}
else if (reason) {
log(`ActiveProfile: ${activeProfile}, PerformanceDegraded: ${reason}`);
}
}
catch (e) {
logError(e)
}
}
});
}
}, powerProfilesCancellable);
}
function disable() {
settings.disconnect(batteryPercentageWatcher);
settings.disconnect(ACDefaultWatcher);
settings.disconnect(batteryDefaultWatcher);
powerManagerProxy.disconnect(batteryThresholdWatcher);
powerManagerCancellable.cancel();
powerProfilesProxy.disconnect(powerProfileWatcher);
powerProfilesCancellable.cancel();
if (perfDebounceTimerId) {
GLib.source_remove(perfDebounceTimerId);
perfDebounceTimerId = null;
}
settings = null;
client = null;
device = null;
activeProfile = null;
switchProfile("balanced");
}