Use async handler instead of applying sound volume in stdin parser

This commit is contained in:
eleksir
2024-01-19 01:07:13 +03:00
parent bf9893feba
commit d47ce88c0d
3 changed files with 52 additions and 43 deletions

View File

@@ -59,6 +59,7 @@ func main() {
go Spawner() go Spawner()
go ParseStdin() go ParseStdin()
go CleanZombies() go CleanZombies()
go SVPAHandler()
go PrintToI3bar() go PrintToI3bar()
if Conf.AppButtons.Enabled { if Conf.AppButtons.Enabled {

View File

@@ -13,6 +13,8 @@ import (
var SoundVolume = "🔊:0%" var SoundVolume = "🔊:0%"
var pa *p.Client 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 // TODO: https://twin.sh/articles/44/add-a-timeout-to-any-function-in-go timeout pulseaudio calls
// UpdateVolumeInfo updates info about current Sound Volume. // UpdateVolumeInfo updates info about current Sound Volume.
@@ -155,3 +157,51 @@ func PaReinit() error {
return err 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)
}
}
}
}

View File

@@ -95,49 +95,7 @@ func ParseStdin() {
if e.Name == "simple-volume-pa" { if e.Name == "simple-volume-pa" {
if Conf.SimpleVolumePa.Enabled { if Conf.SimpleVolumePa.Enabled {
if e.Button == 3 { SVPAHandlerChan <- e
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)
}
}
} }
continue continue