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
ColumnLimit: '100'
AlignConsecutiveDeclarations: true
BinPackArguments: false
ColumnLimit: 100
...

View File

@ -1,64 +1,65 @@
#pragma once
#include <condition_variable>
#include <thread>
#include <fmt/format.h>
#include <mpd/client.h>
#include <condition_variable>
#include <thread>
#include "ALabel.hpp"
namespace waybar::modules {
class MPD : public ALabel {
public:
MPD(const std::string&, const Json::Value&);
auto update() -> void;
private:
std::thread periodic_updater();
void setLabel();
std::string getStateIcon();
std::string getOptionIcon(std::string optionName, bool activated);
public:
MPD(const std::string&, const Json::Value&);
auto update() -> void;
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
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);
std::thread event_listener();
// Assumes `connection_lock_` is locked
void fetchState();
void waitForEvent();
// 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);
bool handlePlayPause(GdkEventButton* const&);
// Assumes `connection_lock_` is locked
void fetchState();
void waitForEvent();
bool stopped();
bool playing();
bool handlePlayPause(GdkEventButton* const&);
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_song = std::unique_ptr<mpd_song, decltype(&mpd_song_free)>;
bool stopped();
bool playing();
// Not using unique_ptr since we don't manage the pointer
// (It's either nullptr, or from the config)
const char* server_;
const unsigned port_;
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_song = std::unique_ptr<mpd_song, decltype(&mpd_song_free)>;
// 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_;
// 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_;
// Not using unique_ptr since we don't manage the pointer
// (It's either nullptr, or from the config)
const char* server_;
const unsigned port_;
// Protect them using the `connection_lock_`
unique_status status_;
mpd_state state_;
unique_song song_;
// 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_;
// 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

View File

@ -1,10 +1,9 @@
#include "modules/mpd.hpp"
#include <fmt/chrono.h>
#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),
server_(nullptr),
port_(config["port"].asUInt()),
@ -24,8 +23,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
event_listener().detach();
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(
sigc::mem_fun(*this, &MPD::handlePlayPause));
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &MPD::handlePlayPause));
}
auto waybar::modules::MPD::update() -> void {
@ -77,14 +75,16 @@ void waybar::modules::MPD::setLabel() {
// In the case connection closed while MPD is stopped
label_.get_style_context()->remove_class("stopped");
auto format = config_["format-disconnected"].isString() ?
config_["format-disconnected"].asString() : "disconnected";
auto format = config_["format-disconnected"].isString()
? config_["format-disconnected"].asString()
: "disconnected";
label_.set_markup(format);
if (tooltipEnabled()) {
std::string tooltip_format;
tooltip_format = config_["tooltip-format-disconnected"].isString() ?
config_["tooltip-format-disconnected"].asString() : "MPD (disconnected)";
tooltip_format = config_["tooltip-format-disconnected"].isString()
? config_["tooltip-format-disconnected"].asString()
: "MPD (disconnected)";
// Nothing to format
label_.set_tooltip_text(tooltip_format);
}
@ -95,67 +95,67 @@ void waybar::modules::MPD::setLabel() {
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 = "";
if (stopped()) {
format = config_["format-stopped"].isString() ?
config_["format-stopped"].asString() : "stopped";
format =
config_["format-stopped"].isString() ? config_["format-stopped"].asString() : "stopped";
label_.get_style_context()->add_class("stopped");
} else {
label_.get_style_context()->remove_class("stopped");
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 = 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()));
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()));
}
bool consumeActivated = mpd_status_get_consume(status_.get());
bool consumeActivated = mpd_status_get_consume(status_.get());
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);
bool repeatActivated = mpd_status_get_repeat(status_.get());
bool repeatActivated = mpd_status_get_repeat(status_.get());
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);
// TODO: format can fail
label_.set_markup(fmt::format(format,
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)));
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)));
if (tooltipEnabled()) {
std::string tooltip_format;
tooltip_format = config_["tooltip-format"].isString() ?
config_["tooltip-format"].asString() : "MPD (connected)";
tooltip_format = config_["tooltip-format"].isString() ? config_["tooltip-format"].asString()
: "MPD (connected)";
auto tooltip_text = fmt::format(tooltip_format,
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));
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));
label_.set_tooltip_text(tooltip_text);
}
}
@ -204,13 +204,10 @@ void waybar::modules::MPD::tryConnect() {
return;
}
connection_ = unique_connection(
mpd_connection_new(server_, port_, 5'000),
&mpd_connection_free);
connection_ = unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
alternate_connection_ = unique_connection(
mpd_connection_new(server_, port_, 5'000),
&mpd_connection_free);
alternate_connection_ =
unique_connection(mpd_connection_new(server_, port_, 5'000), &mpd_connection_free);
if (connection_ == nullptr || alternate_connection_ == nullptr) {
std::cerr << "Failed to connect to MPD" << std::endl;
@ -258,8 +255,10 @@ void waybar::modules::MPD::fetchState() {
void waybar::modules::MPD::waitForEvent() {
auto conn = alternate_connection_.get();
// 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));
// 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));
checkErrors(conn);
}
@ -287,6 +286,4 @@ 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;
}
bool waybar::modules::MPD::playing() { return connection_ != nullptr && state_ == MPD_STATE_PLAY; }