refactor(volume): Support Master only setups

This commit is contained in:
Michael Carlberg 2016-10-16 02:21:12 +02:00
parent 8b91852284
commit 21d91b0d91
2 changed files with 128 additions and 99 deletions

View File

@ -39,7 +39,8 @@ class alsa_ctl_interface {
snd_ctl_elem_info_set_id(m_info, m_id); snd_ctl_elem_info_set_id(m_info, m_id);
if ((err = snd_ctl_open(&m_ctl, ALSA_SOUNDCARD, SND_CTL_NONBLOCK | SND_CTL_READONLY)) < 0) if ((err = snd_ctl_open(&m_ctl, ALSA_SOUNDCARD, SND_CTL_NONBLOCK | SND_CTL_READONLY)) < 0)
throw_exception<alsa_ctl_interface_error>("Could not open control '"+ string{ALSA_SOUNDCARD} +"'", err); throw_exception<alsa_ctl_interface_error>(
"Could not open control '" + string{ALSA_SOUNDCARD} + "'", err);
if ((err = snd_ctl_elem_info(m_ctl, m_info)) < 0) if ((err = snd_ctl_elem_info(m_ctl, m_info)) < 0)
throw_exception<alsa_ctl_interface_error>("Could not get control datal", err); throw_exception<alsa_ctl_interface_error>("Could not get control datal", err);
@ -185,9 +186,11 @@ class alsa_mixer {
snd_mixer_selem_get_playback_volume_range(m_mixerelement, &vol_min, &vol_max); snd_mixer_selem_get_playback_volume_range(m_mixerelement, &vol_min, &vol_max);
for (int i = 0; i < SND_MIXER_SCHN_LAST; i++) { for (int i = 0; i <= SND_MIXER_SCHN_LAST; i++) {
if (snd_mixer_selem_has_playback_channel(m_mixerelement, (snd_mixer_selem_channel_id_t)i)) { if (snd_mixer_selem_has_playback_channel(
snd_mixer_selem_get_playback_volume(m_mixerelement, (snd_mixer_selem_channel_id_t)i, &vol); m_mixerelement, static_cast<snd_mixer_selem_channel_id_t>(i))) {
snd_mixer_selem_get_playback_volume(
m_mixerelement, static_cast<snd_mixer_selem_channel_id_t>(i), &vol);
vol_total += vol; vol_total += vol;
chan_n++; chan_n++;
} }
@ -216,22 +219,23 @@ class alsa_mixer {
void toggle_mute() { void toggle_mute() {
std::lock_guard<threading_util::spin_lock> guard(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
int state; int state;
snd_mixer_selem_get_playback_switch(m_mixerelement, SND_MIXER_SCHN_FRONT_LEFT, &state); snd_mixer_selem_get_playback_switch(m_mixerelement, SND_MIXER_SCHN_MONO, &state);
snd_mixer_selem_set_playback_switch_all(m_mixerelement, !state); snd_mixer_selem_set_playback_switch_all(m_mixerelement, !state);
} }
bool is_muted() { bool is_muted() {
std::lock_guard<threading_util::spin_lock> guard(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
int state = 0; int state = 0;
for (int i = 0; i < SND_MIXER_SCHN_LAST; i++) { for (int i = 0; i <= SND_MIXER_SCHN_LAST; i++) {
if (snd_mixer_selem_has_playback_channel(m_mixerelement, (snd_mixer_selem_channel_id_t)i)) { if (snd_mixer_selem_has_playback_channel(
snd_mixer_selem_get_playback_switch(m_mixerelement, SND_MIXER_SCHN_FRONT_LEFT, &state); m_mixerelement, static_cast<snd_mixer_selem_channel_id_t>(i))) {
if (state == 0) int state_ = 0;
return true; snd_mixer_selem_get_playback_switch(
m_mixerelement, static_cast<snd_mixer_selem_channel_id_t>(i), &state_);
state = state || state_;
} }
} }
return !state;
return false;
} }
private: private:

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "adapters/alsa.hpp" #include "adapters/alsa.hpp"
#include "components/config.hpp"
#include "config.hpp" #include "config.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp" #include "drawtypes/progressbar.hpp"
@ -10,6 +11,12 @@
LEMONBUDDY_NS LEMONBUDDY_NS
namespace modules { namespace modules {
enum class mixer { NONE = 0, MASTER, SPEAKER, HEADPHONE };
enum class control { NONE = 0, HEADPHONE };
using mixer_t = shared_ptr<alsa_mixer>;
using control_t = shared_ptr<alsa_ctl_interface>;
class volume_module : public event_module<volume_module> { class volume_module : public event_module<volume_module> {
public: public:
using event_module::event_module; using event_module::event_module;
@ -21,12 +28,12 @@ namespace modules {
auto speaker_mixer = m_conf.get<string>(name(), "speaker-mixer", ""); auto speaker_mixer = m_conf.get<string>(name(), "speaker-mixer", "");
auto headphone_mixer = m_conf.get<string>(name(), "headphone-mixer", ""); auto headphone_mixer = m_conf.get<string>(name(), "headphone-mixer", "");
m_headphone_id = m_conf.get<int>(name(), "headphone-id", -1); GET_CONFIG_VALUE(name(), m_headphoneid, "headphone-id");
if (!headphone_mixer.empty() && m_headphone_id == -1) if (!headphone_mixer.empty() && m_headphoneid == -1)
throw module_error( throw module_error(
"volume_module: Missing required property value for \"headphone-id\"..."); "volume_module: Missing required property value for \"headphone-id\"...");
else if (headphone_mixer.empty() && m_headphone_id != -1) else if (headphone_mixer.empty() && m_headphoneid != -1)
throw module_error( throw module_error(
"volume_module: Missing required property value for \"headphone-mixer\"..."); "volume_module: Missing required property value for \"headphone-mixer\"...");
@ -42,38 +49,38 @@ namespace modules {
// }}} // }}}
// Setup mixers {{{ // Setup mixers {{{
auto create_mixer = [](const logger& log, string mixer_name) { auto create_mixer = [this](string mixer_name) {
unique_ptr<alsa_mixer> mixer; mixer_t mixer;
try { try {
mixer = make_unique<alsa_mixer>(mixer_name); mixer.reset(new mixer_t::element_type{mixer_name});
} catch (const alsa_mixer_error& e) { } catch (const alsa_mixer_error& e) {
log.err("volume_module: Failed to open '%s' mixer => %s", mixer_name, e.what()); m_log.err("%s: Failed to open '%s' mixer => %s", name(), mixer_name, e.what());
mixer.reset(); mixer.reset();
} }
return mixer; return mixer;
}; };
m_master_mixer = create_mixer(m_log, master_mixer); m_mixers[mixer::MASTER] = create_mixer(master_mixer);
if (!speaker_mixer.empty()) if (!speaker_mixer.empty())
m_speaker_mixer = create_mixer(m_log, speaker_mixer); m_mixers[mixer::SPEAKER] = create_mixer(speaker_mixer);
if (!headphone_mixer.empty()) if (!headphone_mixer.empty())
m_headphone_mixer = create_mixer(m_log, headphone_mixer); m_mixers[mixer::HEADPHONE] = create_mixer(headphone_mixer);
if (!m_master_mixer && !m_speaker_mixer && !m_headphone_mixer) { if (m_mixers.empty()) {
m_log.err("%s: No configured mixers, stopping module...", name()); m_log.err("%s: No configured mixers, stopping module...", name());
stop(); stop();
return; return;
} }
if (m_headphone_mixer && m_headphone_id > -1) { if (m_mixers[mixer::HEADPHONE] && m_headphoneid > -1) {
try { try {
m_headphone_ctrl = make_unique<alsa_ctl_interface>(m_headphone_id); m_controls[control::HEADPHONE] = make_shared<alsa_ctl_interface>(m_headphoneid);
} catch (const alsa_ctl_interface_error& e) { } catch (const alsa_ctl_interface_error& e) {
m_log.err("%s: Failed to open headphone control interface => %s", name(), e.what()); m_log.err("%s: Failed to open headphone control interface => %s", name(), e.what());
m_headphone_ctrl.reset(); m_controls[control::HEADPHONE].reset();
} }
} }
@ -106,79 +113,96 @@ namespace modules {
} }
void stop() { void stop() {
// Deconstruct all mixers before putting the module in its stopped state {{{
std::lock_guard<threading_util::spin_lock> lck(this->update_lock); std::lock_guard<threading_util::spin_lock> lck(this->update_lock);
m_master_mixer.reset(); m_mixers[mixer::MASTER].reset();
m_speaker_mixer.reset(); m_mixers[mixer::SPEAKER].reset();
m_headphone_mixer.reset(); m_mixers[mixer::HEADPHONE].reset();
m_headphone_ctrl.reset(); m_controls[control::HEADPHONE].reset();
event_module::stop(); event_module::stop();
// }}}
} }
bool has_event() { bool has_event() {
// Poll for mixer and control events {{{
try { try {
bool has_event = false; bool has_event = m_updated;
if (m_master_mixer)
has_event |= m_master_mixer->wait(25); if (m_mixers[mixer::MASTER])
if (m_speaker_mixer) has_event |= m_mixers[mixer::MASTER]->wait(25);
has_event |= m_speaker_mixer->wait(25); if (m_mixers[mixer::SPEAKER])
if (m_headphone_mixer) has_event |= m_mixers[mixer::SPEAKER]->wait(25);
has_event |= m_headphone_mixer->wait(25); if (m_mixers[mixer::HEADPHONE])
if (m_headphone_ctrl) has_event |= m_mixers[mixer::HEADPHONE]->wait(25);
has_event |= m_headphone_ctrl->wait(25); if (m_controls[control::HEADPHONE])
has_event |= m_controls[control::HEADPHONE]->wait(25);
m_updated = false;
return has_event; return has_event;
} catch (const alsa_exception& e) { } catch (const alsa_exception& e) {
m_log.err("%s: %s", name(), e.what()); m_log.err("%s: %s", name(), e.what());
return false; return false;
} }
// }}}
} }
bool update() { bool update() {
// Consume any other pending events // Consume pending events {{{
if (m_master_mixer)
m_master_mixer->process_events();
if (m_speaker_mixer)
m_speaker_mixer->process_events();
if (m_headphone_mixer)
m_headphone_mixer->process_events();
if (m_headphone_ctrl)
m_headphone_ctrl->wait(0);
int volume = 100; if (m_mixers[mixer::MASTER])
bool muted = false; m_mixers[mixer::MASTER]->process_events();
if (m_mixers[mixer::SPEAKER])
m_mixers[mixer::SPEAKER]->process_events();
if (m_mixers[mixer::HEADPHONE])
m_mixers[mixer::HEADPHONE]->process_events();
if (m_controls[control::HEADPHONE])
m_controls[control::HEADPHONE]->wait(0);
if (m_master_mixer) { // }}}
volume *= m_master_mixer->get_volume() / 100.0f; // Get volume, mute and headphone state {{{
muted |= m_master_mixer->is_muted();
}
if (m_headphone_mixer && m_headphone_ctrl && m_headphone_ctrl->test_device_plugged()) { m_volume = 100;
m_headphones = true; m_muted = false;
volume *= m_headphone_mixer->get_volume() / 100.0f;
muted |= m_headphone_mixer->is_muted();
} else if (m_speaker_mixer) {
m_headphones = false; m_headphones = false;
volume *= m_speaker_mixer->get_volume() / 100.0f;
muted |= m_speaker_mixer->is_muted(); if (m_mixers[mixer::MASTER]) {
m_volume *= m_mixers[mixer::MASTER]->get_volume() / 100.0f;
m_muted = m_muted || m_mixers[mixer::MASTER]->is_muted();
} }
m_volume = volume; if (m_controls[control::HEADPHONE] && m_controls[control::HEADPHONE]->test_device_plugged()) {
m_muted = muted; m_headphones = true;
m_volume *= m_mixers[mixer::HEADPHONE]->get_volume() / 100.0f;
m_muted = m_muted || m_mixers[mixer::HEADPHONE]->is_muted();
} else if (m_mixers[mixer::SPEAKER]) {
m_volume *= m_mixers[mixer::SPEAKER]->get_volume() / 100.0f;
m_muted = m_muted || m_mixers[mixer::SPEAKER]->is_muted();
}
// }}}
// Replace label tokens {{{
if (m_label_volume_tokenized) { if (m_label_volume_tokenized) {
m_label_volume_tokenized->m_text = m_label_volume->m_text; m_label_volume_tokenized->m_text = m_label_volume->m_text;
m_label_volume_tokenized->replace_token("%percentage%", to_string(m_volume) + "%"); m_label_volume_tokenized->replace_token("%percentage%", to_string(m_volume) + "%");
} }
if (m_label_muted_tokenized) { if (m_label_muted_tokenized) {
m_label_muted_tokenized->m_text = m_label_muted->m_text; m_label_muted_tokenized->m_text = m_label_muted->m_text;
m_label_muted_tokenized->replace_token("%percentage%", to_string(m_volume) + "%"); m_label_muted_tokenized->replace_token("%percentage%", to_string(m_volume) + "%");
} }
// }}}
return true; return true;
} }
string get_format() { string get_format() {
return m_muted == true ? FORMAT_MUTED : FORMAT_VOLUME; return m_muted ? FORMAT_MUTED : FORMAT_VOLUME;
} }
string get_output() { string get_output() {
@ -186,16 +210,10 @@ namespace modules {
if (!m_muted && m_volume < 100) if (!m_muted && m_volume < 100)
m_builder->cmd(mousebtn::SCROLL_UP, EVENT_VOLUME_UP); m_builder->cmd(mousebtn::SCROLL_UP, EVENT_VOLUME_UP);
else
m_log.trace("%s: Not adding scroll up handler (muted or volume = 100)", name());
if (!m_muted && m_volume > 0) if (!m_muted && m_volume > 0)
m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_VOLUME_DOWN); m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_VOLUME_DOWN);
else
m_log.trace("%s: Not adding scroll down handler (muted or volume = 0)", name());
m_builder->node(module::get_output()); m_builder->node(module::get_output());
return m_builder->flush(); return m_builder->flush();
} }
@ -218,33 +236,40 @@ namespace modules {
bool handle_event(string cmd) { bool handle_event(string cmd) {
if (cmd.compare(0, 3, EVENT_PREFIX) != 0) if (cmd.compare(0, 3, EVENT_PREFIX) != 0)
return false; return false;
if (!m_mixers[mixer::MASTER])
if (!m_master_mixer)
return false; return false;
alsa_mixer* master_mixer = m_master_mixer.get(); std::lock_guard<threading_util::spin_lock> lck(this->update_lock);
alsa_mixer* other_mixer = nullptr; {
vector<mixer_t> mixers{m_mixers[mixer::MASTER]};
if (m_headphone_mixer && m_headphones) if (m_mixers[mixer::HEADPHONE] && m_headphones)
other_mixer = m_headphone_mixer.get(); mixers.emplace_back(m_mixers[mixer::HEADPHONE]);
else if (m_speaker_mixer) else if (m_mixers[mixer::SPEAKER])
other_mixer = m_speaker_mixer.get(); mixers.emplace_back(m_mixers[mixer::SPEAKER]);
try {
if (cmd.compare(0, strlen(EVENT_TOGGLE_MUTE), EVENT_TOGGLE_MUTE) == 0) { if (cmd.compare(0, strlen(EVENT_TOGGLE_MUTE), EVENT_TOGGLE_MUTE) == 0) {
master_mixer->set_mute(m_muted); for (auto&& mixer : mixers) {
if (other_mixer != nullptr) mixer->set_mute(m_muted || mixers[0]->is_muted());
other_mixer->set_mute(m_muted); }
} else if (cmd.compare(0, strlen(EVENT_VOLUME_UP), EVENT_VOLUME_UP) == 0) { } else if (cmd.compare(0, strlen(EVENT_VOLUME_UP), EVENT_VOLUME_UP) == 0) {
master_mixer->set_volume(math_util::cap<float>(master_mixer->get_volume() + 5, 0, 100)); for (auto&& mixer : mixers) {
if (other_mixer != nullptr) mixer->set_volume(math_util::cap<float>(mixer->get_volume() + 5, 0, 100));
other_mixer->set_volume(math_util::cap<float>(other_mixer->get_volume() + 5, 0, 100)); }
} else if (cmd.compare(0, strlen(EVENT_VOLUME_DOWN), EVENT_VOLUME_DOWN) == 0) { } else if (cmd.compare(0, strlen(EVENT_VOLUME_DOWN), EVENT_VOLUME_DOWN) == 0) {
master_mixer->set_volume(math_util::cap<float>(master_mixer->get_volume() - 5, 0, 100)); for (auto&& mixer : mixers) {
if (other_mixer != nullptr) mixer->set_volume(math_util::cap<float>(mixer->get_volume() - 5, 0, 100));
other_mixer->set_volume(math_util::cap<float>(other_mixer->get_volume() - 5, 0, 100)); }
} else { } else {
return false; return false;
} }
} catch (const std::exception& err) {
m_log.err("%s: Failed to handle command (%s)", name(), err.what());
}
}
m_updated = true;
return true; return true;
} }
@ -276,15 +301,15 @@ namespace modules {
label_t m_label_muted; label_t m_label_muted;
label_t m_label_muted_tokenized; label_t m_label_muted_tokenized;
unique_ptr<alsa_mixer> m_master_mixer; map<mixer, mixer_t> m_mixers;
unique_ptr<alsa_mixer> m_speaker_mixer; map<control, control_t> m_controls;
unique_ptr<alsa_mixer> m_headphone_mixer;
unique_ptr<alsa_ctl_interface> m_headphone_ctrl; int m_headphoneid = -1;
int m_headphone_id = -1;
int m_volume = 0; int m_volume = 0;
stateflag m_muted{false}; stateflag m_muted{false};
stateflag m_headphones{false}; stateflag m_headphones{false};
stateflag m_updated{false};
}; };
} }