From e1a19b30932c39b2eb3ea5b53ae95c48151d8f0e Mon Sep 17 00:00:00 2001 From: Arseny Date: Fri, 19 Jan 2018 17:03:06 +0300 Subject: [PATCH] Provide support to query the active port on PulseAudio's default sink (#405) * Provide support to query the active port on PA's default sink This commit exposes 2 objects: - pa_sink_active_port_name (example value: "analog-output-headphones") - pa_sink_active_port_description (example value: "Headphones") that give respectively the name and description strings for the active port on the default sink provided by PulseAudio. * Reflect 9ed28f2a in the documentation --- doc/variables.xml | 18 ++++++++++++++++++ src/core.cc | 8 ++++++++ src/pulseaudio.cc | 12 +++++++++++- src/pulseaudio.h | 6 +++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/doc/variables.xml b/doc/variables.xml index 7c5ebb8c..c69f13f8 100644 --- a/doc/variables.xml +++ b/doc/variables.xml @@ -3072,6 +3072,24 @@ Pulseaudio's default sink description. + + + + + + + Pulseaudio's default sink active port name. + + + + + + + + + Pulseaudio's default sink active port description. + + diff --git a/src/core.cc b/src/core.cc index 3bbeb328..f4d20f86 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1888,6 +1888,14 @@ struct text_object *construct_text_object(char *s, const char *arg, obj->callbacks.print = &print_puau_sink_description; obj->callbacks.free = &free_pulseaudio; init_pulseaudio(obj); + END OBJ(pa_sink_active_port_name, 0) + obj->callbacks.print = &print_puau_sink_active_port_name; + obj->callbacks.free = &free_pulseaudio; + init_pulseaudio(obj); + END OBJ(pa_sink_active_port_description, 0) + obj->callbacks.print = &print_puau_sink_active_port_description; + obj->callbacks.free = &free_pulseaudio; + init_pulseaudio(obj); END OBJ(pa_sink_volume, 0) obj->callbacks.percentage = &puau_vol; obj->callbacks.free = &free_pulseaudio; diff --git a/src/pulseaudio.cc b/src/pulseaudio.cc index 0ec5cd90..93226c1b 100644 --- a/src/pulseaudio.cc +++ b/src/pulseaudio.cc @@ -43,7 +43,7 @@ struct pulseaudio_default_results get_result_copy(); const struct pulseaudio_default_results pulseaudio_result0 = - { std::string(), std::string(), 0, 0, 0, 0, std::string(), std::string(), 0 }; + { std::string(), std::string(), std::string(), std::string(), 0, 0, 0, 0, std::string(), std::string(), 0 }; pulseaudio_c *pulseaudio = NULL; void pa_sink_info_callback(pa_context *c, const pa_sink_info *i, int eol, void *data) { @@ -53,6 +53,8 @@ void pa_sink_info_callback(pa_context *c, const pa_sink_info *i, int eol, void * pdr->sink_mute = i->mute; pdr->sink_card = i->card; pdr->sink_index = i->index; + pdr->sink_active_port_name.assign(i->active_port->name); + pdr->sink_active_port_description.assign(i->active_port->description); pdr->sink_volume = round_to_int(100.0f * (float)pa_cvolume_avg(&(i->volume)) / (float)PA_VOLUME_NORM); pa_threaded_mainloop_signal(pulseaudio->mainloop, 0); } @@ -284,6 +286,14 @@ void print_puau_sink_description(struct text_object *obj, char *p, int p_max_siz snprintf(p, p_max_size, "%s", get_pulseaudio(obj).sink_description.c_str()); } +void print_puau_sink_active_port_name(struct text_object *obj, char *p, int p_max_size) { + snprintf(p, p_max_size, "%s", get_pulseaudio(obj).sink_active_port_name.c_str()); +} + +void print_puau_sink_active_port_description(struct text_object *obj, char *p, int p_max_size) { + snprintf(p, p_max_size, "%s", get_pulseaudio(obj).sink_active_port_description.c_str()); +} + void print_puau_card_active_profile(struct text_object *obj, char *p, int p_max_size) { snprintf(p, p_max_size, "%s", get_pulseaudio(obj).card_active_profile_description.c_str()); } diff --git a/src/pulseaudio.h b/src/pulseaudio.h index 59faf872..18abeb51 100644 --- a/src/pulseaudio.h +++ b/src/pulseaudio.h @@ -38,6 +38,8 @@ void init_pulseaudio(struct text_object *obj); void free_pulseaudio(struct text_object *obj); uint8_t puau_vol(struct text_object *); // preserve pa_* for libpulse void print_puau_sink_description(struct text_object *obj, char *p, int p_max_size); +void print_puau_sink_active_port_name(struct text_object *obj, char *p, int p_max_size); +void print_puau_sink_active_port_description(struct text_object *obj, char *p, int p_max_size); void print_puau_card_name(struct text_object *obj, char *p, int p_max_size); void print_puau_card_active_profile(struct text_object *obj, char *p, int p_max_size); double puau_volumebarval(struct text_object *obj); @@ -47,6 +49,8 @@ struct pulseaudio_default_results { // default sink std::string sink_name; std::string sink_description; + std::string sink_active_port_name; + std::string sink_active_port_description; uint32_t sink_card; int sink_mute; uint32_t sink_index; @@ -77,7 +81,7 @@ class pulseaudio_c { context(NULL), cstate(PULSE_CONTEXT_INITIALIZING), ninits(0), - result({ std::string(), std::string(), 0, 0, 0, 0, std::string(), std::string(), 0 }){}; + result({ std::string(), std::string(), std::string(), std::string(), 0, 0, 0, 0, std::string(), std::string(), 0 }){}; }; #endif /* _PULSEAUDIO_H */