-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_event
executable file
·76 lines (65 loc) · 2.14 KB
/
power_event
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
#!/bin/bash
DOCK_DEVICES=("17ef:a393" "2109:0822")
BATTERY_THRESHOLD_PATH="/sys/class/power_supply/BAT0/charge_control_end_threshold"
OVERRIDE_PATH="/tmp/asus_bat_limit_override"
BATTERY_CAPACITY_PATH="/sys/class/power_supply/BAT0/capacity"
log() {
echo "POWER MANAGER: $1" | tee /dev/kmsg
}
log "Power event: {$1}"
# Get the current battery capacity
current_capacity=$(cat "$BATTERY_CAPACITY_PATH")
# If override file exists and battery isn't full, log and exit
if [[ -e $OVERRIDE_PATH && $current_capacity -ne 100 ]]; then
log "Override file found, and battery is not fully charged. Not changing the threshold."
exit
fi
if [[ -e $OVERRIDE_PATH && $current_capacity -eq 100 ]]; then
log "Override file found, and battery is fully charged. Removing the override file."
rm -f $OVERRIDE_PATH
fi
# Check if external power supply is connected
is_power_connected=0
for power_supply_status in /sys/class/power_supply/*/online; do
if [[ $(cat "$power_supply_status") -eq 1 ]]; then
is_power_connected=1
break
fi
done
# Check if the USB dock is connected
is_usb_dock_connected=0
for device in "${DOCK_DEVICES[@]}"; do
if lsusb | grep -q "$device"; then
is_usb_dock_connected=1
break
fi
done
# Check for multiple displays using xrandr
display_count=$(xrandr --query | grep " connected" | wc -l)
if [[ $display_count -gt 1 ]]; then
is_multidisplay=1
else
is_multidisplay=0
fi
# Determine if the system is docked
if [[ $is_usb_dock_connected -eq 1 || $is_multidisplay -eq 1 ]]; then
is_docked=1
else
is_docked=0
fi
# Decide and set battery charging threshold
if [[ $is_power_connected -eq 1 && $is_docked -eq 1 ]]; then
log "External power supply is connected and the system is docked."
echo 70 > $BATTERY_THRESHOLD_PATH
else
echo 100 > $BATTERY_THRESHOLD_PATH
if [[ $is_power_connected -ne 1 ]]; then
log "No external power supply is connected."
fi
if [[ $is_docked -ne 1 ]]; then
log "System is not docked."
fi
fi
# Report the final battery threshold setting
current_threshold=$(cat "$BATTERY_THRESHOLD_PATH")
log "Battery threshold is set to: $current_threshold"