Skip to content

Commit 06e8319

Browse files
author
Thomas Venries
committed
feature: Add multixuser support
1 parent d76dddb commit 06e8319

File tree

3 files changed

+104
-87
lines changed

3 files changed

+104
-87
lines changed

Makefile

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,38 @@ SHELL := /bin/bash
22

33
UDEVDIR = $(DESTDIR)/etc/udev/rules.d
44
BINDIR = $(DESTDIR)/usr/local/bin
5-
ICONDIR = $(DESTDIR)/usr/share/icons
6-
7-
BAT_UDEV = 99-$(shell basename $(basename $(SRC)))-battery.rules
8-
PWR_UDEV = 98-$(shell basename $(basename $(SRC)))-power.rules
9-
10-
SRC = src/batify.sh
11-
12-
all:$(BAT_UDEV) $(PWR_UDEV)
13-
14-
$(BAT_UDEV):$(SRC)
15-
@echo -e 'ACTION=="change", KERNEL=="BAT0", \\' > $(BAT_UDEV)
16-
@echo -e 'SUBSYSTEM=="power_supply", \\' >> $(BAT_UDEV)
17-
@echo -e 'ATTR{status}=="Discharging", \\' >> $(BAT_UDEV)
18-
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(BAT_UDEV)
19-
@echo -e ''\''/usr/local/bin/batify.sh %k $$attr{capacity} none'\''"' >> $(BAT_UDEV)
20-
21-
$(PWR_UDEV):$(SRC)
22-
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(PWR_UDEV)
23-
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="0", ENV{POWER}="off", \\' >> $(PWR_UDEV)
24-
@echo -e 'OPTIONS+="last_rule", \\' >> $(PWR_UDEV)
25-
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(PWR_UDEV)
26-
@echo -e ''\''/usr/local/bin/batify.sh none none 0'\''"' >> $(PWR_UDEV)
27-
@echo >> $(PWR_UDEV)
28-
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(PWR_UDEV)
29-
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="1", ENV{POWER}="on", \\' >> $(PWR_UDEV)
30-
@echo -e 'OPTIONS+="last_rule", \\' >> $(PWR_UDEV)
31-
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(PWR_UDEV)
32-
@echo -e ''\''/usr/local/bin/batify.sh none none 1'\''"' >> $(PWR_UDEV)
5+
ICONDIR = $(DESTDIR)/usr/share/icons/batify
6+
7+
SRC = src/batify.sh
8+
UDEV = 99-batify.rules
9+
10+
all:$(UDEV)
11+
12+
$(UDEV):
13+
@echo -e 'ACTION=="change", KERNEL=="BAT0", \\' > $(UDEV)
14+
@echo -e 'SUBSYSTEM=="power_supply", \\' >> $(UDEV)
15+
@echo -e 'ATTR{status}=="Discharging", \\' >> $(UDEV)
16+
@echo -e 'RUN+="/usr/local/bin/batify.sh %k $$attr{capacity} none"\n' >> $(UDEV)
17+
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(UDEV)
18+
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="0", ENV{POWER}="off", \\' >> $(UDEV)
19+
@echo -e 'OPTIONS+="last_rule", \\' >> $(UDEV)
20+
@echo -e 'RUN+="/usr/local/bin/batify.sh none none 0"\n' >> $(UDEV)
21+
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(UDEV)
22+
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="1", ENV{POWER}="on", \\' >> $(UDEV)
23+
@echo -e 'OPTIONS+="last_rule", \\' >> $(UDEV)
24+
@echo -e 'RUN+="/usr/local/bin/batify.sh none none 1"' >> $(UDEV)
3325

