diff options
author | Nicu Reut <reutnicu@gmail.com> | 2020-12-11 19:04:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-11 19:04:25 +0100 |
commit | 595b8ab9b4feaa3f4eb1bb5064737c7b2ce13473 (patch) | |
tree | 2670c8922c5acdd321b900606628c992db16881f | |
parent | 39c1e37dfa6109c85350c47dba2cebbdcd4334b4 (diff) | |
download | noisetorch-595b8ab9b4feaa3f4eb1bb5064737c7b2ce13473.tar.gz noisetorch-595b8ab9b4feaa3f4eb1bb5064737c7b2ce13473.zip |
Remember last used device and preselect pulseaudio default
-rw-r--r-- | config.go | 3 | ||||
-rw-r--r-- | main.go | 55 | ||||
-rw-r--r-- | ui.go | 2 |
3 files changed, 51 insertions, 9 deletions
@@ -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. @@ -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 +} @@ -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() }() |