playground/toggle_headset
2024-02-23 11:45:12 +01:00

63 lines
1.8 KiB
Clojure
Executable file

#!/usr/bin/env bb
(ns toggle-headset
(:require [clojure.java.shell :refer [sh]]
[cheshire.core :as json]))
(defn get-current-sink-name []
(-> (sh "pactl" "--format=json" "list" "sinks")
:out
(json/parse-string true)
(->> (filter #(= (:state %) "RUNNING"))
(first)
:properties
:device.name)))
(defn get-card-current-profile [card]
(-> (sh "pactl" "--format=json" "list" "cards")
:out
(json/parse-string true)
(->> (filter #(= (:name %) card))
(first)
:active_profile
keyword
)
))
(defn get-card-profiles [card]
(-> (sh "pactl" "--format=json" "list" "cards")
:out
(json/parse-string true)
(->> (filter #(= (:name %) card))
(first)
:profiles
(map #(assoc (second %) :name (first %)))
(filter :available)
(sort-by :priority >=)
(partition-by (comp pos? :sources))
(map first)
(take 2)
(map :name)
)))
(defn profile->emoji [profile]
(if (re-find #"a2dp" (name profile))
"🎧"
"🎤"))
(defn toggle-card-profile [card]
(let [current (get-card-current-profile card)
profiles (get-card-profiles card)]
[profiles current]
(if-let [next-profile (some->> profiles
(remove (partial = current))
first)]
(do
(sh "pactl" "set-card-profile" card (name next-profile))
(profile->emoji next-profile))
(prn "Error selecting next profile"))))
(case (some->> *command-line-args*
first)
"get" (println (profile->emoji (get-card-current-profile (get-current-sink-name))))
"toggle" (println (toggle-card-profile (get-current-sink-name))))