From 130d9bbe6fa59404f71536fd9e7e97f2cdf05b14 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 3 Jan 2013 11:12:03 -0500 Subject: resolve the card choice as late as possible Avoid some needless churn of back and forth between a card name and Card*. We rarely actually need the card unless we're performing an operation on it, so delay it as long as possible. Add a convenience function to resolve the active card or die. --- ponymix.cc | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/ponymix.cc b/ponymix.cc index 31765fa..459305f 100644 --- a/ponymix.cc +++ b/ponymix.cc @@ -226,11 +226,24 @@ static int ListCardsShort(PulseClient& ponymix, int, char*[]) { return list_cards(ponymix, true); } -static int list_profiles(PulseClient& ponymix, bool shirt) { - if (opt_card == nullptr) errx(1, "error: no card selected"); +static Card* resolve_active_card_or_die(PulseClient& ponymix) { + Card* card; + if (opt_card == nullptr) { + auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); + card = ponymix.GetCard(*device); + if (card == nullptr) errx(1, "error: no card found or selected."); + } else { + card = ponymix.GetCard(opt_card); + if (card == nullptr) { + errx(1, "error: no match found for card: %s", opt_card); + } + } + + return card; +} - auto card = ponymix.GetCard(opt_card); - if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); +static int list_profiles(PulseClient& ponymix, bool shirt) { + auto card = resolve_active_card_or_die(ponymix); const auto& profiles = card->Profiles(); for (const auto& p : profiles) Print(p, @@ -387,22 +400,14 @@ static int SetDefault(PulseClient& ponymix, int, char*[]) { } static int GetProfile(PulseClient& ponymix, int, char*[]) { - if (opt_card == nullptr) errx(1, "error: no card selected"); - - auto card = ponymix.GetCard(opt_card); - if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); - + auto card = resolve_active_card_or_die(ponymix); printf("%s\n", card->ActiveProfile().name.c_str()); return true; } static int SetProfile(PulseClient& ponymix, int, char* argv[]) { - if (opt_card == nullptr) errx(1, "error: no card selected"); - - auto card = ponymix.GetCard(opt_card); - if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); - + auto card = resolve_active_card_or_die(ponymix); return !ponymix.SetProfile(*card, argv[0]); } @@ -619,7 +624,8 @@ int main(int argc, char* argv[]) { PulseClient ponymix("ponymix"); ponymix.Populate(); - // defaults + // defaults. intentionally, we don't set a card -- only get + // that on demand if a function needs it. ServerInfo defaults = ponymix.GetDefaults(); opt_action = "defaults"; opt_devtype = DEVTYPE_SINK; @@ -629,15 +635,6 @@ int main(int argc, char* argv[]) { argc -= optind; argv += optind; - // cards are tricky... find the one that belongs to the chosen sink. - if (opt_card == nullptr) { - const Device* device = ponymix.GetDevice(opt_device, opt_devtype); - if (device) { - const Card* card = ponymix.GetCard(*device); - if (card) opt_card = card->Name().c_str(); - } - } - return CommandDispatch(ponymix, argc, argv); } -- cgit v1.2.3