2019-04-16 14:34:37 +00:00
|
|
|
#include "modules/mpd.hpp"
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
#include <fmt/chrono.h>
|
2019-04-16 14:34:37 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
|
2019-04-17 09:01:35 +00:00
|
|
|
: ALabel(config, "{album} - {artist} - {title}", 5),
|
2019-04-18 09:48:38 +00:00
|
|
|
module_name_(id.empty() ? "mpd" : "mpd#" + id),
|
2019-04-17 09:01:35 +00:00
|
|
|
server_(nullptr),
|
|
|
|
port_(config["port"].asUInt()),
|
2019-04-16 14:34:37 +00:00
|
|
|
connection_(nullptr, &mpd_connection_free),
|
2019-04-17 14:16:39 +00:00
|
|
|
alternate_connection_(nullptr, &mpd_connection_free),
|
2019-04-16 14:34:37 +00:00
|
|
|
status_(nullptr, &mpd_status_free),
|
|
|
|
song_(nullptr, &mpd_song_free) {
|
|
|
|
label_.set_name("mpd");
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config["server"].isNull()) {
|
2019-04-17 09:01:35 +00:00
|
|
|
server_ = config["server"].asCString();
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
event_listener().detach();
|
2019-04-17 14:16:39 +00:00
|
|
|
|
|
|
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
2019-04-18 09:31:47 +00:00
|
|
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &MPD::handlePlayPause));
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::MPD::update() -> void {
|
2019-04-18 09:17:08 +00:00
|
|
|
std::lock_guard guard(connection_lock_);
|
2019-04-16 14:34:37 +00:00
|
|
|
tryConnect();
|
|
|
|
|
|
|
|
if (connection_ != nullptr) {
|
|
|
|
try {
|
2019-04-18 09:17:08 +00:00
|
|
|
bool wasPlaying = playing();
|
2019-04-16 14:34:37 +00:00
|
|
|
fetchState();
|
2019-04-18 09:17:08 +00:00
|
|
|
if (!wasPlaying && playing()) {
|
|
|
|
periodic_updater().detach();
|
|
|
|
}
|
2019-04-16 14:34:37 +00:00
|
|
|
} catch (std::exception e) {
|
2019-04-18 09:17:08 +00:00
|
|
|
state_ = MPD_STATE_UNKNOWN;
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setLabel();
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
std::thread waybar::modules::MPD::event_listener() {
|
|
|
|
return std::thread([this] {
|
|
|
|
while (true) {
|
|
|
|
if (connection_ == nullptr) {
|
|
|
|
// Retry periodically if no connection
|
|
|
|
update();
|
|
|
|
std::this_thread::sleep_for(interval_);
|
|
|
|
} else {
|
|
|
|
waitForEvent();
|
|
|
|
dp.emit();
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
2019-04-18 09:17:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2019-04-16 14:34:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::MPD::setLabel() {
|
|
|
|
if (connection_ == nullptr) {
|
|
|
|
label_.get_style_context()->add_class("disconnected");
|
|
|
|
// In the case connection closed while MPD is stopped
|
|
|
|
label_.get_style_context()->remove_class("stopped");
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
auto format = config_["format-disconnected"].isString()
|
|
|
|
? config_["format-disconnected"].asString()
|
|
|
|
: "disconnected";
|
2019-04-16 14:34:37 +00:00
|
|
|
label_.set_markup(format);
|
2019-04-17 08:46:08 +00:00
|
|
|
|
|
|
|
if (tooltipEnabled()) {
|
|
|
|
std::string tooltip_format;
|
2019-04-18 09:31:47 +00:00
|
|
|
tooltip_format = config_["tooltip-format-disconnected"].isString()
|
|
|
|
? config_["tooltip-format-disconnected"].asString()
|
|
|
|
: "MPD (disconnected)";
|
2019-04-17 08:46:08 +00:00
|
|
|
// Nothing to format
|
|
|
|
label_.set_tooltip_text(tooltip_format);
|
|
|
|
}
|
2019-04-16 14:34:37 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class("disconnected");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto format = format_;
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
std::string artist, album_artist, album, title, date;
|
2019-04-18 09:17:08 +00:00
|
|
|
std::chrono::seconds elapsedTime, totalTime;
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-17 11:55:08 +00:00
|
|
|
std::string stateIcon = "";
|
2019-04-18 09:17:08 +00:00
|
|
|
if (stopped()) {
|
2019-04-18 09:31:47 +00:00
|
|
|
format =
|
|
|
|
config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped";
|
2019-04-16 14:34:37 +00:00
|
|
|
label_.get_style_context()->add_class("stopped");
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class("stopped");
|
|
|
|
|
2019-04-17 11:55:08 +00:00
|
|
|
stateIcon = getStateIcon();
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0);
|
2019-04-16 14:34:37 +00:00
|
|
|
album_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0);
|
2019-04-18 09:31:47 +00:00
|
|
|
album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0);
|
|
|
|
title = mpd_song_get_tag(song_.get(), MPD_TAG_TITLE, 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()));
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
bool consumeActivated = mpd_status_get_consume(status_.get());
|
2019-04-17 13:09:06 +00:00
|
|
|
std::string consumeIcon = getOptionIcon("consume", consumeActivated);
|
2019-04-18 09:31:47 +00:00
|
|
|
bool randomActivated = mpd_status_get_random(status_.get());
|
2019-04-17 13:09:06 +00:00
|
|
|
std::string randomIcon = getOptionIcon("random", randomActivated);
|
2019-04-18 09:31:47 +00:00
|
|
|
bool repeatActivated = mpd_status_get_repeat(status_.get());
|
2019-04-17 13:09:06 +00:00
|
|
|
std::string repeatIcon = getOptionIcon("repeat", repeatActivated);
|
2019-04-18 09:31:47 +00:00
|
|
|
bool singleActivated = mpd_status_get_single(status_.get());
|
2019-04-17 13:09:06 +00:00
|
|
|
std::string singleIcon = getOptionIcon("single", singleActivated);
|
|
|
|
|
2019-04-17 11:55:08 +00:00
|
|
|
// TODO: format can fail
|
2019-04-16 14:34:37 +00:00
|
|
|
label_.set_markup(fmt::format(format,
|
2019-04-18 09:31:47 +00:00
|
|
|
fmt::arg("artist", artist),
|
|
|
|
fmt::arg("albumArtist", album_artist),
|
|
|
|
fmt::arg("album", album),
|
|
|
|
fmt::arg("title", title),
|
|
|
|
fmt::arg("date", date),
|
|
|
|
fmt::arg("elapsedTime", elapsedTime),
|
|
|
|
fmt::arg("totalTime", totalTime),
|
|
|
|
fmt::arg("stateIcon", stateIcon),
|
|
|
|
fmt::arg("consumeIcon", consumeIcon),
|
|
|
|
fmt::arg("randomIcon", randomIcon),
|
|
|
|
fmt::arg("repeatIcon", repeatIcon),
|
|
|
|
fmt::arg("singleIcon", singleIcon)));
|
2019-04-16 14:34:37 +00:00
|
|
|
|
|
|
|
if (tooltipEnabled()) {
|
|
|
|
std::string tooltip_format;
|
2019-04-18 09:31:47 +00:00
|
|
|
tooltip_format = config_["tooltip-format"].isString() ? config_["tooltip-format"].asString()
|
|
|
|
: "MPD (connected)";
|
2019-04-16 14:34:37 +00:00
|
|
|
auto tooltip_text = fmt::format(tooltip_format,
|
2019-04-18 09:31:47 +00:00
|
|
|
fmt::arg("artist", artist),
|
|
|
|
fmt::arg("albumArtist", album_artist),
|
|
|
|
fmt::arg("album", album),
|
|
|
|
fmt::arg("title", title),
|
|
|
|
fmt::arg("date", date),
|
|
|
|
fmt::arg("stateIcon", stateIcon),
|
|
|
|
fmt::arg("consumeIcon", consumeIcon),
|
|
|
|
fmt::arg("randomIcon", randomIcon),
|
|
|
|
fmt::arg("repeatIcon", repeatIcon),
|
|
|
|
fmt::arg("singleIcon", singleIcon));
|
2019-04-16 14:34:37 +00:00
|
|
|
label_.set_tooltip_text(tooltip_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:55:08 +00:00
|
|
|
std::string waybar::modules::MPD::getStateIcon() {
|
|
|
|
if (!config_["state-icons"].isObject()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection_ == nullptr) {
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Trying to fetch state icon while disconnected" << std::endl;
|
2019-04-17 11:55:08 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
if (stopped()) {
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Trying to fetch state icon while stopped" << std::endl;
|
2019-04-17 11:55:08 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
if (playing()) {
|
2019-04-17 11:55:08 +00:00
|
|
|
return config_["state-icons"]["playing"].asString();
|
|
|
|
} else {
|
|
|
|
return config_["state-icons"]["paused"].asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:09:06 +00:00
|
|
|
std::string waybar::modules::MPD::getOptionIcon(std::string optionName, bool activated) {
|
|
|
|
if (!config_[optionName + "-icons"].isObject()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection_ == nullptr) {
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Trying to fetch option icon while disconnected" << std::endl;
|
2019-04-17 13:09:06 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activated) {
|
|
|
|
return config_[optionName + "-icons"]["on"].asString();
|
|
|
|
} else {
|
|
|
|
return config_[optionName + "-icons"]["off"].asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:34:37 +00:00
|
|
|
void waybar::modules::MPD::tryConnect() {
|
|
|
|
if (connection_ != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
connection_ = unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
alternate_connection_ =
|
|
|
|
unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
|
2019-04-17 14:16:39 +00:00
|
|
|
|
|
|
|
if (connection_ == nullptr || alternate_connection_ == nullptr) {
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Failed to connect to MPD" << std::endl;
|
2019-04-17 14:16:39 +00:00
|
|
|
connection_.reset();
|
|
|
|
alternate_connection_.reset();
|
2019-04-16 14:34:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-04-18 09:17:08 +00:00
|
|
|
checkErrors(connection_.get());
|
2019-04-16 14:34:37 +00:00
|
|
|
} catch (std::runtime_error e) {
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Failed to connect to MPD: " << e.what() << std::endl;
|
2019-04-16 14:34:37 +00:00
|
|
|
connection_.reset();
|
2019-04-17 14:16:39 +00:00
|
|
|
alternate_connection_.reset();
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:17:08 +00:00
|
|
|
void waybar::modules::MPD::checkErrors(mpd_connection* conn) {
|
2019-04-16 14:34:37 +00:00
|
|
|
switch (mpd_connection_get_error(conn)) {
|
|
|
|
case MPD_ERROR_SUCCESS:
|
|
|
|
return;
|
|
|
|
case MPD_ERROR_CLOSED:
|
2019-04-18 09:48:38 +00:00
|
|
|
std::cerr << module_name_ << ": Connection to MPD closed" << std::endl;
|
2019-04-16 14:34:37 +00:00
|
|
|
mpd_connection_clear_error(conn);
|
|
|
|
connection_.reset();
|
2019-04-17 14:16:39 +00:00
|
|
|
alternate_connection_.reset();
|
2019-04-18 09:39:04 +00:00
|
|
|
state_ = MPD_STATE_UNKNOWN;
|
2019-04-16 14:34:37 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
auto error_message = mpd_connection_get_error_message(conn);
|
|
|
|
mpd_connection_clear_error(conn);
|
|
|
|
throw std::runtime_error(std::string(error_message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::MPD::fetchState() {
|
2019-04-18 09:17:08 +00:00
|
|
|
auto conn = connection_.get();
|
|
|
|
status_ = unique_status(mpd_run_status(conn), &mpd_status_free);
|
|
|
|
checkErrors(conn);
|
|
|
|
state_ = mpd_status_get_state(status_.get());
|
|
|
|
checkErrors(conn);
|
|
|
|
|
|
|
|
song_ = unique_song(mpd_run_current_song(conn), &mpd_song_free);
|
|
|
|
checkErrors(conn);
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::MPD::waitForEvent() {
|
2019-04-18 09:17:08 +00:00
|
|
|
auto conn = alternate_connection_.get();
|
2019-04-18 09:31:47 +00:00
|
|
|
// 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));
|
2019-04-18 09:17:08 +00:00
|
|
|
checkErrors(conn);
|
2019-04-16 14:34:37 +00:00
|
|
|
}
|
2019-04-17 14:16:39 +00:00
|
|
|
|
|
|
|
bool waybar::modules::MPD::handlePlayPause(GdkEventButton* const& e) {
|
2019-04-18 09:17:08 +00:00
|
|
|
if (e->type == GDK_2BUTTON_PRESS || e->type == GDK_3BUTTON_PRESS || connection_ == nullptr) {
|
2019-04-17 14:16:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->button == 1) {
|
2019-04-18 09:17:08 +00:00
|
|
|
std::lock_guard guard(connection_lock_);
|
|
|
|
if (stopped()) {
|
|
|
|
mpd_run_play(connection_.get());
|
2019-04-17 14:16:39 +00:00
|
|
|
} else {
|
2019-04-18 09:17:08 +00:00
|
|
|
mpd_run_toggle_pause(connection_.get());
|
2019-04-17 14:16:39 +00:00
|
|
|
}
|
|
|
|
} else if (e->button == 3) {
|
2019-04-18 09:17:08 +00:00
|
|
|
std::lock_guard guard(connection_lock_);
|
|
|
|
mpd_run_stop(connection_.get());
|
2019-04-17 14:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-18 09:17:08 +00:00
|
|
|
|
|
|
|
bool waybar::modules::MPD::stopped() {
|
|
|
|
return connection_ == nullptr || state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP;
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
bool waybar::modules::MPD::playing() { return connection_ != nullptr && state_ == MPD_STATE_PLAY; }
|