From 160837b900524e11e2ce66e3f6dcc3dcd5e0255c Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Sat, 20 Apr 2019 09:12:30 -0700 Subject: [PATCH] Ensure no NULL tags are set Because `mpd_song_get_tag` from libmpdclient can return NULL, verify the value of tag is valid. Otherwise, set a default string of "N/A". Also adds configuration to specify what this default string should be. --- include/modules/mpd.hpp | 1 + resources/config | 1 + src/modules/mpd.cpp | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp index b3134f4..0574d8d 100644 --- a/include/modules/mpd.hpp +++ b/include/modules/mpd.hpp @@ -15,6 +15,7 @@ class MPD : public ALabel { private: std::thread periodic_updater(); + std::string getTag(mpd_tag_type type, unsigned idx = 0); void setLabel(); std::string getStateIcon(); std::string getOptionIcon(std::string optionName, bool activated); diff --git a/resources/config b/resources/config index 0fcb712..b2d8cc0 100644 --- a/resources/config +++ b/resources/config @@ -30,6 +30,7 @@ "format": "{stateIcon} {consumeIcon}{randomIcon}{repeatIcon}{singleIcon}{artist} - {album} - {title} ({elapsedTime:%M:%S}/{totalTime:%M:%S}) ", "format-disconnected": "Disconnected ", "format-stopped": "{consumeIcon}{randomIcon}{repeatIcon}{singleIcon}Stopped ", + "unknown-tag": "N/A", "interval": 2, "consume-icons": { "on": " " diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index bd9b095..1c43982 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -75,6 +75,17 @@ std::thread waybar::modules::MPD::periodic_updater() { }); } +std::string waybar::modules::MPD::getTag(mpd_tag_type type, unsigned idx) { + std::string result = config_["unknown-tag"].isString() ? config_["unknown-tag"].asString() : "N/A"; + const char* tag = mpd_song_get_tag(song_.get(), type, idx); + + // mpd_song_get_tag can return NULL, so make sure it's valid before setting + if (tag) + result = tag; + + return result; +} + void waybar::modules::MPD::setLabel() { if (connection_ == nullptr) { label_.get_style_context()->add_class("disconnected"); @@ -123,11 +134,11 @@ void waybar::modules::MPD::setLabel() { stateIcon = getStateIcon(); - 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 = 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); + artist = getTag(MPD_TAG_ARTIST); + album_artist = getTag(MPD_TAG_ALBUM_ARTIST); + album = getTag(MPD_TAG_ALBUM); + title = getTag(MPD_TAG_TITLE); + date = getTag(MPD_TAG_DATE); elapsedTime = std::chrono::seconds(mpd_status_get_elapsed_time(status_.get())); totalTime = std::chrono::seconds(mpd_status_get_total_time(status_.get())); }