aboutsummaryrefslogtreecommitdiffhomepage
path: root/util.go
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-07-15 16:25:44 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-07-15 16:25:44 +0900
commit0bd452ad9b773dd389ef9a858d8d48b922bf5ec6 (patch)
tree42e78ccd9627f71fc2ac1ad5b2d5bfc9947b0400 /util.go
parent7d96b0bf350d87463f4c082342d98c54aefe2b51 (diff)
util: PulseAudio cookie discovery
This appears to be how a regular PulseAudio client discovers the PulseAudio cookie. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'util.go')
-rw-r--r--util.go42
1 files changed, 40 insertions, 2 deletions
diff --git a/util.go b/util.go
index 2de8f3be..1a507431 100644
--- a/util.go
+++ b/util.go
@@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"os/exec"
+ "path"
)
const (
@@ -29,9 +30,46 @@ func sdBooted() bool {
return true
}
+// Try various ways to discover the current user's PulseAudio authentication cookie.
+func discoverPulseCookie() string {
+ if p, ok := os.LookupEnv(pulseCookie); ok {
+ return p
+ }
+
+ if p, ok := os.LookupEnv(home); ok {
+ p = path.Join(p, ".pulse-cookie")
+ if s, err := os.Stat(p); err != nil {
+ if !errors.Is(err, fs.ErrNotExist) {
+ fatal("Error accessing PulseAudio cookie:", err)
+ // unreachable
+ return p
+ }
+ } else if !s.IsDir() {
+ return p
+ }
+ }
+
+ if p, ok := os.LookupEnv(xdgConfigHome); ok {
+ p = path.Join(p, "pulse", "cookie")
+ if s, err := os.Stat(p); err != nil {
+ if !errors.Is(err, fs.ErrNotExist) {
+ fatal("Error accessing PulseAudio cookie:", err)
+ // unreachable
+ return p
+ }
+ } else if !s.IsDir() {
+ return p
+ }
+ }
+
+ fatal(fmt.Sprintf("Cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)",
+ pulseCookie, xdgConfigHome, home))
+ return ""
+}
+
func which(file string) (string, bool) {
- path, err := exec.LookPath(file)
- return path, err == nil
+ p, err := exec.LookPath(file)
+ return p, err == nil
}
func copyFile(dst, src string) error {