-
Notifications
You must be signed in to change notification settings - Fork 0
/
batrat
executable file
·47 lines (39 loc) · 958 Bytes
/
batrat
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
#!/usr/bin/env sh
#
# batrat - Detect switches from AC to BAT or viceversa and print them to stdout
# Rodrigo Campos <rodrigo@sdfg.com.ar>
# Creates the var BATS with a list of the paths to the BAT dir in
# /sys/class/power_supply/ and also creates the var ADPS with a list of the
# paths to the ADP dir in /sys/class/power_supply/
load_power_supplies() {
for i in /sys/class/power_supply/*/type; do
i_dir=$(dirname $i)
i_type=$(cat $i)
if [ "$i_type" = "Battery" ]; then
BATS="$BATS $i_dir"
fi
if [ "$i_type" = "Mains" ]; then
ADPS="$ADPS $i_dir"
fi
done
}
# sets running_on
detect_curr_supply() {
load_power_supplies
running_on="BAT"
for ac in $ADPS; do
if [ $(cat ${ac}/online) -eq 1 ]; then
running_on="AC"
return
fi
done
}
supply_change() {
detect_curr_supply
echo $running_on
while inotifywait -qq -e access /sys/class/power_supply/*/online; do
detect_curr_supply
echo $running_on
done
}
supply_change