Now all indicator updates properly
This commit is contained in:
@@ -10,30 +10,25 @@ import (
|
||||
p "github.com/mafik/pulseaudio"
|
||||
)
|
||||
|
||||
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.
|
||||
func (c MyConfig) UpdateVolumeInfo() {
|
||||
func (c *MyConfig) UpdateVolumeInfo() {
|
||||
var err error
|
||||
|
||||
pa, err = p.NewClient()
|
||||
c.Values.PA, err = p.NewClient()
|
||||
|
||||
// It can happen if no pulseaudio server running for current user.
|
||||
// If no server running we have to run one.
|
||||
if err != nil {
|
||||
if err := PaReinit(); err != nil {
|
||||
if err := c.PaReinit(); err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
vol, err := pa.Volume()
|
||||
vol, err := c.Values.PA.Volume()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable get volume from pulseaudio server: %s", err)
|
||||
@@ -41,7 +36,7 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
SoundVolume = fmt.Sprintf(
|
||||
c.Values.SoundVolume = fmt.Sprintf(
|
||||
"<span color='%s' background='%s' font='%s' size='%s'>%s</span>",
|
||||
c.SimpleVolumePa.Color,
|
||||
c.SimpleVolumePa.Background,
|
||||
@@ -50,7 +45,7 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
c.SimpleVolumePa.Symbol,
|
||||
)
|
||||
|
||||
SoundVolume += fmt.Sprintf(
|
||||
c.Values.SoundVolume += fmt.Sprintf(
|
||||
"<span color='%s' background='%s' font='%s' size='%s'>:%d%%</span>",
|
||||
c.SimpleVolumePa.Color,
|
||||
c.SimpleVolumePa.Background,
|
||||
@@ -59,11 +54,11 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
int64(vol*100),
|
||||
)
|
||||
|
||||
c.UpdateReady <- true
|
||||
c.Channels.UpdateReady <- true
|
||||
|
||||
for {
|
||||
// Subscribe to update notification channel, to get info that volume changed.
|
||||
pulseUpdate, err := pa.Updates()
|
||||
pulseUpdate, err := c.Values.PA.Updates()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to subscribe to pulseaudio updates: %s", err)
|
||||
@@ -73,7 +68,7 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
|
||||
// Rake update events.
|
||||
for range pulseUpdate {
|
||||
vol, err = pa.Volume()
|
||||
vol, err = c.Values.PA.Volume()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable get volume from pulseaudio server: %s", err)
|
||||
@@ -81,7 +76,7 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
SoundVolume = fmt.Sprintf(
|
||||
c.Values.SoundVolume = fmt.Sprintf(
|
||||
"<span color='%s' background='%s' font='%s' size='%s'>%s</span>",
|
||||
c.SimpleVolumePa.Color,
|
||||
c.SimpleVolumePa.Background,
|
||||
@@ -90,7 +85,7 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
c.SimpleVolumePa.Symbol,
|
||||
)
|
||||
|
||||
SoundVolume += fmt.Sprintf(
|
||||
c.Values.SoundVolume += fmt.Sprintf(
|
||||
"<span color='%s' background='%s' font='%s' size='%s'>:%d%%</span>",
|
||||
c.SimpleVolumePa.Color,
|
||||
c.SimpleVolumePa.Background,
|
||||
@@ -99,10 +94,10 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
int64(vol*100),
|
||||
)
|
||||
|
||||
c.UpdateReady <- true
|
||||
c.Channels.UpdateReady <- true
|
||||
}
|
||||
|
||||
if err := PaReinit(); err != nil {
|
||||
if err := c.PaReinit(); err != nil {
|
||||
log.Print(err)
|
||||
|
||||
return
|
||||
@@ -110,11 +105,11 @@ func (c MyConfig) UpdateVolumeInfo() {
|
||||
}
|
||||
|
||||
// This code is unreachable :(
|
||||
pa.Close() //nolint:govet
|
||||
c.Values.PA.Close() //nolint:govet
|
||||
}
|
||||
|
||||
// PaReinit re-inits pulseaudio and connection to it.
|
||||
func PaReinit() error {
|
||||
func (c *MyConfig) PaReinit() error {
|
||||
var err error
|
||||
|
||||
// If we out of updates, seems someone killed pulseaudio server. Restart it.
|
||||
@@ -157,7 +152,7 @@ func PaReinit() error {
|
||||
// This setting defines if pulseaudio will be run in manner that allows it to exit is other login detected (how
|
||||
// they guess it - I do not know). In general case we do not want to allow pa to exit and leave our session without
|
||||
// audio.
|
||||
if Conf.SimpleVolumePa.DontExitOnLogin {
|
||||
if c.SimpleVolumePa.DontExitOnLogin {
|
||||
// Do not exit on any kind of login/logout events.
|
||||
cmd = exec.Command("pulseaudio", "--exit-idle-time=-1", "--start")
|
||||
} else {
|
||||
@@ -169,7 +164,7 @@ func PaReinit() error {
|
||||
return fmt.Errorf("unable to initialize pulseaudio server instance: %w", err)
|
||||
}
|
||||
|
||||
pa, err = p.NewClient()
|
||||
c.Values.PA, err = p.NewClient()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to make client connection to pulseaudio: %w", err)
|
||||
@@ -178,21 +173,21 @@ func PaReinit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c MyConfig) SVPAHandler() {
|
||||
for e := range SVPAHandlerChan {
|
||||
func (c *MyConfig) SVPAHandler() {
|
||||
for e := range c.Channels.SVPAHandlerChan {
|
||||
if e.Button == 3 {
|
||||
RunChan <- Conf.SimpleVolumePa.RightClickCmd
|
||||
c.Channels.RunChan <- c.SimpleVolumePa.RightClickCmd
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
vol, err := pa.Volume()
|
||||
vol, err := c.Values.PA.Volume()
|
||||
|
||||
if err != nil {
|
||||
if err := PaReinit(); err != nil {
|
||||
if err := c.PaReinit(); err != nil {
|
||||
log.Printf("Unable to get pulseaudio volume: %s", err)
|
||||
} else {
|
||||
vol, err = pa.Volume()
|
||||
vol, err = c.Values.PA.Volume()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to get volume pulseaudio server behaves weirdly: %s", err)
|
||||
@@ -201,25 +196,25 @@ func (c MyConfig) SVPAHandler() {
|
||||
}
|
||||
|
||||
switch e.Button {
|
||||
case Conf.SimpleVolumePa.WheelUp:
|
||||
vol += float32(Conf.SimpleVolumePa.Step) / 100
|
||||
case c.SimpleVolumePa.WheelUp:
|
||||
vol += float32(c.SimpleVolumePa.Step) / 100
|
||||
|
||||
if vol > (float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100) {
|
||||
vol = float32(Conf.SimpleVolumePa.MaxVolumeLimit) / 100
|
||||
if vol > (float32(c.SimpleVolumePa.MaxVolumeLimit) / 100) {
|
||||
vol = float32(c.SimpleVolumePa.MaxVolumeLimit) / 100
|
||||
}
|
||||
|
||||
if err := pa.SetVolume(vol); err != nil {
|
||||
if err := c.Values.PA.SetVolume(vol); err != nil {
|
||||
log.Printf("Unable to set pulseaudio volume: %s", err)
|
||||
}
|
||||
|
||||
case Conf.SimpleVolumePa.WheelDown:
|
||||
vol -= float32(Conf.SimpleVolumePa.Step) / 100
|
||||
case c.SimpleVolumePa.WheelDown:
|
||||
vol -= float32(c.SimpleVolumePa.Step) / 100
|
||||
|
||||
if vol < 0 {
|
||||
vol = 0
|
||||
}
|
||||
|
||||
if err := pa.SetVolume(vol); err != nil {
|
||||
if err := c.Values.PA.SetVolume(vol); err != nil {
|
||||
log.Printf("Unable to set pulseaudio volume: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user