3426
install: all
3527
mkdir -p $(UDEVDIR)
36-
mkdir -p $(ICONDIR)
3728
mkdir -p $(BINDIR)
38-
cp $(BAT_UDEV) $(UDEVDIR)
39-
cp $(PWR_UDEV) $(UDEVDIR)
40-
cp -r icons $(ICONDIR)/batify
29+
mkdir -p $(ICONDIR)
30+
cp icons/*.png $(ICONDIR)
31+
cp $(UDEV) $(UDEVDIR)/$(UDEV)
4132
cp $(SRC) $(BINDIR)/$(shell basename $(SRC))
4233

43-
clean:
44-
$(RM) $(BAT_UDEV)
45-
$(RM) $(PWR_UDEV)
46-
4734
uninstall:
48-
$(RM) -r $(ICONDIR)/batify
49-
$(RM) $(UDEVDIR)/$(BAT_UDEV)
50-
$(RM) $(UDEVDIR)/$(PWR_UDEV)
35+
$(RM) -r $(ICONDIR)
36+
$(RM) $(UDEVDIR)/$(UDEV)
5137
$(RM) $(BINDIR)/$(shell basename $(SRC))
5238

53-
.PHONY: install clean uninstall
39+
.PHONY: all install uninstall

README.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Batify
22
====
33
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/Ventto/batify/blob/master/LICENSE)
4-
[![Version](https://img.shields.io/badge/version-0.8-orange.svg?style=flat)](https://github.com/Ventto/batify/)
4+
[![Version](https://img.shields.io/badge/version-0.9-orange.svg?style=flat)](https://github.com/Ventto/batify/)
55
[![Language (Bash)](https://img.shields.io/badge/powered_by-Bash-brightgreen.svg)](https://www.gnu.org/software/bash/)
66

77
*Batify is an easy way (for single-user system) to set battery level warnings using [udev rules](https://wiki.archlinux.org/index.php/Udev) and [libnotify](https://wiki.archlinux.org/index.php/Desktop_notifications).*
@@ -33,43 +33,56 @@ $ sudo make install
3333
$ udevadm control --reload-rules
3434
```
3535

36-
# How does it work ?
36+
# Custom
3737

38-
Basically:
38+
*A batifyrc configuration file is comming soon.*
3939

40-
* The batify's udev rule is hooked "change/discharging battery" event
41-
* When it is triggered, the latter runs a script and the script runs *notify-send*.
40+
Useful pathes:
4241

43-
# Custom battery warnings
42+
* `/etc/udev/rules.d/99-batify.rules`
43+
* `/usr/local/bin/batify.sh`
44+
* `/usr/share/icons/batify`
4445

45-
* Edit `/usr/local/bin/batify.sh`:
46+
47+
## Custom battery warnings
48+
49+
* Edit `batify.sh`:
4650

4751
```bash
4852
case ${_bat_capacity} in
49-
[0-9]) ntf_lvl=critical; icon="critical" ;;
50-
1[0-5]) ntf_lvl=low; icon="low" ;;
53+
[0-9]) ntf_lvl=critical; icon="bat-critical" ;;
54+
1[0-5]) ntf_lvl=low; icon="bat-low" ;;
5155

5256
# Custom warnings (battery half level)
53-
5[0-3]) ntf_lvl=normal; icon="half" ;;
57+
5[0-3]) ntf_lvl=normal; icon="bat-half" ;;
5458
50) ... ;;
5559

5660
*) exit ;;
5761
esac
5862
```
5963

60-
# Custom icons
64+
## Custom plug warnings
6165

62-
There are basic icons with batify but you can obviously use yours.
66+
* Edit `batify.sh`:
6367

64-
* Edit `/usr/local/bin/batify.sh`
65-
* Set `icon_dir` variable
66-
* Use `icon` as you please
68+
```bash
69+
if [ "${_bat_plug}" == "1" ]; then
70+
ntf_lvl="normal"; icon="bat-plug"
71+
ntf_msg="Power: plugged in"
72+
else
73+
ntf_lvl="normal" ; icon="bat-unplug"
74+
ntf_msg="Power: unplugged"
75+
fi
76+
```
6777

68-
# FAQ
78+
## Custom icons
6979

70-
* Mysteriously, it does not work:
80+
* Edit `batify.sh`:
81+
82+
```bash
83+
ICON_DIR="/usr/share/icons/batify"
84+
#[...]
85+
icon_path="${ICON_DIR}/${icon}.png"
86+
```
7187

72-
Check on `DBUS_SESSION_BUS_ADDRESS` variable in `/usr/local/bin/batify.sh`
73-
Batify can not find the existing bus address on the X display or in a file in
74-
~/.dbus/session-bus/ to send a X message bus from a shell script.
7588

src/batify.sh

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,50 @@
2020
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
2121
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2222
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-
if [ -z "${DISPLAY}" ]; then
24-
exit
25-
fi
26-
27-
export XAUTHORITY="/home/${USER}/.Xauthority"
23+
xtty=$(cat /sys/class/tty/tty0/active)
24+
xuser=$(who | grep ${xtty} | head -n1 | cut -d' ' -f1)
2825

29-
for xclient in $(xlsclients | cut -d ' ' -f 3); do
30-
for pid in $(pgrep -u $USER -f $xclient); do
31-
env="/proc/${pid}/environ"
26+
if [ -z "${xuser}" ]; then
27+
echo "No user found from ${xtty}."
28+
exit 1
29+
fi
3230

33-
if [ -f "${env}" ]; then
34-
! [ -r "${env}" ] && continue
31+
xdisplay=$(ps -o command -p $(pgrep Xorg) | grep " vt${xtty:3:${#tty}}" | \
32+
grep -o ":[0-9]" | head -n 1)
3533

36-
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS $env | tr -d '\0' | \
37-
sed 's/DBUS_SESSION_BUS_ADDRESS=//g')
34+
if [ -z "${xdisplay}" ]; then
35+
echo "No X process found from ${xtty}."
36+
exit 1
37+
fi
3838

39-
if [ -n $dbus ]; then
40-
break
41-
fi
42-
fi
43-
done
39+
for pid in $(ps -u ${xuser} -o pid --no-headers); do
40+
env="/proc/${pid}/environ"
41+
display=$(grep -z "^DISPLAY=" ${env} | tr -d '\0' | cut -d '=' -f 2)
42+
if [ -n "${display}" ]; then
43+
dbus=$(grep -z "DBUS_SESSION_BUS_ADDRESS=" ${env} | tr -d '\0' | \
44+
sed 's/DBUS_SESSION_BUS_ADDRESS=//g')
45+
if [ -n ${dbus} ]; then
46+
xauth=$(grep -z "XAUTHORITY=" ${env} | tr -d '\0' | sed 's/XAUTHORITY=//g')
47+
break
48+
fi
49+
fi
4450
done
4551

46-
if [ -z "$dbus" ]; then
47-
echo "No dbus-daemon process found on the X display."
48-
exit 1
52+
if [ -z "${dbus}" ]; then
53+
echo "No session bus address found."
54+
exit 1
55+
elif [ -z "${xauth}" ]; then
56+
echo "No Xauthority found."
57+
exit 1
4958
fi
5059

51-
export DBUS_SESSION_BUS_ADDRESS=$dbus
52-
5360
_udev_params=( "$@" )
5461
_bat_name="${_udev_params[0]}"
5562
_bat_capacity="${_udev_params[1]}"
5663
_bat_plug="${_udev_params[2]}"
5764

65+
ICON_DIR="/usr/share/icons/batify"
66+
5867
if [ "${_bat_plug}" != "none" ]; then
5968
if [ "${_bat_plug}" == "1" ]; then
6069
ntf_lvl="normal"; icon="bat-plug"
@@ -65,14 +74,23 @@ if [ "${_bat_plug}" != "none" ]; then
6574
fi
6675
else
6776
case ${_bat_capacity} in
68-
[0-9]) ntf_lvl="critical"; icon="critical" ;;
69-
1[0-5]) ntf_lvl="low"; icon="low" ;;
77+
[0-9]) ntf_lvl="critical"; icon="bat-critical" ;;
78+
1[0-5]) ntf_lvl="low"; icon="bat-low" ;;
7079
*) exit ;;
7180
esac
7281
ntf_msg="[${_bat_name}] - Battery: ${_bat_capacity}%"
7382
fi
7483

75-
icon_dir="/usr/share/icons/batify"
76-
icon_path="${icon_dir}/${icon}.png"
84+
[ -f /usr/bin/su ] && su_path="/usr/bin/su"
85+
[ -f /bin/su ] && su_path="/bin/su" || su_path=
86+
87+
if [ -z "$su_path" ]; then
88+
echo "'su' command not found."
89+
exit 1
90+
fi
91+
92+
icon_path="${ICON_DIR}/${icon}.png"
7793

78-
/usr/bin/notify-send --hint=int:transient:1 -u ${ntf_lvl} -i "${icon_path}" "${ntf_msg}"
94+
DBUS_SESSION_BUS_ADDRESS=${dbus} DISPLAY=${xdisplay} XAUTHORITY=${xauth} \
95+
${su_path} ${xuser} -c \
96+
"/usr/bin/notify-send --hint=int:transient:1 -u ${ntf_lvl} -i \"${icon_path}\" \"${ntf_msg}\""

0 commit comments

Comments
 (0)