forked from agribu/i3-battery-warning
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathi3batwarn.sh
executable file
·96 lines (77 loc) · 2.88 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
#!/bin/sh
#############################################
# 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 [ -e "${LOCK_FILE}" ] ; then
pid="$(awk '{print $1}' "${LOCK_FILE}")"
ppid="$(awk '{print $2}' "${LOCK_FILE}")"
# validate contents of previous lock file
vpid="${pid:-"0"}"
vppid="${ppid:-"0"}"
if [ "${vpid}" -lt '2' ] || [ "${vppid}" -lt '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
if ps -f -p "${pid}" --no-headers | grep -q "${ppid}" ; 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="$(echo '/sys/class/power_supply/BAT'?)"
# get battery status
STAT="$(cat "${BATTERY}/status")"
# get remaining energy value
REM="$(grep -E "POWER_SUPPLY_(CHARGE|ENERGY)_NOW" "${BATTERY}/uevent" | cut -d '=' -f2)"
# get full energy value
FULL="$(grep -E "POWER_SUPPLY_(CHARGE|ENERGY)_FULL_DESIGN" "${BATTERY}/uevent" | cut -d '=' -f2)"
# get current energy value in percent
PERCENT="$((REM * 100 / FULL))"
# set error message
MESSAGE="Low battery warning, find charger"
# set energy limit in percent, where warning should be displayed
LIMIT="15"
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 "${LIMIT}" ] && [ "${STAT}" = "Discharging" ] ; then
#chek if nagbarfile is empty: else open new - to avoid multiples
if [ ! -s "${NAGBARPIDFILE}" ] ; then
/usr/bin/i3-nagbar -m "${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-nagbar -m "${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 "${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