Skip to content

Commit

Permalink
feature: Add multixuser support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Venries committed Jan 9, 2017
1 parent d76dddb commit 06e8319
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 87 deletions.
66 changes: 26 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,38 @@ SHELL := /bin/bash

UDEVDIR = $(DESTDIR)/etc/udev/rules.d
BINDIR = $(DESTDIR)/usr/local/bin
ICONDIR = $(DESTDIR)/usr/share/icons

BAT_UDEV = 99-$(shell basename $(basename $(SRC)))-battery.rules
PWR_UDEV = 98-$(shell basename $(basename $(SRC)))-power.rules

SRC = src/batify.sh

all:$(BAT_UDEV) $(PWR_UDEV)

$(BAT_UDEV):$(SRC)
@echo -e 'ACTION=="change", KERNEL=="BAT0", \\' > $(BAT_UDEV)
@echo -e 'SUBSYSTEM=="power_supply", \\' >> $(BAT_UDEV)
@echo -e 'ATTR{status}=="Discharging", \\' >> $(BAT_UDEV)
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(BAT_UDEV)
@echo -e ''\''/usr/local/bin/batify.sh %k $$attr{capacity} none'\''"' >> $(BAT_UDEV)

$(PWR_UDEV):$(SRC)
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(PWR_UDEV)
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="0", ENV{POWER}="off", \\' >> $(PWR_UDEV)
@echo -e 'OPTIONS+="last_rule", \\' >> $(PWR_UDEV)
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(PWR_UDEV)
@echo -e ''\''/usr/local/bin/batify.sh none none 0'\''"' >> $(PWR_UDEV)
@echo >> $(PWR_UDEV)
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(PWR_UDEV)
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="1", ENV{POWER}="on", \\' >> $(PWR_UDEV)
@echo -e 'OPTIONS+="last_rule", \\' >> $(PWR_UDEV)
@echo -n 'RUN+="$(shell whereis su | cut -d ' ' -f 2) $(USER) -c ' >> $(PWR_UDEV)
@echo -e ''\''/usr/local/bin/batify.sh none none 1'\''"' >> $(PWR_UDEV)
ICONDIR = $(DESTDIR)/usr/share/icons/batify

SRC = src/batify.sh
UDEV = 99-batify.rules

all:$(UDEV)

$(UDEV):
@echo -e 'ACTION=="change", KERNEL=="BAT0", \\' > $(UDEV)
@echo -e 'SUBSYSTEM=="power_supply", \\' >> $(UDEV)
@echo -e 'ATTR{status}=="Discharging", \\' >> $(UDEV)
@echo -e 'RUN+="/usr/local/bin/batify.sh %k $$attr{capacity} none"\n' >> $(UDEV)
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(UDEV)
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="0", ENV{POWER}="off", \\' >> $(UDEV)
@echo -e 'OPTIONS+="last_rule", \\' >> $(UDEV)
@echo -e 'RUN+="/usr/local/bin/batify.sh none none 0"\n' >> $(UDEV)
@echo -e 'SUBSYSTEM=="power_supply", ACTION=="change", \\' >> $(UDEV)
@echo -e 'ENV{POWER_SUPPLY_ONLINE}=="1", ENV{POWER}="on", \\' >> $(UDEV)
@echo -e 'OPTIONS+="last_rule", \\' >> $(UDEV)
@echo -e 'RUN+="/usr/local/bin/batify.sh none none 1"' >> $(UDEV)

