From 07dab2baec768276718d7e88f872ba497b1f8395 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Wed, 17 Apr 2019 15:09:06 +0200 Subject: [PATCH] feat(mpd): Add support for options (random, repeat, etc.) --- include/modules/mpd.hpp | 3 ++- src/modules/mpd.cpp | 46 ++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp index 49169701..fa9662c9 100644 --- a/include/modules/mpd.hpp +++ b/include/modules/mpd.hpp @@ -15,6 +15,7 @@ class MPD : public ALabel { std::thread worker(); void setLabel(); std::string getStateIcon(); + std::string getOptionIcon(std::string optionName, bool activated); void tryConnect(); void checkErrors(); @@ -26,7 +27,7 @@ class MPD : public ALabel { using unique_connection = std::unique_ptr; using unique_status = std::unique_ptr; - using unique_song = std::unique_ptr; + using unique_song = std::unique_ptr; // Not using unique_ptr since we don't manage the pointer // (It's either nullptr, or from the config) diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index 1da68774..aafc4738 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -94,6 +94,15 @@ void waybar::modules::MPD::setLabel() { date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0); } + bool consumeActivated = mpd_status_get_consume(status_.get()); + std::string consumeIcon = getOptionIcon("consume", consumeActivated); + bool randomActivated = mpd_status_get_random(status_.get()); + std::string randomIcon = getOptionIcon("random", randomActivated); + bool repeatActivated = mpd_status_get_repeat(status_.get()); + std::string repeatIcon = getOptionIcon("repeat", repeatActivated); + bool singleActivated = mpd_status_get_single(status_.get()); + std::string singleIcon = getOptionIcon("single", singleActivated); + // TODO: format can fail label_.set_markup(fmt::format(format, fmt::arg("artist", artist), @@ -101,7 +110,11 @@ void waybar::modules::MPD::setLabel() { fmt::arg("album", album), fmt::arg("title", title), fmt::arg("date", date), - fmt::arg("stateIcon", stateIcon))); + fmt::arg("stateIcon", stateIcon), + fmt::arg("consumeIcon", consumeIcon), + fmt::arg("randomIcon", randomIcon), + fmt::arg("repeatIcon", repeatIcon), + fmt::arg("singleIcon", singleIcon))); if (tooltipEnabled()) { std::string tooltip_format; @@ -113,14 +126,17 @@ void waybar::modules::MPD::setLabel() { fmt::arg("album", album), fmt::arg("title", title), fmt::arg("date", date), - fmt::arg("stateIcon", stateIcon)); + fmt::arg("stateIcon", stateIcon), + fmt::arg("consumeIcon", consumeIcon), + fmt::arg("randomIcon", randomIcon), + fmt::arg("repeatIcon", repeatIcon), + fmt::arg("singleIcon", singleIcon)); 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 ""; } @@ -142,6 +158,23 @@ std::string waybar::modules::MPD::getStateIcon() { } } +std::string waybar::modules::MPD::getOptionIcon(std::string optionName, bool activated) { + if (!config_[optionName + "-icons"].isObject()) { + return ""; + } + + if (connection_ == nullptr) { + std::cerr << "MPD: Trying to fetch option icon while disconnected" << std::endl; + return ""; + } + + if (activated) { + return config_[optionName + "-icons"]["on"].asString(); + } else { + return config_[optionName + "-icons"]["off"].asString(); + } +} + void waybar::modules::MPD::tryConnect() { if (connection_ != nullptr) { return; @@ -190,14 +223,13 @@ void waybar::modules::MPD::fetchState() { checkErrors(); stopped_ = state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP; - mpd_send_current_song(connection_.get()); - song_ = unique_song(mpd_recv_song(connection_.get()), &mpd_song_free); - mpd_response_finish(connection_.get()); + song_ = unique_song(mpd_run_current_song(connection_.get()), &mpd_song_free); checkErrors(); } void waybar::modules::MPD::waitForEvent() { auto conn = connection_.get(); - mpd_run_idle_mask(conn, MPD_IDLE_PLAYER /* | MPD_IDLE_OPTIONS */); + // Wait for a player (play/pause), option (random, shuffle, etc.), or playlist change + mpd_run_idle_mask(conn, static_cast(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST)); checkErrors(); }