#!/bin/sh # SPDX-License-Identifier: AGPL-3.0-only # Copyright 2022 Sxmo Contributors # include common definitions # shellcheck source=scripts/core/sxmo_common.sh . "$(dirname "$0")/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 -n1 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() { _can_fail sxmo_terminal.sh sh -ce " notify-send 'Make the device discoverable' bluetoothctl remove '$1' & sxmo_daemons.sh start bluetooth_scan bluetoothctl scan on sleep 5 while : ; do bluetoothctl --timeout 5 pair '$1' if bluetoothctl info '$1' | grep -q 'Paired: yes'; then break fi done sleep 1 bluetoothctl trust '$1' bluetoothctl --timeout 7 connect '$1' sxmo_daemons.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 sxmo_terminal.sh bluetoothctl --timeout 7 disconnect "$MAC" else _can_fail sxmo_terminal.sh bluetoothctl --timeout 7 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=3 sleep 0.5 ;; *) INDEX=0 if [ "$SIMPLE_MODE" = no ]; then device_loop "$PICK" else toggle_connection "$PICK" fi ;; esac done } main_loop