diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-07-15 16:25:44 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-07-15 16:25:44 +0900 |
| commit | 0bd452ad9b773dd389ef9a858d8d48b922bf5ec6 (patch) | |
| tree | 42e78ccd9627f71fc2ac1ad5b2d5bfc9947b0400 | |
| parent | 7d96b0bf350d87463f4c082342d98c54aefe2b51 (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>
| -rw-r--r-- | util.go | 42 |
1 files changed, 40 insertions, 2 deletions
@@ -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 { |
