Respect PULSE_RUNTME_PATH and PULSE_COOKIE

pull/43/head
lawl 4 years ago
parent 3fc46d2d30
commit 9bcb2a0c8d

@ -6,6 +6,6 @@ require (
gioui.org v0.0.0-20200630184602-223f8fd40ae4 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/aarzilli/nucular v0.0.0-20200615134801-81910c722bba
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f
github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
)

@ -17,10 +17,8 @@ github.com/golang/freetype v0.0.0-20161208064710-d9be45aaf745 h1:0d9whnMsm0iklqv
github.com/golang/freetype v0.0.0-20161208064710-d9be45aaf745/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7 h1:LjLOowMTfYESP3XW5/KV6TaQVuhdvkBHJoEaReGds6M=
github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f h1:MA90ko9/uptNdk2wRHAqm1fpyCogyFTKo162qPv2FgA=
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee h1:1ehdFhVi5cpSq8FAiSZC+Pe00NmAMT8bSN0axLtr7pE=
github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

@ -1,22 +1,25 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"unsafe"
"github.com/lawl/pulseaudio"
)
const rlimitRTTime = 15
func getPulsePid() (int, error) {
pulsepidfile := filepath.Join(xdgOrFallback("XDG_RUNTIME_DIR", fmt.Sprintf("/run/user/%d", os.Getuid())), "pulse/pid")
pulsepidfile, err := pulseaudio.RuntimePath("pid")
if err != nil {
return 0, err
}
pidbuf, err := ioutil.ReadFile(pulsepidfile)
if err != nil {
return 0, err

@ -15,7 +15,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/user"
@ -25,8 +24,6 @@ import (
const version = 32
var defaultAddr = fmt.Sprintf("/run/user/%d/pulse/native", os.Getuid())
type packetResponse struct {
buff *bytes.Buffer
err error
@ -57,7 +54,11 @@ type Client struct {
// NewClient establishes a connection to the PulseAudio server.
func NewClient(addressArr ...string) (*Client, error) {
if len(addressArr) < 1 {
addressArr = []string{filepath.Join(xdgOrFallback("XDG_RUNTIME_DIR", fmt.Sprintf("/run/user/%d", os.Getuid())), "pulse/native")}
rtp, err := RuntimePath("native")
if err != nil {
return nil, err
}
addressArr = []string{rtp}
}
conn, err := net.Dial("unix", addressArr[0])
@ -254,7 +255,10 @@ func (c *Client) addPacket(data packet) (err error) {
func (c *Client) auth() error {
const protocolVersionMask = 0x0000FFFF
cookiePath := filepath.Join(xdgOrFallback("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), "/.config")), "pulse/cookie")
cookiePath, err := cookiePath()
if err != nil {
return err
}
cookie, err := ioutil.ReadFile(cookiePath)
if err != nil {
return err
@ -315,27 +319,64 @@ func (c *Client) Close() {
c.conn.Close()
}
func exists(path string) (bool, error) {
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true, nil
return true
}
if os.IsNotExist(err) {
return false, nil
return false
}
return false, err
return false
}
func xdgOrFallback(xdg string, fallback string) string {
dir := os.Getenv(xdg)
if dir != "" {
if ok, err := exists(dir); ok && err == nil {
log.Printf("Resolved $%s to '%s'\n", xdg, dir)
return dir
// RuntimePath resolves a file in the pulse runtime path
// E.g. pass "native" to get the address for pulse' native socket
// Original implementation: https://github.com/pulseaudio/pulseaudio/blob/6c58c69bb6b937c1e758410d3114fc3bc0606fbe/src/pulsecore/core-util.c
// Except we do not support legacy $HOME paths
func RuntimePath(fn string) (string, error) {
if rtp := os.Getenv("PULSE_RUNTIME_PATH"); rtp != "" {
return filepath.Join(rtp, fn), nil
}
if xdgdir := os.Getenv("XDG_RUNTIME_DIR"); xdgdir != "" {
if exists(xdgdir) {
return filepath.Join(xdgdir, "/pulse/", fn), nil
}
}
defaultxdg := fmt.Sprintf("/run/user/%d", os.Getuid())
if exists(defaultxdg) {
return filepath.Join(defaultxdg, "/pulse/", fn), nil
}
return "", fmt.Errorf("No valid directory for Pulse RuntimePath found")
}
func cookiePath() (string, error) {
p := filepath.Join(os.Getenv("PULSE_COOKIE"))
if exists(p) {
return p, nil
}
if confHome := os.Getenv("XDG_CONFIG_HOME"); confHome != "" {
cookie := filepath.Join(os.Getenv("HOME"), "/pulse/cookie")
if exists(cookie) {
return cookie, nil
}
}
p = filepath.Join(os.Getenv("HOME"), "/.config/pulse/cookie")
if exists(p) {
return p, nil
}
p = filepath.Join(os.Getenv("HOME"), "/.pulse_cookie")
if exists(p) {
return p, nil
}
log.Printf("Couldn't resolve $%s falling back to '%s'\n", xdg, fallback)
return fallback
return "", fmt.Errorf("No valid path for Pulse cookie found")
}

@ -62,7 +62,7 @@ github.com/golang/freetype/truetype
# github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad
github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f
# github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee
## explicit
github.com/lawl/pulseaudio
# golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899

Loading…
Cancel
Save