2019-04-16 14:34:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <mpd/client.h>
|
2019-04-18 09:31:47 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <thread>
|
2019-04-16 14:34:37 +00:00
|
|
|
#include "ALabel.hpp"
|
|
|
|
|
|
|
|
namespace waybar::modules {
|
|
|
|
|
|
|
|
class MPD : public ALabel {
|
2019-04-18 09:31:47 +00:00
|
|
|
public:
|
|
|
|
MPD(const std::string&, const Json::Value&);
|
|
|
|
auto update() -> void;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::thread periodic_updater();
|
2019-04-20 16:12:30 +00:00
|
|
|
std::string getTag(mpd_tag_type type, unsigned idx = 0);
|
2019-04-18 09:31:47 +00:00
|
|
|
void setLabel();
|
|
|
|
std::string getStateIcon();
|
|
|
|
std::string getOptionIcon(std::string optionName, bool activated);
|
2019-04-17 11:55:08 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
std::thread event_listener();
|
2019-04-18 09:17:08 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
// 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);
|
2019-04-17 11:55:08 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
// Assumes `connection_lock_` is locked
|
|
|
|
void fetchState();
|
|
|
|
void waitForEvent();
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
bool handlePlayPause(GdkEventButton* const&);
|
2019-04-17 14:16:39 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
bool stopped();
|
|
|
|
bool playing();
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-18 09:48:38 +00:00
|
|
|
const std::string module_name_;
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
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)>;
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
// 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_;
|
2019-04-16 14:34:37 +00:00
|
|
|
|
2019-04-21 08:54:40 +00:00
|
|
|
unsigned timeout_;
|
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
// 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_;
|
2019-04-18 09:17:08 +00:00
|
|
|
|
2019-04-18 09:31:47 +00:00
|
|
|
// Protect them using the `connection_lock_`
|
|
|
|
unique_status status_;
|
|
|
|
mpd_state state_;
|
|
|
|
unique_song song_;
|
2019-09-19 19:43:12 +00:00
|
|
|
|
|
|
|
// To make sure the previous periodic_updater stops before creating a new one
|
|
|
|
std::mutex periodic_lock_;
|
2019-04-16 14:34:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace waybar::modules
|