From 93b7a1d835c68eaf7bbc8ac90b0099f97a303b8c Mon Sep 17 00:00:00 2001 From: stormdragon2976 Date: Fri, 2 Dec 2022 16:15:57 -0500 Subject: [PATCH] Game controler battery status script added. This works with PS4 controlers by default, and probably other batteries if you set the path in the script itself. --- i38.sh | 2 ++ scripts/game_controler.sh | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 scripts/game_controler.sh diff --git a/i38.sh b/i38.sh index 6203f31..d11ab98 100755 --- a/i38.sh +++ b/i38.sh @@ -373,6 +373,7 @@ bindsym e exec $textEditor, mode "default" bindsym f exec $fileBrowser, mode "default" # Web browser bound to w bindsym w exec $webBrowser, mode "default" +bindsym g exec ${i3Path}/scripts/game_controler.sh -s, mode "default" $(if command -v mumble &> /dev/null ; then echo "bindsym m exec $(command -v mumble), mode \"default\"" fi) @@ -425,6 +426,7 @@ bindsym \$mod+e exec $textEditor bindsym \$mod+f exec $fileBrowser # Web browser bound to $mod+w bindsym \$mod+w exec $webBrowser +bindsym \$mod+g exec ${i3Path}/scripts/game_controler.sh -s, mode "default" $(if command -v mumble &> /dev/null ; then echo "bindsym \$mod+m exec $(command -v mumble)" fi) diff --git a/scripts/game_controler.sh b/scripts/game_controler.sh new file mode 100755 index 0000000..1dc028e --- /dev/null +++ b/scripts/game_controler.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Add this to your crontab to have battery status automatically reported when it starts getting low +# */10 * * * * XDG_RUNTIME_DIR=/run/user/1000 /home/user/.config/i3/scripts/game_controler.sh + +# Set the name to your battery here. +# If not set, it will be automatically detected. +batteryName="" + +if [[ "$batteryName" == "" ]]; then + batteryName="$(find /sys/class/power_supply -name 'sony_controller_battery_*' | cut -d/ -f5)" +fi + +# If there's no file, we don't check it. +if [[ ! -f "/sys/class/power_supply/${batteryName}/capacity" ]]; then + spd-say -P important -Cw "Battery not found." + exit 0 +fi + +oldPercent="$(tail -1 "$0" | tr -cd '[:digit:]')" +oldPercent="${oldPercent:-0}" + +percent=$(cat "/sys/class/power_supply/${batteryName}/capacity" | tr -cd '[:digit:]') +status="$(cat "/sys/class/power_supply/${batteryName}/status")" + +# If status is requested, give it and exit. +if [[ "$1" == "-s" || "$1" == "--status" ]]; then + spd-say -P important -Cw "Battery ${percent} percent, ${status,,}." + exit 0 +fi + +if [[ $percent -ne $oldPercent && $percent -gt 10 ]]; then + spd-say "Battery $(> "$0" + +exit 0 +50