From 595b8ab9b4feaa3f4eb1bb5064737c7b2ce13473 Mon Sep 17 00:00:00 2001 From: Nicu Reut Date: Fri, 11 Dec 2020 19:04:25 +0100 Subject: [PATCH] Remember last used device and preselect pulseaudio default --- config.go | 3 ++- main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- ui.go | 2 ++ 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index b45e829..d717b11 100644 --- a/config.go +++ b/config.go @@ -14,13 +14,14 @@ type config struct { Threshold int DisplayMonitorSources bool EnableUpdates bool + LastUsedInput string } const configFile = "config.toml" func initializeConfigIfNot() { 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! // This isn't and never was the proper location to disable the updater. diff --git a/main.go b/main.go index b505df0..a46e7e7 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ func main() { flag.IntVar(&pulsepid, "removerlimit", -1, "for internal use only") flag.BoolVar(&setcap, "setcap", false, "for internal use only") 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.IntVar(&threshold, "t", -1, "Voice activation threshold") flag.BoolVar(&list, "l", false, "List available PulseAudio sources") @@ -124,17 +124,21 @@ func main() { if load { ctx.paClient = paClient - if sourceName == "" { - fmt.Fprintf(os.Stderr, "No source specified to load.\n") - os.Exit(1) - } + sources := getSources(paClient) if supressorState(paClient) != unloaded { fmt.Fprintf(os.Stderr, "Supressor is already loaded.\n") 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 { if sources[i].ID == sourceName { err := loadSupressor(&ctx, sources[i]) @@ -145,7 +149,6 @@ func main() { os.Exit(0) } } - fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", sourceName) os.Exit(1) @@ -231,10 +234,46 @@ func paConnectionWatchdog(ctx *ntcontext) { ctx.paClient = paClient go updateNoiseSupressorLoaded(paClient, &ctx.noiseSupressorState) - ctx.inputList = getSources(paClient) + ctx.inputList = getSourcesWithPreSelectedInput(ctx) resetUI(ctx) 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 +} diff --git a/ui.go b/ui.go index 65e3689..53d5133 100644 --- a/ui.go +++ b/ui.go @@ -202,6 +202,8 @@ func updatefn(ctx *ntcontext, w *nucular.Window) { time.Sleep(time.Millisecond * 500) } } + ctx.config.LastUsedInput = inp.ID + go writeConfig(ctx.config) ctx.loadingScreen = false (*ctx.masterWindow).Changed() }()