1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/src/modules/volume.cpp

267 lines
9 KiB
C++
Raw Normal View History

2016-11-02 15:22:45 -04:00
#include "modules/volume.hpp"
#include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp"
2016-11-02 15:22:45 -04:00
#include "utils/math.hpp"
2016-11-20 17:04:31 -05:00
#include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-11-02 15:22:45 -04:00
namespace modules {
2016-11-20 17:04:31 -05:00
template class module<volume_module>;
template class event_module<volume_module>;
2016-11-02 15:22:45 -04:00
2016-11-20 17:04:31 -05:00
void volume_module::setup() {
// Load configuration values
2016-11-02 15:22:45 -04:00
string master_mixer_name{"Master"};
string speaker_mixer_name;
string headphone_mixer_name;
GET_CONFIG_VALUE(name(), master_mixer_name, "master-mixer");
GET_CONFIG_VALUE(name(), speaker_mixer_name, "speaker-mixer");
GET_CONFIG_VALUE(name(), headphone_mixer_name, "headphone-mixer");
2016-11-20 17:04:31 -05:00
GET_CONFIG_VALUE(name(), m_mapped, "mapped");
2016-11-02 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
if (!headphone_mixer_name.empty()) {
2016-11-02 15:22:45 -04:00
REQ_CONFIG_VALUE(name(), m_headphoneid, "headphone-id");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
if (string_util::compare(speaker_mixer_name, "master")) {
2016-11-02 15:22:45 -04:00
throw module_error("Master mixer is already defined");
2016-11-25 07:55:15 -05:00
}
if (string_util::compare(headphone_mixer_name, "master")) {
2016-11-02 15:22:45 -04:00
throw module_error("Master mixer is already defined");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
2016-11-20 17:04:31 -05:00
// Setup mixers
2016-11-02 15:22:45 -04:00
try {
2016-11-25 07:55:15 -05:00
if (!master_mixer_name.empty()) {
m_mixer[mixer::MASTER].reset(new mixer_t::element_type{master_mixer_name});
}
if (!speaker_mixer_name.empty()) {
m_mixer[mixer::SPEAKER].reset(new mixer_t::element_type{speaker_mixer_name});
}
if (!headphone_mixer_name.empty()) {
m_mixer[mixer::HEADPHONE].reset(new mixer_t::element_type{headphone_mixer_name});
}
if (m_mixer[mixer::HEADPHONE]) {
m_ctrl[control::HEADPHONE].reset(new control_t::element_type{m_headphoneid});
}
if (m_mixer.empty()) {
2016-11-02 15:22:45 -04:00
throw module_error("No configured mixers");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
} catch (const alsa_mixer_error& err) {
throw module_error(err.what());
} catch (const alsa_ctl_interface_error& err) {
throw module_error(err.what());
}
2016-11-20 17:04:31 -05:00
// Add formats and elements
2016-11-25 02:42:31 -05: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 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
if (m_formatter->has(TAG_BAR_VOLUME)) {
2016-11-02 15:22:45 -04:00
m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME);
2016-11-25 07:55:15 -05:00
}
if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) {
2016-11-02 15:22:45 -04:00
m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%");
2016-11-25 07:55:15 -05:00
}
if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) {
2016-11-02 15:22:45 -04:00
m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04: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 volume_module::teardown() {
2016-11-25 07:55:15 -05:00
m_mixer.clear();
2016-11-02 15:22:45 -04:00
}
bool volume_module::has_event() {
2016-11-20 17:04:31 -05:00
// Poll for mixer and control events
2016-11-02 15:22:45 -04:00
try {
2016-11-25 07:55:15 -05:00
if (m_mixer[mixer::MASTER] && m_mixer[mixer::MASTER]->wait(25)) {
2016-11-02 15:22:45 -04:00
return true;
2016-11-25 07:55:15 -05:00
}
if (m_mixer[mixer::SPEAKER] && m_mixer[mixer::SPEAKER]->wait(25)) {
2016-11-02 15:22:45 -04:00
return true;
2016-11-25 07:55:15 -05:00
}
if (m_mixer[mixer::HEADPHONE] && m_mixer[mixer::HEADPHONE]->wait(25)) {
2016-11-02 15:22:45 -04:00
return true;
2016-11-25 07:55:15 -05:00
}
if (m_ctrl[control::HEADPHONE] && m_ctrl[control::HEADPHONE]->wait(25)) {
2016-11-02 15:22:45 -04:00
return true;
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
} catch (const alsa_exception& e) {
m_log.err("%s: %s", name(), e.what());
}
return false;
}
bool volume_module::update() {
2016-11-20 17:04:31 -05:00
// Consume pending events
2016-11-25 07:55:15 -05: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 15:22:45 -04:00
2016-11-20 17:04:31 -05:00
// Get volume, mute and headphone state
2016-11-02 15:22:45 -04:00
m_volume = 100;
m_muted = false;
m_headphones = false;
2016-11-25 07:55:15 -05: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 15:22:45 -04:00
}
2016-11-25 07:55:15 -05: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 15:22:45 -04:00
}
2016-11-20 17:04:31 -05:00
// Replace label tokens
2016-11-02 15:22:45 -04:00
if (m_label_volume) {
m_label_volume->reset_tokens();
m_label_volume->replace_token("%percentage%", to_string(m_volume) + "%");
}
if (m_label_muted) {
m_label_muted->reset_tokens();
m_label_muted->replace_token("%percentage%", to_string(m_volume) + "%");
}
return true;
}
string volume_module::get_format() const {
return m_muted ? FORMAT_MUTED : FORMAT_VOLUME;
}
string volume_module::get_output() {
m_builder->cmd(mousebtn::LEFT, EVENT_TOGGLE_MUTE);
2016-11-25 07:55:15 -05:00
if (!m_muted && m_volume < 100) {
2016-11-02 15:22:45 -04:00
m_builder->cmd(mousebtn::SCROLL_UP, EVENT_VOLUME_UP);
2016-11-25 07:55:15 -05:00
}
if (!m_muted && m_volume > 0) {
2016-11-02 15:22:45 -04:00
m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_VOLUME_DOWN);
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
2016-11-24 22:10:26 -05:00
m_builder->append(module::get_output());
2016-11-02 15:22:45 -04:00
return m_builder->flush();
}
2016-11-25 07:55:15 -05:00
bool volume_module::build(builder* builder, const string& tag) const {
if (tag == TAG_BAR_VOLUME) {
2016-11-02 15:22:45 -04:00
builder->node(m_bar_volume->output(m_volume));
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_RAMP_VOLUME && (!m_headphones || !*m_ramp_headphones)) {
2016-11-02 15:22:45 -04:00
builder->node(m_ramp_volume->get_by_percentage(m_volume));
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_RAMP_VOLUME && m_headphones && *m_ramp_headphones) {
2016-11-02 15:22:45 -04:00
builder->node(m_ramp_headphones->get_by_percentage(m_volume));
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_LABEL_VOLUME) {
2016-11-02 15:22:45 -04:00
builder->node(m_label_volume);
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_LABEL_MUTED) {
2016-11-02 15:22:45 -04:00
builder->node(m_label_muted);
2016-11-25 07:55:15 -05:00
} else {
2016-11-02 15:22:45 -04:00
return false;
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
return true;
}
bool volume_module::handle_event(string cmd) {
2016-11-25 07:55:15 -05:00
if (cmd.compare(0, 3, EVENT_PREFIX) != 0) {
2016-11-02 15:22:45 -04:00
return false;
2016-11-25 07:55:15 -05:00
}
if (!m_mixer[mixer::MASTER]) {
2016-11-02 15:22:45 -04:00
return false;
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
try {
vector<mixer_t> mixers;
bool headphones{m_headphones};
2016-11-02 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
if (m_mixer[mixer::MASTER] && !m_mixer[mixer::MASTER]->get_name().empty()) {
mixers.emplace_back(new mixer_t::element_type(m_mixer[mixer::MASTER]->get_name()));
}
if (m_mixer[mixer::HEADPHONE] && !m_mixer[mixer::HEADPHONE]->get_name().empty() && headphones) {
mixers.emplace_back(new mixer_t::element_type(m_mixer[mixer::HEADPHONE]->get_name()));
}
if (m_mixer[mixer::SPEAKER] && !m_mixer[mixer::SPEAKER]->get_name().empty() && !headphones) {
mixers.emplace_back(new mixer_t::element_type(m_mixer[mixer::SPEAKER]->get_name()));
}
2016-11-02 15:22:45 -04: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 02:42:31 -05: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 15:22:45 -04:00
}
} else if (cmd.compare(0, strlen(EVENT_VOLUME_DOWN), EVENT_VOLUME_DOWN) == 0) {
for (auto&& mixer : mixers) {
2016-11-25 02:42:31 -05: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 15:22:45 -04:00
}
} else {
return false;
}
2016-11-25 07:55:15 -05:00
for (auto&& mixer : mixers) {
if (mixer->wait(0)) {
mixer->process_events();
}
}
} catch (const exception& err) {
2016-11-02 15:22:45 -04:00
m_log.err("%s: Failed to handle command (%s)", name(), err.what());
}
return true;
}
bool volume_module::receive_events() const {
return true;
}
}
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END