1
0
Fork 0

fix: Remove buggy a2dp plugin

This commit is contained in:
lwad 2024-03-29 23:06:55 +00:00
parent 7e5f955643
commit 1e76d30ad0
1 changed files with 87 additions and 93 deletions

View File

@ -17,100 +17,94 @@ with lib; {
};
enable = mkEnableOption "audio controls";
};
config = {
home-manager.users.${config.lwad.definitions.username}.xsession.windowManager.i3.config =
let
cfg = config.lwad.applications.i3;
mod = cfg.modifiers.primary;
config.home-manager.users.${config.lwad.definitions.username}.xsession.windowManager.i3.config =
let
cfg = config.lwad.applications.i3;
mod = cfg.modifiers.primary;
in {
keybindings."${mod}+Shift+a" = mkIf cfg.audioControls.enable "mode audio";
modes.audio = mkIf cfg.audioControls.enable (let
cleanse = (x: (replaceStrings [ ''"'' ] [ ''\\"'' ] x));
commands = cfg.audioControls.commands;
in {
keybindings."${mod}+Shift+a" =
mkIf cfg.audioControls.enable "mode audio";
modes.audio = mkIf cfg.audioControls.enable (let
cleanse = (x: (replaceStrings [ ''"'' ] [ ''\\"'' ] x));
commands = cfg.audioControls.commands;
in {
"1" = mkIf (commands.headphones != null) ''
exec --no-startup-id "${
cleanse commands.headphones
}", mode "default"'';
"2" = mkIf (commands.speakers != null) ''
exec --no-startup-id "${
cleanse commands.speakers
}", mode "default"'';
"3" = mkIf cfg.audioControls.bluetooth.enable ''
exec --no-startup-id "${
(let
cut = "${pkgs.coreutils}/bin/cut";
test = "${pkgs.coreutils}/bin/test";
jq = "${pkgs.jq}/bin/jq";
bluetoothctl = "${pkgs.bluez}/bin/bluetoothctl";
pactl = "${pkgs.pulseaudio}/.bin-unwrapped/pactl";
in pkgs.writeScript "select_bluetooth_audio.sh" ''
IFS=$'\n'
"1" = mkIf (commands.headphones != null) ''
exec --no-startup-id "${
cleanse commands.headphones
}", mode "default"'';
"2" = mkIf (commands.speakers != null) ''
exec --no-startup-id "${cleanse commands.speakers}", mode "default"'';
"3" = mkIf cfg.audioControls.bluetooth.enable ''
exec --no-startup-id "${
(let
cut = "${pkgs.coreutils}/bin/cut";
test = "${pkgs.coreutils}/bin/test";
jq = "${pkgs.jq}/bin/jq";
bluetoothctl = "${pkgs.bluez}/bin/bluetoothctl";
pactl = "${pkgs.pulseaudio}/.bin-unwrapped/pactl";
in pkgs.writeScript "select_bluetooth_audio.sh" ''
IFS=$'\n'
get_devices() {
if ${test} -f "$XDG_CONFIG_HOME/devices"; then
${pkgs.coreutils}/bin/cat "$XDG_CONFIG_HOME/devices"
else
${bluetoothctl} devices Bonded | ${cut} -d " " -f 2,3 | ${pkgs.coreutils}/bin/tee "$XDG_CONFIG_HOME/devices"
fi
}
select_device() {
devices="$(get_devices)"
selected_device="$(${cut} -d " " -f 2 <<< "$devices" | ${pkgs.rofi}/bin/rofi -dmenu -p "device" -i)"
${pkgs.ripgrep}/bin/rg "^[0-9A-F:]{17} $selected_device$" <<< "$devices" | ${cut} -d " " -f 1
}
disconnect_other() {
local address
for address in $(${bluetoothctl} devices Connected | ${cut} -d " " -f 2); do
if ${test} "$address" != "$1"; then
${bluetoothctl} disconnect "$address"
fi
done
}
address="$(select_device)"
if ${test} -n "$address"; then
disconnect_other "$address"
remaining_connection_attempts=3
while ${test} "$remaining_connection_attempts" -gt 0; do
${pkgs.coreutils}/bin/timeout 3 ${bluetoothctl} connect "$address" && break
remaining_connection_attempts=$((remaining_connection_attempts - 1))
done
if ${test} "$remaining_connection_attempts" -ne 0; then
while :; do
sink_name="$(${pactl} --format=json list short sinks | ${jq} --exit-status ".[].name | select(${test}(\"^bluez\"))" -r)" && break
done
${pactl} set-default-sink "$sink_name"
${pactl} set-sink-mute "$sink_name" 1
sleep 1
${pactl} set-sink-volume "$sink_name" 0
${pactl} set-sink-mute "$sink_name" 0
fi
elif sink_name="$(${pactl} --format=json list short sinks | ${jq} --exit-status ".[].name | select(${test}(\"^bluez\"))" -r)"; then
${pactl} set-default-sink "$sink_name"
fi
'')
}", mode "default"'';
"d" = mkIf cfg.audioControls.bluetooth.enable ''
exec --no-startup-id ${pkgs.bluez}/bin/bluetoothctl disconnect, exec --no-startup-id ${
if (commands.speakers != null) then
commands.speakers
else
(if (commands.headphones != null) then
commands.headphones
get_devices() {
if ${test} -f "$XDG_CONFIG_HOME/devices"; then
${pkgs.coreutils}/bin/cat "$XDG_CONFIG_HOME/devices"
else
":")
}, mode "default"'';
"Escape" = ''mode "default"'';
});
};
systemd.services.bluetooth.scriptArgs = "--plugin=a2dp";
};
${bluetoothctl} devices Bonded | ${cut} -d " " -f 2,3 | ${pkgs.coreutils}/bin/tee "$XDG_CONFIG_HOME/devices"
fi
}
select_device() {
devices="$(get_devices)"
selected_device="$(${cut} -d " " -f 2 <<< "$devices" | ${pkgs.rofi}/bin/rofi -dmenu -p "device" -i)"
${pkgs.ripgrep}/bin/rg "^[0-9A-F:]{17} $selected_device$" <<< "$devices" | ${cut} -d " " -f 1
}
disconnect_other() {
local address
for address in $(${bluetoothctl} devices Connected | ${cut} -d " " -f 2); do
if ${test} "$address" != "$1"; then
${bluetoothctl} disconnect "$address"
fi
done
}
address="$(select_device)"
if ${test} -n "$address"; then
disconnect_other "$address"
remaining_connection_attempts=3
while ${test} "$remaining_connection_attempts" -gt 0; do
${pkgs.coreutils}/bin/timeout 3 ${bluetoothctl} connect "$address" && break
remaining_connection_attempts=$((remaining_connection_attempts - 1))
done
if ${test} "$remaining_connection_attempts" -ne 0; then
while :; do
sink_name="$(${pactl} --format=json list short sinks | ${jq} --exit-status ".[].name | select(${test}(\"^bluez\"))" -r)" && break
done
${pactl} set-default-sink "$sink_name"
${pactl} set-sink-mute "$sink_name" 1
sleep 1
${pactl} set-sink-volume "$sink_name" 0
${pactl} set-sink-mute "$sink_name" 0
fi
elif sink_name="$(${pactl} --format=json list short sinks | ${jq} --exit-status ".[].name | select(${test}(\"^bluez\"))" -r)"; then
${pactl} set-default-sink "$sink_name"
fi
'')
}", mode "default"'';
"d" = mkIf cfg.audioControls.bluetooth.enable ''
exec --no-startup-id ${pkgs.bluez}/bin/bluetoothctl disconnect, exec --no-startup-id ${
if (commands.speakers != null) then
commands.speakers
else
(if (commands.headphones != null) then
commands.headphones
else
":")
}, mode "default"'';
"Escape" = ''mode "default"'';
});
};
}