Fix various mpd bugs

- Add elapsedTime and totalTime to tooltip format arguments
- Catch format exceptions and print error
- Copy mpd connection error message before it gets freed
- Update display after connection to mpd was lost
pull/895/head
Flakebi 2020-10-23 21:13:20 +02:00
parent 9ea13e790d
commit be3f47b374
No known key found for this signature in database
GPG Key ID: 38E7ED984D7DCD02
2 changed files with 43 additions and 32 deletions

View File

@ -144,7 +144,7 @@ void waybar::modules::MPD::setLabel() {
bool singleActivated = mpd_status_get_single(status_.get()); bool singleActivated = mpd_status_get_single(status_.get());
std::string singleIcon = getOptionIcon("single", singleActivated); std::string singleIcon = getOptionIcon("single", singleActivated);
// TODO: format can fail try {
label_.set_markup( label_.set_markup(
fmt::format(format, fmt::format(format,
fmt::arg("artist", Glib::Markup::escape_text(artist).raw()), fmt::arg("artist", Glib::Markup::escape_text(artist).raw()),
@ -161,17 +161,23 @@ void waybar::modules::MPD::setLabel() {
fmt::arg("randomIcon", randomIcon), fmt::arg("randomIcon", randomIcon),
fmt::arg("repeatIcon", repeatIcon), fmt::arg("repeatIcon", repeatIcon),
fmt::arg("singleIcon", singleIcon))); fmt::arg("singleIcon", singleIcon)));
} catch (fmt::format_error const& e) {
spdlog::warn("mpd: format error: {}", e.what());
}
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
tooltip_format = config_["tooltip-format"].isString() ? config_["tooltip-format"].asString() tooltip_format = config_["tooltip-format"].isString() ? config_["tooltip-format"].asString()
: "MPD (connected)"; : "MPD (connected)";
try {
auto tooltip_text = fmt::format(tooltip_format, auto tooltip_text = fmt::format(tooltip_format,
fmt::arg("artist", artist), fmt::arg("artist", artist),
fmt::arg("albumArtist", 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("elapsedTime", elapsedTime),
fmt::arg("totalTime", totalTime),
fmt::arg("songPosition", song_pos), fmt::arg("songPosition", song_pos),
fmt::arg("queueLength", queue_length), fmt::arg("queueLength", queue_length),
fmt::arg("stateIcon", stateIcon), fmt::arg("stateIcon", stateIcon),
@ -180,6 +186,9 @@ void waybar::modules::MPD::setLabel() {
fmt::arg("repeatIcon", repeatIcon), fmt::arg("repeatIcon", repeatIcon),
fmt::arg("singleIcon", singleIcon)); fmt::arg("singleIcon", singleIcon));
label_.set_tooltip_text(tooltip_text); label_.set_tooltip_text(tooltip_text);
} catch (fmt::format_error const& e) {
spdlog::warn("mpd: format error (tooltip): {}", e.what());
}
} }
} }
@ -269,8 +278,9 @@ void waybar::modules::MPD::checkErrors(mpd_connection* conn) {
default: default:
if (conn) { if (conn) {
auto error_message = mpd_connection_get_error_message(conn); auto error_message = mpd_connection_get_error_message(conn);
std::string error(error_message);
mpd_connection_clear_error(conn); mpd_connection_clear_error(conn);
throw std::runtime_error(std::string(error_message)); throw std::runtime_error(error);
} }
throw std::runtime_error("Invalid connection"); throw std::runtime_error("Invalid connection");
} }

View File

@ -341,6 +341,7 @@ void Disconnected::disarm_timer() noexcept {
} }
void Disconnected::entry() noexcept { void Disconnected::entry() noexcept {
ctx_->emit();
arm_timer(1'000); arm_timer(1'000);
} }