diff options
author | Dave Reisner <dreisner@archlinux.org> | 2013-01-03 11:12:03 -0500 |
---|---|---|
committer | Dave Reisner <dreisner@archlinux.org> | 2013-01-03 14:05:55 -0500 |
commit | 130d9bbe6fa59404f71536fd9e7e97f2cdf05b14 (patch) | |
tree | 53aa2987dbb326c35d8a17d1ff3c8604936a46de | |
parent | 6d04ac9ad89293ecc08013d6df97b880ec1e3a69 (diff) | |
download | mirror-ponymix-130d9bbe6fa59404f71536fd9e7e97f2cdf05b14.tar.gz mirror-ponymix-130d9bbe6fa59404f71536fd9e7e97f2cdf05b14.tar.bz2 mirror-ponymix-130d9bbe6fa59404f71536fd9e7e97f2cdf05b14.zip |
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.
-rw-r--r-- | ponymix.cc | 45 |
1 files changed, 21 insertions, 24 deletions
@@ -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); } |