feat(mpd): Add support for elapsed and total time

pull/268/head
Minijackson 2019-04-18 11:17:08 +02:00
parent 80a12d0238
commit 235997fa73
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
2 changed files with 85 additions and 45 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <condition_variable>
#include <thread> #include <thread>
#include <fmt/format.h> #include <fmt/format.h>
#include <mpd/client.h> #include <mpd/client.h>
@ -12,20 +13,27 @@ class MPD : public ALabel {
MPD(const std::string&, const Json::Value&); MPD(const std::string&, const Json::Value&);
auto update() -> void; auto update() -> void;
private: private:
std::thread worker(); std::thread periodic_updater();
void setLabel(); void setLabel();
std::string getStateIcon(); std::string getStateIcon();
std::string getOptionIcon(std::string optionName, bool activated); std::string getOptionIcon(std::string optionName, bool activated);
void tryConnect(); std::thread event_listener();
void checkErrors();
// Assumes `connection_lock_` is locked
void tryConnect();
// If checking errors on the main connection, make sure to lock it using
// `connection_lock_` before calling checkErrors
void checkErrors(mpd_connection* conn);
// Assumes `connection_lock_` is locked
void fetchState(); void fetchState();
void waitForEvent(); void waitForEvent();
bool handlePlayPause(GdkEventButton* const&); bool handlePlayPause(GdkEventButton* const&);
std::thread worker_; bool stopped();
bool playing();
using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>; using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>;
using unique_status = std::unique_ptr<mpd_status, decltype(&mpd_status_free)>; using unique_status = std::unique_ptr<mpd_status, decltype(&mpd_status_free)>;
@ -36,15 +44,21 @@ class MPD : public ALabel {
const char* server_; const char* server_;
const unsigned port_; const unsigned port_;
// We need a mutex here because we can trigger updates from multiple thread:
// the event based updates, the periodic updates needed for the elapsed time,
// and the click play/pause feature
std::mutex connection_lock_;
unique_connection connection_; unique_connection connection_;
// Use two connections because the main one will be blocking most of the time. // The alternate connection will be used to wait for events: since it will
// Used to send play/pause events and poll elapsed time. // be blocking (idle) we can't send commands via this connection
//
// No lock since only used in the event listener thread
unique_connection alternate_connection_; unique_connection alternate_connection_;
// Protect them using the `connection_lock_`
unique_status status_; unique_status status_;
mpd_state state_; mpd_state state_;
unique_song song_; unique_song song_;
bool stopped_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -1,5 +1,7 @@
#include "modules/mpd.hpp" #include "modules/mpd.hpp"
#include <fmt/chrono.h>
#include <iostream> #include <iostream>
waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config) waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
@ -19,7 +21,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
server_ = config["server"].asCString(); server_ = config["server"].asCString();
} }
worker_ = worker(); event_listener().detach();
event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect( event_box_.signal_button_press_event().connect(
@ -27,32 +29,45 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
} }
auto waybar::modules::MPD::update() -> void { auto waybar::modules::MPD::update() -> void {
std::lock_guard guard(connection_lock_);
tryConnect(); tryConnect();
if (connection_ != nullptr) { if (connection_ != nullptr) {
try { try {
bool wasPlaying = playing();
fetchState(); fetchState();
if (!wasPlaying && playing()) {
periodic_updater().detach();
}
} catch (std::exception e) { } catch (std::exception e) {
stopped_ = true; state_ = MPD_STATE_UNKNOWN;
} }
} }
setLabel(); setLabel();
} }
std::thread waybar::modules::MPD::worker() { std::thread waybar::modules::MPD::event_listener() {
return std::thread([this] () { return std::thread([this] {
while (true) { while (true) {
if (connection_ == nullptr) { if (connection_ == nullptr) {
// Retry periodically if no connection // Retry periodically if no connection
update(); update();
std::this_thread::sleep_for(interval_); std::this_thread::sleep_for(interval_);
} else { } else {
// Else, update on any event waitForEvent();
waitForEvent(); dp.emit();
update();
}
} }
}
});
}
std::thread waybar::modules::MPD::periodic_updater() {
return std::thread([this] {
while (connection_ != nullptr && playing()) {
dp.emit();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}); });
} }
@ -81,9 +96,10 @@ void waybar::modules::MPD::setLabel() {
auto format = format_; auto format = format_;
std::string artist, album_artist, album, title, date; std::string artist, album_artist, album, title, date;
std::chrono::seconds elapsedTime, totalTime;
std::string stateIcon = ""; 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";
label_.get_style_context()->add_class("stopped"); label_.get_style_context()->add_class("stopped");
@ -97,6 +113,8 @@ void waybar::modules::MPD::setLabel() {
album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0); album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0);
title = mpd_song_get_tag(song_.get(), MPD_TAG_TITLE, 0); title = mpd_song_get_tag(song_.get(), MPD_TAG_TITLE, 0);
date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0); date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0);
elapsedTime = std::chrono::seconds(mpd_status_get_elapsed_time(status_.get()));
totalTime = std::chrono::seconds(mpd_status_get_total_time(status_.get()));
} }
bool consumeActivated = mpd_status_get_consume(status_.get()); bool consumeActivated = mpd_status_get_consume(status_.get());
@ -115,6 +133,8 @@ void waybar::modules::MPD::setLabel() {
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("elapsedTime", elapsedTime),
fmt::arg("totalTime", totalTime),
fmt::arg("stateIcon", stateIcon), fmt::arg("stateIcon", stateIcon),
fmt::arg("consumeIcon", consumeIcon), fmt::arg("consumeIcon", consumeIcon),
fmt::arg("randomIcon", randomIcon), fmt::arg("randomIcon", randomIcon),
@ -150,15 +170,14 @@ std::string waybar::modules::MPD::getStateIcon() {
return ""; return "";
} }
if (stopped_) { if (stopped()) {
std::cerr << "MPD: Trying to fetch state icon while stopped" << std::endl; std::cerr << "MPD: Trying to fetch state icon while stopped" << std::endl;
return ""; return "";
} }
if (state_ == MPD_STATE_PLAY) { if (playing()) {
return config_["state-icons"]["playing"].asString(); return config_["state-icons"]["playing"].asString();
} else { } else {
// MPD_STATE_PAUSE
return config_["state-icons"]["paused"].asString(); return config_["state-icons"]["paused"].asString();
} }
} }
@ -201,18 +220,15 @@ void waybar::modules::MPD::tryConnect() {
} }
try { try {
checkErrors(); checkErrors(connection_.get());
} catch (std::runtime_error e) { } catch (std::runtime_error e) {
std::cerr << "Failed to connect to MPD: " << e.what() << std::endl; std::cerr << "Failed to connect to MPD: " << e.what() << std::endl;
connection_.reset(); connection_.reset();
alternate_connection_.reset(); alternate_connection_.reset();
} }
} }
void waybar::modules::MPD::checkErrors() { void waybar::modules::MPD::checkErrors(mpd_connection* conn) {
auto conn = connection_.get();
switch (mpd_connection_get_error(conn)) { switch (mpd_connection_get_error(conn)) {
case MPD_ERROR_SUCCESS: case MPD_ERROR_SUCCESS:
return; return;
@ -230,37 +246,47 @@ 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); auto conn = connection_.get();
checkErrors(); status_ = unique_status(mpd_run_status(conn), &mpd_status_free);
state_ = mpd_status_get_state(status_.get()); checkErrors(conn);
checkErrors(); state_ = mpd_status_get_state(status_.get());
stopped_ = state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP; checkErrors(conn);
song_ = unique_song(mpd_run_current_song(connection_.get()), &mpd_song_free); song_ = unique_song(mpd_run_current_song(conn), &mpd_song_free);
checkErrors(); checkErrors(conn);
} }
void waybar::modules::MPD::waitForEvent() { void waybar::modules::MPD::waitForEvent() {
auto conn = connection_.get(); auto conn = alternate_connection_.get();
// Wait for a player (play/pause), option (random, shuffle, etc.), or playlist change // Wait for a player (play/pause), option (random, shuffle, etc.), or playlist change
mpd_run_idle_mask(conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST)); mpd_run_idle_mask(conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST));
checkErrors(); checkErrors(conn);
} }
bool waybar::modules::MPD::handlePlayPause(GdkEventButton* const& e) { bool waybar::modules::MPD::handlePlayPause(GdkEventButton* const& e) {
if (e->type == GDK_2BUTTON_PRESS || e->type == GDK_3BUTTON_PRESS || alternate_connection_ == nullptr) { if (e->type == GDK_2BUTTON_PRESS || e->type == GDK_3BUTTON_PRESS || connection_ == nullptr) {
return false; return false;
} }
if (e->button == 1) { if (e->button == 1) {
if (stopped_) { std::lock_guard guard(connection_lock_);
mpd_run_play(alternate_connection_.get()); if (stopped()) {
mpd_run_play(connection_.get());
} else { } else {
mpd_run_toggle_pause(alternate_connection_.get()); mpd_run_toggle_pause(connection_.get());
} }
} else if (e->button == 3) { } else if (e->button == 3) {
mpd_run_stop(alternate_connection_.get()); std::lock_guard guard(connection_lock_);
mpd_run_stop(connection_.get());
} }
return true; return true;
} }
bool waybar::modules::MPD::stopped() {
return connection_ == nullptr || state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP;
}
bool waybar::modules::MPD::playing() {
return connection_ != nullptr && state_ == MPD_STATE_PLAY;
}