#!/bin/sh # SPDX-License-Identifier: AGPL-3.0-only # Copyright 2022 Sxmo Contributors # include common definitions # shellcheck source=scripts/core/sxmo_common.sh . sxmo_common.sh # shellcheck source=configs/default_hooks/sxmo_hook_icons.sh . sxmo_hook_icons.sh set -e SIMPLE_MODE=yes _prompt() { sxmo_dmenu.sh -i "$@" } _device_list() { bluetoothctl devices | \ cut -d" " -f2 | \ xargs -rn1 bluetoothctl info | \ awk ' function print_cached_device() { print icon linkedsep paired connected " " name " " mac name=icon=mac="" } { $1=$1 } /^Device/ && name { print_cached_device() } /^Device/ { mac=$2; paired=""; connected=""; linkedsep="" } /Name:/ { $1="";$0=$0;$1=$1; name=$0 } /Paired: yes/ { paired="'$icon_lnk'"; linkedsep=" " } /Connected: yes/ { connected="'$icon_a2x'"; linkedsep=" " } /Icon: computer/ { icon="'$icon_com'" } /Icon: phone/ { icon="'$icon_phn'" } /Icon: modem/ { icon="'$icon_mod'" } /Appearance: 0x00c2/ { icon="'$icon_wat'" } /Icon: watch/ { icon="'$icon_wat'" } /Icon: network-wireless/ { icon="'$icon_wif'" } /Icon: audio-headset/ { icon="'$icon_hdp'" } /Icon: audio-headphones/ { icon="'$icon_spk'" } /Icon: camera-video/ { icon="'$icon_vid'" } /Icon: audio-card/ { icon="'$icon_mus'" } /Icon: input-gaming/ { icon="'$icon_gam'" } /Icon: input-keyboard/ { icon="'$icon_kbd'" } /Icon: input-tablet/ { icon="'$icon_drw'" } /Icon: input-mouse/ { icon="'$icon_mse'" } /Icon: printer/ { icon="'$icon_prn'" } /Icon: camera-photo/ { icon="'$icon_cam'" } END { print_cached_device() } ' } _restart_bluetooth() { case "$SXMO_OS" in alpine|postmarketos) doas rc-service bluetooth restart ;; arch|archarm|nixos) doas systemctl restart bluetooth ;; esac } _full_reconnection() { sxmo_terminal.sh sh -c " notify-send 'Make the device discoverable' bluetoothctl remove '$1' sxmo_jobs.sh start bluetooth_scan bluetoothctl --timeout 300 scan on sleep 5 while : ; do timeout 7 bluetoothctl connect '$1' if bluetoothctl info '$1' | grep -q 'Connected: yes'; then break fi sleep 1 done " sxmo_jobs.sh stop bluetooth_scan } _notify_failure() { notify-send "Something failed" } _can_fail() { "$@" || _notify_failure } _show_toggle() { if [ "$1" = yes ]; then printf %s "$icon_ton" else printf %s "$icon_tof" fi } toggle_connection() { DEVICE="$1" MAC="$(printf "%s\n" "$DEVICE" | awk '{print $NF}')" if printf "%s\n" "$PICK" | grep -q "$icon_a2x"; then _can_fail timeout 7 sxmo_terminal.sh bluetoothctl disconnect "$MAC" else _can_fail timeout 7 sxmo_terminal.sh bluetoothctl connect "$MAC" fi } device_loop() { DEVICE="$1" MAC="$(printf "%s\n" "$DEVICE" | awk '{print $NF}')" INDEX=0 while : ; do INFO="$(bluetoothctl info "$MAC")" PAIRED="$(printf "%s\n" "$INFO" | grep "Paired:" | awk '{print $NF}')" TRUSTED="$(printf "%s\n" "$INFO" | grep "Trusted:" | awk '{print $NF}')" BLOCKED="$(printf "%s\n" "$INFO" | grep "Blocked:" | awk '{print $NF}')" CONNECTED="$(printf "%s\n" "$INFO" | grep "Connected:" | awk '{print $NF}')" PICK="$( cat < /dev/null notify-send "Scanning for 60 seconds" INDEX=5 sleep 0.5 ;; *) INDEX=0 if [ "$SIMPLE_MODE" = no ]; then device_loop "$PICK" else toggle_connection "$PICK" fi ;; esac done } main_loop