forked from stagnation/i3-battery-warning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi3batwarn.sh
executable file
·103 lines (83 loc) · 3.03 KB
/
i3batwarn.sh
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
#!/bin/bash
#############################################
# This is a simple battery warning script. #
# It uses i3's nagbar to display warnings. #
# #
# @author agribu #
#############################################
# lock file location
export LOCK_FILE=/tmp/battery_state.lock
# check if another copy is running
if [[ -a $LOCK_FILE ]]; then
pid=$(cat $LOCK_FILE | awk '{print $1}')
ppid=$(cat $LOCK_FILE | awk '{print $2}')
# validate contents of previous lock file
vpid=${pid:-"0"}
vppid=${ppid:-"0"}
if (( $vpid < 2 || $vppid < 2 )); then
# corrupt lock file $LOCK_FILE ... Exiting
cp -f $LOCK_FILE ${LOCK_FILE}.`date +%Y%m%d%H%M%S`
exit
fi
# check if ppid matches pid
ps -f -p $pid --no-headers | grep $ppid >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
# another copy of script running with process id $pid
exit
else
# bogus lock file found, removing
rm -f $LOCK_FILE >/dev/null
fi
fi
pid=$$
ps -f -p $pid --no-headers | awk '{print $2,$3}' > $LOCK_FILE
# starting with process id $pid
# set Battery
BATTERY=$(ls /sys/class/power_supply/ | grep '^BAT')
# set full path
ACPI_PATH="/sys/class/power_supply/$BATTERY"
# get battery status
STAT=$(cat $ACPI_PATH/status)
# get remaining energy value
REM=`grep "POWER_SUPPLY_ENERGY_NOW" $ACPI_PATH/uevent | cut -d= -f2`
# get full energy value
FULL=`grep "POWER_SUPPLY_ENERGY_FULL_DESIGN" $ACPI_PATH/uevent | cut -d= -f2`
# get current energy value in percent
PERCENT=`echo $(( $REM * 100 / $FULL ))`
# set error message
MESSAGE="Low battery warning, find charger"
# set energy limit in percent, where warning should be displayed
LIMIT="10"
I3BAT_TMPDIR=$(mktemp --directory --tmpdir i3batwarn.XXX)
NAGBARPIDFILE="$I3BAT_TMPDIR/nagbarpid_file"
# show warning if energy limit in percent is less then user set limit and
# if battery is discharging
if [ $PERCENT -le "$(echo $LIMIT)" ] && [ "$STAT" == "Discharging" ]; then
#chek if nagbarfile is empty: else open new - to avoid multiples
if [ ! -s $NAGBARPIDFILE ] ; then
/usr/bin/i3-msg fullscreen disable &
/usr/bin/i3-nagbar -m "$(echo $MESSAGE)" &
echo $! > $NAGBARPIDFILE
elif ps -e | grep $(cat $NAGBARPIDFILE) | grep "i3-nagbar"; then
echo "pidfile in order, nothing to do"
else
rm $NAGBARPIDFILE
/usr/bin/i3-msg fullscreen disable &
/usr/bin/i3-nagbar -m "$(echo $MESSAGE)" &
echo $! > $NAGBARPIDFILE
fi #else if, nagbarpid points to something else create new.
fi
#warning, if the nagbar is closed manually the pidfile might not be emptied properly
#for safety the charging requirement below is relaxed, if you use the nagbar for other reasons
#it might get closed accidentaly by this
if [ $PERCENT -gt "$(echo $LIMIT)" ] || [ "$STAT" == "Charging" ]
then
if [ -s $NAGBARPIDFILE ] ; then
if ps -e | grep $(cat $NAGBARPIDFILE) | grep "i3-nagbar"; then
kill $(cat $NAGBARPIDFILE)
rm $NAGBARPIDFILE
else
rm $NAGBARPIDFILE
fi
fi
fi