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.cc | 561 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 561 insertions(+) create mode 100644 pulse.cc (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc new file mode 100644 index 0000000..c09b61b --- /dev/null +++ b/pulse.cc @@ -0,0 +1,561 @@ +// Self +#include "pulse.h" + +// C +#include +#include +#include +#include + +// C++ +#include +#include + +// External +#include + +namespace { +static void connect_state_cb(pa_context* context, void* raw) { + enum pa_context_state *state = (enum pa_context_state*)raw; + *state = pa_context_get_state(context); +} + +static void success_cb(pa_context* context, int success, void* raw) { + int *r = (int*)raw; + *r = success; + if (!success) { + fprintf(stderr, + "operation failed: %s\n", + pa_strerror(pa_context_errno(context))); + } +} + +static void card_info_cb(pa_context* context, + const pa_card_info* info, + int eol, + void* raw) { + if (eol < 0) { + fprintf(stderr, "%s error in %s: \n", __func__, + pa_strerror(pa_context_errno(context))); + return; + } + + if (!eol) { + vector* cards = (vector*)raw; + cards->push_back(info); + } +} + +template +static void device_info_cb(pa_context* context, + const T* info, + int eol, + void* raw) { + if (eol < 0) { + fprintf(stderr, "%s error in %s: \n", __func__, + pa_strerror(pa_context_errno(context))); + return; + } + + if (!eol) { + vector* devices_ = (vector*)raw; + devices_->push_back(info); + } +} + +static void server_info_cb(pa_context* context __attribute__((unused)), + const pa_server_info* i, + void* raw) { + ServerInfo* defaults = (ServerInfo*)raw; + defaults->sink = i->default_sink_name; + defaults->source = i->default_source_name; +} + +static pa_cvolume* value_to_cvol(long value, pa_cvolume *cvol) { + return pa_cvolume_set(cvol, cvol->channels, + fmax((double)(value + .5) * PA_VOLUME_NORM / 100, 0)); +} + +static int volume_as_percent(const pa_cvolume* cvol) { + return pa_cvolume_avg(cvol) * 100.0 / PA_VOLUME_NORM; +} + +static int xstrtol(const char *str, long *out) { + char *end = NULL; + + if (str == NULL || *str == '\0') return -1; + errno = 0; + + *out = strtol(str, &end, 10); + if (errno || str == end || (end && *end)) return -1; + + return 0; +} + + +} // anonymous namespace + +Pulse::Pulse(string client_name) : + client_name_(client_name), + volume_range_(0, 150), + balance_range_(-100, 100) { + enum pa_context_state state = PA_CONTEXT_CONNECTING; + + pa_proplist* proplist = pa_proplist_new(); + pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, client_name.c_str()); + pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, "com.falconindy.ponymix"); + pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, "0.1"); + pa_proplist_sets(proplist, PA_PROP_APPLICATION_ICON_NAME, "audio-card"); + + mainloop_ = pa_mainloop_new(); + context_ = pa_context_new_with_proplist(pa_mainloop_get_api(mainloop_), + nullptr, proplist); + + pa_proplist_free(proplist); + + pa_context_set_state_callback(context_, connect_state_cb, &state); + pa_context_connect(context_, nullptr, PA_CONTEXT_NOFAIL, nullptr); + while (state != PA_CONTEXT_READY && state != PA_CONTEXT_FAILED) { + pa_mainloop_iterate(mainloop_, 1, nullptr); + } + + if (state != PA_CONTEXT_READY) { + fprintf(stderr, "failed to connect to pulse daemon: %s\n", + pa_strerror(pa_context_errno(context_))); + exit(EXIT_FAILURE); + } +} + +// +// Pulse Client +// +Pulse::~Pulse() { + pa_context_unref(context_); + pa_mainloop_free(mainloop_); +} + +void Pulse::Populate() { + populate_server_info(); + populate_sinks(); + populate_sources(); + populate_cards(); +} + +Card* Pulse::GetCard(const uint32_t& index) { + for (Card& card : cards_) { + if (card.index_ == index) return &card; + } + return nullptr; +} + +Card* Pulse::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) { + for (Device& device : devices) { + if (device.index_ == index) return &device; + } + return nullptr; +} + +Device* Pulse::get_device(vector& devices, const string& name) { + long val; + if (xstrtol(name.c_str(), &val) == 0) return get_device(devices, val); + + for (Device& device : devices) { + if (device.name_ == name) return &device; + } + return nullptr; +} + +Device* Pulse::GetSink(const uint32_t& index) { + return get_device(sinks_, index); +} + +Device* Pulse::GetSink(const string& name) { + return get_device(sinks_, name); +} + +Device* Pulse::GetSource(const uint32_t& index) { + return get_device(sources_, index); +} + +Device* Pulse::GetSource(const string& name) { + return get_device(sources_, name); +} + +Device* Pulse::GetSinkInput(const uint32_t& index) { + return get_device(sink_inputs_, index); +} + +Device* Pulse::GetSinkInput(const string& name) { + return get_device(sink_inputs_, name); +} + +Device* Pulse::GetSourceOutput(const uint32_t& index) { + return get_device(source_outputs_, index); +} + +Device* Pulse::GetSourceOutput(const string& name) { + return get_device(source_outputs_, name); +} + +void Pulse::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() { + cards_.clear(); + pa_operation* op = pa_context_get_card_info_list(context_, + card_info_cb, + (void*)&cards_); + mainloop_iterate(op); + pa_operation_unref(op); +} + +void Pulse::populate_server_info() { + pa_operation* op = pa_context_get_server_info(context_, + server_info_cb, + &defaults_); + mainloop_iterate(op); + pa_operation_unref(op); +} + +void Pulse::populate_sinks() { + sinks_.clear(); + pa_operation* op = pa_context_get_sink_info_list(context_, + device_info_cb, + (void*)&sinks_); + mainloop_iterate(op); + pa_operation_unref(op); + + sink_inputs_.clear(); + op = pa_context_get_sink_input_info_list(context_, + device_info_cb, + (void*)&sink_inputs_); + mainloop_iterate(op); + pa_operation_unref(op); +} + +void Pulse::populate_sources() { + sources_.clear(); + pa_operation* op = pa_context_get_source_info_list(context_, + device_info_cb, + (void*)&sources_); + mainloop_iterate(op); + pa_operation_unref(op); + + source_outputs_.clear(); + op = pa_context_get_source_output_info_list(context_, + device_info_cb, + (void*)&source_outputs_); + mainloop_iterate(op); + pa_operation_unref(op); +} + +bool Pulse::SetMute(Device& device, bool mute) { + int success; + + if (device.ops_.Mute == nullptr) { + warnx("device does not support muting."); + return false; + } + + pa_operation* op = device.ops_.Mute(context_, + device.index_, + mute, + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) device.mute_ = mute; + + return success; +} + +bool Pulse::SetVolume(Device& device, long volume) { + int success; + + if (device.ops_.SetVolume == nullptr) { + warnx("device does not support setting balance."); + return false; + } + + const pa_cvolume *cvol = value_to_cvol(volume, &device.volume_); + pa_operation* op = device.ops_.SetVolume(context_, + device.index_, + cvol, + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) device.update_volume(*cvol); + + return success; +} + +bool Pulse::IncreaseVolume(Device& device, long increment) { + return SetVolume(device, + volume_range_.clamp(device.volume_percent_ + increment)); +} + +bool Pulse::DecreaseVolume(Device& device, long increment) { + return SetVolume(device, + volume_range_.clamp(device.volume_percent_ - increment)); +} + +bool Pulse::SetBalance(Device& device, long balance) { + int success; + + if (device.ops_.SetVolume == nullptr) { + warnx("device does not support setting balance."); + return false; + } + + pa_cvolume *cvol = pa_cvolume_set_balance(&device.volume_, + &device.channels_, + balance / 100.0); + pa_operation* op = device.ops_.SetVolume(context_, + device.index_, + cvol, + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) device.update_volume(*cvol); + + return success; +} + +bool Pulse::IncreaseBalance(Device& device, long increment) { + return SetBalance(device, + balance_range_.clamp(device.balance_ + increment)); +} + +bool Pulse::DecreaseBalance(Device& device, long increment) { + return SetBalance(device, + balance_range_.clamp(device.balance_ - increment)); +} + +int Pulse::GetVolume(const Device& device) const { + return device.Volume(); +} + +int Pulse::GetBalance(const Device& device) const { + return device.Balance(); +} + +bool Pulse::SetProfile(Card& card, const string& profile) { + int success; + pa_operation* op = + pa_context_set_card_profile_by_index(context_, + card.index_, + profile.c_str(), + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) { + // Update the profile + for (const Profile p : card.profiles_) { + if (p.name == profile) { + card.active_profile_ = p; + break; + } + } + } + + return success; +} + +bool Pulse::Move(Device& source, Device& dest) { + int success; + + if (source.ops_.Move == nullptr) { + warnx("source device does not support moving."); + return false; + } + + pa_operation* op = source.ops_.Move(context_, + source.index_, + dest.index_, + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + return success; +} + +bool Pulse::Kill(Device& device) { + int success; + + if (device.ops_.Kill == nullptr) { + warnx("source device does not support being killed."); + return false; + } + + pa_operation* op = device.ops_.Kill(context_, + device.index_, + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) remove_device(device); + + return success; +} + +bool Pulse::SetDefault(Device& device) { + int success; + + if (device.ops_.SetDefault == nullptr) { + warnx("device does not support defaults"); + return false; + } + + pa_operation* op = device.ops_.SetDefault(context_, + device.name_.c_str(), + success_cb, + &success); + mainloop_iterate(op); + pa_operation_unref(op); + + if (success) { + switch (device.type_) { + case DEVTYPE_SINK: + defaults_.sink = device.name_; + break; + case DEVTYPE_SOURCE: + defaults_.source = device.name_; + break; + default: + errx(1, "impossible to set a default for device type %d", + device.type_); + } + } + + return success; +} + +void Pulse::remove_device(Device& device) { + vector* devlist; + + switch (device.type_) { + case DEVTYPE_SINK: + devlist = &sinks_; + case DEVTYPE_SINK_INPUT: + devlist = &sink_inputs_; + break; + case DEVTYPE_SOURCE: + devlist = &sources_; + break; + case DEVTYPE_SOURCE_OUTPUT: + devlist = &source_outputs_; + break; + } + devlist->erase( + std::remove_if( + devlist->begin(), devlist->end(), + [=](Device& d) { return d.index_ == device.index_; }), + devlist->end()); +} + +// +// Cards +// +Card::Card(const pa_card_info* info) : + index_(info->index), + name_(info->name), + owner_module_(info->owner_module), + driver_(info->driver), + active_profile_(*info->active_profile) { + for (int i = 0; info->profiles[i].name != nullptr; i++) { + profiles_.push_back(info->profiles[i]); + } +} + +// +// Devices +// +Device::Device(const pa_sink_info* info) : + type_(DEVTYPE_SINK), + index_(info->index), + name_(info->name), + desc_(info->description), + mute_(info->mute) { + update_volume(info->volume); + memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); + balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + + ops_.SetVolume = pa_context_set_sink_volume_by_index; + ops_.Mute = pa_context_set_sink_mute_by_index; + ops_.SetDefault = pa_context_set_default_sink; +} + +Device::Device(const pa_source_info* info) : + type_(DEVTYPE_SOURCE), + index_(info->index), + name_(info->name), + desc_(info->description), + mute_(info->mute) { + update_volume(info->volume); + memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); + balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + + ops_.SetVolume = pa_context_set_source_volume_by_index; + ops_.Mute = pa_context_set_source_mute_by_index; + ops_.SetDefault = pa_context_set_default_source; +} + +Device::Device(const pa_sink_input_info* info) : + type_(DEVTYPE_SINK_INPUT), + index_(info->index), + name_(info->name), + desc_(pa_proplist_gets(info->proplist, PA_PROP_APPLICATION_NAME)), + mute_(info->mute) { + update_volume(info->volume); + memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); + balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + + ops_.SetVolume = pa_context_set_sink_input_volume; + ops_.Mute = pa_context_set_sink_input_mute; + ops_.Kill = pa_context_kill_sink_input; + ops_.Move = pa_context_move_sink_input_by_index; +} + +Device::Device(const pa_source_output_info* info) : + type_(DEVTYPE_SOURCE_OUTPUT), + index_(info->index), + name_(info->name), + desc_(pa_proplist_gets(info->proplist, PA_PROP_APPLICATION_NAME)), + mute_(info->mute) { + update_volume(info->volume); + volume_percent_ = volume_as_percent(&volume_); + balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + + ops_.SetVolume = pa_context_set_source_output_volume; + ops_.Mute = pa_context_set_source_output_mute; + ops_.Kill = pa_context_kill_source_output; + ops_.Move = pa_context_move_source_output_by_index; +} + +void Device::update_volume(const pa_cvolume& newvol) { + memcpy(&volume_, &newvol, sizeof(pa_cvolume)); + volume_percent_ = volume_as_percent(&volume_); + balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; +} + +// vim: set et ts=2 sw=2: -- cgit v1.2.3 From 544047c06ffc7be6fa7f22f895eaab691038ee22 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Tue, 1 Jan 2013 21:07:29 -0500 Subject: fix null constructions --- pulse.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index c09b61b..1ac1867 100644 --- a/pulse.cc +++ b/pulse.cc @@ -493,7 +493,7 @@ Card::Card(const pa_card_info* info) : Device::Device(const pa_sink_info* info) : type_(DEVTYPE_SINK), index_(info->index), - name_(info->name), + name_(!info->name.empty() ? info->name : ""), desc_(info->description), mute_(info->mute) { update_volume(info->volume); @@ -508,7 +508,7 @@ Device::Device(const pa_sink_info* info) : Device::Device(const pa_source_info* info) : type_(DEVTYPE_SOURCE), index_(info->index), - name_(info->name), + name_(!info->name.empty() ? info->name : ""), desc_(info->description), mute_(info->mute) { update_volume(info->volume); @@ -523,13 +523,16 @@ Device::Device(const pa_source_info* info) : Device::Device(const pa_sink_input_info* info) : type_(DEVTYPE_SINK_INPUT), index_(info->index), - name_(info->name), - desc_(pa_proplist_gets(info->proplist, PA_PROP_APPLICATION_NAME)), + name_(!info->name.empty() ? info->name : ""), mute_(info->mute) { update_volume(info->volume); memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + const char *desc = pa_proplist_gets(info->proplist, + PA_PROP_APPLICATION_NAME); + if (desc) desc_ = desc; + ops_.SetVolume = pa_context_set_sink_input_volume; ops_.Mute = pa_context_set_sink_input_mute; ops_.Kill = pa_context_kill_sink_input; @@ -539,13 +542,16 @@ Device::Device(const pa_sink_input_info* info) : Device::Device(const pa_source_output_info* info) : type_(DEVTYPE_SOURCE_OUTPUT), index_(info->index), - name_(info->name), - desc_(pa_proplist_gets(info->proplist, PA_PROP_APPLICATION_NAME)), + name_(!info->name.empty() ? info->name : ""), mute_(info->mute) { update_volume(info->volume); volume_percent_ = volume_as_percent(&volume_); balance_ = pa_cvolume_get_balance(&volume_, &channels_) * 100.0; + const char *desc = pa_proplist_gets(info->proplist, + PA_PROP_APPLICATION_NAME); + if (desc) desc_ = desc; + ops_.SetVolume = pa_context_set_source_output_volume; ops_.Mute = pa_context_set_source_output_mute; ops_.Kill = pa_context_kill_source_output; -- cgit v1.2.3 From 07347623642f6f841580fac139dba2b3b6d97b35 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Tue, 1 Jan 2013 21:12:37 -0500 Subject: oops --- pulse.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index 1ac1867..77a1568 100644 --- a/pulse.cc +++ b/pulse.cc @@ -493,7 +493,7 @@ Card::Card(const pa_card_info* info) : Device::Device(const pa_sink_info* info) : type_(DEVTYPE_SINK), index_(info->index), - name_(!info->name.empty() ? info->name : ""), + name_(info->name ? info->name : ""), desc_(info->description), mute_(info->mute) { update_volume(info->volume); @@ -508,7 +508,7 @@ Device::Device(const pa_sink_info* info) : Device::Device(const pa_source_info* info) : type_(DEVTYPE_SOURCE), index_(info->index), - name_(!info->name.empty() ? info->name : ""), + name_(info->name ? info->name : ""), desc_(info->description), mute_(info->mute) { update_volume(info->volume); @@ -523,7 +523,7 @@ Device::Device(const pa_source_info* info) : Device::Device(const pa_sink_input_info* info) : type_(DEVTYPE_SINK_INPUT), index_(info->index), - name_(!info->name.empty() ? info->name : ""), + name_(info->name ? info->name : ""), mute_(info->mute) { update_volume(info->volume); memcpy(&channels_, &info->channel_map, sizeof(pa_channel_map)); @@ -542,7 +542,7 @@ Device::Device(const pa_sink_input_info* info) : Device::Device(const pa_source_output_info* info) : type_(DEVTYPE_SOURCE_OUTPUT), index_(info->index), - name_(!info->name.empty() ? info->name : ""), + name_(info->name ? info->name : ""), mute_(info->mute) { update_volume(info->volume); volume_percent_ = volume_as_percent(&volume_); -- 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.cc') 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.cc') 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.cc') 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 From f44dd5995a8da0e9f9e1d6372d9fc879b944b768 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 2 Jan 2013 15:33:28 -0500 Subject: always defined behaviors for devices --- pulse.cc | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index b765d64..7fc1bd1 100644 --- a/pulse.cc +++ b/pulse.cc @@ -553,6 +553,8 @@ Device::Device(const pa_sink_info* info) : ops_.SetVolume = pa_context_set_sink_volume_by_index; ops_.Mute = pa_context_set_sink_mute_by_index; + ops_.Kill = nullptr; + ops_.Move = nullptr; ops_.SetDefault = pa_context_set_default_sink; } @@ -569,6 +571,8 @@ Device::Device(const pa_source_info* info) : ops_.SetVolume = pa_context_set_source_volume_by_index; ops_.Mute = pa_context_set_source_mute_by_index; + ops_.Kill = nullptr; + ops_.Move = nullptr; ops_.SetDefault = pa_context_set_default_source; } @@ -590,6 +594,7 @@ Device::Device(const pa_sink_input_info* info) : ops_.Mute = pa_context_set_sink_input_mute; ops_.Kill = pa_context_kill_sink_input; ops_.Move = pa_context_move_sink_input_by_index; + ops_.SetDefault = nullptr; } Device::Device(const pa_source_output_info* info) : @@ -610,6 +615,7 @@ Device::Device(const pa_source_output_info* info) : ops_.Mute = pa_context_set_source_output_mute; ops_.Kill = pa_context_kill_source_output; ops_.Move = pa_context_move_source_output_by_index; + ops_.SetDefault = nullptr; } void Device::update_volume(const pa_cvolume& newvol) { -- cgit v1.2.3 From acbd81fc241a400325a4e47a3f09d2f4f4d9f0d2 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Wed, 2 Jan 2013 19:11:54 -0500 Subject: whitespace police --- pulse.cc | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index 7fc1bd1..edd0449 100644 --- a/pulse.cc +++ b/pulse.cc @@ -182,42 +182,42 @@ Device* PulseClient::get_device(vector& devices, const string& name) { 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); + 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); + 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(); + 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"); } -- cgit v1.2.3 From 6d04ac9ad89293ecc08013d6df97b880ec1e3a69 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 3 Jan 2013 10:58:32 -0500 Subject: allow card lookup by index --- pulse.cc | 3 +++ 1 file changed, 3 insertions(+) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index edd0449..c423c72 100644 --- a/pulse.cc +++ b/pulse.cc @@ -150,6 +150,9 @@ Card* PulseClient::GetCard(const uint32_t& index) { } Card* PulseClient::GetCard(const string& name) { + long val; + if (xstrtol(name.c_str(), &val) == 0) return GetCard(val); + for (Card& card : cards_) { if (card.name_ == name) return &card; } -- cgit v1.2.3 From f374fb2792f5a9fb0aefe4dd1387b2bdace418b3 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 3 Jan 2013 14:36:46 -0500 Subject: properly clamp values in Set{Balance,Volume} --- pulse.cc | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'pulse.cc') diff --git a/pulse.cc b/pulse.cc index c423c72..0488717 100644 --- a/pulse.cc +++ b/pulse.cc @@ -342,6 +342,7 @@ bool PulseClient::SetVolume(Device& device, long volume) { return false; } + volume = volume_range_.Clamp(volume); const pa_cvolume *cvol = value_to_cvol(volume, &device.volume_); pa_operation* op = device.ops_.SetVolume(context_, device.index_, @@ -357,13 +358,11 @@ bool PulseClient::SetVolume(Device& device, long volume) { } bool PulseClient::IncreaseVolume(Device& device, long increment) { - return SetVolume(device, - volume_range_.Clamp(device.volume_percent_ + increment)); + return SetVolume(device, device.volume_percent_ + increment); } bool PulseClient::DecreaseVolume(Device& device, long increment) { - return SetVolume(device, - volume_range_.Clamp(device.volume_percent_ - increment)); + return SetVolume(device, device.volume_percent_ - increment); } bool PulseClient::SetBalance(Device& device, long balance) { @@ -374,6 +373,7 @@ bool PulseClient::SetBalance(Device& device, long balance) { return false; } + balance = balance_range_.Clamp(balance); pa_cvolume *cvol = pa_cvolume_set_balance(&device.volume_, &device.channels_, balance / 100.0); @@ -391,13 +391,11 @@ bool PulseClient::SetBalance(Device& device, long balance) { } bool PulseClient::IncreaseBalance(Device& device, long increment) { - return SetBalance(device, - balance_range_.Clamp(device.balance_ + increment)); + return SetBalance(device, device.balance_ + increment); } bool PulseClient::DecreaseBalance(Device& device, long increment) { - return SetBalance(device, - balance_range_.Clamp(device.balance_ - increment)); + return SetBalance(device, device.balance_ - increment); } int PulseClient::GetVolume(const Device& device) const { -- cgit v1.2.3