diff --git a/main.go b/main.go index b7504a5..58aa686 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,7 @@ func main() { go Spawner() go ParseStdin() go CleanZombies() + go SVPAHandler() go PrintToI3bar() if Conf.AppButtons.Enabled { diff --git a/pulseaudio.go b/pulseaudio.go index e5aff38..67530a2 100644 --- a/pulseaudio.go +++ b/pulseaudio.go @@ -13,6 +13,8 @@ import ( var SoundVolume = "🔊:0%" var pa *p.Client +var SVPAHandlerChan = make(chan ClickEvent, 256) + // TODO: https://twin.sh/articles/44/add-a-timeout-to-any-function-in-go timeout pulseaudio calls // UpdateVolumeInfo updates info about current Sound Volume. @@ -155,3 +157,51 @@ func PaReinit() error { return err } + +func SVPAHandler() { + for e := range SVPAHandlerChan { + if e.Button == 3 { + RunChan <- Conf.SimpleVolumePa.RightClickCmd + + continue + } + + vol, err := pa.Volume() + + if err != nil { + if err := PaReinit(); err != nil { + log.Printf("Unable to get pulseaudio volume: %s", err) + } else { + vol, err = pa.Volume() + + if err != nil { + log.Printf("Unable to get volume pulseaudio server behaves weirdly: %s", err) + } + } + } + + switch e.Button { + case Conf.SimpleVolumePa.WheelUp: + vol += float32(Conf.SimpleVolumePa.Step) / 100 + + if vol > (float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100) { + vol = float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100 + } + + if err := pa.SetVolume(vol); err != nil { + log.Printf("Unable to set pulseaudio volume: %s", err) + } + + case Conf.SimpleVolumePa.WheelDown: + vol -= float32(Conf.SimpleVolumePa.Step) / 100 + + if vol < 0 { + vol = 0 + } + + if err := pa.SetVolume(vol); err != nil { + log.Printf("Unable to set pulseaudio volume: %s", err) + } + } + } +} diff --git a/stdin.go b/stdin.go index fc98e80..a07b825 100644 --- a/stdin.go +++ b/stdin.go @@ -95,49 +95,7 @@ func ParseStdin() { if e.Name == "simple-volume-pa" { if Conf.SimpleVolumePa.Enabled { - if e.Button == 3 { - RunChan <- Conf.SimpleVolumePa.RightClickCmd - - continue - } - - vol, err := pa.Volume() - - if err != nil { - if err := PaReinit(); err != nil { - log.Printf("Unable to get pulseaudio volume: %s", err) - } else { - vol, err = pa.Volume() - - if err != nil { - log.Printf("Unable to get volume pulseaudio server behaves weirdly: %s", err) - } - } - } - - switch e.Button { - case Conf.SimpleVolumePa.WheelUp: - vol += float32(Conf.SimpleVolumePa.Step) / 100 - - if vol > (float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100) { - vol = float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100 - } - - if err := pa.SetVolume(vol); err != nil { - log.Printf("Unable to set pulseaudio volume: %s", err) - } - - case Conf.SimpleVolumePa.WheelDown: - vol -= float32(Conf.SimpleVolumePa.Step) / 100 - - if vol < 0 { - vol = 0 - } - - if err := pa.SetVolume(vol); err != nil { - log.Printf("Unable to set pulseaudio volume: %s", err) - } - } + SVPAHandlerChan <- e } continue