chore: Add clang-format configuration and format MPD module

This commit is contained in:
Minijackson 2019-04-18 11:31:47 +02:00
parent 235997fa73
commit cd92b475ad
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
3 changed files with 100 additions and 101 deletions

View File

@ -1,5 +1,6 @@
--- ---
BasedOnStyle: Google BasedOnStyle: Google
ColumnLimit: '100' AlignConsecutiveDeclarations: true
BinPackArguments: false
ColumnLimit: 100
... ...

View File

@ -1,64 +1,65 @@
#pragma once #pragma once
#include <condition_variable>
#include <thread>
#include <fmt/format.h> #include <fmt/format.h>
#include <mpd/client.h> #include <mpd/client.h>
#include <condition_variable>
#include <thread>
#include "ALabel.hpp" #include "ALabel.hpp"
namespace waybar::modules { namespace waybar::modules {
class MPD : public ALabel { class MPD : public ALabel {
public: public:
MPD(const std::string&, const Json::Value&); MPD(const std::string&, const Json::Value&);
auto update() -> void; auto update() -> void;
private:
std::thread periodic_updater();
void setLabel();
std::string getStateIcon();
std::string getOptionIcon(std::string optionName, bool activated);
std::thread event_listener(); private:
std::thread periodic_updater();
void setLabel();
std::string getStateIcon();
std::string getOptionIcon(std::string optionName, bool activated);
// Assumes `connection_lock_` is locked std::thread event_listener();
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 // Assumes `connection_lock_` is locked
void fetchState(); void tryConnect();
void waitForEvent(); // If checking errors on the main connection, make sure to lock it using
// `connection_lock_` before calling checkErrors
void checkErrors(mpd_connection* conn);
bool handlePlayPause(GdkEventButton* const&); // Assumes `connection_lock_` is locked
void fetchState();
void waitForEvent();
bool stopped(); bool handlePlayPause(GdkEventButton* const&);
bool playing();
using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>; bool stopped();
using unique_status = std::unique_ptr<mpd_status, decltype(&mpd_status_free)>; bool playing();
using unique_song = std::unique_ptr<mpd_song, decltype(&mpd_song_free)>;
// Not using unique_ptr since we don't manage the pointer using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>;
// (It's either nullptr, or from the config) using unique_status = std::unique_ptr<mpd_status, decltype(&mpd_status_free)>;
const char* server_; using unique_song = std::unique_ptr<mpd_song, decltype(&mpd_song_free)>;
const unsigned port_;
// We need a mutex here because we can trigger updates from multiple thread: // Not using unique_ptr since we don't manage the pointer
// the event based updates, the periodic updates needed for the elapsed time, // (It's either nullptr, or from the config)
// and the click play/pause feature const char* server_;
std::mutex connection_lock_; const unsigned port_;
unique_connection connection_;
// The alternate connection will be used to wait for events: since it will
// 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_;
// Protect them using the `connection_lock_` // We need a mutex here because we can trigger updates from multiple thread:
unique_status status_; // the event based updates, the periodic updates needed for the elapsed time,
mpd_state state_; // and the click play/pause feature
unique_song song_; std::mutex connection_lock_;
unique_connection connection_;
// The alternate connection will be used to wait for events: since it will
// 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_;
// Protect them using the `connection_lock_`
unique_status status_;
mpd_state state_;
unique_song song_;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -1,10 +1,9 @@
#include "modules/mpd.hpp" #include "modules/mpd.hpp"
#include <fmt/chrono.h> #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)
: ALabel(config, "{album} - {artist} - {title}", 5), : ALabel(config, "{album} - {artist} - {title}", 5),
server_(nullptr), server_(nullptr),
port_(config["port"].asUInt()), port_(config["port"].asUInt()),
@ -24,8 +23,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
event_listener().detach(); 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(sigc::mem_fun(*this, &MPD::handlePlayPause));
sigc::mem_fun(*this, &MPD::handlePlayPause));
} }
auto waybar::modules::MPD::update() -> void { auto waybar::modules::MPD::update() -> void {
@ -77,14 +75,16 @@ void waybar::modules::MPD::setLabel() {
// In the case connection closed while MPD is stopped // In the case connection closed while MPD is stopped
label_.get_style_context()->remove_class("stopped"); label_.get_style_context()->remove_class("stopped");
auto format = config_["format-disconnected"].isString() ? auto format = config_["format-disconnected"].isString()
config_["format-disconnected"].asString() : "disconnected"; ? config_["format-disconnected"].asString()
: "disconnected";
label_.set_markup(format); label_.set_markup(format);
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
tooltip_format = config_["tooltip-format-disconnected"].isString() ? tooltip_format = config_["tooltip-format-disconnected"].isString()
config_["tooltip-format-disconnected"].asString() : "MPD (disconnected)"; ? config_["tooltip-format-disconnected"].asString()
: "MPD (disconnected)";
// Nothing to format // Nothing to format
label_.set_tooltip_text(tooltip_format); label_.set_tooltip_text(tooltip_format);
} }
@ -95,67 +95,67 @@ 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::chrono::seconds elapsedTime, totalTime;
std::string stateIcon = ""; std::string stateIcon = "";
if (stopped()) { if (stopped()) {
format = config_["format-stopped"].isString() ? format =
config_["format-stopped"].asString() : "stopped"; config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped";
label_.get_style_context()->add_class("stopped"); label_.get_style_context()->add_class("stopped");
} else { } else {
label_.get_style_context()->remove_class("stopped"); label_.get_style_context()->remove_class("stopped");
stateIcon = getStateIcon(); stateIcon = getStateIcon();
artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0); 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_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0);
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())); elapsedTime = std::chrono::seconds(mpd_status_get_elapsed_time(status_.get()));
totalTime = std::chrono::seconds(mpd_status_get_total_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());
std::string consumeIcon = getOptionIcon("consume", consumeActivated); std::string consumeIcon = getOptionIcon("consume", consumeActivated);
bool randomActivated = mpd_status_get_random(status_.get()); bool randomActivated = mpd_status_get_random(status_.get());
std::string randomIcon = getOptionIcon("random", randomActivated); std::string randomIcon = getOptionIcon("random", randomActivated);
bool repeatActivated = mpd_status_get_repeat(status_.get()); bool repeatActivated = mpd_status_get_repeat(status_.get());
std::string repeatIcon = getOptionIcon("repeat", repeatActivated); std::string repeatIcon = getOptionIcon("repeat", repeatActivated);
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 // TODO: format can fail
label_.set_markup(fmt::format(format, label_.set_markup(fmt::format(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("elapsedTime", elapsedTime),
fmt::arg("totalTime", totalTime), 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),
fmt::arg("repeatIcon", repeatIcon), fmt::arg("repeatIcon", repeatIcon),
fmt::arg("singleIcon", singleIcon))); fmt::arg("singleIcon", singleIcon)));
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
tooltip_format = config_["tooltip-format"].isString() ? tooltip_format = config_["tooltip-format"].isString() ? config_["tooltip-format"].asString()
config_["tooltip-format"].asString() : "MPD (connected)"; : "MPD (connected)";
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("stateIcon", stateIcon), fmt::arg("stateIcon", stateIcon),
fmt::arg("consumeIcon", consumeIcon), fmt::arg("consumeIcon", consumeIcon),
fmt::arg("randomIcon", randomIcon), fmt::arg("randomIcon", randomIcon),
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);
} }
} }
@ -204,13 +204,10 @@ void waybar::modules::MPD::tryConnect() {
return; return;
} }
connection_ = unique_connection( connection_ = unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
mpd_connection_new(server_, port_, 5'000),
&mpd_connection_free);
alternate_connection_ = unique_connection( alternate_connection_ =
mpd_connection_new(server_, port_, 5'000), unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
&mpd_connection_free);
if (connection_ == nullptr || alternate_connection_ == nullptr) { if (connection_ == nullptr || alternate_connection_ == nullptr) {
std::cerr << "Failed to connect to MPD" << std::endl; std::cerr << "Failed to connect to MPD" << std::endl;
@ -258,8 +255,10 @@ void waybar::modules::MPD::fetchState() {
void waybar::modules::MPD::waitForEvent() { void waybar::modules::MPD::waitForEvent() {
auto conn = alternate_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
mpd_run_idle_mask(conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST)); // change
mpd_run_idle_mask(conn,
static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST));
checkErrors(conn); checkErrors(conn);
} }
@ -287,6 +286,4 @@ bool waybar::modules::MPD::stopped() {
return connection_ == nullptr || state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP; return connection_ == nullptr || state_ == MPD_STATE_UNKNOWN || state_ == MPD_STATE_STOP;
} }
bool waybar::modules::MPD::playing() { bool waybar::modules::MPD::playing() { return connection_ != nullptr && state_ == MPD_STATE_PLAY; }
return connection_ != nullptr && state_ == MPD_STATE_PLAY;
}