diff --git a/config.go b/config.go index 6482b04..de16249 100644 --- a/config.go +++ b/config.go @@ -16,13 +16,12 @@ type config struct { EnableUpdates bool } -const configDir = ".config/noisetorch/" const configFile = "config.toml" func initializeConfigIfNot() { log.Println("Checking if config needs to be initialized") conf := config{Threshold: 95, DisplayMonitorSources: false, EnableUpdates: true} - configdir := filepath.Join(os.Getenv("HOME"), configDir) + configdir := configDir() ok, err := exists(configdir) if err != nil { log.Fatalf("Couldn't check if config directory exists: %v\n", err) @@ -45,7 +44,7 @@ func initializeConfigIfNot() { } func readConfig() *config { - f := filepath.Join(os.Getenv("HOME"), configDir, configFile) + f := filepath.Join(configDir(), configFile) config := config{} if _, err := toml.DecodeFile(f, &config); err != nil { log.Fatalf("Couldn't read config file: %v\n", err) @@ -55,7 +54,7 @@ func readConfig() *config { } func writeConfig(conf *config) { - f := filepath.Join(os.Getenv("HOME"), configDir, configFile) + f := filepath.Join(configDir(), configFile) var buffer bytes.Buffer if err := toml.NewEncoder(&buffer).Encode(&conf); err != nil { log.Fatalf("Couldn't write config file: %v\n", err) @@ -63,6 +62,10 @@ func writeConfig(conf *config) { ioutil.WriteFile(f, []byte(buffer.String()), 0644) } +func configDir() string { + return filepath.Join(xdgOrFallback("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config")), "noisetorch") +} + func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { @@ -73,3 +76,17 @@ func exists(path string) (bool, error) { } return false, err } + +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 + } + + } + + log.Printf("Couldn't resolve $%s falling back to '%s'\n", xdg, fallback) + return fallback +} diff --git a/go.mod b/go.mod index aeede58..3e8e221 100644 --- a/go.mod +++ b/go.mod @@ -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-20200704145757-7d4b4b92e7b7 + github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 ) diff --git a/go.sum b/go.sum index b773f61..e7083ab 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0Pv 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= 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= diff --git a/rlimit.go b/rlimit.go index 1355965..bb76c5e 100644 --- a/rlimit.go +++ b/rlimit.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/ioutil" "log" "os" @@ -15,8 +16,7 @@ import ( const rlimitRTTime = 15 func getPulsePid() (int, error) { - runtimeDir := os.Getenv("XDG_RUNTIME_DIR") - pulsepidfile := filepath.Join(runtimeDir, "pulse/pid") + pulsepidfile := filepath.Join(xdgOrFallback("XDG_RUNTIME_DIR", fmt.Sprintf("/run/user/%d", os.Getuid())), "pulse/pid") pidbuf, err := ioutil.ReadFile(pulsepidfile) if err != nil { return 0, err diff --git a/vendor/github.com/lawl/pulseaudio/README.md b/vendor/github.com/lawl/pulseaudio/README.md index 17eb764..372f6ff 100644 --- a/vendor/github.com/lawl/pulseaudio/README.md +++ b/vendor/github.com/lawl/pulseaudio/README.md @@ -1,4 +1,4 @@ -# pulseaudio +# pulseaudio [![GoDoc](https://godoc.org/github.com/lawl/pulseaudio?status.svg)](https://godoc.org/github.com/lawl/pulseaudio) Package pulseaudio is a pure-Go (no libpulse) implementation of the PulseAudio native protocol. Download: @@ -12,4 +12,6 @@ Package pulseaudio is a pure-Go (no libpulse) implementation of the PulseAudio n This library is a fork of https://github.com/mafik/pulseaudio The original library deliberately tries to hide pulseaudio internals and doesn't expose them. -For my usecase I needed the exact opposite, access to pulseaudio internals. \ No newline at end of file +For my usecase I needed the exact opposite, access to pulseaudio internals. +I will most likely only maintain this as far as is required for [noisetorch](https://github.com/lawl/NoiseTorch) to work. +Pull Requests are however welcome. diff --git a/vendor/github.com/lawl/pulseaudio/client.go b/vendor/github.com/lawl/pulseaudio/client.go index 6740182..ab8c347 100644 --- a/vendor/github.com/lawl/pulseaudio/client.go +++ b/vendor/github.com/lawl/pulseaudio/client.go @@ -1,24 +1,12 @@ // Package pulseaudio is a pure-Go (no libpulse) implementation of the PulseAudio native protocol. // -// Rather than exposing the PulseAudio protocol directly this library attempts to hide -// the PulseAudio complexity behind a Go interface. -// Some of the things which are deliberately not exposed in the API are: -// -// → backwards compatibility for old PulseAudio servers -// -// → transport mechanism used for the connection (Unix sockets / memfd / shm) -// -// → encoding used in the pulseaudio-native protocol -// -// Working features -// -// Querying and setting the volume. -// -// Listing audio outputs. -// -// Changing the default audio output. -// -// Notifications on config updates. +// Package pulseaudio is a pure-Go (no libpulse) implementation of the PulseAudio native protocol. + +// This library is a fork of https://github.com/mafik/pulseaudio +// The original library deliberately tries to hide pulseaudio internals and doesn't expose them. + +// For my usecase I needed the exact opposite, access to pulseaudio internals. + package pulseaudio import ( @@ -27,10 +15,12 @@ import ( "fmt" "io" "io/ioutil" + "log" "net" "os" "os/user" "path" + "path/filepath" ) const version = 32 @@ -67,7 +57,7 @@ type Client struct { // NewClient establishes a connection to the PulseAudio server. func NewClient(addressArr ...string) (*Client, error) { if len(addressArr) < 1 { - addressArr = []string{defaultAddr} + addressArr = []string{filepath.Join(xdgOrFallback("XDG_RUNTIME_DIR", fmt.Sprintf("/run/user/%d", os.Getuid())), "pulse/native")} } conn, err := net.Dial("unix", addressArr[0]) @@ -264,7 +254,7 @@ func (c *Client) addPacket(data packet) (err error) { func (c *Client) auth() error { const protocolVersionMask = 0x0000FFFF - cookiePath := os.Getenv("HOME") + "/.config/pulse/cookie" + cookiePath := filepath.Join(xdgOrFallback("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), "/.config")), "pulse/cookie") cookie, err := ioutil.ReadFile(cookiePath) if err != nil { return err @@ -324,3 +314,28 @@ func (c *Client) Close() { close(c.packets) c.conn.Close() } + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +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 + } + + } + + log.Printf("Couldn't resolve $%s falling back to '%s'\n", xdg, fallback) + return fallback +} diff --git a/vendor/modules.txt b/vendor/modules.txt index c785aeb..4dc3e93 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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-20200704145757-7d4b4b92e7b7 +# github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f ## explicit github.com/lawl/pulseaudio # golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899