Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
  1. #!/bin/bash
  2. # Checks if bluetooth controller is powered on
  3. power_on() {
  4. if bluetoothctl show | grep -q "Powered: yes"; then
  5. return 0
  6. else
  7. return 1
  8. fi
  9. }
  10. # Toggles power state
  11. toggle_power() {
  12. if power_on; then
  13. bluetoothctl power off
  14. else
  15. bluetoothctl power on
  16. fi
  17. }
  18. # Checks if controller is scanning for new devices
  19. scan_on() {
  20. if bluetoothctl show | grep -q "Discovering: yes"; then
  21. echo "Scan: on"
  22. return 0
  23. else
  24. echo "Scan: off"
  25. return 1
  26. fi
  27. }
  28. # Toggles scanning state
  29. toggle_scan() {
  30. if scan_on; then
  31. kill $(pgrep -f "bluetoothctl scan on")
  32. bluetoothctl scan off
  33. else
  34. bluetoothctl scan on &
  35. fi
  36. }
  37. # Checks if controller is able to pair to devices
  38. pairable_on() {
  39. if bluetoothctl show | grep -q "Pairable: yes"; then
  40. echo "Pairable: on"
  41. return 0
  42. else
  43. echo "Pairable: off"
  44. return 1
  45. fi
  46. }
  47. # Toggles pairable state
  48. toggle_pairable() {
  49. if pairable_on; then
  50. bluetoothctl pairable off
  51. else
  52. bluetoothctl pairable on
  53. fi
  54. }
  55. # Checks if controller is discoverable by other devices
  56. discoverable_on() {
  57. if bluetoothctl show | grep -q "Discoverable: yes"; then
  58. echo "Discoverable: on"
  59. return 0
  60. else
  61. echo "Discoverable: off"
  62. return 1
  63. fi
  64. }
  65. # Toggles discoverable state
  66. toggle_discoverable() {
  67. if discoverable_on; then
  68. bluetoothctl discoverable off
  69. else
  70. bluetoothctl discoverable on
  71. fi
  72. }
  73. # Checks if a device is connected
  74. device_connected() {
  75. device_info=$(bluetoothctl info "$1")
  76. if echo "$device_info" | grep -q "Connected: yes"; then
  77. return 0
  78. else
  79. return 1
  80. fi
  81. }
  82. # Toggles device connection
  83. toggle_connection() {
  84. if device_connected "$1"; then
  85. bluetoothctl disconnect "$1"
  86. else
  87. bluetoothctl connect "$1"
  88. fi
  89. }
  90. # Checks if a device is paired
  91. device_paired() {
  92. device_info=$(bluetoothctl info "$1")
  93. if echo "$device_info" | grep -q "Paired: yes"; then
  94. echo "Paired: yes"
  95. return 0
  96. else
  97. echo "Paired: no"
  98. return 1
  99. fi
  100. }
  101. # Toggles device paired state
  102. toggle_paired() {
  103. if device_paired "$1"; then
  104. bluetoothctl remove "$1"
  105. else
  106. bluetoothctl pair "$1"
  107. fi
  108. }
  109. # Checks if a device is trusted
  110. device_trusted() {
  111. device_info=$(bluetoothctl info "$1")
  112. if echo "$device_info" | grep -q "Trusted: yes"; then
  113. echo "Trusted: yes"
  114. return 0
  115. else
  116. echo "Trusted: no"
  117. return 1
  118. fi
  119. }
  120. # Toggles device connection
  121. toggle_trust() {
  122. if device_trusted "$1"; then
  123. bluetoothctl untrust "$1"
  124. else
  125. bluetoothctl trust "$1"
  126. fi
  127. }
  128. # Prints a short string with the current bluetooth status
  129. # Useful for status bars like polybar, etc.
  130. print_status() {
  131. if power_on; then
  132. printf ''
  133. mapfile -t paired_devices < <(bluetoothctl paired-devices | grep Device | cut -d ' ' -f 2)
  134. counter=0
  135. for device in "${paired_devices[@]}"; do
  136. if device_connected "$device"; then
  137. device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-)
  138. if [ $counter -gt 0 ]; then
  139. printf ", %s" "$device_alias"
  140. else
  141. printf " %s" "$device_alias"
  142. fi
  143. ((counter++))
  144. fi
  145. done
  146. if [ $counter -eq 0 ]; then
  147. printf " On"
  148. fi
  149. else
  150. echo " Off"
  151. fi
  152. }
  153. # A submenu for a specific device that allows connecting, pairing, and trusting
  154. device_menu() {
  155. device="$1"
  156. # Get device name and mac address
  157. device_name=$(echo "$device" | cut -d ' ' -f 3-)
  158. mac=$(echo "$device" | cut -d ' ' -f 2)
  159. # Build options
  160. if device_connected $mac; then
  161. connected="Connected: yes"
  162. else
  163. connected="Connected: no"
  164. fi
  165. paired=$(device_paired $mac)
  166. trusted=$(device_trusted $mac)
  167. options="$connected\n$paired\n$trusted"
  168. # Open dmenu menu, read chosen option
  169. chosen="$(echo -e "$options" | $dmenu_command "$device_name")"
  170. # Match chosen option to command
  171. case $chosen in
  172. "")
  173. echo "No option chosen."
  174. ;;
  175. $connected)
  176. toggle_connection $mac
  177. ;;
  178. $paired)
  179. toggle_paired $mac
  180. ;;
  181. $trusted)
  182. toggle_trust $mac
  183. ;;
  184. esac
  185. }
  186. # Opens a dmenu menu with current bluetooth status and options to connect
  187. show_menu() {
  188. # Get menu options
  189. if power_on; then
  190. power="Power: on"
  191. # Human-readable names of devices, one per line
  192. # If scan is off, will only list paired devices
  193. devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-)
  194. # Get controller flags
  195. scan=$(scan_on)
  196. pairable=$(pairable_on)
  197. discoverable=$(discoverable_on)
  198. divider="---------"
  199. # Options passed to dmenu
  200. options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable"
  201. else
  202. power="Power: off"
  203. options="$power"
  204. fi
  205. # Open dmenu menu, read chosen option
  206. chosen="$(echo -e "$options" | $dmenu_command "Bluetooth")"
  207. # Match chosen option to command
  208. case $chosen in
  209. "" | $divider)
  210. echo "No option chosen."
  211. ;;
  212. $power)
  213. toggle_power
  214. ;;
  215. $scan)
  216. toggle_scan
  217. ;;
  218. $discoverable)
  219. toggle_discoverable
  220. ;;
  221. $pairable)
  222. toggle_pairable
  223. ;;
  224. *)
  225. device=$(bluetoothctl devices | grep "$chosen")
  226. # Open a submenu if a device is selected
  227. if [[ "$device" ]]; then device_menu "$device"; fi
  228. ;;
  229. esac
  230. }
  231. # dmenu command to pipe into, can add any options here
  232. dmenu_command="dmenu -z 1900 -x 10 -y 10 -i -p"
  233. case "$1" in
  234. --status)
  235. print_status
  236. ;;
  237. *)
  238. show_menu
  239. ;;
  240. esac
  241. kill -60 $(pidof dwmblocks)