From c2a5b6b6179ee4cef5e27fe9e84976fd2851e5f4 Mon Sep 17 00:00:00 2001 From: twoneis Date: Wed, 19 Mar 2025 11:20:05 +0100 Subject: [PATCH] added script to connect and disconnect headset --- modules/niri/connect-headset.sh | 11 +++++++ modules/niri/niri.conf.nix | 54 ++------------------------------- modules/niri/switch-sink.sh | 46 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 51 deletions(-) create mode 100755 modules/niri/connect-headset.sh create mode 100755 modules/niri/switch-sink.sh diff --git a/modules/niri/connect-headset.sh b/modules/niri/connect-headset.sh new file mode 100755 index 0000000..b01e3da --- /dev/null +++ b/modules/niri/connect-headset.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env fish + +set -l mac "FC:C3:3A:00:04:C2" + +set -l connected (bluetoothctl info $mac | grep "Connected:") + +if string match -qr ".*yes" $connected + bluetoothctl disconnect $mac +else + bluetoothctl connect $mac +end diff --git a/modules/niri/niri.conf.nix b/modules/niri/niri.conf.nix index 10b337a..0c21bc2 100644 --- a/modules/niri/niri.conf.nix +++ b/modules/niri/niri.conf.nix @@ -7,57 +7,8 @@ inherit (config.conf) keys extraLayout; inherit (lib.attrsets) genAttrs; inherit (lib) mkMerge; - switch-sink = - pkgs.writeScript "switch-sink" - '' - #!/usr/bin/env fish - - # set -l regex '^.* (\*?) \s+ ([[:digit:]]*)\. .* \[.*$' - set -l sed 's/^.* ([[:digit:]]*)\. .*$/\1/' - set -l awk 'BEGIN { A=0; S=0; } /^Audio/ { A=1; } /Sinks/ { S=1; } /Sources/ { S=0; } /^Video/ { A=0; } { if (A==1 && S==1 && / [[:digit:]]*\./) { print; } }' - - set -l lines (wpctl status | awk $awk) - set -l len (count $lines) - - set -l current - set -l sinks - - for i in (seq 1 $len) - set -l sink (echo $lines[$i] | sed -E $sed) - set -a sinks $sink - - if string match -qr '\*' $lines[$i] - set current $sink - end - - end - - set -l len (count $sinks) - set -l index - - for i in (seq 1 $len) - if [ $sinks[$i] = $current ] - if [ $argv[1] = '-f' ] - if [ $i = $len ] - set index 1 - else - set index (math $i + 1) - end - else if [ $argv[1] = '-b' ] - if [ $i = 1 ] - set index $len - else - set index (math $i - 1) - end - else - exit 1 - end - break - end - end - - wpctl set-default $sinks[$index] - ''; + switch-sink = pkgs.writeScript "switch-sink" (builtins.readFile ./switch-sink.sh); + connect-headset = pkgs.writeScript "connect-headset" (builtins.readFile ./connect-headset.sh); in { input = { keyboard = { @@ -196,6 +147,7 @@ in { "XF86AudioNext".action.spawn = ["${switch-sink}" "-f"]; "XF86AudioPrev".action.spawn = ["${switch-sink}" "-b"]; + "XF86AudioPlay".action.spawn = ["${connect-headset}"]; "XF86AudioRaiseVolume".action.spawn = [ "swayosd-client" diff --git a/modules/niri/switch-sink.sh b/modules/niri/switch-sink.sh new file mode 100755 index 0000000..56f2f71 --- /dev/null +++ b/modules/niri/switch-sink.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env fish + +set -l sed 's/^.* ([[:digit:]]*)\. .*$/\1/' +set -l awk 'BEGIN { A=0; S=0; } /^Audio/ { A=1; } /Sinks/ { S=1; } /Sources/ { S=0; } /^Video/ { A=0; } { if (A==1 && S==1 && / [[:digit:]]*\./) { print; } }' + +set -l lines (wpctl status | awk $awk) +set -l len (count $lines) + +set -l current +set -l sinks + +for i in (seq 1 $len) + set -l sink (echo $lines[$i] | sed -E $sed) + set -a sinks $sink + + if string match -qr '\*' $lines[$i] + set current $sink + end + +end + +set -l len (count $sinks) +set -l index + +for i in (seq 1 $len) + if [ $sinks[$i] = $current ] + if [ $argv[1] = '-f' ] + if [ $i = $len ] + set index 1 + else + set index (math $i + 1) + end + else if [ $argv[1] = '-b' ] + if [ $i = 1 ] + set index $len + else + set index (math $i - 1) + end + else + exit 1 + end + break + end +end + +wpctl set-default $sinks[$index]