polybar-dwm/src/modules/alsa.cpp

278 lines
9.8 KiB
C++
Raw Normal View History

#include "modules/alsa.hpp"
#include "adapters/alsa/control.hpp"
#include "adapters/alsa/generic.hpp"
#include "adapters/alsa/mixer.hpp"
#include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp"
2016-11-02 19:22:45 +00:00
#include "utils/math.hpp"
2016-11-20 22:04:31 +00:00
#include "modules/meta/base.inl"
#include "settings.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
using namespace alsa;
2016-11-02 19:22:45 +00:00
namespace modules {
template class module<alsa_module>;
2016-11-02 19:22:45 +00:00
alsa_module::alsa_module(const bar_settings& bar, string name_) : event_module<alsa_module>(bar, move(name_)) {
2016-11-20 22:04:31 +00:00
// Load configuration values
m_mapped = m_conf.get(name(), "mapped", m_mapped);
2016-11-02 19:22:45 +00:00
auto master_mixer_name = m_conf.get(name(), "master-mixer", "Master"s);
auto speaker_mixer_name = m_conf.get(name(), "speaker-mixer", ""s);
auto headphone_mixer_name = m_conf.get(name(), "headphone-mixer", ""s);
2016-11-02 19:22:45 +00:00
// m_soundcard_name: Master Soundcard Name
// s_soundcard_name: Speaker Soundcard Name
// h_soundcard_name: Headphone Soundcard Name
auto m_soundcard_name = m_conf.get(name(), "master-soundcard", "default"s);
auto s_soundcard_name = m_conf.get(name(), "speaker-soundcard", "default"s);
auto h_soundcard_name = m_conf.get(name(), "headphone-soundcard", "default"s);
2016-11-25 12:55:15 +00:00
if (!headphone_mixer_name.empty()) {
m_headphoneid = m_conf.get<decltype(m_headphoneid)>(name(), "headphone-id");
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (string_util::compare(speaker_mixer_name, "master")) {
2016-11-02 19:22:45 +00:00
throw module_error("Master mixer is already defined");
2016-11-25 12:55:15 +00:00
}
if (string_util::compare(headphone_mixer_name, "master")) {
2016-11-02 19:22:45 +00:00
throw module_error("Master mixer is already defined");
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-20 22:04:31 +00:00
// Setup mixers
2016-11-02 19:22:45 +00:00
try {
2016-11-25 12:55:15 +00:00
if (!master_mixer_name.empty()) {
m_mixer[mixer::MASTER].reset(new mixer_t::element_type{move(master_mixer_name), move(m_soundcard_name)});
2016-11-25 12:55:15 +00:00
}
if (!speaker_mixer_name.empty()) {
m_mixer[mixer::SPEAKER].reset(new mixer_t::element_type{move(speaker_mixer_name), move(s_soundcard_name)});
2016-11-25 12:55:15 +00:00
}
if (!headphone_mixer_name.empty()) {
m_mixer[mixer::HEADPHONE].reset(new mixer_t::element_type{move(headphone_mixer_name), move(h_soundcard_name)});
2016-11-25 12:55:15 +00:00
}
if (m_mixer[mixer::HEADPHONE]) {
m_ctrl[control::HEADPHONE].reset(new control_t::element_type{m_headphoneid});
}
if (m_mixer.empty()) {
2016-11-02 19:22:45 +00:00
throw module_error("No configured mixers");
2016-11-25 12:55:15 +00:00
}
} catch (const mixer_error& err) {
2016-11-02 19:22:45 +00:00
throw module_error(err.what());
} catch (const control_error& err) {
2016-11-02 19:22:45 +00:00
throw module_error(err.what());
}
2016-11-20 22:04:31 +00:00
// Add formats and elements
2016-11-25 07:42:31 +00:00
m_formatter->add(FORMAT_VOLUME, TAG_LABEL_VOLUME, {TAG_RAMP_VOLUME, TAG_LABEL_VOLUME, TAG_BAR_VOLUME});
m_formatter->add(FORMAT_MUTED, TAG_LABEL_MUTED, {TAG_RAMP_VOLUME, TAG_LABEL_MUTED, TAG_BAR_VOLUME});
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (m_formatter->has(TAG_BAR_VOLUME)) {
2016-11-02 19:22:45 +00:00
m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME);
2016-11-25 12:55:15 +00:00
}
if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) {
m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%%");
2016-11-25 12:55:15 +00:00
}
if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) {
m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%%");
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
if (m_formatter->has(TAG_RAMP_VOLUME)) {
m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME);
m_ramp_headphones = load_ramp(m_conf, name(), TAG_RAMP_HEADPHONES, false);
}
}
void alsa_module::teardown() {
2016-11-25 12:55:15 +00:00
m_mixer.clear();
2016-12-09 21:53:26 +00:00
m_ctrl.clear();
snd_config_update_free_global();
2016-11-02 19:22:45 +00:00
}
bool alsa_module::has_event() {
2016-11-20 22:04:31 +00:00
// Poll for mixer and control events
2016-11-02 19:22:45 +00:00
try {
2016-11-25 12:55:15 +00:00
if (m_mixer[mixer::MASTER] && m_mixer[mixer::MASTER]->wait(25)) {
2016-11-02 19:22:45 +00:00
return true;
2016-11-25 12:55:15 +00:00
}
if (m_mixer[mixer::SPEAKER] && m_mixer[mixer::SPEAKER]->wait(25)) {
2016-11-02 19:22:45 +00:00
return true;
2016-11-25 12:55:15 +00:00
}
if (m_mixer[mixer::HEADPHONE] && m_mixer[mixer::HEADPHONE]->wait(25)) {
2016-11-02 19:22:45 +00:00
return true;
2016-11-25 12:55:15 +00:00
}
if (m_ctrl[control::HEADPHONE] && m_ctrl[control::HEADPHONE]->wait(25)) {
2016-11-02 19:22:45 +00:00
return true;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
} catch (const alsa_exception& e) {
m_log.err("%s: %s", name(), e.what());
}
2016-11-02 19:22:45 +00:00
return false;
}
bool alsa_module::update() {
2016-11-20 22:04:31 +00:00
// Consume pending events
2016-11-25 12:55:15 +00:00
if (m_mixer[mixer::MASTER]) {
m_mixer[mixer::MASTER]->process_events();
}
if (m_mixer[mixer::SPEAKER]) {
m_mixer[mixer::SPEAKER]->process_events();
}
if (m_mixer[mixer::HEADPHONE]) {
m_mixer[mixer::HEADPHONE]->process_events();
}
if (m_ctrl[control::HEADPHONE]) {
m_ctrl[control::HEADPHONE]->process_events();
}
2016-11-02 19:22:45 +00:00
2016-11-20 22:04:31 +00:00
// Get volume, mute and headphone state
2016-11-02 19:22:45 +00:00
m_volume = 100;
m_muted = false;
m_headphones = false;
2016-11-25 12:55:15 +00:00
try {
if (m_mixer[mixer::MASTER]) {
m_volume = m_volume * (m_mapped ? m_mixer[mixer::MASTER]->get_normalized_volume() / 100.0f
: m_mixer[mixer::MASTER]->get_volume() / 100.0f);
m_muted = m_muted || m_mixer[mixer::MASTER]->is_muted();
}
} catch (const alsa_exception& err) {
m_log.err("%s: Failed to query master mixer (%s)", name(), err.what());
2016-11-02 19:22:45 +00:00
}
2016-11-25 12:55:15 +00:00
try {
if (m_ctrl[control::HEADPHONE] && m_ctrl[control::HEADPHONE]->test_device_plugged()) {
m_headphones = true;
m_volume = m_volume * (m_mapped ? m_mixer[mixer::HEADPHONE]->get_normalized_volume() / 100.0f
: m_mixer[mixer::HEADPHONE]->get_volume() / 100.0f);
m_muted = m_muted || m_mixer[mixer::HEADPHONE]->is_muted();
}
} catch (const alsa_exception& err) {
m_log.err("%s: Failed to query headphone mixer (%s)", name(), err.what());
}
try {
if (!m_headphones && m_mixer[mixer::SPEAKER]) {
m_volume = m_volume * (m_mapped ? m_mixer[mixer::SPEAKER]->get_normalized_volume() / 100.0f
: m_mixer[mixer::SPEAKER]->get_volume() / 100.0f);
m_muted = m_muted || m_mixer[mixer::SPEAKER]->is_muted();
}
} catch (const alsa_exception& err) {
m_log.err("%s: Failed to query speaker mixer (%s)", name(), err.what());
2016-11-02 19:22:45 +00:00
}
2016-11-20 22:04:31 +00:00
// Replace label tokens
2016-11-02 19:22:45 +00:00
if (m_label_volume) {
m_label_volume->reset_tokens();
m_label_volume->replace_token("%percentage%", to_string(m_volume));
2016-11-02 19:22:45 +00:00
}
if (m_label_muted) {
m_label_muted->reset_tokens();
m_label_muted->replace_token("%percentage%", to_string(m_volume));
2016-11-02 19:22:45 +00:00
}
return true;
}
string alsa_module::get_format() const {
2016-11-02 19:22:45 +00:00
return m_muted ? FORMAT_MUTED : FORMAT_VOLUME;
}
string alsa_module::get_output() {
2016-12-05 04:32:10 +00:00
// Get the module output early so that
// the format prefix/suffix also gets wrapper
// with the cmd handlers
string output{module::get_output()};
2016-11-02 19:22:45 +00:00
if (m_handle_events) {
m_builder->cmd(mousebtn::LEFT, EVENT_TOGGLE_MUTE);
m_builder->cmd(mousebtn::SCROLL_UP, EVENT_VOLUME_UP);
m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_VOLUME_DOWN);
}
2016-11-02 19:22:45 +00:00
2016-12-05 04:32:10 +00:00
m_builder->append(output);
2016-11-02 19:22:45 +00:00
return m_builder->flush();
}
bool alsa_module::build(builder* builder, const string& tag) const {
2016-11-25 12:55:15 +00:00
if (tag == TAG_BAR_VOLUME) {
2016-11-02 19:22:45 +00:00
builder->node(m_bar_volume->output(m_volume));
2016-11-25 12:55:15 +00:00
} else if (tag == TAG_RAMP_VOLUME && (!m_headphones || !*m_ramp_headphones)) {
2016-11-02 19:22:45 +00:00
builder->node(m_ramp_volume->get_by_percentage(m_volume));
2016-11-25 12:55:15 +00:00
} else if (tag == TAG_RAMP_VOLUME && m_headphones && *m_ramp_headphones) {
2016-11-02 19:22:45 +00:00
builder->node(m_ramp_headphones->get_by_percentage(m_volume));
2016-11-25 12:55:15 +00:00
} else if (tag == TAG_LABEL_VOLUME) {
2016-11-02 19:22:45 +00:00
builder->node(m_label_volume);
2016-11-25 12:55:15 +00:00
} else if (tag == TAG_LABEL_MUTED) {
2016-11-02 19:22:45 +00:00
builder->node(m_label_muted);
2016-11-25 12:55:15 +00:00
} else {
2016-11-02 19:22:45 +00:00
return false;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
return true;
}
bool alsa_module::input(string&& cmd) {
if (!m_handle_events) {
2016-11-02 19:22:45 +00:00
return false;
} else if (cmd.compare(0, 3, EVENT_PREFIX) != 0) {
return false;
} else if (!m_mixer[mixer::MASTER]) {
return false;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
try {
vector<mixer_t> mixers;
bool headphones{m_headphones};
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (m_mixer[mixer::MASTER] && !m_mixer[mixer::MASTER]->get_name().empty()) {
2017-01-24 07:01:04 +00:00
mixers.emplace_back(new mixer_t::element_type(
string{m_mixer[mixer::MASTER]->get_name()}, string{m_mixer[mixer::MASTER]->get_sound_card()}));
2016-11-25 12:55:15 +00:00
}
if (m_mixer[mixer::HEADPHONE] && !m_mixer[mixer::HEADPHONE]->get_name().empty() && headphones) {
2017-01-24 07:01:04 +00:00
mixers.emplace_back(new mixer_t::element_type(
string{m_mixer[mixer::HEADPHONE]->get_name()}, string{m_mixer[mixer::HEADPHONE]->get_sound_card()}));
2016-11-25 12:55:15 +00:00
}
if (m_mixer[mixer::SPEAKER] && !m_mixer[mixer::SPEAKER]->get_name().empty() && !headphones) {
2017-01-24 07:01:04 +00:00
mixers.emplace_back(new mixer_t::element_type(
string{m_mixer[mixer::SPEAKER]->get_name()}, string{m_mixer[mixer::SPEAKER]->get_sound_card()}));
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
if (cmd.compare(0, strlen(EVENT_TOGGLE_MUTE), EVENT_TOGGLE_MUTE) == 0) {
for (auto&& mixer : mixers) {
mixer->set_mute(m_muted || mixers[0]->is_muted());
}
} else if (cmd.compare(0, strlen(EVENT_VOLUME_UP), EVENT_VOLUME_UP) == 0) {
for (auto&& mixer : mixers) {
2016-11-25 07:42:31 +00:00
m_mapped ? mixer->set_normalized_volume(math_util::cap<float>(mixer->get_normalized_volume() + 5, 0, 100))
: mixer->set_volume(math_util::cap<float>(mixer->get_volume() + 5, 0, 100));
2016-11-02 19:22:45 +00:00
}
} else if (cmd.compare(0, strlen(EVENT_VOLUME_DOWN), EVENT_VOLUME_DOWN) == 0) {
for (auto&& mixer : mixers) {
2016-11-25 07:42:31 +00:00
m_mapped ? mixer->set_normalized_volume(math_util::cap<float>(mixer->get_normalized_volume() - 5, 0, 100))
: mixer->set_volume(math_util::cap<float>(mixer->get_volume() - 5, 0, 100));
2016-11-02 19:22:45 +00:00
}
} else {
return false;
}
2016-11-25 12:55:15 +00:00
for (auto&& mixer : mixers) {
if (mixer->wait(0)) {
mixer->process_events();
}
}
} catch (const exception& err) {
2016-11-02 19:22:45 +00:00
m_log.err("%s: Failed to handle command (%s)", name(), err.what());
}
return true;
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END