feat(mpd): Add support for play/pause icons

This commit is contained in:
Minijackson 2019-04-17 13:55:08 +02:00
parent 557b786ce0
commit cbfcec4867
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
2 changed files with 41 additions and 7 deletions

View File

@ -14,8 +14,11 @@ class MPD : public ALabel {
private: private:
std::thread worker(); std::thread worker();
void setLabel(); void setLabel();
std::string getStateIcon();
void tryConnect(); void tryConnect();
void checkErrors(); void checkErrors();
void fetchState(); void fetchState();
void waitForEvent(); void waitForEvent();
@ -28,10 +31,11 @@ class MPD : public ALabel {
// Not using unique_ptr since we don't manage the pointer // Not using unique_ptr since we don't manage the pointer
// (It's either nullptr, or from the config) // (It's either nullptr, or from the config)
const char* server_; const char* server_;
unsigned port_; const unsigned port_;
unique_connection connection_; unique_connection connection_;
unique_status status_; unique_status status_;
mpd_state state_;
unique_song song_; unique_song song_;
bool stopped_; bool stopped_;

View File

@ -77,6 +77,7 @@ void waybar::modules::MPD::setLabel() {
std::string artist, album_artist, album, title, date; std::string artist, album_artist, album, title, date;
std::string stateIcon = "";
if (stopped_) { if (stopped_) {
format = config_["format-stopped"].isString() ? format = config_["format-stopped"].isString() ?
config_["format-stopped"].asString() : "stopped"; config_["format-stopped"].asString() : "stopped";
@ -84,6 +85,8 @@ void waybar::modules::MPD::setLabel() {
} else { } else {
label_.get_style_context()->remove_class("stopped"); label_.get_style_context()->remove_class("stopped");
stateIcon = getStateIcon();
artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0); artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0);
album_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0); album_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0);
album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0); album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0);
@ -91,12 +94,14 @@ void waybar::modules::MPD::setLabel() {
date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0); date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0);
} }
// TODO: format can fail
label_.set_markup(fmt::format(format, label_.set_markup(fmt::format(format,
fmt::arg("artist", artist), fmt::arg("artist", artist),
fmt::arg("album-artist", album_artist), fmt::arg("albumArtist", album_artist),
fmt::arg("album", album), fmt::arg("album", album),
fmt::arg("title", title), fmt::arg("title", title),
fmt::arg("date", date))); fmt::arg("date", date),
fmt::arg("stateIcon", stateIcon)));
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
@ -104,14 +109,39 @@ void waybar::modules::MPD::setLabel() {
config_["tooltip-format"].asString() : "MPD (connected)"; config_["tooltip-format"].asString() : "MPD (connected)";
auto tooltip_text = fmt::format(tooltip_format, auto tooltip_text = fmt::format(tooltip_format,
fmt::arg("artist", artist), fmt::arg("artist", artist),
fmt::arg("album-artist", album_artist), fmt::arg("albumArtist", album_artist),
fmt::arg("album", album), fmt::arg("album", album),
fmt::arg("title", title), fmt::arg("title", title),
fmt::arg("date", date)); fmt::arg("date", date),
fmt::arg("stateIcon", stateIcon));
label_.set_tooltip_text(tooltip_text); label_.set_tooltip_text(tooltip_text);
} }
} }
std::string waybar::modules::MPD::getStateIcon() {
if (!config_["state-icons"].isObject()) {
std::cerr << "No state icons defined" << std::endl;
return "";
}
if (connection_ == nullptr) {
std::cerr << "MPD: Trying to fetch state icon while disconnected" << std::endl;
return "";
}
if (stopped_) {
std::cerr << "MPD: Trying to fetch state icon while stopped" << std::endl;
return "";
}
if (state_ == MPD_STATE_PLAY) {
return config_["state-icons"]["playing"].asString();
} else {
// MPD_STATE_PAUSE
return config_["state-icons"]["paused"].asString();
}
}
void waybar::modules::MPD::tryConnect() { void waybar::modules::MPD::tryConnect() {
if (connection_ != nullptr) { if (connection_ != nullptr) {
return; return;
@ -156,9 +186,9 @@ void waybar::modules::MPD::checkErrors() {
void waybar::modules::MPD::fetchState() { void waybar::modules::MPD::fetchState() {
status_ = unique_status(mpd_run_status(connection_.get()), &mpd_status_free); status_ = unique_status(mpd_run_status(connection_.get()), &mpd_status_free);
checkErrors(); checkErrors();
mpd_state state = mpd_status_get_state(status_.get()); state_ = mpd_status_get_state(status_.get());
checkErrors(); checkErrors();
stopped_ = state == MPD_STATE_UNKNOWN || state == MPD_STATE_STOP; stopped_ = state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP;
mpd_send_current_song(connection_.get()); mpd_send_current_song(connection_.get());
song_ = unique_song(mpd_recv_song(connection_.get()), &mpd_song_free); song_ = unique_song(mpd_recv_song(connection_.get()), &mpd_song_free);