From 4243d5036d1e7d339ca8ee3ff8a0be88a9f0d92c Mon Sep 17 00:00:00 2001 From: lawl Date: Sun, 2 Aug 2020 11:40:44 +0200 Subject: [PATCH] Don't fail to display UI when pa connection fails Additionaly we now re-establish the connection if it fails while we are already running. --- go.mod | 2 +- go.sum | 6 +- main.go | 83 ++++++++++++--------- module.go | 5 ++ ui.go | 10 +++ vendor/github.com/lawl/pulseaudio/client.go | 16 +++- vendor/modules.txt | 2 +- 7 files changed, 79 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 874acc5..c279578 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-20200721170903-af3730493af9 + github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0 golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 ) diff --git a/go.sum b/go.sum index d9ee030..4e11754 100644 --- a/go.sum +++ b/go.sum @@ -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-20200721164730-42f022e1b3ee h1:1ehdFhVi5cpSq8FAiSZC+Pe00NmAMT8bSN0axLtr7pE= -github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290= -github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9 h1:HGt/8C24W2BdSObJZYc8V6C8a7jDxjb9xefVHvjuK3o= -github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290= +github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0 h1:JrvOwrr1teFiqsp0EQxgEPJsm0pet+YLTL+HdYmnMx0= +github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0/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/main.go b/main.go index 39cd8f7..6bd2ca0 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "syscall" + "time" "github.com/aarzilli/nucular/font" @@ -66,42 +67,7 @@ func main() { go updateCheck(&ui) } - paClient, err := pulseaudio.NewClient() - defer paClient.Close() - - ui.paClient = paClient - if err != nil { - log.Fatalf("Couldn't create pulseaudio client: %v\n", err) - } - - go updateNoiseSupressorLoaded(paClient, &ui.noiseSupressorState) - - sources, err := paClient.Sources() - if err != nil { - log.Fatalf("Couldn't fetch sources from pulseaudio\n") - } - - inputs := make([]input, 0) - for i := range sources { - if sources[i].Name == "nui_mic_remap" { - continue - } - - log.Printf("Input %s, %+v\n", sources[i].Name, sources[i]) - - var inp input - - inp.ID = sources[i].Name - inp.Name = sources[i].PropList["device.description"] - inp.isMonitor = (sources[i].MonitorSourceIndex != 0xffffffff) - - //PA_SOURCE_DYNAMIC_LATENCY = 0x0040U - inp.dynamicLatency = sources[i].Flags&uint32(0x0040) != 0 - - inputs = append(inputs, inp) - } - - ui.inputList = inputs + go paConnectionWatchdog(&ui) wnd := nucular.NewMasterWindowSize(0, "NoiseTorch", image.Point{550, 300}, func(w *nucular.Window) { updatefn(w, &ui) @@ -131,3 +97,48 @@ func removeLib(file string) { } log.Printf("Deleted temp librnnoise: %s\n", file) } + +func paConnectionWatchdog(ui *uistate) { + for { + if ui.paClient.Connected() { + continue + } + + paClient, err := pulseaudio.NewClient() + if err != nil { + log.Printf("Couldn't create pulseaudio client: %v\n", err) + } + + ui.paClient = paClient + go updateNoiseSupressorLoaded(paClient, &ui.noiseSupressorState) + + sources, err := ui.paClient.Sources() + if err != nil { + log.Printf("Couldn't fetch sources from pulseaudio\n") + } + + inputs := make([]input, 0) + for i := range sources { + if sources[i].Name == "nui_mic_remap" { + continue + } + + log.Printf("Input %s, %+v\n", sources[i].Name, sources[i]) + + var inp input + + inp.ID = sources[i].Name + inp.Name = sources[i].PropList["device.description"] + inp.isMonitor = (sources[i].MonitorSourceIndex != 0xffffffff) + + //PA_SOURCE_DYNAMIC_LATENCY = 0x0040U + inp.dynamicLatency = sources[i].Flags&uint32(0x0040) != 0 + + inputs = append(inputs, inp) + } + + ui.inputList = inputs + + time.Sleep(500 * time.Millisecond) + } +} diff --git a/module.go b/module.go index 4a4ca7b..f2c0a4b 100644 --- a/module.go +++ b/module.go @@ -26,6 +26,11 @@ func updateNoiseSupressorLoaded(c *pulseaudio.Client, b *int) { for { *b = supressorState(c) + + if !c.Connected() { + break + } + <-upd } } diff --git a/ui.go b/ui.go index d4334fa..0b5b7e3 100644 --- a/ui.go +++ b/ui.go @@ -50,6 +50,11 @@ func updatefn(w *nucular.Window, ui *uistate) { return } + if !ui.paClient.Connected() { + connectScreen(w, ui) + return + } + w.MenubarBegin() w.Row(10).Dynamic(2) @@ -260,3 +265,8 @@ func versionScreen(w *nucular.Window, ui *uistate) { ui.versionScreen = false } } + +func connectScreen(w *nucular.Window, ui *uistate) { + w.Row(50).Dynamic(1) + w.Label("Connecting to pulseaudio...", "CB") +} diff --git a/vendor/github.com/lawl/pulseaudio/client.go b/vendor/github.com/lawl/pulseaudio/client.go index 0d88762..ac762a5 100644 --- a/vendor/github.com/lawl/pulseaudio/client.go +++ b/vendor/github.com/lawl/pulseaudio/client.go @@ -49,6 +49,7 @@ type Client struct { clientIndex int packets chan packet updates chan struct{} + connected bool } // NewClient establishes a connection to the PulseAudio server. @@ -67,9 +68,10 @@ func NewClient(addressArr ...string) (*Client, error) { } c := &Client{ - conn: conn, - packets: make(chan packet), - updates: make(chan struct{}, 1), + conn: conn, + packets: make(chan packet), + updates: make(chan struct{}, 1), + connected: true, } go c.processPackets() @@ -205,7 +207,10 @@ loop: } } } + // end of packet processing loop, e.g. disconnected + c.connected = false for _, p := range pending { + p.responseChan <- packetResponse{ buff: nil, err: fmt.Errorf("PulseAudio client was closed"), @@ -330,6 +335,11 @@ func exists(path string) bool { return false } +// Connected returns a bool specifying if the connection to pulse is alive +func (c *Client) Connected() bool { + return c != nil && c.connected +} + // 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 diff --git a/vendor/modules.txt b/vendor/modules.txt index f47ab25..48d69df 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-20200721170903-af3730493af9 +# github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0 ## explicit github.com/lawl/pulseaudio # golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899