feat(pulseaudio): add support for source device

Update pulseaudio module to accept a source instead of a sink.

This is quite similar to #2238 but I had the feeling there was too
much code duplication. At first, I did try to rely heavily on
templating to avoid that and keep the number of lines changed to a
minimum, but my C++ foo was not good enough (I can push what I have
currently if someone is interested, but it does not compile).

In this attempt, only a few functions are templated (not the whole
class) and a few helper functions are introduced to implement the
differences.

Instead of using switch/case to implement the differences, helper
functions could be specified as an abstract class and implemented by
different classes. I think this is not worth the effort.

```ini
[module/speaker]
type = internal/pulseaudio
format-volume = <label-volume>
format-muted = <label-muted>
label-volume = speaker: %percentage: 2%%
label-muted = muted speaker: %percentage: 2%%

[module/micro]
type = internal/pulseaudio
source = "@DEFAULT_SOURCE"
format-volume = <label-volume>
format-muted = <label-muted>
label-volume = micro: %percentage: 2%%
label-muted = muted micro: %percentage: 2%%
```

Fix #1096.
This commit is contained in:
Vincent Bernat 2021-09-02 09:43:57 +02:00
parent d817080ee8
commit 7d3151eb42
3 changed files with 153 additions and 64 deletions

View File

@ -27,7 +27,8 @@ class pulseaudio {
using queue = std::queue<evtype>;
public:
explicit pulseaudio(const logger& logger, string&& sink_name, bool m_max_volume);
enum class devicetype { SINK = 0, SOURCE };
explicit pulseaudio(const logger& logger, devicetype device_type, string&& device_name, bool m_max_volume);
~pulseaudio();
pulseaudio(const pulseaudio& o) = delete;
@ -40,23 +41,29 @@ class pulseaudio {
int get_volume();
double get_decibels();
void set_volume(float percentage);
void inc_volume(int delta_perc);
void set_mute(bool mode);
void toggle_mute();
bool is_muted();
private:
void update_volume(pa_operation* o);
static void check_mute_callback(pa_context* context, const pa_sink_info* info, int eol, void* userdata);
static void get_sink_volume_callback(pa_context* context, const pa_sink_info* info, int is_last, void* userdata);
template<typename T>
static void get_volume_callback(pa_context* context, const T* info, int is_last, void* userdata);
static void subscribe_callback(pa_context* context, pa_subscription_event_type_t t, uint32_t idx, void* userdata);
static void simple_callback(pa_context* context, int success, void* userdata);
static void sink_info_callback(pa_context* context, const pa_sink_info* info, int eol, void* userdata);
template<typename T>
static void info_callback(pa_context* context, const T* info, int eol, void* userdata);
static void context_state_callback(pa_context* context, void* userdata);
inline void wait_loop(pa_operation* op, pa_threaded_mainloop* loop);
/* Abstraction for sink/source */
const string get_device_type();
void device_update_info(string device_name);
void device_update_info();
void device_set_volume();
void device_set_mute(bool mode);
const logger& m_log;
/**
@ -70,13 +77,15 @@ class pulseaudio {
bool muted{false};
// default sink name
static constexpr auto DEFAULT_SINK = "@DEFAULT_SINK@";
static constexpr auto DEFAULT_SOURCE = "@DEFAULT_SOURCE@";
pa_context* m_context{nullptr};
pa_threaded_mainloop* m_mainloop{nullptr};
queue m_events;
// specified sink name
// specified sink/source
devicetype m_device_type;
string spec_s_name;
string s_name;
uint32_t m_index{0};

View File

@ -7,8 +7,9 @@ POLYBAR_NS
/**
* Construct pulseaudio object
*/
pulseaudio::pulseaudio(const logger& logger, string&& sink_name, bool max_volume)
: m_log(logger), spec_s_name(sink_name) {
pulseaudio::pulseaudio(const logger& logger, devicetype device_type, string&& device_name, bool max_volume)
: m_log(logger), spec_s_name(device_name) {
m_device_type = device_type;
m_mainloop = pa_threaded_mainloop_new();
if (!m_mainloop) {
throw pulseaudio_error("Could not create pulseaudio threaded mainloop.");
@ -65,30 +66,38 @@ pulseaudio::pulseaudio(const logger& logger, string&& sink_name, bool max_volume
}
pa_operation* op{nullptr};
if (!sink_name.empty()) {
op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this);
wait_loop(op, m_mainloop);
if (!device_name.empty()) {
device_update_info(device_name);
}
if (s_name.empty()) {
// get the sink index
op = pa_context_get_sink_info_by_name(m_context, DEFAULT_SINK, sink_info_callback, this);
wait_loop(op, m_mainloop);
m_log.notice("pulseaudio: using default sink %s", s_name);
// get the index
device_update_info(""s);
m_log.notice("pulseaudio: using default %s %s", get_device_type(), s_name);
} else {
m_log.trace("pulseaudio: using sink %s", s_name);
m_log.trace("pulseaudio: using %s %s", get_device_type(), s_name);
}
m_max_volume = max_volume ? PA_VOLUME_UI_MAX : PA_VOLUME_NORM;
auto event_types = static_cast<pa_subscription_mask_t>(PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER);
pa_subscription_mask_t event_types;
switch (m_device_type) {
case devicetype::SINK:
event_types = static_cast<pa_subscription_mask_t>(PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER);
break;
case devicetype::SOURCE:
event_types = static_cast<pa_subscription_mask_t>(PA_SUBSCRIPTION_MASK_SOURCE | PA_SUBSCRIPTION_MASK_SERVER);
break;
default:
throw pulseaudio_error("unreachable");
}
op = pa_context_subscribe(m_context, event_types, simple_callback, this);
wait_loop(op, m_mainloop);
if (!success) {
throw pulseaudio_error("Failed to subscribe to sink.");
throw pulseaudio_error("Failed to subscribe to device.");
}
pa_context_set_subscribe_callback(m_context, subscribe_callback, this);
update_volume(op);
device_update_info();
pa_threaded_mainloop_unlock(m_mainloop);
}
@ -110,6 +119,87 @@ const string& pulseaudio::get_name() {
return s_name;
}
/**
* Get device type as string
*/
const string pulseaudio::get_device_type() {
switch (m_device_type) {
case devicetype::SINK:
return "sink"s;
case devicetype::SOURCE:
return "source"s;
}
throw pulseaudio_error("unreachable");
}
/**
* Wrapper around pa_context_get_XXX_info_by_name().
*/
void pulseaudio::device_update_info(string device_name) {
pa_operation *op{nullptr};
switch (m_device_type) {
case devicetype::SINK:
op = pa_context_get_sink_info_by_name(m_context,
device_name.empty()?DEFAULT_SINK:device_name.c_str(),
info_callback<pa_sink_info>, this);
break;
case devicetype::SOURCE:
op = pa_context_get_source_info_by_name(m_context,
device_name.empty()?DEFAULT_SOURCE:device_name.c_str(),
info_callback<pa_source_info>, this);
break;
}
wait_loop(op, m_mainloop);
}
/**
* Wrapper around pa_context_get_XXX_info_by_index().
*/
void pulseaudio::device_update_info() {
pa_operation *op{nullptr};
switch (m_device_type) {
case devicetype::SINK:
op = pa_context_get_sink_info_by_index(m_context, m_index, get_volume_callback<pa_sink_info>, this);
break;
case devicetype::SOURCE:
op = pa_context_get_source_info_by_index(m_context, m_index, get_volume_callback<pa_source_info>, this);
break;
}
wait_loop(op, m_mainloop);
}
/**
* Wrapper around pa_context_set_XXX_volume_by_index().
*/
void pulseaudio::device_set_volume() {
pa_operation *op{nullptr};
switch (m_device_type) {
case devicetype::SINK:
op = pa_context_set_sink_volume_by_index(m_context, m_index, &cv, simple_callback, this);
break;
case devicetype::SOURCE:
op = pa_context_set_source_volume_by_index(m_context, m_index, &cv, simple_callback, this);
break;
}
wait_loop(op, m_mainloop);
}
/**
* Wrapper around pa_context_set_XXX_mute_by_index().
*/
void pulseaudio::device_set_mute(bool mode) {
pa_operation *op{nullptr};
switch (m_device_type) {
case devicetype::SINK:
op = pa_context_set_sink_mute_by_index(m_context, m_index, mode, simple_callback, this);
break;
case devicetype::SOURCE:
op = pa_context_set_source_mute_by_index(m_context, m_index, mode, simple_callback, this);
break;
}
wait_loop(op, m_mainloop);
}
/**
* Wait for events
*/
@ -123,37 +213,34 @@ bool pulseaudio::wait() {
int pulseaudio::process_events() {
int ret = m_events.size();
pa_threaded_mainloop_lock(m_mainloop);
pa_operation* o{nullptr};
// clear the queue
while (!m_events.empty()) {
switch (m_events.front()) {
// try to get specified sink
// try to get specified sink/source
case evtype::NEW:
// redundant if already using specified sink
// redundant if already using specified sink/source
if (!spec_s_name.empty()) {
o = pa_context_get_sink_info_by_name(m_context, spec_s_name.c_str(), sink_info_callback, this);
wait_loop(o, m_mainloop);
device_update_info(spec_s_name);
break;
}
// FALLTHRU
case evtype::SERVER:
// don't fallthrough only if always using default sink
// don't fallthrough only if always using default sink/source
if (!spec_s_name.empty()) {
break;
}
// FALLTHRU
// get default sink
// get default sink/source
case evtype::REMOVE:
o = pa_context_get_sink_info_by_name(m_context, DEFAULT_SINK, sink_info_callback, this);
wait_loop(o, m_mainloop);
device_update_info(""s);
if (spec_s_name != s_name) {
m_log.notice("pulseaudio: using default sink %s", s_name);
m_log.notice("pulseaudio: using default %s %s", get_device_type(), s_name);
}
break;
default:
break;
}
update_volume(o);
device_update_info();
m_events.pop();
}
pa_threaded_mainloop_unlock(m_mainloop);
@ -175,20 +262,6 @@ double pulseaudio::get_decibels() {
return pa_sw_volume_to_dB(pa_cvolume_max(&cv));
}
/**
* Set volume to given percentage
*/
void pulseaudio::set_volume(float percentage) {
pa_threaded_mainloop_lock(m_mainloop);
pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(percentage, PA_VOLUME_MUTED, PA_VOLUME_NORM);
pa_cvolume_scale(&cv, vol);
pa_operation* op = pa_context_set_sink_volume_by_index(m_context, m_index, &cv, simple_callback, this);
wait_loop(op, m_mainloop);
if (!success)
throw pulseaudio_error("Failed to set sink volume.");
pa_threaded_mainloop_unlock(m_mainloop);
}
/**
* Increment or decrement volume by given percentage (prevents accumulation of rounding errors from get_volume)
*/
@ -207,10 +280,9 @@ void pulseaudio::inc_volume(int delta_perc) {
}
} else
pa_cvolume_dec(&cv, vol);
pa_operation* op = pa_context_set_sink_volume_by_index(m_context, m_index, &cv, simple_callback, this);
wait_loop(op, m_mainloop);
device_set_volume();
if (!success)
throw pulseaudio_error("Failed to set sink volume.");
throw pulseaudio_error("Failed to set device volume.");
pa_threaded_mainloop_unlock(m_mainloop);
}
@ -219,10 +291,9 @@ void pulseaudio::inc_volume(int delta_perc) {
*/
void pulseaudio::set_mute(bool mode) {
pa_threaded_mainloop_lock(m_mainloop);
pa_operation* op = pa_context_set_sink_mute_by_index(m_context, m_index, mode, simple_callback, this);
wait_loop(op, m_mainloop);
device_set_mute(mode);
if (!success)
throw pulseaudio_error("Failed to mute sink.");
throw pulseaudio_error("Failed to mute device.");
pa_threaded_mainloop_unlock(m_mainloop);
}
@ -240,18 +311,11 @@ bool pulseaudio::is_muted() {
return muted;
}
/**
* Update local volume cache
*/
void pulseaudio::update_volume(pa_operation* o) {
o = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this);
wait_loop(o, m_mainloop);
}
/**
* Callback when getting volume
*/
void pulseaudio::get_sink_volume_callback(pa_context*, const pa_sink_info* info, int, void* userdata) {
template <typename T>
void pulseaudio::get_volume_callback(pa_context*, const T* info, int, void* userdata) {
pulseaudio* This = static_cast<pulseaudio*>(userdata);
if (info) {
This->cv = info->volume;
@ -274,6 +338,7 @@ void pulseaudio::subscribe_callback(pa_context*, pa_subscription_event_type_t t,
}
break;
case PA_SUBSCRIPTION_EVENT_SINK:
case PA_SUBSCRIPTION_EVENT_SOURCE:
switch (t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
case PA_SUBSCRIPTION_EVENT_NEW:
This->m_events.emplace(evtype::NEW);
@ -302,10 +367,11 @@ void pulseaudio::simple_callback(pa_context*, int success, void* userdata) {
}
/**
* Callback when getting sink info & existence
* Callback when getting sink/source info & existence
*/
void pulseaudio::sink_info_callback(pa_context*, const pa_sink_info* info, int eol, void* userdata) {
pulseaudio* This = static_cast<pulseaudio*>(userdata);
template <typename T>
void pulseaudio::info_callback(pa_context*, const T* info, int eol, void* userdata) {
pulseaudio *This = static_cast<pulseaudio*>(userdata);
if (!eol && info) {
This->m_index = info->index;
This->s_name = info->name;

View File

@ -25,11 +25,25 @@ namespace modules {
m_interval = m_conf.get(name(), "interval", m_interval);
auto sink_name = m_conf.get(name(), "sink", ""s);
auto source_name = m_conf.get(name(), "source", ""s);
string device_name;
bool m_max_volume = m_conf.get(name(), "use-ui-max", true);
m_reverse_scroll = m_conf.get(name(), "reverse-scroll", false);
if (!sink_name.empty() && !source_name.empty()) {
throw module_error("Use either source or sink, not both");
}
pulseaudio::devicetype device_type;
if (!source_name.empty()) {
device_type = pulseaudio::devicetype::SOURCE;
device_name = move(source_name);
} else {
device_type = pulseaudio::devicetype::SINK;
device_name = move(sink_name);
}
try {
m_pulseaudio = std::make_unique<pulseaudio>(m_log, move(sink_name), m_max_volume);
m_pulseaudio = std::make_unique<pulseaudio>(m_log, device_type, move(device_name), m_max_volume);
} catch (const pulseaudio_error& err) {
throw module_error(err.what());
}