Re-add paplay as an option and make audio files configurable

This commit is contained in:
Tulir Asokan 2021-09-19 20:51:08 -04:00
parent 5b70a6f1e5
commit 580393f218

View File

@ -18,7 +18,39 @@
package notification package notification
import "os/exec" import (
"os"
"os/exec"
)
var audioCommand string
var tryAudioCommands = []string{"paplay", "ogg123"}
var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga"
var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga"
func getSoundPath(env, defaultPath string) string {
if path, ok := os.LookupEnv(env); ok {
// Sound file overriden by environment
return path
} else if _, err := os.Stat(defaultPath); os.IsNotExist(err) {
// Sound file doesn't exist, disable it
return ""
} else {
// Default sound file exists and wasn't overridden by environment
return defaultPath
}
}
func init() {
var err error
for _, cmd := range tryAudioCommands {
if audioCommand, err = exec.LookPath(cmd); err == nil {
break
}
}
soundNormal = getSoundPath("GOMUKS_SOUND_NORMAL", soundNormal)
soundCritical = getSoundPath("GOMUKS_SOUND_CRITICAL", soundCritical)
}
func Send(title, text string, critical, sound bool) error { func Send(title, text string, critical, sound bool) error {
args := []string{"-a", "gomuks"} args := []string{"-a", "gomuks"}
@ -29,12 +61,12 @@ func Send(title, text string, critical, sound bool) error {
// args = append(args, "-i", iconPath) // args = append(args, "-i", iconPath)
//} //}
args = append(args, title, text) args = append(args, title, text)
if sound { if sound && len(audioCommand) > 0 && len(soundNormal) > 0 {
soundName := "message-new-instant" audioFile := soundNormal
if critical { if critical && len(soundCritical) > 0 {
soundName = "complete" audioFile = soundCritical
} }
exec.Command("ogg123", "/usr/share/sounds/freedesktop/stereo/"+soundName+".oga").Run() _ = exec.Command(audioCommand, audioFile).Run()
} }
return exec.Command("notify-send", args...).Run() return exec.Command("notify-send", args...).Run()
} }