Remember last used device and preselect pulseaudio default

pull/81/head 0.8.3
Nicu Reut 3 years ago committed by GitHub
parent 39c1e37dfa
commit 595b8ab9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,13 +14,14 @@ type config struct {
Threshold int Threshold int
DisplayMonitorSources bool DisplayMonitorSources bool
EnableUpdates bool EnableUpdates bool
LastUsedInput string
} }
const configFile = "config.toml" const configFile = "config.toml"
func initializeConfigIfNot() { func initializeConfigIfNot() {
log.Println("Checking if config needs to be initialized") log.Println("Checking if config needs to be initialized")
conf := config{Threshold: 95, DisplayMonitorSources: false, EnableUpdates: true} // if you're a package maintainer and you mess with this, we have a problem. conf := config{Threshold: 95, DisplayMonitorSources: false, EnableUpdates: true, LastUsedInput: ""} // if you're a package maintainer and you mess with this, we have a problem.
// Unless you set -tags release on the build the updater is *not* compiled in any. DO NOT MESS WITH THIS! // Unless you set -tags release on the build the updater is *not* compiled in any. DO NOT MESS WITH THIS!
// This isn't and never was the proper location to disable the updater. // This isn't and never was the proper location to disable the updater.

@ -44,7 +44,7 @@ func main() {
flag.IntVar(&pulsepid, "removerlimit", -1, "for internal use only") flag.IntVar(&pulsepid, "removerlimit", -1, "for internal use only")
flag.BoolVar(&setcap, "setcap", false, "for internal use only") flag.BoolVar(&setcap, "setcap", false, "for internal use only")
flag.StringVar(&sourceName, "s", "", "Use the specified source device ID") flag.StringVar(&sourceName, "s", "", "Use the specified source device ID")
flag.BoolVar(&load, "i", false, "Load supressor for input") flag.BoolVar(&load, "i", false, "Load supressor for input. If no source device ID is specified the default pulse audio source is used.")
flag.BoolVar(&unload, "u", false, "Unload supressor") flag.BoolVar(&unload, "u", false, "Unload supressor")
flag.IntVar(&threshold, "t", -1, "Voice activation threshold") flag.IntVar(&threshold, "t", -1, "Voice activation threshold")
flag.BoolVar(&list, "l", false, "List available PulseAudio sources") flag.BoolVar(&list, "l", false, "List available PulseAudio sources")
@ -124,17 +124,21 @@ func main() {
if load { if load {
ctx.paClient = paClient ctx.paClient = paClient
if sourceName == "" { sources := getSources(paClient)
fmt.Fprintf(os.Stderr, "No source specified to load.\n")
os.Exit(1)
}
if supressorState(paClient) != unloaded { if supressorState(paClient) != unloaded {
fmt.Fprintf(os.Stderr, "Supressor is already loaded.\n") fmt.Fprintf(os.Stderr, "Supressor is already loaded.\n")
os.Exit(1) os.Exit(1)
} }
sources := getSources(paClient) if sourceName == "" {
defaultSource, err := getDefaultSourceID(paClient)
if err != nil {
fmt.Fprintf(os.Stderr, "No source specified to load and failed to load default source: %+v\n", err)
os.Exit(1)
}
sourceName = defaultSource
}
for i := range sources { for i := range sources {
if sources[i].ID == sourceName { if sources[i].ID == sourceName {
err := loadSupressor(&ctx, sources[i]) err := loadSupressor(&ctx, sources[i])
@ -145,7 +149,6 @@ func main() {
os.Exit(0) os.Exit(0)
} }
} }
fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", sourceName) fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", sourceName)
os.Exit(1) os.Exit(1)
@ -231,10 +234,46 @@ func paConnectionWatchdog(ctx *ntcontext) {
ctx.paClient = paClient ctx.paClient = paClient
go updateNoiseSupressorLoaded(paClient, &ctx.noiseSupressorState) go updateNoiseSupressorLoaded(paClient, &ctx.noiseSupressorState)
ctx.inputList = getSources(paClient) ctx.inputList = getSourcesWithPreSelectedInput(ctx)
resetUI(ctx) resetUI(ctx)
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }
} }
func getSourcesWithPreSelectedInput(ctx *ntcontext) []input {
inputs := getSources(ctx.paClient)
preselectedInputID := &ctx.config.LastUsedInput
inputExists := false
if preselectedInputID != nil {
for _, input := range inputs {
inputExists = inputExists || input.ID == *preselectedInputID
}
}
if !inputExists {
defaultSource, err := getDefaultSourceID(ctx.paClient)
if err != nil {
log.Printf("Failed to load default source: %+v\n", err)
} else {
preselectedInputID = &defaultSource
}
}
if preselectedInputID != nil {
for i := range inputs {
if inputs[i].ID == *preselectedInputID {
inputs[i].checked = true
}
}
}
return inputs
}
func getDefaultSourceID(client *pulseaudio.Client) (string, error) {
server, err := client.ServerInfo()
if err != nil {
return "", err
}
return server.DefaultSource, nil
}

@ -202,6 +202,8 @@ func updatefn(ctx *ntcontext, w *nucular.Window) {
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)
} }
} }
ctx.config.LastUsedInput = inp.ID
go writeConfig(ctx.config)
ctx.loadingScreen = false ctx.loadingScreen = false
(*ctx.masterWindow).Changed() (*ctx.masterWindow).Changed()
}() }()

Loading…
Cancel
Save