install: all
mkdir -p $(UDEVDIR)
mkdir -p $(ICONDIR)
mkdir -p $(BINDIR)
cp $(BAT_UDEV) $(UDEVDIR)
cp $(PWR_UDEV) $(UDEVDIR)
cp -r icons $(ICONDIR)/batify
mkdir -p $(ICONDIR)
cp icons/*.png $(ICONDIR)
cp $(UDEV) $(UDEVDIR)/$(UDEV)
cp $(SRC) $(BINDIR)/$(shell basename $(SRC))

clean:
$(RM) $(BAT_UDEV)
$(RM) $(PWR_UDEV)

uninstall:
$(RM) -r $(ICONDIR)/batify
$(RM) $(UDEVDIR)/$(BAT_UDEV)
$(RM) $(UDEVDIR)/$(PWR_UDEV)
$(RM) -r $(ICONDIR)
$(RM) $(UDEVDIR)/$(UDEV)
$(RM) $(BINDIR)/$(shell basename $(SRC))

.PHONY: install clean uninstall
.PHONY: all install uninstall
53 changes: 33 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Batify
====
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/Ventto/batify/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/version-0.8-orange.svg?style=flat)](https://github.com/Ventto/batify/)
[![Version](https://img.shields.io/badge/version-0.9-orange.svg?style=flat)](https://github.com/Ventto/batify/)
[![Language (Bash)](https://img.shields.io/badge/powered_by-Bash-brightgreen.svg)](https://www.gnu.org/software/bash/)

*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).*
Expand Down Expand Up @@ -33,43 +33,56 @@ $ sudo make install
$ udevadm control --reload-rules
```

# How does it work ?
# Custom

Basically:
*A batifyrc configuration file is comming soon.*

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

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

* Edit `/usr/local/bin/batify.sh`:

## Custom battery warnings

* Edit `batify.sh`:

```bash
case ${_bat_capacity} in
[0-9]) ntf_lvl=critical; icon="critical" ;;
1[0-5]) ntf_lvl=low; icon="low" ;;
[0-9]) ntf_lvl=critical; icon="bat-critical" ;;
1[0-5]) ntf_lvl=low; icon="bat-low" ;;

# Custom warnings (battery half level)
5[0-3]) ntf_lvl=normal; icon="half" ;;
5[0-3]) ntf_lvl=normal; icon="bat-half" ;;
50) ... ;;

*) exit ;;
esac
```

# Custom icons
## Custom plug warnings

There are basic icons with batify but you can obviously use yours.
* Edit `batify.sh`:

* Edit `/usr/local/bin/batify.sh`
* Set `icon_dir` variable
* Use `icon` as you please
```bash
if [ "${_bat_plug}" == "1" ]; then
ntf_lvl="normal"; icon="bat-plug"
ntf_msg="Power: plugged in"
else
ntf_lvl="normal" ; icon="bat-unplug"
ntf_msg="Power: unplugged"
fi
```

# FAQ
## Custom icons

* Mysteriously, it does not work:
* Edit `batify.sh`:

```bash
ICON_DIR="/usr/share/icons/batify"
#[...]
icon_path="${ICON_DIR}/${icon}.png"
```

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

72 changes: 45 additions & 27 deletions src/batify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,50 @@
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if [ -z "${DISPLAY}" ]; then
exit
fi

export XAUTHORITY="/home/${USER}/.Xauthority"
xtty=$(cat /sys/class/tty/tty0/active)
xuser=$(who | grep ${xtty} | head -n1 | cut -d' ' -f1)

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

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

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

if [ -n $dbus ]; then
break
fi
fi
done
for pid in $(ps -u ${xuser} -o pid --no-headers); do
env="/proc/${pid}/environ"
display=$(grep -z "^DISPLAY=" ${env} | tr -d '\0' | cut -d '=' -f 2)
if [ -n "${display}" ]; then
dbus=$(grep -z "DBUS_SESSION_BUS_ADDRESS=" ${env} | tr -d '\0' | \
sed 's/DBUS_SESSION_BUS_ADDRESS=//g')
if [ -n ${dbus} ]; then
xauth=$(grep -z "XAUTHORITY=" ${env} | tr -d '\0' | sed 's/XAUTHORITY=//g')
break
fi
fi
done

if [ -z "$dbus" ]; then
echo "No dbus-daemon process found on the X display."
exit 1
if [ -z "${dbus}" ]; then
echo "No session bus address found."
exit 1
elif [ -z "${xauth}" ]; then
echo "No Xauthority found."
exit 1
fi

export DBUS_SESSION_BUS_ADDRESS=$dbus

_udev_params=( "$@" )
_bat_name="${_udev_params[0]}"
_bat_capacity="${_udev_params[1]}"
_bat_plug="${_udev_params[2]}"

ICON_DIR="/usr/share/icons/batify"

if [ "${_bat_plug}" != "none" ]; then
if [ "${_bat_plug}" == "1" ]; then
ntf_lvl="normal"; icon="bat-plug"
Expand All @@ -65,14 +74,23 @@ if [ "${_bat_plug}" != "none" ]; then
fi
else
case ${_bat_capacity} in
[0-9]) ntf_lvl="critical"; icon="critical" ;;
1[0-5]) ntf_lvl="low"; icon="low" ;;
[0-9]) ntf_lvl="critical"; icon="bat-critical" ;;
1[0-5]) ntf_lvl="low"; icon="bat-low" ;;
*) exit ;;
esac
ntf_msg="[${_bat_name}] - Battery: ${_bat_capacity}%"
fi

icon_dir="/usr/share/icons/batify"
icon_path="${icon_dir}/${icon}.png"
[ -f /usr/bin/su ] && su_path="/usr/bin/su"
[ -f /bin/su ] && su_path="/bin/su" || su_path=

if [ -z "$su_path" ]; then
echo "'su' command not found."
exit 1
fi

icon_path="${ICON_DIR}/${icon}.png"

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

0 comments on commit 06e8319

Please sign in to comment.