-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower-profiles.sh
More file actions
executable file
·76 lines (53 loc) · 2.73 KB
/
Copy pathpower-profiles.sh
File metadata and controls
executable file
·76 lines (53 loc) · 2.73 KB
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
#!/usr/bin/env bash
# The battery percentage required for 'balanced' mode to be used when charging.
# This value should be lower than 'THRESHOLD_FOR_PERFORMANCE_MODE'.
# Set this to '0' to only use 'balanced' mode when charging until past 'THRESHOLD_FOR_PERFORMANCE_MODE'.
THRESHOLD_FOR_BALANCED_MODE=0
# The battery percentage required for 'performance' mode to be used when charging (if available).
# This value should be higher than 'THRESHOLD_FOR_BALANCED_MODE'.
# Set this to '100' to only use 'balanced' mode when charging past 'THRESHOLD_FOR_BALANCED_MODE'.
THRESHOLD_FOR_PERFORMANCE_MODE=100
# Note that 'performance' mode is not available on all devices. Run 'powerprofilesctl list' to see the available modes for the device.
# If 'performance' mode is not available on the device, this script will (hopefully) only use 'balanced' and 'power-saving' mode without issues.
# How frequently, in seconds, this script should check if the charging/discharging state has changed.
SLEEP_INTERVAL=15
check_dependencies() {
for dependency in powerprofilesctl upower grep awk; do
if ! command -v "$dependency" >/dev/null 2>&1; then
exit 1
fi
done
}
battery_threshold_met() {
threshold="$1"
battery_percentage=$(upower --show-info /org/freedesktop/UPower/devices/battery_BAT0 | awk '/percentage:/ {print $2}')
battery_percentage="${battery_percentage%\%}" # Remove trailing '%'
if [ $battery_percentage -gt $threshold ]; then
return 0
else
return 1
fi
}
check_dependencies
powerprofilesctl list | grep -q "performance"
performance_mode_available=$?
ppd_mode=$(powerprofilesctl get)
last_used_mode="$ppd_mode"
# TODO: Prompt user with YAD if user changes charging state after manually changing mode
while [[ "$last_used_mode" == "$ppd_mode" ]]; do # Exit loop if mode is changed manually
charging_state=$(upower --show-info /org/freedesktop/UPower/devices/battery_BAT0 | awk '/state:/ {print $2}')
if ([[ "$charging_state" == "discharging" ]] || ! battery_threshold_met "$THRESHOLD_FOR_BALANCED_MODE") &&
[[ "$ppd_mode" != "power-saver" ]]; then
powerprofilesctl set "power-saver"
last_used_mode="power-saver"
elif [[ "$charging_state" == "charging" && "$performance_mode_available" -eq 0 && "$ppd_mode" != "performance" ]] &&
battery_threshold_met "$THRESHOLD_FOR_PERFORMANCE_MODE"; then
powerprofilesctl set "performance"
last_used_mode="performance"
elif [[ "$charging_state" == "charging" && "$ppd_mode" != "balanced" ]] && battery_threshold_met "$THRESHOLD_FOR_BALANCED_MODE"; then
powerprofilesctl set "balanced"
last_used_mode="balanced"
fi
sleep $SLEEP_INTERVAL
ppd_mode=$(powerprofilesctl get)
done