diff --git a/rofi-bluetooth b/rofi-bluetooth index 16ca443..c1e7dd4 100755 --- a/rofi-bluetooth +++ b/rofi-bluetooth @@ -6,6 +6,7 @@ # |_| \___/|_| |_| |_.__/|_|\__,_|\___|\__\___/ \___/ \__|_| |_| # # Author: Nick Clyde (clydedroid) +# Enhanced with theme support # # A script that generates a rofi menu that uses bluetoothctl to # connect to bluetooth devices and display status info. @@ -20,297 +21,373 @@ divider="---------" goback="Back" +# Default theme and configuration +DEFAULT_THEME="" +CUSTOM_THEME="" +ROFI_EXTRA_ARGS="" + +# Parse command line arguments for theme and other options +parse_arguments() { + while [[ $# -gt 0 ]]; do + case $1 in + --theme) + CUSTOM_THEME="$2" + shift 2 + ;; + --theme=*) + CUSTOM_THEME="${1#*=}" + shift + ;; + --status) + print_status + exit 0 + ;; + --help | -h) + show_help + exit 0 + ;; + *) + # Collect other rofi arguments + ROFI_EXTRA_ARGS="$ROFI_EXTRA_ARGS $1" + shift + ;; + esac + done +} + +# Show help information +show_help() { + cat </dev/null + bluetoothctl scan off + show_menu + else + bluetoothctl --timeout 5 scan on & + echo "Scanning..." + show_menu + fi } # Checks if controller is able to pair to devices pairable_on() { - if bluetoothctl show | grep -q "Pairable: yes"; then - echo "Pairable: on" - return 0 - else - echo "Pairable: off" - return 1 - fi + if bluetoothctl show | grep -q "Pairable: yes"; then + echo "Pairable: on" + return 0 + else + echo "Pairable: off" + return 1 + fi } # Toggles pairable state toggle_pairable() { - if pairable_on; then - bluetoothctl pairable off - show_menu - else - bluetoothctl pairable on - show_menu - fi + if pairable_on; then + bluetoothctl pairable off + show_menu + else + bluetoothctl pairable on + show_menu + fi } # Checks if controller is discoverable by other devices discoverable_on() { - if bluetoothctl show | grep -q "Discoverable: yes"; then - echo "Discoverable: on" - return 0 - else - echo "Discoverable: off" - return 1 - fi + if bluetoothctl show | grep -q "Discoverable: yes"; then + echo "Discoverable: on" + return 0 + else + echo "Discoverable: off" + return 1 + fi } # Toggles discoverable state toggle_discoverable() { - if discoverable_on; then - bluetoothctl discoverable off - show_menu - else - bluetoothctl discoverable on - show_menu - fi + if discoverable_on; then + bluetoothctl discoverable off + show_menu + else + bluetoothctl discoverable on + show_menu + fi } # Checks if a device is connected device_connected() { - device_info=$(bluetoothctl info "$1") - if echo "$device_info" | grep -q "Connected: yes"; then - return 0 - else - return 1 - fi + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Connected: yes"; then + return 0 + else + return 1 + fi } # Toggles device connection toggle_connection() { - if device_connected "$1"; then - bluetoothctl disconnect "$1" - device_menu "$device" - else - bluetoothctl connect "$1" - device_menu "$device" - fi + if device_connected "$1"; then + bluetoothctl disconnect "$1" + device_menu "$device" + else + bluetoothctl connect "$1" + device_menu "$device" + fi } # Checks if a device is paired device_paired() { - device_info=$(bluetoothctl info "$1") - if echo "$device_info" | grep -q "Paired: yes"; then - echo "Paired: yes" - return 0 - else - echo "Paired: no" - return 1 - fi + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Paired: yes"; then + echo "Paired: yes" + return 0 + else + echo "Paired: no" + return 1 + fi } # Toggles device paired state toggle_paired() { - if device_paired "$1"; then - bluetoothctl remove "$1" - device_menu "$device" - else - bluetoothctl pair "$1" - device_menu "$device" - fi + if device_paired "$1"; then + bluetoothctl remove "$1" + device_menu "$device" + else + bluetoothctl pair "$1" + device_menu "$device" + fi } # Checks if a device is trusted device_trusted() { - device_info=$(bluetoothctl info "$1") - if echo "$device_info" | grep -q "Trusted: yes"; then - echo "Trusted: yes" - return 0 - else - echo "Trusted: no" - return 1 - fi + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Trusted: yes"; then + echo "Trusted: yes" + return 0 + else + echo "Trusted: no" + return 1 + fi } -# Toggles device connection +# Toggles device trust state toggle_trust() { - if device_trusted "$1"; then - bluetoothctl untrust "$1" - device_menu "$device" - else - bluetoothctl trust "$1" - device_menu "$device" - fi + if device_trusted "$1"; then + bluetoothctl untrust "$1" + device_menu "$device" + else + bluetoothctl trust "$1" + device_menu "$device" + fi } # Prints a short string with the current bluetooth status # Useful for status bars like polybar, etc. print_status() { - if power_on; then - printf '' + if power_on; then + printf '' + + paired_devices_cmd="devices Paired" + # Check if an outdated version of bluetoothctl is used to preserve backwards compatibility + if (($(echo "$(bluetoothctl version | cut -d ' ' -f 2) < 5.65" | bc -l))); then + paired_devices_cmd="paired-devices" + fi + + mapfile -t paired_devices < <(bluetoothctl $paired_devices_cmd | grep Device | cut -d ' ' -f 2) + counter=0 - paired_devices_cmd="devices Paired" - # Check if an outdated version of bluetoothctl is used to preserve backwards compatibility - if (( $(echo "$(bluetoothctl version | cut -d ' ' -f 2) < 5.65" | bc -l) )); then - paired_devices_cmd="paired-devices" + for device in "${paired_devices[@]}"; do + if device_connected "$device"; then + device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-) + + if [ $counter -gt 0 ]; then + printf ", %s" "$device_alias" + else + printf " %s" "$device_alias" fi - mapfile -t paired_devices < <(bluetoothctl $paired_devices_cmd | grep Device | cut -d ' ' -f 2) - counter=0 - - for device in "${paired_devices[@]}"; do - if device_connected "$device"; then - device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-) - - if [ $counter -gt 0 ]; then - printf ", %s" "$device_alias" - else - printf " %s" "$device_alias" - fi - - ((counter++)) - fi - done - printf "\n" - else - echo "" - fi + ((counter++)) + fi + done + printf "\n" + else + echo "" + fi } # A submenu for a specific device that allows connecting, pairing, and trusting device_menu() { - device=$1 - - # Get device name and mac address - device_name=$(echo "$device" | cut -d ' ' -f 3-) - mac=$(echo "$device" | cut -d ' ' -f 2) - - # Build options - if device_connected "$mac"; then - connected="Connected: yes" - else - connected="Connected: no" - fi - paired=$(device_paired "$mac") - trusted=$(device_trusted "$mac") - options="$connected\n$paired\n$trusted\n$divider\n$goback\nExit" - - # Open rofi menu, read chosen option - chosen="$(echo -e "$options" | $rofi_command "$device_name")" - - # Match chosen option to command - case "$chosen" in - "" | "$divider") - echo "No option chosen." - ;; - "$connected") - toggle_connection "$mac" - ;; - "$paired") - toggle_paired "$mac" - ;; - "$trusted") - toggle_trust "$mac" - ;; - "$goback") - show_menu - ;; - esac + device=$1 + + # Get device name and mac address + device_name=$(echo "$device" | cut -d ' ' -f 3-) + mac=$(echo "$device" | cut -d ' ' -f 2) + + # Build options + if device_connected "$mac"; then + connected="Connected: yes" + else + connected="Connected: no" + fi + paired=$(device_paired "$mac") + trusted=$(device_trusted "$mac") + options="$connected\n$paired\n$trusted\n$divider\n$goback\nExit" + + # Open rofi menu, read chosen option + rofi_cmd=$(build_rofi_command "$device_name") + chosen="$(echo -e "$options" | eval "$rofi_cmd")" + + # Match chosen option to command + case "$chosen" in + "" | "$divider") + echo "No option chosen." + ;; + "$connected") + toggle_connection "$mac" + ;; + "$paired") + toggle_paired "$mac" + ;; + "$trusted") + toggle_trust "$mac" + ;; + "$goback") + show_menu + ;; + esac } # Opens a rofi menu with current bluetooth status and options to connect show_menu() { - # Get menu options - if power_on; then - power="Power: on" - - # Human-readable names of devices, one per line - # If scan is off, will only list paired devices - devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-) - - # Get controller flags - scan=$(scan_on) - pairable=$(pairable_on) - discoverable=$(discoverable_on) - - # Options passed to rofi - options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit" - else - power="Power: off" - options="$power\nExit" - fi - - # Open rofi menu, read chosen option - chosen="$(echo -e "$options" | $rofi_command "Bluetooth")" - - # Match chosen option to command - case "$chosen" in - "" | "$divider") - echo "No option chosen." - ;; - "$power") - toggle_power - ;; - "$scan") - toggle_scan - ;; - "$discoverable") - toggle_discoverable - ;; - "$pairable") - toggle_pairable - ;; - *) - device=$(bluetoothctl devices | grep "$chosen") - # Open a submenu if a device is selected - if [[ $device ]]; then device_menu "$device"; fi - ;; - esac + # Get menu options + if power_on; then + power="Power: on" + + # Human-readable names of devices, one per line + # If scan is off, will only list paired devices + devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-) + + # Get controller flags + scan=$(scan_on) + pairable=$(pairable_on) + discoverable=$(discoverable_on) + + # Options passed to rofi + options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit" + else + power="Power: off" + options="$power\nExit" + fi + + # Open rofi menu, read chosen option + rofi_cmd=$(build_rofi_command "Bluetooth") + chosen="$(echo -e "$options" | eval "$rofi_cmd")" + + # Match chosen option to command + case "$chosen" in + "" | "$divider") + echo "No option chosen." + ;; + "$power") + toggle_power + ;; + "$scan") + toggle_scan + ;; + "$discoverable") + toggle_discoverable + ;; + "$pairable") + toggle_pairable + ;; + *) + device=$(bluetoothctl devices | grep "$chosen") + # Open a submenu if a device is selected + if [[ $device ]]; then device_menu "$device"; fi + ;; + esac } -# Rofi command to pipe into, can add any options here -rofi_command="rofi -dmenu $* -p" - -case "$1" in - --status) - print_status - ;; - *) - show_menu - ;; -esac +# Main execution +parse_arguments "$@" +show_menu