feat(Pulseadio): port icons

pull/45/head
Alexis 2018-08-29 23:50:41 +02:00
parent d0933ab50f
commit 6dd9b5ccc4
No known key found for this signature in database
GPG Key ID: 20B1EC2EBBA96BD9
5 changed files with 54 additions and 9 deletions

View File

@ -10,7 +10,7 @@ class ALabel : public IModule {
ALabel(const Json::Value&, const std::string format); ALabel(const Json::Value&, const std::string format);
virtual ~ALabel() = default; virtual ~ALabel() = default;
virtual auto update() -> void; virtual auto update() -> void;
virtual std::string getIcon(uint16_t percentage); virtual std::string getIcon(uint16_t, const std::string& alt = "");
virtual operator Gtk::Widget &(); virtual operator Gtk::Widget &();
protected: protected:
Gtk::EventBox event_box_; Gtk::EventBox event_box_;

View File

@ -19,12 +19,15 @@ class Pulseaudio : public ALabel {
static void sinkInfoCb(pa_context*, const pa_sink_info*, int, void*); static void sinkInfoCb(pa_context*, const pa_sink_info*, int, void*);
static void serverInfoCb(pa_context*, const pa_server_info*, void*); static void serverInfoCb(pa_context*, const pa_server_info*, void*);
const std::string getPortIcon() const;
pa_threaded_mainloop* mainloop_; pa_threaded_mainloop* mainloop_;
pa_mainloop_api* mainloop_api_; pa_mainloop_api* mainloop_api_;
pa_context* context_; pa_context* context_;
uint32_t sink_idx_{0}; uint32_t sink_idx_{0};
uint16_t volume_; uint16_t volume_;
bool muted_; bool muted_;
std::string port_name_;
std::string desc_; std::string desc_;
}; };

View File

@ -44,7 +44,11 @@
"pulseaudio": { "pulseaudio": {
"format": "{volume}% {icon}", "format": "{volume}% {icon}",
"format-muted": "", "format-muted": "",
"format-icons": ["", ""] "format-icons": {
"headphones": "",
"handsfree": "",
"default": ["", ""]
}
}, },
"custom/spotify": { "custom/spotify": {
"format": " {}", "format": " {}",

View File

@ -36,14 +36,25 @@ bool waybar::ALabel::handleToggle(GdkEventButton* const& ev)
return true; return true;
} }
std::string waybar::ALabel::getIcon(uint16_t percentage) std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt)
{ {
if (!config_["format-icons"] || !config_["format-icons"].isArray()) { auto format_icons = config_["format-icons"];
return ""; if (format_icons.isObject()) {
if (!alt.empty() && format_icons[alt]) {
format_icons = format_icons[alt];
} else {
format_icons = format_icons["default"];
} }
auto size = config_["format-icons"].size(); }
if (format_icons.isArray()) {
auto size = format_icons.size();
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return config_["format-icons"][idx].asString(); format_icons = format_icons[idx];
}
if (format_icons.isString()) {
return format_icons.asString();
}
return "";
} }
waybar::ALabel::operator Gtk::Widget &() { waybar::ALabel::operator Gtk::Widget &() {

View File

@ -89,6 +89,7 @@ void waybar::modules::Pulseaudio::sinkInfoCb(pa_context* /*context*/,
pa->volume_ = volume * 100.0f; pa->volume_ = volume * 100.0f;
pa->muted_ = i->mute != 0; pa->muted_ = i->mute != 0;
pa->desc_ = i->description; pa->desc_ = i->description;
pa->port_name_ = i->active_port->name;
pa->dp.emit(); pa->dp.emit();
} }
} }
@ -104,6 +105,27 @@ void waybar::modules::Pulseaudio::serverInfoCb(pa_context *context,
sinkInfoCb, data); sinkInfoCb, data);
} }
const std::string waybar::modules::Pulseaudio::getPortIcon() const
{
std::vector<std::string> ports = {
"headphones",
"speaker",
"hdmi",
"headset",
"handsfree",
"portable",
"car",
"hifi",
"phone",
};
for (auto port : ports) {
if (port_name_.find(port) != std::string::npos) {
return port;
}
}
return "";
}
auto waybar::modules::Pulseaudio::update() -> void auto waybar::modules::Pulseaudio::update() -> void
{ {
auto format = format_; auto format = format_;
@ -111,11 +133,16 @@ auto waybar::modules::Pulseaudio::update() -> void
format = format =
config_["format-muted"] ? config_["format-muted"].asString() : format; config_["format-muted"] ? config_["format-muted"].asString() : format;
label_.get_style_context()->add_class("muted"); label_.get_style_context()->add_class("muted");
} else if (port_name_.find("a2dp_sink") != std::string::npos) {
format = config_["format-bluetooth"]
? config_["format-bluetooth"].asString() : format;
label_.get_style_context()->add_class("bluetooth");
} else { } else {
label_.get_style_context()->remove_class("muted"); label_.get_style_context()->remove_class("muted");
label_.get_style_context()->add_class("bluetooth");
} }
label_.set_label(fmt::format(format, label_.set_label(fmt::format(format,
fmt::arg("volume", volume_), fmt::arg("volume", volume_),
fmt::arg("icon", getIcon(volume_)))); fmt::arg("icon", getIcon(volume_, getPortIcon()))));
label_.set_tooltip_text(desc_); label_.set_tooltip_text(desc_);
} }