removes unnecessary power option depending on power state
This commit is contained in:
parent
28661c0ab3
commit
f70de55ec9
84
bt
84
bt
@ -4,17 +4,14 @@ SCAN_PERIOD=5
|
|||||||
#locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install)
|
#locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install)
|
||||||
BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist
|
BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist
|
||||||
PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired
|
PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired
|
||||||
AUTOTRUST=true
|
|
||||||
actions="turn off
|
actions="pair
|
||||||
turn on
|
disconnect
|
||||||
pair
|
|
||||||
unpair"
|
unpair"
|
||||||
|
|
||||||
if ! $AUTOTRUST; then
|
#get power state of bluetooth controller
|
||||||
actions+="
|
powerstatus="$( bluetoothctl show | grep Powered | awk '{print $2}' )"
|
||||||
trust"
|
scanstatus="$( bluetoothctl show | grep Discovering | awk '{print $2}' )"
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
#Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side.
|
#Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side.
|
||||||
for prog in dmenu bluetoothctl awk cat date nl; do
|
for prog in dmenu bluetoothctl awk cat date nl; do
|
||||||
@ -24,9 +21,20 @@ for prog in dmenu bluetoothctl awk cat date nl; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
#start scanning as early as possible to speed up pairing process:
|
poweron(){
|
||||||
bluetoothctl power on & bluetoothctl scan on &>/dev/null &
|
[ "$powerstatus" = "no" ] && bluetoothctl power on
|
||||||
start_scan="$( date +'%s' )"
|
}
|
||||||
|
scanon(){
|
||||||
|
if [ "$scanstatus" = "no" ]; then
|
||||||
|
bluetoothctl scan on &
|
||||||
|
start_scan="$( date +'%s' )"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#start scanning as early as possible to speed up pairing process
|
||||||
|
#=> maybe use an option to do this? Otherwise ever invocation of bt powers on the controller.
|
||||||
|
poweron
|
||||||
|
scanon
|
||||||
|
|
||||||
#Compile list of all Bluetooth IDS of paired devices (from bluetoothctl and from hardcoded list)
|
#Compile list of all Bluetooth IDS of paired devices (from bluetoothctl and from hardcoded list)
|
||||||
bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{print $2}' )"
|
bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{print $2}' )"
|
||||||
@ -35,10 +43,14 @@ bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{
|
|||||||
paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
|
paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
|
||||||
|
|
||||||
disp_devices="$( echo "$paired_devices" | grep -vf "$BLACKLIST" )"
|
disp_devices="$( echo "$paired_devices" | grep -vf "$BLACKLIST" )"
|
||||||
|
echo "$paired_devices"
|
||||||
|
echo ----------------
|
||||||
|
echo "$disp_devices"
|
||||||
|
|
||||||
|
poweroption="$( echo "$powerstatus" | sed 's/yes/power off/; s/no/power on/' )"
|
||||||
#Don't print empty device list, removes unnecessary empty choice in dmenu
|
#Don't print empty device list, removes unnecessary empty choice in dmenu
|
||||||
[ "$disp_devices" = "" ] && choice=$( echo "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
|
[ "$disp_devices" = "" ] && choice=$( printf "%s\n%s" "$poweroption" "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
|
||||||
[ "$disp_devices" != "" ] && choice=$( ( echo "$disp_devices" && echo "$actions" ) | dmenu -i -p 'What BT action would you like to perform:' )
|
[ "$disp_devices" != "" ] && choice=$( ( echo "$disp_devices" && printf "%s\n%s" "$poweroption" "$actions" ) | dmenu -i -p 'What BT action would you like to perform:' )
|
||||||
|
|
||||||
cleanup(){
|
cleanup(){
|
||||||
bluetoothctl scan off
|
bluetoothctl scan off
|
||||||
@ -47,8 +59,8 @@ cleanup(){
|
|||||||
|
|
||||||
pair(){
|
pair(){
|
||||||
#since this function can get called indefinitely, make sure to always be scanning and controller has power in the case that it got deactived by some other process.
|
#since this function can get called indefinitely, make sure to always be scanning and controller has power in the case that it got deactived by some other process.
|
||||||
bluetoothctl power on &>/dev/null &
|
#bluetoothctl power on &>/dev/null &
|
||||||
bluetoothctl scan on &>/dev/null &
|
#bluetoothctl scan on &>/dev/null &
|
||||||
#check whether $SCAN_PERIOD seconds has already passed since starting scanning, if not, wait for the rest of that time.
|
#check whether $SCAN_PERIOD seconds has already passed since starting scanning, if not, wait for the rest of that time.
|
||||||
if [ $((( "$(date +'%s')" - "$start_scan" ))) -lt $SCAN_PERIOD ]; then
|
if [ $((( "$(date +'%s')" - "$start_scan" ))) -lt $SCAN_PERIOD ]; then
|
||||||
sleep_period="$((( "$SCAN_PERIOD" - "$( date +'%s')" + "$start_scan" )))"
|
sleep_period="$((( "$SCAN_PERIOD" - "$( date +'%s')" + "$start_scan" )))"
|
||||||
@ -60,10 +72,15 @@ pair(){
|
|||||||
|
|
||||||
sleep "$sleep_period"
|
sleep "$sleep_period"
|
||||||
fi
|
fi
|
||||||
|
if [ $paired_devices = "" ]; then
|
||||||
echo "$paired_devices" > /tmp/paired_devices
|
echo "$paired_devices" > /tmp/paired_devices
|
||||||
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )"
|
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )"
|
||||||
rm -f /tmp/paired_devices
|
rm -f /tmp/paired_devices
|
||||||
choice=$( (echo "$new_devices" && echo 'rescan' ) | dmenu -l 10 -i -p 'pair with which device?' )
|
else
|
||||||
|
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
|
||||||
|
fi
|
||||||
|
[ $new_devices = "" ] && options="rescan" || options=$(echo "$new_devices" && echo 'rescan')
|
||||||
|
choice=$( echo "$options" | dmenu -l 10 -i -p 'pair with which device?' )
|
||||||
if [ -n "$choice" ]; then
|
if [ -n "$choice" ]; then
|
||||||
if [ "$choice" = "rescan" ]; then
|
if [ "$choice" = "rescan" ]; then
|
||||||
start_scan="$( date +'%s')"
|
start_scan="$( date +'%s')"
|
||||||
@ -79,22 +96,20 @@ pair(){
|
|||||||
bluetoothctl trust "$dev_id"
|
bluetoothctl trust "$dev_id"
|
||||||
fi
|
fi
|
||||||
#if device is not already hard coded as paired, add to paired devices list
|
#if device is not already hard coded as paired, add to paired devices list
|
||||||
if grep -q "$dev_id" "$PAIRLIST"
|
if [ "$( grep "$dev_id" "$PAIRLIST")" != "" ]
|
||||||
then
|
then
|
||||||
|
echo to be added to $PAIRLIST
|
||||||
|
echo Device "$dev_id" "$choice"
|
||||||
echo Device "$dev_id" "$choice" >> "$PAIRLIST"
|
echo Device "$dev_id" "$choice" >> "$PAIRLIST"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
cleanup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case $choice in
|
case $choice in
|
||||||
"turn on") bluetoothctl power on
|
"turn on") bluetoothctl power on;;
|
||||||
cleanup;;
|
"turn off") bluetoothctl power off;;
|
||||||
"turn off") bluetoothctl power off
|
|
||||||
cleanup;;
|
|
||||||
"scan on") bluetoothctl power on && echo power on && sleep 2
|
"scan on") bluetoothctl power on && echo power on && sleep 2
|
||||||
([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on
|
([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on;;
|
||||||
cleanup;;
|
|
||||||
"pair") pair;;
|
"pair") pair;;
|
||||||
"unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
|
"unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
|
||||||
if [ -n "$choice" ]; then
|
if [ -n "$choice" ]; then
|
||||||
@ -104,22 +119,25 @@ case $choice in
|
|||||||
#remove device to unpair from hard coded paired devices list
|
#remove device to unpair from hard coded paired devices list
|
||||||
new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )"
|
new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )"
|
||||||
echo "$new_paired_list" > "$PAIRLIST"
|
echo "$new_paired_list" > "$PAIRLIST"
|
||||||
fi
|
fi;;
|
||||||
cleanup;;
|
|
||||||
"trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
|
"trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
|
||||||
if [ -n "$choice" ]; then
|
if [ -n "$choice" ]; then
|
||||||
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
|
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
|
||||||
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
|
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
|
||||||
bluetoothctl trust "$dev_id"
|
bluetoothctl trust "$dev_id"
|
||||||
fi
|
fi;;
|
||||||
esac
|
"disconnect") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
|
||||||
|
if [ -n "$choice" ]; then
|
||||||
if [ -n "$choice" ]; then
|
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
|
||||||
|
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
|
||||||
|
bluetoothctl disconnect "$dev_id"
|
||||||
|
fi;;
|
||||||
|
"*")
|
||||||
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
|
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
|
||||||
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}')
|
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}')
|
||||||
bluetoothctl power on
|
bluetoothctl power on
|
||||||
bluetoothctl pair "$dev_id"
|
bluetoothctl pair "$dev_id"
|
||||||
bluetoothctl connect "$dev_id"
|
bluetoothctl connect "$dev_id";;
|
||||||
fi
|
esac
|
||||||
|
|
||||||
cleanup
|
cleanup
|
||||||
|
Loading…
Reference in New Issue
Block a user