From 2a529357b7516922c600e5e95815da9f32dda3c9 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Mon, 31 Dec 2012 17:05:35 -0500 Subject: initial commit --- pulse.h | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 pulse.h (limited to 'pulse.h') diff --git a/pulse.h b/pulse.h new file mode 100644 index 0000000..1e91352 --- /dev/null +++ b/pulse.h @@ -0,0 +1,216 @@ +#pragma once + +// C +#include + +// C++ +#include +#include +#include + +// external +#include + +using std::string; +using std::vector; +using std::unique_ptr; + +enum DeviceType { + DEVTYPE_SINK, + DEVTYPE_SOURCE, + DEVTYPE_SINK_INPUT, + DEVTYPE_SOURCE_OUTPUT, +}; + +struct Profile { + Profile(const pa_card_profile_info& info) : + name(info.name), + desc(info.description) {} + + string name; + string desc; +}; + +struct Operations { + pa_operation* (*Mute)(pa_context*, uint32_t, int, pa_context_success_cb_t, void*); + pa_operation* (*SetVolume)(pa_context*, uint32_t, const pa_cvolume*, pa_context_success_cb_t, void*); + pa_operation* (*SetDefault)(pa_context*, const char*, pa_context_success_cb_t, void*); + pa_operation* (*Kill)(pa_context*, uint32_t, pa_context_success_cb_t, void*); + pa_operation* (*Move)(pa_context *, uint32_t, uint32_t, pa_context_success_cb_t, void *); +}; + +class Device { + public: + Device(const pa_source_info* info); + Device(const pa_sink_info* info); + Device(const pa_sink_input_info* info); + Device(const pa_source_output_info* info); + + uint32_t Index() const { return index_; } + const string& Name() const { return name_; } + const string& Desc() const { return desc_; } + int Volume() const { return volume_percent_; } + int Balance() const { return balance_; } + bool Muted() const { return mute_; } + enum DeviceType Type() const { return type_; } + + private: + friend class Pulse; + + void update_volume(const pa_cvolume& newvol); + + enum DeviceType type_; + uint32_t index_; + string name_; + string desc_; + pa_cvolume volume_; + int volume_percent_; + pa_channel_map channels_; + int mute_; + int balance_; + Operations ops_; +}; + +class Card { + public: + Card(const pa_card_info* info); + + const string& Name() const { return name_; } + uint32_t Index() const { return index_; } + const string& Driver() const { return driver_; } + + const vector& Profiles() const { return profiles_; } + const Profile& ActiveProfile() const { return active_profile_; } + + private: + friend class Pulse; + + uint32_t index_; + string name_; + uint32_t owner_module_; + string driver_; + vector profiles_; + Profile active_profile_; +}; + +struct ServerInfo { + string sink; + string source; +}; + +template +struct Range { + Range(T min, T max) : min(min), max(max) {} + + T clamp(T value) { + return value < min ? min : (value > max ? max : value); + } + + T min; + T max; +}; + +class Pulse { + public: + Pulse(string client_name); + ~Pulse(); + + // Populates all known devices and cards. Any currently known + // devices and cards are cleared before the new data is stored. + void Populate(); + + // Get a sink by index or name, or all sinks. + Device* GetSink(const uint32_t& index); + Device* GetSink(const string& name); + const vector& GetSinks() const { return sinks_; } + + // Get a source by index or name, or all sources. + Device* GetSource(const uint32_t& index); + Device* GetSource(const string& name); + const vector& GetSources() const { return sources_; } + + // Get a sink input by index or name, or all sink inputs. + Device* GetSinkInput(const uint32_t& name); + Device* GetSinkInput(const string& name); + const vector& GetSinkInputs() const { return sink_inputs_; } + + // Get a source output by index or name, or all source outputs. + Device* GetSourceOutput(const uint32_t& name); + Device* GetSourceOutput(const string& name); + const vector& GetSourceOutputs() const { return source_outputs_; } + + // Get a card by index or name, or all cards. + Card* GetCard(const uint32_t& index); + Card* GetCard(const string& name); + const vector& GetCards() const { return cards_; } + + // Get or set the volume of a device. + int GetVolume(const Device& device) const; + bool SetVolume(Device& device, long value); + + // Convenience wrappers for adjusting volume + bool IncreaseVolume(Device& device, long increment); + bool DecreaseVolume(Device& device, long decrement); + + // Get or set the volume of a device. Not all devices support this. + int GetBalance(const Device& device) const; + bool SetBalance(Device& device, long value); + + // Convenience wrappers for adjusting balance + bool IncreaseBalance(Device& device, long increment); + bool DecreaseBalance(Device& device, long decrement); + + // Get and set mute for a device. + bool IsMuted(const Device& device) const { return device.mute_; }; + bool SetMute(Device& device, bool mute); + + // Set the profile for a card by name. + bool SetProfile(Card& card, const string& profile); + + // Move a given source output or sink input to the destination. + bool Move(Device& source, Device& dest); + + // Kill a source output or sink input. + bool Kill(Device& device); + + // Get or set the default sink and source. + const ServerInfo& GetDefaults() const { return defaults_; } + bool SetDefault(Device& device); + + // Set minimum and maximum allowed volume + void SetVolumeRange(int min, int max) { + volume_range_ = { min, max }; + } + + // Set minimum and maximum allowed balance + void SetBalanceRange(int min, int max) { + balance_range_ = { min, max }; + } + + private: + void mainloop_iterate(pa_operation* op); + + void populate_server_info(); + void populate_cards(); + void populate_sinks(); + void populate_sources(); + + Device* get_device(vector& devices, const uint32_t& index); + Device* get_device(vector& devices, const string& name); + + void remove_device(Device& device); + + string client_name_; + pa_context* context_; + pa_mainloop* mainloop_; + vector sinks_; + vector sources_; + vector sink_inputs_; + vector source_outputs_; + vector cards_; + ServerInfo defaults_; + Range volume_range_; + Range balance_range_; +}; + +// vim: set et ts=2 sw=2: -- cgit v1.2.3 From aa6847b94511728250333125c3dd9bec7b65759a Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 2 Jan 2013 09:24:59 -0500 Subject: rename class Pulse to PulseClient --- ponymix.cc | 48 ++++++++++++++++++++++---------------------- pulse.cc | 68 +++++++++++++++++++++++++++++++------------------------------- pulse.h | 10 ++++----- 3 files changed, 63 insertions(+), 63 deletions(-) (limited to 'pulse.h') diff --git a/ponymix.cc b/ponymix.cc index 1f124df..a6ce950 100644 --- a/ponymix.cc +++ b/ponymix.cc @@ -102,7 +102,7 @@ static enum DeviceType string_to_devtype_or_die(const char* str) { } } -static Device* string_to_device(Pulse& ponymix, string arg, enum DeviceType type) { +static Device* string_to_device(PulseClient& ponymix, string arg, enum DeviceType type) { switch (type) { case DEVTYPE_SINK: return ponymix.GetSink(arg); @@ -117,7 +117,7 @@ static Device* string_to_device(Pulse& ponymix, string arg, enum DeviceType type } } -static Device* string_to_device_or_die(Pulse& ponymix, +static Device* string_to_device_or_die(PulseClient& ponymix, string arg, enum DeviceType type) { Device* device = string_to_device(ponymix, arg, type); @@ -146,14 +146,14 @@ static void Print(const Profile& profile, bool active) { profile.name.c_str(), profile.desc.c_str(), active ? " [active]" : ""); } -static int ShowDefaults(Pulse& ponymix, int, char*[]) { +static int ShowDefaults(PulseClient& ponymix, int, char*[]) { const auto& info = ponymix.GetDefaults(); Print(*ponymix.GetSink(info.sink)); Print(*ponymix.GetSource(info.source)); return 0; } -static int List(Pulse& ponymix, int argc, char*[]) { +static int List(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: list requires 0 arguments"); const auto& sinks = ponymix.GetSinks(); @@ -171,7 +171,7 @@ static int List(Pulse& ponymix, int argc, char*[]) { return 0; } -static int ListCards(Pulse& ponymix, int argc, char*[]) { +static int ListCards(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: list-cards requires 0 arguments"); const auto& cards = ponymix.GetCards(); @@ -180,7 +180,7 @@ static int ListCards(Pulse& ponymix, int argc, char*[]) { return 0; } -static int ListProfiles(Pulse& ponymix, int argc, char*[]) { +static int ListProfiles(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: list-profiles requires 0 arguments"); // TODO: figure out how to get a list of cards? @@ -193,7 +193,7 @@ static int ListProfiles(Pulse& ponymix, int argc, char*[]) { return 0; } -static int GetVolume(Pulse& ponymix, int argc, char*[]) { +static int GetVolume(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: get-volume requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -201,7 +201,7 @@ static int GetVolume(Pulse& ponymix, int argc, char*[]) { return 0; } -static int SetVolume(Pulse& ponymix, int argc, char* argv[]) { +static int SetVolume(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: set-volume requires exactly 1 argument"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -220,7 +220,7 @@ static int SetVolume(Pulse& ponymix, int argc, char* argv[]) { return 0; } -static int GetBalance(Pulse& ponymix, int argc, char*[]) { +static int GetBalance(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: get-balance requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -228,7 +228,7 @@ static int GetBalance(Pulse& ponymix, int argc, char*[]) { return 0; } -static int SetBalance(Pulse& ponymix, int argc, char* argv[]) { +static int SetBalance(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: set-balance requires exactly 1 argument"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -247,7 +247,7 @@ static int SetBalance(Pulse& ponymix, int argc, char* argv[]) { return 0; } -static int AdjBalance(Pulse& ponymix, int argc, char* argv[]) { +static int AdjBalance(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: adj-balance requires exactly 1 argument"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -266,7 +266,7 @@ static int AdjBalance(Pulse& ponymix, int argc, char* argv[]) { return 0; } -static int IncreaseVolume(Pulse& ponymix, int argc, char* argv[]) { +static int IncreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: increase requires exactly 1 argument"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -285,7 +285,7 @@ static int IncreaseVolume(Pulse& ponymix, int argc, char* argv[]) { return 0; } -static int DecreaseVolume(Pulse& ponymix, int argc, char* argv[]) { +static int DecreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: decrease requires exactly 1 argument"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -304,7 +304,7 @@ static int DecreaseVolume(Pulse& ponymix, int argc, char* argv[]) { return 0; } -static int Mute(Pulse& ponymix, int argc, char*[]) { +static int Mute(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: mute requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -316,7 +316,7 @@ static int Mute(Pulse& ponymix, int argc, char*[]) { return 0; } -static int Unmute(Pulse& ponymix, int argc, char*[]) { +static int Unmute(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: unmute requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -328,7 +328,7 @@ static int Unmute(Pulse& ponymix, int argc, char*[]) { return 0; } -static int ToggleMute(Pulse& ponymix, int argc, char*[]) { +static int ToggleMute(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: toggle requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -340,14 +340,14 @@ static int ToggleMute(Pulse& ponymix, int argc, char*[]) { return 0; } -static int IsMuted(Pulse& ponymix, int argc, char*[]) { +static int IsMuted(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: is-muted requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); return !ponymix.IsMuted(*device); } -static int SetDefault(Pulse& ponymix, int argc, char*[]) { +static int SetDefault(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: set-default requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -355,7 +355,7 @@ static int SetDefault(Pulse& ponymix, int argc, char*[]) { return !ponymix.SetDefault(*device); } -static int GetProfile(Pulse& ponymix, int argc, char*[]) { +static int GetProfile(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: get-profile requires 0 arguments"); auto card = ponymix.GetCard(opt_card); @@ -366,7 +366,7 @@ static int GetProfile(Pulse& ponymix, int argc, char*[]) { return true; } -static int SetProfile(Pulse& ponymix, int argc, char* argv[]) { +static int SetProfile(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: set-profile requires 1 argument"); auto card = ponymix.GetCard(opt_card); @@ -375,7 +375,7 @@ static int SetProfile(Pulse& ponymix, int argc, char* argv[]) { return !ponymix.SetProfile(*card, argv[0]); } -static int Move(Pulse& ponymix, int argc, char* argv[]) { +static int Move(PulseClient& ponymix, int argc, char* argv[]) { if (argc != 1) errx(1, "error: move requires 1 argument"); // this assignment is a lie. stfu g++ @@ -400,7 +400,7 @@ static int Move(Pulse& ponymix, int argc, char* argv[]) { return !ponymix.Move(*source, *target); } -static int Kill(Pulse& ponymix, int argc, char*[]) { +static int Kill(PulseClient& ponymix, int argc, char*[]) { if (argc != 0) errx(1, "error: set-default requires 0 arguments"); auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); @@ -408,7 +408,7 @@ static int Kill(Pulse& ponymix, int argc, char*[]) { return !ponymix.Kill(*device); } -static int (*fn[])(Pulse& ponymix, int argc, char* argv[]) = { +static int (*fn[])(PulseClient& ponymix, int argc, char* argv[]) = { [ACTION_DEFAULTS] = ShowDefaults, [ACTION_LIST] = List, [ACTION_LISTCARDS] = ListCards, @@ -533,7 +533,7 @@ bool parse_options(int argc, char** argv) { } int main(int argc, char* argv[]) { - Pulse ponymix("ponymix"); + PulseClient ponymix("ponymix"); ponymix.Populate(); // defaults diff --git a/pulse.cc b/pulse.cc index 77a1568..cbbe405 100644 --- a/pulse.cc +++ b/pulse.cc @@ -95,7 +95,7 @@ static int xstrtol(const char *str, long *out) { } // anonymous namespace -Pulse::Pulse(string client_name) : +PulseClient::PulseClient(string client_name) : client_name_(client_name), volume_range_(0, 150), balance_range_(-100, 100) { @@ -129,40 +129,40 @@ Pulse::Pulse(string client_name) : // // Pulse Client // -Pulse::~Pulse() { +PulseClient::~PulseClient() { pa_context_unref(context_); pa_mainloop_free(mainloop_); } -void Pulse::Populate() { +void PulseClient::Populate() { populate_server_info(); populate_sinks(); populate_sources(); populate_cards(); } -Card* Pulse::GetCard(const uint32_t& index) { +Card* PulseClient::GetCard(const uint32_t& index) { for (Card& card : cards_) { if (card.index_ == index) return &card; } return nullptr; } -Card* Pulse::GetCard(const string& name) { +Card* PulseClient::GetCard(const string& name) { for (Card& card : cards_) { if (card.name_ == name) return &card; } return nullptr; } -Device* Pulse::get_device(vector& devices, const uint32_t& index) { +Device* PulseClient::get_device(vector& devices, const uint32_t& index) { for (Device& device : devices) { if (device.index_ == index) return &device; } return nullptr; } -Device* Pulse::get_device(vector& devices, const string& name) { +Device* PulseClient::get_device(vector& devices, const string& name) { long val; if (xstrtol(name.c_str(), &val) == 0) return get_device(devices, val); @@ -172,46 +172,46 @@ Device* Pulse::get_device(vector& devices, const string& name) { return nullptr; } -Device* Pulse::GetSink(const uint32_t& index) { +Device* PulseClient::GetSink(const uint32_t& index) { return get_device(sinks_, index); } -Device* Pulse::GetSink(const string& name) { +Device* PulseClient::GetSink(const string& name) { return get_device(sinks_, name); } -Device* Pulse::GetSource(const uint32_t& index) { +Device* PulseClient::GetSource(const uint32_t& index) { return get_device(sources_, index); } -Device* Pulse::GetSource(const string& name) { +Device* PulseClient::GetSource(const string& name) { return get_device(sources_, name); } -Device* Pulse::GetSinkInput(const uint32_t& index) { +Device* PulseClient::GetSinkInput(const uint32_t& index) { return get_device(sink_inputs_, index); } -Device* Pulse::GetSinkInput(const string& name) { +Device* PulseClient::GetSinkInput(const string& name) { return get_device(sink_inputs_, name); } -Device* Pulse::GetSourceOutput(const uint32_t& index) { +Device* PulseClient::GetSourceOutput(const uint32_t& index) { return get_device(source_outputs_, index); } -Device* Pulse::GetSourceOutput(const string& name) { +Device* PulseClient::GetSourceOutput(const string& name) { return get_device(source_outputs_, name); } -void Pulse::mainloop_iterate(pa_operation* op) { +void PulseClient::mainloop_iterate(pa_operation* op) { int r; while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { pa_mainloop_iterate(mainloop_, 1, &r); } } -void Pulse::populate_cards() { +void PulseClient::populate_cards() { cards_.clear(); pa_operation* op = pa_context_get_card_info_list(context_, card_info_cb, @@ -220,7 +220,7 @@ void Pulse::populate_cards() { pa_operation_unref(op); } -void Pulse::populate_server_info() { +void PulseClient::populate_server_info() { pa_operation* op = pa_context_get_server_info(context_, server_info_cb, &defaults_); @@ -228,7 +228,7 @@ void Pulse::populate_server_info() { pa_operation_unref(op); } -void Pulse::populate_sinks() { +void PulseClient::populate_sinks() { sinks_.clear(); pa_operation* op = pa_context_get_sink_info_list(context_, device_info_cb, @@ -244,7 +244,7 @@ void Pulse::populate_sinks() { pa_operation_unref(op); } -void Pulse::populate_sources() { +void PulseClient::populate_sources() { sources_.clear(); pa_operation* op = pa_context_get_source_info_list(context_, device_info_cb, @@ -260,7 +260,7 @@ void Pulse::populate_sources() { pa_operation_unref(op); } -bool Pulse::SetMute(Device& device, bool mute) { +bool PulseClient::SetMute(Device& device, bool mute) { int success; if (device.ops_.Mute == nullptr) { @@ -281,7 +281,7 @@ bool Pulse::SetMute(Device& device, bool mute) { return success; } -bool Pulse::SetVolume(Device& device, long volume) { +bool PulseClient::SetVolume(Device& device, long volume) { int success; if (device.ops_.SetVolume == nullptr) { @@ -303,17 +303,17 @@ bool Pulse::SetVolume(Device& device, long volume) { return success; } -bool Pulse::IncreaseVolume(Device& device, long increment) { +bool PulseClient::IncreaseVolume(Device& device, long increment) { return SetVolume(device, volume_range_.clamp(device.volume_percent_ + increment)); } -bool Pulse::DecreaseVolume(Device& device, long increment) { +bool PulseClient::DecreaseVolume(Device& device, long increment) { return SetVolume(device, volume_range_.clamp(device.volume_percent_ - increment)); } -bool Pulse::SetBalance(Device& device, long balance) { +bool PulseClient::SetBalance(Device& device, long balance) { int success; if (device.ops_.SetVolume == nullptr) { @@ -337,25 +337,25 @@ bool Pulse::SetBalance(Device& device, long balance) { return success; } -bool Pulse::IncreaseBalance(Device& device, long increment) { +bool PulseClient::IncreaseBalance(Device& device, long increment) { return SetBalance(device, balance_range_.clamp(device.balance_ + increment)); } -bool Pulse::DecreaseBalance(Device& device, long increment) { +bool PulseClient::DecreaseBalance(Device& device, long increment) { return SetBalance(device, balance_range_.clamp(device.balance_ - increment)); } -int Pulse::GetVolume(const Device& device) const { +int PulseClient::GetVolume(const Device& device) const { return device.Volume(); } -int Pulse::GetBalance(const Device& device) const { +int PulseClient::GetBalance(const Device& device) const { return device.Balance(); } -bool Pulse::SetProfile(Card& card, const string& profile) { +bool PulseClient::SetProfile(Card& card, const string& profile) { int success; pa_operation* op = pa_context_set_card_profile_by_index(context_, @@ -379,7 +379,7 @@ bool Pulse::SetProfile(Card& card, const string& profile) { return success; } -bool Pulse::Move(Device& source, Device& dest) { +bool PulseClient::Move(Device& source, Device& dest) { int success; if (source.ops_.Move == nullptr) { @@ -398,7 +398,7 @@ bool Pulse::Move(Device& source, Device& dest) { return success; } -bool Pulse::Kill(Device& device) { +bool PulseClient::Kill(Device& device) { int success; if (device.ops_.Kill == nullptr) { @@ -418,7 +418,7 @@ bool Pulse::Kill(Device& device) { return success; } -bool Pulse::SetDefault(Device& device) { +bool PulseClient::SetDefault(Device& device) { int success; if (device.ops_.SetDefault == nullptr) { @@ -450,7 +450,7 @@ bool Pulse::SetDefault(Device& device) { return success; } -void Pulse::remove_device(Device& device) { +void PulseClient::remove_device(Device& device) { vector* devlist; switch (device.type_) { diff --git a/pulse.h b/pulse.h index 1e91352..9a61108 100644 --- a/pulse.h +++ b/pulse.h @@ -55,7 +55,7 @@ class Device { enum DeviceType Type() const { return type_; } private: - friend class Pulse; + friend class PulseClient; void update_volume(const pa_cvolume& newvol); @@ -83,7 +83,7 @@ class Card { const Profile& ActiveProfile() const { return active_profile_; } private: - friend class Pulse; + friend class PulseClient; uint32_t index_; string name_; @@ -110,10 +110,10 @@ struct Range { T max; }; -class Pulse { +class PulseClient { public: - Pulse(string client_name); - ~Pulse(); + PulseClient(string client_name); + ~PulseClient(); // Populates all known devices and cards. Any currently known // devices and cards are cleared before the new data is stored. -- cgit v1.2.3 From e35ce0069094c135ed9065f2bda945fdf4b6d83e Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 2 Jan 2013 11:33:59 -0500 Subject: ponymix: validate arg count before invoking function A few changes make this fun and easy: - Merge the function array into the string to action lookup and return a Command instead of simply an enum. A Command is the function and the min/max arg count. - Implement an InRange method for the Range class. - Add a Dispatch function to convert the string to Command and validate the arguments. This leaves us in a position where the argc parameter to each method is never used, but maybe some day a command will be added that takes a range of args rather than a fixed number. --- ponymix.cc | 209 ++++++++++++++++++++++++------------------------------------- pulse.cc | 8 +-- pulse.h | 10 ++- 3 files changed, 96 insertions(+), 131 deletions(-) (limited to 'pulse.h') diff --git a/ponymix.cc b/ponymix.cc index a6ce950..824daa6 100644 --- a/ponymix.cc +++ b/ponymix.cc @@ -31,16 +31,16 @@ enum Action { ACTION_INVALID, }; +struct Command { + int (*fn)(PulseClient&, int, char*[]); + Range args; +}; + static enum DeviceType opt_devtype; -static enum Action opt_action; +static const char* opt_action; static const char* opt_device; static const char* opt_card; -static const int kMinVolume = 0; -static const int kMaxVolume = 150; -static const int kMinBalance = -100; -static const int kMaxBalance = 100; - static const char* type_to_string(enum DeviceType t) { switch (t) { case DEVTYPE_SINK: @@ -54,38 +54,7 @@ static const char* type_to_string(enum DeviceType t) { } /* impossibiru! */ - return NULL; -} - -static enum Action string_to_action(const char* str) { - static std::map actionmap = { - { "defaults", ACTION_DEFAULTS }, - { "list", ACTION_LIST }, - { "list-cards", ACTION_LISTCARDS }, - { "list-profiles", ACTION_LISTPROFILES }, - { "get-volume", ACTION_GETVOL }, - { "set-volume", ACTION_SETVOL }, - { "get-balance", ACTION_GETBAL }, - { "set-balance", ACTION_SETBAL }, - { "adj-balance", ACTION_ADJBAL }, - { "increase", ACTION_INCREASE }, - { "decrease", ACTION_DECREASE }, - { "mute", ACTION_MUTE }, - { "unmute", ACTION_UNMUTE }, - { "toggle", ACTION_TOGGLE }, - { "is-muted", ACTION_ISMUTED }, - { "set-default", ACTION_SETDEFAULT }, - { "get-profile", ACTION_GETPROFILE }, - { "set-profile", ACTION_SETPROFILE }, - { "move", ACTION_MOVE }, - { "kill", ACTION_KILL } - }; - - try { - return actionmap.at(str); - } catch(std::out_of_range) { - errx(1, "error: Invalid action specified: %s", str); - } + throw std::out_of_range("device type out of range"); } static enum DeviceType string_to_devtype_or_die(const char* str) { @@ -153,9 +122,7 @@ static int ShowDefaults(PulseClient& ponymix, int, char*[]) { return 0; } -static int List(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: list requires 0 arguments"); - +static int List(PulseClient& ponymix, int, char*[]) { const auto& sinks = ponymix.GetSinks(); for (const auto& s : sinks) Print(s); @@ -171,19 +138,15 @@ static int List(PulseClient& ponymix, int argc, char*[]) { return 0; } -static int ListCards(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: list-cards requires 0 arguments"); - +static int ListCards(PulseClient& ponymix, int, char*[]) { const auto& cards = ponymix.GetCards(); for (const auto& c : cards) Print(c); return 0; } -static int ListProfiles(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: list-profiles requires 0 arguments"); - - // TODO: figure out how to get a list of cards? +static int ListProfiles(PulseClient& ponymix, int, char*[]) { + // TODO: Is there any sense of a "default card" ? auto card = ponymix.GetCard(opt_card); if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); @@ -193,17 +156,13 @@ static int ListProfiles(PulseClient& ponymix, int argc, char*[]) { return 0; } -static int GetVolume(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: get-volume requires 0 arguments"); - +static int GetVolume(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); printf("%d\n", device->Volume()); return 0; } -static int SetVolume(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: set-volume requires exactly 1 argument"); - +static int SetVolume(PulseClient& ponymix, int, char* argv[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); long volume; @@ -220,17 +179,13 @@ static int SetVolume(PulseClient& ponymix, int argc, char* argv[]) { return 0; } -static int GetBalance(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: get-balance requires 0 arguments"); - +static int GetBalance(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); printf("%d\n", device->Balance()); return 0; } -static int SetBalance(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: set-balance requires exactly 1 argument"); - +static int SetBalance(PulseClient& ponymix, int, char* argv[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); long balance; @@ -247,9 +202,7 @@ static int SetBalance(PulseClient& ponymix, int argc, char* argv[]) { return 0; } -static int AdjBalance(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: adj-balance requires exactly 1 argument"); - +static int AdjBalance(PulseClient& ponymix, int, char* argv[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); long balance; @@ -266,9 +219,7 @@ static int AdjBalance(PulseClient& ponymix, int argc, char* argv[]) { return 0; } -static int IncreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: increase requires exactly 1 argument"); - +static int IncreaseVolume(PulseClient& ponymix, int, char* argv[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); long delta; @@ -285,9 +236,7 @@ static int IncreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { return 0; } -static int DecreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: decrease requires exactly 1 argument"); - +static int DecreaseVolume(PulseClient& ponymix, int, char* argv[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); long delta; @@ -304,9 +253,7 @@ static int DecreaseVolume(PulseClient& ponymix, int argc, char* argv[]) { return 0; } -static int Mute(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: mute requires 0 arguments"); - +static int Mute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); if (!ponymix.SetMute(*device, true)) return 1; @@ -316,9 +263,7 @@ static int Mute(PulseClient& ponymix, int argc, char*[]) { return 0; } -static int Unmute(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: unmute requires 0 arguments"); - +static int Unmute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); if (!ponymix.SetMute(*device, false)) return 1; @@ -328,9 +273,7 @@ static int Unmute(PulseClient& ponymix, int argc, char*[]) { return 0; } -static int ToggleMute(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: toggle requires 0 arguments"); - +static int ToggleMute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); if (!ponymix.SetMute(*device, !ponymix.IsMuted(*device))) return 1; @@ -340,24 +283,18 @@ static int ToggleMute(PulseClient& ponymix, int argc, char*[]) { return 0; } -static int IsMuted(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: is-muted requires 0 arguments"); - +static int IsMuted(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); return !ponymix.IsMuted(*device); } -static int SetDefault(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: set-default requires 0 arguments"); - +static int SetDefault(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); return !ponymix.SetDefault(*device); } -static int GetProfile(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: get-profile requires 0 arguments"); - +static int GetProfile(PulseClient& ponymix, int, char*[]) { auto card = ponymix.GetCard(opt_card); if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); @@ -366,18 +303,14 @@ static int GetProfile(PulseClient& ponymix, int argc, char*[]) { return true; } -static int SetProfile(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: set-profile requires 1 argument"); - +static int SetProfile(PulseClient& ponymix, int, char* argv[]) { auto card = ponymix.GetCard(opt_card); if (card == nullptr) errx(1, "error: no match found for card: %s", opt_card); return !ponymix.SetProfile(*card, argv[0]); } -static int Move(PulseClient& ponymix, int argc, char* argv[]) { - if (argc != 1) errx(1, "error: move requires 1 argument"); - +static int Move(PulseClient& ponymix, int, char* argv[]) { // this assignment is a lie. stfu g++ enum DeviceType target_devtype = opt_devtype; switch (opt_devtype) { @@ -400,36 +333,66 @@ static int Move(PulseClient& ponymix, int argc, char* argv[]) { return !ponymix.Move(*source, *target); } -static int Kill(PulseClient& ponymix, int argc, char*[]) { - if (argc != 0) errx(1, "error: set-default requires 0 arguments"); - +static int Kill(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); return !ponymix.Kill(*device); } -static int (*fn[])(PulseClient& ponymix, int argc, char* argv[]) = { - [ACTION_DEFAULTS] = ShowDefaults, - [ACTION_LIST] = List, - [ACTION_LISTCARDS] = ListCards, - [ACTION_LISTPROFILES] = ListProfiles, - [ACTION_GETVOL] = GetVolume, - [ACTION_SETVOL] = SetVolume, - [ACTION_GETBAL] = GetBalance, - [ACTION_SETBAL] = SetBalance, - [ACTION_ADJBAL] = AdjBalance, - [ACTION_INCREASE] = IncreaseVolume, - [ACTION_DECREASE] = DecreaseVolume, - [ACTION_MUTE] = Mute, - [ACTION_UNMUTE] = Unmute, - [ACTION_TOGGLE] = ToggleMute, - [ACTION_ISMUTED] = IsMuted, - [ACTION_SETDEFAULT] = SetDefault, - [ACTION_GETPROFILE] = GetProfile, - [ACTION_SETPROFILE] = SetProfile, - [ACTION_MOVE] = Move, - [ACTION_KILL] = Kill, -}; +static const Command& string_to_command(const char* str) { + static std::map actionmap = { + // command name function arg min arg max + { "defaults", { ShowDefaults, { 0, 0 } } }, + { "list", { List, { 0, 0 } } }, + { "list-cards", { ListCards, { 0, 0 } } }, + { "list-profiles", { ListProfiles, { 0, 0 } } }, + { "get-volume", { GetVolume, { 0, 0 } } }, + { "set-volume", { SetVolume, { 1, 1 } } }, + { "get-balance", { GetBalance, { 0, 0 } } }, + { "set-balance", { SetBalance, { 1, 1 } } }, + { "adj-balance", { AdjBalance, { 1, 1 } } }, + { "increase", { IncreaseVolume, { 1, 1 } } }, + { "decrease", { DecreaseVolume, { 1, 1 } } }, + { "mute", { Mute, { 0, 0 } } }, + { "unmute", { Unmute, { 0, 0 } } }, + { "toggle", { ToggleMute, { 0, 0 } } }, + { "is-muted", { IsMuted, { 0, 0 } } }, + { "set-default", { SetDefault, { 0, 0 } } }, + { "get-profile", { GetProfile, { 0, 0 } } }, + { "set-profile", { SetProfile, { 1, 1 } } }, + { "move", { Move, { 1, 1 } } }, + { "kill", { Kill, { 0, 0 } } } + }; + + try { + return actionmap.at(str); + } catch(std::out_of_range) { + errx(1, "error: Invalid action specified: %s", str); + } +} + +void error_wrong_args(const Command& cmd, const char* cmdname) { + if (cmd.args.min == cmd.args.max) { + errx(1, "error: %s takes exactly %d argument%c", + cmdname, cmd.args.min, cmd.args.min == 1 ? '\0' : 's'); + } else { + errx(1, "error: %s takes %d to %d arguments\n", + cmdname, cmd.args.min, cmd.args.max); + } +} + +static int CommandDispatch(PulseClient& ponymix, int argc, char *argv[]) { + if (argc > 0) { + opt_action = argv[0]; + argv++; + argc--; + } + + const Command& cmd = string_to_command(opt_action); + if (cmd.args.InRange(argc) != 0) error_wrong_args(cmd, opt_action); + + return cmd.fn(ponymix, argc, argv); +} void usage() { printf("usage: %s [options] ...\n", program_invocation_short_name); @@ -537,7 +500,7 @@ int main(int argc, char* argv[]) { ponymix.Populate(); // defaults - opt_action = ACTION_DEFAULTS; + opt_action = "defaults"; opt_devtype = DEVTYPE_SINK; opt_device = ponymix.GetDefaults().sink.c_str(); opt_card = ponymix.GetCards()[0].Name().c_str(); @@ -546,13 +509,7 @@ int main(int argc, char* argv[]) { argc -= optind; argv += optind; - if (argc > 0) { - opt_action = string_to_action(argv[0]); - argc--; - argv++; - } - - return fn[opt_action](ponymix, argc, argv); + return CommandDispatch(ponymix, argc, argv); } // vim: set et ts=2 sw=2: diff --git a/pulse.cc b/pulse.cc index cbbe405..e42db12 100644 --- a/pulse.cc +++ b/pulse.cc @@ -305,12 +305,12 @@ bool PulseClient::SetVolume(Device& device, long volume) { bool PulseClient::IncreaseVolume(Device& device, long increment) { return SetVolume(device, - volume_range_.clamp(device.volume_percent_ + increment)); + volume_range_.Clamp(device.volume_percent_ + increment)); } bool PulseClient::DecreaseVolume(Device& device, long increment) { return SetVolume(device, - volume_range_.clamp(device.volume_percent_ - increment)); + volume_range_.Clamp(device.volume_percent_ - increment)); } bool PulseClient::SetBalance(Device& device, long balance) { @@ -339,12 +339,12 @@ bool PulseClient::SetBalance(Device& device, long balance) { bool PulseClient::IncreaseBalance(Device& device, long increment) { return SetBalance(device, - balance_range_.clamp(device.balance_ + increment)); + balance_range_.Clamp(device.balance_ + increment)); } bool PulseClient::DecreaseBalance(Device& device, long increment) { return SetBalance(device, - balance_range_.clamp(device.balance_ - increment)); + balance_range_.Clamp(device.balance_ - increment)); } int PulseClient::GetVolume(const Device& device) const { diff --git a/pulse.h b/pulse.h index 9a61108..9bb0733 100644 --- a/pulse.h +++ b/pulse.h @@ -102,10 +102,18 @@ template struct Range { Range(T min, T max) : min(min), max(max) {} - T clamp(T value) { + // Clamps a value to the stored range + T Clamp(T value) { return value < min ? min : (value > max ? max : value); } + // Determine if the passed value is within the range. Returns 0 + // on success, else -1 if lower than the minimum, and 1 if higher + // than the maximum. + int InRange(T value) const { + return value < min ? -1 : (value > max ? 1 : 0); + } + T min; T max; }; -- cgit v1.2.3 From 4292241040661f4b9b042b24d2f0773de0eb1366 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 2 Jan 2013 15:27:14 -0500 Subject: Derive the card from the targetted device MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unless explicitly specified, target a card based on the selected device. Note that not all devices will be tied to a card. falconindy » put differently, if i have multiple cards, what determines which card is used by pulse for a given app? tanuk » In theory, the logic can be anything (it depends on what policy-implementing modules are loaded). By default, routing is mostly handled by module-stream-restore, which chooses the sink based on the user's previous routing choices. tanuk » If the user hasn't done any routing choices, the fallback logic is to select the current "default sink". tanuk » I don't recommend trying to guess the routing policy. falconindy » i guess my understanding of pulse internals is lacking falconindy » but that's rather enlightening falconindy » is there any way to figure out the connection between a sink and a card? tanuk » Yes... (One moment, I'll look up things.) falconindy » ah. uint32_t card falconindy » appears to be in pa_sink_info falconindy » so that ties the sink to the index of a card? tanuk » Yep. falconindy » awesome, that's good enough for what i need to do tanuk » Not all sinks are part of a card, though, but those that are will have the card index set. falconindy » also good to know --- ponymix.cc | 16 +++++++++++++--- pulse.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- pulse.h | 21 ++++++++++++++++++++- 3 files changed, 91 insertions(+), 8 deletions(-) (limited to 'pulse.h') diff --git a/ponymix.cc b/ponymix.cc index 824daa6..874728f 100644 --- a/ponymix.cc +++ b/ponymix.cc @@ -146,7 +146,8 @@ static int ListCards(PulseClient& ponymix, int, char*[]) { } static int ListProfiles(PulseClient& ponymix, int, char*[]) { - // TODO: Is there any sense of a "default card" ? + 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); @@ -500,15 +501,24 @@ int main(int argc, char* argv[]) { ponymix.Populate(); // defaults + ServerInfo defaults = ponymix.GetDefaults(); opt_action = "defaults"; opt_devtype = DEVTYPE_SINK; - opt_device = ponymix.GetDefaults().sink.c_str(); - opt_card = ponymix.GetCards()[0].Name().c_str(); + opt_device = defaults.sink.c_str(); if (!parse_options(argc, argv)) return 1; 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); } diff --git a/pulse.cc b/pulse.cc index e42db12..b765d64 100644 --- a/pulse.cc +++ b/pulse.cc @@ -10,6 +10,7 @@ // C++ #include #include +#include // External #include @@ -155,6 +156,13 @@ Card* PulseClient::GetCard(const string& name) { return nullptr; } +Card* PulseClient::GetCard(const Device& device) { + for (Card& card : cards_) { + if (device.card_idx_ == card.index_) return &card; + } + return nullptr; +} + Device* PulseClient::get_device(vector& devices, const uint32_t& index) { for (Device& device : devices) { if (device.index_ == index) return &device; @@ -172,6 +180,48 @@ Device* PulseClient::get_device(vector& devices, const string& name) { return nullptr; } +Device* PulseClient::GetDevice(const uint32_t& index, enum DeviceType type) { + switch (type) { + case DEVTYPE_SINK: + return GetSink(index); + case DEVTYPE_SOURCE: + return GetSource(index); + case DEVTYPE_SINK_INPUT: + return GetSinkInput(index); + case DEVTYPE_SOURCE_OUTPUT: + return GetSourceOutput(index); + } + throw std::runtime_error("Impossible DeviceType encountered in GetDevice"); +} + +Device* PulseClient::GetDevice(const string& name, enum DeviceType type) { + switch (type) { + case DEVTYPE_SINK: + return GetSink(name); + case DEVTYPE_SOURCE: + return GetSource(name); + case DEVTYPE_SINK_INPUT: + return GetSinkInput(name); + case DEVTYPE_SOURCE_OUTPUT: + return GetSourceOutput(name); + } + throw std::runtime_error("Impossible DeviceType encountered in GetDevice"); +} + +const vector& PulseClient::GetDevices(enum DeviceType type) const { + switch (type) { + case DEVTYPE_SINK: + return GetSinks(); + case DEVTYPE_SOURCE: + return GetSources(); + case DEVTYPE_SINK_INPUT: + return GetSinkInputs(); + case DEVTYPE_SOURCE_OUTPUT: + return GetSourceOutputs(); + } + throw std::runtime_error("Impossible DeviceType encountered in GetDevices"); +} + Device* PulseClient::GetSink(const uint32_t& index) { return get_device(sinks_, index); } @@ -495,7 +545,8 @@ Device::Device(const pa_sink_info* info) : index_(info->index), name_(info->name ? info->name : ""), desc_(info->description), - mute_(info->mute) { + mute_(info->mute), + card_idx_(info->card) { update_volume(info->volume); memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; @@ -510,7 +561,8 @@ Device::Device(const pa_source_info* info) : index_(info->index), name_(info->name ? info->name : ""), desc_(info->description), - mute_(info->mute) { + mute_(info->mute), + card_idx_(info->card) { update_volume(info->volume); memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; @@ -524,7 +576,8 @@ Device::Device(const pa_sink_input_info* info) : type_(DEVTYPE_SINK_INPUT), index_(info->index), name_(info->name ? info->name : ""), - mute_(info->mute) { + mute_(info->mute), + card_idx_(-1) { update_volume(info->volume); memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; @@ -543,7 +596,8 @@ Device::Device(const pa_source_output_info* info) : type_(DEVTYPE_SOURCE_OUTPUT), index_(info->index), name_(info->name ? info->name : ""), - mute_(info->mute) { + mute_(info->mute), + card_idx_(-1) { update_volume(info->volume); volume_percent_ = volume_as_percent(&volume_); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; diff --git a/pulse.h b/pulse.h index 9bb0733..2c010b2 100644 --- a/pulse.h +++ b/pulse.h @@ -68,6 +68,7 @@ class Device { pa_channel_map channels_; int mute_; int balance_; + uint32_t card_idx_; Operations ops_; }; @@ -96,6 +97,17 @@ class Card { struct ServerInfo { string sink; string source; + + const string GetDefault(enum DeviceType type) { + switch (type) { + case DEVTYPE_SINK: + return sink; + case DEVTYPE_SOURCE: + return source; + default: + return ""; + } + } }; template @@ -127,6 +139,11 @@ class PulseClient { // devices and cards are cleared before the new data is stored. void Populate(); + // Get a device by index or name and type, or all devices by type. + Device* GetDevice(const uint32_t& index, enum DeviceType type); + Device* GetDevice(const string& name, enum DeviceType type); + const vector& GetDevices(enum DeviceType type) const; + // Get a sink by index or name, or all sinks. Device* GetSink(const uint32_t& index); Device* GetSink(const string& name); @@ -147,9 +164,11 @@ class PulseClient { Device* GetSourceOutput(const string& name); const vector& GetSourceOutputs() const { return source_outputs_; } - // Get a card by index or name, or all cards. + // Get a card by index or name, all cards, or get the card which + // a sink is attached to. Card* GetCard(const uint32_t& index); Card* GetCard(const string& name); + Card* GetCard(const Device& device); const vector& GetCards() const { return cards_; } // Get or set the volume of a device. -- cgit v1.2.3