diff --git a/config.go b/config.go index b45e829..fbe746f 100644 --- a/config.go +++ b/config.go @@ -14,6 +14,7 @@ type config struct { Threshold int DisplayMonitorSources bool EnableUpdates bool + LastUsedInput *string } const configFile = "config.toml" diff --git a/main.go b/main.go index b505df0..31033bd 100644 --- a/main.go +++ b/main.go @@ -231,10 +231,43 @@ func paConnectionWatchdog(ctx *ntcontext) { ctx.paClient = paClient go updateNoiseSupressorLoaded(paClient, &ctx.noiseSupressorState) - ctx.inputList = getSources(paClient) + ctx.inputList = getSourcesWithPreSelectedInput(paClient, ctx.config) resetUI(ctx) time.Sleep(500 * time.Millisecond) } } + +func getSourcesWithPreSelectedInput(client *pulseaudio.Client, config *config) []input { + inputs := getSources(client) + preselectedInputId := config.LastUsedInput + inputExists := false + for _, input := range inputs { + inputExists = inputExists || input.ID == *preselectedInputId + } + if !inputExists { + defaultSource, err := getDefaultSourceID(client) + if err != nil { + log.Printf("Failed to load default source\n") + } 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 +}